color.cpp 156.4 KB
Newer Older
1 2 3 4 5 6 7 8
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
9 10
//
//                           License Agreement
11 12
//                For Open Source Computer Vision Library
//
13 14
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved.
15 16 17 18 19 20 21 22 23 24 25 26
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
27
//   * The name of the copyright holders may not be used to endorse or promote products
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/********************************* COPYRIGHT NOTICE *******************************\
  The function for RGB to Lab conversion is based on the MATLAB script
  RGB2Lab.m translated by Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
  See the page [http://vision.stanford.edu/~ruzon/software/rgblab.html]
\**********************************************************************************/

/********************************* COPYRIGHT NOTICE *******************************\
  Original code for Bayer->BGR/RGB conversion is provided by Dirk Schaefer
  from MD-Mathematische Dienste GmbH. Below is the copyright notice:

    IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
    By downloading, copying, installing or using the software you agree
    to this license. If you do not agree to this license, do not download,
    install, copy or use the software.

    Contributors License Agreement:

      Copyright (c) 2002,
      MD-Mathematische Dienste GmbH
      Im Defdahl 5-10
      44141 Dortmund
      Germany
      www.md-it.de
A
Andrey Kamaev 已提交
66

67 68
    Redistribution and use in source and binary forms,
    with or without modification, are permitted provided
A
Andrey Kamaev 已提交
69
    that the following conditions are met:
70 71

    Redistributions of source code must retain
A
Andrey Kamaev 已提交
72
    the above copyright notice, this list of conditions and the following disclaimer.
73 74
    Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
A
Andrey Kamaev 已提交
75
    and/or other materials provided with the distribution.
76
    The name of Contributor may not be used to endorse or promote products
A
Andrey Kamaev 已提交
77
    derived from this software without specific prior written permission.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    THE POSSIBILITY OF SUCH DAMAGE.
\**********************************************************************************/

#include "precomp.hpp"
V
Vadim Pisarevsky 已提交
93
#include "opencl_kernels.hpp"
94
#include <limits>
95

96 97
#define  CV_DESCALE(x,n)     (((x) + (1 << ((n)-1))) >> (n))

K
kdrobnyh 已提交
98
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
99 100 101
#define MAX_IPP8u   255
#define MAX_IPP16u  65535
#define MAX_IPP32f  1.0
102
static IppStatus sts = ippInit();
K
kdrobnyh 已提交
103 104
#endif

105 106
namespace cv
{
107 108 109 110 111 112 113

// computes cubic spline coefficients for a function: (xi=i, yi=f[i]), i=0..n
template<typename _Tp> static void splineBuild(const _Tp* f, int n, _Tp* tab)
{
    _Tp cn = 0;
    int i;
    tab[0] = tab[1] = (_Tp)0;
A
Andrey Kamaev 已提交
114

115 116 117 118 119 120
    for(i = 1; i < n-1; i++)
    {
        _Tp t = 3*(f[i+1] - 2*f[i] + f[i-1]);
        _Tp l = 1/(4 - tab[(i-1)*4]);
        tab[i*4] = l; tab[i*4+1] = (t - tab[(i-1)*4+1])*l;
    }
A
Andrey Kamaev 已提交
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135
    for(i = n-1; i >= 0; i--)
    {
        _Tp c = tab[i*4+1] - tab[i*4]*cn;
        _Tp b = f[i+1] - f[i] - (cn + c*2)*(_Tp)0.3333333333333333;
        _Tp d = (cn - c)*(_Tp)0.3333333333333333;
        tab[i*4] = f[i]; tab[i*4+1] = b;
        tab[i*4+2] = c; tab[i*4+3] = d;
        cn = c;
    }
}

// interpolates value of a function at x, 0 <= x <= n using a cubic spline.
template<typename _Tp> static inline _Tp splineInterpolate(_Tp x, const _Tp* tab, int n)
{
136 137
    // don't touch this function without urgent need - some versions of gcc fail to inline it correctly
    int ix = std::min(std::max(int(x), 0), n-1);
138 139 140
    x -= ix;
    tab += ix*4;
    return ((tab[3]*x + tab[2])*x + tab[1])*x + tab[0];
A
Andrey Kamaev 已提交
141 142
}

143

144 145 146 147
template<typename _Tp> struct ColorChannel
{
    typedef float worktype_f;
    static _Tp max() { return std::numeric_limits<_Tp>::max(); }
A
Andrey Kamaev 已提交
148
    static _Tp half() { return (_Tp)(max()/2 + 1); }
149
};
150

151 152 153 154 155 156
template<> struct ColorChannel<float>
{
    typedef float worktype_f;
    static float max() { return 1.f; }
    static float half() { return 0.5f; }
};
157

158 159 160 161 162 163
/*template<> struct ColorChannel<double>
{
    typedef double worktype_f;
    static double max() { return 1.; }
    static double half() { return 0.5; }
};*/
164

A
Andrey Kamaev 已提交
165

166
///////////////////////////// Top-level template function ////////////////////////////////
167

168
template <typename Cvt>
169
class CvtColorLoop_Invoker : public ParallelLoopBody
170 171
{
    typedef typename Cvt::channel_type _Tp;
172
public:
173

174 175
    CvtColorLoop_Invoker(const Mat& _src, Mat& _dst, const Cvt& _cvt) :
        ParallelLoopBody(), src(_src), dst(_dst), cvt(_cvt)
176
    {
A
Andrey Kamaev 已提交
177
    }
178

179 180
    virtual void operator()(const Range& range) const
    {
181 182
        const uchar* yS = src.ptr<uchar>(range.start);
        uchar* yD = dst.ptr<uchar>(range.start);
A
Andrey Kamaev 已提交
183

184
        for( int i = range.start; i < range.end; ++i, yS += src.step, yD += dst.step )
185 186
            cvt((const _Tp*)yS, (_Tp*)yD, src.cols);
    }
187

188
private:
189 190 191 192
    const Mat& src;
    Mat& dst;
    const Cvt& cvt;

193 194
    const CvtColorLoop_Invoker& operator= (const CvtColorLoop_Invoker&);
};
A
Andrey Kamaev 已提交
195

196
template <typename Cvt>
197 198
void CvtColorLoop(const Mat& src, Mat& dst, const Cvt& cvt)
{
199
    parallel_for_(Range(0, src.rows), CvtColorLoop_Invoker<Cvt>(src, dst, cvt), src.total()/(double)(1<<16) );
200
}
A
Andrey Kamaev 已提交
201

K
kdrobnyh 已提交
202
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
I
Ilya Lavrenov 已提交
203

204 205 206
typedef IppStatus (CV_STDCALL* ippiReorderFunc)(const void *, int, void *, int, IppiSize, const int *);
typedef IppStatus (CV_STDCALL* ippiGeneralFunc)(const void *, int, void *, int, IppiSize);
typedef IppStatus (CV_STDCALL* ippiColor2GrayFunc)(const void *, int, void *, int, IppiSize, const Ipp32f *);
207 208

template <typename Cvt>
I
Ilya Lavrenov 已提交
209 210
class CvtColorIPPLoop_Invoker :
        public ParallelLoopBody
211 212 213 214 215 216 217 218 219 220 221 222 223
{
public:

    CvtColorIPPLoop_Invoker(const Mat& _src, Mat& _dst, const Cvt& _cvt, bool *_ok) :
        ParallelLoopBody(), src(_src), dst(_dst), cvt(_cvt), ok(_ok)
    {
        *ok = true;
    }

    virtual void operator()(const Range& range) const
    {
        const void *yS = src.ptr<uchar>(range.start);
        void *yD = dst.ptr<uchar>(range.start);
I
Ilya Lavrenov 已提交
224
        if( !cvt(yS, (int)src.step[0], yD, (int)dst.step[0], src.cols, range.end - range.start) )
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
            *ok = false;
    }

private:
    const Mat& src;
    Mat& dst;
    const Cvt& cvt;
    bool *ok;

    const CvtColorIPPLoop_Invoker& operator= (const CvtColorIPPLoop_Invoker&);
};

template <typename Cvt>
bool CvtColorIPPLoop(const Mat& src, Mat& dst, const Cvt& cvt)
{
    bool ok;
    parallel_for_(Range(0, src.rows), CvtColorIPPLoop_Invoker<Cvt>(src, dst, cvt, &ok), src.total()/(double)(1<<16) );
    return ok;
}

template <typename Cvt>
bool CvtColorIPPLoopCopy(Mat& src, Mat& dst, const Cvt& cvt)
{
    Mat temp;
    Mat &source = src;
    if( src.data == dst.data )
    {
        src.copyTo(temp);
        source = temp;
    }
    bool ok;
I
Ilya Lavrenov 已提交
256 257
    parallel_for_(Range(0, source.rows), CvtColorIPPLoop_Invoker<Cvt>(source, dst, cvt, &ok),
                  source.total()/(double)(1<<16) );
258 259 260
    return ok;
}

261
static IppStatus CV_STDCALL ippiSwapChannels_8u_C3C4Rf(const Ipp8u* pSrc, int srcStep, Ipp8u* pDst, int dstStep,
262 263
         IppiSize roiSize, const int *dstOrder)
{
264
    return ippiSwapChannels_8u_C3C4R(pSrc, srcStep, pDst, dstStep, roiSize, dstOrder, MAX_IPP8u);
265 266
}

267
static IppStatus CV_STDCALL ippiSwapChannels_16u_C3C4Rf(const Ipp16u* pSrc, int srcStep, Ipp16u* pDst, int dstStep,
268 269
         IppiSize roiSize, const int *dstOrder)
{
270
    return ippiSwapChannels_16u_C3C4R(pSrc, srcStep, pDst, dstStep, roiSize, dstOrder, MAX_IPP16u);
271 272
}

273
static IppStatus CV_STDCALL ippiSwapChannels_32f_C3C4Rf(const Ipp32f* pSrc, int srcStep, Ipp32f* pDst, int dstStep,
274 275
         IppiSize roiSize, const int *dstOrder)
{
276
    return ippiSwapChannels_32f_C3C4R(pSrc, srcStep, pDst, dstStep, roiSize, dstOrder, MAX_IPP32f);
277 278
}

279
static ippiReorderFunc ippiSwapChannelsC3C4RTab[] =
280
{
281 282
    (ippiReorderFunc)ippiSwapChannels_8u_C3C4Rf, 0, (ippiReorderFunc)ippiSwapChannels_16u_C3C4Rf, 0,
    0, (ippiReorderFunc)ippiSwapChannels_32f_C3C4Rf, 0, 0
283 284
};

285
static ippiGeneralFunc ippiCopyAC4C3RTab[] =
286
{
287 288
    (ippiGeneralFunc)ippiCopy_8u_AC4C3R, 0, (ippiGeneralFunc)ippiCopy_16u_AC4C3R, 0,
    0, (ippiGeneralFunc)ippiCopy_32f_AC4C3R, 0, 0
289 290
};

291
static ippiReorderFunc ippiSwapChannelsC4C3RTab[] =
292
{
293 294
    (ippiReorderFunc)ippiSwapChannels_8u_C4C3R, 0, (ippiReorderFunc)ippiSwapChannels_16u_C4C3R, 0,
    0, (ippiReorderFunc)ippiSwapChannels_32f_C4C3R, 0, 0
295 296
};

297
static ippiReorderFunc ippiSwapChannelsC3RTab[] =
298
{
299 300
    (ippiReorderFunc)ippiSwapChannels_8u_C3R, 0, (ippiReorderFunc)ippiSwapChannels_16u_C3R, 0,
    0, (ippiReorderFunc)ippiSwapChannels_32f_C3R, 0, 0
301 302
};

303
#if !defined(HAVE_IPP_ICV_ONLY) && IPP_VERSION_X100 >= 801
304
static ippiReorderFunc ippiSwapChannelsC4RTab[] =
305
{
306 307
    (ippiReorderFunc)ippiSwapChannels_8u_C4R, 0, (ippiReorderFunc)ippiSwapChannels_16u_C4R, 0,
    0, (ippiReorderFunc)ippiSwapChannels_32f_C4R, 0, 0
308
};
309
#endif
310

311
static ippiColor2GrayFunc ippiColor2GrayC3Tab[] =
312
{
I
Ilya Lavrenov 已提交
313 314
    (ippiColor2GrayFunc)ippiColorToGray_8u_C3C1R, 0, (ippiColor2GrayFunc)ippiColorToGray_16u_C3C1R, 0,
    0, (ippiColor2GrayFunc)ippiColorToGray_32f_C3C1R, 0, 0
315 316
};

317
static ippiColor2GrayFunc ippiColor2GrayC4Tab[] =
318
{
319 320
    (ippiColor2GrayFunc)ippiColorToGray_8u_AC4C1R, 0, (ippiColor2GrayFunc)ippiColorToGray_16u_AC4C1R, 0,
    0, (ippiColor2GrayFunc)ippiColorToGray_32f_AC4C1R, 0, 0
321 322
};

323
static ippiGeneralFunc ippiRGB2GrayC3Tab[] =
324
{
325 326
    (ippiGeneralFunc)ippiRGBToGray_8u_C3C1R, 0, (ippiGeneralFunc)ippiRGBToGray_16u_C3C1R, 0,
    0, (ippiGeneralFunc)ippiRGBToGray_32f_C3C1R, 0, 0
327 328
};

329
static ippiGeneralFunc ippiRGB2GrayC4Tab[] =
330
{
331 332
    (ippiGeneralFunc)ippiRGBToGray_8u_AC4C1R, 0, (ippiGeneralFunc)ippiRGBToGray_16u_AC4C1R, 0,
    0, (ippiGeneralFunc)ippiRGBToGray_32f_AC4C1R, 0, 0
333 334
};

335
static ippiGeneralFunc ippiCopyP3C3RTab[] =
336
{
337 338
    (ippiGeneralFunc)ippiCopy_8u_P3C3R, 0, (ippiGeneralFunc)ippiCopy_16u_P3C3R, 0,
    0, (ippiGeneralFunc)ippiCopy_32f_P3C3R, 0, 0
339 340
};

341
static ippiGeneralFunc ippiRGB2XYZTab[] =
342
{
343
    (ippiGeneralFunc)ippiRGBToXYZ_8u_C3R, 0, (ippiGeneralFunc)ippiRGBToXYZ_16u_C3R, 0,
I
Ilya Lavrenov 已提交
344
    0, (ippiGeneralFunc)ippiRGBToXYZ_32f_C3R, 0, 0
345 346
};

347
static ippiGeneralFunc ippiXYZ2RGBTab[] =
348
{
349
    (ippiGeneralFunc)ippiXYZToRGB_8u_C3R, 0, (ippiGeneralFunc)ippiXYZToRGB_16u_C3R, 0,
I
Ilya Lavrenov 已提交
350
    0, (ippiGeneralFunc)ippiXYZToRGB_32f_C3R, 0, 0
351 352
};

353
static ippiGeneralFunc ippiRGB2HSVTab[] =
354
{
I
Ilya Lavrenov 已提交
355
    (ippiGeneralFunc)ippiRGBToHSV_8u_C3R, 0, (ippiGeneralFunc)ippiRGBToHSV_16u_C3R, 0,
356 357 358
    0, 0, 0, 0
};

359
static ippiGeneralFunc ippiHSV2RGBTab[] =
360
{
361
    (ippiGeneralFunc)ippiHSVToRGB_8u_C3R, 0, (ippiGeneralFunc)ippiHSVToRGB_16u_C3R, 0,
362 363 364
    0, 0, 0, 0
};

365
static ippiGeneralFunc ippiRGB2HLSTab[] =
366
{
367 368
    (ippiGeneralFunc)ippiRGBToHLS_8u_C3R, 0, (ippiGeneralFunc)ippiRGBToHLS_16u_C3R, 0,
    0, (ippiGeneralFunc)ippiRGBToHLS_32f_C3R, 0, 0
369 370
};

371
static ippiGeneralFunc ippiHLS2RGBTab[] =
372
{
373 374
    (ippiGeneralFunc)ippiHLSToRGB_8u_C3R, 0, (ippiGeneralFunc)ippiHLSToRGB_16u_C3R, 0,
    0, (ippiGeneralFunc)ippiHLSToRGB_32f_C3R, 0, 0
375 376
};

I
Ilya Lavrenov 已提交
377
#if !defined(HAVE_IPP_ICV_ONLY) && 0
I
Ilya Lavrenov 已提交
378 379 380 381 382 383 384 385 386 387 388
static ippiGeneralFunc ippiRGBToLUVTab[] =
{
    (ippiGeneralFunc)ippiRGBToLUV_8u_C3R, 0, (ippiGeneralFunc)ippiRGBToLUV_16u_C3R, 0,
    0, (ippiGeneralFunc)ippiRGBToLUV_32f_C3R, 0, 0
};

static ippiGeneralFunc ippiLUVToRGBTab[] =
{
    (ippiGeneralFunc)ippiLUVToRGB_8u_C3R, 0, (ippiGeneralFunc)ippiLUVToRGB_16u_C3R, 0,
    0, (ippiGeneralFunc)ippiLUVToRGB_32f_C3R, 0, 0
};
A
Alexander Alekhin 已提交
389
#endif
I
Ilya Lavrenov 已提交
390

391 392
struct IPPGeneralFunctor
{
393
    IPPGeneralFunctor(ippiGeneralFunc _func) : func(_func){}
394 395
    bool operator()(const void *src, int srcStep, void *dst, int dstStep, int cols, int rows) const
    {
I
Ilya Lavrenov 已提交
396
        return func ? func(src, srcStep, dst, dstStep, ippiSize(cols, rows)) >= 0 : false;
397 398
    }
private:
399
    ippiGeneralFunc func;
400 401 402 403
};

struct IPPReorderFunctor
{
404
    IPPReorderFunctor(ippiReorderFunc _func, int _order0, int _order1, int _order2) : func(_func)
405 406 407 408 409 410 411 412
    {
        order[0] = _order0;
        order[1] = _order1;
        order[2] = _order2;
        order[3] = 3;
    }
    bool operator()(const void *src, int srcStep, void *dst, int dstStep, int cols, int rows) const
    {
I
Ilya Lavrenov 已提交
413
        return func ? func(src, srcStep, dst, dstStep, ippiSize(cols, rows), order) >= 0 : false;
414 415
    }
private:
416
    ippiReorderFunc func;
417 418 419 420 421
    int order[4];
};

struct IPPColor2GrayFunctor
{
I
Ilya Lavrenov 已提交
422 423
    IPPColor2GrayFunctor(ippiColor2GrayFunc _func) :
        func(_func)
424 425 426 427 428 429 430
    {
        coeffs[0] = 0.114f;
        coeffs[1] = 0.587f;
        coeffs[2] = 0.299f;
    }
    bool operator()(const void *src, int srcStep, void *dst, int dstStep, int cols, int rows) const
    {
I
Ilya Lavrenov 已提交
431
        return func ? func(src, srcStep, dst, dstStep, ippiSize(cols, rows), coeffs) >= 0 : false;
432 433
    }
private:
434
    ippiColor2GrayFunc func;
435 436 437 438 439
    Ipp32f coeffs[3];
};

struct IPPGray2BGRFunctor
{
I
Ilya Lavrenov 已提交
440 441 442 443 444
    IPPGray2BGRFunctor(ippiGeneralFunc _func) :
        func(_func)
    {
    }

445 446
    bool operator()(const void *src, int srcStep, void *dst, int dstStep, int cols, int rows) const
    {
I
Ilya Lavrenov 已提交
447 448 449
        if (func == 0)
            return false;

450 451 452 453
        const void* srcarray[3] = { src, src, src };
        return func(srcarray, srcStep, dst, dstStep, ippiSize(cols, rows)) >= 0;
    }
private:
454
    ippiGeneralFunc func;
455 456 457 458
};

struct IPPGray2BGRAFunctor
{
I
Ilya Lavrenov 已提交
459 460 461 462 463
    IPPGray2BGRAFunctor(ippiGeneralFunc _func1, ippiReorderFunc _func2, int _depth) :
        func1(_func1), func2(_func2), depth(_depth)
    {
    }

464 465
    bool operator()(const void *src, int srcStep, void *dst, int dstStep, int cols, int rows) const
    {
I
Ilya Lavrenov 已提交
466 467 468
        if (func1 == 0 || func2 == 0)
            return false;

469 470 471 472 473 474 475 476
        const void* srcarray[3] = { src, src, src };
        Mat temp(rows, cols, CV_MAKETYPE(depth, 3));
        if(func1(srcarray, srcStep, temp.data, (int)temp.step[0], ippiSize(cols, rows)) < 0)
            return false;
        int order[4] = {0, 1, 2, 3};
        return func2(temp.data, (int)temp.step[0], dst, dstStep, ippiSize(cols, rows), order) >= 0;
    }
private:
477 478
    ippiGeneralFunc func1;
    ippiReorderFunc func2;
479 480 481 482 483
    int depth;
};

struct IPPReorderGeneralFunctor
{
I
Ilya Lavrenov 已提交
484 485
    IPPReorderGeneralFunctor(ippiReorderFunc _func1, ippiGeneralFunc _func2, int _order0, int _order1, int _order2, int _depth) :
        func1(_func1), func2(_func2), depth(_depth)
486 487 488 489 490 491 492 493
    {
        order[0] = _order0;
        order[1] = _order1;
        order[2] = _order2;
        order[3] = 3;
    }
    bool operator()(const void *src, int srcStep, void *dst, int dstStep, int cols, int rows) const
    {
I
Ilya Lavrenov 已提交
494 495 496
        if (func1 == 0 || func2 == 0)
            return false;

497
        Mat temp;
498 499 500 501 502 503
        temp.create(rows, cols, CV_MAKETYPE(depth, 3));
        if(func1(src, srcStep, temp.data, (int)temp.step[0], ippiSize(cols, rows), order) < 0)
            return false;
        return func2(temp.data, (int)temp.step[0], dst, dstStep, ippiSize(cols, rows)) >= 0;
    }
private:
504 505
    ippiReorderFunc func1;
    ippiGeneralFunc func2;
506 507 508 509 510 511
    int order[4];
    int depth;
};

struct IPPGeneralReorderFunctor
{
I
Ilya Lavrenov 已提交
512 513
    IPPGeneralReorderFunctor(ippiGeneralFunc _func1, ippiReorderFunc _func2, int _order0, int _order1, int _order2, int _depth) :
        func1(_func1), func2(_func2), depth(_depth)
514 515 516 517 518 519 520 521
    {
        order[0] = _order0;
        order[1] = _order1;
        order[2] = _order2;
        order[3] = 3;
    }
    bool operator()(const void *src, int srcStep, void *dst, int dstStep, int cols, int rows) const
    {
I
Ilya Lavrenov 已提交
522 523 524
        if (func1 == 0 || func2 == 0)
            return false;

525
        Mat temp;
526 527 528 529 530 531
        temp.create(rows, cols, CV_MAKETYPE(depth, 3));
        if(func1(src, srcStep, temp.data, (int)temp.step[0], ippiSize(cols, rows)) < 0)
            return false;
        return func2(temp.data, (int)temp.step[0], dst, dstStep, ippiSize(cols, rows), order) >= 0;
    }
private:
532 533
    ippiGeneralFunc func1;
    ippiReorderFunc func2;
534 535 536
    int order[4];
    int depth;
};
I
Ilya Lavrenov 已提交
537

K
kdrobnyh 已提交
538 539
#endif

540
////////////////// Various 3/4-channel to 3/4-channel RGB transformations /////////////////
A
Andrey Kamaev 已提交
541

542
template<typename _Tp> struct RGB2RGB
543
{
544
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
545

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    RGB2RGB(int _srccn, int _dstcn, int _blueIdx) : srccn(_srccn), dstcn(_dstcn), blueIdx(_blueIdx) {}
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int scn = srccn, dcn = dstcn, bidx = blueIdx;
        if( dcn == 3 )
        {
            n *= 3;
            for( int i = 0; i < n; i += 3, src += scn )
            {
                _Tp t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];
                dst[i] = t0; dst[i+1] = t1; dst[i+2] = t2;
            }
        }
        else if( scn == 3 )
        {
            n *= 3;
            _Tp alpha = ColorChannel<_Tp>::max();
            for( int i = 0; i < n; i += 3, dst += 4 )
            {
                _Tp t0 = src[i], t1 = src[i+1], t2 = src[i+2];
                dst[bidx] = t0; dst[1] = t1; dst[bidx^2] = t2; dst[3] = alpha;
            }
        }
        else
        {
            n *= 4;
            for( int i = 0; i < n; i += 4 )
            {
                _Tp t0 = src[i], t1 = src[i+1], t2 = src[i+2], t3 = src[i+3];
                dst[i] = t2; dst[i+1] = t1; dst[i+2] = t0; dst[i+3] = t3;
            }
        }
    }
A
Andrey Kamaev 已提交
579

580 581
    int srccn, dstcn, blueIdx;
};
A
Andrey Kamaev 已提交
582

583
/////////// Transforming 16-bit (565 or 555) RGB to/from 24/32-bit (888[8]) RGB //////////
584

585 586 587
struct RGB5x52RGB
{
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
588

589
    RGB5x52RGB(int _dstcn, int _blueIdx, int _greenBits)
A
Andrey Kamaev 已提交
590 591
        : dstcn(_dstcn), blueIdx(_blueIdx), greenBits(_greenBits) {}

592
    void operator()(const uchar* src, uchar* dst, int n) const
593
    {
594 595 596
        int dcn = dstcn, bidx = blueIdx;
        if( greenBits == 6 )
            for( int i = 0; i < n; i++, dst += dcn )
597 598
            {
                unsigned t = ((const ushort*)src)[i];
599
                dst[bidx] = (uchar)(t << 3);
600
                dst[1] = (uchar)((t >> 3) & ~3);
601 602 603
                dst[bidx ^ 2] = (uchar)((t >> 8) & ~7);
                if( dcn == 4 )
                    dst[3] = 255;
604 605
            }
        else
606
            for( int i = 0; i < n; i++, dst += dcn )
607 608
            {
                unsigned t = ((const ushort*)src)[i];
609
                dst[bidx] = (uchar)(t << 3);
610
                dst[1] = (uchar)((t >> 2) & ~7);
611 612 613
                dst[bidx ^ 2] = (uchar)((t >> 7) & ~7);
                if( dcn == 4 )
                    dst[3] = t & 0x8000 ? 255 : 0;
614 615
            }
    }
A
Andrey Kamaev 已提交
616

617 618
    int dstcn, blueIdx, greenBits;
};
619

A
Andrey Kamaev 已提交
620

