BGRToYUV420
Converts a BGR image into the YUV color model using the 4:2:0 sampling format.
This function converts a gamma-corrected R'G'B' image to a Y'U'V' image with the 4:2:0 sampling format, according to the following formulas:



For digital BGR values in the range [0, 255], Y' varies in the range [0, 255], U' varies in the range [-112, +112], and V' varies in the range [-157, +157]. To accommodate the range [0, 255], the constant value 128 is added to the calculated U and V, and then V is saturated.
The function interface declaration is as follows:
HmppResult HMPPI_BGRToYUV420_8u_AC4P3R(const uint8_t *src, int32_t srcStep, uint8_t *dst[3], int32_t dstStep[3], HmppiSize roiSize);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Pointer to the source buffer of pixel-order data. If the input is planar data, this parameter indicates an array of pointers to separate source color planes. |
The value cannot be NULL. |
Input |
srcStep |
Distance between starts of consecutive lines in the source image, in bytes |
The value must be zero or a positive integer. |
Input |
dst |
Pointer to the destination buffer of pixel-order data. If the input is planar data, this parameter indicates an array of pointers to separate destination color planes. |
The value cannot be NULL. |
Output |
dstStep |
Distance between starts of consecutive lines in the destination image, in bytes |
The value must be zero or a positive integer. |
Output |
roiSize |
Size of the ROI of the source and destination images, in pixels |
roiSize.width ∈ (0, INT_MAX], roiSize.height ∈ (0, INT_MAX] |
Input |
Return Value
- Success: HMPP_STS_NO_ERR
- Failure: An error code is returned.
Error Codes
Error Code |
Description |
|---|---|
HMPP_STS_NULL_PTR_ERR |
The value of src or dst is NULL. |
HMPP_STS_SIZE_ERR |
The value of roiSize is 0 or negative. |
HMPP_STS_STEP_ERR |
The value of srcStep or dstStep is zero or negative. |
HMPP_STS_ROI_ERR |
roiSize.width x Number of channels x Number of bytes occupied by the data type > Step |
HMPP_STS_DOUBLE_SIZE |
The width or height of roiSize is not a multiple of 2. |
Example
#define SRC_BUFFER_SIZE_T 90
#define DST_BUFFER_SIZE_T 90
void TestExample()
{
HmppiSize roi = { 4, 4 };
const uint8_t src[SRC_BUFFER_SIZE_T] = {
11, 22, 33, 44, 55, 66, 77, 88, 22, 88, 77, 66, 55, 44, 33,
11, 22, 33, 44, 55, 66, 77, 88, 22, 88, 77, 66, 55, 44, 33,
11, 22, 33, 44, 55, 66, 77, 88, 22, 88, 77, 66, 55, 44, 33,
11, 22, 33, 44, 55, 66, 77, 88, 22, 88, 77, 66, 55, 44, 33,
11, 22, 33, 44, 55, 66, 77, 88, 22, 88, 77, 66, 55, 44, 33,
11, 22, 33, 44, 55, 66, 77, 88, 22, 88, 77, 66, 55, 44, 33
};
uint8_t a[DST_BUFFER_SIZE_T], b[DST_BUFFER_SIZE_T], c[DST_BUFFER_SIZE_T];
uint8_t *dst[3] = {a, b, c};
int32_t srcStep = 20 * sizeof(uint8_t);
int32_t dstStep[3] = {12 * sizeof(uint8_t),12 * sizeof(uint8_t),12 * sizeof(uint8_t)};
HmppResult result = HMPPI_BGRToYUV420_8u_AC4P3R(src, srcStep, dst, dstStep, roi);
printf("result = %d \ndst = ", result);
if (result != HMPP_STS_NO_ERR) {
printf("result error: %d\n", result);
}
for(int32_t i = 0; i < 3; i++){
int32_t dstWidth = dstStep[i] / sizeof(uint8_t);
for (int32_t j = 0; j < DST_BUFFER_SIZE_T; j++) {
if( j % dstWidth == 0 ){
printf("\n");
}
printf("%3d ",dst[i][j]);
}
}
printf("\n");
}
int main()
{
TestExample();
return 0;
}
Output:
result = 0 dst = 24 68 77 41 0 0 0 0 104 17 137 186 79 74 27 46 0 0 0 0 0 0 0 0 63 16 57 49 0 0 0 0 224 95 137 186 24 68 77 41 224 30 227 233 255 255 0 0 46 78 61 246 0 0 0 0 88 161 135 186 255 255 0 0 24 0 0 0 0 0 0 0 255 255 255 255 0 0 0 0 4 0 0 0 0 0 0 0 56 245 124 123 69 186 255 255 0 0 136 58 69 186 128 126 0 0 80 162 88 186 255 255 0 0 96 125 69 186 255 255 0 0 80 51 137 186 255 255 0 0 0 0 0 0 0 0 0 0 224 95 137 186 255 255 0 0 80 51 137 186 255 255 0 0 221 4 64 0 0 0 0 0 224 8 70 186 255 255 0 0 224 3 64 0 0 0 0 0 0 0 132 124 0 0 1 0 0 0 1 0 0 0 131 136 0 0 0 0 0 0 0 0 0 0 1 0 0 0 255 255 0 0 104 17 137 186 255 255 0 0 96 31 227 233 255 255 0 0 200 214 134 186 255 255 0 0 224 107 137 186 255 255 0 0 0 0 0 0 0 0 0 0 144 30 227 233 255 255 0 0 48 138 72 186 255 255 0 0 28 231