Skip to content

Commit e3070ed

Browse files
committed
fixed YUV channel equivalence
1 parent 4e7f288 commit e3070ed

File tree

2 files changed

+134
-65
lines changed

2 files changed

+134
-65
lines changed

modules/imgproc/src/color.cpp

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,38 @@ static IppStatus sts = ippInit();
102102

103103
namespace cv
104104
{
105+
//constants for conversion from/to RGB and Gray, YUV, YCrCb according to BT.601
106+
const float B2YF = 0.114f;
107+
const float G2YF = 0.587f;
108+
const float R2YF = 0.299f;
109+
//to YCbCr
110+
const float YCBF = 0.564f; // == 1/2/(1-B2YF)
111+
const float YCRF = 0.713f; // == 1/2/(1-R2YF)
112+
const int YCBI = 9241; // == YCBF*16384
113+
const int YCRI = 11682; // == YCRF*16384
114+
//to YUV
115+
const float B2UF = 0.492f;
116+
const float R2VF = 0.877f;
117+
const int B2UI = 8061; // == B2UF*16384
118+
const int R2VI = 14369; // == R2VF*16384
119+
//from YUV
120+
const float U2BF = 2.032f;
121+
const float U2GF = -0.395f;
122+
const float V2GF = -0.581f;
123+
const float V2RF = 1.140f;
124+
const int U2BI = 33292;
125+
const int U2GI = -6472;
126+
const int V2GI = -9519;
127+
const int V2RI = 18678;
128+
//from YCrCb
129+
const float CR2RF = 1.403f;
130+
const float CB2GF = -0.344f;
131+
const float CR2GF = -0.714f;
132+
const float CB2BF = 1.773f;
133+
const int CR2RI = 22987;
134+
const int CB2GI = -5636;
135+
const int CR2GI = -11698;
136+
const int CB2BI = 29049;
105137

106138
// computes cubic spline coefficients for a function: (xi=i, yi=f[i]), i=0..n
107139
template<typename _Tp> static void splineBuild(const _Tp* f, int n, _Tp* tab)
@@ -402,9 +434,9 @@ struct IPPColor2GrayFunctor
402434
{
403435
IPPColor2GrayFunctor(ippiColor2GrayFunc _func) : func(_func)
404436
{
405-
coeffs[0] = 0.114f;
406-
coeffs[1] = 0.587f;
407-
coeffs[2] = 0.299f;
437+
coeffs[0] = B2YF;
438+
coeffs[1] = G2YF;
439+
coeffs[2] = R2YF;
408440
}
409441
bool operator()(const void *src, int srcStep, void *dst, int dstStep, int cols, int rows) const
410442
{
@@ -668,9 +700,9 @@ enum
668700
{
669701
yuv_shift = 14,
670702
xyz_shift = 12,
671-
R2Y = 4899,
672-
G2Y = 9617,
673-
B2Y = 1868,
703+
R2Y = 4899, // B2YF*16384
704+
G2Y = 9617, // G2YF*16384
705+
B2Y = 1868, // B2YF*16384
674706
BLOCK_SIZE = 256
675707
};
676708

@@ -709,7 +741,7 @@ template<typename _Tp> struct RGB2Gray
709741

710742
RGB2Gray(int _srccn, int blueIdx, const float* _coeffs) : srccn(_srccn)
711743
{
712-
static const float coeffs0[] = { 0.299f, 0.587f, 0.114f };
744+
static const float coeffs0[] = { R2YF, G2YF, B2YF };
713745
memcpy( coeffs, _coeffs ? _coeffs : coeffs0, 3*sizeof(coeffs[0]) );
714746
if(blueIdx == 0)
715747
std::swap(coeffs[0], coeffs[2]);
@@ -789,7 +821,7 @@ template<typename _Tp> struct RGB2YCrCb_f
789821

790822
RGB2YCrCb_f(int _srccn, int _blueIdx, const float* _coeffs) : srccn(_srccn), blueIdx(_blueIdx)
791823
{
792-
static const float coeffs0[] = {0.299f, 0.587f, 0.114f, 0.713f, 0.564f};
824+
static const float coeffs0[] = {R2YF, G2YF, B2YF, YCRF, YCBF};
793825
memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 5*sizeof(coeffs[0]));
794826
if(blueIdx==0) std::swap(coeffs[0], coeffs[2]);
795827
}
@@ -820,7 +852,7 @@ template<typename _Tp> struct RGB2YCrCb_i
820852
RGB2YCrCb_i(int _srccn, int _blueIdx, const int* _coeffs)
821853
: srccn(_srccn), blueIdx(_blueIdx)
822854
{
823-
static const int coeffs0[] = {R2Y, G2Y, B2Y, 11682, 9241};
855+
static const int coeffs0[] = {R2Y, G2Y, B2Y, YCRI, YCBI};
824856
memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 5*sizeof(coeffs[0]));
825857
if(blueIdx==0) std::swap(coeffs[0], coeffs[2]);
826858
}
@@ -852,7 +884,7 @@ template<typename _Tp> struct YCrCb2RGB_f
852884
YCrCb2RGB_f(int _dstcn, int _blueIdx, const float* _coeffs)
853885
: dstcn(_dstcn), blueIdx(_blueIdx)
854886
{
855-
static const float coeffs0[] = {1.403f, -0.714f, -0.344f, 1.773f};
887+
static const float coeffs0[] = {CR2RF, CR2GF, CB2GF, CB2BF};
856888
memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 4*sizeof(coeffs[0]));
857889
}
858890
void operator()(const _Tp* src, _Tp* dst, int n) const
@@ -888,7 +920,7 @@ template<typename _Tp> struct YCrCb2RGB_i
888920
YCrCb2RGB_i(int _dstcn, int _blueIdx, const int* _coeffs)
889921
: dstcn(_dstcn), blueIdx(_blueIdx)
890922
{
891-
static const int coeffs0[] = {22987, -11698, -5636, 29049};
923+
static const int coeffs0[] = {CR2RI, CR2GI, CB2GI, CB2BI};
892924
memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 4*sizeof(coeffs[0]));
893925
}
894926