621
struct RGB2RGB5x5
622
{
623
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
624

625
    RGB2RGB5x5(int _srccn, int _blueIdx, int _greenBits)
A
Andrey Kamaev 已提交
626 627
        : srccn(_srccn), blueIdx(_blueIdx), greenBits(_greenBits) {}

628
    void operator()(const uchar* src, uchar* dst, int n) const
629
    {
630 631 632
        int scn = srccn, bidx = blueIdx;
        if( greenBits == 6 )
            for( int i = 0; i < n; i++, src += scn )
633
            {
634 635 636 637 638 639
                ((ushort*)dst)[i] = (ushort)((src[bidx] >> 3)|((src[1]&~3) << 3)|((src[bidx^2]&~7) << 8));
            }
        else if( scn == 3 )
            for( int i = 0; i < n; i++, src += 3 )
            {
                ((ushort*)dst)[i] = (ushort)((src[bidx] >> 3)|((src[1]&~7) << 2)|((src[bidx^2]&~7) << 7));
640 641
            }
        else
642
            for( int i = 0; i < n; i++, src += 4 )
643
            {
644 645
                ((ushort*)dst)[i] = (ushort)((src[bidx] >> 3)|((src[1]&~7) << 2)|
                    ((src[bidx^2]&~7) << 7)|(src[3] ? 0x8000 : 0));
646 647
            }
    }
A
Andrey Kamaev 已提交
648

649 650
    int srccn, blueIdx, greenBits;
};
A
Andrey Kamaev 已提交
651

652
///////////////////////////////// Color to/from Grayscale ////////////////////////////////
653

654 655
template<typename _Tp>
struct Gray2RGB
656
{
657
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
658

659 660
    Gray2RGB(int _dstcn) : dstcn(_dstcn) {}
    void operator()(const _Tp* src, _Tp* dst, int n) const
661
    {
662 663 664 665 666 667
        if( dstcn == 3 )
            for( int i = 0; i < n; i++, dst += 3 )
            {
                dst[0] = dst[1] = dst[2] = src[i];
            }
        else
668
        {
669 670
            _Tp alpha = ColorChannel<_Tp>::max();
            for( int i = 0; i < n; i++, dst += 4 )
671
            {
672 673
                dst[0] = dst[1] = dst[2] = src[i];
                dst[3] = alpha;
674 675 676
            }
        }
    }
A
Andrey Kamaev 已提交
677

678 679
    int dstcn;
};
A
Andrey Kamaev 已提交
680

681

682
struct Gray2RGB5x5
683
{
684
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
685

686 687
    Gray2RGB5x5(int _greenBits) : greenBits(_greenBits) {}
    void operator()(const uchar* src, uchar* dst, int n) const
688
    {
689 690
        if( greenBits == 6 )
            for( int i = 0; i < n; i++ )
691
            {
692 693
                int t = src[i];
                ((ushort*)dst)[i] = (ushort)((t >> 3)|((t & ~3) << 3)|((t & ~7) << 8));
694 695
            }
        else
696
            for( int i = 0; i < n; i++ )
697
            {
698 699
                int t = src[i] >> 3;
                ((ushort*)dst)[i] = (ushort)(t|(t << 5)|(t << 10));
700 701
            }
    }
702 703
    int greenBits;
};
704 705


706 707 708
#undef R2Y
#undef G2Y
#undef B2Y
A
Andrey Kamaev 已提交
709

710
enum
711
{
712 713 714 715 716 717 718 719
    yuv_shift = 14,
    xyz_shift = 12,
    R2Y = 4899,
    G2Y = 9617,
    B2Y = 1868,
    BLOCK_SIZE = 256
};

720

721 722 723
struct RGB5x52Gray
{
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
724

725 726
    RGB5x52Gray(int _greenBits) : greenBits(_greenBits) {}
    void operator()(const uchar* src, uchar* dst, int n) const
727
    {
728 729
        if( greenBits == 6 )
            for( int i = 0; i < n; i++ )
730
            {
731 732 733 734
                int t = ((ushort*)src)[i];
                dst[i] = (uchar)CV_DESCALE(((t << 3) & 0xf8)*B2Y +
                                           ((t >> 3) & 0xfc)*G2Y +
                                           ((t >> 8) & 0xf8)*R2Y, yuv_shift);
735 736
            }
        else
737
            for( int i = 0; i < n; i++ )
738
            {
739 740 741 742
                int t = ((ushort*)src)[i];
                dst[i] = (uchar)CV_DESCALE(((t << 3) & 0xf8)*B2Y +
                                           ((t >> 2) & 0xf8)*G2Y +
                                           ((t >> 7) & 0xf8)*R2Y, yuv_shift);
743 744
            }
    }
745 746
    int greenBits;
};
747 748


749
template<typename _Tp> struct RGB2Gray
750
{
751
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
752

753
    RGB2Gray(int _srccn, int blueIdx, const float* _coeffs) : srccn(_srccn)
754
    {
755 756 757 758 759
        static const float coeffs0[] = { 0.299f, 0.587f, 0.114f };
        memcpy( coeffs, _coeffs ? _coeffs : coeffs0, 3*sizeof(coeffs[0]) );
        if(blueIdx == 0)
            std::swap(coeffs[0], coeffs[2]);
    }
A
Andrey Kamaev 已提交
760

761 762 763 764 765 766 767 768 769 770 771
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int scn = srccn;
        float cb = coeffs[0], cg = coeffs[1], cr = coeffs[2];
        for(int i = 0; i < n; i++, src += scn)
            dst[i] = saturate_cast<_Tp>(src[0]*cb + src[1]*cg + src[2]*cr);
    }
    int srccn;
    float coeffs[3];
};

A
Andrey Kamaev 已提交
772

773 774 775
template<> struct RGB2Gray<uchar>
{
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
776

I
Ilya Lavrenov 已提交
777
    RGB2Gray(int _srccn, int blueIdx, const int* coeffs) : srccn(_srccn)
778 779 780
    {
        const int coeffs0[] = { R2Y, G2Y, B2Y };
        if(!coeffs) coeffs = coeffs0;
A
Andrey Kamaev 已提交
781

782 783
        int b = 0, g = 0, r = (1 << (yuv_shift-1));
        int db = coeffs[blueIdx^2], dg = coeffs[1], dr = coeffs[blueIdx];
A
Andrey Kamaev 已提交
784

785
        for( int i = 0; i < 256; i++, b += db, g += dg, r += dr )
786 787 788 789 790 791
        {
            tab[i] = b;
            tab[i+256] = g;
            tab[i+512] = r;
        }
    }
792
    void operator()(const uchar* src, uchar* dst, int n) const
793
    {
794
        int scn = srccn;
A
Andrey Kamaev 已提交
795
        const int* _tab = tab;
796 797
        for(int i = 0; i < n; i++, src += scn)
            dst[i] = (uchar)((_tab[src[0]] + _tab[src[1]+256] + _tab[src[2]+512]) >> yuv_shift);
798
    }
A
Andrey Kamaev 已提交
799
    int srccn;
800 801
    int tab[256*3];
};
802

A
Andrey Kamaev 已提交
803

804
template<> struct RGB2Gray<ushort>
805
{
806
    typedef ushort channel_type;
A
Andrey Kamaev 已提交
807

I
Ilya Lavrenov 已提交
808
    RGB2Gray(int _srccn, int blueIdx, const int* _coeffs) : srccn(_srccn)
809 810 811 812 813 814
    {
        static const int coeffs0[] = { R2Y, G2Y, B2Y };
        memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 3*sizeof(coeffs[0]));
        if( blueIdx == 0 )
            std::swap(coeffs[0], coeffs[2]);
    }
A
Andrey Kamaev 已提交
815

816 817 818 819 820 821 822 823 824
    void operator()(const ushort* src, ushort* dst, int n) const
    {
        int scn = srccn, cb = coeffs[0], cg = coeffs[1], cr = coeffs[2];
        for(int i = 0; i < n; i++, src += scn)
            dst[i] = (ushort)CV_DESCALE((unsigned)(src[0]*cb + src[1]*cg + src[2]*cr), yuv_shift);
    }
    int srccn;
    int coeffs[3];
};
825

A
Andrey Kamaev 已提交
826

827
///////////////////////////////////// RGB <-> YCrCb //////////////////////////////////////
828

829
template<typename _Tp> struct RGB2YCrCb_f
830
{
831
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
832

833
    RGB2YCrCb_f(int _srccn, int _blueIdx, const float* _coeffs) : srccn(_srccn), blueIdx(_blueIdx)
A
Andrey Kamaev 已提交
834 835 836 837 838 839
    {
        static const float coeffs0[] = {0.299f, 0.587f, 0.114f, 0.713f, 0.564f};
        memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 5*sizeof(coeffs[0]));
        if(blueIdx==0) std::swap(coeffs[0], coeffs[2]);
    }

840 841 842 843
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int scn = srccn, bidx = blueIdx;
        const _Tp delta = ColorChannel<_Tp>::half();
A
Andrey Kamaev 已提交
844
        float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2], C3 = coeffs[3], C4 = coeffs[4];
845 846 847 848 849 850 851 852 853 854
        n *= 3;
        for(int i = 0; i < n; i += 3, src += scn)
        {
            _Tp Y = saturate_cast<_Tp>(src[0]*C0 + src[1]*C1 + src[2]*C2);
            _Tp Cr = saturate_cast<_Tp>((src[bidx^2] - Y)*C3 + delta);
            _Tp Cb = saturate_cast<_Tp>((src[bidx] - Y)*C4 + delta);
            dst[i] = Y; dst[i+1] = Cr; dst[i+2] = Cb;
        }
    }
    int srccn, blueIdx;
A
Andrey Kamaev 已提交
855
    float coeffs[5];
856
};
857 858


859
template<typename _Tp> struct RGB2YCrCb_i
860
{
861
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
862

863
    RGB2YCrCb_i(int _srccn, int _blueIdx, const int* _coeffs)
A
Andrey Kamaev 已提交
864 865 866 867 868 869
        : srccn(_srccn), blueIdx(_blueIdx)
    {
        static const int coeffs0[] = {R2Y, G2Y, B2Y, 11682, 9241};
        memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 5*sizeof(coeffs[0]));
        if(blueIdx==0) std::swap(coeffs[0], coeffs[2]);
    }
870 871 872
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int scn = srccn, bidx = blueIdx;
A
Andrey Kamaev 已提交
873
        int C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2], C3 = coeffs[3], C4 = coeffs[4];
874 875 876 877 878 879 880 881 882 883 884 885 886
        int delta = ColorChannel<_Tp>::half()*(1 << yuv_shift);
        n *= 3;
        for(int i = 0; i < n; i += 3, src += scn)
        {
            int Y = CV_DESCALE(src[0]*C0 + src[1]*C1 + src[2]*C2, yuv_shift);
            int Cr = CV_DESCALE((src[bidx^2] - Y)*C3 + delta, yuv_shift);
            int Cb = CV_DESCALE((src[bidx] - Y)*C4 + delta, yuv_shift);
            dst[i] = saturate_cast<_Tp>(Y);
            dst[i+1] = saturate_cast<_Tp>(Cr);
            dst[i+2] = saturate_cast<_Tp>(Cb);
        }
    }
    int srccn, blueIdx;
A
Andrey Kamaev 已提交
887 888
    int coeffs[5];
};
889 890


891 892 893
template<typename _Tp> struct YCrCb2RGB_f
{
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
894

895
    YCrCb2RGB_f(int _dstcn, int _blueIdx, const float* _coeffs)
A
Andrey Kamaev 已提交
896 897 898 899 900
        : dstcn(_dstcn), blueIdx(_blueIdx)
    {
        static const float coeffs0[] = {1.403f, -0.714f, -0.344f, 1.773f};
        memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 4*sizeof(coeffs[0]));
    }
901
    void operator()(const _Tp* src, _Tp* dst, int n) const
902
    {
903 904 905 906 907
        int dcn = dstcn, bidx = blueIdx;
        const _Tp delta = ColorChannel<_Tp>::half(), alpha = ColorChannel<_Tp>::max();
        float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2], C3 = coeffs[3];
        n *= 3;
        for(int i = 0; i < n; i += 3, dst += dcn)
908
        {
909 910 911
            _Tp Y = src[i];
            _Tp Cr = src[i+1];
            _Tp Cb = src[i+2];
A
Andrey Kamaev 已提交
912

913 914 915
            _Tp b = saturate_cast<_Tp>(Y + (Cb - delta)*C3);
            _Tp g = saturate_cast<_Tp>(Y + (Cb - delta)*C2 + (Cr - delta)*C1);
            _Tp r = saturate_cast<_Tp>(Y + (Cr - delta)*C0);
A
Andrey Kamaev 已提交
916

917 918 919
            dst[bidx] = b; dst[1] = g; dst[bidx^2] = r;
            if( dcn == 4 )
                dst[3] = alpha;
920 921
        }
    }
922
    int dstcn, blueIdx;
A
Andrey Kamaev 已提交
923 924
    float coeffs[4];
};
925 926


927
template<typename _Tp> struct YCrCb2RGB_i
928
{
929
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
930

931 932
    YCrCb2RGB_i(int _dstcn, int _blueIdx, const int* _coeffs)
        : dstcn(_dstcn), blueIdx(_blueIdx)
933
    {
A
Andrey Kamaev 已提交
934 935
        static const int coeffs0[] = {22987, -11698, -5636, 29049};
        memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 4*sizeof(coeffs[0]));
936
    }
A
Andrey Kamaev 已提交
937

938 939 940 941 942 943 944
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int dcn = dstcn, bidx = blueIdx;
        const _Tp delta = ColorChannel<_Tp>::half(), alpha = ColorChannel<_Tp>::max();
        int C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2], C3 = coeffs[3];
        n *= 3;
        for(int i = 0; i < n; i += 3, dst += dcn)
945
        {
946 947 948
            _Tp Y = src[i];
            _Tp Cr = src[i+1];
            _Tp Cb = src[i+2];
A
Andrey Kamaev 已提交
949

950 951 952
            int b = Y + CV_DESCALE((Cb - delta)*C3, yuv_shift);
            int g = Y + CV_DESCALE((Cb - delta)*C2 + (Cr - delta)*C1, yuv_shift);
            int r = Y + CV_DESCALE((Cr - delta)*C0, yuv_shift);
A
Andrey Kamaev 已提交
953

954 955 956 957 958
            dst[bidx] = saturate_cast<_Tp>(b);
            dst[1] = saturate_cast<_Tp>(g);
            dst[bidx^2] = saturate_cast<_Tp>(r);
            if( dcn == 4 )
                dst[3] = alpha;
959 960
        }
    }
961 962
    int dstcn, blueIdx;
    int coeffs[4];
A
Andrey Kamaev 已提交
963 964
};

965

966
////////////////////////////////////// RGB <-> XYZ ///////////////////////////////////////
967

968 969 970 971 972 973
static const float sRGB2XYZ_D65[] =
{
    0.412453f, 0.357580f, 0.180423f,
    0.212671f, 0.715160f, 0.072169f,
    0.019334f, 0.119193f, 0.950227f
};
A
Andrey Kamaev 已提交
974

975
static const float XYZ2sRGB_D65[] =
976
{
977 978 979 980
    3.240479f, -1.53715f, -0.498535f,
    -0.969256f, 1.875991f, 0.041556f,
    0.055648f, -0.204043f, 1.057311f
};
A
Andrey Kamaev 已提交
981

982 983 984
template<typename _Tp> struct RGB2XYZ_f
{
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
985

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
    RGB2XYZ_f(int _srccn, int blueIdx, const float* _coeffs) : srccn(_srccn)
    {
        memcpy(coeffs, _coeffs ? _coeffs : sRGB2XYZ_D65, 9*sizeof(coeffs[0]));
        if(blueIdx == 0)
        {
            std::swap(coeffs[0], coeffs[2]);
            std::swap(coeffs[3], coeffs[5]);
            std::swap(coeffs[6], coeffs[8]);
        }
    }
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int scn = srccn;
        float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
              C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
              C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
A
Andrey Kamaev 已提交
1002

1003 1004 1005
        n *= 3;
        for(int i = 0; i < n; i += 3, src += scn)
        {
A
Andrey Kamaev 已提交
1006 1007 1008
            _Tp X = saturate_cast<_Tp>(src[0]*C0 + src[1]*C1 + src[2]*C2);
            _Tp Y = saturate_cast<_Tp>(src[0]*C3 + src[1]*C4 + src[2]*C5);
            _Tp Z = saturate_cast<_Tp>(src[0]*C6 + src[1]*C7 + src[2]*C8);
1009 1010 1011 1012 1013
            dst[i] = X; dst[i+1] = Y; dst[i+2] = Z;
        }
    }
    int srccn;
    float coeffs[9];
1014 1015 1016
};


1017
template<typename _Tp> struct RGB2XYZ_i
1018
{
1019
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
1020

1021 1022 1023 1024
    RGB2XYZ_i(int _srccn, int blueIdx, const float* _coeffs) : srccn(_srccn)
    {
        static const int coeffs0[] =
        {
A
Andrey Kamaev 已提交
1025 1026
            1689,    1465,    739,
            871,     2929,    296,
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
            79,      488,     3892
        };
        for( int i = 0; i < 9; i++ )
            coeffs[i] = _coeffs ? cvRound(_coeffs[i]*(1 << xyz_shift)) : coeffs0[i];
        if(blueIdx == 0)
        {
            std::swap(coeffs[0], coeffs[2]);
            std::swap(coeffs[3], coeffs[5]);
            std::swap(coeffs[6], coeffs[8]);
        }
    }
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int scn = srccn;
        int C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
            C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
            C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
        n *= 3;
        for(int i = 0; i < n; i += 3, src += scn)
        {
            int X = CV_DESCALE(src[0]*C0 + src[1]*C1 + src[2]*C2, xyz_shift);
            int Y = CV_DESCALE(src[0]*C3 + src[1]*C4 + src[2]*C5, xyz_shift);
            int Z = CV_DESCALE(src[0]*C6 + src[1]*C7 + src[2]*C8, xyz_shift);
            dst[i] = saturate_cast<_Tp>(X); dst[i+1] = saturate_cast<_Tp>(Y);
            dst[i+2] = saturate_cast<_Tp>(Z);
        }
    }
    int srccn;
    int coeffs[9];
};
A
Andrey Kamaev 已提交
1057 1058


1059 1060 1061
template<typename _Tp> struct XYZ2RGB_f
{
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
1062

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    XYZ2RGB_f(int _dstcn, int _blueIdx, const float* _coeffs)
    : dstcn(_dstcn), blueIdx(_blueIdx)
    {
        memcpy(coeffs, _coeffs ? _coeffs : XYZ2sRGB_D65, 9*sizeof(coeffs[0]));
        if(blueIdx == 0)
        {
            std::swap(coeffs[0], coeffs[6]);
            std::swap(coeffs[1], coeffs[7]);
            std::swap(coeffs[2], coeffs[8]);
        }
    }
A
Andrey Kamaev 已提交
1074

1075 1076 1077
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int dcn = dstcn;
A
Andrey Kamaev 已提交
1078
        _Tp alpha = ColorChannel<_Tp>::max();
1079 1080 1081 1082 1083 1084
        float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
              C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
              C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
        n *= 3;
        for(int i = 0; i < n; i += 3, dst += dcn)
        {
A
Andrey Kamaev 已提交
1085 1086 1087
            _Tp B = saturate_cast<_Tp>(src[i]*C0 + src[i+1]*C1 + src[i+2]*C2);
            _Tp G = saturate_cast<_Tp>(src[i]*C3 + src[i+1]*C4 + src[i+2]*C5);
            _Tp R = saturate_cast<_Tp>(src[i]*C6 + src[i+1]*C7 + src[i+2]*C8);
1088
            dst[0] = B; dst[1] = G; dst[2] = R;
A
Andrey Kamaev 已提交
1089 1090
            if( dcn == 4 )
                dst[3] = alpha;
1091 1092 1093 1094
        }
    }
    int dstcn, blueIdx;
    float coeffs[9];
1095 1096 1097
};


1098
template<typename _Tp> struct XYZ2RGB_i
1099
{
1100
    typedef _Tp channel_type;
A
Andrey Kamaev 已提交
1101

1102 1103
    XYZ2RGB_i(int _dstcn, int _blueIdx, const int* _coeffs)
    : dstcn(_dstcn), blueIdx(_blueIdx)
1104
    {
1105
        static const int coeffs0[] =
1106
        {
A
Andrey Kamaev 已提交
1107 1108
            13273,  -6296,  -2042,
            -3970,   7684,    170,
1109 1110 1111 1112
              228,   -836,   4331
        };
        for(int i = 0; i < 9; i++)
            coeffs[i] = _coeffs ? cvRound(_coeffs[i]*(1 << xyz_shift)) : coeffs0[i];
A
Andrey Kamaev 已提交
1113

1114 1115 1116 1117 1118
        if(blueIdx == 0)
        {
            std::swap(coeffs[0], coeffs[6]);
            std::swap(coeffs[1], coeffs[7]);
            std::swap(coeffs[2], coeffs[8]);
1119 1120
        }
    }
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        int dcn = dstcn;
        _Tp alpha = ColorChannel<_Tp>::max();
        int C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
            C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
            C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
        n *= 3;
        for(int i = 0; i < n; i += 3, dst += dcn)
        {
            int B = CV_DESCALE(src[i]*C0 + src[i+1]*C1 + src[i+2]*C2, xyz_shift);
            int G = CV_DESCALE(src[i]*C3 + src[i+1]*C4 + src[i+2]*C5, xyz_shift);
            int R = CV_DESCALE(src[i]*C6 + src[i+1]*C7 + src[i+2]*C8, xyz_shift);
            dst[0] = saturate_cast<_Tp>(B); dst[1] = saturate_cast<_Tp>(G);
            dst[2] = saturate_cast<_Tp>(R);
            if( dcn == 4 )
A
Andrey Kamaev 已提交
1137
                dst[3] = alpha;
1138 1139 1140 1141 1142
        }
    }
    int dstcn, blueIdx;
    int coeffs[9];
};
A
Andrey Kamaev 已提交
1143

1144 1145 1146 1147 1148 1149 1150

////////////////////////////////////// RGB <-> HSV ///////////////////////////////////////


struct RGB2HSV_b
{
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
1151

1152
    RGB2HSV_b(int _srccn, int _blueIdx, int _hrange)
1153 1154 1155 1156
    : srccn(_srccn), blueIdx(_blueIdx), hrange(_hrange)
    {
        CV_Assert( hrange == 180 || hrange == 256 );
    }
A
Andrey Kamaev 已提交
1157

1158
    void operator()(const uchar* src, uchar* dst, int n) const
1159
    {
1160 1161
        int i, bidx = blueIdx, scn = srccn;
        const int hsv_shift = 12;
A
Andrey Kamaev 已提交
1162

1163 1164 1165 1166
        static int sdiv_table[256];
        static int hdiv_table180[256];
        static int hdiv_table256[256];
        static volatile bool initialized = false;
A
Andrey Kamaev 已提交
1167

1168 1169
        int hr = hrange;
        const int* hdiv_table = hr == 180 ? hdiv_table180 : hdiv_table256;
1170
        n *= 3;
A
Andrey Kamaev 已提交
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
        if( !initialized )
        {
            sdiv_table[0] = hdiv_table180[0] = hdiv_table256[0] = 0;
            for( i = 1; i < 256; i++ )
            {
                sdiv_table[i] = saturate_cast<int>((255 << hsv_shift)/(1.*i));
                hdiv_table180[i] = saturate_cast<int>((180 << hsv_shift)/(6.*i));
                hdiv_table256[i] = saturate_cast<int>((256 << hsv_shift)/(6.*i));
            }
            initialized = true;
        }
A
Andrey Kamaev 已提交
1183

1184
        for( i = 0; i < n; i += 3, src += scn )
1185
        {
1186
            int b = src[bidx], g = src[1], r = src[bidx^2];
1187 1188 1189
            int h, s, v = b;
            int vmin = b, diff;
            int vr, vg;
A
Andrey Kamaev 已提交
1190

1191 1192 1193 1194
            CV_CALC_MAX_8U( v, g );
            CV_CALC_MAX_8U( v, r );
            CV_CALC_MIN_8U( vmin, g );
            CV_CALC_MIN_8U( vmin, r );
A
Andrey Kamaev 已提交
1195

1196 1197 1198
            diff = v - vmin;
            vr = v == r ? -1 : 0;
            vg = v == g ? -1 : 0;
A
Andrey Kamaev 已提交
1199

1200
            s = (diff * sdiv_table[v] + (1 << (hsv_shift-1))) >> hsv_shift;
1201 1202
            h = (vr & (g - b)) +
                (~vr & ((vg & (b - r + 2 * diff)) + ((~vg) & (r - g + 4 * diff))));
1203
            h = (h * hdiv_table[diff] + (1 << (hsv_shift-1))) >> hsv_shift;
1204
            h += h < 0 ? hr : 0;
A
Andrey Kamaev 已提交
1205

1206
            dst[i] = saturate_cast<uchar>(h);
1207 1208 1209 1210
            dst[i+1] = (uchar)s;
            dst[i+2] = (uchar)v;
        }
    }
A
Andrey Kamaev 已提交
1211

1212
    int srccn, blueIdx, hrange;
A
Andrey Kamaev 已提交
1213 1214
};

1215

1216
struct RGB2HSV_f
1217
{
1218
    typedef float channel_type;
A
Andrey Kamaev 已提交
1219

1220 1221
    RGB2HSV_f(int _srccn, int _blueIdx, float _hrange)
    : srccn(_srccn), blueIdx(_blueIdx), hrange(_hrange) {}
A
Andrey Kamaev 已提交
1222

1223
    void operator()(const float* src, float* dst, int n) const
1224
    {
1225 1226 1227
        int i, bidx = blueIdx, scn = srccn;
        float hscale = hrange*(1.f/360.f);
        n *= 3;
A
Andrey Kamaev 已提交
1228

1229
        for( i = 0; i < n; i += 3, src += scn )
1230
        {
1231
            float b = src[bidx], g = src[1], r = src[bidx^2];
1232
            float h, s, v;
A
Andrey Kamaev 已提交
1233

1234
            float vmin, diff;
A
Andrey Kamaev 已提交
1235

1236 1237 1238 1239 1240
            v = vmin = r;
            if( v < g ) v = g;
            if( v < b ) v = b;
            if( vmin > g ) vmin = g;
            if( vmin > b ) vmin = b;
A
Andrey Kamaev 已提交
1241

1242 1243 1244 1245 1246 1247 1248 1249 1250
            diff = v - vmin;
            s = diff/(float)(fabs(v) + FLT_EPSILON);
            diff = (float)(60./(diff + FLT_EPSILON));
            if( v == r )
                h = (g - b)*diff;
            else if( v == g )
                h = (b - r)*diff + 120.f;
            else
                h = (r - g)*diff + 240.f;
A
Andrey Kamaev 已提交
1251

1252
            if( h < 0 ) h += 360.f;
A
Andrey Kamaev 已提交
1253

1254
            dst[i] = h*hscale;
1255 1256 1257 1258
            dst[i+1] = s;
            dst[i+2] = v;
        }
    }
A
Andrey Kamaev 已提交
1259

1260 1261 1262
    int srccn, blueIdx;
    float hrange;
};
1263 1264


