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.pswp.cn/news/449706.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/449706.shtml
英文地址,請注明出處:http://en.pswp.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…