Yuy2 is often used in TV systems and output formats of many cameras. during processing, we often need to convert it to RGB for processing. Here we will briefly introduce the Conversion Relationship Between yuy2 (YUV) and RGB:
Http://msdn2.microsoft.com/en-us/library/ms893078.aspx
Yuy2 (YUV) to RGB:
C = Y-16
D = u-128
E = V-128
R = clip(( 298 * C + 409 * E + 128) >> 8)
G = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8)
B = clip(( 298 * C + 516 * D + 128) >> 8)
Clip () is a restricted function and its value must be between 0 and.
RGB to yuy2 (YUV ):
Y = (66 * r + 129 * g + 25 * B + 128)> 8) + 16
U = (-38 * r-74 * g + 112 * B + 128)> 8) + 128
V = (112 * r-94 * g-18 * B + 128)> 8) + 128
In the code
Int yuv2rgb (void * pyuv, void * prgb, int width, int height, bool alphayuv, bool alphargb );
Int rgb2yuv (void * prgb, void * pyuvx, int width, int height, bool alphayuv, bool alphargb );
Function Conversion.
In data acquisition such as cameras, we often need to perform some image processing directly in the yuy2 (YUV) space.
(YUV) for some RGB processing. Here, we use blending as an example to overlay two yuy2 (YUV) images with transparency,
In order to achieve the effect of image synthesis in RGB space.
The RGB space overlays images. Generally, the background (BG) is not transparent, while the foreground (FG) is transparent. In the RGB space, it can be simply expressed:
Rdest = rfg * Alpha + rbg * (1-alpha );
Gdest = gfg * Alpha + GBG * (1-alpha );
Bdest = BFG * Alpha + BBG * (1-alpha );
// Rdest, gdest, and bdest are the final merged pixel values.
Considering
Y = (66 * r + 129 * g + 25 * B + 128)> 8) + 16
U = (-38 * r-74 * g + 112 * B + 128)> 8) + 128
V = (112 * r-94 * g-18 * B + 128)> 8) + 128
We can export
(Ydest-16) <8 = (Yfg-16) <8) * Alpha + (Ybg-16) <8) * (1-alpha );
(Udest-128) <8 = (Ufg-128) <8) * Alpha + (Ubg-128) <8) * (1-alpha );
(Vdest-128) <8 = (Vfg-128) <8) * Alpha + (Vbg-128) <8) * (1-alpha );
To obtain
Ydest = (Yfg-16) * Alpha + (Ybg-16) * (1-alpha) + 16;
Udest = (Ufg-128) * Alpha + (Ubg-128) * (1-alpha) + 128;
Vdest = (Vfg-128) * Alpha + (Vbg-128) * (1-alpha) + 128;
This superposition process is implemented in the function
Int yuvblending (void * pbgyuv, void * pfgyuv, int width, int height, bool alphabg, bool alphafg)
.
As this article processes the data collected by the camera, the data is in the yuy2 format, that is, four bytes to represent the YUV information of two pixels,
Arranged as Y1 U1 Y2 V2, where 1 is (Y1, u1, V1) and 2 is (Y2, u1, V1 ). That is, the two pixels share the U and V information.
Assume that the YUV format with Alpha transparency uses 6 bytes to represent the YUV and Alpha information of two pixels, arranged as Y1 U1 Y2 V1 alpha1 alpha2
Here, pixel 1 is (Y1, u1, V1, alpha1), and pixel 2 is (Y2, u1, V1, alpha2 ). Alpha indicates the transparency of the corresponding vertex.
For an image with Alpha transparency in RGB format, assume that it is a BMP image of 32bits, and each pixel is represented by 4 bytes, Which is B g r Alpha information respectively.
The specific implementation of the above functions is:
//////////////////////////////////////// /// // Yuv2rgb // pyuvpoint to the YUV data // prgbpoint to the RGB data // widthwidth of the picture // heightheight of the picture // alphayuvis there an alpha channel in YUV // alphargbis there an alpha channel in RGB /////////////////////////////////////// /// // int yuv2rgb (void * pyuv, void * prgb, int wid Th, int height, bool alphayuv, bool alphargb) {If (null = pyuv) {return-1;} unsigned char * pyuvdata = (unsigned char *) pyuv; unsigned char * prgbdata = (unsigned char *) prgb; If (null = prgbdata) {If (alphargb) {prgbdata = new unsigned char [width * height * 4];} elseprgbdata = new unsigned char [width * height * 3];} int Y1, u1, V1, Y2, alpha1, alpha2, R1, G1, B1, R2, G2, B2; int C1, D1, E1, C2; If (alphargb) {if (Lphayuv) {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {y1 = * (pyuvdata + I * width * 3 + J * 6); U1 = * (pyuvdata + I * width * 3 + J * 6 + 1 ); y2 = * (pyuvdata + I * width * 3 + J * 6 + 2); V1 = * (pyuvdata + I * width * 3 + J * 6 + 3 ); alpha1 = * (pyuvdata + I * width * 3 + J * 6 + 4); alpha2 = * (pyuvdata + I * width * 3 + J * 6 + 5 ); c1 = Y1-16; C2 = Y2-16; D1 = U1-128; e1 = V1-128; R1 = (298 * C1 + 409 * E1 + 128)> 8> 255? 255: (298 * C1 + 409 * E1 + 128)> 8); G1 = (298 * C1-100 * D1-208 * E1 + 128)> 8> 255? 255: (298 * C1-100 * D1-208 * E1 + 128)> 8); b1 = (298 * C1 + 516 * D1 + 128)> 8> 255? 255: (298 * C1 + 516 * D1 + 128)> 8); r2 = (298 * C2 + 409 * E1 + 128)> 8> 255? 255: (298 * C2 + 409 * E1 + 128)> 8); g2 = (298 * C2-100 * D1-208 * E1 + 128)> 8> 255? 255: (298 * C2-100 * D1-208 * E1 + 128)> 8); b2 = (298 * C2 + 516 * D1 + 128)> 8> 255? 255: (298 * C2 + 516 * D1 + 128)> 8); * (prgbdata + (height-i-1) * width * 4 + J * 8 + 2) = R1 <0? 0: R1; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 1) = G1 <0? 0: G1; * (prgbdata + (height-i-1) * width * 4 + J * 8) = b1 <0? 0: B1; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 3) = alpha1; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 6) = R2 <0? 0: R2; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 5) = G2 <0? 0: g2; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 4) = b2 <0? 0: B2; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 7) = alpha2 ;}} else {int alpha = 255; for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {y1 = * (pyuvdata + I * width * 2 + J * 4); U1 = * (pyuvdata + I * width * 2 + J * 4 + 1 ); y2 = * (pyuvdata + I * width * 2 + J * 4 + 2); V1 = * (pyuvdata + I * width * 2 + J * 4 + 3 ); c1 = Y1-16; C2 = Y2-16; D1 = U1-128; e1 = V1-128; R1 = (298 * C1 + 409 * E1 + 128)> 8> 255? 255: (298 * C1 + 409 * E1 + 128)> 8); G1 = (298 * C1-100 * D1-208 * E1 + 128)> 8> 255? 255: (298 * C1-100 * D1-208 * E1 + 128)> 8); b1 = (298 * C1 + 516 * D1 + 128)> 8> 255? 255: (298 * C1 + 516 * D1 + 128)> 8); r2 = (298 * C2 + 409 * E1 + 128)> 8> 255? 255: (298 * C2 + 409 * E1 + 128)> 8); g2 = (298 * C2-100 * D1-208 * E1 + 128)> 8> 255? 255: (298 * C2-100 * D1-208 * E1 + 128)> 8); b2 = (298 * C2 + 516 * D1 + 128)> 8> 255? 255: (298 * C2 + 516 * D1 + 128)> 8); * (prgbdata + (height-i-1) * width * 4 + J * 8 + 2) = R1 <0? 0: R1; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 1) = G1 <0? 0: G1; * (prgbdata + (height-i-1) * width * 4 + J * 8) = b1 <0? 0: B1; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 3) = Alpha; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 6) = R2 <0? 0: R2; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 5) = G2 <0? 0: g2; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 4) = b2 <0? 0: B2; * (prgbdata + (height-i-1) * width * 4 + J * 8 + 7) = Alpha ;}}} else {If (alphayuv) {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {y1 = * (pyuvdata + I * width * 3 + J * 4); U1 = * (pyuvdata + I * width * 3 + J * 4 + 1 ); y2 = * (pyuvdata + I * width * 3 + J * 4 + 2); V1 = * (pyuvdata + I * width * 3 + J * 4 + 3 ); c1 = Y1-16; C2 = Y2-16; D1 = U1-128; e1 = V1-128; R1 = (298 * C1 + 409 * E1 + 128)> 8> 255? 255: (298 * C1 + 409 * E1 + 128)> 8); G1 = (298 * C1-100 * D1-208 * E1 + 128)> 8> 255? 255: (298 * C1-100 * D1-208 * E1 + 128)> 8); b1 = (298 * C1 + 516 * D1 + 128)> 8> 255? 255: (298 * C1 + 516 * D1 + 128)> 8); r2 = (298 * C2 + 409 * E1 + 128)> 8> 255? 255: (298 * C2 + 409 * E1 + 128)> 8); g2 = (298 * C2-100 * D1-208 * E1 + 128)> 8> 255? 255: (298 * C2-100 * D1-208 * E1 + 128)> 8); b2 = (298 * C2 + 516 * D1 + 128)> 8> 255? 255: (298 * C2 + 516 * D1 + 128)> 8); * (prgbdata + (height-i-1) * width * 3 + J * 6 + 2) = R1 <0? 0: R1; * (prgbdata + (height-i-1) * width * 3 + J * 6 + 1) = G1 <0? 0: G1; * (prgbdata + (height-i-1) * width * 3 + J * 6) = b1 <0? 0: B1; * (prgbdata + (height-i-1) * width * 3 + J * 6 + 5) = R2 <0? 0: R2; * (prgbdata + (height-i-1) * width * 3 + J * 6 + 4) = G2 <0? 0: g2; * (prgbdata + (height-i-1) * width * 3 + J * 6 + 3) = b2 <0? 0: B2 ;}}else {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {y1 = * (pyuvdata + I * width * 2 + J * 4 ); u1 = * (pyuvdata + I * width * 2 + J * 4 + 1); y2 = * (pyuvdata + I * width * 2 + J * 4 + 2 ); v1 = * (pyuvdata + I * width * 2 + J * 4 + 3); C1 = Y1-16; C2 = Y2-16; D1 = U1-128; e1 = V1-128; r1 = (298 * C1 + 409 * E1 + 128)> 8> 255? 255: (298 * C1 + 409 * E1 + 128)> 8); G1 = (298 * C1-100 * D1-208 * E1 + 128)> 8> 255? 255: (298 * C1-100 * D1-208 * E1 + 128)> 8); b1 = (298 * C1 + 516 * D1 + 128)> 8> 255? 255: (298 * C1 + 516 * D1 + 128)> 8); r2 = (298 * C2 + 409 * E1 + 128)> 8> 255? 255: (298 * C2 + 409 * E1 + 128)> 8); g2 = (298 * C2-100 * D1-208 * E1 + 128)> 8> 255? 255: (298 * C2-100 * D1-208 * E1 + 128)> 8); b2 = (298 * C2 + 516 * D1 + 128)> 8> 255? 255: (298 * C2 + 516 * D1 + 128)> 8); * (prgbdata + (height-i-1) * width * 3 + J * 6 + 2) = R1 <0? 0: R1; * (prgbdata + (height-i-1) * width * 3 + J * 6 + 1) = G1 <0? 0: G1; * (prgbdata + (height-i-1) * width * 3 + J * 6) = b1 <0? 0: B1; * (prgbdata + (height-i-1) * width * 3 + J * 6 + 5) = R2 <0? 0: R2; * (prgbdata + (height-i-1) * width * 3 + J * 6 + 4) = G2 <0? 0: g2; * (prgbdata + (height-i-1) * width * 3 + J * 6 + 3) = b2 <0? 0: B2 ;}}} return 0 ;} //////////////////////////////////////// /// // rgb2yuv // prgbpoint to the RGB data // pyuvpoint to the YUV data // widthwidth of the picture // heightheight of the picture // alphayuvis there an alpha channel in YUV // alphargbis there an alpha channel in RGB /////////////////////////////////////// /// // int rgb2yuv (void * PRG B, void * pyuv, int width, int height, bool alphayuv, bool alphargb) {If (null = prgb) {return-1 ;} unsigned char * prgbdata = (unsigned char *) prgb; unsigned char * pyuvdata = (unsigned char *) pyuv; If (null = pyuvdata) {If (alphayuv) {pyuvdata = new unsigned char [width * height * 3];} elsepyuvdata = new unsigned char [width * height * 2];} int R1, G1, B1, R2, G2, b2, Y1, u1, Y2, V1; int alpha1, alpha2; If (alphayuv) {If (alphargb) {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {b1 = * (prgbdata + (height-i-1) * width * 4 + J * 8); G1 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 1); R1 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 2 ); alpha1 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 3); b2 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 4); g2 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 5 ); r2 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 6); alpha2 = * (prgbd ATA + (height-i-1) * width * 4 + J * 8 + 7); Y1 = (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16) & gt; 255? 255: (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16 ); u1 = (-38 * R1-74 * G1 + 112 * B1 + 128)> 8) + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8)/2 + 128)> 255? 255: (-38 * R1-74 * G1 + 112 * B1 + 128)> 8) + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8)/2 + 128 ); y2 = (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16)> 255? 255: (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16; v1 = (112 * R1-94 * G1-18 * B1 + 128)> 8) + (112 * R2-94 * G2-18 * B2 + 128)> 8 )) /2 + 128)> 255? 255: (112 * R1-94 * G1-18 * B1 + 128)> 8) + (112 * R2-94 * G2-18 * B2 + 128)> 8 )) /2 + 128); * (pyuvdata + I * width * 3 + J * 6) = Y1; * (pyuvdata + I * width * 3 + J * 6 + 1) = U1; * (pyuvdata + I * width * 3 + J * 6 + 2) = Y2; * (pyuvdata + I * width * 3 + J * 6 + 3) = V1; * (pyuvdata + I * width * 3 + J * 6 + 4) = alpha1; * (pyuvdata + I * width * 3 + J * 6 + 5) = alpha2 ;}}else {unsigned char alpha = 255; For (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {b1 = * (prgbdata + (height -I-1) * width * 3 + J * 6); G1 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 1 ); r1 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 2); b2 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 3); g2 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 4 ); r2 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 5 ); y1 = (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16; u1 = (-38 * R1-74 * G1 + 112 * B1 + 128)> 8 + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8) /2 + 128; y2 = (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16; V1 = (1 12 * R1-94 * G1-18 * B1 + 128)> 8 + (112 * R2-94 * G2-18 * B2 + 128)> 8)/2 + 128; y1 = (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16)> 255? 255: (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16 ); u1 = (-38 * R1-74 * G1 + 112 * B1 + 128)> 8) + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8)/2 + 128)> 255? 255: (-38 * R1-74 * G1 + 112 * B1 + 128)> 8) + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8)/2 + 128 ); y2 = (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16)> 255? 255: (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16; v1 = (112 * R1-94 * G1-18 * B1 + 128)> 8) + (112 * R2-94 * G2-18 * B2 + 128)> 8 )) /2 + 128)> 255? 255: (112 * R1-94 * G1-18 * B1 + 128)> 8) + (112 * R2-94 * G2-18 * B2 + 128)> 8 )) /2 + 128); * (pyuvdata + I * width * 3 + J * 6) = Y1; * (pyuvdata + I * width * 3 + J * 6 + 1) = U1; * (pyuvdata + I * width * 3 + J * 6 + 2) = Y2; * (pyuvdata + I * width * 3 + J * 6 + 3) = V1; * (pyuvdata + I * width * 3 + J * 6 + 4) = Alpha; * (pyuvdata + I * width * 3 + J * 6 + 5) = Alpha ;}}} else {If (alphargb) {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {b1 = * (prgbdata + (height-i-1) * width * 4 + J * 8); G1 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 1); R1 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 2); b2 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 4 ); g2 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 5); r2 = * (prgbdata + (height-i-1) * width * 4 + J * 8 + 6); Y1 = (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16)> 255? 255: (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16 ); u1 = (-38 * R1-74 * G1 + 112 * B1 + 128)> 8) + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8)/2 + 128)> 255? 255: (-38 * R1-74 * G1 + 112 * B1 + 128)> 8) + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8)/2 + 128 ); y2 = (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16)> 255? 255: (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16; v1 = (112 * R1-94 * G1-18 * B1 + 128)> 8) + (112 * R2-94 * G2-18 * B2 + 128)> 8 )) /2 + 128)> 255? 255: (112 * R1-94 * G1-18 * B1 + 128)> 8) + (112 * R2-94 * G2-18 * B2 + 128)> 8 )) /2 + 128); * (pyuvdata + I * width * 2 + J * 4) = Y1; * (pyuvdata + I * width * 2 + J * 4 + 1) = U1; * (pyuvdata + I * width * 2 + J * 4 + 2) = Y2; * (pyuvdata + I * width * 2 + J * 4 + 3) = V1 ;}} else {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; + + J) {b1 = * (prgbdata + (height-i-1) * width * 3 + J * 6); G1 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 1); R1 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 2); b2 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 3); g2 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 4); r2 = * (prgbdata + (height-i-1) * width * 3 + J * 6 + 5 ); y1 = (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16)> 255? 255: (66 * R1 + 129 * G1 + 25 * B1 + 128)> 8) + 16 ); u1 = (-38 * R1-74 * G1 + 112 * B1 + 128)> 8) + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8)/2 + 128)> 255? 255: (-38 * R1-74 * G1 + 112 * B1 + 128)> 8) + (-38 * R2-74 * G2 + 112 * B2 + 128)> 8)/2 + 128 ); y2 = (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16)> 255? 255: (66 * r2 + 129 * G2 + 25 * B2 + 128)> 8) + 16; v1 = (112 * R1-94 * G1-18 * B1 + 128)> 8) + (112 * R2-94 * G2-18 * B2 + 128)> 8 )) /2 + 128)> 255? 255: (112 * R1-94 * G1-18 * B1 + 128)> 8) + (112 * R2-94 * G2-18 * B2 + 128)> 8 )) /2 + 128); * (pyuvdata + I * width * 2 + J * 4) = Y1; * (pyuvdata + I * width * 2 + J * 4 + 1) = U1; * (pyuvdata + I * width * 2 + J * 4 + 2) = Y2; * (pyuvdata + I * width * 2 + J * 4 + 3) = V1 ;}}} return 0 ;} //////////////////////////////////////// /// // pgbyuvpoint to the background YUV data // pfgyuvpoint to the foreground YUV data // widthwidth of Picture // heightheight of the picture // alphabgis there an alpha channel in background YUV data // alphafgis there an alpha channel in fourground YUV data /////////// //////////////////////////////////////// //// // int yuvblending (void * pbgyuv, void * pfgyuv, int width, int height, bool alphabg, bool alphafg) {If (null = pbgyuv | null = pfgyuv) {return-1 ;} unsigned char * pbgdata = (UN Signed char *) pbgyuv; unsigned char * pfgdata = (unsigned char *) pfgyuv; If (! Alphafg) {If (! Alphabg) {memcpy (pbgdata, pfgdata, width * height * 2);} else {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {* (pbgdata + I * width * 2 + J * 4) = * (pfgdata + I * width * 2 + J * 4); * (pbgdata + I * width * 2 + J * 4 + 1) = * (pfgdata + I * width * 2 + J * 4 + 1); * (pbgdata + I * width * 2 + J * 4 + 2) = * (pfgdata + I * width * 2 + J * 4 + 2); * (pbgdata + I * width * 2 + J * 4 + 3) = * (pfgdata + I * width * 2 + J * 4 + 3) ;}}} int y11, u11, V11, Y12, Y21, u21, V21, y22; int alpha1, alpha2; If (! Alphabg) {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {y11 = * (pbgdata + I * width * 2 + J * 4); u11 = * (pbgdata + I * width * 2 + J * 4 + 1 ); y12 = * (pbgdata + I * width * 2 + J * 4 + 2); V11 = * (pbgdata + I * width * 2 + J * 4 + 3 ); y21 = * (pfgdata + I * width * 3 + J * 6); int32 = * (pfgdata + I * width * 3 + J * 6 + 1 ); y22 = * (pfgdata + I * width * 3 + J * 6 + 2); V21 = * (pfgdata + I * width * 3 + J * 6 + 3 ); alpha1 = * (pfgdata + I * width * 3 + J * 6 + 4); alpha2 = * (pfgdata + I * width * 3 + J * 6 + 5 ); * (pbgdata + I * width * 2 + J * 4) = (Y21-16) * alpha1/255 + (Y11-16) * (255-alpha1)/255 + 16; * (pbgdata + I * width * 2 + J * 4 + 1) = (U21-128) * alpha1/255 + (U11-128) * (255-alpha1)/255 + (U21-128) * alpha2/255 + (U11-128) * (255-alpha2)/255)/2 + 128; * (pbgdata + I * width * 2 + J * 4 + 3) = (V21-128) * alpha1/255 + (V11-128) * (255-alpha1)/255 + (V21-128) * alpha2/255 + (V11-128) * (255-alpha2)/255) /2 + 128; * (pbgdata + I * width * 2 + J * 4 + 2) = (Y22-16) * alpha2/255 + (Y12-16) * (255-alpha2) /255 + 16 ;}} else {for (INT I = 0; I <peight; ++ I) {for (Int J = 0; j <width/2; ++ J) {y11 = * (pbgdata + I * width * 3 + J * 6 ); u11 = * (pbgdata + I * width * 3 + J * 6 + 1); Y12 = * (pbgdata + I * width * 3 + J * 6 + 2 ); v11 = * (pbgdata + I * width * 3 + J * 6 + 3); Y21 = * (pfgdata + I * width * 3 + J * 6 ); 21 = * (pfgdata + I * width * 3 + J * 6 + 1); y22 = * (pfgdata + I * width * 3 + J * 6 + 2 ); v21 = * (pfgdata + I * width * 3 + J * 6 + 3); alpha1 = * (pfgdata + I * width * 3 + J * 6 + 4 ); alpha2 = * (pfgdata + I * width * 3 + J * 6 + 5); * (pbgdata + I * width * 3 + J * 6) = (Y21-16) * alpha1/255 + (Y11-16) * (255-alpha1)/255 + 16; * (pbgdata + I * width * 3 + J * 6 + 1) = (U21-128) * alpha1/255 + (U11-128) * (255-alpha1)/255 + (U21-128) * alpha2/255 + (U11-128) * (255-alpha2)/255)/2 + 128; * (pbgdata + I * width * 3 + J * 6 + 3) = (V21-128) * alpha1/255 + (V11-128) * (255-alpha1)/255 + (V21-128) * alpha2/255 + (V11-128) * (255-alpha2)/255)/2 + 128; * (pbgdata + I * width * 3 + J * 6 + 2) = (Y22-16) * alpha2/255 + (Y12-16) * (255-alpha2)/255 + 16;} return 0 ;}
After testing, the function has been implemented. If any errors or exceptions occur, please point out.
Mosesyuan at gmail dot com