1265
struct HSV2RGB_f
1266
{
1267
    typedef float channel_type;
A
Andrey Kamaev 已提交
1268

1269 1270
    HSV2RGB_f(int _dstcn, int _blueIdx, float _hrange)
    : dstcn(_dstcn), blueIdx(_blueIdx), hscale(6.f/_hrange) {}
A
Andrey Kamaev 已提交
1271

1272
    void operator()(const float* src, float* dst, int n) const
1273
    {
1274 1275 1276 1277
        int i, bidx = blueIdx, dcn = dstcn;
        float _hscale = hscale;
        float alpha = ColorChannel<float>::max();
        n *= 3;
A
Andrey Kamaev 已提交
1278

1279
        for( i = 0; i < n; i += 3, dst += dcn )
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
        {
            float h = src[i], s = src[i+1], v = src[i+2];
            float b, g, r;

            if( s == 0 )
                b = g = r = v;
            else
            {
                static const int sector_data[][3]=
                    {{1,3,0}, {1,0,2}, {3,0,1}, {0,2,1}, {0,1,3}, {2,1,0}};
                float tab[4];
                int sector;
1292
                h *= _hscale;
1293 1294 1295 1296 1297 1298
                if( h < 0 )
                    do h += 6; while( h < 0 );
                else if( h >= 6 )
                    do h -= 6; while( h >= 6 );
                sector = cvFloor(h);
                h -= sector;
1299 1300 1301 1302 1303
                if( (unsigned)sector >= 6u )
                {
                    sector = 0;
                    h = 0.f;
                }
1304 1305 1306 1307 1308

                tab[0] = v;
                tab[1] = v*(1.f - s);
                tab[2] = v*(1.f - s*h);
                tab[3] = v*(1.f - s*(1.f - h));
A
Andrey Kamaev 已提交
1309

1310 1311 1312 1313 1314
                b = tab[sector_data[sector][0]];
                g = tab[sector_data[sector][1]];
                r = tab[sector_data[sector][2]];
            }

1315
            dst[bidx] = b;
1316
            dst[1] = g;
1317 1318 1319
            dst[bidx^2] = r;
            if( dcn == 4 )
                dst[3] = alpha;
1320 1321 1322
        }
    }

1323 1324 1325
    int dstcn, blueIdx;
    float hscale;
};
A
Andrey Kamaev 已提交
1326

1327

1328
struct HSV2RGB_b
1329
{
1330
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
1331

1332
    HSV2RGB_b(int _dstcn, int _blueIdx, int _hrange)
1333
    : dstcn(_dstcn), cvt(3, _blueIdx, (float)_hrange)
1334
    {}
A
Andrey Kamaev 已提交
1335

1336
    void operator()(const uchar* src, uchar* dst, int n) const
1337
    {
1338 1339 1340
        int i, j, dcn = dstcn;
        uchar alpha = ColorChannel<uchar>::max();
        float buf[3*BLOCK_SIZE];
A
Andrey Kamaev 已提交
1341

1342
        for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 )
1343
        {
1344
            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Kamaev 已提交
1345

1346
            for( j = 0; j < dn*3; j += 3 )
1347
            {
1348 1349 1350
                buf[j] = src[j];
                buf[j+1] = src[j+1]*(1.f/255.f);
                buf[j+2] = src[j+2]*(1.f/255.f);
1351
            }
1352
            cvt(buf, buf, dn);
A
Andrey Kamaev 已提交
1353

1354
            for( j = 0; j < dn*3; j += 3, dst += dcn )
1355
            {
1356 1357 1358 1359 1360
                dst[0] = saturate_cast<uchar>(buf[j]*255.f);
                dst[1] = saturate_cast<uchar>(buf[j+1]*255.f);
                dst[2] = saturate_cast<uchar>(buf[j+2]*255.f);
                if( dcn == 4 )
                    dst[3] = alpha;
1361 1362 1363
            }
        }
    }
A
Andrey Kamaev 已提交
1364

1365 1366 1367
    int dstcn;
    HSV2RGB_f cvt;
};
1368

A
Andrey Kamaev 已提交
1369

1370
///////////////////////////////////// RGB <-> HLS ////////////////////////////////////////
1371

1372
struct RGB2HLS_f
1373
{
1374
    typedef float channel_type;
A
Andrey Kamaev 已提交
1375

1376 1377
    RGB2HLS_f(int _srccn, int _blueIdx, float _hrange)
    : srccn(_srccn), blueIdx(_blueIdx), hrange(_hrange) {}
A
Andrey Kamaev 已提交
1378

1379
    void operator()(const float* src, float* dst, int n) const
1380
    {
1381 1382 1383
        int i, bidx = blueIdx, scn = srccn;
        float hscale = hrange*(1.f/360.f);
        n *= 3;
A
Andrey Kamaev 已提交
1384

1385
        for( i = 0; i < n; i += 3, src += scn )
1386
        {
1387
            float b = src[bidx], g = src[1], r = src[bidx^2];
1388 1389
            float h = 0.f, s = 0.f, l;
            float vmin, vmax, diff;
A
Andrey Kamaev 已提交
1390

1391 1392 1393 1394 1395
            vmax = vmin = r;
            if( vmax < g ) vmax = g;
            if( vmax < b ) vmax = b;
            if( vmin > g ) vmin = g;
            if( vmin > b ) vmin = b;
A
Andrey Kamaev 已提交
1396

1397 1398
            diff = vmax - vmin;
            l = (vmax + vmin)*0.5f;
A
Andrey Kamaev 已提交
1399

1400 1401 1402 1403
            if( diff > FLT_EPSILON )
            {
                s = l < 0.5f ? diff/(vmax + vmin) : diff/(2 - vmax - vmin);
                diff = 60.f/diff;
A
Andrey Kamaev 已提交
1404

1405 1406 1407 1408 1409 1410
                if( vmax == r )
                    h = (g - b)*diff;
                else if( vmax == g )
                    h = (b - r)*diff + 120.f;
                else
                    h = (r - g)*diff + 240.f;
A
Andrey Kamaev 已提交
1411

1412 1413
                if( h < 0.f ) h += 360.f;
            }
A
Andrey Kamaev 已提交
1414

1415
            dst[i] = h*hscale;
1416 1417 1418 1419
            dst[i+1] = l;
            dst[i+2] = s;
        }
    }
A
Andrey Kamaev 已提交
1420

1421 1422 1423
    int srccn, blueIdx;
    float hrange;
};
A
Andrey Kamaev 已提交
1424 1425


1426
struct RGB2HLS_b
1427
{
1428
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
1429

1430 1431
    RGB2HLS_b(int _srccn, int _blueIdx, int _hrange)
    : srccn(_srccn), cvt(3, _blueIdx, (float)_hrange) {}
A
Andrey Kamaev 已提交
1432

1433
    void operator()(const uchar* src, uchar* dst, int n) const
1434
    {
1435 1436
        int i, j, scn = srccn;
        float buf[3*BLOCK_SIZE];
A
Andrey Kamaev 已提交
1437

1438
        for( i = 0; i < n; i += BLOCK_SIZE, dst += BLOCK_SIZE*3 )
1439
        {
1440
            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Kamaev 已提交
1441

1442
            for( j = 0; j < dn*3; j += 3, src += scn )
1443
            {
1444 1445 1446
                buf[j] = src[0]*(1.f/255.f);
                buf[j+1] = src[1]*(1.f/255.f);
                buf[j+2] = src[2]*(1.f/255.f);
1447
            }
1448
            cvt(buf, buf, dn);
A
Andrey Kamaev 已提交
1449

1450
            for( j = 0; j < dn*3; j += 3 )
1451
            {
1452 1453 1454
                dst[j] = saturate_cast<uchar>(buf[j]);
                dst[j+1] = saturate_cast<uchar>(buf[j+1]*255.f);
                dst[j+2] = saturate_cast<uchar>(buf[j+2]*255.f);
1455 1456 1457
            }
        }
    }
A
Andrey Kamaev 已提交
1458

1459 1460 1461
    int srccn;
    RGB2HLS_f cvt;
};
A
Andrey Kamaev 已提交
1462

1463

1464 1465 1466
struct HLS2RGB_f
{
    typedef float channel_type;
A
Andrey Kamaev 已提交
1467

1468 1469
    HLS2RGB_f(int _dstcn, int _blueIdx, float _hrange)
    : dstcn(_dstcn), blueIdx(_blueIdx), hscale(6.f/_hrange) {}
A
Andrey Kamaev 已提交
1470

1471
    void operator()(const float* src, float* dst, int n) const
1472
    {
1473 1474 1475 1476
        int i, bidx = blueIdx, dcn = dstcn;
        float _hscale = hscale;
        float alpha = ColorChannel<float>::max();
        n *= 3;
A
Andrey Kamaev 已提交
1477

1478
        for( i = 0; i < n; i += 3, dst += dcn )
1479 1480 1481
        {
            float h = src[i], l = src[i+1], s = src[i+2];
            float b, g, r;
A
Andrey Kamaev 已提交
1482

1483 1484 1485 1486 1487
            if( s == 0 )
                b = g = r = l;
            else
            {
                static const int sector_data[][3]=
1488
                {{1,3,0}, {1,0,2}, {3,0,1}, {0,2,1}, {0,1,3}, {2,1,0}};
1489 1490
                float tab[4];
                int sector;
A
Andrey Kamaev 已提交
1491

1492 1493
                float p2 = l <= 0.5f ? l*(1 + s) : l + s - l*s;
                float p1 = 2*l - p2;
A
Andrey Kamaev 已提交
1494

1495
                h *= _hscale;
1496 1497 1498 1499
                if( h < 0 )
                    do h += 6; while( h < 0 );
                else if( h >= 6 )
                    do h -= 6; while( h >= 6 );
A
Andrey Kamaev 已提交
1500

1501 1502 1503
                assert( 0 <= h && h < 6 );
                sector = cvFloor(h);
                h -= sector;
A
Andrey Kamaev 已提交
1504

1505 1506 1507 1508
                tab[0] = p2;
                tab[1] = p1;
                tab[2] = p1 + (p2 - p1)*(1-h);
                tab[3] = p1 + (p2 - p1)*h;
A
Andrey Kamaev 已提交
1509

1510 1511 1512 1513
                b = tab[sector_data[sector][0]];
                g = tab[sector_data[sector][1]];
                r = tab[sector_data[sector][2]];
            }
A
Andrey Kamaev 已提交
1514

1515
            dst[bidx] = b;
1516
            dst[1] = g;
1517 1518 1519
            dst[bidx^2] = r;
            if( dcn == 4 )
                dst[3] = alpha;
1520 1521
        }
    }
A
Andrey Kamaev 已提交
1522

1523 1524 1525
    int dstcn, blueIdx;
    float hscale;
};
A
Andrey Kamaev 已提交
1526

1527

1528
struct HLS2RGB_b
1529
{
1530
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
1531

1532
    HLS2RGB_b(int _dstcn, int _blueIdx, int _hrange)
1533
    : dstcn(_dstcn), cvt(3, _blueIdx, (float)_hrange)
1534
    {}
A
Andrey Kamaev 已提交
1535

1536
    void operator()(const uchar* src, uchar* dst, int n) const
1537
    {
1538 1539 1540
        int i, j, dcn = dstcn;
        uchar alpha = ColorChannel<uchar>::max();
        float buf[3*BLOCK_SIZE];
A
Andrey Kamaev 已提交
1541

1542
        for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 )
1543
        {
1544
            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Kamaev 已提交
1545

1546 1547 1548 1549 1550 1551 1552
            for( j = 0; j < dn*3; j += 3 )
            {
                buf[j] = src[j];
                buf[j+1] = src[j+1]*(1.f/255.f);
                buf[j+2] = src[j+2]*(1.f/255.f);
            }
            cvt(buf, buf, dn);
A
Andrey Kamaev 已提交
1553

1554
            for( j = 0; j < dn*3; j += 3, dst += dcn )
1555
            {
1556 1557 1558 1559 1560
                dst[0] = saturate_cast<uchar>(buf[j]*255.f);
                dst[1] = saturate_cast<uchar>(buf[j+1]*255.f);
                dst[2] = saturate_cast<uchar>(buf[j+2]*255.f);
                if( dcn == 4 )
                    dst[3] = alpha;
1561 1562 1563
            }
        }
    }
A
Andrey Kamaev 已提交
1564

1565 1566 1567
    int dstcn;
    HLS2RGB_f cvt;
};
1568

A
Andrey Kamaev 已提交
1569

1570
///////////////////////////////////// RGB <-> L*a*b* /////////////////////////////////////
1571

1572
static const float D65[] = { 0.950456f, 1.f, 1.088754f };
1573

1574 1575 1576
enum { LAB_CBRT_TAB_SIZE = 1024, GAMMA_TAB_SIZE = 1024 };
static float LabCbrtTab[LAB_CBRT_TAB_SIZE*4];
static const float LabCbrtTabScale = LAB_CBRT_TAB_SIZE/1.5f;
1577

1578 1579
static float sRGBGammaTab[GAMMA_TAB_SIZE*4], sRGBInvGammaTab[GAMMA_TAB_SIZE*4];
static const float GammaTabScale = (float)GAMMA_TAB_SIZE;
A
Andrey Kamaev 已提交
1580 1581

static ushort sRGBGammaTab_b[256], linearGammaTab_b[256];
1582 1583 1584 1585 1586 1587
#undef lab_shift
#define lab_shift xyz_shift
#define gamma_shift 3
#define lab_shift2 (lab_shift + gamma_shift)
#define LAB_CBRT_TAB_SIZE_B (256*3/2*(1<<gamma_shift))
static ushort LabCbrtTab_b[LAB_CBRT_TAB_SIZE_B];
A
Andrey Kamaev 已提交
1588

1589 1590 1591 1592
static void initLabTabs()
{
    static bool initialized = false;
    if(!initialized)
1593
    {
1594
        float f[LAB_CBRT_TAB_SIZE+1], g[GAMMA_TAB_SIZE+1], ig[GAMMA_TAB_SIZE+1], scale = 1.f/LabCbrtTabScale;
1595 1596
        int i;
        for(i = 0; i <= LAB_CBRT_TAB_SIZE; i++)
1597
        {
1598 1599 1600 1601
            float x = i*scale;
            f[i] = x < 0.008856f ? x*7.787f + 0.13793103448275862f : cvCbrt(x);
        }
        splineBuild(f, LAB_CBRT_TAB_SIZE, LabCbrtTab);
A
Andrey Kamaev 已提交
1602

1603 1604 1605 1606
        scale = 1.f/GammaTabScale;
        for(i = 0; i <= GAMMA_TAB_SIZE; i++)
        {
            float x = i*scale;
1607 1608
            g[i] = x <= 0.04045f ? x*(1.f/12.92f) : (float)std::pow((double)(x + 0.055)*(1./1.055), 2.4);
            ig[i] = x <= 0.0031308 ? x*12.92f : (float)(1.055*std::pow((double)x, 1./2.4) - 0.055);
1609 1610 1611
        }
        splineBuild(g, GAMMA_TAB_SIZE, sRGBGammaTab);
        splineBuild(ig, GAMMA_TAB_SIZE, sRGBInvGammaTab);
A
Andrey Kamaev 已提交
1612

1613 1614 1615
        for(i = 0; i < 256; i++)
        {
            float x = i*(1.f/255.f);
1616
            sRGBGammaTab_b[i] = saturate_cast<ushort>(255.f*(1 << gamma_shift)*(x <= 0.04045f ? x*(1.f/12.92f) : (float)std::pow((double)(x + 0.055)*(1./1.055), 2.4)));
1617
            linearGammaTab_b[i] = (ushort)(i*(1 << gamma_shift));
1618
        }
A
Andrey Kamaev 已提交
1619

1620 1621 1622 1623 1624 1625
        for(i = 0; i < LAB_CBRT_TAB_SIZE_B; i++)
        {
            float x = i*(1.f/(255.f*(1 << gamma_shift)));
            LabCbrtTab_b[i] = saturate_cast<ushort>((1 << lab_shift2)*(x < 0.008856f ? x*7.787f + 0.13793103448275862f : cvCbrt(x)));
        }
        initialized = true;
1626 1627 1628
    }
}

1629 1630 1631
struct RGB2Lab_b
{
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
1632

1633 1634 1635 1636
    RGB2Lab_b(int _srccn, int blueIdx, const float* _coeffs,
              const float* _whitept, bool _srgb)
    : srccn(_srccn), srgb(_srgb)
    {
1637
        static volatile int _3 = 3;
1638
        initLabTabs();
A
Andrey Kamaev 已提交
1639

1640 1641 1642 1643 1644
        if (!_coeffs)
            _coeffs = sRGB2XYZ_D65;
        if (!_whitept)
            _whitept = D65;

1645 1646 1647 1648 1649 1650
        float scale[] =
        {
            (1 << lab_shift)/_whitept[0],
            (float)(1 << lab_shift),
            (1 << lab_shift)/_whitept[2]
        };
A
Andrey Kamaev 已提交
1651

1652
        for( int i = 0; i < _3; i++ )
1653 1654 1655 1656
        {
            coeffs[i*3+(blueIdx^2)] = cvRound(_coeffs[i*3]*scale[i]);
            coeffs[i*3+1] = cvRound(_coeffs[i*3+1]*scale[i]);
            coeffs[i*3+blueIdx] = cvRound(_coeffs[i*3+2]*scale[i]);
A
Andrey Kamaev 已提交
1657

1658 1659
            CV_Assert( coeffs[i] >= 0 && coeffs[i*3+1] >= 0 && coeffs[i*3+2] >= 0 &&
                      coeffs[i*3] + coeffs[i*3+1] + coeffs[i*3+2] < 2*(1 << lab_shift) );
1660 1661
        }
    }
A
Andrey Kamaev 已提交
1662

1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
    void operator()(const uchar* src, uchar* dst, int n) const
    {
        const int Lscale = (116*255+50)/100;
        const int Lshift = -((16*255*(1 << lab_shift2) + 50)/100);
        const ushort* tab = srgb ? sRGBGammaTab_b : linearGammaTab_b;
        int i, scn = srccn;
        int C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
            C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
            C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
        n *= 3;
A
Andrey Kamaev 已提交
1673

1674 1675 1676 1677 1678 1679
        for( i = 0; i < n; i += 3, src += scn )
        {
            int R = tab[src[0]], G = tab[src[1]], B = tab[src[2]];
            int fX = LabCbrtTab_b[CV_DESCALE(R*C0 + G*C1 + B*C2, lab_shift)];
            int fY = LabCbrtTab_b[CV_DESCALE(R*C3 + G*C4 + B*C5, lab_shift)];
            int fZ = LabCbrtTab_b[CV_DESCALE(R*C6 + G*C7 + B*C8, lab_shift)];
A
Andrey Kamaev 已提交
1680

1681 1682 1683
            int L = CV_DESCALE( Lscale*fY + Lshift, lab_shift2 );
            int a = CV_DESCALE( 500*(fX - fY) + 128*(1 << lab_shift2), lab_shift2 );
            int b = CV_DESCALE( 200*(fY - fZ) + 128*(1 << lab_shift2), lab_shift2 );
A
Andrey Kamaev 已提交
1684

1685 1686 1687 1688 1689
            dst[i] = saturate_cast<uchar>(L);
            dst[i+1] = saturate_cast<uchar>(a);
            dst[i+2] = saturate_cast<uchar>(b);
        }
    }
A
Andrey Kamaev 已提交
1690

1691 1692 1693
    int srccn;
    int coeffs[9];
    bool srgb;
1694
};
A
Andrey Kamaev 已提交
1695 1696


1697 1698 1699
#define clip(value) \
    value < 0.0f ? 0.0f : value > 1.0f ? 1.0f : value;

1700
struct RGB2Lab_f
1701
{
1702
    typedef float channel_type;
A
Andrey Kamaev 已提交
1703

1704 1705 1706
    RGB2Lab_f(int _srccn, int blueIdx, const float* _coeffs,
              const float* _whitept, bool _srgb)
    : srccn(_srccn), srgb(_srgb)
1707
    {
1708
        volatile int _3 = 3;
1709
        initLabTabs();
A
Andrey Kamaev 已提交
1710

1711 1712 1713 1714
        if (!_coeffs)
            _coeffs = sRGB2XYZ_D65;
        if (!_whitept)
            _whitept = D65;
1715

1716
        float scale[] = { 1.0f / _whitept[0], 1.0f, 1.0f / _whitept[2] };
A
Andrey Kamaev 已提交
1717

1718
        for( int i = 0; i < _3; i++ )
1719
        {
1720 1721 1722 1723
            int j = i * 3;
            coeffs[j + (blueIdx ^ 2)] = _coeffs[j] * scale[i];
            coeffs[j + 1] = _coeffs[j + 1] * scale[i];
            coeffs[j + blueIdx] = _coeffs[j + 2] * scale[i];
1724

1725 1726
            CV_Assert( coeffs[j] >= 0 && coeffs[j + 1] >= 0 && coeffs[j + 2] >= 0 &&
                       coeffs[j] + coeffs[j + 1] + coeffs[j + 2] < 1.5f*LabCbrtTabScale );
1727 1728
        }
    }
A
Andrey Kamaev 已提交
1729

1730 1731 1732 1733 1734 1735 1736 1737 1738
    void operator()(const float* src, float* dst, int n) const
    {
        int i, scn = srccn;
        float gscale = GammaTabScale;
        const float* gammaTab = srgb ? sRGBGammaTab : 0;
        float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
              C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
              C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
        n *= 3;
A
Andrey Kamaev 已提交
1739

A
Andrey Kamaev 已提交
1740 1741
        static const float _1_3 = 1.0f / 3.0f;
        static const float _a = 16.0f / 116.0f;
1742
        for (i = 0; i < n; i += 3, src += scn )
1743
        {
1744 1745 1746
            float R = clip(src[0]);
            float G = clip(src[1]);
            float B = clip(src[2]);
1747

1748
            if (gammaTab)
1749
            {
1750 1751 1752
                R = splineInterpolate(R * gscale, gammaTab, GAMMA_TAB_SIZE);
                G = splineInterpolate(G * gscale, gammaTab, GAMMA_TAB_SIZE);
                B = splineInterpolate(B * gscale, gammaTab, GAMMA_TAB_SIZE);
1753
            }
1754 1755 1756
            float X = R*C0 + G*C1 + B*C2;
            float Y = R*C3 + G*C4 + B*C5;
            float Z = R*C6 + G*C7 + B*C8;
1757

1758 1759 1760
            float FX = X > 0.008856f ? std::pow(X, _1_3) : (7.787f * X + _a);
            float FY = Y > 0.008856f ? std::pow(Y, _1_3) : (7.787f * Y + _a);
            float FZ = Z > 0.008856f ? std::pow(Z, _1_3) : (7.787f * Z + _a);
1761

A
Andrey Kamaev 已提交
1762
            float L = Y > 0.008856f ? (116.f * FY - 16.f) : (903.3f * Y);
1763 1764
            float a = 500.f * (FX - FY);
            float b = 200.f * (FY - FZ);
1765

1766 1767 1768
            dst[i] = L;
            dst[i + 1] = a;
            dst[i + 2] = b;
1769 1770
        }
    }
A
Andrey Kamaev 已提交
1771

1772 1773 1774 1775
    int srccn;
    float coeffs[9];
    bool srgb;
};
1776

1777 1778 1779
struct Lab2RGB_f
{
    typedef float channel_type;
1780

1781
    Lab2RGB_f( int _dstcn, int blueIdx, const float* _coeffs,
1782
              const float* _whitept, bool _srgb )
1783
    : dstcn(_dstcn), srgb(_srgb)
1784 1785
    {
        initLabTabs();
1786

1787 1788 1789 1790
        if(!_coeffs)
            _coeffs = XYZ2sRGB_D65;
        if(!_whitept)
            _whitept = D65;
1791

1792 1793 1794 1795 1796 1797 1798
        for( int i = 0; i < 3; i++ )
        {
            coeffs[i+(blueIdx^2)*3] = _coeffs[i]*_whitept[i];
            coeffs[i+3] = _coeffs[i+3]*_whitept[i];
            coeffs[i+blueIdx*3] = _coeffs[i+6]*_whitept[i];
        }
    }
1799

1800 1801 1802 1803 1804 1805
    void operator()(const float* src, float* dst, int n) const
    {
        int i, dcn = dstcn;
        const float* gammaTab = srgb ? sRGBInvGammaTab : 0;
        float gscale = GammaTabScale;
        float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
1806 1807
        C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
        C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
1808 1809
        float alpha = ColorChannel<float>::max();
        n *= 3;
1810

1811 1812 1813
        static const float lThresh = 0.008856f * 903.3f;
        static const float fThresh = 7.787f * 0.008856f + 16.0f / 116.0f;
        for (i = 0; i < n; i += 3, dst += dcn)
1814
        {
1815 1816 1817
            float li = src[i];
            float ai = src[i + 1];
            float bi = src[i + 2];
1818

1819 1820
            float y, fy;
            if (li <= lThresh)
1821
            {
1822 1823
                y = li / 903.3f;
                fy = 7.787f * y + 16.0f / 116.0f;
1824
            }
1825 1826 1827 1828 1829
            else
            {
                fy = (li + 16.0f) / 116.0f;
                y = fy * fy * fy;
            }
1830

1831
            float fxz[] = { ai / 500.0f + fy, fy - bi / 200.0f };
1832

1833 1834 1835 1836 1837
            for (int j = 0; j < 2; j++)
                if (fxz[j] <= fThresh)
                    fxz[j] = (fxz[j] - 16.0f / 116.0f) / 7.787f;
                else
                    fxz[j] = fxz[j] * fxz[j] * fxz[j];
1838 1839


1840
            float x = fxz[0], z = fxz[1];
1841 1842 1843 1844 1845 1846
            float ro = C0 * x + C1 * y + C2 * z;
            float go = C3 * x + C4 * y + C5 * z;
            float bo = C6 * x + C7 * y + C8 * z;
            ro = clip(ro);
            go = clip(go);
            bo = clip(bo);
1847

1848 1849 1850 1851 1852 1853
            if (gammaTab)
            {
                ro = splineInterpolate(ro * gscale, gammaTab, GAMMA_TAB_SIZE);
                go = splineInterpolate(go * gscale, gammaTab, GAMMA_TAB_SIZE);
                bo = splineInterpolate(bo * gscale, gammaTab, GAMMA_TAB_SIZE);
            }
1854

1855
            dst[0] = ro, dst[1] = go, dst[2] = bo;
1856 1857
            if( dcn == 4 )
                dst[3] = alpha;
1858 1859
        }
    }
1860

1861 1862 1863 1864
    int dstcn;
    float coeffs[9];
    bool srgb;
};
1865

1866
#undef clip
A
Andrey Kamaev 已提交
1867