@@ -3832,8 +3864,8 @@ void cv::cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
38323864
{
38333865
CV_Assert( scn == 3 || scn == 4 );
38343866
bidx = code == CV_BGR2YCrCb || code == CV_BGR2YUV ? 0 : 2;
3835-
static const float yuv_f[] = { 0.114f, 0.587f, 0.299f, 0.492f, 0.877f };
3836-
static const int yuv_i[] = { B2Y, G2Y, R2Y, 8061, 14369 };
3867+
static const float yuv_f[] = { R2YF, G2YF, B2YF, R2VF, B2UF };
3868+
static const int yuv_i[] = { R2Y, G2Y, B2Y, R2VI, B2UI };
38373869
const float* coeffs_f = code == CV_BGR2YCrCb || code == CV_RGB2YCrCb ? 0 : yuv_f;
38383870
const int* coeffs_i = code == CV_BGR2YCrCb || code == CV_RGB2YCrCb ? 0 : yuv_i;
38393871

@@ -3861,8 +3893,8 @@ void cv::cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
38613893
if( dcn <= 0 ) dcn = 3;
38623894
CV_Assert( scn == 3 && (dcn == 3 || dcn == 4) );
38633895
bidx = code == CV_YCrCb2BGR || code == CV_YUV2BGR ? 0 : 2;
3864-
static const float yuv_f[] = { 2.032f, -0.395f, -0.581f, 1.140f };
3865-
static const int yuv_i[] = { 33292, -6472, -9519, 18678 };
3896+
static const float yuv_f[] = {V2RF, V2GF, U2GF, U2BF};
3897+
static const int yuv_i[] = { V2RI, V2GI, U2GI, U2BI };
38663898
const float* coeffs_f = code == CV_YCrCb2BGR || code == CV_YCrCb2RGB ? 0 : yuv_f;
38673899
const int* coeffs_i = code == CV_YCrCb2BGR || code == CV_YCrCb2RGB ? 0 : yuv_i;
38683900

modules/ocl/src/opencl/cvt_color.cl

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,42 @@ enum
115115
BLOCK_SIZE = 256
116116
};
117117

