RGBToYUV
Converts an RGB image to the YUV color model.
This function converts a gamma-corrected R'G'B' image src to a Y'U'V' image dst according to the following formulas:
Y' = 0.299*R' + 0.587*G' + 0.114*B'
U' = -0.147*R' - 0.289*G' + 0.436*B' = 0.492*(B'-Y')
V' = 0.615*R' - 0.515*G' - 0.100*B' = 0.877*(R'-Y')
For digital RGB 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:
- Operation on pixel-order data:
HmppResult HMPPI_RGBToYUV_8u_C3R(const uint8_t *src, int32_t srcStep, uint8_t *dst, int32_t dstStep, HmppiSize roiSize);
HmppResult HMPPI_RGBToYUV_8u_AC4R(const uint8_t *src, int32_t srcStep, uint8_t *dst, int32_t dstStep, HmppiSize roiSize);
- Operation on planar data:
HmppResult HMPPI_RGBToYUV_8u_P3R(const uint8_t *src[3], int32_t srcStep, uint8_t *dst[3], int32_t dstStep, HmppiSize roiSize);
- Conversion from pixel-order to planar data:
HmppResult HMPPI_RGBToYUV_8u_C3P3R(const uint8_t *src, int32_t srcStep, uint8_t *dst[3], int32_t dstStep, HmppiSize roiSize);
HmppResult HMPPI_RGBToYUV_8u_AC4P4R(const uint8_t *src, int32_t srcStep, uint8_t *dst[4], int32_t dstStep, HmppiSize roiSize);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Pointer to the source image. An array of pointers to the color planes of the source planar image. |
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 image ROI. An array of pointers to the color planes of the destination planar image. |
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. |
Input |
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 is NULL. |
HMPP_STS_SIZE_ERR |
The value of roiSize is 0 or negative. |
HMPP_STS_STEP_ERR |
The value of srcStep is zero or negative. |
HMPP_STS_ROI_ERR |
roiSize.width x Number of channels x Number of bytes occupied by the data type > Step |
Example
#define SRC_BUFFER_SIZE_T 90
#define DST_BUFFER_SIZE_T 90
void TestExample_RGBToYUV()
{
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
};
int32_t srcStep = 20 * sizeof(uint8_t);
int32_t dstStep = 15 * sizeof(uint8_t);;
uint8_t dst[DST_BUFFER_SIZE_T] = {0};
HmppResult result = HMPPI_RGBToYUV_8u_C3R(src, srcStep, dst, dstStep, roi);
printf("result = %d \ndst = ", result);
if (result != HMPP_STS_NO_ERR) {
printf("result error: %d\n", result);
}
int32_t dstWidth = dstStep / sizeof(uint8_t);
for(int32_t i = 0; i < DST_BUFFER_SIZE_T; i++){
if (i % dstWidth == 0) {
printf("\n");
}
printf("%3d ",dst[i]);
}
printf("\n");
}
int main()
{
TestExample_RGBToYUV();
return 0;
}
Output:
result = 0 dst = 19 134 120 52 134 120 77 100 128 79 121 135 0 0 0 74 134 120 67 132 88 57 121 135 18 129 140 0 0 0 68 121 135 33 116 136 30 134 120 63 134 120 0 0 0 19 134 120 52 134 120 77 100 128 79 121 135 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0