日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RGB 24和YUY2相互转换

發布時間:2023/12/13 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?
  • // ??
  • //?YUV2RGB ??
  • //?pYUV?????????point?to?the?YUV?data ??
  • //?pRGB?????????point?to?the?RGB?data ??
  • //?width????????width?of?the?picture ??
  • //?height???????height?of?the?picture ??
  • //?alphaYUV?????is?there?an?alpha?channel?in?YUV ??
  • //?alphaRGB?????is?there?an?alpha?channel?in?RGB ??
  • // ??
  • int?YUV2RGB(void*?pYUV,?void*?pRGB,?int?width,?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];??
  • ????????}??
  • ????????else??
  • ????????????pRGBData?=?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?(alphaYUV)??
  • ????????{??
  • ????????????for?(int?i=0;?i<height;?++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<height;?++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<height;?++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<height;?++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 ??
  • //?pRGB?????????point?to?the?RGB?data ??
  • //?pYUV?????????point?to?the?YUV?data ??
  • //?width????????width?of?the?picture ??
  • //?height???????height?of?the?picture ??
  • //?alphaYUV?????is?there?an?alpha?channel?in?YUV ??
  • //?alphaRGB?????is?there?an?alpha?channel?in?RGB ??
  • // ??
  • int?RGB2YUV(void*?pRGB,?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];??
  • ????????}??
  • ????????else??
  • ????????????pYUVData?=?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<height;?++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?=?*(pRGBData+(height-i-1)*width*4+j*8+7);??
  • ????????????????????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)?=?alpha1;??
  • ????????????????????*(pYUVData+i*width*3+j*6+5)?=?alpha2;??
  • ????????????????}??
  • ????????????}?????
  • ????????}??
  • ????????else??
  • ????????{??
  • ????????????unsigned?char?alpha?=?255;??
  • ????????????for?(int?i=0;?i<height;?++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?=?((112*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<height;?++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<height;?++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;??
  • }??
  • ??
  • // ??
  • //?pGBYUV???????????point?to?the?background?YUV?data ??
  • //?pFGYUV???????????point?to?the?foreground?YUV?data ??
  • //?width????????????width?of?the?picture ??
  • //?height???????????height?of?the?picture ??
  • //?alphaBG??????????is?there?an?alpha?channel?in?background?YUV?data ??
  • //?alphaFG??????????is?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?=?(unsigned?char*)pBGYUV;??
  • ????unsigned?char*?pFGData?=?(unsigned?char*)pFGYUV;??
  • ????if?(!alphaFG)??
  • ????{??
  • ????????if?(!alphaBG)??
  • ????????{??
  • ????????????memcpy(pBGData,?pFGData,?width*height*2);??
  • ????????}??
  • ????????else??
  • ????????{??
  • ????????????for?(int?i=0;?i<height;?++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<height;?++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);??
  • ????????????????U21?=?*(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<height;?++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);??
  • ????????????????U21?=?*(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;??
  • }??
  • [c-sharp] view plaincopyprint?
  • //??
  • //?YUV2RGB??
  • //?pYUV?????????point?to?the?YUV?data??
  • //?pRGB?????????point?to?the?RGB?data??
  • //?width????????width?of?the?picture??
  • //?height???????height?of?the?picture??
  • //?alphaYUV?????is?there?an?alpha?channel?in?YUV??
  • //?alphaRGB?????is?there?an?alpha?channel?in?RGB??
  • //??
  • int?YUV2RGB(void*?pYUV,?void*?pRGB,?int?width,?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];??
  • ????????}??
  • ????????else??
  • ????????????pRGBData?=?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?(alphaYUV)??
  • ????????{??
  • ????????????for?(int?i=0;?i<height;?++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<height;?++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<height;?++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<height;?++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??
  • //?pRGB?????????point?to?the?RGB?data??
  • //?pYUV?????????point?to?the?YUV?data??
  • //?width????????width?of?the?picture??
  • //?height???????height?of?the?picture??
  • //?alphaYUV?????is?there?an?alpha?channel?in?YUV??
  • //?alphaRGB?????is?there?an?alpha?channel?in?RGB??
  • //??
  • int?RGB2YUV(void*?pRGB,?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];??
  • ????????}??
  • ????????else??
  • ????????????pYUVData?=?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<height;?++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?=?*(pRGBData+(height-i-1)*width*4+j*8+7);??
  • ????????????????????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)?=?alpha1;??
  • ????????????????????*(pYUVData+i*width*3+j*6+5)?=?alpha2;??
  • ????????????????}??
  • ????????????}?????
  • ????????}??
  • ????????else??
  • ????????{??
  • ????????????unsigned?char?alpha?=?255;??
  • ????????????for?(int?i=0;?i<height;?++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?=?((112*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<height;?++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<height;?++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;??
  • }??
  • ??
  • //??
  • //?pGBYUV???????????point?to?the?background?YUV?data??
  • //?pFGYUV???????????point?to?the?foreground?YUV?data??
  • //?width????????????width?of?the?picture??
  • //?height???????????height?of?the?picture??
  • //?alphaBG??????????is?there?an?alpha?channel?in?background?YUV?data??
  • //?alphaFG??????????is?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?=?(unsigned?char*)pBGYUV;??
  • ????unsigned?char*?pFGData?=?(unsigned?char*)pFGYUV;??
  • ????if?(!alphaFG)??
  • ????{??
  • ????????if?(!alphaBG)??
  • ????????{??
  • ????????????memcpy(pBGData,?pFGData,?width*height*2);??
  • ????????}??
  • ????????else??
  • ????????{??
  • ????????????for?(int?i=0;?i<height;?++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<height;?++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);??
  • ????????????????U21?=?*(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<height;?++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);??
  • ????????????????U21?=?*(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;??
  • }??
  • 經測試,功能已經實現,如有錯誤或者不妥的地方,懇請指出。 mosesyuan at gmail dot com

    總結

    以上是生活随笔為你收集整理的RGB 24和YUY2相互转换的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。