RGB 24和YUY2相互转换

YUY2经常用于电视制式以及许多摄像头的输出格式.而我们在处理时经常需要将其转化为RGB进行处理,这里简单介绍下YUY2(YUV)与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()为限制函数,将其取值限制在0-255之间.

 

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上述两个公式在代码中的
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);
函数中转换。在诸如摄像头的数据获取中,我们往往需要直接在YUY2(YUV)空间上进行一些图象处理,我们希望能够在YUY2
(YUV)进行一些RGB上可以做到的处理。这里已blending为例,将两张带有透明度的YUY2(YUV)图片进行叠加,
以达到在RGB空间进行图像合成的效果。RGB空间进行图像叠加,通常背景(BG)是不透明的,而前景(FG)是带有透明度的。在RGB空间,可以简单表示为:
Rdest = Rfg*alpha + Rbg*(1-alpha);
Gdest = Gfg*alpha + Gbg*(1-alpha);
Bdest = Bfg*alpha + Bbg*(1-alpha);
// Rdest、Gdest、Bdest 为最终合成后的像素值考虑到
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
我们可以推导出(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);从而可以得到
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;这个叠加过程在函数
int YUVBlending(void* pBGYUV, void* pFGYUV, int width, int height, bool alphaBG, bool alphaFG)
中实现。由于本文针对摄像头采集所得的数据进行处理,因此数据为YUY2格式,即4个字节来表示两个像素点的YUV信息,
排列为Y1 U1 Y2 V2, 对于像素点1为(Y1, U1, V1),像素点2为(Y2, U1, V1)。即两个像素点共用U、V信息。这里假设带有alpha透明度的YUV格式用6个字节来表示两个像素点的YUV以及alpha信息,排列为 Y1 U1 Y2 V1 alpha1 alpha2
其中像素点1为(Y1, U1, V1, alpha1),像素点2为(Y2, U1, V1, alpha2)。其中alpha为对应点的透明度信息。而带有alpha透明度RGB格式的图片,假设为32bits的BMP图片,每个像素点用4bytes来表示,分别为B G R alpha信息。上述函数的具体实现为:
view plaincopy to clipboardprint?
  1. //   
  2. // YUV2RGB   
  3. // pYUV         point to the YUV data   
  4. // pRGB         point to the RGB data   
  5. // width        width of the picture   
  6. // height       height of the picture   
  7. // alphaYUV     is there an alpha channel in YUV   
  8. // alphaRGB     is there an alpha channel in RGB   
  9. //   
  10. int YUV2RGB(void* pYUV, void* pRGB, int width, int height, bool alphaYUV, bool alphaRGB)  
  11. {  
  12.     if (NULL == pYUV)  
  13.     {  
  14.         return -1;  
  15.     }  
  16.     unsigned char* pYUVData = (unsigned char *)pYUV;  
  17.     unsigned char* pRGBData = (unsigned char *)pRGB;  
  18.     if (NULL == pRGBData)  
  19.     {  
  20.         if (alphaRGB)  
  21.         {  
  22.             pRGBData = new unsigned char[width*height*4];  
  23.         }  
  24.         else  
  25.             pRGBData = new unsigned char[width*height*3];  
  26.     }  
  27.     int Y1, U1, V1, Y2, alpha1, alpha2, R1, G1, B1, R2, G2, B2;  
  28.     int C1, D1, E1, C2;  
  29.     if (alphaRGB)  
  30.     {  
  31.         if (alphaYUV)  
  32.         {  
  33.             for (int i=0; i<height; ++i)  
  34.             {  
  35.                 for (int j=0; j<width/2; ++j)  
  36.                 {  
  37.                     Y1 = *(pYUVData+i*width*3+j*6);  
  38.                     U1 = *(pYUVData+i*width*3+j*6+1);  
  39.                     Y2 = *(pYUVData+i*width*3+j*6+2);  
  40.                     V1 = *(pYUVData+i*width*3+j*6+3);  
  41.                     alpha1 = *(pYUVData+i*width*3+j*6+4);  
  42.                     alpha2 = *(pYUVData+i*width*3+j*6+5);  
  43.                     C1 = Y1-16;  
  44.                     C2 = Y2-16;  
  45.                     D1 = U1-128;  
  46.                     E1 = V1-128;  
  47.                     R1 = ((298*C1 + 409*E1 + 128)>>8>255 ? 255 : (298*C1 + 409*E1 + 128)>>8);  
  48.                     G1 = ((298*C1 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C1 - 100*D1 - 208*E1 + 128)>>8);    
  49.                     B1 = ((298*C1+516*D1 +128)>>8>255 ? 255 : (298*C1+516*D1 +128)>>8);    
  50.                     R2 = ((298*C2 + 409*E1 + 128)>>8>255 ? 255 : (298*C2 + 409*E1 + 128)>>8);  
  51.                     G2 = ((298*C2 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C2 - 100*D1 - 208*E1 + 128)>>8);  
  52.                     B2 = ((298*C2 + 516*D1 +128)>>8>255 ? 255 : (298*C2 + 516*D1 +128)>>8);    
  53.                     *(pRGBData+(height-i-1)*width*4+j*8+2) = R1<0 ? 0 : R1;  
  54.                     *(pRGBData+(height-i-1)*width*4+j*8+1) = G1<0 ? 0 : G1;  
  55.                     *(pRGBData+(height-i-1)*width*4+j*8) = B1<0 ? 0 : B1;  
  56.                     *(pRGBData+(height-i-1)*width*4+j*8+3) = alpha1;      
  57.                     *(pRGBData+(height-i-1)*width*4+j*8+6) = R2<0 ? 0 : R2;  
  58.                     *(pRGBData+(height-i-1)*width*4+j*8+5) = G2<0 ? 0 : G2;  
  59.                     *(pRGBData+(height-i-1)*width*4+j*8+4) = B2<0 ? 0 : B2;  
  60.                     *(pRGBData+(height-i-1)*width*4+j*8+7) = alpha2;      
  61.                 }  
  62.             }     
  63.         }  
  64.         else  
  65.         {  
  66.             int alpha = 255;  
  67.             for (int i=0; i<height; ++i)  
  68.             {  
  69.                 for (int j=0; j<width/2; ++j)  
  70.                 {  
  71.                     Y1 = *(pYUVData+i*width*2+j*4);  
  72.                     U1 = *(pYUVData+i*width*2+j*4+1);  
  73.                     Y2 = *(pYUVData+i*width*2+j*4+2);  
  74.                     V1 = *(pYUVData+i*width*2+j*4+3);  
  75.                     C1 = Y1-16;  
  76.                     C2 = Y2-16;  
  77.                     D1 = U1-128;  
  78.                     E1 = V1-128;  
  79.                     R1 = ((298*C1 + 409*E1 + 128)>>8>255 ? 255 : (298*C1 + 409*E1 + 128)>>8);  
  80.                     G1 = ((298*C1 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C1 - 100*D1 - 208*E1 + 128)>>8);    
  81.                     B1 = ((298*C1+516*D1 +128)>>8>255 ? 255 : (298*C1+516*D1 +128)>>8);    
  82.                     R2 = ((298*C2 + 409*E1 + 128)>>8>255 ? 255 : (298*C2 + 409*E1 + 128)>>8);  
  83.                     G2 = ((298*C2 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C2 - 100*D1 - 208*E1 + 128)>>8);  
  84.                     B2 = ((298*C2 + 516*D1 +128)>>8>255 ? 255 : (298*C2 + 516*D1 +128)>>8);    
  85.                     *(pRGBData+(height-i-1)*width*4+j*8+2) = R1<0 ? 0 : R1;  
  86.                     *(pRGBData+(height-i-1)*width*4+j*8+1) = G1<0 ? 0 : G1;  
  87.                     *(pRGBData+(height-i-1)*width*4+j*8) = B1<0 ? 0 : B1;  
  88.                     *(pRGBData+(height-i-1)*width*4+j*8+3) = alpha;   
  89.                     *(pRGBData+(height-i-1)*width*4+j*8+6) = R2<0 ? 0 : R2;  
  90.                     *(pRGBData+(height-i-1)*width*4+j*8+5) = G2<0 ? 0 : G2;  
  91.                     *(pRGBData+(height-i-1)*width*4+j*8+4) = B2<0 ? 0 : B2;  
  92.                     *(pRGBData+(height-i-1)*width*4+j*8+7) = alpha;   
  93.                 }  
  94.             }     
  95.         }  
  96.     }  
  97.     else  
  98.     {  
  99.         if (alphaYUV)  
  100.         {  
  101.             for (int i=0; i<height; ++i)  
  102.             {  
  103.                 for (int j=0; j<width/2; ++j)  
  104.                 {  
  105.                     Y1 = *(pYUVData+i*width*3+j*4);  
  106.                     U1 = *(pYUVData+i*width*3+j*4+1);  
  107.                     Y2 = *(pYUVData+i*width*3+j*4+2);  
  108.                     V1 = *(pYUVData+i*width*3+j*4+3);  
  109.                     C1 = Y1-16;  
  110.                     C2 = Y2-16;  
  111.                     D1 = U1-128;  
  112.                     E1 = V1-128;  
  113.                     R1 = ((298*C1 + 409*E1 + 128)>>8>255 ? 255 : (298*C1 + 409*E1 + 128)>>8);  
  114.                     G1 = ((298*C1 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C1 - 100*D1 - 208*E1 + 128)>>8);    
  115.                     B1 = ((298*C1+516*D1 +128)>>8>255 ? 255 : (298*C1+516*D1 +128)>>8);    
  116.                     R2 = ((298*C2 + 409*E1 + 128)>>8>255 ? 255 : (298*C2 + 409*E1 + 128)>>8);  
  117.                     G2 = ((298*C2 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C2 - 100*D1 - 208*E1 + 128)>>8);  
  118.                     B2 = ((298*C2 + 516*D1 +128)>>8>255 ? 255 : (298*C2 + 516*D1 +128)>>8);    
  119.                     *(pRGBData+(height-i-1)*width*3+j*6+2) = R1<0 ? 0 : R1;  
  120.                     *(pRGBData+(height-i-1)*width*3+j*6+1) = G1<0 ? 0 : G1;  
  121.                     *(pRGBData+(height-i-1)*width*3+j*6) = B1<0 ? 0 : B1;  
  122.                     *(pRGBData+(height-i-1)*width*3+j*6+5) = R2<0 ? 0 : R2;  
  123.                     *(pRGBData+(height-i-1)*width*3+j*6+4) = G2<0 ? 0 : G2;  
  124.                     *(pRGBData+(height-i-1)*width*3+j*6+3) = B2<0 ? 0 : B2;  
  125.                 }  
  126.             }  
  127.         }  
  128.         else  
  129.         {  
  130.             for (int i=0; i<height; ++i)  
  131.             {  
  132.                 for (int j=0; j<width/2; ++j)  
  133.                 {  
  134.                     Y1 = *(pYUVData+i*width*2+j*4);  
  135.                     U1 = *(pYUVData+i*width*2+j*4+1);  
  136.                     Y2 = *(pYUVData+i*width*2+j*4+2);  
  137.                     V1 = *(pYUVData+i*width*2+j*4+3);  
  138.                     C1 = Y1-16;  
  139.                     C2 = Y2-16;  
  140.                     D1 = U1-128;  
  141.                     E1 = V1-128;  
  142.                     R1 = ((298*C1 + 409*E1 + 128)>>8>255 ? 255 : (298*C1 + 409*E1 + 128)>>8);  
  143.                     G1 = ((298*C1 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C1 - 100*D1 - 208*E1 + 128)>>8);    
  144.                     B1 = ((298*C1+516*D1 +128)>>8>255 ? 255 : (298*C1+516*D1 +128)>>8);    
  145.                     R2 = ((298*C2 + 409*E1 + 128)>>8>255 ? 255 : (298*C2 + 409*E1 + 128)>>8);  
  146.                     G2 = ((298*C2 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C2 - 100*D1 - 208*E1 + 128)>>8);  
  147.                     B2 = ((298*C2 + 516*D1 +128)>>8>255 ? 255 : (298*C2 + 516*D1 +128)>>8);    
  148.                     *(pRGBData+(height-i-1)*width*3+j*6+2) = R1<0 ? 0 : R1;  
  149.                     *(pRGBData+(height-i-1)*width*3+j*6+1) = G1<0 ? 0 : G1;  
  150.                     *(pRGBData+(height-i-1)*width*3+j*6) = B1<0 ? 0 : B1;  
  151.                     *(pRGBData+(height-i-1)*width*3+j*6+5) = R2<0 ? 0 : R2;  
  152.                     *(pRGBData+(height-i-1)*width*3+j*6+4) = G2<0 ? 0 : G2;  
  153.                     *(pRGBData+(height-i-1)*width*3+j*6+3) = B2<0 ? 0 : B2;  
  154.                 }  
  155.             }     
  156.         }  
  157.     }  
  158.     return 0;  
  159. }  
  160.   
  161. //   
  162. // RGB2YUV   
  163. // pRGB         point to the RGB data   
  164. // pYUV         point to the YUV data   
  165. // width        width of the picture   
  166. // height       height of the picture   
  167. // alphaYUV     is there an alpha channel in YUV   
  168. // alphaRGB     is there an alpha channel in RGB   
  169. //   
  170. int RGB2YUV(void* pRGB, void* pYUV, int width, int height, bool alphaYUV, bool alphaRGB)  
  171. {  
  172.     if (NULL == pRGB)  
  173.     {  
  174.         return -1;  
  175.     }  
  176.     unsigned char* pRGBData = (unsigned char *)pRGB;  
  177.     unsigned char* pYUVData = (unsigned char *)pYUV;  
  178.     if (NULL == pYUVData)  
  179.     {  
  180.         if (alphaYUV)  
  181.         {  
  182.             pYUVData = new unsigned char[width*height*3];  
  183.         }  
  184.         else  
  185.             pYUVData = new unsigned char[width*height*2];  
  186.     }  
  187.     int R1, G1, B1, R2, G2, B2, Y1, U1, Y2, V1;  
  188.     int alpha1, alpha2;  
  189.     if (alphaYUV)  
  190.     {  
  191.         if (alphaRGB)  
  192.         {  
  193.             for (int i=0; i<height; ++i)  
  194.             {  
  195.                 for (int j=0; j<width/2; ++j)  
  196.                 {  
  197.                     B1 = *(pRGBData+(height-i-1)*width*4+j*8);  
  198.                     G1 = *(pRGBData+(height-i-1)*width*4+j*8+1);  
  199.                     R1 = *(pRGBData+(height-i-1)*width*4+j*8+2);  
  200.                     alpha1 = *(pRGBData+(height-i-1)*width*4+j*8+3);  
  201.                     B2 = *(pRGBData+(height-i-1)*width*4+j*8+4);  
  202.                     G2 = *(pRGBData+(height-i-1)*width*4+j*8+5);  
  203.                     R2 = *(pRGBData+(height-i-1)*width*4+j*8+6);  
  204.                     alpha2 = *(pRGBData+(height-i-1)*width*4+j*8+7);  
  205.                     Y1 = (((66*R1+129*G1+25*B1+128)>>8) + 16) > 255 ? 255 : (((66*R1+129*G1+25*B1+128)>>8) + 16);  
  206.                     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);  
  207.                     Y2 = (((66*R2+129*G2+25*B2+128)>>8) + 16)>255 ? 255 : ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  208.                     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);  
  209.                     *(pYUVData+i*width*3+j*6) = Y1;  
  210.                     *(pYUVData+i*width*3+j*6+1) = U1;  
  211.                     *(pYUVData+i*width*3+j*6+2) = Y2;  
  212.                     *(pYUVData+i*width*3+j*6+3) = V1;  
  213.                     *(pYUVData+i*width*3+j*6+4) = alpha1;  
  214.                     *(pYUVData+i*width*3+j*6+5) = alpha2;  
  215.                 }  
  216.             }     
  217.         }  
  218.         else  
  219.         {  
  220.             unsigned char alpha = 255;  
  221.             for (int i=0; i<height; ++i)  
  222.             {  
  223.                 for (int j=0; j<width/2; ++j)  
  224.                 {  
  225.                     B1 = *(pRGBData+(height-i-1)*width*3+j*6);  
  226.                     G1 = *(pRGBData+(height-i-1)*width*3+j*6+1);  
  227.                     R1 = *(pRGBData+(height-i-1)*width*3+j*6+2);  
  228.                     B2 = *(pRGBData+(height-i-1)*width*3+j*6+3);  
  229.                     G2 = *(pRGBData+(height-i-1)*width*3+j*6+4);  
  230.                     R2 = *(pRGBData+(height-i-1)*width*3+j*6+5);  
  231.                     Y1 = ((66*R1+129*G1+25*B1+128)>>8) + 16;  
  232.                     U1 = ((-38*R1-74*G1+112*B1+128)>>8+(-38*R2-74*G2+112*B2+128)>>8)/2 + 128;  
  233.                     Y2 = ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  234.                     V1 = ((112*R1-94*G1-18*B1+128)>>8 + (112*R2-94*G2-18*B2+128)>>8)/2 + 128;  
  235.                     Y1 = (((66*R1+129*G1+25*B1+128)>>8) + 16) > 255 ? 255 : (((66*R1+129*G1+25*B1+128)>>8) + 16);  
  236.                     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);  
  237.                     Y2 = (((66*R2+129*G2+25*B2+128)>>8) + 16)>255 ? 255 : ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  238.                     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);  
  239.                     *(pYUVData+i*width*3+j*6) = Y1;  
  240.                     *(pYUVData+i*width*3+j*6+1) = U1;  
  241.                     *(pYUVData+i*width*3+j*6+2) = Y2;  
  242.                     *(pYUVData+i*width*3+j*6+3) = V1;  
  243.                     *(pYUVData+i*width*3+j*6+4) = alpha;  
  244.                     *(pYUVData+i*width*3+j*6+5) = alpha;  
  245.                 }  
  246.             }     
  247.         }  
  248.     }  
  249.     else  
  250.     {  
  251.         if (alphaRGB)  
  252.         {  
  253.             for (int i=0; i<height; ++i)  
  254.             {  
  255.                 for (int j=0; j<width/2; ++j)  
  256.                 {  
  257.                     B1 = *(pRGBData+(height-i-1)*width*4+j*8);  
  258.                     G1 = *(pRGBData+(height-i-1)*width*4+j*8+1);  
  259.                     R1 = *(pRGBData+(height-i-1)*width*4+j*8+2);  
  260.                     B2 = *(pRGBData+(height-i-1)*width*4+j*8+4);  
  261.                     G2 = *(pRGBData+(height-i-1)*width*4+j*8+5);  
  262.                     R2 = *(pRGBData+(height-i-1)*width*4+j*8+6);  
  263.                     Y1 = (((66*R1+129*G1+25*B1+128)>>8) + 16) > 255 ? 255 : (((66*R1+129*G1+25*B1+128)>>8) + 16);  
  264.                     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);  
  265.                     Y2 = (((66*R2+129*G2+25*B2+128)>>8) + 16)>255 ? 255 : ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  266.                     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);  
  267.                     *(pYUVData+i*width*2+j*4) = Y1;  
  268.                     *(pYUVData+i*width*2+j*4+1) = U1;  
  269.                     *(pYUVData+i*width*2+j*4+2) = Y2;  
  270.                     *(pYUVData+i*width*2+j*4+3) = V1;  
  271.                 }  
  272.             }     
  273.         }  
  274.         else  
  275.         {  
  276.             for (int i=0; i<height; ++i)  
  277.             {  
  278.                 for (int j=0; j<width/2; ++j)  
  279.                 {  
  280.                     B1 = *(pRGBData+(height-i-1)*width*3+j*6);  
  281.                     G1 = *(pRGBData+(height-i-1)*width*3+j*6+1);  
  282.                     R1 = *(pRGBData+(height-i-1)*width*3+j*6+2);  
  283.                     B2 = *(pRGBData+(height-i-1)*width*3+j*6+3);  
  284.                     G2 = *(pRGBData+(height-i-1)*width*3+j*6+4);  
  285.                     R2 = *(pRGBData+(height-i-1)*width*3+j*6+5);  
  286.                     Y1 = (((66*R1+129*G1+25*B1+128)>>8) + 16) > 255 ? 255 : (((66*R1+129*G1+25*B1+128)>>8) + 16);  
  287.                     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);  
  288.                     Y2 = (((66*R2+129*G2+25*B2+128)>>8) + 16)>255 ? 255 : ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  289.                     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);  
  290.                     *(pYUVData+i*width*2+j*4) = Y1;  
  291.                     *(pYUVData+i*width*2+j*4+1) = U1;  
  292.                     *(pYUVData+i*width*2+j*4+2) = Y2;  
  293.                     *(pYUVData+i*width*2+j*4+3) = V1;  
  294.                 }  
  295.             }     
  296.         }  
  297.     }  
  298.     return 0;  
  299. }  
  300.   
  301. //   
  302. // pGBYUV           point to the background YUV data   
  303. // pFGYUV           point to the foreground YUV data   
  304. // width            width of the picture   
  305. // height           height of the picture   
  306. // alphaBG          is there an alpha channel in background YUV data   
  307. // alphaFG          is there an alpha channel in fourground YUV data   
  308. //   
  309. int YUVBlending(void* pBGYUV, void* pFGYUV, int width, int height, bool alphaBG, bool alphaFG)  
  310. {  
  311.     if (NULL == pBGYUV || NULL == pFGYUV)  
  312.     {  
  313.         return -1;  
  314.     }  
  315.     unsigned char* pBGData = (unsigned char*)pBGYUV;  
  316.     unsigned char* pFGData = (unsigned char*)pFGYUV;  
  317.     if (!alphaFG)  
  318.     {  
  319.         if (!alphaBG)  
  320.         {  
  321.             memcpy(pBGData, pFGData, width*height*2);  
  322.         }  
  323.         else  
  324.         {  
  325.             for (int i=0; i<height; ++i)  
  326.             {  
  327.                 for (int j=0; j<width/2; ++j)  
  328.                 {  
  329.                     *(pBGData+i*width*2+j*4) = *(pFGData+i*width*2+j*4);  
  330.                     *(pBGData+i*width*2+j*4+1) = *(pFGData+i*width*2+j*4+1);  
  331.                     *(pBGData+i*width*2+j*4+2) = *(pFGData+i*width*2+j*4+2);  
  332.                     *(pBGData+i*width*2+j*4+3) = *(pFGData+i*width*2+j*4+3);  
  333.                 }  
  334.             }  
  335.         }  
  336.     }  
  337.     int Y11, U11, V11, Y12, Y21, U21, V21, Y22;  
  338.     int alpha1, alpha2;  
  339.     if (!alphaBG)  
  340.     {  
  341.         for (int i=0; i<height; ++i)  
  342.         {  
  343.             for (int j=0; j<width/2; ++j)  
  344.             {  
  345.                 Y11 = *(pBGData+i*width*2+j*4);  
  346.                 U11 = *(pBGData+i*width*2+j*4+1);  
  347.                 Y12 = *(pBGData+i*width*2+j*4+2);  
  348.                 V11 = *(pBGData+i*width*2+j*4+3);  
  349.   
  350.                 Y21 = *(pFGData+i*width*3+j*6);  
  351.                 U21 = *(pFGData+i*width*3+j*6+1);  
  352.                 Y22 = *(pFGData+i*width*3+j*6+2);  
  353.                 V21 = *(pFGData+i*width*3+j*6+3);  
  354.                 alpha1 = *(pFGData+i*width*3+j*6+4);  
  355.                 alpha2 = *(pFGData+i*width*3+j*6+5);  
  356.   
  357.                 *(pBGData+i*width*2+j*4) = (Y21-16)*alpha1/255+(Y11-16)*(255-alpha1)/255+16;  
  358.                 *(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;  
  359.                 *(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;  
  360.                 *(pBGData+i*width*2+j*4+2) = (Y22-16)*alpha2/255+(Y12-16)*(255-alpha2)/255+16;  
  361.             }  
  362.         }  
  363.     }  
  364.     else  
  365.     {  
  366.         for (int i=0; i<height; ++i)  
  367.         {  
  368.             for (int j=0; j<width/2; ++j)  
  369.             {  
  370.                 Y11 = *(pBGData+i*width*3+j*6);  
  371.                 U11 = *(pBGData+i*width*3+j*6+1);  
  372.                 Y12 = *(pBGData+i*width*3+j*6+2);  
  373.                 V11 = *(pBGData+i*width*3+j*6+3);  
  374.   
  375.                 Y21 = *(pFGData+i*width*3+j*6);  
  376.                 U21 = *(pFGData+i*width*3+j*6+1);  
  377.                 Y22 = *(pFGData+i*width*3+j*6+2);  
  378.                 V21 = *(pFGData+i*width*3+j*6+3);  
  379.                 alpha1 = *(pFGData+i*width*3+j*6+4);  
  380.                 alpha2 = *(pFGData+i*width*3+j*6+5);  
  381.   
  382.                 *(pBGData+i*width*3+j*6) = (Y21-16)*alpha1/255+(Y11-16)*(255-alpha1)/255+16;  
  383.                 *(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;  
  384.                 *(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;  
  385.                 *(pBGData+i*width*3+j*6+2) = (Y22-16)*alpha2/255+(Y12-16)*(255-alpha2)/255+16;  
  386.             }  
  387.         }  
  388.     }  
  389.     return 0;  
  390. }  
[c-sharp] view plaincopyprint?
  1. //  
  2. // YUV2RGB  
  3. // pYUV         point to the YUV data  
  4. // pRGB         point to the RGB data  
  5. // width        width of the picture  
  6. // height       height of the picture  
  7. // alphaYUV     is there an alpha channel in YUV  
  8. // alphaRGB     is there an alpha channel in RGB  
  9. //  
  10. int YUV2RGB(void* pYUV, void* pRGB, int width, int height, bool alphaYUV, bool alphaRGB)  
  11. {  
  12.     if (NULL == pYUV)  
  13.     {  
  14.         return -1;  
  15.     }  
  16.     unsigned char* pYUVData = (unsigned char *)pYUV;  
  17.     unsigned char* pRGBData = (unsigned char *)pRGB;  
  18.     if (NULL == pRGBData)  
  19.     {  
  20.         if (alphaRGB)  
  21.         {  
  22.             pRGBData = new unsigned char[width*height*4];  
  23.         }  
  24.         else  
  25.             pRGBData = new unsigned char[width*height*3];  
  26.     }  
  27.     int Y1, U1, V1, Y2, alpha1, alpha2, R1, G1, B1, R2, G2, B2;  
  28.     int C1, D1, E1, C2;  
  29.     if (alphaRGB)  
  30.     {  
  31.         if (alphaYUV)  
  32.         {  
  33.             for (int i=0; i<height; ++i)  
  34.             {  
  35.                 for (int j=0; j<width/2; ++j)  
  36.                 {  
  37.                     Y1 = *(pYUVData+i*width*3+j*6);  
  38.                     U1 = *(pYUVData+i*width*3+j*6+1);  
  39.                     Y2 = *(pYUVData+i*width*3+j*6+2);  
  40.                     V1 = *(pYUVData+i*width*3+j*6+3);  
  41.                     alpha1 = *(pYUVData+i*width*3+j*6+4);  
  42.                     alpha2 = *(pYUVData+i*width*3+j*6+5);  
  43.                     C1 = Y1-16;  
  44.                     C2 = Y2-16;  
  45.                     D1 = U1-128;  
  46.                     E1 = V1-128;  
  47.                     R1 = ((298*C1 + 409*E1 + 128)>>8>255 ? 255 : (298*C1 + 409*E1 + 128)>>8);  
  48.                     G1 = ((298*C1 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C1 - 100*D1 - 208*E1 + 128)>>8);    
  49.                     B1 = ((298*C1+516*D1 +128)>>8>255 ? 255 : (298*C1+516*D1 +128)>>8);    
  50.                     R2 = ((298*C2 + 409*E1 + 128)>>8>255 ? 255 : (298*C2 + 409*E1 + 128)>>8);  
  51.                     G2 = ((298*C2 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C2 - 100*D1 - 208*E1 + 128)>>8);  
  52.                     B2 = ((298*C2 + 516*D1 +128)>>8>255 ? 255 : (298*C2 + 516*D1 +128)>>8);    
  53.                     *(pRGBData+(height-i-1)*width*4+j*8+2) = R1<0 ? 0 : R1;  
  54.                     *(pRGBData+(height-i-1)*width*4+j*8+1) = G1<0 ? 0 : G1;  
  55.                     *(pRGBData+(height-i-1)*width*4+j*8) = B1<0 ? 0 : B1;  
  56.                     *(pRGBData+(height-i-1)*width*4+j*8+3) = alpha1;      
  57.                     *(pRGBData+(height-i-1)*width*4+j*8+6) = R2<0 ? 0 : R2;  
  58.                     *(pRGBData+(height-i-1)*width*4+j*8+5) = G2<0 ? 0 : G2;  
  59.                     *(pRGBData+(height-i-1)*width*4+j*8+4) = B2<0 ? 0 : B2;  
  60.                     *(pRGBData+(height-i-1)*width*4+j*8+7) = alpha2;      
  61.                 }  
  62.             }     
  63.         }  
  64.         else  
  65.         {  
  66.             int alpha = 255;  
  67.             for (int i=0; i<height; ++i)  
  68.             {  
  69.                 for (int j=0; j<width/2; ++j)  
  70.                 {  
  71.                     Y1 = *(pYUVData+i*width*2+j*4);  
  72.                     U1 = *(pYUVData+i*width*2+j*4+1);  
  73.                     Y2 = *(pYUVData+i*width*2+j*4+2);  
  74.                     V1 = *(pYUVData+i*width*2+j*4+3);  
  75.                     C1 = Y1-16;  
  76.                     C2 = Y2-16;  
  77.                     D1 = U1-128;  
  78.                     E1 = V1-128;  
  79.                     R1 = ((298*C1 + 409*E1 + 128)>>8>255 ? 255 : (298*C1 + 409*E1 + 128)>>8);  
  80.                     G1 = ((298*C1 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C1 - 100*D1 - 208*E1 + 128)>>8);    
  81.                     B1 = ((298*C1+516*D1 +128)>>8>255 ? 255 : (298*C1+516*D1 +128)>>8);    
  82.                     R2 = ((298*C2 + 409*E1 + 128)>>8>255 ? 255 : (298*C2 + 409*E1 + 128)>>8);  
  83.                     G2 = ((298*C2 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C2 - 100*D1 - 208*E1 + 128)>>8);  
  84.                     B2 = ((298*C2 + 516*D1 +128)>>8>255 ? 255 : (298*C2 + 516*D1 +128)>>8);    
  85.                     *(pRGBData+(height-i-1)*width*4+j*8+2) = R1<0 ? 0 : R1;  
  86.                     *(pRGBData+(height-i-1)*width*4+j*8+1) = G1<0 ? 0 : G1;  
  87.                     *(pRGBData+(height-i-1)*width*4+j*8) = B1<0 ? 0 : B1;  
  88.                     *(pRGBData+(height-i-1)*width*4+j*8+3) = alpha;   
  89.                     *(pRGBData+(height-i-1)*width*4+j*8+6) = R2<0 ? 0 : R2;  
  90.                     *(pRGBData+(height-i-1)*width*4+j*8+5) = G2<0 ? 0 : G2;  
  91.                     *(pRGBData+(height-i-1)*width*4+j*8+4) = B2<0 ? 0 : B2;  
  92.                     *(pRGBData+(height-i-1)*width*4+j*8+7) = alpha;   
  93.                 }  
  94.             }     
  95.         }  
  96.     }  
  97.     else  
  98.     {  
  99.         if (alphaYUV)  
  100.         {  
  101.             for (int i=0; i<height; ++i)  
  102.             {  
  103.                 for (int j=0; j<width/2; ++j)  
  104.                 {  
  105.                     Y1 = *(pYUVData+i*width*3+j*4);  
  106.                     U1 = *(pYUVData+i*width*3+j*4+1);  
  107.                     Y2 = *(pYUVData+i*width*3+j*4+2);  
  108.                     V1 = *(pYUVData+i*width*3+j*4+3);  
  109.                     C1 = Y1-16;  
  110.                     C2 = Y2-16;  
  111.                     D1 = U1-128;  
  112.                     E1 = V1-128;  
  113.                     R1 = ((298*C1 + 409*E1 + 128)>>8>255 ? 255 : (298*C1 + 409*E1 + 128)>>8);  
  114.                     G1 = ((298*C1 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C1 - 100*D1 - 208*E1 + 128)>>8);    
  115.                     B1 = ((298*C1+516*D1 +128)>>8>255 ? 255 : (298*C1+516*D1 +128)>>8);    
  116.                     R2 = ((298*C2 + 409*E1 + 128)>>8>255 ? 255 : (298*C2 + 409*E1 + 128)>>8);  
  117.                     G2 = ((298*C2 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C2 - 100*D1 - 208*E1 + 128)>>8);  
  118.                     B2 = ((298*C2 + 516*D1 +128)>>8>255 ? 255 : (298*C2 + 516*D1 +128)>>8);    
  119.                     *(pRGBData+(height-i-1)*width*3+j*6+2) = R1<0 ? 0 : R1;  
  120.                     *(pRGBData+(height-i-1)*width*3+j*6+1) = G1<0 ? 0 : G1;  
  121.                     *(pRGBData+(height-i-1)*width*3+j*6) = B1<0 ? 0 : B1;  
  122.                     *(pRGBData+(height-i-1)*width*3+j*6+5) = R2<0 ? 0 : R2;  
  123.                     *(pRGBData+(height-i-1)*width*3+j*6+4) = G2<0 ? 0 : G2;  
  124.                     *(pRGBData+(height-i-1)*width*3+j*6+3) = B2<0 ? 0 : B2;  
  125.                 }  
  126.             }  
  127.         }  
  128.         else  
  129.         {  
  130.             for (int i=0; i<height; ++i)  
  131.             {  
  132.                 for (int j=0; j<width/2; ++j)  
  133.                 {  
  134.                     Y1 = *(pYUVData+i*width*2+j*4);  
  135.                     U1 = *(pYUVData+i*width*2+j*4+1);  
  136.                     Y2 = *(pYUVData+i*width*2+j*4+2);  
  137.                     V1 = *(pYUVData+i*width*2+j*4+3);  
  138.                     C1 = Y1-16;  
  139.                     C2 = Y2-16;  
  140.                     D1 = U1-128;  
  141.                     E1 = V1-128;  
  142.                     R1 = ((298*C1 + 409*E1 + 128)>>8>255 ? 255 : (298*C1 + 409*E1 + 128)>>8);  
  143.                     G1 = ((298*C1 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C1 - 100*D1 - 208*E1 + 128)>>8);    
  144.                     B1 = ((298*C1+516*D1 +128)>>8>255 ? 255 : (298*C1+516*D1 +128)>>8);    
  145.                     R2 = ((298*C2 + 409*E1 + 128)>>8>255 ? 255 : (298*C2 + 409*E1 + 128)>>8);  
  146.                     G2 = ((298*C2 - 100*D1 - 208*E1 + 128)>>8>255 ? 255 : (298*C2 - 100*D1 - 208*E1 + 128)>>8);  
  147.                     B2 = ((298*C2 + 516*D1 +128)>>8>255 ? 255 : (298*C2 + 516*D1 +128)>>8);    
  148.                     *(pRGBData+(height-i-1)*width*3+j*6+2) = R1<0 ? 0 : R1;  
  149.                     *(pRGBData+(height-i-1)*width*3+j*6+1) = G1<0 ? 0 : G1;  
  150.                     *(pRGBData+(height-i-1)*width*3+j*6) = B1<0 ? 0 : B1;  
  151.                     *(pRGBData+(height-i-1)*width*3+j*6+5) = R2<0 ? 0 : R2;  
  152.                     *(pRGBData+(height-i-1)*width*3+j*6+4) = G2<0 ? 0 : G2;  
  153.                     *(pRGBData+(height-i-1)*width*3+j*6+3) = B2<0 ? 0 : B2;  
  154.                 }  
  155.             }     
  156.         }  
  157.     }  
  158.     return 0;  
  159. }  
  160.   
  161. //  
  162. // RGB2YUV  
  163. // pRGB         point to the RGB data  
  164. // pYUV         point to the YUV data  
  165. // width        width of the picture  
  166. // height       height of the picture  
  167. // alphaYUV     is there an alpha channel in YUV  
  168. // alphaRGB     is there an alpha channel in RGB  
  169. //  
  170. int RGB2YUV(void* pRGB, void* pYUV, int width, int height, bool alphaYUV, bool alphaRGB)  
  171. {  
  172.     if (NULL == pRGB)  
  173.     {  
  174.         return -1;  
  175.     }  
  176.     unsigned char* pRGBData = (unsigned char *)pRGB;  
  177.     unsigned char* pYUVData = (unsigned char *)pYUV;  
  178.     if (NULL == pYUVData)  
  179.     {  
  180.         if (alphaYUV)  
  181.         {  
  182.             pYUVData = new unsigned char[width*height*3];  
  183.         }  
  184.         else  
  185.             pYUVData = new unsigned char[width*height*2];  
  186.     }  
  187.     int R1, G1, B1, R2, G2, B2, Y1, U1, Y2, V1;  
  188.     int alpha1, alpha2;  
  189.     if (alphaYUV)  
  190.     {  
  191.         if (alphaRGB)  
  192.         {  
  193.             for (int i=0; i<height; ++i)  
  194.             {  
  195.                 for (int j=0; j<width/2; ++j)  
  196.                 {  
  197.                     B1 = *(pRGBData+(height-i-1)*width*4+j*8);  
  198.                     G1 = *(pRGBData+(height-i-1)*width*4+j*8+1);  
  199.                     R1 = *(pRGBData+(height-i-1)*width*4+j*8+2);  
  200.                     alpha1 = *(pRGBData+(height-i-1)*width*4+j*8+3);  
  201.                     B2 = *(pRGBData+(height-i-1)*width*4+j*8+4);  
  202.                     G2 = *(pRGBData+(height-i-1)*width*4+j*8+5);  
  203.                     R2 = *(pRGBData+(height-i-1)*width*4+j*8+6);  
  204.                     alpha2 = *(pRGBData+(height-i-1)*width*4+j*8+7);  
  205.                     Y1 = (((66*R1+129*G1+25*B1+128)>>8) + 16) > 255 ? 255 : (((66*R1+129*G1+25*B1+128)>>8) + 16);  
  206.                     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);  
  207.                     Y2 = (((66*R2+129*G2+25*B2+128)>>8) + 16)>255 ? 255 : ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  208.                     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);  
  209.                     *(pYUVData+i*width*3+j*6) = Y1;  
  210.                     *(pYUVData+i*width*3+j*6+1) = U1;  
  211.                     *(pYUVData+i*width*3+j*6+2) = Y2;  
  212.                     *(pYUVData+i*width*3+j*6+3) = V1;  
  213.                     *(pYUVData+i*width*3+j*6+4) = alpha1;  
  214.                     *(pYUVData+i*width*3+j*6+5) = alpha2;  
  215.                 }  
  216.             }     
  217.         }  
  218.         else  
  219.         {  
  220.             unsigned char alpha = 255;  
  221.             for (int i=0; i<height; ++i)  
  222.             {  
  223.                 for (int j=0; j<width/2; ++j)  
  224.                 {  
  225.                     B1 = *(pRGBData+(height-i-1)*width*3+j*6);  
  226.                     G1 = *(pRGBData+(height-i-1)*width*3+j*6+1);  
  227.                     R1 = *(pRGBData+(height-i-1)*width*3+j*6+2);  
  228.                     B2 = *(pRGBData+(height-i-1)*width*3+j*6+3);  
  229.                     G2 = *(pRGBData+(height-i-1)*width*3+j*6+4);  
  230.                     R2 = *(pRGBData+(height-i-1)*width*3+j*6+5);  
  231.                     Y1 = ((66*R1+129*G1+25*B1+128)>>8) + 16;  
  232.                     U1 = ((-38*R1-74*G1+112*B1+128)>>8+(-38*R2-74*G2+112*B2+128)>>8)/2 + 128;  
  233.                     Y2 = ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  234.                     V1 = ((112*R1-94*G1-18*B1+128)>>8 + (112*R2-94*G2-18*B2+128)>>8)/2 + 128;  
  235.                     Y1 = (((66*R1+129*G1+25*B1+128)>>8) + 16) > 255 ? 255 : (((66*R1+129*G1+25*B1+128)>>8) + 16);  
  236.                     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);  
  237.                     Y2 = (((66*R2+129*G2+25*B2+128)>>8) + 16)>255 ? 255 : ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  238.                     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);  
  239.                     *(pYUVData+i*width*3+j*6) = Y1;  
  240.                     *(pYUVData+i*width*3+j*6+1) = U1;  
  241.                     *(pYUVData+i*width*3+j*6+2) = Y2;  
  242.                     *(pYUVData+i*width*3+j*6+3) = V1;  
  243.                     *(pYUVData+i*width*3+j*6+4) = alpha;  
  244.                     *(pYUVData+i*width*3+j*6+5) = alpha;  
  245.                 }  
  246.             }     
  247.         }  
  248.     }  
  249.     else  
  250.     {  
  251.         if (alphaRGB)  
  252.         {  
  253.             for (int i=0; i<height; ++i)  
  254.             {  
  255.                 for (int j=0; j<width/2; ++j)  
  256.                 {  
  257.                     B1 = *(pRGBData+(height-i-1)*width*4+j*8);  
  258.                     G1 = *(pRGBData+(height-i-1)*width*4+j*8+1);  
  259.                     R1 = *(pRGBData+(height-i-1)*width*4+j*8+2);  
  260.                     B2 = *(pRGBData+(height-i-1)*width*4+j*8+4);  
  261.                     G2 = *(pRGBData+(height-i-1)*width*4+j*8+5);  
  262.                     R2 = *(pRGBData+(height-i-1)*width*4+j*8+6);  
  263.                     Y1 = (((66*R1+129*G1+25*B1+128)>>8) + 16) > 255 ? 255 : (((66*R1+129*G1+25*B1+128)>>8) + 16);  
  264.                     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);  
  265.                     Y2 = (((66*R2+129*G2+25*B2+128)>>8) + 16)>255 ? 255 : ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  266.                     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);  
  267.                     *(pYUVData+i*width*2+j*4) = Y1;  
  268.                     *(pYUVData+i*width*2+j*4+1) = U1;  
  269.                     *(pYUVData+i*width*2+j*4+2) = Y2;  
  270.                     *(pYUVData+i*width*2+j*4+3) = V1;  
  271.                 }  
  272.             }     
  273.         }  
  274.         else  
  275.         {  
  276.             for (int i=0; i<height; ++i)  
  277.             {  
  278.                 for (int j=0; j<width/2; ++j)  
  279.                 {  
  280.                     B1 = *(pRGBData+(height-i-1)*width*3+j*6);  
  281.                     G1 = *(pRGBData+(height-i-1)*width*3+j*6+1);  
  282.                     R1 = *(pRGBData+(height-i-1)*width*3+j*6+2);  
  283.                     B2 = *(pRGBData+(height-i-1)*width*3+j*6+3);  
  284.                     G2 = *(pRGBData+(height-i-1)*width*3+j*6+4);  
  285.                     R2 = *(pRGBData+(height-i-1)*width*3+j*6+5);  
  286.                     Y1 = (((66*R1+129*G1+25*B1+128)>>8) + 16) > 255 ? 255 : (((66*R1+129*G1+25*B1+128)>>8) + 16);  
  287.                     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);  
  288.                     Y2 = (((66*R2+129*G2+25*B2+128)>>8) + 16)>255 ? 255 : ((66*R2+129*G2+25*B2+128)>>8) + 16;  
  289.                     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);  
  290.                     *(pYUVData+i*width*2+j*4) = Y1;  
  291.                     *(pYUVData+i*width*2+j*4+1) = U1;  
  292.                     *(pYUVData+i*width*2+j*4+2) = Y2;  
  293.                     *(pYUVData+i*width*2+j*4+3) = V1;  
  294.                 }  
  295.             }     
  296.         }  
  297.     }  
  298.     return 0;  
  299. }  
  300.   
  301. //  
  302. // pGBYUV           point to the background YUV data  
  303. // pFGYUV           point to the foreground YUV data  
  304. // width            width of the picture  
  305. // height           height of the picture  
  306. // alphaBG          is there an alpha channel in background YUV data  
  307. // alphaFG          is there an alpha channel in fourground YUV data  
  308. //  
  309. int YUVBlending(void* pBGYUV, void* pFGYUV, int width, int height, bool alphaBG, bool alphaFG)  
  310. {  
  311.     if (NULL == pBGYUV || NULL == pFGYUV)  
  312.     {  
  313.         return -1;  
  314.     }  
  315.     unsigned char* pBGData = (unsigned char*)pBGYUV;  
  316.     unsigned char* pFGData = (unsigned char*)pFGYUV;  
  317.     if (!alphaFG)  
  318.     {  
  319.         if (!alphaBG)  
  320.         {  
  321.             memcpy(pBGData, pFGData, width*height*2);  
  322.         }  
  323.         else  
  324.         {  
  325.             for (int i=0; i<height; ++i)  
  326.             {  
  327.                 for (int j=0; j<width/2; ++j)  
  328.                 {  
  329.                     *(pBGData+i*width*2+j*4) = *(pFGData+i*width*2+j*4);  
  330.                     *(pBGData+i*width*2+j*4+1) = *(pFGData+i*width*2+j*4+1);  
  331.                     *(pBGData+i*width*2+j*4+2) = *(pFGData+i*width*2+j*4+2);  
  332.                     *(pBGData+i*width*2+j*4+3) = *(pFGData+i*width*2+j*4+3);  
  333.                 }  
  334.             }  
  335.         }  
  336.     }  
  337.     int Y11, U11, V11, Y12, Y21, U21, V21, Y22;  
  338.     int alpha1, alpha2;  
  339.     if (!alphaBG)  
  340.     {  
  341.         for (int i=0; i<height; ++i)  
  342.         {  
  343.             for (int j=0; j<width/2; ++j)  
  344.             {  
  345.                 Y11 = *(pBGData+i*width*2+j*4);  
  346.                 U11 = *(pBGData+i*width*2+j*4+1);  
  347.                 Y12 = *(pBGData+i*width*2+j*4+2);  
  348.                 V11 = *(pBGData+i*width*2+j*4+3);  
  349.   
  350.                 Y21 = *(pFGData+i*width*3+j*6);  
  351.                 U21 = *(pFGData+i*width*3+j*6+1);  
  352.                 Y22 = *(pFGData+i*width*3+j*6+2);  
  353.                 V21 = *(pFGData+i*width*3+j*6+3);  
  354.                 alpha1 = *(pFGData+i*width*3+j*6+4);  
  355.                 alpha2 = *(pFGData+i*width*3+j*6+5);  
  356.   
  357.                 *(pBGData+i*width*2+j*4) = (Y21-16)*alpha1/255+(Y11-16)*(255-alpha1)/255+16;  
  358.                 *(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;  
  359.                 *(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;  
  360.                 *(pBGData+i*width*2+j*4+2) = (Y22-16)*alpha2/255+(Y12-16)*(255-alpha2)/255+16;  
  361.             }  
  362.         }  
  363.     }  
  364.     else  
  365.     {  
  366.         for (int i=0; i<height; ++i)  
  367.         {  
  368.             for (int j=0; j<width/2; ++j)  
  369.             {  
  370.                 Y11 = *(pBGData+i*width*3+j*6);  
  371.                 U11 = *(pBGData+i*width*3+j*6+1);  
  372.                 Y12 = *(pBGData+i*width*3+j*6+2);  
  373.                 V11 = *(pBGData+i*width*3+j*6+3);  
  374.   
  375.                 Y21 = *(pFGData+i*width*3+j*6);  
  376.                 U21 = *(pFGData+i*width*3+j*6+1);  
  377.                 Y22 = *(pFGData+i*width*3+j*6+2);  
  378.                 V21 = *(pFGData+i*width*3+j*6+3);  
  379.                 alpha1 = *(pFGData+i*width*3+j*6+4);  
  380.                 alpha2 = *(pFGData+i*width*3+j*6+5);  
  381.   
  382.                 *(pBGData+i*width*3+j*6) = (Y21-16)*alpha1/255+(Y11-16)*(255-alpha1)/255+16;  
  383.                 *(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;  
  384.                 *(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;  
  385.                 *(pBGData+i*width*3+j*6+2) = (Y22-16)*alpha2/255+(Y12-16)*(255-alpha2)/255+16;  
  386.             }  
  387.         }  
  388.     }  
  389.     return 0;  
  390. }  
经测试,功能已经实现,如有错误或者不妥的地方,恳请指出。 mosesyuan at gmail dot com

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/449706.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

通达信获取数据

#python第三方库pytdx获取 from pytdx.hq import TdxHq_API api TdxHq_API() # 数据获取接口一般返回list结构&#xff0c;如果需要转化为pandas Dataframe接口&#xff0c;可以使用 api.to_df 进行转化 with api.connect(119.147.212.81, 7709): # 返回普通list data …

ICMP (互联网控制消息协议 )是什么

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 互联网控制消息协议&#xff08;英语&#xff1a;Internet Control Message Protocol&#xff0c;缩写&#xff1a;ICMP&#xff09;是互…

股票数据相关性分析

导入相关包 import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import LineCollection import akshare as ak from sklearn import cluster, covariance, manifold %matplotlib inline #Jupyter Notebook显示图形专用 plt…

分享一个辅助分析内存泄漏的脚本

最近给系统做了一点优化&#xff0c;前几天去查看系统监控&#xff0c;想看看上线前后cpu使用率曲线变化情况。查看的时候意外发现上线前后内存占用相差不少&#xff0c;20%以上。 本来我没怎么在意这个问题&#xff0c;因为我们系统会在运行过程中缓存部分数据内容。但客户觉得…

windows Virtualbox下配置Ubuntu,且用ssh连接

1、软件介绍 1&#xff09;virtualbox 5.2.22 2&#xff09;Ubuntu 18.04 3&#xff09;git bash 2、virtualbox设置 安装完Ubuntu后点击该镜像的设置&#xff0c;依次点击“网络”——“端口转发” 将主机端口设置为一个闲置端口&#xff0c;子系统端口也就是Ubuntu端口设置…

专访刘伟:软件开发人员的内功修炼之道

摘要&#xff1a;数学修养对软件开发之路起着什么作用&#xff1f;码农如何修炼自己的内功并成长为优秀的软件开发员&#xff1f;带着相关思考&#xff0c;社区之星第10期采访了中南大学副教授——刘伟。他对数学修养、设计模式、软件架构和重构方面的独特见解&#xff0c;相信…

多线程数据下载(akshare)

import akshare as ak import pandas as pd from multiprocessing.dummy import Pool as ThreadPool import datetime import timedef get_hs300_stock_codes():获取沪深300股票代码列表:return:hs300ak.index_stock_cons_sina("000300")codeshs300[code]codescodes.…

MongoDB 4.6.1 c++ driver 编译

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主同意不得转载。https://blog.csdn.net/sheismylife/article/details/25512251 这个版本号已经和之前不一样了。有专门的github的项目。https://github.com/mongodb/mongo-cxx-driver首先获取源码&#xff1a;git cl…

地址解析协议 (ARP) 是什么

地址解析协议 (ARP) 是通过解析网路层地址来找寻数据链路层地址的一个在网络协议包中极其重要的网络传输协议。 ARP是通过网络地址(例&#xff1a;IPv4)来定位MAC地址 (也称为乙太地址)。 ARP已经在很多网路层和数据链接层之间得以实现&#xff0c;包括IPv4&#xff0c;Chaosn…

04.React事件 方法、 React定义方法的几种方式 获取数据 改变数据 执行方法传值...

2019独角兽企业重金招聘Python工程师标准>>> 一.基本用法 在以类继承的方式定义的组件中&#xff0c;为了能方便地调用当前组件的其他成员方法或属性&#xff08;如&#xff1a;this.state&#xff09;&#xff0c;通常需要将事件处理函数运行时的 this 指向当前组件…

代码之美——Doom3源代码赏析

摘要&#xff1a;Dyad作者、资深C工程师Shawn McGrathz在空闲时翻看了Doom3的源代码&#xff0c;发出了这样的惊叹&#xff1a;“这是我见过的最整洁、最优美的代码&#xff01;”“Doom 3的源代码让我对那些优秀的程序员刮目相看。”因此有了本文。 背景介绍&#xff1a; Doom…

UDP:用户数据报协议 是什么

用户数据报协议&#xff08;英语&#xff1a;User Datagram Protocol&#xff0c;缩写为UDP&#xff09;&#xff0c;又称用户数据报文协议&#xff0c;是一个简单的面向数据报的传输层协议&#xff0c;正式规范为RFC 768。在TCP/IP模型中&#xff0c;UDP为网络层以上和应用层以…

随想录(程序员和收入)

距离上一次写博客已经很长时间了&#xff0c;大约过了三个星期。这三个星期发生了很多事情&#xff0c;这中间也有我自己的思考积累&#xff0c;也有工作上的变故。总之&#xff0c;自己想了很多&#xff0c;也得到了很多。每到这个时候&#xff0c;毕业生朋友们都在寻找工作&a…

iOS进阶之正则表达式

最近一直在弄正则表达式&#xff0c;于是在这里整理一下&#xff0c;便于日后查阅。 1、常用符号 ^&#xff1a;字符串的开始$&#xff1a;字符串的结束*&#xff1a;表示零个或若干个?&#xff1a;表示零个或一个&#xff1a;表示一个或若干个| &#xff1a;表示 或 操作. &a…

akshare分析涨停板股票数据

导入包&#xff0c;获取日期数据 import pandas as pd import numpy as np import akshare as ak #画图 import matplotlib.pyplot as plt #正确显示中文和负号 plt.rcParams[font.sans-serif][SimHei] plt.rcParams[axes.unicode_minus]False #处理时间 from dateutil.parser…

DNS(域名系统) 是什么

域名系统&#xff08;英文&#xff1a;Domain Name System&#xff0c;缩写&#xff1a;DNS&#xff09;是互联网的一项服务。 它作为将域名和IP地址相互映射的一个分布式数据库&#xff0c;能够使人更方便地访问互联网。 DNS使用TCP和UDP端口53。当前&#xff0c;对于每一级域…

《The Art of Readable Code》学习笔记(一)

放寒假回家有些颓废&#xff0c;就是不想看书。但是已经大三了&#xff0c;春节过后就要找实习了。哎&#xff0c;快乐的大学生活终于要过去了。 先从简单的书看起吧&#xff01;在图书馆借了本《The Art of Readable Code》&#xff0c;就是教你咋写好优雅的代码的&#xff0c…

文件基本处理

1 打开文件&#xff0c;将文件句柄赋值给一个变量 2 拿句柄对文件进行操作 3 关闭文件 将一个文件第一行写道另外一个文件 f open("test","r",encoding"utf-8") # open找的是系统的编码 x f.readlines() f.close() f1 open("test1"…

C++ ofstream和ifstream详细用法

ofstream是从内存到硬盘&#xff0c;ifstream是从硬盘到内存&#xff0c;其实所谓的流缓冲就是内存空间; 在C中&#xff0c;有一个stream这个类&#xff0c;所有的I/O都以这个“流”类为基础的&#xff0c;包括我们要认识的文件I/O&#xff0c;stream这个类有两个重要的运算符&…

如何将JAR包发布到Maven中央仓库?

将jar包发布到Maven中央仓库(Maven Central Repository)&#xff0c;这样所有的Java开发者都可以使用Maven直接导入依赖&#xff0c;例如fundebug-java&#xff1a; <!-- https://mvnrepository.com/artifact/com.fundebug/fundebug-java --> <dependency><grou…