118+
//constants for conversion from/to RGB and Gray, YUV, YCrCb according to BT.601
119+
#define B2YF 0.114f
120+
#define G2YF 0.587f
121+
#define R2YF 0.299f
122+
//to YCbCr
123+
#define YCBF 0.564f
124+
#define YCRF 0.713f
125+
#define YCBI 9241
126+
#define YCRI 11682
127+
//to YUV
128+
#define B2UF 0.492f
129+
#define R2VF 0.877f
130+
#define B2UI 8061
131+
#define R2VI 14369
132+
//from YUV
133+
#define U2BF 2.032f
134+
#define U2GF -0.395f
135+
#define V2GF -0.581f
136+
#define V2RF 1.140f
137+
#define U2BI 33292
138+
#define U2GI -6472
139+
#define V2GI -9519
140+
#define V2RI 18678
141+
//from YCrCb
142+
#define CR2RF 1.403f
143+
#define CB2GF -0.344f
144+
#define CR2GF -0.714f
145+
#define CB2BF 1.773f
146+
#define CR2RI 22987
147+
#define CB2GI -5636
148+
#define CR2GI -11698
149+
#define CB2BI 29049
150+
118151
///////////////////////////////////// RGB <-> GRAY //////////////////////////////////////
119152

120-
__constant float c_RGB2GrayCoeffs_f[3] = { 0.114f, 0.587f, 0.299f };
153+
__constant float c_RGB2GrayCoeffs_f[3] = { B2YF, G2YF, R2YF };
121154
__constant int c_RGB2GrayCoeffs_i[3] = { B2Y, G2Y, R2Y };
122155

