From b7242085107de47253786ff79a478b78bbeb312d Mon Sep 17 00:00:00 2001 From: vbystricky Date: Fri, 1 Aug 2014 14:18:35 +0400 Subject: [PATCH] Fix Luv2RGB function --- modules/imgproc/src/color.cpp | 4 ++++ modules/imgproc/src/opencl/cvtcolor.cl | 22 +++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/modules/imgproc/src/color.cpp b/modules/imgproc/src/color.cpp index fe460ee75a..351ee74e43 100644 --- a/modules/imgproc/src/color.cpp +++ b/modules/imgproc/src/color.cpp @@ -2038,6 +2038,10 @@ struct Luv2RGB_f float G = X*C3 + Y*C4 + Z*C5; float B = X*C6 + Y*C7 + Z*C8; + R = std::min(std::max(R, 0.f), 1.f); + G = std::min(std::max(G, 0.f), 1.f); + B = std::min(std::max(B, 0.f), 1.f); + if( gammaTab ) { R = splineInterpolate(R*gscale, gammaTab, GAMMA_TAB_SIZE); diff --git a/modules/imgproc/src/opencl/cvtcolor.cl b/modules/imgproc/src/opencl/cvtcolor.cl index da835e08f3..18827ebfa7 100644 --- a/modules/imgproc/src/opencl/cvtcolor.cl +++ b/modules/imgproc/src/opencl/cvtcolor.cl @@ -441,18 +441,18 @@ __kernel void YCrCb2RGB(__global const uchar* src, int src_step, int src_offset, __global DATA_TYPE * dstptr = (__global DATA_TYPE*)(dst + dst_index); DATA_TYPE_4 src_pix = vload4(0, srcptr); - DATA_TYPE y = src_pix.x, cr = src_pix.y, cb = src_pix.z; + DATA_TYPE yp = src_pix.x, cr = src_pix.y, cb = src_pix.z; #ifdef DEPTH_5 __constant float * coeff = c_YCrCb2RGBCoeffs_f; - float r = fma(coeff[0], cr - HALF_MAX, y); - float g = fma(coeff[1], cr - HALF_MAX, fma(coeff[2], cb - HALF_MAX, y)); - float b = fma(coeff[3], cb - HALF_MAX, y); + float r = fma(coeff[0], cr - HALF_MAX, yp); + float g = fma(coeff[1], cr - HALF_MAX, fma(coeff[2], cb - HALF_MAX, yp)); + float b = fma(coeff[3], cb - HALF_MAX, yp); #else __constant int * coeff = c_YCrCb2RGBCoeffs_i; - int r = y + CV_DESCALE(coeff[0] * (cr - HALF_MAX), yuv_shift); - int g = y + CV_DESCALE(mad24(coeff[1], cr - HALF_MAX, coeff[2] * (cb - HALF_MAX)), yuv_shift); - int b = y + CV_DESCALE(coeff[3] * (cb - HALF_MAX), yuv_shift); + int r = yp + CV_DESCALE(coeff[0] * (cr - HALF_MAX), yuv_shift); + int g = yp + CV_DESCALE(mad24(coeff[1], cr - HALF_MAX, coeff[2] * (cb - HALF_MAX)), yuv_shift); + int b = yp + CV_DESCALE(coeff[3] * (cb - HALF_MAX), yuv_shift); #endif dstptr[(bidx^2)] = SAT_CAST(r); @@ -1796,6 +1796,10 @@ __kernel void Luv2BGR(__global const uchar * srcptr, int src_step, int src_offse float G = fma(X, coeffs[3], fma(Y, coeffs[4], Z * coeffs[5])); float B = fma(X, coeffs[6], fma(Y, coeffs[7], Z * coeffs[8])); + R = min(max(R, 0.f), 1.f); + G = min(max(G, 0.f), 1.f); + B = min(max(B, 0.f), 1.f); + #ifdef SRGB R = splineInterpolate(R*GammaTabScale, gammaTab, GAMMA_TAB_SIZE); G = splineInterpolate(G*GammaTabScale, gammaTab, GAMMA_TAB_SIZE); @@ -1853,6 +1857,10 @@ __kernel void Luv2BGR(__global const uchar * src, int src_step, int src_offset, float G = fma(X, coeffs[3], fma(Y, coeffs[4], Z * coeffs[5])); float B = fma(X, coeffs[6], fma(Y, coeffs[7], Z * coeffs[8])); + R = min(max(R, 0.f), 1.f); + G = min(max(G, 0.f), 1.f); + B = min(max(B, 0.f), 1.f); + #ifdef SRGB R = splineInterpolate(R*GammaTabScale, gammaTab, GAMMA_TAB_SIZE); G = splineInterpolate(G*GammaTabScale, gammaTab, GAMMA_TAB_SIZE);