1868
struct Lab2RGB_b
1869
{
1870
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
1871

1872 1873 1874
    Lab2RGB_b( int _dstcn, int blueIdx, const float* _coeffs,
               const float* _whitept, bool _srgb )
    : dstcn(_dstcn), cvt(3, blueIdx, _coeffs, _whitept, _srgb ) {}
A
Andrey Kamaev 已提交
1875

1876
    void operator()(const uchar* src, uchar* dst, int n) const
1877
    {
1878 1879 1880
        int i, j, dcn = dstcn;
        uchar alpha = ColorChannel<uchar>::max();
        float buf[3*BLOCK_SIZE];
A
Andrey Kamaev 已提交
1881

1882
        for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 )
1883
        {
1884
            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Kamaev 已提交
1885

1886
            for( j = 0; j < dn*3; j += 3 )
1887
            {
1888 1889 1890
                buf[j] = src[j]*(100.f/255.f);
                buf[j+1] = (float)(src[j+1] - 128);
                buf[j+2] = (float)(src[j+2] - 128);
1891
            }
1892
            cvt(buf, buf, dn);
A
Andrey Kamaev 已提交
1893

1894
            for( j = 0; j < dn*3; j += 3, dst += dcn )
1895
            {
1896 1897 1898 1899 1900
                dst[0] = saturate_cast<uchar>(buf[j]*255.f);
                dst[1] = saturate_cast<uchar>(buf[j+1]*255.f);
                dst[2] = saturate_cast<uchar>(buf[j+2]*255.f);
                if( dcn == 4 )
                    dst[3] = alpha;
1901 1902 1903
            }
        }
    }
A
Andrey Kamaev 已提交
1904

1905 1906 1907
    int dstcn;
    Lab2RGB_f cvt;
};
A
Andrey Kamaev 已提交
1908 1909


1910
///////////////////////////////////// RGB <-> L*u*v* /////////////////////////////////////
1911

1912
struct RGB2Luv_f
1913
{
1914
    typedef float channel_type;
A
Andrey Kamaev 已提交
1915

1916 1917 1918
    RGB2Luv_f( int _srccn, int blueIdx, const float* _coeffs,
               const float* whitept, bool _srgb )
    : srccn(_srccn), srgb(_srgb)
1919
    {
A
Andrey Kamaev 已提交
1920
        volatile int i;
1921
        initLabTabs();
A
Andrey Kamaev 已提交
1922

1923 1924
        if(!_coeffs) _coeffs = sRGB2XYZ_D65;
        if(!whitept) whitept = D65;
A
Andrey Kamaev 已提交
1925

1926
        for( i = 0; i < 3; i++ )
1927
        {
1928
            coeffs[i*3] = _coeffs[i*3];
1929
            coeffs[i*3+1] = _coeffs[i*3+1];
1930 1931 1932
            coeffs[i*3+2] = _coeffs[i*3+2];
            if( blueIdx == 0 )
                std::swap(coeffs[i*3], coeffs[i*3+2]);
1933 1934
            CV_Assert( coeffs[i*3] >= 0 && coeffs[i*3+1] >= 0 && coeffs[i*3+2] >= 0 &&
                      coeffs[i*3] + coeffs[i*3+1] + coeffs[i*3+2] < 1.5f );
1935
        }
A
Andrey Kamaev 已提交
1936

1937 1938 1939
        float d = 1.f/(whitept[0] + whitept[1]*15 + whitept[2]*3);
        un = 4*whitept[0]*d;
        vn = 9*whitept[1]*d;
A
Andrey Kamaev 已提交
1940

1941
        CV_Assert(whitept[1] == 1.f);
1942
    }
A
Andrey Kamaev 已提交
1943

1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
    void operator()(const float* src, float* dst, int n) const
    {
        int i, scn = srccn;
        float gscale = GammaTabScale;
        const float* gammaTab = srgb ? sRGBGammaTab : 0;
        float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
              C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
              C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
        float _un = 13*un, _vn = 13*vn;
        n *= 3;
A
Andrey Kamaev 已提交
1954

1955 1956 1957 1958 1959 1960 1961 1962 1963
        for( i = 0; i < n; i += 3, src += scn )
        {
            float R = src[0], G = src[1], B = src[2];
            if( gammaTab )
            {
                R = splineInterpolate(R*gscale, gammaTab, GAMMA_TAB_SIZE);
                G = splineInterpolate(G*gscale, gammaTab, GAMMA_TAB_SIZE);
                B = splineInterpolate(B*gscale, gammaTab, GAMMA_TAB_SIZE);
            }
A
Andrey Kamaev 已提交
1964

1965 1966 1967
            float X = R*C0 + G*C1 + B*C2;
            float Y = R*C3 + G*C4 + B*C5;
            float Z = R*C6 + G*C7 + B*C8;
A
Andrey Kamaev 已提交
1968

1969 1970
            float L = splineInterpolate(Y*LabCbrtTabScale, LabCbrtTab, LAB_CBRT_TAB_SIZE);
            L = 116.f*L - 16.f;
A
Andrey Kamaev 已提交
1971 1972

            float d = (4*13) / std::max(X + 15 * Y + 3 * Z, FLT_EPSILON);
1973
            float u = L*(X*d - _un);
1974
            float v = L*((9*0.25f)*Y*d - _vn);
A
Andrey Kamaev 已提交
1975

1976 1977 1978
            dst[i] = L; dst[i+1] = u; dst[i+2] = v;
        }
    }
A
Andrey Kamaev 已提交
1979

1980 1981 1982 1983
    int srccn;
    float coeffs[9], un, vn;
    bool srgb;
};
1984

A
Andrey Kamaev 已提交
1985

1986
struct Luv2RGB_f
1987
{
1988
    typedef float channel_type;
A
Andrey Kamaev 已提交
1989

1990 1991 1992
    Luv2RGB_f( int _dstcn, int blueIdx, const float* _coeffs,
              const float* whitept, bool _srgb )
    : dstcn(_dstcn), srgb(_srgb)
1993
    {
1994
        initLabTabs();
A
Andrey Kamaev 已提交
1995

1996 1997
        if(!_coeffs) _coeffs = XYZ2sRGB_D65;
        if(!whitept) whitept = D65;
A
Andrey Kamaev 已提交
1998

1999
        for( int i = 0; i < 3; i++ )
2000
        {
2001 2002 2003 2004
            coeffs[i+(blueIdx^2)*3] = _coeffs[i];
            coeffs[i+3] = _coeffs[i+3];
            coeffs[i+blueIdx*3] = _coeffs[i+6];
        }
A
Andrey Kamaev 已提交
2005

2006 2007 2008
        float d = 1.f/(whitept[0] + whitept[1]*15 + whitept[2]*3);
        un = 4*whitept[0]*d;
        vn = 9*whitept[1]*d;
A
Andrey Kamaev 已提交
2009

2010 2011
        CV_Assert(whitept[1] == 1.f);
    }
A
Andrey Kamaev 已提交
2012

2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
    void operator()(const float* src, float* dst, int n) const
    {
        int i, dcn = dstcn;
        const float* gammaTab = srgb ? sRGBInvGammaTab : 0;
        float gscale = GammaTabScale;
        float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2],
              C3 = coeffs[3], C4 = coeffs[4], C5 = coeffs[5],
              C6 = coeffs[6], C7 = coeffs[7], C8 = coeffs[8];
        float alpha = ColorChannel<float>::max();
        float _un = un, _vn = vn;
        n *= 3;
A
Andrey Kamaev 已提交
2024

2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
        for( i = 0; i < n; i += 3, dst += dcn )
        {
            float L = src[i], u = src[i+1], v = src[i+2], d, X, Y, Z;
            Y = (L + 16.f) * (1.f/116.f);
            Y = Y*Y*Y;
            d = (1.f/13.f)/L;
            u = u*d + _un;
            v = v*d + _vn;
            float iv = 1.f/v;
            X = 2.25f * u * Y * iv ;
A
Andrey Kamaev 已提交
2035 2036
            Z = (12 - 3 * u - 20 * v) * Y * 0.25f * iv;

2037 2038 2039
            float R = X*C0 + Y*C1 + Z*C2;
            float G = X*C3 + Y*C4 + Z*C5;
            float B = X*C6 + Y*C7 + Z*C8;
A
Andrey Kamaev 已提交
2040

2041
            if( gammaTab )
2042
            {
2043 2044 2045
                R = splineInterpolate(R*gscale, gammaTab, GAMMA_TAB_SIZE);
                G = splineInterpolate(G*gscale, gammaTab, GAMMA_TAB_SIZE);
                B = splineInterpolate(B*gscale, gammaTab, GAMMA_TAB_SIZE);
2046
            }
A
Andrey Kamaev 已提交
2047

2048 2049 2050
            dst[0] = R; dst[1] = G; dst[2] = B;
            if( dcn == 4 )
                dst[3] = alpha;
2051 2052
        }
    }
A
Andrey Kamaev 已提交
2053

2054 2055 2056 2057
    int dstcn;
    float coeffs[9], un, vn;
    bool srgb;
};
2058

A
Andrey Kamaev 已提交
2059

2060
struct RGB2Luv_b
2061
{
2062
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
2063

2064 2065 2066
    RGB2Luv_b( int _srccn, int blueIdx, const float* _coeffs,
               const float* _whitept, bool _srgb )
    : srccn(_srccn), cvt(3, blueIdx, _coeffs, _whitept, _srgb) {}
A
Andrey Kamaev 已提交
2067

2068
    void operator()(const uchar* src, uchar* dst, int n) const
2069
    {
2070 2071
        int i, j, scn = srccn;
        float buf[3*BLOCK_SIZE];
A
Andrey Kamaev 已提交
2072

2073
        for( i = 0; i < n; i += BLOCK_SIZE, dst += BLOCK_SIZE*3 )
2074
        {
2075
            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Kamaev 已提交
2076

2077
            for( j = 0; j < dn*3; j += 3, src += scn )
2078
            {
2079 2080 2081
                buf[j] = src[0]*(1.f/255.f);
                buf[j+1] = (float)(src[1]*(1.f/255.f));
                buf[j+2] = (float)(src[2]*(1.f/255.f));
2082
            }
2083
            cvt(buf, buf, dn);
A
Andrey Kamaev 已提交
2084

2085
            for( j = 0; j < dn*3; j += 3 )
2086
            {
2087 2088 2089
                dst[j] = saturate_cast<uchar>(buf[j]*2.55f);
                dst[j+1] = saturate_cast<uchar>(buf[j+1]*0.72033898305084743f + 96.525423728813564f);
                dst[j+2] = saturate_cast<uchar>(buf[j+2]*0.99609375f + 139.453125f);
2090 2091 2092
            }
        }
    }
A
Andrey Kamaev 已提交
2093

2094 2095 2096
    int srccn;
    RGB2Luv_f cvt;
};
A
Andrey Kamaev 已提交
2097

2098

2099
struct Luv2RGB_b
2100
{
2101
    typedef uchar channel_type;
A
Andrey Kamaev 已提交
2102

2103 2104 2105
    Luv2RGB_b( int _dstcn, int blueIdx, const float* _coeffs,
               const float* _whitept, bool _srgb )
    : dstcn(_dstcn), cvt(3, blueIdx, _coeffs, _whitept, _srgb ) {}
A
Andrey Kamaev 已提交
2106

2107 2108 2109 2110 2111
    void operator()(const uchar* src, uchar* dst, int n) const
    {
        int i, j, dcn = dstcn;
        uchar alpha = ColorChannel<uchar>::max();
        float buf[3*BLOCK_SIZE];
A
Andrey Kamaev 已提交
2112

2113 2114 2115
        for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 )
        {
            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Kamaev 已提交
2116

2117 2118 2119 2120 2121 2122 2123
            for( j = 0; j < dn*3; j += 3 )
            {
                buf[j] = src[j]*(100.f/255.f);
                buf[j+1] = (float)(src[j+1]*1.388235294117647f - 134.f);
                buf[j+2] = (float)(src[j+2]*1.003921568627451f - 140.f);
            }
            cvt(buf, buf, dn);
A
Andrey Kamaev 已提交
2124

2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
            for( j = 0; j < dn*3; j += 3, dst += dcn )
            {
                dst[0] = saturate_cast<uchar>(buf[j]*255.f);
                dst[1] = saturate_cast<uchar>(buf[j+1]*255.f);
                dst[2] = saturate_cast<uchar>(buf[j+2]*255.f);
                if( dcn == 4 )
                    dst[3] = alpha;
            }
        }
    }
A
Andrey Kamaev 已提交
2135

2136 2137 2138
    int dstcn;
    Luv2RGB_f cvt;
};
2139

A
Andrey Kamaev 已提交
2140

2141
///////////////////////////////////// YUV420 -> RGB /////////////////////////////////////
2142

2143 2144 2145 2146 2147 2148 2149
const int ITUR_BT_601_CY = 1220542;
const int ITUR_BT_601_CUB = 2116026;
const int ITUR_BT_601_CUG = -409993;
const int ITUR_BT_601_CVG = -852492;
const int ITUR_BT_601_CVR = 1673527;
const int ITUR_BT_601_SHIFT = 20;

2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
// Coefficients for RGB to YUV420p conversion
const int ITUR_BT_601_CRY =  269484;
const int ITUR_BT_601_CGY =  528482;
const int ITUR_BT_601_CBY =  102760;
const int ITUR_BT_601_CRU = -155188;
const int ITUR_BT_601_CGU = -305135;
const int ITUR_BT_601_CBU =  460324;
const int ITUR_BT_601_CGV = -385875;
const int ITUR_BT_601_CBV = -74448;

2160
template<int bIdx, int uIdx>
2161
struct YUV420sp2RGB888Invoker : ParallelLoopBody
2162
{
2163
    Mat* dst;
2164
    const uchar* my1, *muv;
2165
    int width, stride;
2166

2167
    YUV420sp2RGB888Invoker(Mat* _dst, int _stride, const uchar* _y1, const uchar* _uv)
2168
        : dst(_dst), my1(_y1), muv(_uv), width(_dst->cols), stride(_stride) {}
2169

2170
    void operator()(const Range& range) const
2171
    {
2172 2173
        int rangeBegin = range.start * 2;
        int rangeEnd = range.end * 2;
2174

2175
        //R = 1.164(Y - 16) + 1.596(V - 128)
2176 2177 2178 2179 2180 2181 2182 2183
        //G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
        //B = 1.164(Y - 16)                  + 2.018(U - 128)

        //R = (1220542(Y - 16) + 1673527(V - 128)                  + (1 << 19)) >> 20
        //G = (1220542(Y - 16) - 852492(V - 128) - 409993(U - 128) + (1 << 19)) >> 20
        //B = (1220542(Y - 16)                  + 2116026(U - 128) + (1 << 19)) >> 20

        const uchar* y1 = my1 + rangeBegin * stride, *uv = muv + rangeBegin * stride / 2;
2184

2185 2186 2187 2188 2189 2190
#ifdef HAVE_TEGRA_OPTIMIZATION
        if(tegra::cvtYUV4202RGB(bIdx, uIdx, 3, y1, uv, stride, dst->ptr<uchar>(rangeBegin), dst->step, rangeEnd - rangeBegin, dst->cols))
            return;
#endif

        for (int j = rangeBegin; j < rangeEnd; j += 2, y1 += stride * 2, uv += stride)
2191
        {
2192
            uchar* row1 = dst->ptr<uchar>(j);
2193 2194
            uchar* row2 = dst->ptr<uchar>(j + 1);
            const uchar* y2 = y1 + stride;
2195

2196
            for (int i = 0; i < width; i += 2, row1 += 6, row2 += 6)
2197
            {
2198 2199 2200
                int u = int(uv[i + 0 + uIdx]) - 128;
                int v = int(uv[i + 1 - uIdx]) - 128;

2201 2202 2203
                int ruv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVR * v;
                int guv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVG * v + ITUR_BT_601_CUG * u;
                int buv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CUB * u;
2204

2205 2206 2207 2208
                int y00 = std::max(0, int(y1[i]) - 16) * ITUR_BT_601_CY;
                row1[2-bIdx] = saturate_cast<uchar>((y00 + ruv) >> ITUR_BT_601_SHIFT);
                row1[1]      = saturate_cast<uchar>((y00 + guv) >> ITUR_BT_601_SHIFT);
                row1[bIdx]   = saturate_cast<uchar>((y00 + buv) >> ITUR_BT_601_SHIFT);
2209

2210 2211 2212 2213
                int y01 = std::max(0, int(y1[i + 1]) - 16) * ITUR_BT_601_CY;
                row1[5-bIdx] = saturate_cast<uchar>((y01 + ruv) >> ITUR_BT_601_SHIFT);
                row1[4]      = saturate_cast<uchar>((y01 + guv) >> ITUR_BT_601_SHIFT);
                row1[3+bIdx] = saturate_cast<uchar>((y01 + buv) >> ITUR_BT_601_SHIFT);
2214

2215 2216 2217 2218
                int y10 = std::max(0, int(y2[i]) - 16) * ITUR_BT_601_CY;
                row2[2-bIdx] = saturate_cast<uchar>((y10 + ruv) >> ITUR_BT_601_SHIFT);
                row2[1]      = saturate_cast<uchar>((y10 + guv) >> ITUR_BT_601_SHIFT);
                row2[bIdx]   = saturate_cast<uchar>((y10 + buv) >> ITUR_BT_601_SHIFT);
2219

2220 2221 2222 2223
                int y11 = std::max(0, int(y2[i + 1]) - 16) * ITUR_BT_601_CY;
                row2[5-bIdx] = saturate_cast<uchar>((y11 + ruv) >> ITUR_BT_601_SHIFT);
                row2[4]      = saturate_cast<uchar>((y11 + guv) >> ITUR_BT_601_SHIFT);
                row2[3+bIdx] = saturate_cast<uchar>((y11 + buv) >> ITUR_BT_601_SHIFT);
2224 2225 2226 2227 2228
            }
        }
    }
};

2229
template<int bIdx, int uIdx>
2230
struct YUV420sp2RGBA8888Invoker : ParallelLoopBody
2231
{
2232
    Mat* dst;
2233
    const uchar* my1, *muv;
2234
    int width, stride;
2235

2236
    YUV420sp2RGBA8888Invoker(Mat* _dst, int _stride, const uchar* _y1, const uchar* _uv)
2237
        : dst(_dst), my1(_y1), muv(_uv), width(_dst->cols), stride(_stride) {}
2238

2239
    void operator()(const Range& range) const
2240
    {
2241 2242
        int rangeBegin = range.start * 2;
        int rangeEnd = range.end * 2;
2243

2244
        //R = 1.164(Y - 16) + 1.596(V - 128)
2245 2246 2247 2248 2249 2250
        //G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
        //B = 1.164(Y - 16)                  + 2.018(U - 128)

        //R = (1220542(Y - 16) + 1673527(V - 128)                  + (1 << 19)) >> 20
        //G = (1220542(Y - 16) - 852492(V - 128) - 409993(U - 128) + (1 << 19)) >> 20
        //B = (1220542(Y - 16)                  + 2116026(U - 128) + (1 << 19)) >> 20
2251

2252 2253 2254 2255 2256 2257 2258 2259
        const uchar* y1 = my1 + rangeBegin * stride, *uv = muv + rangeBegin * stride / 2;

#ifdef HAVE_TEGRA_OPTIMIZATION
        if(tegra::cvtYUV4202RGB(bIdx, uIdx, 4, y1, uv, stride, dst->ptr<uchar>(rangeBegin), dst->step, rangeEnd - rangeBegin, dst->cols))
            return;
#endif

        for (int j = rangeBegin; j < rangeEnd; j += 2, y1 += stride * 2, uv += stride)
2260
        {
2261
            uchar* row1 = dst->ptr<uchar>(j);
2262 2263
            uchar* row2 = dst->ptr<uchar>(j + 1);
            const uchar* y2 = y1 + stride;
2264

2265
            for (int i = 0; i < width; i += 2, row1 += 8, row2 += 8)
2266
            {
2267 2268 2269
                int u = int(uv[i + 0 + uIdx]) - 128;
                int v = int(uv[i + 1 - uIdx]) - 128;

2270 2271 2272
                int ruv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVR * v;
                int guv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVG * v + ITUR_BT_601_CUG * u;
                int buv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CUB * u;
2273

2274 2275 2276 2277
                int y00 = std::max(0, int(y1[i]) - 16) * ITUR_BT_601_CY;
                row1[2-bIdx] = saturate_cast<uchar>((y00 + ruv) >> ITUR_BT_601_SHIFT);
                row1[1]      = saturate_cast<uchar>((y00 + guv) >> ITUR_BT_601_SHIFT);
                row1[bIdx]   = saturate_cast<uchar>((y00 + buv) >> ITUR_BT_601_SHIFT);
2278 2279
                row1[3]      = uchar(0xff);

2280 2281 2282 2283
                int y01 = std::max(0, int(y1[i + 1]) - 16) * ITUR_BT_601_CY;
                row1[6-bIdx] = saturate_cast<uchar>((y01 + ruv) >> ITUR_BT_601_SHIFT);
                row1[5]      = saturate_cast<uchar>((y01 + guv) >> ITUR_BT_601_SHIFT);
                row1[4+bIdx] = saturate_cast<uchar>((y01 + buv) >> ITUR_BT_601_SHIFT);
2284 2285
                row1[7]      = uchar(0xff);

2286 2287 2288 2289
                int y10 = std::max(0, int(y2[i]) - 16) * ITUR_BT_601_CY;
                row2[2-bIdx] = saturate_cast<uchar>((y10 + ruv) >> ITUR_BT_601_SHIFT);
                row2[1]      = saturate_cast<uchar>((y10 + guv) >> ITUR_BT_601_SHIFT);
                row2[bIdx]   = saturate_cast<uchar>((y10 + buv) >> ITUR_BT_601_SHIFT);
2290 2291
                row2[3]      = uchar(0xff);

2292 2293 2294 2295
                int y11 = std::max(0, int(y2[i + 1]) - 16) * ITUR_BT_601_CY;
                row2[6-bIdx] = saturate_cast<uchar>((y11 + ruv) >> ITUR_BT_601_SHIFT);
                row2[5]      = saturate_cast<uchar>((y11 + guv) >> ITUR_BT_601_SHIFT);
                row2[4+bIdx] = saturate_cast<uchar>((y11 + buv) >> ITUR_BT_601_SHIFT);
2296
                row2[7]      = uchar(0xff);
2297 2298 2299 2300 2301
            }
        }
    }
};

2302
template<int bIdx>
2303
struct YUV420p2RGB888Invoker : ParallelLoopBody
2304 2305 2306 2307
{
    Mat* dst;
    const uchar* my1, *mu, *mv;
    int width, stride;
2308
    int ustepIdx, vstepIdx;
2309

2310 2311
    YUV420p2RGB888Invoker(Mat* _dst, int _stride, const uchar* _y1, const uchar* _u, const uchar* _v, int _ustepIdx, int _vstepIdx)
        : dst(_dst), my1(_y1), mu(_u), mv(_v), width(_dst->cols), stride(_stride), ustepIdx(_ustepIdx), vstepIdx(_vstepIdx) {}
2312

2313
    void operator()(const Range& range) const
2314
    {
2315 2316
        const int rangeBegin = range.start * 2;
        const int rangeEnd = range.end * 2;
A
Andrey Kamaev 已提交
2317

A
Alexander Smorkalov 已提交
2318
        int uvsteps[2] = {width/2, stride - width/2};
2319
        int usIdx = ustepIdx, vsIdx = vstepIdx;
2320 2321

        const uchar* y1 = my1 + rangeBegin * stride;
2322 2323
        const uchar* u1 = mu + (range.start / 2) * stride;
        const uchar* v1 = mv + (range.start / 2) * stride;
A
Andrey Kamaev 已提交
2324

2325
        if(range.start % 2 == 1)
2326 2327 2328 2329
        {
            u1 += uvsteps[(usIdx++) & 1];
            v1 += uvsteps[(vsIdx++) & 1];
        }
2330

2331
        for (int j = rangeBegin; j < rangeEnd; j += 2, y1 += stride * 2, u1 += uvsteps[(usIdx++) & 1], v1 += uvsteps[(vsIdx++) & 1])
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
        {
            uchar* row1 = dst->ptr<uchar>(j);
            uchar* row2 = dst->ptr<uchar>(j + 1);
            const uchar* y2 = y1 + stride;

            for (int i = 0; i < width / 2; i += 1, row1 += 6, row2 += 6)
            {
                int u = int(u1[i]) - 128;
                int v = int(v1[i]) - 128;

2342 2343 2344
                int ruv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVR * v;
                int guv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVG * v + ITUR_BT_601_CUG * u;
                int buv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CUB * u;
2345

2346 2347 2348 2349
                int y00 = std::max(0, int(y1[2 * i]) - 16) * ITUR_BT_601_CY;
                row1[2-bIdx] = saturate_cast<uchar>((y00 + ruv) >> ITUR_BT_601_SHIFT);
                row1[1]      = saturate_cast<uchar>((y00 + guv) >> ITUR_BT_601_SHIFT);
                row1[bIdx]   = saturate_cast<uchar>((y00 + buv) >> ITUR_BT_601_SHIFT);
2350

2351 2352 2353 2354
                int y01 = std::max(0, int(y1[2 * i + 1]) - 16) * ITUR_BT_601_CY;
                row1[5-bIdx] = saturate_cast<uchar>((y01 + ruv) >> ITUR_BT_601_SHIFT);
                row1[4]      = saturate_cast<uchar>((y01 + guv) >> ITUR_BT_601_SHIFT);
                row1[3+bIdx] = saturate_cast<uchar>((y01 + buv) >> ITUR_BT_601_SHIFT);
2355

2356 2357 2358 2359
                int y10 = std::max(0, int(y2[2 * i]) - 16) * ITUR_BT_601_CY;
                row2[2-bIdx] = saturate_cast<uchar>((y10 + ruv) >> ITUR_BT_601_SHIFT);
                row2[1]      = saturate_cast<uchar>((y10 + guv) >> ITUR_BT_601_SHIFT);
                row2[bIdx]   = saturate_cast<uchar>((y10 + buv) >> ITUR_BT_601_SHIFT);
2360

2361 2362 2363 2364
                int y11 = std::max(0, int(y2[2 * i + 1]) - 16) * ITUR_BT_601_CY;
                row2[5-bIdx] = saturate_cast<uchar>((y11 + ruv) >> ITUR_BT_601_SHIFT);
                row2[4]      = saturate_cast<uchar>((y11 + guv) >> ITUR_BT_601_SHIFT);
                row2[3+bIdx] = saturate_cast<uchar>((y11 + buv) >> ITUR_BT_601_SHIFT);
2365 2366 2367 2368 2369 2370
            }
        }
    }
};