123156
__kernel void RGB2Gray(int cols, int rows, int src_step, int dst_step,
@@ -135,7 +168,7 @@ __kernel void RGB2Gray(int cols, int rows, int src_step, int dst_step,
135168
#ifndef INTEL_DEVICE
136169

137170
#ifdef DEPTH_5
138-
dst[dst_idx] = src[src_idx + bidx] * 0.114f + src[src_idx + 1] * 0.587f + src[src_idx + (bidx^2)] * 0.299f;
171+
dst[dst_idx] = src[src_idx + bidx] * B2YF + src[src_idx + 1] * G2YF + src[src_idx + (bidx^2)] * R2YF;
139172
#else
140173
dst[dst_idx] = (DATA_TYPE)CV_DESCALE((src[src_idx + bidx] * B2Y + src[src_idx + 1] * G2Y + src[src_idx + (bidx^2)] * R2Y), yuv_shift);
141174
#endif
@@ -221,8 +254,8 @@ __kernel void Gray2RGB(int cols, int rows, int src_step, int dst_step,
221254

222255
///////////////////////////////////// RGB <-> YUV //////////////////////////////////////
223256

224-
__constant float c_RGB2YUVCoeffs_f[5] = { 0.114f, 0.587f, 0.299f, 0.492f, 0.877f };
225-
__constant int c_RGB2YUVCoeffs_i[5] = { B2Y, G2Y, R2Y, 8061, 14369 };
257+
__constant float c_RGB2YUVCoeffs_f[5] = { B2YF, G2YF, R2YF, B2UF, R2VF };
258+
__constant int c_RGB2YUVCoeffs_i[5] = { B2Y, G2Y, R2Y, B2UI, R2VI };
226259

227260
__kernel void RGB2YUV(int cols, int rows, int src_step, int dst_step,
228261
__global const DATA_TYPE* src, __global DATA_TYPE* dst,
@@ -252,18 +285,18 @@ __kernel void RGB2YUV(int cols, int rows, int src_step, int dst_step,
252285
const DATA_TYPE rgb[] = {src_ptr[0], src_ptr[1], src_ptr[2]};
253286

254287
#ifdef DEPTH_5
255-
float Y = rgb[0] * coeffs[bidx^2] + rgb[1] * coeffs[1] + rgb[2] * coeffs[bidx];
256-
float U = (rgb[bidx^2] - Y) * coeffs[3] + HALF_MAX;
257-
float V = (rgb[bidx] - Y) * coeffs[4] + HALF_MAX;
288+
float Y = rgb[0] * coeffs[bidx] + rgb[1] * coeffs[1] + rgb[2] * coeffs[bidx^2];
289+
float U = (rgb[bidx] - Y) * coeffs[3] + HALF_MAX;
290+
float V = (rgb[bidx^2] - Y) * coeffs[4] + HALF_MAX;
258291
#else
259-
int Y = CV_DESCALE(rgb[0] * coeffs[bidx^2] + rgb[1] * coeffs[1] + rgb[2] * coeffs[bidx], yuv_shift);
260-
int U = CV_DESCALE((rgb[bidx^2] - Y) * coeffs[3] + delta, yuv_shift);
261-
int V = CV_DESCALE((rgb[bidx] - Y) * coeffs[4] + delta, yuv_shift);
292+
int Y = CV_DESCALE(rgb[0] * coeffs[bidx] + rgb[1] * coeffs[1] + rgb[2] * coeffs[bidx^2], yuv_shift);
293+
int U = CV_DESCALE((rgb[bidx] - Y) * coeffs[3] + delta, yuv_shift);
294+
int V = CV_DESCALE((rgb[bidx^2] - Y) * coeffs[4] + delta, yuv_shift);
262295
#endif
263296

264297
dst_ptr[0] = SAT_CAST( Y );
265-
dst_ptr[1] = SAT_CAST( U );
266-
dst_ptr[2] = SAT_CAST( V );
298+
dst_ptr[1] = SAT_CAST( V ); //sic! store channels as YVU, not YUV
299+
dst_ptr[2] = SAT_CAST( U );
267300
}
268301
#elif (2 == pixels_per_work_item)
269302
{
@@ -274,24 +307,24 @@ __kernel void RGB2YUV(int cols, int rows, int src_step, int dst_step,
274307
const float2 c1 = r0.s15;
275308
const float2 c2 = r0.s26;
276309

277-
const float2 Y = (bidx == 0) ? (c0 * coeffs[2] + c1 * coeffs[1] + c2 * coeffs[0]) : (c0 * coeffs[0] + c1 * coeffs[1] + c2 * coeffs[2]);
278-
const float2 U = (bidx == 0) ? ((c2 - Y) * coeffs[3] + HALF_MAX) : ((c0 - Y) * coeffs[3] + HALF_MAX);
279-
const float2 V = (bidx == 0) ? ((c0 - Y) * coeffs[4] + HALF_MAX) : ((c2 - Y) * coeffs[4] + HALF_MAX);
310+
const float2 Y = (bidx == 0) ? (c0 * coeffs[0] + c1 * coeffs[1] + c2 * coeffs[2]) : (c0 * coeffs[2] + c1 * coeffs[1] + c2 * coeffs[0]);
311+
const float2 U = (bidx == 0) ? ((c0 - Y) * coeffs[3] + HALF_MAX) : ((c2 - Y) * coeffs[3] + HALF_MAX);
312+
const float2 V = (bidx == 0) ? ((c2 - Y) * coeffs[4] + HALF_MAX) : ((c0 - Y) * coeffs[4] + HALF_MAX);
280313
#else
281314
const int2 c0 = convert_int2(r0.s04);
282315
const int2 c1 = convert_int2(r0.s15);
283316
const int2 c2 = convert_int2(r0.s26);
284317

285-
const int2 yi = (bidx == 0) ? CV_DESCALE(c0 * coeffs[2] + c1 * coeffs[1] + c2 * coeffs[0], yuv_shift) : CV_DESCALE(c0 * coeffs[0] + c1 * coeffs[1] + c2 * coeffs[2], yuv_shift);
286-
const int2 ui = (bidx == 0) ? CV_DESCALE((c2 - yi) * coeffs[3] + delta, yuv_shift) : CV_DESCALE((c0 - yi) * coeffs[3] + delta, yuv_shift);
287-
const int2 vi = (bidx == 0) ? CV_DESCALE((c0 - yi) * coeffs[4] + delta, yuv_shift) : CV_DESCALE((c2 - yi) * coeffs[4] + delta, yuv_shift);
318+
const int2 yi = (bidx == 0) ? CV_DESCALE(c0 * coeffs[0] + c1 * coeffs[1] + c2 * coeffs[2], yuv_shift) : CV_DESCALE(c0 * coeffs[2] + c1 * coeffs[1] + c2 * coeffs[0], yuv_shift);
319+
const int2 ui = (bidx == 0) ? CV_DESCALE((c0 - yi) * coeffs[3] + delta, yuv_shift) : CV_DESCALE((c2 - yi) * coeffs[3] + delta, yuv_shift);
320+
const int2 vi = (bidx == 0) ? CV_DESCALE((c2 - yi) * coeffs[4] + delta, yuv_shift) : CV_DESCALE((c0 - yi) * coeffs[4] + delta, yuv_shift);
288321

289322
const VECTOR2 Y = SAT_CAST2(yi);
290323
const VECTOR2 U = SAT_CAST2(ui);
291324
const VECTOR2 V = SAT_CAST2(vi);
292325
#endif
293-
294-
vstore8((VECTOR8)(Y.s0, U.s0, V.s0, 0, Y.s1, U.s1, V.s1, 0), 0, dst_ptr);
326+
//sic! store channels as YVU, not YUV
327+
vstore8((VECTOR8)(Y.s0, V.s0, U.s0, 0, Y.s1, V.s1, U.s1, 0), 0, dst_ptr);
295328
}
296329
#elif (4 == pixels_per_work_item)
297330
{
@@ -302,23 +335,23 @@ __kernel void RGB2YUV(int cols, int rows, int src_step, int dst_step,
302335
const int4 c1 = convert_int4(r0.s159d);
303336
const int4 c2 = convert_int4(r0.s26ae);
304337

305-
const int4 yi = (bidx == 0) ? CV_DESCALE(c0 * coeffs[2] + c1 * coeffs[1] + c2 * coeffs[0], yuv_shift) : CV_DESCALE(c0 * coeffs[0] + c1 * coeffs[1] + c2 * coeffs[2], yuv_shift);
306-
const int4 ui = (bidx == 0) ? CV_DESCALE((c2 - yi) * coeffs[3] + delta, yuv_shift) : CV_DESCALE((c0 - yi) * coeffs[3] + delta, yuv_shift);
307-
const int4 vi = (bidx == 0) ? CV_DESCALE((c0 - yi) * coeffs[4] + delta, yuv_shift) : CV_DESCALE((c2 - yi) * coeffs[4] + delta, yuv_shift);
338+
const int4 yi = (bidx == 0) ? CV_DESCALE(c0 * coeffs[0] + c1 * coeffs[1] + c2 * coeffs[2], yuv_shift) : CV_DESCALE(c0 * coeffs[2] + c1 * coeffs[1] + c2 * coeffs[0], yuv_shift);
339+
const int4 ui = (bidx == 0) ? CV_DESCALE((c0 - yi) * coeffs[3] + delta, yuv_shift) : CV_DESCALE((c2 - yi) * coeffs[3] + delta, yuv_shift);
340+
const int4 vi = (bidx == 0) ? CV_DESCALE((c2 - yi) * coeffs[4] + delta, yuv_shift) : CV_DESCALE((c0 - yi) * coeffs[4] + delta, yuv_shift);
308341

309342
const VECTOR4 Y = SAT_CAST4(yi);
310343
const VECTOR4 U = SAT_CAST4(ui);
311344
const VECTOR4 V = SAT_CAST4(vi);
312-
313-
vstore16((VECTOR16)(Y.s0, U.s0, V.s0, 0, Y.s1, U.s1, V.s1, 0, Y.s2, U.s2, V.s2, 0, Y.s3, U.s3, V.s3, 0), 0, dst_ptr);
345+
//sic! store channels as YVU, not YUV
346+
vstore16((VECTOR16)(Y.s0, V.s0, U.s0, 0, Y.s1, V.s1, U.s1, 0, Y.s2, V.s2, U.s2, 0, Y.s3, V.s3, U.s3, 0), 0, dst_ptr);
314347
#endif
315348
}
316349
#endif //pixels_per_work_item
317350
}
318351
}
319352

320-
__constant float c_YUV2RGBCoeffs_f[5] = { 2.032f, -0.395f, -0.581f, 1.140f };
321-
__constant int c_YUV2RGBCoeffs_i[5] = { 33292, -6472, -9519, 18678 };
353+
__constant float c_YUV2RGBCoeffs_f[5] = { U2BF, U2GF, V2GF, V2RF };
354+
__constant int c_YUV2RGBCoeffs_i[5] = { U2BI, U2GI, V2GI, V2RI };
322355

323356
__kernel void YUV2RGB(int cols, int rows, int src_step, int dst_step,
324357
__global const DATA_TYPE* src, __global DATA_TYPE* dst,
@@ -344,16 +377,17 @@ __kernel void YUV2RGB(int cols, int rows, int src_step, int dst_step,
344377

345378
#if (1 == pixels_per_work_item)
346379
{
347-
const DATA_TYPE yuv[] = {src_ptr[0], src_ptr[1], src_ptr[2]};
380+
//sic! channels stored as YVU, not YUV
381+
const DATA_TYPE yuv[] = {src_ptr[0], src_ptr[2], src_ptr[1]};
348382

349383
#ifdef DEPTH_5
350-
float B = yuv[0] + (yuv[2] - HALF_MAX) * coeffs[3];
351-
float G = yuv[0] + (yuv[2] - HALF_MAX) * coeffs[2] + (yuv[1] - HALF_MAX) * coeffs[1];
352-
float R = yuv[0] + (yuv[1] - HALF_MAX) * coeffs[0];
384+
float B = yuv[0] + (yuv[1] - HALF_MAX) * coeffs[0];
385+
float G = yuv[0] + (yuv[1] - HALF_MAX) * coeffs[1] + (yuv[2] - HALF_MAX) * coeffs[2];
386+
float R = yuv[0] + (yuv[2] - HALF_MAX) * coeffs[3];
353387
#else
354-
int B = yuv[0] + CV_DESCALE((yuv[2] - HALF_MAX) * coeffs[3], yuv_shift);
355-
int G = yuv[0] + CV_DESCALE((yuv[2] - HALF_MAX) * coeffs[2] + (yuv[1] - HALF_MAX) * coeffs[1], yuv_shift);
356-
int R = yuv[0] + CV_DESCALE((yuv[1] - HALF_MAX) * coeffs[0], yuv_shift);
388+
int B = yuv[0] + CV_DESCALE((yuv[1] - HALF_MAX) * coeffs[0], yuv_shift);
389+
int G = yuv[0] + CV_DESCALE((yuv[1] - HALF_MAX) * coeffs[1] + (yuv[2] - HALF_MAX) * coeffs[2], yuv_shift);
390+
int R = yuv[0] + CV_DESCALE((yuv[2] - HALF_MAX) * coeffs[3], yuv_shift);
357391
#endif
358392

359393
dst_ptr[bidx] = SAT_CAST( B );
@@ -368,21 +402,23 @@ __kernel void YUV2RGB(int cols, int rows, int src_step, int dst_step,
368402
const VECTOR8 r0 = vload8(0, src_ptr);
369403

370404
#ifdef DEPTH_5
405+
//sic! channels stored as YVU, not YUV
371406
const float2 Y = r0.s04;
372-
const float2 U = r0.s15;
373-
const float2 V = r0.s26;
407+
const float2 U = r0.s26;
408+
const float2 V = r0.s15;
374409

375-
const float2 c0 = (bidx == 0) ? (Y + (V - HALF_MAX) * coeffs[3]) : (Y + (U - HALF_MAX) * coeffs[0]);
410+
const float2 c0 = (bidx != 0) ? (Y + (V - HALF_MAX) * coeffs[3]) : (Y + (U - HALF_MAX) * coeffs[0]);
376411
const float2 c1 = Y + (V - HALF_MAX) * coeffs[2] + (U - HALF_MAX) * coeffs[1];
377-
const float2 c2 = (bidx == 0) ? (Y + (U - HALF_MAX) * coeffs[0]) : (Y + (V - HALF_MAX) * coeffs[3]);
412+
const float2 c2 = (bidx != 0) ? (Y + (U - HALF_MAX) * coeffs[0]) : (Y + (V - HALF_MAX) * coeffs[3]);
378413
#else
414+
//sic! channels stored as YVU, not YUV
379415
const int2 Y = convert_int2(r0.s04);
380-
const int2 U = convert_int2(r0.s15);
381-
const int2 V = convert_int2(r0.s26);
416+
const int2 U = convert_int2(r0.s26);
417+
const int2 V = convert_int2(r0.s15);
382418

383-
const int2 c0i = (bidx == 0) ? (Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift)) : (Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift));
419+
const int2 c0i = (bidx != 0) ? (Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift)) : (Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift));
384420
const int2 c1i = Y + CV_DESCALE((V - HALF_MAX) * coeffs[2] + (U - HALF_MAX) * coeffs[1], yuv_shift);
385-
const int2 c2i = (bidx == 0) ? (Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift)) : (Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift));
421+
const int2 c2i = (bidx != 0) ? (Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift)) : (Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift));
386422