template<int bIdx>
2371
struct YUV420p2RGBA8888Invoker : ParallelLoopBody
2372 2373 2374 2375
{
    Mat* dst;
    const uchar* my1, *mu, *mv;
    int width, stride;
2376
    int ustepIdx, vstepIdx;
2377

2378 2379
    YUV420p2RGBA8888Invoker(Mat* _dst, int _stride, const uchar* _y1, const uchar* _u, const uchar* _v, int _ustepIdx, int _vstepIdx)
        : dst(_dst), my1(_y1), mu(_u), mv(_v), width(_dst->cols), stride(_stride), ustepIdx(_ustepIdx), vstepIdx(_vstepIdx) {}
2380

2381
    void operator()(const Range& range) const
2382
    {
2383 2384
        int rangeBegin = range.start * 2;
        int rangeEnd = range.end * 2;
2385

A
Alexander Smorkalov 已提交
2386
        int uvsteps[2] = {width/2, stride - width/2};
2387
        int usIdx = ustepIdx, vsIdx = vstepIdx;
2388 2389

        const uchar* y1 = my1 + rangeBegin * stride;
2390 2391
        const uchar* u1 = mu + (range.start / 2) * stride;
        const uchar* v1 = mv + (range.start / 2) * stride;
A
Andrey Kamaev 已提交
2392

2393
        if(range.start % 2 == 1)
2394 2395 2396 2397
        {
            u1 += uvsteps[(usIdx++) & 1];
            v1 += uvsteps[(vsIdx++) & 1];
        }
2398

2399
        for (int j = rangeBegin; j < rangeEnd; j += 2, y1 += stride * 2, u1 += uvsteps[(usIdx++) & 1], v1 += uvsteps[(vsIdx++) & 1])
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
        {
            uchar* row1 = dst->ptr<uchar>(j);
            uchar* row2 = dst->ptr<uchar>(j + 1);
            const uchar* y2 = y1 + stride;

            for (int i = 0; i < width / 2; i += 1, row1 += 8, row2 += 8)
            {
                int u = int(u1[i]) - 128;
                int v = int(v1[i]) - 128;

2410 2411 2412
                int ruv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVR * v;
                int guv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVG * v + ITUR_BT_601_CUG * u;
                int buv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CUB * u;
2413

2414 2415 2416 2417
                int y00 = std::max(0, int(y1[2 * i]) - 16) * ITUR_BT_601_CY;
                row1[2-bIdx] = saturate_cast<uchar>((y00 + ruv) >> ITUR_BT_601_SHIFT);
                row1[1]      = saturate_cast<uchar>((y00 + guv) >> ITUR_BT_601_SHIFT);
                row1[bIdx]   = saturate_cast<uchar>((y00 + buv) >> ITUR_BT_601_SHIFT);
2418 2419
                row1[3]      = uchar(0xff);

2420 2421 2422 2423
                int y01 = std::max(0, int(y1[2 * i + 1]) - 16) * ITUR_BT_601_CY;
                row1[6-bIdx] = saturate_cast<uchar>((y01 + ruv) >> ITUR_BT_601_SHIFT);
                row1[5]      = saturate_cast<uchar>((y01 + guv) >> ITUR_BT_601_SHIFT);
                row1[4+bIdx] = saturate_cast<uchar>((y01 + buv) >> ITUR_BT_601_SHIFT);
2424 2425
                row1[7]      = uchar(0xff);

2426 2427 2428 2429
                int y10 = std::max(0, int(y2[2 * i]) - 16) * ITUR_BT_601_CY;
                row2[2-bIdx] = saturate_cast<uchar>((y10 + ruv) >> ITUR_BT_601_SHIFT);
                row2[1]      = saturate_cast<uchar>((y10 + guv) >> ITUR_BT_601_SHIFT);
                row2[bIdx]   = saturate_cast<uchar>((y10 + buv) >> ITUR_BT_601_SHIFT);
2430 2431
                row2[3]      = uchar(0xff);

2432 2433 2434 2435
                int y11 = std::max(0, int(y2[2 * i + 1]) - 16) * ITUR_BT_601_CY;
                row2[6-bIdx] = saturate_cast<uchar>((y11 + ruv) >> ITUR_BT_601_SHIFT);
                row2[5]      = saturate_cast<uchar>((y11 + guv) >> ITUR_BT_601_SHIFT);
                row2[4+bIdx] = saturate_cast<uchar>((y11 + buv) >> ITUR_BT_601_SHIFT);
2436 2437 2438 2439 2440 2441
                row2[7]      = uchar(0xff);
            }
        }
    }
};

2442
#define MIN_SIZE_FOR_PARALLEL_YUV420_CONVERSION (320*240)
2443

2444
template<int bIdx, int uIdx>
2445
inline void cvtYUV420sp2RGB(Mat& _dst, int _stride, const uchar* _y1, const uchar* _uv)
2446
{
2447
    YUV420sp2RGB888Invoker<bIdx, uIdx> converter(&_dst, _stride, _y1,  _uv);
2448
    if (_dst.total() >= MIN_SIZE_FOR_PARALLEL_YUV420_CONVERSION)
2449
        parallel_for_(Range(0, _dst.rows/2), converter);
2450
    else
2451
        converter(Range(0, _dst.rows/2));
2452 2453 2454
}

template<int bIdx, int uIdx>
2455
inline void cvtYUV420sp2RGBA(Mat& _dst, int _stride, const uchar* _y1, const uchar* _uv)
2456
{
2457 2458
    YUV420sp2RGBA8888Invoker<bIdx, uIdx> converter(&_dst, _stride, _y1,  _uv);
    if (_dst.total() >= MIN_SIZE_FOR_PARALLEL_YUV420_CONVERSION)
2459
        parallel_for_(Range(0, _dst.rows/2), converter);
2460
    else
2461
        converter(Range(0, _dst.rows/2));
2462 2463 2464
}

template<int bIdx>
2465
inline void cvtYUV420p2RGB(Mat& _dst, int _stride, const uchar* _y1, const uchar* _u, const uchar* _v, int ustepIdx, int vstepIdx)
2466
{
2467
    YUV420p2RGB888Invoker<bIdx> converter(&_dst, _stride, _y1,  _u, _v, ustepIdx, vstepIdx);
2468
    if (_dst.total() >= MIN_SIZE_FOR_PARALLEL_YUV420_CONVERSION)
2469
        parallel_for_(Range(0, _dst.rows/2), converter);
2470
    else
2471
        converter(Range(0, _dst.rows/2));
2472 2473 2474
}

template<int bIdx>
2475
inline void cvtYUV420p2RGBA(Mat& _dst, int _stride, const uchar* _y1, const uchar* _u, const uchar* _v, int ustepIdx, int vstepIdx)
2476
{
2477
    YUV420p2RGBA8888Invoker<bIdx> converter(&_dst, _stride, _y1,  _u, _v, ustepIdx, vstepIdx);
2478
    if (_dst.total() >= MIN_SIZE_FOR_PARALLEL_YUV420_CONVERSION)
2479
        parallel_for_(Range(0, _dst.rows/2), converter);
2480
    else
2481
        converter(Range(0, _dst.rows/2));
2482 2483
}

2484 2485
///////////////////////////////////// RGB -> YUV420p /////////////////////////////////////

2486 2487
template<int bIdx>
struct RGB888toYUV420pInvoker: public ParallelLoopBody
2488
{
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
    RGB888toYUV420pInvoker( const Mat& src, Mat* dst, const int uIdx )
        : src_(src),
          dst_(dst),
          uIdx_(uIdx) { }

    void operator()(const Range& rowRange) const
    {
        const int w = src_.cols;
        const int h = src_.rows;

        const int cn = src_.channels();
        for( int i = rowRange.start; i < rowRange.end; i++ )
        {
            const uchar* row0 = src_.ptr<uchar>(2 * i);
            const uchar* row1 = src_.ptr<uchar>(2 * i + 1);

            uchar* y = dst_->ptr<uchar>(2*i);
            uchar* u = dst_->ptr<uchar>(h + i/2) + (i % 2) * (w/2);
            uchar* v = dst_->ptr<uchar>(h + (i + h/2)/2) + ((i + h/2) % 2) * (w/2);
            if( uIdx_ == 2 ) std::swap(u, v);

            for( int j = 0, k = 0; j < w * cn; j += 2 * cn, k++ )
            {
                int r00 = row0[2-bIdx + j];      int g00 = row0[1 + j];      int b00 = row0[bIdx + j];
                int r01 = row0[2-bIdx + cn + j]; int g01 = row0[1 + cn + j]; int b01 = row0[bIdx + cn + j];
                int r10 = row1[2-bIdx + j];      int g10 = row1[1 + j];      int b10 = row1[bIdx + j];
                int r11 = row1[2-bIdx + cn + j]; int g11 = row1[1 + cn + j]; int b11 = row1[bIdx + cn + j];

                const int shifted16 = (16 << ITUR_BT_601_SHIFT);
                const int halfShift = (1 << (ITUR_BT_601_SHIFT - 1));
                int y00 = ITUR_BT_601_CRY * r00 + ITUR_BT_601_CGY * g00 + ITUR_BT_601_CBY * b00 + halfShift + shifted16;
                int y01 = ITUR_BT_601_CRY * r01 + ITUR_BT_601_CGY * g01 + ITUR_BT_601_CBY * b01 + halfShift + shifted16;
                int y10 = ITUR_BT_601_CRY * r10 + ITUR_BT_601_CGY * g10 + ITUR_BT_601_CBY * b10 + halfShift + shifted16;
                int y11 = ITUR_BT_601_CRY * r11 + ITUR_BT_601_CGY * g11 + ITUR_BT_601_CBY * b11 + halfShift + shifted16;

                y[2*k + 0]            = saturate_cast<uchar>(y00 >> ITUR_BT_601_SHIFT);
                y[2*k + 1]            = saturate_cast<uchar>(y01 >> ITUR_BT_601_SHIFT);
                y[2*k + dst_->step + 0] = saturate_cast<uchar>(y10 >> ITUR_BT_601_SHIFT);
                y[2*k + dst_->step + 1] = saturate_cast<uchar>(y11 >> ITUR_BT_601_SHIFT);

                const int shifted128 = (128 << ITUR_BT_601_SHIFT);
                int u00 = ITUR_BT_601_CRU * r00 + ITUR_BT_601_CGU * g00 + ITUR_BT_601_CBU * b00 + halfShift + shifted128;
                int v00 = ITUR_BT_601_CBU * r00 + ITUR_BT_601_CGV * g00 + ITUR_BT_601_CBV * b00 + halfShift + shifted128;

                u[k] = saturate_cast<uchar>(u00 >> ITUR_BT_601_SHIFT);
                v[k] = saturate_cast<uchar>(v00 >> ITUR_BT_601_SHIFT);
            }
2536 2537
        }
    }
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559

    static bool isFit( const Mat& src )
    {
        return (src.total() >= 320*240);
    }

private:
    RGB888toYUV420pInvoker& operator=(const RGB888toYUV420pInvoker&);

    const Mat& src_;
    Mat* const dst_;
    const int uIdx_;
};

template<int bIdx, int uIdx>
static void cvtRGBtoYUV420p(const Mat& src, Mat& dst)
{
    RGB888toYUV420pInvoker<bIdx> colorConverter(src, &dst, uIdx);
    if( RGB888toYUV420pInvoker<bIdx>::isFit(src) )
        parallel_for_(Range(0, src.rows/2), colorConverter);
    else
        colorConverter(Range(0, src.rows/2));
2560 2561
}

2562 2563 2564
///////////////////////////////////// YUV422 -> RGB /////////////////////////////////////

template<int bIdx, int uIdx, int yIdx>
2565
struct YUV422toRGB888Invoker : ParallelLoopBody
2566 2567 2568 2569 2570 2571 2572 2573
{
    Mat* dst;
    const uchar* src;
    int width, stride;

    YUV422toRGB888Invoker(Mat* _dst, int _stride, const uchar* _yuv)
        : dst(_dst), src(_yuv), width(_dst->cols), stride(_stride) {}

2574
    void operator()(const Range& range) const
2575
    {
2576 2577
        int rangeBegin = range.start;
        int rangeEnd = range.end;
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610

        const int uidx = 1 - yIdx + uIdx * 2;
        const int vidx = (2 + uidx) % 4;
        const uchar* yuv_src = src + rangeBegin * stride;

        for (int j = rangeBegin; j < rangeEnd; j++, yuv_src += stride)
        {
            uchar* row = dst->ptr<uchar>(j);

            for (int i = 0; i < 2 * width; i += 4, row += 6)
            {
                int u = int(yuv_src[i + uidx]) - 128;
                int v = int(yuv_src[i + vidx]) - 128;

                int ruv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVR * v;
                int guv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVG * v + ITUR_BT_601_CUG * u;
                int buv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CUB * u;

                int y00 = std::max(0, int(yuv_src[i + yIdx]) - 16) * ITUR_BT_601_CY;
                row[2-bIdx] = saturate_cast<uchar>((y00 + ruv) >> ITUR_BT_601_SHIFT);
                row[1]      = saturate_cast<uchar>((y00 + guv) >> ITUR_BT_601_SHIFT);
                row[bIdx]   = saturate_cast<uchar>((y00 + buv) >> ITUR_BT_601_SHIFT);

                int y01 = std::max(0, int(yuv_src[i + yIdx + 2]) - 16) * ITUR_BT_601_CY;
                row[5-bIdx] = saturate_cast<uchar>((y01 + ruv) >> ITUR_BT_601_SHIFT);
                row[4]      = saturate_cast<uchar>((y01 + guv) >> ITUR_BT_601_SHIFT);
                row[3+bIdx] = saturate_cast<uchar>((y01 + buv) >> ITUR_BT_601_SHIFT);
            }
        }
    }
};

template<int bIdx, int uIdx, int yIdx>
2611
struct YUV422toRGBA8888Invoker : ParallelLoopBody
2612 2613 2614 2615 2616 2617 2618 2619
{
    Mat* dst;
    const uchar* src;
    int width, stride;

    YUV422toRGBA8888Invoker(Mat* _dst, int _stride, const uchar* _yuv)
        : dst(_dst), src(_yuv), width(_dst->cols), stride(_stride) {}

2620
    void operator()(const Range& range) const
2621
    {
2622 2623
        int rangeBegin = range.start;
        int rangeEnd = range.end;
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664

        const int uidx = 1 - yIdx + uIdx * 2;
        const int vidx = (2 + uidx) % 4;
        const uchar* yuv_src = src + rangeBegin * stride;

        for (int j = rangeBegin; j < rangeEnd; j++, yuv_src += stride)
        {
            uchar* row = dst->ptr<uchar>(j);

            for (int i = 0; i < 2 * width; i += 4, row += 8)
            {
                int u = int(yuv_src[i + uidx]) - 128;
                int v = int(yuv_src[i + vidx]) - 128;

                int ruv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVR * v;
                int guv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVG * v + ITUR_BT_601_CUG * u;
                int buv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CUB * u;

                int y00 = std::max(0, int(yuv_src[i + yIdx]) - 16) * ITUR_BT_601_CY;
                row[2-bIdx] = saturate_cast<uchar>((y00 + ruv) >> ITUR_BT_601_SHIFT);
                row[1]      = saturate_cast<uchar>((y00 + guv) >> ITUR_BT_601_SHIFT);
                row[bIdx]   = saturate_cast<uchar>((y00 + buv) >> ITUR_BT_601_SHIFT);
                row[3]      = uchar(0xff);

                int y01 = std::max(0, int(yuv_src[i + yIdx + 2]) - 16) * ITUR_BT_601_CY;
                row[6-bIdx] = saturate_cast<uchar>((y01 + ruv) >> ITUR_BT_601_SHIFT);
                row[5]      = saturate_cast<uchar>((y01 + guv) >> ITUR_BT_601_SHIFT);
                row[4+bIdx] = saturate_cast<uchar>((y01 + buv) >> ITUR_BT_601_SHIFT);
                row[7]      = uchar(0xff);
            }
        }
    }
};

#define MIN_SIZE_FOR_PARALLEL_YUV422_CONVERSION (320*240)

template<int bIdx, int uIdx, int yIdx>
inline void cvtYUV422toRGB(Mat& _dst, int _stride, const uchar* _yuv)
{
    YUV422toRGB888Invoker<bIdx, uIdx, yIdx> converter(&_dst, _stride, _yuv);
    if (_dst.total() >= MIN_SIZE_FOR_PARALLEL_YUV422_CONVERSION)
2665
        parallel_for_(Range(0, _dst.rows), converter);
2666
    else
2667
        converter(Range(0, _dst.rows));
2668 2669 2670 2671 2672 2673 2674
}

template<int bIdx, int uIdx, int yIdx>
inline void cvtYUV422toRGBA(Mat& _dst, int _stride, const uchar* _yuv)
{
    YUV422toRGBA8888Invoker<bIdx, uIdx, yIdx> converter(&_dst, _stride, _yuv);
    if (_dst.total() >= MIN_SIZE_FOR_PARALLEL_YUV422_CONVERSION)
2675
        parallel_for_(Range(0, _dst.rows), converter);
2676
    else
2677
        converter(Range(0, _dst.rows));
2678 2679
}

2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
/////////////////////////// RGBA <-> mRGBA (alpha premultiplied) //////////////

template<typename _Tp>
struct RGBA2mRGBA
{
    typedef _Tp channel_type;

    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        _Tp max_val  = ColorChannel<_Tp>::max();
        _Tp half_val = ColorChannel<_Tp>::half();
        for( int i = 0; i < n; i++ )
        {
            _Tp v0 = *src++;
            _Tp v1 = *src++;
            _Tp v2 = *src++;
            _Tp v3 = *src++;
2697

2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
            *dst++ = (v0 * v3 + half_val) / max_val;
            *dst++ = (v1 * v3 + half_val) / max_val;
            *dst++ = (v2 * v3 + half_val) / max_val;
            *dst++ = v3;
        }
    }
};


template<typename _Tp>
struct mRGBA2RGBA
{
    typedef _Tp channel_type;

    void operator()(const _Tp* src, _Tp* dst, int n) const
    {
        _Tp max_val = ColorChannel<_Tp>::max();
        for( int i = 0; i < n; i++ )
        {
            _Tp v0 = *src++;
            _Tp v1 = *src++;
            _Tp v2 = *src++;
            _Tp v3 = *src++;
            _Tp v3_half = v3 / 2;
2722

2723 2724 2725 2726 2727 2728 2729 2730
            *dst++ = (v3==0)? 0 : (v0 * max_val + v3_half) / v3;
            *dst++ = (v3==0)? 0 : (v1 * max_val + v3_half) / v3;
            *dst++ = (v3==0)? 0 : (v2 * max_val + v3_half) / v3;
            *dst++ = v3;
        }
    }
};

I
Ilya Lavrenov 已提交
2731
#ifdef HAVE_OPENCL
2732

2733 2734
#define DIVUP(total, grain) (((total) + (grain) - 1) / (grain))

2735 2736
static bool ocl_cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
{
I
Ilya Lavrenov 已提交
2737
    bool ok = false;
2738 2739
    UMat src = _src.getUMat(), dst;
    Size sz = src.size(), dstSz = sz;
V
Vadim Pisarevsky 已提交
2740
    int scn = src.channels(), depth = src.depth(), bidx;
2741
    int dims = 2, stripeSize = 1;
2742 2743 2744
    size_t globalsize[] = { src.cols, src.rows };
    ocl::Kernel k;

2745
    if (depth != CV_8U && depth != CV_16U && depth != CV_32F)
2746 2747
        return false;

2748 2749 2750 2751 2752 2753 2754 2755
    cv::String opts = format("-D depth=%d -D scn=%d ", depth, scn);

    ocl::Device dev = ocl::Device::getDefault();
    int pxPerWIy = 1;
    if (dev.isIntel() && (dev.type() & ocl::Device::TYPE_GPU))
    {
        pxPerWIy = 4;
    }
2756
    globalsize[1] = DIVUP(globalsize[1], pxPerWIy);
2757 2758
    opts +=  format("-D PIX_PER_WI_Y=%d ", pxPerWIy);

2759 2760
    switch (code)
    {
I
Ilya Lavrenov 已提交
2761 2762 2763 2764 2765 2766 2767
    case COLOR_BGR2BGRA: case COLOR_RGB2BGRA: case COLOR_BGRA2BGR:
    case COLOR_RGBA2BGR: case COLOR_RGB2BGR: case COLOR_BGRA2RGBA:
    {
        CV_Assert(scn == 3 || scn == 4);
        dcn = code == COLOR_BGR2BGRA || code == COLOR_RGB2BGRA || code == COLOR_BGRA2RGBA ? 4 : 3;
        bool reverse = !(code == COLOR_BGR2BGRA || code == COLOR_BGRA2BGR);
        k.create("RGB", ocl::imgproc::cvtcolor_oclsrc,
2768
                 opts + format("-D dcn=%d -D bidx=0 -D %s", dcn,
I
Ilya Lavrenov 已提交
2769 2770 2771
                        reverse ? "REVERSE" : "ORDER"));
        break;
    }
I
Ilya Lavrenov 已提交
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
    case COLOR_BGR5652BGR: case COLOR_BGR5552BGR: case COLOR_BGR5652RGB: case COLOR_BGR5552RGB:
    case COLOR_BGR5652BGRA: case COLOR_BGR5552BGRA: case COLOR_BGR5652RGBA: case COLOR_BGR5552RGBA:
    {
        dcn = code == COLOR_BGR5652BGRA || code == COLOR_BGR5552BGRA || code == COLOR_BGR5652RGBA || code == COLOR_BGR5552RGBA ? 4 : 3;
        CV_Assert((dcn == 3 || dcn == 4) && scn == 2 && depth == CV_8U);
        bidx = code == COLOR_BGR5652BGR || code == COLOR_BGR5552BGR ||
            code == COLOR_BGR5652BGRA || code == COLOR_BGR5552BGRA ? 0 : 2;
        int greenbits = code == COLOR_BGR5652BGR || code == COLOR_BGR5652RGB ||
            code == COLOR_BGR5652BGRA || code == COLOR_BGR5652RGBA ? 6 : 5;
        k.create("RGB5x52RGB", ocl::imgproc::cvtcolor_oclsrc,
2782
                 opts + format("-D dcn=%d -D bidx=%d -D greenbits=%d", dcn, bidx, greenbits));
I
Ilya Lavrenov 已提交
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794
        break;
    }
    case COLOR_BGR2BGR565: case COLOR_BGR2BGR555: case COLOR_RGB2BGR565: case COLOR_RGB2BGR555:
    case COLOR_BGRA2BGR565: case COLOR_BGRA2BGR555: case COLOR_RGBA2BGR565: case COLOR_RGBA2BGR555:
    {
        CV_Assert((scn == 3 || scn == 4) && depth == CV_8U );
        bidx = code == COLOR_BGR2BGR565 || code == COLOR_BGR2BGR555 ||
            code == COLOR_BGRA2BGR565 || code == COLOR_BGRA2BGR555 ? 0 : 2;
        int greenbits = code == COLOR_BGR2BGR565 || code == COLOR_RGB2BGR565 ||
            code == COLOR_BGRA2BGR565 || code == COLOR_RGBA2BGR565 ? 6 : 5;
        dcn = 2;
        k.create("RGB2RGB5x5", ocl::imgproc::cvtcolor_oclsrc,
2795
                 opts + format("-D dcn=2 -D bidx=%d -D greenbits=%d", bidx, greenbits));
I
Ilya Lavrenov 已提交
2796 2797
        break;
    }
I
Ilya Lavrenov 已提交
2798 2799 2800 2801 2802 2803
    case COLOR_BGR5652GRAY: case COLOR_BGR5552GRAY:
    {
        CV_Assert(scn == 2 && depth == CV_8U);
        dcn = 1;
        int greenbits = code == COLOR_BGR5652GRAY ? 6 : 5;
        k.create("BGR5x52Gray", ocl::imgproc::cvtcolor_oclsrc,
2804
                 opts + format("-D dcn=1 -D bidx=0 -D greenbits=%d", greenbits));
I
Ilya Lavrenov 已提交
2805 2806 2807 2808 2809 2810 2811 2812
        break;
    }
    case COLOR_GRAY2BGR565: case COLOR_GRAY2BGR555:
    {
        CV_Assert(scn == 1 && depth == CV_8U);
        dcn = 2;
        int greenbits = code == COLOR_GRAY2BGR565 ? 6 : 5;
        k.create("Gray2BGR5x5", ocl::imgproc::cvtcolor_oclsrc,
2813
                 opts + format("-D dcn=2 -D bidx=0 -D greenbits=%d", greenbits));
I
Ilya Lavrenov 已提交
2814 2815
        break;
    }
I
Ilya Lavrenov 已提交
2816 2817
    case COLOR_BGR2GRAY: case COLOR_BGRA2GRAY:
    case COLOR_RGB2GRAY: case COLOR_RGBA2GRAY:
2818 2819 2820
    {
        CV_Assert(scn == 3 || scn == 4);
        bidx = code == COLOR_BGR2GRAY || code == COLOR_BGRA2GRAY ? 0 : 2;
V
Vadim Pisarevsky 已提交
2821
        dcn = 1;
2822
        k.create("RGB2Gray", ocl::imgproc::cvtcolor_oclsrc,
2823 2824
                 opts + format("-D dcn=1 -D bidx=%d -D STRIPE_SIZE=%d",
                               bidx, stripeSize));
2825
        globalsize[0] = (src.cols + stripeSize-1)/stripeSize;
2826 2827 2828 2829 2830 2831 2832 2833
        break;
    }
    case COLOR_GRAY2BGR:
    case COLOR_GRAY2BGRA:
    {
        CV_Assert(scn == 1);
        dcn = code == COLOR_GRAY2BGRA ? 4 : 3;
        k.create("Gray2RGB", ocl::imgproc::cvtcolor_oclsrc,
2834
                 opts + format("-D bidx=0 -D dcn=%d", dcn));
2835 2836 2837 2838 2839 2840 2841
        break;
    }
    case COLOR_BGR2YUV:
    case COLOR_RGB2YUV:
    {
        CV_Assert(scn == 3 || scn == 4);
        bidx = code == COLOR_RGB2YUV ? 0 : 2;
V
Vadim Pisarevsky 已提交
2842
        dcn = 3;
2843
        k.create("RGB2YUV", ocl::imgproc::cvtcolor_oclsrc,
2844
                 opts + format("-D dcn=3 -D bidx=%d", bidx));
2845 2846 2847 2848 2849 2850 2851 2852 2853
        break;
    }
    case COLOR_YUV2BGR:
    case COLOR_YUV2RGB:
    {
        if(dcn < 0) dcn = 3;
        CV_Assert(dcn == 3 || dcn == 4);
        bidx = code == COLOR_YUV2RGB ? 0 : 2;
        k.create("YUV2RGB", ocl::imgproc::cvtcolor_oclsrc,
2854
                 opts + format("-D dcn=%d -D bidx=%d", dcn, bidx));
2855 2856
        break;
    }
I
Ilya Lavrenov 已提交
2857 2858
    case COLOR_YUV2RGB_NV12: case COLOR_YUV2BGR_NV12:
    case COLOR_YUV2RGBA_NV12: case COLOR_YUV2BGRA_NV12:
2859 2860 2861 2862 2863 2864 2865
    {
        CV_Assert( scn == 1 );
        CV_Assert( sz.width % 2 == 0 && sz.height % 3 == 0 && depth == CV_8U );
        dcn  = code == COLOR_YUV2BGRA_NV12 || code == COLOR_YUV2RGBA_NV12 ? 4 : 3;
        bidx = code == COLOR_YUV2BGRA_NV12 || code == COLOR_YUV2BGR_NV12 ? 0 : 2;

        dstSz = Size(sz.width, sz.height * 2 / 3);
I
Ilya Lavrenov 已提交
2866
        k.create("YUV2RGB_NV12", ocl::imgproc::cvtcolor_oclsrc,
2867
                 opts + format("-D dcn=%d -D bidx=%d", dcn, bidx));
2868 2869 2870 2871 2872 2873 2874
        break;
    }
    case COLOR_BGR2YCrCb:
    case COLOR_RGB2YCrCb:
    {
        CV_Assert(scn == 3 || scn == 4);
        bidx = code == COLOR_BGR2YCrCb ? 0 : 2;
V
Vadim Pisarevsky 已提交
2875
        dcn = 3;
2876
        k.create("RGB2YCrCb", ocl::imgproc::cvtcolor_oclsrc,
2877
                 opts + format("-D dcn=3 -D bidx=%d", bidx));
2878 2879 2880 2881
        break;
    }
    case COLOR_YCrCb2BGR:
    case COLOR_YCrCb2RGB:
I
Ilya Lavrenov 已提交
2882 2883 2884 2885 2886 2887
    {
        if( dcn <= 0 )
            dcn = 3;
        CV_Assert(scn == 3 && (dcn == 3 || dcn == 4));
        bidx = code == COLOR_YCrCb2BGR ? 0 : 2;
        k.create("YCrCb2RGB", ocl::imgproc::cvtcolor_oclsrc,
2888
                 opts + format("-D dcn=%d -D bidx=%d", dcn, bidx));
2889
        break;
I
Ilya Lavrenov 已提交
2890
    }
I
Ilya Lavrenov 已提交
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933
    case COLOR_BGR2XYZ: case COLOR_RGB2XYZ:
    {
        CV_Assert(scn == 3 || scn == 4);
        bidx = code == COLOR_BGR2XYZ ? 0 : 2;

        UMat c;
        if (depth == CV_32F)
        {
            float coeffs[] =
            {
                0.412453f, 0.357580f, 0.180423f,
                0.212671f, 0.715160f, 0.072169f,
                0.019334f, 0.119193f, 0.950227f
            };
            if (bidx == 0)
            {
                std::swap(coeffs[0], coeffs[2]);
                std::swap(coeffs[3], coeffs[5]);
                std::swap(coeffs[6], coeffs[8]);
            }
            Mat(1, 9, CV_32FC1, &coeffs[0]).copyTo(c);
        }
        else
        {
            int coeffs[] =
            {
                1689,    1465,    739,
                871,     2929,    296,
                79,      488,     3892
            };
            if (bidx == 0)
            {
                std::swap(coeffs[0], coeffs[2]);
                std::swap(coeffs[3], coeffs[5]);
                std::swap(coeffs[6], coeffs[8]);
            }
            Mat(1, 9, CV_32SC1, &coeffs[0]).copyTo(c);
        }

        _dst.create(dstSz, CV_MAKETYPE(depth, 3));
        dst = _dst.getUMat();

        k.create("RGB2XYZ", ocl::imgproc::cvtcolor_oclsrc,
2934
                 opts + format("-D dcn=3 -D bidx=%d", bidx));
I
Ilya Lavrenov 已提交
2935 2936
        if (k.empty())
            return false;
2937
        k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst), ocl::KernelArg::PtrReadOnly(c));
I
Ilya Lavrenov 已提交
2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
        return k.run(2, globalsize, 0, false);
    }
    case COLOR_XYZ2BGR: case COLOR_XYZ2RGB:
    {
        if (dcn <= 0)
            dcn = 3;
        CV_Assert(scn == 3 && (dcn == 3 || dcn == 4));
        bidx = code == COLOR_XYZ2BGR ? 0 : 2;

        UMat c;
        if (depth == CV_32F)
        {
            float coeffs[] =
            {
                3.240479f, -1.53715f, -0.498535f,
                -0.969256f, 1.875991f, 0.041556f,
                0.055648f, -0.204043f, 1.057311f
            };
            if (bidx == 0)
            {
                std::swap(coeffs[0], coeffs[6]);
                std::swap(coeffs[1], coeffs[7]);
                std::swap(coeffs[2], coeffs[8]);
            }
            Mat(1, 9, CV_32FC1, &coeffs[0]).copyTo(c);
        }
        else
        {
            int coeffs[] =
            {
                13273,  -6296,  -2042,
                -3970,   7684,    170,
                  228,   -836,   4331
            };
            if (bidx == 0)
            {
                std::swap(coeffs[0], coeffs[6]);
                std::swap(coeffs[1], coeffs[7]);
                std::swap(coeffs[2], coeffs[8]);
            }
            Mat(1, 9, CV_32SC1, &coeffs[0]).copyTo(c);
        }

        _dst.create(dstSz, CV_MAKETYPE(depth, dcn));
        dst = _dst.getUMat();

        k.create("XYZ2RGB", ocl::imgproc::cvtcolor_oclsrc,
2985
                 opts + format("-D dcn=%d -D bidx=%d", dcn, bidx));
I
Ilya Lavrenov 已提交
2986 2987
        if (k.empty())
            return false;
2988
        k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst), ocl::KernelArg::PtrReadOnly(c));
I
Ilya Lavrenov 已提交
2989 2990
        return k.run(2, globalsize, 0, false);
    }
I
Ilya Lavrenov 已提交
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039
    case COLOR_BGR2HSV: case COLOR_RGB2HSV: case COLOR_BGR2HSV_FULL: case COLOR_RGB2HSV_FULL:
    case COLOR_BGR2HLS: case COLOR_RGB2HLS: case COLOR_BGR2HLS_FULL: case COLOR_RGB2HLS_FULL:
    {
        CV_Assert((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F));
        bidx = code == COLOR_BGR2HSV || code == COLOR_BGR2HLS ||
            code == COLOR_BGR2HSV_FULL || code == COLOR_BGR2HLS_FULL ? 0 : 2;
        int hrange = depth == CV_32F ? 360 : code == COLOR_BGR2HSV || code == COLOR_RGB2HSV ||
            code == COLOR_BGR2HLS || code == COLOR_RGB2HLS ? 180 : 256;
        bool is_hsv = code == COLOR_BGR2HSV || code == COLOR_RGB2HSV || code == COLOR_BGR2HSV_FULL || code == COLOR_RGB2HSV_FULL;
        String kernelName = String("RGB2") + (is_hsv ? "HSV" : "HLS");
        dcn = 3;

        if (is_hsv && depth == CV_8U)
        {
            static UMat sdiv_data;
            static UMat hdiv_data180;
            static UMat hdiv_data256;
            static int sdiv_table[256];
            static int hdiv_table180[256];
            static int hdiv_table256[256];
            static volatile bool initialized180 = false, initialized256 = false;
            volatile bool & initialized = hrange == 180 ? initialized180 : initialized256;

            if (!initialized)
            {
                int * const hdiv_table = hrange == 180 ? hdiv_table180 : hdiv_table256, hsv_shift = 12;
                UMat & hdiv_data = hrange == 180 ? hdiv_data180 : hdiv_data256;

                sdiv_table[0] = hdiv_table180[0] = hdiv_table256[0] = 0;

                int v = 255 << hsv_shift;
                if (!initialized180 && !initialized256)
                {
                    for(int i = 1; i < 256; i++ )
                        sdiv_table[i] = saturate_cast<int>(v/(1.*i));
                    Mat(1, 256, CV_32SC1, sdiv_table).copyTo(sdiv_data);
                }

                v = hrange << hsv_shift;
                for (int i = 1; i < 256; i++ )
                    hdiv_table[i] = saturate_cast<int>(v/(6.*i));

                Mat(1, 256, CV_32SC1, hdiv_table).copyTo(hdiv_data);
                initialized = true;
            }

            _dst.create(dstSz, CV_8UC3);
            dst = _dst.getUMat();

3040 3041 3042
            k.create("RGB2HSV", ocl::imgproc::cvtcolor_oclsrc,
                     opts + format("-D hrange=%d -D bidx=%d -D dcn=3",
                                   hrange, bidx));
I
Ilya Lavrenov 已提交
3043 3044
            if (k.empty())
                return false;
I
Ilya Lavrenov 已提交
3045 3046

            k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst),
3047 3048
                   ocl::KernelArg::PtrReadOnly(sdiv_data), hrange == 256 ? ocl::KernelArg::PtrReadOnly(hdiv_data256) :
                                                                       ocl::KernelArg::PtrReadOnly(hdiv_data180));
I
Ilya Lavrenov 已提交
3049 3050 3051 3052 3053

            return k.run(2, globalsize, NULL, false);
        }
        else
            k.create(kernelName.c_str(), ocl::imgproc::cvtcolor_oclsrc,
3054 3055
                     opts + format("-D hscale=%ff -D bidx=%d -D dcn=3",
                                   hrange*(1.f/360.f), bidx));
I
Ilya Lavrenov 已提交
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
        break;
    }
    case COLOR_HSV2BGR: case COLOR_HSV2RGB: case COLOR_HSV2BGR_FULL: case COLOR_HSV2RGB_FULL:
    case COLOR_HLS2BGR: case COLOR_HLS2RGB: case COLOR_HLS2BGR_FULL: case COLOR_HLS2RGB_FULL:
    {
        if (dcn <= 0)
            dcn = 3;
        CV_Assert(scn == 3 && (dcn == 3 || dcn == 4) && (depth == CV_8U || depth == CV_32F));
        bidx = code == COLOR_HSV2BGR || code == COLOR_HLS2BGR ||
            code == COLOR_HSV2BGR_FULL || code == COLOR_HLS2BGR_FULL ? 0 : 2;
        int hrange = depth == CV_32F ? 360 : code == COLOR_HSV2BGR || code == COLOR_HSV2RGB ||
            code == COLOR_HLS2BGR || code == COLOR_HLS2RGB ? 180 : 255;
        bool is_hsv = code == COLOR_HSV2BGR || code == COLOR_HSV2RGB ||
                code == COLOR_HSV2BGR_FULL || code == COLOR_HSV2RGB_FULL;

        String kernelName = String(is_hsv ? "HSV" : "HLS") + "2RGB";
        k.create(kernelName.c_str(), ocl::imgproc::cvtcolor_oclsrc,
3073 3074
                 opts + format("-D dcn=%d -D bidx=%d -D hrange=%d -D hscale=%ff",
                               dcn, bidx, hrange, 6.f/hrange));
I
Ilya Lavrenov 已提交
3075
        break;
I
Ilya Lavrenov 已提交
3076 3077 3078 3079 3080 3081 3082
    }
    case COLOR_RGBA2mRGBA: case COLOR_mRGBA2RGBA:
    {
        CV_Assert(scn == 4 && depth == CV_8U);
        dcn = 4;

        k.create(code == COLOR_RGBA2mRGBA ? "RGBA2mRGBA" : "mRGBA2RGBA", ocl::imgproc::cvtcolor_oclsrc,
3083
                 opts + "-D dcn=4 -D bidx=3");
I
Ilya Lavrenov 已提交
3084
        break;
I
Ilya Lavrenov 已提交
3085
    }
3086 3087 3088 3089 3090 3091 3092 3093 3094
    case CV_BGR2Lab: case CV_RGB2Lab: case CV_LBGR2Lab: case CV_LRGB2Lab:
    {
        CV_Assert( (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) );

        bidx = code == CV_BGR2Lab || code == CV_LBGR2Lab ? 0 : 2;
        bool srgb = code == CV_BGR2Lab || code == CV_RGB2Lab;
        dcn = 3;

        k.create("BGR2Lab", ocl::imgproc::cvtcolor_oclsrc,
3095 3096
                 opts + format("-D dcn=3 -D bidx=%d%s",
                               bidx, srgb ? " -D SRGB" : ""));
3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196
        if (k.empty())
            return false;

        initLabTabs();

        _dst.create(dstSz, CV_MAKETYPE(depth, dcn));
        dst = _dst.getUMat();

        ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
                dstarg = ocl::KernelArg::WriteOnly(dst);

        if (depth == CV_8U)
        {
            static UMat usRGBGammaTab, ulinearGammaTab, uLabCbrtTab, ucoeffs;

            if (srgb && usRGBGammaTab.empty())
                Mat(1, 256, CV_16UC1, sRGBGammaTab_b).copyTo(usRGBGammaTab);
            else if (ulinearGammaTab.empty())
                Mat(1, 256, CV_16UC1, linearGammaTab_b).copyTo(ulinearGammaTab);
            if (uLabCbrtTab.empty())
                Mat(1, LAB_CBRT_TAB_SIZE_B, CV_16UC1, LabCbrtTab_b).copyTo(uLabCbrtTab);

            {
                int coeffs[9];
                const float * const _coeffs = sRGB2XYZ_D65, * const _whitept = D65;
                const float scale[] =
                {
                    (1 << lab_shift)/_whitept[0],
                    (float)(1 << lab_shift),
                    (1 << lab_shift)/_whitept[2]
                };

                for (int i = 0; i < 3; i++ )
                {
                    coeffs[i*3+(bidx^2)] = cvRound(_coeffs[i*3]*scale[i]);
                    coeffs[i*3+1] = cvRound(_coeffs[i*3+1]*scale[i]);
                    coeffs[i*3+bidx] = cvRound(_coeffs[i*3+2]*scale[i]);

                    CV_Assert( coeffs[i] >= 0 && coeffs[i*3+1] >= 0 && coeffs[i*3+2] >= 0 &&
                              coeffs[i*3] + coeffs[i*3+1] + coeffs[i*3+2] < 2*(1 << lab_shift) );
                }
                Mat(1, 9, CV_32SC1, coeffs).copyTo(ucoeffs);
            }

            const int Lscale = (116*255+50)/100;
            const int Lshift = -((16*255*(1 << lab_shift2) + 50)/100);

            k.args(srcarg, dstarg,
                   ocl::KernelArg::PtrReadOnly(srgb ? usRGBGammaTab : ulinearGammaTab),
                   ocl::KernelArg::PtrReadOnly(uLabCbrtTab), ocl::KernelArg::PtrReadOnly(ucoeffs),
                   Lscale, Lshift);
        }
        else
        {
            static UMat usRGBGammaTab, ucoeffs;

            if (srgb && usRGBGammaTab.empty())
                Mat(1, GAMMA_TAB_SIZE * 4, CV_32FC1, sRGBGammaTab).copyTo(usRGBGammaTab);

            {
                float coeffs[9];
                const float * const _coeffs = sRGB2XYZ_D65, * const _whitept = D65;
                float scale[] = { 1.0f / _whitept[0], 1.0f, 1.0f / _whitept[2] };

                for (int i = 0; i < 3; i++)
                {
                    int j = i * 3;
                    coeffs[j + (bidx ^ 2)] = _coeffs[j] * scale[i];
                    coeffs[j + 1] = _coeffs[j + 1] * scale[i];
                    coeffs[j + bidx] = _coeffs[j + 2] * scale[i];

                    CV_Assert( coeffs[j] >= 0 && coeffs[j + 1] >= 0 && coeffs[j + 2] >= 0 &&
                               coeffs[j] + coeffs[j + 1] + coeffs[j + 2] < 1.5f*LabCbrtTabScale );
                }

                Mat(1, 9, CV_32FC1, coeffs).copyTo(ucoeffs);
            }

            float _1_3 = 1.0f / 3.0f, _a = 16.0f / 116.0f;
            ocl::KernelArg ucoeffsarg = ocl::KernelArg::PtrReadOnly(ucoeffs);

            if (srgb)
                k.args(srcarg, dstarg, ocl::KernelArg::PtrReadOnly(usRGBGammaTab),
                       ucoeffsarg, _1_3, _a);
            else
                k.args(srcarg, dstarg, ucoeffsarg, _1_3, _a);
        }

        return k.run(dims, globalsize, NULL, false);
    }
    case CV_Lab2BGR: case CV_Lab2RGB: case CV_Lab2LBGR: case CV_Lab2LRGB:
    {
        if( dcn <= 0 )
            dcn = 3;
        CV_Assert( scn == 3 && (dcn == 3 || dcn == 4) && (depth == CV_8U || depth == CV_32F) );

        bidx = code == CV_Lab2BGR || code == CV_Lab2LBGR ? 0 : 2;
        bool srgb = code == CV_Lab2BGR || code == CV_Lab2RGB;

        k.create("Lab2BGR", ocl::imgproc::cvtcolor_oclsrc,
3197 3198
                 opts + format("-D dcn=%d -D bidx=%d%s",
                               dcn, bidx, srgb ? " -D SRGB" : ""));
3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218
        if (k.empty())
            return false;

        initLabTabs();
        static UMat ucoeffs, usRGBInvGammaTab;

        if (srgb && usRGBInvGammaTab.empty())
            Mat(1, GAMMA_TAB_SIZE*4, CV_32FC1, sRGBInvGammaTab).copyTo(usRGBInvGammaTab);

        {
            float coeffs[9];
            const float * const _coeffs = XYZ2sRGB_D65, * const _whitept = D65;

            for( int i = 0; i < 3; i++ )
            {
                coeffs[i+(bidx^2)*3] = _coeffs[i]*_whitept[i];
                coeffs[i+3] = _coeffs[i+3]*_whitept[i];
                coeffs[i+bidx*3] = _coeffs[i+6]*_whitept[i];
            }

I
Ilya Lavrenov 已提交
3219
            Mat(1, 9, CV_32FC1, coeffs).copyTo(ucoeffs);
3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
        }

        _dst.create(sz, CV_MAKETYPE(depth, dcn));
        dst = _dst.getUMat();

        float lThresh = 0.008856f * 903.3f;
        float fThresh = 7.787f * 0.008856f + 16.0f / 116.0f;

        ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
                dstarg = ocl::KernelArg::WriteOnly(dst),
                coeffsarg = ocl::KernelArg::PtrReadOnly(ucoeffs);

        if (srgb)
            k.args(srcarg, dstarg, ocl::KernelArg::PtrReadOnly(usRGBInvGammaTab),
                   coeffsarg, lThresh, fThresh);
        else
            k.args(srcarg, dstarg, coeffsarg, lThresh, fThresh);

        return k.run(dims, globalsize, NULL, false);
    }
3240
    default:
3241
        break;
3242 3243 3244 3245
    }

    if( !k.empty() )
    {
V
Vadim Pisarevsky 已提交
3246
        _dst.create(dstSz, CV_MAKETYPE(depth, dcn));
3247 3248
        dst = _dst.getUMat();
        k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst));
3249
        ok = k.run(dims, globalsize, NULL, false);
3250 3251 3252 3253
    }
    return ok;
}

I
Ilya Lavrenov 已提交
3254 3255
#endif

3256
}//namespace cv
3257

3258
//////////////////////////////////////////////////////////////////////////////////////////
3259
//                                   The main function                                  //
3260
//////////////////////////////////////////////////////////////////////////////////////////
3261

3262
void cv::cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
3263
{
3264 3265 3266
    int stype = _src.type();
    int scn = CV_MAT_CN(stype), depth = CV_MAT_DEPTH(stype), bidx;

I
Ilya Lavrenov 已提交
3267 3268
    CV_OCL_RUN( _src.dims() <= 2 && _dst.isUMat(),
                ocl_cvtColor(_src, _dst, code, dcn) )
3269

3270
    Mat src = _src.getMat(), dst;
3271
    Size sz = src.size();
A
Andrey Kamaev 已提交
3272

3273
    CV_Assert( depth == CV_8U || depth == CV_16U || depth == CV_32F );
A
Andrey Kamaev 已提交
3274

3275 3276
    switch( code )
    {
3277 3278 3279 3280 3281
        case CV_BGR2BGRA: case CV_RGB2BGRA: case CV_BGRA2BGR:
        case CV_RGBA2BGR: case CV_RGB2BGR: case CV_BGRA2RGBA:
            CV_Assert( scn == 3 || scn == 4 );
            dcn = code == CV_BGR2BGRA || code == CV_RGB2BGRA || code == CV_BGRA2RGBA ? 4 : 3;
            bidx = code == CV_BGR2BGRA || code == CV_BGRA2BGR ? 0 : 2;
A
Andrey Kamaev 已提交
3282

3283 3284
            _dst.create( sz, CV_MAKETYPE(depth, dcn));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3285

3286
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
3287 3288
            if( code == CV_BGR2BGRA || code == CV_RGB2RGBA)
            {
3289
                if ( CvtColorIPPLoop(src, dst, IPPReorderFunctor(ippiSwapChannelsC3C4RTab[depth], 0, 1, 2)) )
3290
                    return;
I
Ilya Lavrenov 已提交
3291
                setIppErrorStatus();
3292 3293 3294
            }
            else if( code == CV_BGRA2BGR )
            {
3295
                if ( CvtColorIPPLoop(src, dst, IPPGeneralFunctor(ippiCopyAC4C3RTab[depth])) )
3296
                    return;
I
Ilya Lavrenov 已提交
3297
                setIppErrorStatus();
3298 3299 3300
            }
            else if( code == CV_BGR2RGBA )
            {
3301
                if( CvtColorIPPLoop(src, dst, IPPReorderFunctor(ippiSwapChannelsC3C4RTab[depth], 2, 1, 0)) )
3302
                    return;
I
Ilya Lavrenov 已提交
3303
                setIppErrorStatus();
3304 3305 3306
            }
            else if( code == CV_RGBA2BGR )
            {
3307
                if( CvtColorIPPLoop(src, dst, IPPReorderFunctor(ippiSwapChannelsC4C3RTab[depth], 2, 1, 0)) )
3308
                    return;
I
Ilya Lavrenov 已提交
3309
                setIppErrorStatus();
3310 3311 3312
            }
            else if( code == CV_RGB2BGR )
            {
3313
                if( CvtColorIPPLoopCopy(src, dst, IPPReorderFunctor(ippiSwapChannelsC3RTab[depth], 2, 1, 0)) )
3314
                    return;
I
Ilya Lavrenov 已提交
3315
                setIppErrorStatus();
3316
            }
3317
#if !defined(HAVE_IPP_ICV_ONLY) && (IPP_VERSION_X100 >= 801)
3318 3319
            else if( code == CV_RGBA2BGRA )
            {
3320
                if( CvtColorIPPLoopCopy(src, dst, IPPReorderFunctor(ippiSwapChannelsC4RTab[depth], 2, 1, 0)) )
3321
                    return;
I
Ilya Lavrenov 已提交
3322
                setIppErrorStatus();
3323
            }
3324
#endif
K
kdrobnyh 已提交
3325
#endif
A
Andrey Kamaev 已提交
3326

3327
            if( depth == CV_8U )
3328 3329 3330 3331 3332 3333
            {
#ifdef HAVE_TEGRA_OPTIMIZATION
                if(!tegra::cvtBGR2RGB(src, dst, bidx))
#endif
                    CvtColorLoop(src, dst, RGB2RGB<uchar>(scn, dcn, bidx));
            }
3334 3335 3336 3337 3338
            else if( depth == CV_16U )
                CvtColorLoop(src, dst, RGB2RGB<ushort>(scn, dcn, bidx));
            else
                CvtColorLoop(src, dst, RGB2RGB<float>(scn, dcn, bidx));
            break;
A
Andrey Kamaev 已提交
3339

3340 3341 3342
        case CV_BGR2BGR565: case CV_BGR2BGR555: case CV_RGB2BGR565: case CV_RGB2BGR555:
        case CV_BGRA2BGR565: case CV_BGRA2BGR555: case CV_RGBA2BGR565: case CV_RGBA2BGR555:
            CV_Assert( (scn == 3 || scn == 4) && depth == CV_8U );
3343 3344
            _dst.create(sz, CV_8UC2);
            dst = _dst.getMat();
3345

A
Alexander Alekhin 已提交
3346
#if defined HAVE_IPP && !defined(HAVE_IPP_ICV_ONLY)
I
Ilya Lavrenov 已提交
3347
            CV_SUPPRESS_DEPRECATED_START
I
Ilya Lavrenov 已提交
3348
#if 0
I
Ilya Lavrenov 已提交
3349 3350 3351 3352
            if (code == CV_BGR2BGR565 && scn == 3)
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralFunctor((ippiGeneralFunc)ippiBGRToBGR565_8u16u_C3R)))
                    return;
I
Ilya Lavrenov 已提交
3353
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3354
            }
I
Ilya Lavrenov 已提交
3355 3356 3357
            else
#endif
            if (code == CV_BGRA2BGR565 && scn == 4)
I
Ilya Lavrenov 已提交
3358
            {
I
Ilya Lavrenov 已提交
3359 3360 3361
                if (CvtColorIPPLoopCopy(src, dst,
                                        IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth],
                                        (ippiGeneralFunc)ippiBGRToBGR565_8u16u_C3R, 0, 1, 2, depth)))
I
Ilya Lavrenov 已提交
3362
                    return;
I
Ilya Lavrenov 已提交
3363
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3364
            }
I
Ilya Lavrenov 已提交
3365
            else if (code == CV_RGB2BGR565 && scn == 3)
I
Ilya Lavrenov 已提交
3366 3367 3368 3369
            {
                if( CvtColorIPPLoopCopy(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC3RTab[depth],
                                                                           (ippiGeneralFunc)ippiBGRToBGR565_8u16u_C3R, 2, 1, 0, depth)) )
                    return;
I
Ilya Lavrenov 已提交
3370
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3371
            }
I
Ilya Lavrenov 已提交
3372
            else if (code == CV_RGBA2BGR565 && scn == 4)
I
Ilya Lavrenov 已提交
3373 3374 3375 3376
            {
                if( CvtColorIPPLoopCopy(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth],
                                                                           (ippiGeneralFunc)ippiBGRToBGR565_8u16u_C3R, 2, 1, 0, depth)) )
                    return;
I
Ilya Lavrenov 已提交
3377
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3378 3379 3380 3381
            }
            CV_SUPPRESS_DEPRECATED_END
#endif

3382 3383 3384 3385 3386
#ifdef HAVE_TEGRA_OPTIMIZATION
            if(code == CV_BGR2BGR565 || code == CV_BGRA2BGR565 || code == CV_RGB2BGR565  || code == CV_RGBA2BGR565)
                if(tegra::cvtRGB2RGB565(src, dst, code == CV_RGB2BGR565 || code == CV_RGBA2BGR565 ? 0 : 2))
                    break;
#endif
A
Andrey Kamaev 已提交
3387

3388 3389 3390 3391 3392 3393 3394
            CvtColorLoop(src, dst, RGB2RGB5x5(scn,
                      code == CV_BGR2BGR565 || code == CV_BGR2BGR555 ||
                      code == CV_BGRA2BGR565 || code == CV_BGRA2BGR555 ? 0 : 2,
                      code == CV_BGR2BGR565 || code == CV_RGB2BGR565 ||
                      code == CV_BGRA2BGR565 || code == CV_RGBA2BGR565 ? 6 : 5 // green bits
                                              ));
            break;
A
Andrey Kamaev 已提交
3395

3396 3397
        case CV_BGR5652BGR: case CV_BGR5552BGR: case CV_BGR5652RGB: case CV_BGR5552RGB:
        case CV_BGR5652BGRA: case CV_BGR5552BGRA: case CV_BGR5652RGBA: case CV_BGR5552RGBA:
3398
            if(dcn <= 0) dcn = (code==CV_BGR5652BGRA || code==CV_BGR5552BGRA || code==CV_BGR5652RGBA || code==CV_BGR5552RGBA) ? 4 : 3;