387423
const VECTOR2 c0 = SAT_CAST2(c0i);
388424
const VECTOR2 c1 = SAT_CAST2(c1i);
@@ -400,13 +436,14 @@ __kernel void YUV2RGB(int cols, int rows, int src_step, int dst_step,
400436
#ifndef DEPTH_5
401437
const VECTOR16 r0 = vload16(0, src_ptr);
402438

439+
//sic! channels stored as YVU, not YUV
403440
const int4 Y = convert_int4(r0.s048c);
404-
const int4 U = convert_int4(r0.s159d);
405-
const int4 V = convert_int4(r0.s26ae);
441+
const int4 U = convert_int4(r0.s26ae);
442+
const int4 V = convert_int4(r0.s159d);
406443

407-
const int4 c0i = (bidx == 0) ? (Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift)) : (Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift));
444+
const int4 c0i = (bidx != 0) ? (Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift)) : (Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift));
408445
const int4 c1i = Y + CV_DESCALE((V - HALF_MAX) * coeffs[2] + (U - HALF_MAX) * coeffs[1], yuv_shift);
409-
const int4 c2i = (bidx == 0) ? (Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift)) : (Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift));
446+
const int4 c2i = (bidx != 0) ? (Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift)) : (Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift));
410447

411448
const VECTOR4 c0 = SAT_CAST4(c0i);
412449
const VECTOR4 c1 = SAT_CAST4(c1i);
@@ -484,8 +521,8 @@ __kernel void YUV2RGBA_NV12(int cols, int rows, int src_step, int dst_step,
484521

485522
///////////////////////////////////// RGB <-> YCrCb //////////////////////////////////////
486523

487-
__constant float c_RGB2YCrCbCoeffs_f[5] = {0.299f, 0.587f, 0.114f, 0.713f, 0.564f};
488-
__constant int c_RGB2YCrCbCoeffs_i[5] = {R2Y, G2Y, B2Y, 11682, 9241};
524+
__constant float c_RGB2YCrCbCoeffs_f[5] = {R2YF, G2YF, B2YF, YCRF, YCBF};
525+
__constant int c_RGB2YCrCbCoeffs_i[5] = {R2Y, G2Y, B2Y, YCRI, YCBI};
489526

490527
__kernel void RGB2YCrCb(int cols, int rows, int src_step, int dst_step,
491528
__global const DATA_TYPE* src, __global DATA_TYPE* dst,
@@ -579,8 +616,8 @@ __kernel void RGB2YCrCb(int cols, int rows, int src_step, int dst_step,
579616
}
580617
}
581618

582-
__constant float c_YCrCb2RGBCoeffs_f[4] = { 1.403f, -0.714f, -0.344f, 1.773f };
583-
__constant int c_YCrCb2RGBCoeffs_i[4] = { 22987, -11698, -5636, 29049 };
619+
__constant float c_YCrCb2RGBCoeffs_f[4] = { CR2RF, CR2GF, CB2GF, CB2BF };
620+
__constant int c_YCrCb2RGBCoeffs_i[4] = { CR2RI, CR2GI, CB2GI, CB2BI };
584621

585622
__kernel void YCrCb2RGB(int cols, int rows, int src_step, int dst_step,
586623
__global const DATA_TYPE* src, __global DATA_TYPE* dst,

0 commit comments

Comments
 (0)