3399
            CV_Assert( (dcn == 3 || dcn == 4) && scn == 2 && depth == CV_8U );
3400 3401
            _dst.create(sz, CV_MAKETYPE(depth, dcn));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3402

A
Alexander Alekhin 已提交
3403
#if defined HAVE_IPP && !defined(HAVE_IPP_ICV_ONLY)
I
Ilya Lavrenov 已提交
3404
            CV_SUPPRESS_DEPRECATED_START
I
Ilya Lavrenov 已提交
3405
            if (code == CV_BGR5652BGR && dcn == 3)
I
Ilya Lavrenov 已提交
3406 3407 3408
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralFunctor((ippiGeneralFunc)ippiBGR565ToBGR_16u8u_C3R)))
                    return;
I
Ilya Lavrenov 已提交
3409
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3410
            }
I
Ilya Lavrenov 已提交
3411
            else if (code == CV_BGR5652RGB && dcn == 3)
I
Ilya Lavrenov 已提交
3412 3413 3414 3415
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiBGR565ToBGR_16u8u_C3R,
                                                                       ippiSwapChannelsC3RTab[depth], 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3416
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3417
            }
I
Ilya Lavrenov 已提交
3418
            else if (code == CV_BGR5652BGRA && dcn == 4)
I
Ilya Lavrenov 已提交
3419 3420 3421 3422
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiBGR565ToBGR_16u8u_C3R,
                                                                       ippiSwapChannelsC3C4RTab[depth], 0, 1, 2, depth)))
                    return;
I
Ilya Lavrenov 已提交
3423
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3424
            }
I
Ilya Lavrenov 已提交
3425
            else if (code == CV_BGR5652RGBA && dcn == 4)
I
Ilya Lavrenov 已提交
3426 3427 3428 3429
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiBGR565ToBGR_16u8u_C3R,
                                                                       ippiSwapChannelsC3C4RTab[depth], 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3430
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3431 3432 3433 3434
            }
            CV_SUPPRESS_DEPRECATED_END
#endif

3435 3436 3437 3438 3439 3440 3441
            CvtColorLoop(src, dst, RGB5x52RGB(dcn,
                      code == CV_BGR5652BGR || code == CV_BGR5552BGR ||
                      code == CV_BGR5652BGRA || code == CV_BGR5552BGRA ? 0 : 2, // blue idx
                      code == CV_BGR5652BGR || code == CV_BGR5652RGB ||
                      code == CV_BGR5652BGRA || code == CV_BGR5652RGBA ? 6 : 5 // green bits
                      ));
            break;
A
Andrey Kamaev 已提交
3442

3443 3444
        case CV_BGR2GRAY: case CV_BGRA2GRAY: case CV_RGB2GRAY: case CV_RGBA2GRAY:
            CV_Assert( scn == 3 || scn == 4 );
3445 3446
            _dst.create(sz, CV_MAKETYPE(depth, 1));
            dst = _dst.getMat();
I
Ilya Lavrenov 已提交
3447

K
kdrobnyh 已提交
3448
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
I
Ilya Lavrenov 已提交
3449
            if( code == CV_BGR2GRAY && depth == CV_32F )
3450
            {
3451
                if( CvtColorIPPLoop(src, dst, IPPColor2GrayFunctor(ippiColor2GrayC3Tab[depth])) )
3452
                    return;
I
Ilya Lavrenov 已提交
3453
                setIppErrorStatus();
3454
            }
I
Ilya Lavrenov 已提交
3455
            else if( code == CV_RGB2GRAY && depth == CV_32F )
3456
            {
3457
                if( CvtColorIPPLoop(src, dst, IPPGeneralFunctor(ippiRGB2GrayC3Tab[depth])) )
3458
                    return;
I
Ilya Lavrenov 已提交
3459
                setIppErrorStatus();
3460
            }
I
Ilya Lavrenov 已提交
3461
            else if( code == CV_BGRA2GRAY && depth == CV_32F )
3462
            {
3463
                if( CvtColorIPPLoop(src, dst, IPPColor2GrayFunctor(ippiColor2GrayC4Tab[depth])) )
3464
                    return;
I
Ilya Lavrenov 已提交
3465
                setIppErrorStatus();
3466
            }
I
Ilya Lavrenov 已提交
3467
            else if( code == CV_RGBA2GRAY && depth == CV_32F )
3468
            {
3469
                if( CvtColorIPPLoop(src, dst, IPPGeneralFunctor(ippiRGB2GrayC4Tab[depth])) )
3470
                    return;
I
Ilya Lavrenov 已提交
3471
                setIppErrorStatus();
3472
            }
K
kdrobnyh 已提交
3473
#endif
I
Ilya Lavrenov 已提交
3474

3475
            bidx = code == CV_BGR2GRAY || code == CV_BGRA2GRAY ? 0 : 2;
A
Andrey Kamaev 已提交
3476

3477
            if( depth == CV_8U )
3478 3479
            {
#ifdef HAVE_TEGRA_OPTIMIZATION
A
Andrey Kamaev 已提交
3480
                if(!tegra::cvtRGB2Gray(src, dst, bidx))
3481
#endif
3482
                CvtColorLoop(src, dst, RGB2Gray<uchar>(scn, bidx, 0));
3483
            }
3484 3485 3486 3487 3488
            else if( depth == CV_16U )
                CvtColorLoop(src, dst, RGB2Gray<ushort>(scn, bidx, 0));
            else
                CvtColorLoop(src, dst, RGB2Gray<float>(scn, bidx, 0));
            break;
A
Andrey Kamaev 已提交
3489

3490 3491
        case CV_BGR5652GRAY: case CV_BGR5552GRAY:
            CV_Assert( scn == 2 && depth == CV_8U );
3492 3493
            _dst.create(sz, CV_8UC1);
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3494

3495 3496
            CvtColorLoop(src, dst, RGB5x52Gray(code == CV_BGR5652GRAY ? 6 : 5));
            break;
A
Andrey Kamaev 已提交
3497

3498
        case CV_GRAY2BGR: case CV_GRAY2BGRA:
3499
            if( dcn <= 0 ) dcn = (code==CV_GRAY2BGRA) ? 4 : 3;
3500
            CV_Assert( scn == 1 && (dcn == 3 || dcn == 4));
3501 3502
            _dst.create(sz, CV_MAKETYPE(depth, dcn));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3503

K
kdrobnyh 已提交
3504
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
3505 3506
            if( code == CV_GRAY2BGR )
            {
3507
                if( CvtColorIPPLoop(src, dst, IPPGray2BGRFunctor(ippiCopyP3C3RTab[depth])) )
3508
                    return;
I
Ilya Lavrenov 已提交
3509
                setIppErrorStatus();
3510
            }
3511 3512
            else if( code == CV_GRAY2BGRA )
            {
3513
                if( CvtColorIPPLoop(src, dst, IPPGray2BGRAFunctor(ippiCopyP3C3RTab[depth], ippiSwapChannelsC3C4RTab[depth], depth)) )
3514
                    return;
I
Ilya Lavrenov 已提交
3515
                setIppErrorStatus();
3516
            }
K
kdrobnyh 已提交
3517
#endif
A
Andrey Kamaev 已提交
3518 3519


3520
            if( depth == CV_8U )
3521 3522
            {
#ifdef HAVE_TEGRA_OPTIMIZATION
A
Andrey Kamaev 已提交
3523
                if(!tegra::cvtGray2RGB(src, dst))
3524
#endif
3525
                CvtColorLoop(src, dst, Gray2RGB<uchar>(dcn));
3526
            }
3527 3528 3529 3530 3531
            else if( depth == CV_16U )
                CvtColorLoop(src, dst, Gray2RGB<ushort>(dcn));
            else
                CvtColorLoop(src, dst, Gray2RGB<float>(dcn));
            break;
A
Andrey Kamaev 已提交
3532

3533 3534
        case CV_GRAY2BGR565: case CV_GRAY2BGR555:
            CV_Assert( scn == 1 && depth == CV_8U );
3535 3536
            _dst.create(sz, CV_8UC2);
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3537

3538 3539
            CvtColorLoop(src, dst, Gray2RGB5x5(code == CV_GRAY2BGR565 ? 6 : 5));
            break;
A
Andrey Kamaev 已提交
3540

3541 3542 3543 3544
        case CV_BGR2YCrCb: case CV_RGB2YCrCb:
        case CV_BGR2YUV: case CV_RGB2YUV:
            {
            CV_Assert( scn == 3 || scn == 4 );
3545
            bidx = code == CV_BGR2YCrCb || code == CV_BGR2YUV ? 0 : 2;
3546 3547 3548 3549
            static const float yuv_f[] = { 0.114f, 0.587f, 0.299f, 0.492f, 0.877f };
            static const int yuv_i[] = { B2Y, G2Y, R2Y, 8061, 14369 };
            const float* coeffs_f = code == CV_BGR2YCrCb || code == CV_RGB2YCrCb ? 0 : yuv_f;
            const int* coeffs_i = code == CV_BGR2YCrCb || code == CV_RGB2YCrCb ? 0 : yuv_i;
A
Andrey Kamaev 已提交
3550

3551 3552
            _dst.create(sz, CV_MAKETYPE(depth, 3));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3553

I
Ilya Lavrenov 已提交
3554 3555 3556 3557 3558
#if defined HAVE_IPP && 0
            if (code == CV_RGB2YUV && scn == 3 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralFunctor((ippiGeneralFunc)ippiRGBToYUV_8u_C3R)))
                    return;
I
Ilya Lavrenov 已提交
3559
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3560 3561 3562 3563 3564 3565
            }
            else if (code == CV_BGR2YUV && scn == 3 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC3RTab[depth],
                                                                       (ippiGeneralFunc)ippiRGBToYUV_8u_C3R, 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3566
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3567 3568 3569 3570 3571 3572
            }
            else if (code == CV_RGB2YUV && scn == 4 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth],
                                                                       (ippiGeneralFunc)ippiRGBToYUV_8u_C3R, 0, 1, 2, depth)))
                    return;
I
Ilya Lavrenov 已提交
3573
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3574 3575 3576 3577 3578 3579
            }
            else if (code == CV_BGR2YUV && scn == 4 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth],
                                                                       (ippiGeneralFunc)ippiRGBToYUV_8u_C3R, 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3580
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3581 3582 3583
            }
#endif

3584
            if( depth == CV_8U )
3585 3586
            {
#ifdef HAVE_TEGRA_OPTIMIZATION
A
Andrey Kamaev 已提交
3587
                if((code == CV_RGB2YCrCb || code == CV_BGR2YCrCb) && tegra::cvtRGB2YCrCb(src, dst, bidx))
3588 3589
                    break;
#endif
3590
                CvtColorLoop(src, dst, RGB2YCrCb_i<uchar>(scn, bidx, coeffs_i));
3591
            }
3592 3593 3594 3595 3596 3597
            else if( depth == CV_16U )
                CvtColorLoop(src, dst, RGB2YCrCb_i<ushort>(scn, bidx, coeffs_i));
            else
                CvtColorLoop(src, dst, RGB2YCrCb_f<float>(scn, bidx, coeffs_f));
            }
            break;
A
Andrey Kamaev 已提交
3598

3599 3600 3601 3602 3603
        case CV_YCrCb2BGR: case CV_YCrCb2RGB:
        case CV_YUV2BGR: case CV_YUV2RGB:
            {
            if( dcn <= 0 ) dcn = 3;
            CV_Assert( scn == 3 && (dcn == 3 || dcn == 4) );
3604
            bidx = code == CV_YCrCb2BGR || code == CV_YUV2BGR ? 0 : 2;
3605
            static const float yuv_f[] = { 2.032f, -0.395f, -0.581f, 1.140f };
A
Andrey Kamaev 已提交
3606
            static const int yuv_i[] = { 33292, -6472, -9519, 18678 };
3607 3608
            const float* coeffs_f = code == CV_YCrCb2BGR || code == CV_YCrCb2RGB ? 0 : yuv_f;
            const int* coeffs_i = code == CV_YCrCb2BGR || code == CV_YCrCb2RGB ? 0 : yuv_i;
A
Andrey Kamaev 已提交
3609

3610 3611
            _dst.create(sz, CV_MAKETYPE(depth, dcn));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3612

I
Ilya Lavrenov 已提交
3613 3614 3615 3616 3617
#if defined HAVE_IPP && 0
            if (code == CV_YUV2RGB && dcn == 3 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralFunctor((ippiGeneralFunc)ippiYUVToRGB_8u_C3R)))
                    return;
I
Ilya Lavrenov 已提交
3618
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3619 3620 3621 3622 3623 3624
            }
            else if (code == CV_YUV2BGR && dcn == 3 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiYUVToRGB_8u_C3R,
                                                                       ippiSwapChannelsC3RTab[depth], 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3625
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3626 3627 3628 3629 3630 3631
            }
            else if (code == CV_YUV2RGB && dcn == 4 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiYUVToRGB_8u_C3R,
                                                                       ippiSwapChannelsC3C4RTab[depth], 0, 1, 2, depth)))
                    return;
I
Ilya Lavrenov 已提交
3632
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3633 3634 3635 3636 3637 3638
            }
            else if (code == CV_YUV2BGR && dcn == 4 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiYUVToRGB_8u_C3R,
                                                                       ippiSwapChannelsC3C4RTab[depth], 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3639
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3640 3641 3642
            }
#endif

3643 3644 3645 3646 3647 3648 3649 3650
            if( depth == CV_8U )
                CvtColorLoop(src, dst, YCrCb2RGB_i<uchar>(dcn, bidx, coeffs_i));
            else if( depth == CV_16U )
                CvtColorLoop(src, dst, YCrCb2RGB_i<ushort>(dcn, bidx, coeffs_i));
            else
                CvtColorLoop(src, dst, YCrCb2RGB_f<float>(dcn, bidx, coeffs_f));
            }
            break;
A
Andrey Kamaev 已提交
3651

3652 3653 3654
        case CV_BGR2XYZ: case CV_RGB2XYZ:
            CV_Assert( scn == 3 || scn == 4 );
            bidx = code == CV_BGR2XYZ ? 0 : 2;
A
Andrey Kamaev 已提交
3655

3656 3657
            _dst.create(sz, CV_MAKETYPE(depth, 3));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3658

K
kdrobnyh 已提交
3659
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
I
Ilya Lavrenov 已提交
3660
            if( code == CV_BGR2XYZ && scn == 3 && depth != CV_32F )
3661
            {
3662
                if( CvtColorIPPLoopCopy(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC3RTab[depth], ippiRGB2XYZTab[depth], 2, 1, 0, depth)) )
3663
                    return;
I
Ilya Lavrenov 已提交
3664
                setIppErrorStatus();
3665
            }
I
Ilya Lavrenov 已提交
3666
            else if( code == CV_BGR2XYZ && scn == 4 && depth != CV_32F )
3667
            {
3668
                if( CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth], ippiRGB2XYZTab[depth], 2, 1, 0, depth)) )
3669
                    return;
I
Ilya Lavrenov 已提交
3670
                setIppErrorStatus();
3671
            }
I
Ilya Lavrenov 已提交
3672
            else if( code == CV_RGB2XYZ && scn == 3 && depth != CV_32F )
3673
            {
3674
                if( CvtColorIPPLoopCopy(src, dst, IPPGeneralFunctor(ippiRGB2XYZTab[depth])) )
3675
                    return;
I
Ilya Lavrenov 已提交
3676
                setIppErrorStatus();
3677
            }
I
Ilya Lavrenov 已提交
3678
            else if( code == CV_RGB2XYZ && scn == 4 && depth != CV_32F )
3679
            {
3680
                if( CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth], ippiRGB2XYZTab[depth], 0, 1, 2, depth)) )
3681
                    return;
I
Ilya Lavrenov 已提交
3682
                setIppErrorStatus();
3683
            }
K
kdrobnyh 已提交
3684
#endif
A
Andrey Kamaev 已提交
3685

3686 3687 3688 3689 3690 3691 3692
            if( depth == CV_8U )
                CvtColorLoop(src, dst, RGB2XYZ_i<uchar>(scn, bidx, 0));
            else if( depth == CV_16U )
                CvtColorLoop(src, dst, RGB2XYZ_i<ushort>(scn, bidx, 0));
            else
                CvtColorLoop(src, dst, RGB2XYZ_f<float>(scn, bidx, 0));
            break;
A
Andrey Kamaev 已提交
3693

3694 3695 3696 3697
        case CV_XYZ2BGR: case CV_XYZ2RGB:
            if( dcn <= 0 ) dcn = 3;
            CV_Assert( scn == 3 && (dcn == 3 || dcn == 4) );
            bidx = code == CV_XYZ2BGR ? 0 : 2;
A
Andrey Kamaev 已提交
3698

3699 3700
            _dst.create(sz, CV_MAKETYPE(depth, dcn));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3701

K
kdrobnyh 已提交
3702
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
I
Ilya Lavrenov 已提交
3703
            if( code == CV_XYZ2BGR && dcn == 3 && depth != CV_32F )
3704
            {
3705
                if( CvtColorIPPLoopCopy(src, dst, IPPGeneralReorderFunctor(ippiXYZ2RGBTab[depth], ippiSwapChannelsC3RTab[depth], 2, 1, 0, depth)) )
3706
                    return;
I
Ilya Lavrenov 已提交
3707
                setIppErrorStatus();
3708
            }
I
Ilya Lavrenov 已提交
3709
            else if( code == CV_XYZ2BGR && dcn == 4 && depth != CV_32F )
3710
            {
3711
                if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiXYZ2RGBTab[depth], ippiSwapChannelsC3C4RTab[depth], 2, 1, 0, depth)) )
3712
                    return;
I
Ilya Lavrenov 已提交
3713
                setIppErrorStatus();
3714
            }
I
Ilya Lavrenov 已提交
3715
            if( code == CV_XYZ2RGB && dcn == 3 && depth != CV_32F )
3716
            {
3717
                if( CvtColorIPPLoopCopy(src, dst, IPPGeneralFunctor(ippiXYZ2RGBTab[depth])) )
3718
                    return;
I
Ilya Lavrenov 已提交
3719
                setIppErrorStatus();
3720
            }
I
Ilya Lavrenov 已提交
3721
            else if( code == CV_XYZ2RGB && dcn == 4 && depth != CV_32F )
3722
            {
3723
                if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiXYZ2RGBTab[depth], ippiSwapChannelsC3C4RTab[depth], 0, 1, 2, depth)) )
3724
                    return;
I
Ilya Lavrenov 已提交
3725
                setIppErrorStatus();
3726
            }
K
kdrobnyh 已提交
3727
#endif
A
Andrey Kamaev 已提交
3728

3729 3730 3731 3732 3733 3734 3735
            if( depth == CV_8U )
                CvtColorLoop(src, dst, XYZ2RGB_i<uchar>(dcn, bidx, 0));
            else if( depth == CV_16U )
                CvtColorLoop(src, dst, XYZ2RGB_i<ushort>(dcn, bidx, 0));
            else
                CvtColorLoop(src, dst, XYZ2RGB_f<float>(dcn, bidx, 0));
            break;
A
Andrey Kamaev 已提交
3736

3737 3738 3739 3740 3741 3742 3743
        case CV_BGR2HSV: case CV_RGB2HSV: case CV_BGR2HSV_FULL: case CV_RGB2HSV_FULL:
        case CV_BGR2HLS: case CV_RGB2HLS: case CV_BGR2HLS_FULL: case CV_RGB2HLS_FULL:
            {
            CV_Assert( (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) );
            bidx = code == CV_BGR2HSV || code == CV_BGR2HLS ||
                code == CV_BGR2HSV_FULL || code == CV_BGR2HLS_FULL ? 0 : 2;
            int hrange = depth == CV_32F ? 360 : code == CV_BGR2HSV || code == CV_RGB2HSV ||
3744
                code == CV_BGR2HLS || code == CV_RGB2HLS ? 180 : 256;
A
Andrey Kamaev 已提交
3745

3746 3747
            _dst.create(sz, CV_MAKETYPE(depth, 3));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3748

K
kdrobnyh 已提交
3749
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
3750 3751 3752 3753
            if( depth == CV_8U || depth == CV_16U )
            {
                if( code == CV_BGR2HSV_FULL && scn == 3 )
                {
3754
                    if( CvtColorIPPLoopCopy(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC3RTab[depth], ippiRGB2HSVTab[depth], 2, 1, 0, depth)) )
3755
                        return;
I
Ilya Lavrenov 已提交
3756
                    setIppErrorStatus();
3757 3758 3759
                }
                else if( code == CV_BGR2HSV_FULL && scn == 4 )
                {
3760
                    if( CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth], ippiRGB2HSVTab[depth], 2, 1, 0, depth)) )
3761
                        return;
I
Ilya Lavrenov 已提交
3762
                    setIppErrorStatus();
3763
                }
I
Ilya Lavrenov 已提交
3764
                else if( code == CV_RGB2HSV_FULL && scn == 3 && depth == CV_16U )
3765
                {
3766
                    if( CvtColorIPPLoopCopy(src, dst, IPPGeneralFunctor(ippiRGB2HSVTab[depth])) )
3767
                        return;
I
Ilya Lavrenov 已提交
3768
                    setIppErrorStatus();
3769
                }
3770 3771
                else if( code == CV_RGB2HSV_FULL && scn == 4 )
                {
3772
                    if( CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth], ippiRGB2HSVTab[depth], 0, 1, 2, depth)) )
3773
                        return;
I
Ilya Lavrenov 已提交
3774
                    setIppErrorStatus();
3775
                }
3776 3777
                else if( code == CV_BGR2HLS_FULL && scn == 3 )
                {
3778
                    if( CvtColorIPPLoopCopy(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC3RTab[depth], ippiRGB2HLSTab[depth], 2, 1, 0, depth)) )
3779
                        return;
I
Ilya Lavrenov 已提交
3780
                    setIppErrorStatus();
3781 3782 3783
                }
                else if( code == CV_BGR2HLS_FULL && scn == 4 )
                {
3784
                    if( CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth], ippiRGB2HLSTab[depth], 2, 1, 0, depth)) )
3785
                        return;
I
Ilya Lavrenov 已提交
3786
                    setIppErrorStatus();
3787 3788 3789
                }
                else if( code == CV_RGB2HLS_FULL && scn == 3 )
                {
3790
                    if( CvtColorIPPLoopCopy(src, dst, IPPGeneralFunctor(ippiRGB2HLSTab[depth])) )
3791
                        return;
I
Ilya Lavrenov 已提交
3792
                    setIppErrorStatus();
3793
                }
3794 3795
                else if( code == CV_RGB2HLS_FULL && scn == 4 )
                {
3796
                    if( CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth], ippiRGB2HLSTab[depth], 0, 1, 2, depth)) )
3797
                        return;
I
Ilya Lavrenov 已提交
3798
                    setIppErrorStatus();
3799 3800
                }
            }
K
kdrobnyh 已提交
3801
#endif
A
Andrey Kamaev 已提交
3802

3803 3804 3805
            if( code == CV_BGR2HSV || code == CV_RGB2HSV ||
                code == CV_BGR2HSV_FULL || code == CV_RGB2HSV_FULL )
            {
3806
#ifdef HAVE_TEGRA_OPTIMIZATION
A
Andrey Kamaev 已提交
3807
                if(tegra::cvtRGB2HSV(src, dst, bidx, hrange))
3808 3809
                    break;
#endif
3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823
                if( depth == CV_8U )
                    CvtColorLoop(src, dst, RGB2HSV_b(scn, bidx, hrange));
                else
                    CvtColorLoop(src, dst, RGB2HSV_f(scn, bidx, (float)hrange));
            }
            else
            {
                if( depth == CV_8U )
                    CvtColorLoop(src, dst, RGB2HLS_b(scn, bidx, hrange));
                else
                    CvtColorLoop(src, dst, RGB2HLS_f(scn, bidx, (float)hrange));
            }
            }
            break;
A
Andrey Kamaev 已提交
3824

3825 3826 3827 3828 3829 3830 3831 3832 3833
        case CV_HSV2BGR: case CV_HSV2RGB: case CV_HSV2BGR_FULL: case CV_HSV2RGB_FULL:
        case CV_HLS2BGR: case CV_HLS2RGB: case CV_HLS2BGR_FULL: case CV_HLS2RGB_FULL:
            {
            if( dcn <= 0 ) dcn = 3;
            CV_Assert( scn == 3 && (dcn == 3 || dcn == 4) && (depth == CV_8U || depth == CV_32F) );
            bidx = code == CV_HSV2BGR || code == CV_HLS2BGR ||
                code == CV_HSV2BGR_FULL || code == CV_HLS2BGR_FULL ? 0 : 2;
            int hrange = depth == CV_32F ? 360 : code == CV_HSV2BGR || code == CV_HSV2RGB ||
                code == CV_HLS2BGR || code == CV_HLS2RGB ? 180 : 255;
A
Andrey Kamaev 已提交
3834

3835 3836
            _dst.create(sz, CV_MAKETYPE(depth, dcn));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3837

K
kdrobnyh 已提交
3838
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
3839 3840 3841 3842
            if( depth == CV_8U || depth == CV_16U )
            {
                if( code == CV_HSV2BGR_FULL && dcn == 3 )
                {
3843
                    if( CvtColorIPPLoopCopy(src, dst, IPPGeneralReorderFunctor(ippiHSV2RGBTab[depth], ippiSwapChannelsC3RTab[depth], 2, 1, 0, depth)) )
3844
                        return;
I
Ilya Lavrenov 已提交
3845
                    setIppErrorStatus();
3846 3847 3848
                }
                else if( code == CV_HSV2BGR_FULL && dcn == 4 )
                {
3849
                    if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiHSV2RGBTab[depth], ippiSwapChannelsC3C4RTab[depth], 2, 1, 0, depth)) )
3850
                        return;
I
Ilya Lavrenov 已提交
3851
                    setIppErrorStatus();
3852 3853 3854
                }
                else if( code == CV_HSV2RGB_FULL && dcn == 3 )
                {
3855
                    if( CvtColorIPPLoopCopy(src, dst, IPPGeneralFunctor(ippiHSV2RGBTab[depth])) )
3856
                        return;
I
Ilya Lavrenov 已提交
3857
                    setIppErrorStatus();
3858
                }
3859 3860
                else if( code == CV_HSV2RGB_FULL && dcn == 4 )
                {
3861
                    if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiHSV2RGBTab[depth], ippiSwapChannelsC3C4RTab[depth], 0, 1, 2, depth)) )
3862
                        return;
I
Ilya Lavrenov 已提交
3863
                    setIppErrorStatus();
3864
                }
3865 3866
                else if( code == CV_HLS2BGR_FULL && dcn == 3 )
                {
3867
                    if( CvtColorIPPLoopCopy(src, dst, IPPGeneralReorderFunctor(ippiHLS2RGBTab[depth], ippiSwapChannelsC3RTab[depth], 2, 1, 0, depth)) )
3868
                        return;
I
Ilya Lavrenov 已提交
3869
                    setIppErrorStatus();
3870 3871 3872
                }
                else if( code == CV_HLS2BGR_FULL && dcn == 4 )
                {
3873
                    if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiHLS2RGBTab[depth], ippiSwapChannelsC3C4RTab[depth], 2, 1, 0, depth)) )
3874
                        return;
I
Ilya Lavrenov 已提交
3875
                    setIppErrorStatus();
3876 3877 3878
                }
                else if( code == CV_HLS2RGB_FULL && dcn == 3 )
                {
3879
                    if( CvtColorIPPLoopCopy(src, dst, IPPGeneralFunctor(ippiHLS2RGBTab[depth])) )
3880
                        return;
I
Ilya Lavrenov 已提交
3881
                    setIppErrorStatus();
3882
                }
3883 3884
                else if( code == CV_HLS2RGB_FULL && dcn == 4 )
                {
3885
                    if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiHLS2RGBTab[depth], ippiSwapChannelsC3C4RTab[depth], 0, 1, 2, depth)) )
3886
                        return;
I
Ilya Lavrenov 已提交
3887
                    setIppErrorStatus();
3888 3889
                }
            }
K
kdrobnyh 已提交
3890
#endif
A
Andrey Kamaev 已提交
3891

3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908
            if( code == CV_HSV2BGR || code == CV_HSV2RGB ||
                code == CV_HSV2BGR_FULL || code == CV_HSV2RGB_FULL )
            {
                if( depth == CV_8U )
                    CvtColorLoop(src, dst, HSV2RGB_b(dcn, bidx, hrange));
                else
                    CvtColorLoop(src, dst, HSV2RGB_f(dcn, bidx, (float)hrange));
            }
            else
            {
                if( depth == CV_8U )
                    CvtColorLoop(src, dst, HLS2RGB_b(dcn, bidx, hrange));
                else
                    CvtColorLoop(src, dst, HLS2RGB_f(dcn, bidx, (float)hrange));
            }
            }
            break;
A
Andrey Kamaev 已提交
3909

3910 3911 3912 3913 3914 3915 3916 3917
        case CV_BGR2Lab: case CV_RGB2Lab: case CV_LBGR2Lab: case CV_LRGB2Lab:
        case CV_BGR2Luv: case CV_RGB2Luv: case CV_LBGR2Luv: case CV_LRGB2Luv:
            {
            CV_Assert( (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) );
            bidx = code == CV_BGR2Lab || code == CV_BGR2Luv ||
                   code == CV_LBGR2Lab || code == CV_LBGR2Luv ? 0 : 2;
            bool srgb = code == CV_BGR2Lab || code == CV_RGB2Lab ||
                        code == CV_BGR2Luv || code == CV_RGB2Luv;
A
Andrey Kamaev 已提交
3918

3919 3920
            _dst.create(sz, CV_MAKETYPE(depth, 3));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
3921

A
Alexander Alekhin 已提交
3922
#if defined HAVE_IPP && !defined(HAVE_IPP_ICV_ONLY)
I
Ilya Lavrenov 已提交
3923
#if 0
I
Ilya Lavrenov 已提交
3924 3925 3926 3927
            if (code == CV_LBGR2Lab && scn == 3 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralFunctor((ippiGeneralFunc)ippiBGRToLab_8u_C3R)))
                    return;
I
Ilya Lavrenov 已提交
3928
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3929 3930 3931 3932 3933 3934
            }
            else if (code == CV_LBGR2Lab && scn == 4 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth],
                                                                       (ippiGeneralFunc)ippiBGRToLab_8u_C3R, 0, 1, 2, depth)))
                    return;
I
Ilya Lavrenov 已提交
3935
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3936
            }
I
Ilya Lavrenov 已提交
3937 3938 3939
            else
#endif
            if (code == CV_LRGB2Lab && scn == 3 && depth == CV_8U)
I
Ilya Lavrenov 已提交
3940 3941 3942 3943
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC3RTab[depth],
                                                                       (ippiGeneralFunc)ippiBGRToLab_8u_C3R, 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3944
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3945 3946 3947 3948 3949 3950
            }
            else if (code == CV_LRGB2Lab && scn == 4 && depth == CV_8U)
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth],
                                                                       (ippiGeneralFunc)ippiBGRToLab_8u_C3R, 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3951
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3952
            }
I
Ilya Lavrenov 已提交
3953
#if 0
I
Ilya Lavrenov 已提交
3954 3955 3956 3957
            else if (code == CV_LRGB2Luv && scn == 3)
            {
                if (CvtColorIPPLoop(src, dst, IPPGeneralFunctor(ippiRGBToLUVTab[depth])))
                    return;
I
Ilya Lavrenov 已提交
3958
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3959 3960 3961 3962 3963 3964
            }
            else if (code == CV_LRGB2Luv && scn == 4)
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth],
                                                                       ippiRGBToLUVTab[depth], 0, 1, 2, depth)))
                    return;
I
Ilya Lavrenov 已提交
3965
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3966 3967 3968 3969 3970 3971
            }
            else if (code == CV_LBGR2Luv && scn == 3)
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC3RTab[depth],
                                                                       ippiRGBToLUVTab[depth], 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3972
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3973 3974 3975 3976 3977 3978
            }
            else if (code == CV_LBGR2Luv && scn == 4)
            {
                if (CvtColorIPPLoop(src, dst, IPPReorderGeneralFunctor(ippiSwapChannelsC4C3RTab[depth],
                                                                       ippiRGBToLUVTab[depth], 2, 1, 0, depth)))
                    return;
I
Ilya Lavrenov 已提交
3979
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
3980
            }
I
Ilya Lavrenov 已提交
3981
#endif
I
Ilya Lavrenov 已提交
3982 3983
#endif

3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000
            if( code == CV_BGR2Lab || code == CV_RGB2Lab ||
                code == CV_LBGR2Lab || code == CV_LRGB2Lab )
            {
                if( depth == CV_8U )
                    CvtColorLoop(src, dst, RGB2Lab_b(scn, bidx, 0, 0, srgb));
                else
                    CvtColorLoop(src, dst, RGB2Lab_f(scn, bidx, 0, 0, srgb));
            }
            else
            {
                if( depth == CV_8U )
                    CvtColorLoop(src, dst, RGB2Luv_b(scn, bidx, 0, 0, srgb));
                else
                    CvtColorLoop(src, dst, RGB2Luv_f(scn, bidx, 0, 0, srgb));
            }
            }
            break;
A
Andrey Kamaev 已提交
4001

4002 4003 4004 4005 4006 4007 4008 4009 4010
        case CV_Lab2BGR: case CV_Lab2RGB: case CV_Lab2LBGR: case CV_Lab2LRGB:
        case CV_Luv2BGR: case CV_Luv2RGB: case CV_Luv2LBGR: case CV_Luv2LRGB:
            {
            if( dcn <= 0 ) dcn = 3;
            CV_Assert( scn == 3 && (dcn == 3 || dcn == 4) && (depth == CV_8U || depth == CV_32F) );
            bidx = code == CV_Lab2BGR || code == CV_Luv2BGR ||
                   code == CV_Lab2LBGR || code == CV_Luv2LBGR ? 0 : 2;
            bool srgb = code == CV_Lab2BGR || code == CV_Lab2RGB ||
                    code == CV_Luv2BGR || code == CV_Luv2RGB;
A
Andrey Kamaev 已提交
4011

4012 4013
            _dst.create(sz, CV_MAKETYPE(depth, dcn));
            dst = _dst.getMat();
A
Andrey Kamaev 已提交
4014

A
Alexander Alekhin 已提交
4015
#if defined(HAVE_IPP) && !defined(HAVE_IPP_ICV_ONLY)
I
Ilya Lavrenov 已提交
4016 4017 4018 4019 4020
#if 0
            if( code == CV_Lab2LBGR && dcn == 3 && depth == CV_8U)
            {
                if( CvtColorIPPLoop(src, dst, IPPGeneralFunctor((ippiGeneralFunc)ippiLabToBGR_8u_C3R)) )
                    return;
I
Ilya Lavrenov 已提交
4021
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
4022 4023 4024 4025 4026 4027
            }
            else if( code == CV_Lab2LBGR && dcn == 4 && depth == CV_8U )
            {
                if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiLabToBGR_8u_C3R,
                                    ippiSwapChannelsC3C4RTab[depth], 0, 1, 2, depth)) )
                    return;
I
Ilya Lavrenov 已提交
4028
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
4029 4030 4031 4032 4033 4034
            }
            if( code == CV_Lab2LRGB && dcn == 3 && depth == CV_8U )
            {
                if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiLabToBGR_8u_C3R,
                                                                           ippiSwapChannelsC3RTab[depth], 2, 1, 0, depth)) )
                    return;
I
Ilya Lavrenov 已提交
4035
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
4036 4037 4038 4039 4040 4041
            }
            else if( code == CV_Lab2LRGB && dcn == 4 && depth == CV_8U )
            {
                if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor((ippiGeneralFunc)ippiLabToBGR_8u_C3R,
                                                                       ippiSwapChannelsC3C4RTab[depth], 2, 1, 0, depth)) )
                    return;
I
Ilya Lavrenov 已提交
4042
                setIppErrorStatus();
I
Ilya Lavrenov 已提交
4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066
            }
            if( code == CV_Luv2LRGB && dcn == 3 )
            {
                if( CvtColorIPPLoop(src, dst, IPPGeneralFunctor(ippiLUVToRGBTab[depth])) )
                    return;
            }
            else if( code == CV_Luv2LRGB && dcn == 4 )
            {
                if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiLUVToRGBTab[depth],
                                                                       ippiSwapChannelsC3C4RTab[depth], 0, 1, 2, depth)) )
                    return;
            }
            if( code == CV_Luv2LBGR && dcn == 3 )
            {
                if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiLUVToRGBTab[depth],
                                                                       ippiSwapChannelsC3RTab[depth], 2, 1, 0, depth)) )
                    return;
            }
            else if( code == CV_Luv2LBGR && dcn == 4 )
            {
                if( CvtColorIPPLoop(src, dst, IPPGeneralReorderFunctor(ippiLUVToRGBTab[depth],
                                                                       ippiSwapChannelsC3C4RTab[depth], 2, 1, 0, depth)) )
                    return;
            }
I
Ilya Lavrenov 已提交
4067
#endif
I
Ilya Lavrenov 已提交
4068 4069
#endif

4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086
            if( code == CV_Lab2BGR || code == CV_Lab2RGB ||
                code == CV_Lab2LBGR || code == CV_Lab2LRGB )
            {
                if( depth == CV_8U )
                    CvtColorLoop(src, dst, Lab2RGB_b(dcn, bidx, 0, 0, srgb));
                else
                    CvtColorLoop(src, dst, Lab2RGB_f(dcn, bidx, 0, 0, srgb));
            }
            else
            {
                if( depth == CV_8U )
                    CvtColorLoop(src, dst, Luv2RGB_b(dcn, bidx, 0, 0, srgb));
                else
                    CvtColorLoop(src, dst, Luv2RGB_f(dcn, bidx, 0, 0, srgb));
            }
            }
            break;
A
Andrey Kamaev 已提交
4087

4088
        case CV_BayerBG2GRAY: case CV_BayerGB2GRAY: case CV_BayerRG2GRAY: case CV_BayerGR2GRAY:
4089 4090
        case CV_BayerBG2BGR: case CV_BayerGB2BGR: case CV_BayerRG2BGR: case CV_BayerGR2BGR:
        case CV_BayerBG2BGR_VNG: case CV_BayerGB2BGR_VNG: case CV_BayerRG2BGR_VNG: case CV_BayerGR2BGR_VNG:
4091 4092
        case CV_BayerBG2BGR_EA: case CV_BayerGB2BGR_EA: case CV_BayerRG2BGR_EA: case CV_BayerGR2BGR_EA:
            demosaicing(src, _dst, code, dcn);
4093
            break;
4094

4095 4096
        case CV_YUV2BGR_NV21:  case CV_YUV2RGB_NV21:  case CV_YUV2BGR_NV12:  case CV_YUV2RGB_NV12:
        case CV_YUV2BGRA_NV21: case CV_YUV2RGBA_NV21: case CV_YUV2BGRA_NV12: case CV_YUV2RGBA_NV12:
4097
            {
4098 4099
                // http://www.fourcc.org/yuv.php#NV21 == yuv420sp -> a plane of 8 bit Y samples followed by an interleaved V/U plane containing 8 bit 2x2 subsampled chroma samples
                // http://www.fourcc.org/yuv.php#NV12 -> a plane of 8 bit Y samples followed by an interleaved U/V plane containing 8 bit 2x2 subsampled colour difference samples
A
Andrey Kamaev 已提交
4100

4101
                if (dcn <= 0) dcn = (code==CV_YUV420sp2BGRA || code==CV_YUV420sp2RGBA || code==CV_YUV2BGRA_NV12 || code==CV_YUV2RGBA_NV12) ? 4 : 3;
A
Andrey Kamaev 已提交
4102 4103 4104
                const int bIdx = (code==CV_YUV2BGR_NV21 || code==CV_YUV2BGRA_NV21 || code==CV_YUV2BGR_NV12 || code==CV_YUV2BGRA_NV12) ? 0 : 2;
                const int uIdx = (code==CV_YUV2BGR_NV21 || code==CV_YUV2BGRA_NV21 || code==CV_YUV2RGB_NV21 || code==CV_YUV2RGBA_NV21) ? 1 : 0;

4105
                CV_Assert( dcn == 3 || dcn == 4 );
4106
                CV_Assert( sz.width % 2 == 0 && sz.height % 3 == 0 && depth == CV_8U );
A
Andrey Kamaev 已提交
4107

4108
                Size dstSz(sz.width, sz.height * 2 / 3);
4109
                _dst.create(dstSz, CV_MAKETYPE(depth, dcn));
4110 4111
                dst = _dst.getMat();

4112
                int srcstep = (int)src.step;
4113 4114
                const uchar* y = src.ptr();
                const uchar* uv = y + srcstep * dstSz.height;
A
Andrey Kamaev 已提交
4115 4116

                switch(dcn*100 + bIdx * 10 + uIdx)
4117
                {
4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134
                    case 300: cvtYUV420sp2RGB<0, 0> (dst, srcstep, y, uv); break;
                    case 301: cvtYUV420sp2RGB<0, 1> (dst, srcstep, y, uv); break;
                    case 320: cvtYUV420sp2RGB<2, 0> (dst, srcstep, y, uv); break;
                    case 321: cvtYUV420sp2RGB<2, 1> (dst, srcstep, y, uv); break;
                    case 400: cvtYUV420sp2RGBA<0, 0>(dst, srcstep, y, uv); break;
                    case 401: cvtYUV420sp2RGBA<0, 1>(dst, srcstep, y, uv); break;
                    case 420: cvtYUV420sp2RGBA<2, 0>(dst, srcstep, y, uv); break;
                    case 421: cvtYUV420sp2RGBA<2, 1>(dst, srcstep, y, uv); break;
                    default: CV_Error( CV_StsBadFlag, "Unknown/unsupported color conversion code" ); break;
                };
            }
            break;
        case CV_YUV2BGR_YV12: case CV_YUV2RGB_YV12: case CV_YUV2BGRA_YV12: case CV_YUV2RGBA_YV12:
        case CV_YUV2BGR_IYUV: case CV_YUV2RGB_IYUV: case CV_YUV2BGRA_IYUV: case CV_YUV2RGBA_IYUV:
            {
                //http://www.fourcc.org/yuv.php#YV12 == yuv420p -> It comprises an NxM Y plane followed by (N/2)x(M/2) V and U planes.
                //http://www.fourcc.org/yuv.php#IYUV == I420 -> It comprises an NxN Y plane followed by (N/2)x(N/2) U and V planes
A
Andrey Kamaev 已提交
4135

4136
                if (dcn <= 0) dcn = (code==CV_YUV2BGRA_YV12 || code==CV_YUV2RGBA_YV12 || code==CV_YUV2RGBA_IYUV || code==CV_YUV2BGRA_IYUV) ? 4 : 3;
A
Andrey Kamaev 已提交
4137 4138 4139
                const int bIdx = (code==CV_YUV2BGR_YV12 || code==CV_YUV2BGRA_YV12 || code==CV_YUV2BGR_IYUV || code==CV_YUV2BGRA_IYUV) ? 0 : 2;
                const int uIdx  = (code==CV_YUV2BGR_YV12 || code==CV_YUV2RGB_YV12 || code==CV_YUV2BGRA_YV12 || code==CV_YUV2RGBA_YV12) ? 1 : 0;

4140 4141 4142 4143 4144 4145
                CV_Assert( dcn == 3 || dcn == 4 );
                CV_Assert( sz.width % 2 == 0 && sz.height % 3 == 0 && depth == CV_8U );

                Size dstSz(sz.width, sz.height * 2 / 3);
                _dst.create(dstSz, CV_MAKETYPE(depth, dcn));
                dst = _dst.getMat();
A
Andrey Kamaev 已提交
4146

4147 4148 4149
                int srcstep = (int)src.step;
                const uchar* y = src.ptr();
                const uchar* u = y + srcstep * dstSz.height;
4150
                const uchar* v = y + srcstep * (dstSz.height + dstSz.height/4) + (dstSz.width/2) * ((dstSz.height % 4)/2);
A
Andrey Kamaev 已提交
4151

4152 4153
                int ustepIdx = 0;
                int vstepIdx = dstSz.height % 4 == 2 ? 1 : 0;
A
Andrey Kamaev 已提交
4154

4155
                if(uIdx == 1) { std::swap(u ,v), std::swap(ustepIdx, vstepIdx); }
A
Andrey Kamaev 已提交
4156 4157

                switch(dcn*10 + bIdx)
4158
                {
4159 4160 4161 4162
                    case 30: cvtYUV420p2RGB<0>(dst, srcstep, y, u, v, ustepIdx, vstepIdx); break;
                    case 32: cvtYUV420p2RGB<2>(dst, srcstep, y, u, v, ustepIdx, vstepIdx); break;
                    case 40: cvtYUV420p2RGBA<0>(dst, srcstep, y, u, v, ustepIdx, vstepIdx); break;
                    case 42: cvtYUV420p2RGBA<2>(dst, srcstep, y, u, v, ustepIdx, vstepIdx); break;
4163 4164
                    default: CV_Error( CV_StsBadFlag, "Unknown/unsupported color conversion code" ); break;
                };
4165 4166
            }
            break;
4167 4168 4169
        case CV_YUV2GRAY_420:
            {
                if (dcn <= 0) dcn = 1;
A
Andrey Kamaev 已提交
4170

4171 4172 4173 4174 4175 4176
                CV_Assert( dcn == 1 );
                CV_Assert( sz.width % 2 == 0 && sz.height % 3 == 0 && depth == CV_8U );

                Size dstSz(sz.width, sz.height * 2 / 3);
                _dst.create(dstSz, CV_MAKETYPE(depth, dcn));
                dst = _dst.getMat();
A
Andrey Kamaev 已提交
4177

4178 4179 4180
                src(Range(0, dstSz.height), Range::all()).copyTo(dst);
            }
            break;
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205
        case CV_RGB2YUV_YV12: case CV_BGR2YUV_YV12: case CV_RGBA2YUV_YV12: case CV_BGRA2YUV_YV12:
        case CV_RGB2YUV_IYUV: case CV_BGR2YUV_IYUV: case CV_RGBA2YUV_IYUV: case CV_BGRA2YUV_IYUV:
            {
                if (dcn <= 0) dcn = 1;
                const int bIdx = (code == CV_BGR2YUV_IYUV || code == CV_BGRA2YUV_IYUV || code == CV_BGR2YUV_YV12 || code == CV_BGRA2YUV_YV12) ? 0 : 2;
                const int uIdx = (code == CV_BGR2YUV_IYUV || code == CV_BGRA2YUV_IYUV || code == CV_RGB2YUV_IYUV || code == CV_RGBA2YUV_IYUV) ? 1 : 2;

                CV_Assert( (scn == 3 || scn == 4) && depth == CV_8U );
                CV_Assert( dcn == 1 );
                CV_Assert( sz.width % 2 == 0 && sz.height % 2 == 0 );

                Size dstSz(sz.width, sz.height / 2 * 3);
                _dst.create(dstSz, CV_MAKETYPE(depth, dcn));
                dst = _dst.getMat();

                switch(bIdx + uIdx*10)
                {
                    case 10: cvtRGBtoYUV420p<0, 1>(src, dst); break;
                    case 12: cvtRGBtoYUV420p<2, 1>(src, dst); break;
                    case 20: cvtRGBtoYUV420p<0, 2>(src, dst); break;
                    case 22: cvtRGBtoYUV420p<2, 2>(src, dst); break;
                    default: CV_Error( CV_StsBadFlag, "Unknown/unsupported color conversion code" ); break;
                };
            }
            break;
4206 4207 4208 4209 4210 4211
        case CV_YUV2RGB_UYVY: case CV_YUV2BGR_UYVY: case CV_YUV2RGBA_UYVY: case CV_YUV2BGRA_UYVY:
        case CV_YUV2RGB_YUY2: case CV_YUV2BGR_YUY2: case CV_YUV2RGB_YVYU: case CV_YUV2BGR_YVYU:
        case CV_YUV2RGBA_YUY2: case CV_YUV2BGRA_YUY2: case CV_YUV2RGBA_YVYU: case CV_YUV2BGRA_YVYU:
            {
                //http://www.fourcc.org/yuv.php#UYVY
                //http://www.fourcc.org/yuv.php#YUY2
A
Andrey Kamaev 已提交
4212 4213
                //http://www.fourcc.org/yuv.php#YVYU

4214
                if (dcn <= 0) dcn = (code==CV_YUV2RGBA_UYVY || code==CV_YUV2BGRA_UYVY || code==CV_YUV2RGBA_YUY2 || code==CV_YUV2BGRA_YUY2 || code==CV_YUV2RGBA_YVYU || code==CV_YUV2BGRA_YVYU) ? 4 : 3;
A
Andrey Kamaev 已提交
4215 4216 4217 4218
                const int bIdx = (code==CV_YUV2BGR_UYVY || code==CV_YUV2BGRA_UYVY || code==CV_YUV2BGR_YUY2 || code==CV_YUV2BGRA_YUY2 || code==CV_YUV2BGR_YVYU || code==CV_YUV2BGRA_YVYU) ? 0 : 2;
                const int ycn  = (code==CV_YUV2RGB_UYVY || code==CV_YUV2BGR_UYVY || code==CV_YUV2RGBA_UYVY || code==CV_YUV2BGRA_UYVY) ? 1 : 0;
                const int uIdx = (code==CV_YUV2RGB_YVYU || code==CV_YUV2BGR_YVYU || code==CV_YUV2RGBA_YVYU || code==CV_YUV2BGRA_YVYU) ? 1 : 0;

4219 4220 4221 4222 4223
                CV_Assert( dcn == 3 || dcn == 4 );
                CV_Assert( scn == 2 && depth == CV_8U );

                _dst.create(sz, CV_8UC(dcn));
                dst = _dst.getMat();
A
Andrey Kamaev 已提交
4224 4225

                switch(dcn*1000 + bIdx*100 + uIdx*10 + ycn)
4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249
                {
                    case 3000: cvtYUV422toRGB<0,0,0>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 3001: cvtYUV422toRGB<0,0,1>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 3010: cvtYUV422toRGB<0,1,0>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 3011: cvtYUV422toRGB<0,1,1>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 3200: cvtYUV422toRGB<2,0,0>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 3201: cvtYUV422toRGB<2,0,1>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 3210: cvtYUV422toRGB<2,1,0>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 3211: cvtYUV422toRGB<2,1,1>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 4000: cvtYUV422toRGBA<0,0,0>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 4001: cvtYUV422toRGBA<0,0,1>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 4010: cvtYUV422toRGBA<0,1,0>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 4011: cvtYUV422toRGBA<0,1,1>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 4200: cvtYUV422toRGBA<2,0,0>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 4201: cvtYUV422toRGBA<2,0,1>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 4210: cvtYUV422toRGBA<2,1,0>(dst, (int)src.step, src.ptr<uchar>()); break;
                    case 4211: cvtYUV422toRGBA<2,1,1>(dst, (int)src.step, src.ptr<uchar>()); break;
                    default: CV_Error( CV_StsBadFlag, "Unknown/unsupported color conversion code" ); break;
                };
            }
            break;
        case CV_YUV2GRAY_UYVY: case CV_YUV2GRAY_YUY2:
            {
                if (dcn <= 0) dcn = 1;
A
Andrey Kamaev 已提交
4250

4251 4252 4253 4254 4255
                CV_Assert( dcn == 1 );
                CV_Assert( scn == 2 && depth == CV_8U );

                extractChannel(_src, _dst, code == CV_YUV2GRAY_UYVY ? 1 : 0);
            }
4256
            break;
4257 4258 4259 4260 4261 4262 4263 4264 4265
        case CV_RGBA2mRGBA:
            {
                if (dcn <= 0) dcn = 4;
                CV_Assert( scn == 4 && dcn == 4 );

                _dst.create(sz, CV_MAKETYPE(depth, dcn));
                dst = _dst.getMat();

                if( depth == CV_8U )
I
Ilya Lavrenov 已提交
4266
                {
A
Alexander Alekhin 已提交
4267
#if defined(HAVE_IPP) && !defined(HAVE_IPP_ICV_ONLY)
I
Ilya Lavrenov 已提交
4268 4269
                    if (CvtColorIPPLoop(src, dst, IPPGeneralFunctor((ippiGeneralFunc)ippiAlphaPremul_8u_AC4R)))
                        return;
I
Ilya Lavrenov 已提交
4270
                    setIppErrorStatus();
I
Ilya Lavrenov 已提交
4271
#endif
4272
                    CvtColorLoop(src, dst, RGBA2mRGBA<uchar>());
I
Ilya Lavrenov 已提交
4273
                }
4274 4275
                else
                {
4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289
                    CV_Error( CV_StsBadArg, "Unsupported image depth" );
                }
            }
            break;
        case CV_mRGBA2RGBA:
            {
                if (dcn <= 0) dcn = 4;
                CV_Assert( scn == 4 && dcn == 4 );

                _dst.create(sz, CV_MAKETYPE(depth, dcn));
                dst = _dst.getMat();

                if( depth == CV_8U )
                    CvtColorLoop(src, dst, mRGBA2RGBA<uchar>());
4290 4291
                else
                {
4292 4293 4294 4295
                    CV_Error( CV_StsBadArg, "Unsupported image depth" );
                }
            }
            break;
4296 4297
        default:
            CV_Error( CV_StsBadFlag, "Unknown/unsupported color conversion code" );
4298 4299
    }
}
A
Andrey Kamaev 已提交
4300

4301 4302
CV_IMPL void
cvCvtColor( const CvArr* srcarr, CvArr* dstarr, int code )
4303
{
4304 4305
    cv::Mat src = cv::cvarrToMat(srcarr), dst0 = cv::cvarrToMat(dstarr), dst = dst0;
    CV_Assert( src.depth() == dst.depth() );
A
Andrey Kamaev 已提交
4306

4307 4308
    cv::cvtColor(src, dst, code, dst.channels());
    CV_Assert( dst.data == dst0.data );
4309 4310
}

4311

4312
/* End of file. */