color.cu 99.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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
/*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.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// 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.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     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*/

#include "cuda_shared.hpp"
#include "saturate_cast.hpp"

using namespace cv::gpu;

#ifndef CV_DESCALE
#define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
#endif

namespace imgproc
A
Andrey Morozov 已提交
53
{
54 55 56 57 58 59 60 61 62 63 64 65 66
    template<typename _Tp> struct ColorChannel
    {
    };

    template<> struct ColorChannel<uchar>
    {
        typedef float worktype_f;
        typedef uchar3 vec3_t;
        typedef uchar4 vec4_t;
        static __device__ unsigned char max() { return UCHAR_MAX; }
        static __device__ unsigned char half() { return (unsigned char)(max()/2 + 1); }
    };

A
Andrey Morozov 已提交
67
    template<> struct ColorChannel<unsigned short>
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    {
        typedef float worktype_f;
        typedef ushort3 vec3_t;
        typedef ushort4 vec4_t;
        static __device__ unsigned short max() { return USHRT_MAX; }
        static __device__ unsigned short half() { return (unsigned short)(max()/2 + 1); }
    };

    template<> struct ColorChannel<float>
    {
        typedef float worktype_f;
        typedef float3 vec3_t;
        typedef float4 vec4_t;
        static __device__ float max() { return 1.f; }
        static __device__ float half() { return 0.5f; }
    };
}

////////////////// Various 3/4-channel to 3/4-channel RGB transformations /////////////////

namespace imgproc
{
    template <typename T>
A
Andrey Morozov 已提交
91 92 93 94
        __global__ void RGB2RGB_3_3(const T* src_, size_t src_step, T* dst_, size_t dst_step, int rows, int cols, int bidx)
        {
                const int x = blockDim.x * blockIdx.x + threadIdx.x;
                const int y = blockDim.y * blockIdx.y + threadIdx.y;
95 96 97 98 99

        if (y < rows && x < cols)
        {
            const T* src = src_ + y * src_step + x * 3;
            T* dst = dst_ + y * dst_step + x * 3;
A
Andrey Morozov 已提交
100

101 102 103
            T t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];
            dst[0] = t0; dst[1] = t1; dst[2] = t2;
        }
A
Andrey Morozov 已提交
104
        }
105 106

    template <typename T>
A
Andrey Morozov 已提交
107 108
        __global__ void RGB2RGB_4_3(const T* src_, size_t src_step, T* dst_, size_t dst_step, int rows, int cols, int bidx)
        {
109 110 111 112 113 114 115 116 117
        typedef typename ColorChannel<T>::vec4_t vec4_t;

		const int x = blockDim.x * blockIdx.x + threadIdx.x;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            vec4_t src = *(vec4_t*)(src_ + y * src_step + (x << 2));
            T* dst = dst_ + y * dst_step + x * 3;
A
Andrey Morozov 已提交
118

119 120 121
            T t0 = ((T*)(&src))[bidx], t1 = src.y, t2 = ((T*)(&src))[bidx ^ 2];
            dst[0] = t0; dst[1] = t1; dst[2] = t2;
        }
A
Andrey Morozov 已提交
122
        }
123 124

    template <typename T>
A
Andrey Morozov 已提交
125 126
        __global__ void RGB2RGB_3_4(const T* src_, size_t src_step, T* dst_, size_t dst_step, int rows, int cols, int bidx)
        {
127 128 129 130 131 132 133 134 135 136
        typedef typename ColorChannel<T>::vec4_t vec4_t;

		const int x = blockDim.x * blockIdx.x + threadIdx.x;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            const T* src = src_ + y * src_step + x * 3;

            vec4_t dst;
A
Andrey Morozov 已提交
137

138 139 140 141 142 143
            dst.x = src[bidx];
            dst.y = src[1];
            dst.z = src[bidx ^ 2];
            dst.w = ColorChannel<T>::max();
            *(vec4_t*)(dst_ + y * dst_step + (x << 2)) = dst;
        }
A
Andrey Morozov 已提交
144
        }
145 146

    template <typename T>
A
Andrey Morozov 已提交
147 148
        __global__ void RGB2RGB_4_4(const T* src_, size_t src_step, T* dst_, size_t dst_step, int rows, int cols, int bidx)
        {
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        typedef typename ColorChannel<T>::vec4_t vec4_t;

		const int x = blockDim.x * blockIdx.x + threadIdx.x;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            vec4_t src = *(const vec4_t*)(src_ + y * src_step + (x << 2));
            vec4_t dst;

            dst.x = ((T*)(&src))[bidx];
            dst.y = src.y;
            dst.z = ((T*)(&src))[bidx ^ 2];
            dst.w = src.w;

            *(vec4_t*)(dst_ + y * dst_step + (x << 2)) = dst;
        }
A
Andrey Morozov 已提交
166
        }
167 168
}

169
namespace cv { namespace gpu { namespace improc
170 171 172
{
    template <typename T>
    void RGB2RGB_caller(const DevMem2D_<T>& src, int srccn, const DevMem2D_<T>& dst, int dstcn, int bidx, cudaStream_t stream)
A
Andrey Morozov 已提交
173
    {
174 175 176 177 178 179 180 181
        dim3 threads(32, 8, 1);
        dim3 grid(1, 1, 1);

        grid.x = divUp(src.cols, threads.x);
        grid.y = divUp(src.rows, threads.y);

        switch (dstcn)
        {
A
Andrey Morozov 已提交
182
        case 3:
183 184 185
            switch (srccn)
            {
            case 3:
A
Andrey Morozov 已提交
186
                imgproc::RGB2RGB_3_3<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(T), dst.ptr, dst.step / sizeof(T),
187 188 189
                                                                          src.rows, src.cols, bidx);
                break;
            case 4:
A
Andrey Morozov 已提交
190
                imgproc::RGB2RGB_4_3<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(T), dst.ptr, dst.step / sizeof(T),
191 192 193 194 195 196 197
                                                                          src.rows, src.cols, bidx);
                break;
            default:
                cv::gpu::error("Unsupported channels count", __FILE__, __LINE__);
                break;
            }
            break;
A
Andrey Morozov 已提交
198
        case 4:
199 200 201
            switch (srccn)
            {
            case 3:
A
Andrey Morozov 已提交
202
                imgproc::RGB2RGB_3_4<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(T), dst.ptr, dst.step / sizeof(T),
203 204 205
                                                                          src.rows, src.cols, bidx);
                break;
            case 4:
A
Andrey Morozov 已提交
206
                imgproc::RGB2RGB_4_4<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(T), dst.ptr, dst.step / sizeof(T),
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
                                                                          src.rows, src.cols, bidx);
                break;
            default:
                cv::gpu::error("Unsupported channels count", __FILE__, __LINE__);
                break;
            }
            break;
        default:
            cv::gpu::error("Unsupported channels count", __FILE__, __LINE__);
            break;
        }

        if (stream == 0)
            cudaSafeCall( cudaThreadSynchronize() );
    }

    void RGB2RGB_gpu(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, cudaStream_t stream)
    {
        RGB2RGB_caller(src, srccn, dst, dstcn, bidx, stream);
    }

A
Andrey Morozov 已提交
228
    void RGB2RGB_gpu(const DevMem2D_<unsigned short>& src, int srccn, const DevMem2D_<unsigned short>& dst, int dstcn, int bidx, cudaStream_t stream)
229 230 231 232 233 234 235 236 237
    {
        RGB2RGB_caller(src, srccn, dst, dstcn, bidx, stream);
    }

    void RGB2RGB_gpu(const DevMem2Df& src, int srccn, const DevMem2Df& dst, int dstcn, int bidx, cudaStream_t stream)
    {
        RGB2RGB_caller(src, srccn, dst, dstcn, bidx, stream);
    }
}}}
A
Andrey Morozov 已提交
238

239 240 241 242 243 244 245
/////////// Transforming 16-bit (565 or 555) RGB to/from 24/32-bit (888[8]) RGB //////////

//namespace imgproc
//{
//    struct RGB5x52RGB
//    {
//        typedef uchar channel_type;
A
Andrey Morozov 已提交
246
//
247 248
//        RGB5x52RGB(int _dstcn, int _blueIdx, int _greenBits)
//		    : dstcn(_dstcn), blueIdx(_blueIdx), greenBits(_greenBits) {}
A
Andrey Morozov 已提交
249
//
250 251 252 253 254 255
//        void operator()(const uchar* src, uchar* dst, int n) const
//        {
//            int dcn = dstcn, bidx = blueIdx;
//            if( greenBits == 6 )
//                for( int i = 0; i < n; i++, dst += dcn )
//                {
A
Andrey Morozov 已提交
256
//                    unsigned t = ((const unsigned short*)src)[i];
257 258 259 260 261 262 263 264 265
//                    dst[bidx] = (uchar)(t << 3);
//                    dst[1] = (uchar)((t >> 3) & ~3);
//                    dst[bidx ^ 2] = (uchar)((t >> 8) & ~7);
//                    if( dcn == 4 )
//                        dst[3] = 255;
//                }
//            else
//                for( int i = 0; i < n; i++, dst += dcn )
//                {
A
Andrey Morozov 已提交
266
//                    unsigned t = ((const unsigned short*)src)[i];
267 268 269 270 271 272 273
//                    dst[bidx] = (uchar)(t << 3);
//                    dst[1] = (uchar)((t >> 2) & ~7);
//                    dst[bidx ^ 2] = (uchar)((t >> 7) & ~7);
//                    if( dcn == 4 )
//                        dst[3] = t & 0x8000 ? 255 : 0;
//                }
//        }
A
Andrey Morozov 已提交
274
//
275 276 277
//        int dstcn, blueIdx, greenBits;
//    };
//
A
Andrey Morozov 已提交
278
//
279 280 281
//    struct RGB2RGB5x5
//    {
//        typedef uchar channel_type;
A
Andrey Morozov 已提交
282
//
283 284
//        RGB2RGB5x5(int _srccn, int _blueIdx, int _greenBits)
//		    : srccn(_srccn), blueIdx(_blueIdx), greenBits(_greenBits) {}
A
Andrey Morozov 已提交
285
//
286 287 288 289 290 291
//        void operator()(const uchar* src, uchar* dst, int n) const
//        {
//            int scn = srccn, bidx = blueIdx;
//            if( greenBits == 6 )
//                for( int i = 0; i < n; i++, src += scn )
//                {
A
Andrey Morozov 已提交
292
//                    ((unsigned short*)dst)[i] = (unsigned short)((src[bidx] >> 3)|((src[1]&~3) << 3)|((src[bidx^2]&~7) << 8));
293 294 295 296
//                }
//            else if( scn == 3 )
//                for( int i = 0; i < n; i++, src += 3 )
//                {
A
Andrey Morozov 已提交
297
//                    ((unsigned short*)dst)[i] = (unsigned short)((src[bidx] >> 3)|((src[1]&~7) << 2)|((src[bidx^2]&~7) << 7));
298 299 300 301
//                }
//            else
//                for( int i = 0; i < n; i++, src += 4 )
//                {
A
Andrey Morozov 已提交
302
//                    ((unsigned short*)dst)[i] = (unsigned short)((src[bidx] >> 3)|((src[1]&~7) << 2)|
303 304 305
//                        ((src[bidx^2]&~7) << 7)|(src[3] ? 0x8000 : 0));
//                }
//        }
A
Andrey Morozov 已提交
306
//
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
//        int srccn, blueIdx, greenBits;
//    };
//}
//
//namespace cv { namespace gpu { namespace impl
//{
//}}}

///////////////////////////////// Grayscale to Color ////////////////////////////////

namespace imgproc
{
    template <typename T>
    __global__ void Gray2RGB_3(const T* src_, size_t src_step, T* dst_, size_t dst_step, int rows, int cols)
    {
A
Andrey Morozov 已提交
322 323
                const int x = blockDim.x * blockIdx.x + threadIdx.x;
                const int y = blockDim.y * blockIdx.y + threadIdx.y;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

        if (y < rows && x < cols)
        {
            T src = src_[y * src_step + x];
            T* dst = dst_ + y * dst_step + x * 3;
            dst[0] = src;
            dst[1] = src;
            dst[2] = src;
        }
    }

    template <typename T>
    __global__ void Gray2RGB_4(const T* src_, size_t src_step, T* dst_, size_t dst_step, int rows, int cols)
    {
        typedef typename ColorChannel<T>::vec4_t vec4_t;

		const int x = blockDim.x * blockIdx.x + threadIdx.x;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            T src = src_[y * src_step + x];
            vec4_t dst;
            dst.x = src;
            dst.y = src;
            dst.z = src;
            dst.w = ColorChannel<T>::max();
            *(vec4_t*)(dst_ + y * dst_step + (x << 2)) = dst;
        }
    }

    //struct Gray2RGB5x5
    //{
    //    typedef uchar channel_type;
A
Andrey Morozov 已提交
358
    //
359 360 361 362 363 364 365
    //    Gray2RGB5x5(int _greenBits) : greenBits(_greenBits) {}
    //    void operator()(const uchar* src, uchar* dst, int n) const
    //    {
    //        if( greenBits == 6 )
    //            for( int i = 0; i < n; i++ )
    //            {
    //                int t = src[i];
A
Andrey Morozov 已提交
366
    //                ((unsigned short*)dst)[i] = (unsigned short)((t >> 3)|((t & ~3) << 3)|((t & ~7) << 8));
367 368 369 370 371
    //            }
    //        else
    //            for( int i = 0; i < n; i++ )
    //            {
    //                int t = src[i] >> 3;
A
Andrey Morozov 已提交
372
    //                ((unsigned short*)dst)[i] = (unsigned short)(t|(t << 5)|(t << 10));
373 374 375 376 377 378
    //            }
    //    }
    //    int greenBits;
    //};
}

379
namespace cv { namespace gpu { namespace improc
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
{
    template <typename T>
    void Gray2RGB_caller(const DevMem2D_<T>& src, const DevMem2D_<T>& dst, int dstcn, cudaStream_t stream)
    {
        dim3 threads(32, 8, 1);
        dim3 grid(1, 1, 1);

        grid.x = divUp(src.cols, threads.x);
        grid.y = divUp(src.rows, threads.y);

        switch (dstcn)
        {
        case 3:
            imgproc::Gray2RGB_3<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(T), dst.ptr, dst.step / sizeof(T), src.rows, src.cols);
            break;
        case 4:
            imgproc::Gray2RGB_4<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(T), dst.ptr, dst.step / sizeof(T), src.rows, src.cols);
            break;
        default:
            cv::gpu::error("Unsupported channels count", __FILE__, __LINE__);
            break;
        }

        if (stream == 0)
            cudaSafeCall( cudaThreadSynchronize() );
    }

    void Gray2RGB_gpu(const DevMem2D& src, const DevMem2D& dst, int dstcn, cudaStream_t stream)
    {
        Gray2RGB_caller(src, dst, dstcn, stream);
    }

A
Andrey Morozov 已提交
412
    void Gray2RGB_gpu(const DevMem2D_<unsigned short>& src, const DevMem2D_<unsigned short>& dst, int dstcn, cudaStream_t stream)
413 414 415 416 417 418 419 420 421
    {
        Gray2RGB_caller(src, dst, dstcn, stream);
    }

    void Gray2RGB_gpu(const DevMem2Df& src, const DevMem2Df& dst, int dstcn, cudaStream_t stream)
    {
        Gray2RGB_caller(src, dst, dstcn, stream);
    }
}}}
A
Andrey Morozov 已提交
422

423 424 425 426 427 428 429
///////////////////////////////// Color to Grayscale ////////////////////////////////

namespace imgproc
{
    //#undef R2Y
    //#undef G2Y
    //#undef B2Y
A
Andrey Morozov 已提交
430
    //
431 432 433 434 435 436 437 438 439 440 441 442 443
    //enum
    //{
    //    yuv_shift = 14,
    //    xyz_shift = 12,
    //    R2Y = 4899,
    //    G2Y = 9617,
    //    B2Y = 1868,
    //    BLOCK_SIZE = 256
    //};

    //struct RGB5x52Gray
    //{
    //    typedef uchar channel_type;
A
Andrey Morozov 已提交
444
    //
445 446 447 448 449 450
    //    RGB5x52Gray(int _greenBits) : greenBits(_greenBits) {}
    //    void operator()(const uchar* src, uchar* dst, int n) const
    //    {
    //        if( greenBits == 6 )
    //            for( int i = 0; i < n; i++ )
    //            {
A
Andrey Morozov 已提交
451
    //                int t = ((unsigned short*)src)[i];
452 453 454 455 456 457 458
    //                dst[i] = (uchar)CV_DESCALE(((t << 3) & 0xf8)*B2Y +
    //                                           ((t >> 3) & 0xfc)*G2Y +
    //                                           ((t >> 8) & 0xf8)*R2Y, yuv_shift);
    //            }
    //        else
    //            for( int i = 0; i < n; i++ )
    //            {
A
Andrey Morozov 已提交
459
    //                int t = ((unsigned short*)src)[i];
460 461 462 463 464 465 466 467 468 469 470 471 472 473
    //                dst[i] = (uchar)CV_DESCALE(((t << 3) & 0xf8)*B2Y +
    //                                           ((t >> 2) & 0xf8)*G2Y +
    //                                           ((t >> 7) & 0xf8)*R2Y, yuv_shift);
    //            }
    //    }
    //    int greenBits;
    //};

    __global__ void RGB2Gray_3(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        const int cr = 4899;
        const int cg = 9617;
        const int cb = 1868;
        const int yuv_shift = 14;
A
Andrey Morozov 已提交
474

475 476 477 478 479 480
		const int x = (blockDim.x * blockIdx.x + threadIdx.x) << 2;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            const uchar* src = src_ + y * src_step + x * 3;
A
Andrey Morozov 已提交
481

482 483 484 485 486
            uchar t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];

            uchar4 dst;
            dst.x = (uchar)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);

A
Andrey Morozov 已提交
487
            src += 3;
488 489 490
            t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];
            dst.y = (uchar)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);

A
Andrey Morozov 已提交
491
            src += 3;
492 493 494
            t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];
            dst.z = (uchar)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);

A
Andrey Morozov 已提交
495
            src += 3;
496 497 498 499 500 501 502
            t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];
            dst.w = (uchar)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);

            *(uchar4*)(dst_ + y * dst_step + x) = dst;
        }
    }

A
Andrey Morozov 已提交
503
    __global__ void RGB2Gray_3(const unsigned short* src_, size_t src_step, unsigned short* dst_, size_t dst_step, int rows, int cols, int bidx)
504 505 506 507 508
    {
        const int cr = 4899;
        const int cg = 9617;
        const int cb = 1868;
        const int yuv_shift = 14;
A
Andrey Morozov 已提交
509

510 511 512 513 514
		const int x = (blockDim.x * blockIdx.x + threadIdx.x) << 1;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
A
Andrey Morozov 已提交
515 516 517
            const unsigned short* src = src_ + y * src_step + x * 3;

            unsigned short t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];
518 519

            ushort2 dst;
A
Andrey Morozov 已提交
520
            dst.x = (unsigned short)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);
521

A
Andrey Morozov 已提交
522
            src += 3;
523
            t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];
A
Andrey Morozov 已提交
524
            dst.y = (unsigned short)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);
525 526 527 528 529 530 531 532 533 534

            *(ushort2*)(dst_ + y * dst_step + x) = dst;
        }
    }

    __global__ void RGB2Gray_3(const float* src_, size_t src_step, float* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        const float cr = 0.299f;
        const float cg = 0.587f;
        const float cb = 0.114f;
A
Andrey Morozov 已提交
535

536 537 538 539 540 541
		const int x = blockDim.x * blockIdx.x + threadIdx.x;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            const float* src = src_ + y * src_step + x * 3;
A
Andrey Morozov 已提交
542

543 544 545 546 547 548 549 550 551 552 553
            float t0 = src[bidx], t1 = src[1], t2 = src[bidx ^ 2];
            *(dst_ + y * dst_step + x) = t0 * cb + t1 * cg + t2 * cr;
        }
    }

    __global__ void RGB2Gray_4(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        const int cr = 4899;
        const int cg = 9617;
        const int cb = 1868;
        const int yuv_shift = 14;
A
Andrey Morozov 已提交
554

555 556 557 558 559 560
		const int x = (blockDim.x * blockIdx.x + threadIdx.x) << 2;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            uchar4 src = *(uchar4*)(src_ + y * src_step + (x << 2));
A
Andrey Morozov 已提交
561

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
            uchar t0 = ((uchar*)(&src))[bidx], t1 = src.y, t2 = ((uchar*)(&src))[bidx ^ 2];

            uchar4 dst;
            dst.x = (uchar)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);

            src = *(uchar4*)(src_ + y * src_step + (x << 2) + 4);
            t0 = ((uchar*)(&src))[bidx], t1 = src.y, t2 = ((uchar*)(&src))[bidx ^ 2];
            dst.y = (uchar)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);

            src = *(uchar4*)(src_ + y * src_step + (x << 2) + 8);
            t0 = ((uchar*)(&src))[bidx], t1 = src.y, t2 = ((uchar*)(&src))[bidx ^ 2];
            dst.z = (uchar)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);

            src = *(uchar4*)(src_ + y * src_step + (x << 2) + 12);
            t0 = ((uchar*)(&src))[bidx], t1 = src.y, t2 = ((uchar*)(&src))[bidx ^ 2];
            dst.w = (uchar)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);

            *(uchar4*)(dst_ + y * dst_step + x) = dst;
        }
    }

A
Andrey Morozov 已提交
583
    __global__ void RGB2Gray_4(const unsigned short* src_, size_t src_step, unsigned short* dst_, size_t dst_step, int rows, int cols, int bidx)
584 585 586 587 588
    {
        const int cr = 4899;
        const int cg = 9617;
        const int cb = 1868;
        const int yuv_shift = 14;
A
Andrey Morozov 已提交
589

590 591 592 593 594 595
		const int x = (blockDim.x * blockIdx.x + threadIdx.x) << 1;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            ushort4 src = *(ushort4*)(src_ + y * src_step + (x << 2));
A
Andrey Morozov 已提交
596 597

            unsigned short t0 = ((unsigned short*)(&src))[bidx], t1 = src.y, t2 = ((unsigned short*)(&src))[bidx ^ 2];
598 599

            ushort2 dst;
A
Andrey Morozov 已提交
600
            dst.x = (unsigned short)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);
601 602

            src = *(ushort4*)(src_ + y * src_step + (x << 2) + 4);
A
Andrey Morozov 已提交
603 604
            t0 = ((unsigned short*)(&src))[bidx], t1 = src.y, t2 = ((unsigned short*)(&src))[bidx ^ 2];
            dst.y = (unsigned short)CV_DESCALE((unsigned)(t0 * cb + t1 * cg + t2 * cr), yuv_shift);
605 606 607 608 609 610 611 612 613 614

            *(ushort2*)(dst_ + y * dst_step + x) = dst;
        }
    }

    __global__ void RGB2Gray_4(const float* src_, size_t src_step, float* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        const float cr = 0.299f;
        const float cg = 0.587f;
        const float cb = 0.114f;
A
Andrey Morozov 已提交
615

616 617 618 619 620 621
		const int x = blockDim.x * blockIdx.x + threadIdx.x;
		const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
            float4 src = *(float4*)(src_ + y * src_step + (x << 2));
A
Andrey Morozov 已提交
622

623 624 625 626 627 628
            float t0 = ((float*)(&src))[bidx], t1 = src.y, t2 = ((float*)(&src))[bidx ^ 2];
            *(dst_ + y * dst_step + x) = t0 * cb + t1 * cg + t2 * cr;
        }
    }
}

629
namespace cv { namespace gpu { namespace improc
A
Andrey Morozov 已提交
630
{
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    void RGB2Gray_gpu(const DevMem2D& src, int srccn, const DevMem2D& dst, int bidx, cudaStream_t stream)
    {
        dim3 threads(32, 8, 1);
        dim3 grid(1, 1, 1);

        grid.x = divUp(src.cols, threads.x << 2);
        grid.y = divUp(src.rows, threads.y);

        switch (srccn)
        {
        case 3:
            imgproc::RGB2Gray_3<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(uchar), dst.ptr, dst.step / sizeof(uchar), src.rows, src.cols, bidx);
            break;
        case 4:
            imgproc::RGB2Gray_4<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(uchar), dst.ptr, dst.step / sizeof(uchar), src.rows, src.cols, bidx);
            break;
        default:
            cv::gpu::error("Unsupported channels count", __FILE__, __LINE__);
            break;
        }

        if (stream == 0)
            cudaSafeCall( cudaThreadSynchronize() );
    }

A
Andrey Morozov 已提交
656
    void RGB2Gray_gpu(const DevMem2D_<unsigned short>& src, int srccn, const DevMem2D_<unsigned short>& dst, int bidx, cudaStream_t stream)
657 658 659 660 661 662 663 664 665 666
    {
        dim3 threads(32, 8, 1);
        dim3 grid(1, 1, 1);

        grid.x = divUp(src.cols, threads.x << 1);
        grid.y = divUp(src.rows, threads.y);

        switch (srccn)
        {
        case 3:
A
Andrey Morozov 已提交
667
            imgproc::RGB2Gray_3<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(unsigned short), dst.ptr, dst.step / sizeof(unsigned short), src.rows, src.cols, bidx);
668 669
            break;
        case 4:
A
Andrey Morozov 已提交
670
            imgproc::RGB2Gray_4<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(unsigned short), dst.ptr, dst.step / sizeof(unsigned short), src.rows, src.cols, bidx);
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
            break;
        default:
            cv::gpu::error("Unsupported channels count", __FILE__, __LINE__);
            break;
        }

        if (stream == 0)
            cudaSafeCall( cudaThreadSynchronize() );
    }

    void RGB2Gray_gpu(const DevMem2Df& src, int srccn, const DevMem2Df& dst, int bidx, cudaStream_t stream)
    {
        dim3 threads(32, 8, 1);
        dim3 grid(1, 1, 1);

        grid.x = divUp(src.cols, threads.x);
        grid.y = divUp(src.rows, threads.y);

        switch (srccn)
        {
        case 3:
            imgproc::RGB2Gray_3<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(float), dst.ptr, dst.step / sizeof(float), src.rows, src.cols, bidx);
            break;
        case 4:
            imgproc::RGB2Gray_4<<<grid, threads, 0, stream>>>(src.ptr, src.step / sizeof(float), dst.ptr, dst.step / sizeof(float), src.rows, src.cols, bidx);
            break;
        default:
            cv::gpu::error("Unsupported channels count", __FILE__, __LINE__);
            break;
        }

        if (stream == 0)
            cudaSafeCall( cudaThreadSynchronize() );
    }
}}}
A
Andrey Morozov 已提交
706

707 708 709 710 711 712 713
///////////////////////////////////// RGB <-> YCrCb //////////////////////////////////////

//namespace imgproc
//{
//    template<typename _Tp> struct RGB2YCrCb_f
//    {
//        typedef _Tp channel_type;
A
Andrey Morozov 已提交
714
//
715 716 717 718 719 720
//        RGB2YCrCb_f(int _srccn, int _blueIdx, const float* _coeffs) : srccn(_srccn), blueIdx(_blueIdx)
//	    {
//		    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]);
//	    }
A
Andrey Morozov 已提交
721
//
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
//        void operator()(const _Tp* src, _Tp* dst, int n) const
//        {
//            int scn = srccn, bidx = blueIdx;
//            const _Tp delta = ColorChannel<_Tp>::half();
//		    float C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2], C3 = coeffs[3], C4 = coeffs[4];
//            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;
// 	    float coeffs[5];
//    };
//
//    template<typename _Tp> struct RGB2YCrCb_i
//    {
//        typedef _Tp channel_type;
A
Andrey Morozov 已提交
743
//
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
//        RGB2YCrCb_i(int _srccn, int _blueIdx, const int* _coeffs)
//		    : 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]);
//	    }
//        void operator()(const _Tp* src, _Tp* dst, int n) const
//        {
//            int scn = srccn, bidx = blueIdx;
//		    int C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2], C3 = coeffs[3], C4 = coeffs[4];
//            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;
//	    int coeffs[5];
//    };
//
//    template<typename _Tp> struct YCrCb2RGB_f
//    {
//        typedef _Tp channel_type;
A
Andrey Morozov 已提交
774
//
775 776 777
//        YCrCb2RGB_f(int _dstcn, int _blueIdx, const float* _coeffs)
//		    : dstcn(_dstcn), blueIdx(_blueIdx)
//	    {
A
Andrey Morozov 已提交
778
//		    static const float coeffs0[] = {1.403f, -0.714f, -0.344f, 1.773f};
779 780 781 782 783 784 785 786 787 788 789 790 791
//		    memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 4*sizeof(coeffs[0]));
//	    }
//        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();
//            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)
//            {
//                _Tp Y = src[i];
//                _Tp Cr = src[i+1];
//                _Tp Cb = src[i+2];
A
Andrey Morozov 已提交
792
//
793 794 795
//                _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 Morozov 已提交
796
//
797 798 799 800 801 802 803 804 805 806 807 808
//                dst[bidx] = b; dst[1] = g; dst[bidx^2] = r;
//                if( dcn == 4 )
//                    dst[3] = alpha;
//            }
//        }
//        int dstcn, blueIdx;
//	    float coeffs[4];
//    };
//
//    template<typename _Tp> struct YCrCb2RGB_i
//    {
//        typedef _Tp channel_type;
A
Andrey Morozov 已提交
809
//
810 811 812
//        YCrCb2RGB_i(int _dstcn, int _blueIdx, const int* _coeffs)
//            : dstcn(_dstcn), blueIdx(_blueIdx)
//        {
A
Andrey Morozov 已提交
813
//            static const int coeffs0[] = {22987, -11698, -5636, 29049};
814 815
//		    memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 4*sizeof(coeffs[0]));
//        }
A
Andrey Morozov 已提交
816
//
817 818 819 820 821 822 823 824 825 826 827
//        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)
//            {
//                _Tp Y = src[i];
//                _Tp Cr = src[i+1];
//                _Tp Cb = src[i+2];
A
Andrey Morozov 已提交
828
//
829 830 831
//                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 Morozov 已提交
832
//
833 834 835 836 837 838 839 840 841 842 843 844
//                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;
//            }
//        }
//        int dstcn, blueIdx;
//        int coeffs[4];
//    };
//}
//
A
Andrey Morozov 已提交
845
//namespace cv { namespace gpu { namespace impl
846 847
//{
//}}}
A
Andrey Morozov 已提交
848

849 850 851 852 853 854 855 856 857 858
////////////////////////////////////// RGB <-> XYZ ///////////////////////////////////////

//namespace imgproc
//{
//    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 Morozov 已提交
859
//
860 861 862 863 864 865
//    static const float XYZ2sRGB_D65[] =
//    {
//        3.240479f, -1.53715f, -0.498535f,
//        -0.969256f, 1.875991f, 0.041556f,
//        0.055648f, -0.204043f, 1.057311f
//    };
A
Andrey Morozov 已提交
866
//
867 868 869
//    template<typename _Tp> struct RGB2XYZ_f
//    {
//        typedef _Tp channel_type;
A
Andrey Morozov 已提交
870
//
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
//        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 Morozov 已提交
887
//
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
//            n *= 3;
//            for(int i = 0; i < n; i += 3, src += scn)
//            {
//			    _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);
//                dst[i] = X; dst[i+1] = Y; dst[i+2] = Z;
//            }
//        }
//        int srccn;
//        float coeffs[9];
//    };
//
//    template<typename _Tp> struct RGB2XYZ_i
//    {
//        typedef _Tp channel_type;
A
Andrey Morozov 已提交
904
//
905 906 907 908
//        RGB2XYZ_i(int _srccn, int blueIdx, const float* _coeffs) : srccn(_srccn)
//        {
//            static const int coeffs0[] =
//            {
A
Andrey Morozov 已提交
909 910
//                1689,    1465,    739,
//                871,     2929,    296,
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
//                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 Morozov 已提交
941
//
942 943 944
//    template<typename _Tp> struct XYZ2RGB_f
//    {
//        typedef _Tp channel_type;
A
Andrey Morozov 已提交
945
//
946 947 948 949 950 951 952 953 954 955 956
//        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 Morozov 已提交
957
//
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
//        void operator()(const _Tp* src, _Tp* dst, int n) const
//        {
//            int dcn = dstcn;
//		    _Tp alpha = ColorChannel<_Tp>::max();
//            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)
//            {
//			    _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);
//                dst[0] = B; dst[1] = G; dst[2] = R;
//			    if( dcn == 4 )
//				    dst[3] = alpha;
//            }
//        }
//        int dstcn, blueIdx;
//        float coeffs[9];
//    };
//
//    template<typename _Tp> struct XYZ2RGB_i
//    {
//        typedef _Tp channel_type;
A
Andrey Morozov 已提交
983
//
984 985 986 987 988
//        XYZ2RGB_i(int _dstcn, int _blueIdx, const int* _coeffs)
//        : dstcn(_dstcn), blueIdx(_blueIdx)
//        {
//            static const int coeffs0[] =
//            {
A
Andrey Morozov 已提交
989 990
//                13273,  -6296,  -2042,
//                -3970,   7684,    170,
991 992 993 994
//                  228,   -836,   4331
//            };
//            for(int i = 0; i < 9; i++)
//                coeffs[i] = _coeffs ? cvRound(_coeffs[i]*(1 << xyz_shift)) : coeffs0[i];
A
Andrey Morozov 已提交
995
//
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
//            if(blueIdx == 0)
//            {
//                std::swap(coeffs[0], coeffs[6]);
//                std::swap(coeffs[1], coeffs[7]);
//                std::swap(coeffs[2], coeffs[8]);
//            }
//        }
//        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 )
//				    dst[3] = alpha;
//            }
//        }
//        int dstcn, blueIdx;
//        int coeffs[9];
//    };
//}
//
//namespace cv { namespace gpu { namespace impl
//{
//}}}

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

//struct RGB2HSV_b
//{
//    typedef uchar channel_type;
A
Andrey Morozov 已提交
1036
//
1037 1038
//    RGB2HSV_b(int _srccn, int _blueIdx, int _hrange)
//    : srccn(_srccn), blueIdx(_blueIdx), hrange(_hrange) {}
A
Andrey Morozov 已提交
1039
//
1040 1041 1042 1043
//    void operator()(const uchar* src, uchar* dst, int n) const
//    {
//        int i, bidx = blueIdx, scn = srccn;
//        const int hsv_shift = 12;
A
Andrey Morozov 已提交
1044
//
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
//        static const int div_table[] = {
//            0, 1044480, 522240, 348160, 261120, 208896, 174080, 149211,
//            130560, 116053, 104448, 94953, 87040, 80345, 74606, 69632,
//            65280, 61440, 58027, 54973, 52224, 49737, 47476, 45412,
//            43520, 41779, 40172, 38684, 37303, 36017, 34816, 33693,
//            32640, 31651, 30720, 29842, 29013, 28229, 27486, 26782,
//            26112, 25475, 24869, 24290, 23738, 23211, 22706, 22223,
//            21760, 21316, 20890, 20480, 20086, 19707, 19342, 18991,
//            18651, 18324, 18008, 17703, 17408, 17123, 16846, 16579,
//            16320, 16069, 15825, 15589, 15360, 15137, 14921, 14711,
//            14507, 14308, 14115, 13926, 13743, 13565, 13391, 13221,
//            13056, 12895, 12738, 12584, 12434, 12288, 12145, 12006,
//            11869, 11736, 11605, 11478, 11353, 11231, 11111, 10995,
//            10880, 10768, 10658, 10550, 10445, 10341, 10240, 10141,
//            10043, 9947, 9854, 9761, 9671, 9582, 9495, 9410,
//            9326, 9243, 9162, 9082, 9004, 8927, 8852, 8777,
//            8704, 8632, 8561, 8492, 8423, 8356, 8290, 8224,
//            8160, 8097, 8034, 7973, 7913, 7853, 7795, 7737,
//            7680, 7624, 7569, 7514, 7461, 7408, 7355, 7304,
//            7253, 7203, 7154, 7105, 7057, 7010, 6963, 6917,
//            6872, 6827, 6782, 6739, 6695, 6653, 6611, 6569,
//            6528, 6487, 6447, 6408, 6369, 6330, 6292, 6254,
//            6217, 6180, 6144, 6108, 6073, 6037, 6003, 5968,
//            5935, 5901, 5868, 5835, 5803, 5771, 5739, 5708,
//            5677, 5646, 5615, 5585, 5556, 5526, 5497, 5468,
//            5440, 5412, 5384, 5356, 5329, 5302, 5275, 5249,
//            5222, 5196, 5171, 5145, 5120, 5095, 5070, 5046,
//            5022, 4998, 4974, 4950, 4927, 4904, 4881, 4858,
//            4836, 4813, 4791, 4769, 4748, 4726, 4705, 4684,
//            4663, 4642, 4622, 4601, 4581, 4561, 4541, 4522,
//            4502, 4483, 4464, 4445, 4426, 4407, 4389, 4370,
//            4352, 4334, 4316, 4298, 4281, 4263, 4246, 4229,
//            4212, 4195, 4178, 4161, 4145, 4128, 4112, 4096
//        };
//        int hr = hrange, hscale = hr == 180 ? 15 : 21;
//        n *= 3;
A
Andrey Morozov 已提交
1081
//
1082 1083 1084 1085 1086 1087
//        for( i = 0; i < n; i += 3, src += scn )
//        {
//            int b = src[bidx], g = src[1], r = src[bidx^2];
//            int h, s, v = b;
//            int vmin = b, diff;
//            int vr, vg;
A
Andrey Morozov 已提交
1088
//
1089 1090 1091 1092
//            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 Morozov 已提交
1093
//
1094 1095 1096
//            diff = v - vmin;
//            vr = v == r ? -1 : 0;
//            vg = v == g ? -1 : 0;
A
Andrey Morozov 已提交
1097
//
1098 1099 1100 1101 1102
//            s = diff * div_table[v] >> hsv_shift;
//            h = (vr & (g - b)) +
//                (~vr & ((vg & (b - r + 2 * diff)) + ((~vg) & (r - g + 4 * diff))));
//            h = (h * div_table[diff] * hscale + (1 << (hsv_shift + 6))) >> (7 + hsv_shift);
//            h += h < 0 ? hr : 0;
A
Andrey Morozov 已提交
1103
//
1104 1105 1106 1107 1108
//            dst[i] = (uchar)h;
//            dst[i+1] = (uchar)s;
//            dst[i+2] = (uchar)v;
//        }
//    }
A
Andrey Morozov 已提交
1109
//
1110
//    int srccn, blueIdx, hrange;
A
Andrey Morozov 已提交
1111 1112
//};
//
1113 1114 1115 1116
//
//struct RGB2HSV_f
//{
//    typedef float channel_type;
A
Andrey Morozov 已提交
1117
//
1118 1119
//    RGB2HSV_f(int _srccn, int _blueIdx, float _hrange)
//    : srccn(_srccn), blueIdx(_blueIdx), hrange(_hrange) {}
A
Andrey Morozov 已提交
1120
//
1121 1122 1123 1124 1125
//    void operator()(const float* src, float* dst, int n) const
//    {
//        int i, bidx = blueIdx, scn = srccn;
//        float hscale = hrange*(1.f/360.f);
//        n *= 3;
A
Andrey Morozov 已提交
1126
//
1127 1128 1129 1130
//        for( i = 0; i < n; i += 3, src += scn )
//        {
//            float b = src[bidx], g = src[1], r = src[bidx^2];
//            float h, s, v;
A
Andrey Morozov 已提交
1131
//
1132
//            float vmin, diff;
A
Andrey Morozov 已提交
1133
//
1134 1135 1136 1137 1138
//            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 Morozov 已提交
1139
//
1140 1141 1142 1143 1144 1145 1146 1147 1148
//            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 Morozov 已提交
1149
//
1150
//            if( h < 0 ) h += 360.f;
A
Andrey Morozov 已提交
1151
//
1152 1153 1154 1155 1156
//            dst[i] = h*hscale;
//            dst[i+1] = s;
//            dst[i+2] = v;
//        }
//    }
A
Andrey Morozov 已提交
1157
//
1158 1159 1160 1161 1162 1163 1164 1165
//    int srccn, blueIdx;
//    float hrange;
//};
//
//
//struct HSV2RGB_f
//{
//    typedef float channel_type;
A
Andrey Morozov 已提交
1166
//
1167 1168
//    HSV2RGB_f(int _dstcn, int _blueIdx, float _hrange)
//    : dstcn(_dstcn), blueIdx(_blueIdx), hscale(6.f/_hrange) {}
A
Andrey Morozov 已提交
1169
//
1170 1171 1172 1173 1174 1175
//    void operator()(const float* src, float* dst, int n) const
//    {
//        int i, bidx = blueIdx, dcn = dstcn;
//        float _hscale = hscale;
//        float alpha = ColorChannel<float>::max();
//        n *= 3;
A
Andrey Morozov 已提交
1176
//
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
//        for( i = 0; i < n; i += 3, dst += dcn )
//        {
//            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;
//                h *= _hscale;
//                if( h < 0 )
//                    do h += 6; while( h < 0 );
//                else if( h >= 6 )
//                    do h -= 6; while( h >= 6 );
//                sector = cvFloor(h);
//                h -= sector;
//
//                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 Morozov 已提交
1202
//
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
//                b = tab[sector_data[sector][0]];
//                g = tab[sector_data[sector][1]];
//                r = tab[sector_data[sector][2]];
//            }
//
//            dst[bidx] = b;
//            dst[1] = g;
//            dst[bidx^2] = r;
//            if( dcn == 4 )
//                dst[3] = alpha;
//        }
//    }
//
//    int dstcn, blueIdx;
//    float hscale;
//};
A
Andrey Morozov 已提交
1219
//
1220 1221 1222 1223
//
//struct HSV2RGB_b
//{
//    typedef uchar channel_type;
A
Andrey Morozov 已提交
1224
//
1225 1226 1227
//    HSV2RGB_b(int _dstcn, int _blueIdx, int _hrange)
//    : dstcn(_dstcn), cvt(3, _blueIdx, _hrange)
//    {}
A
Andrey Morozov 已提交
1228
//
1229 1230 1231 1232 1233
//    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 Morozov 已提交
1234
//
1235 1236 1237
//        for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 )
//        {
//            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Morozov 已提交
1238
//
1239 1240 1241 1242 1243 1244 1245
//            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 Morozov 已提交
1246
//
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
//            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 Morozov 已提交
1257
//
1258 1259 1260 1261
//    int dstcn;
//    HSV2RGB_f cvt;
//};
//
A
Andrey Morozov 已提交
1262
//
1263 1264 1265 1266 1267
/////////////////////////////////////// RGB <-> HLS ////////////////////////////////////////
//
//struct RGB2HLS_f
//{
//    typedef float channel_type;
A
Andrey Morozov 已提交
1268
//
1269 1270
//    RGB2HLS_f(int _srccn, int _blueIdx, float _hrange)
//    : srccn(_srccn), blueIdx(_blueIdx), hrange(_hrange) {}
A
Andrey Morozov 已提交
1271
//
1272 1273 1274 1275 1276
//    void operator()(const float* src, float* dst, int n) const
//    {
//        int i, bidx = blueIdx, scn = srccn;
//        float hscale = hrange*(1.f/360.f);
//        n *= 3;
A
Andrey Morozov 已提交
1277
//
1278 1279 1280 1281 1282
//        for( i = 0; i < n; i += 3, src += scn )
//        {
//            float b = src[bidx], g = src[1], r = src[bidx^2];
//            float h = 0.f, s = 0.f, l;
//            float vmin, vmax, diff;
A
Andrey Morozov 已提交
1283
//
1284 1285 1286 1287 1288
//            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 Morozov 已提交
1289
//
1290 1291
//            diff = vmax - vmin;
//            l = (vmax + vmin)*0.5f;
A
Andrey Morozov 已提交
1292
//
1293 1294 1295 1296
//            if( diff > FLT_EPSILON )
//            {
//                s = l < 0.5f ? diff/(vmax + vmin) : diff/(2 - vmax - vmin);
//                diff = 60.f/diff;
A
Andrey Morozov 已提交
1297
//
1298 1299 1300 1301 1302 1303
//                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 Morozov 已提交
1304
//
1305 1306
//                if( h < 0.f ) h += 360.f;
//            }
A
Andrey Morozov 已提交
1307
//
1308 1309 1310 1311 1312
//            dst[i] = h*hscale;
//            dst[i+1] = l;
//            dst[i+2] = s;
//        }
//    }
A
Andrey Morozov 已提交
1313
//
1314 1315 1316
//    int srccn, blueIdx;
//    float hrange;
//};
A
Andrey Morozov 已提交
1317 1318
//
//
1319 1320 1321
//struct RGB2HLS_b
//{
//    typedef uchar channel_type;
A
Andrey Morozov 已提交
1322
//
1323 1324
//    RGB2HLS_b(int _srccn, int _blueIdx, int _hrange)
//    : srccn(_srccn), cvt(3, _blueIdx, (float)_hrange) {}
A
Andrey Morozov 已提交
1325
//
1326 1327 1328 1329
//    void operator()(const uchar* src, uchar* dst, int n) const
//    {
//        int i, j, scn = srccn;
//        float buf[3*BLOCK_SIZE];
A
Andrey Morozov 已提交
1330
//
1331 1332 1333
//        for( i = 0; i < n; i += BLOCK_SIZE, dst += BLOCK_SIZE*3 )
//        {
//            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Morozov 已提交
1334
//
1335 1336 1337 1338 1339 1340 1341
//            for( j = 0; j < dn*3; j += 3, src += scn )
//            {
//                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);
//            }
//            cvt(buf, buf, dn);
A
Andrey Morozov 已提交
1342
//
1343 1344 1345 1346 1347 1348 1349 1350
//            for( j = 0; j < dn*3; j += 3 )
//            {
//                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);
//            }
//        }
//    }
A
Andrey Morozov 已提交
1351
//
1352 1353 1354
//    int srccn;
//    RGB2HLS_f cvt;
//};
A
Andrey Morozov 已提交
1355
//
1356 1357 1358 1359
//
//struct HLS2RGB_f
//{
//    typedef float channel_type;
A
Andrey Morozov 已提交
1360
//
1361 1362
//    HLS2RGB_f(int _dstcn, int _blueIdx, float _hrange)
//    : dstcn(_dstcn), blueIdx(_blueIdx), hscale(6.f/_hrange) {}
A
Andrey Morozov 已提交
1363
//
1364 1365 1366 1367 1368 1369
//    void operator()(const float* src, float* dst, int n) const
//    {
//        int i, bidx = blueIdx, dcn = dstcn;
//        float _hscale = hscale;
//        float alpha = ColorChannel<float>::max();
//        n *= 3;
A
Andrey Morozov 已提交
1370
//
1371 1372 1373 1374
//        for( i = 0; i < n; i += 3, dst += dcn )
//        {
//            float h = src[i], l = src[i+1], s = src[i+2];
//            float b, g, r;
A
Andrey Morozov 已提交
1375
//
1376 1377 1378 1379 1380 1381 1382 1383
//            if( s == 0 )
//                b = g = r = l;
//            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;
A
Andrey Morozov 已提交
1384
//
1385 1386
//                float p2 = l <= 0.5f ? l*(1 + s) : l + s - l*s;
//                float p1 = 2*l - p2;
A
Andrey Morozov 已提交
1387
//
1388 1389 1390 1391 1392
//                h *= _hscale;
//                if( h < 0 )
//                    do h += 6; while( h < 0 );
//                else if( h >= 6 )
//                    do h -= 6; while( h >= 6 );
A
Andrey Morozov 已提交
1393
//
1394 1395 1396
//                assert( 0 <= h && h < 6 );
//                sector = cvFloor(h);
//                h -= sector;
A
Andrey Morozov 已提交
1397
//
1398 1399 1400 1401
//                tab[0] = p2;
//                tab[1] = p1;
//                tab[2] = p1 + (p2 - p1)*(1-h);
//                tab[3] = p1 + (p2 - p1)*h;
A
Andrey Morozov 已提交
1402
//
1403 1404 1405 1406
//                b = tab[sector_data[sector][0]];
//                g = tab[sector_data[sector][1]];
//                r = tab[sector_data[sector][2]];
//            }
A
Andrey Morozov 已提交
1407
//
1408 1409 1410 1411 1412 1413 1414
//            dst[bidx] = b;
//            dst[1] = g;
//            dst[bidx^2] = r;
//            if( dcn == 4 )
//                dst[3] = alpha;
//        }
//    }
A
Andrey Morozov 已提交
1415
//
1416 1417 1418
//    int dstcn, blueIdx;
//    float hscale;
//};
A
Andrey Morozov 已提交
1419
//
1420 1421 1422 1423
//
//struct HLS2RGB_b
//{
//    typedef uchar channel_type;
A
Andrey Morozov 已提交
1424
//
1425 1426 1427
//    HLS2RGB_b(int _dstcn, int _blueIdx, int _hrange)
//    : dstcn(_dstcn), cvt(3, _blueIdx, _hrange)
//    {}
A
Andrey Morozov 已提交
1428
//
1429 1430 1431 1432 1433
//    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 Morozov 已提交
1434
//
1435 1436 1437
//        for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 )
//        {
//            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Morozov 已提交
1438
//
1439 1440 1441 1442 1443 1444 1445
//            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 Morozov 已提交
1446
//
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
//            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 Morozov 已提交
1457
//
1458 1459 1460 1461
//    int dstcn;
//    HLS2RGB_f cvt;
//};
//
A
Andrey Morozov 已提交
1462
//
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
/////////////////////////////////////// RGB <-> L*a*b* /////////////////////////////////////
//
//static const float D65[] = { 0.950456f, 1.f, 1.088754f };
//
//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;
//
//static float sRGBGammaTab[GAMMA_TAB_SIZE*4], sRGBInvGammaTab[GAMMA_TAB_SIZE*4];
//static const float GammaTabScale = (float)GAMMA_TAB_SIZE;
A
Andrey Morozov 已提交
1473 1474
//
//static unsigned short sRGBGammaTab_b[256], linearGammaTab_b[256];
1475 1476 1477 1478 1479
//#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))
A
Andrey Morozov 已提交
1480 1481
//static unsigned short LabCbrtTab_b[LAB_CBRT_TAB_SIZE_B];
//
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
//static void initLabTabs()
//{
//    static bool initialized = false;
//    if(!initialized)
//    {
//        float f[LAB_CBRT_TAB_SIZE+1], g[GAMMA_TAB_SIZE], ig[GAMMA_TAB_SIZE], scale = 1.f/LabCbrtTabScale;
//        int i;
//        for(i = 0; i <= LAB_CBRT_TAB_SIZE; i++)
//        {
//            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 Morozov 已提交
1495
//
1496 1497 1498 1499 1500 1501 1502 1503 1504
//        scale = 1.f/GammaTabScale;
//        for(i = 0; i <= GAMMA_TAB_SIZE; i++)
//        {
//            float x = i*scale;
//            g[i] = x <= 0.04045f ? x*(1.f/12.92f) : (float)pow((double)(x + 0.055)*(1./1.055), 2.4);
//            ig[i] = x <= 0.0031308 ? x*12.92f : (float)(1.055*pow((double)x, 1./2.4) - 0.055);
//        }
//        splineBuild(g, GAMMA_TAB_SIZE, sRGBGammaTab);
//        splineBuild(ig, GAMMA_TAB_SIZE, sRGBInvGammaTab);
A
Andrey Morozov 已提交
1505
//
1506 1507 1508
//        for(i = 0; i < 256; i++)
//        {
//            float x = i*(1.f/255.f);
A
Andrey Morozov 已提交
1509 1510
//            sRGBGammaTab_b[i] = saturate_cast<unsigned short>(255.f*(1 << gamma_shift)*(x <= 0.04045f ? x*(1.f/12.92f) : (float)pow((double)(x + 0.055)*(1./1.055), 2.4)));
//            linearGammaTab_b[i] = (unsigned short)(i*(1 << gamma_shift));
1511
//        }
A
Andrey Morozov 已提交
1512
//
1513 1514 1515
//        for(i = 0; i < LAB_CBRT_TAB_SIZE_B; i++)
//        {
//            float x = i*(1.f/(255.f*(1 << gamma_shift)));
A
Andrey Morozov 已提交
1516
//            LabCbrtTab_b[i] = saturate_cast<unsigned short>((1 << lab_shift2)*(x < 0.008856f ? x*7.787f + 0.13793103448275862f : cvCbrt(x)));
1517 1518 1519 1520 1521 1522 1523 1524 1525
//        }
//        initialized = true;
//    }
//}
//
//
//struct RGB2Lab_b
//{
//    typedef uchar channel_type;
A
Andrey Morozov 已提交
1526
//
1527 1528 1529 1530 1531
//    RGB2Lab_b(int _srccn, int blueIdx, const float* _coeffs,
//              const float* _whitept, bool _srgb)
//    : srccn(_srccn), srgb(_srgb)
//    {
//        initLabTabs();
A
Andrey Morozov 已提交
1532
//
1533 1534 1535 1536 1537 1538 1539 1540
//        if(!_coeffs) _coeffs = sRGB2XYZ_D65;
//        if(!_whitept) _whitept = D65;
//        float scale[] =
//        {
//            (1 << lab_shift)/_whitept[0],
//            (float)(1 << lab_shift),
//            (1 << lab_shift)/_whitept[2]
//        };
A
Andrey Morozov 已提交
1541
//
1542 1543 1544 1545 1546 1547 1548 1549 1550
//        for( int i = 0; i < 3; i++ )
//        {
//            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]);
//            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) );
//        }
//    }
A
Andrey Morozov 已提交
1551
//
1552 1553 1554 1555
//    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);
A
Andrey Morozov 已提交
1556
//        const unsigned short* tab = srgb ? sRGBGammaTab_b : linearGammaTab_b;
1557 1558 1559 1560 1561
//        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 Morozov 已提交
1562
//
1563 1564 1565 1566 1567 1568
//        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 Morozov 已提交
1569
//
1570 1571 1572
//            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 Morozov 已提交
1573
//
1574 1575 1576 1577 1578
//            dst[i] = saturate_cast<uchar>(L);
//            dst[i+1] = saturate_cast<uchar>(a);
//            dst[i+2] = saturate_cast<uchar>(b);
//        }
//    }
A
Andrey Morozov 已提交
1579
//
1580 1581 1582 1583
//    int srccn;
//    int coeffs[9];
//    bool srgb;
//};
A
Andrey Morozov 已提交
1584 1585
//
//
1586 1587 1588
//struct RGB2Lab_f
//{
//    typedef float channel_type;
A
Andrey Morozov 已提交
1589
//
1590 1591 1592 1593 1594
//    RGB2Lab_f(int _srccn, int blueIdx, const float* _coeffs,
//              const float* _whitept, bool _srgb)
//    : srccn(_srccn), srgb(_srgb)
//    {
//        initLabTabs();
A
Andrey Morozov 已提交
1595
//
1596 1597 1598
//        if(!_coeffs) _coeffs = sRGB2XYZ_D65;
//        if(!_whitept) _whitept = D65;
//        float scale[] = { LabCbrtTabScale/_whitept[0], LabCbrtTabScale, LabCbrtTabScale/_whitept[2] };
A
Andrey Morozov 已提交
1599
//
1600 1601 1602 1603 1604 1605 1606 1607 1608
//        for( int i = 0; i < 3; i++ )
//        {
//            coeffs[i*3+(blueIdx^2)] = _coeffs[i*3]*scale[i];
//            coeffs[i*3+1] = _coeffs[i*3+1]*scale[i];
//            coeffs[i*3+blueIdx] = _coeffs[i*3+2]*scale[i];
//            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*LabCbrtTabScale );
//        }
//    }
A
Andrey Morozov 已提交
1609
//
1610 1611 1612 1613 1614 1615 1616 1617 1618
//    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 Morozov 已提交
1619
//
1620 1621 1622 1623 1624 1625 1626 1627 1628
//        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 Morozov 已提交
1629
//            float fX = splineInterpolate(R*C0 + G*C1 + B*C2, LabCbrtTab, LAB_CBRT_TAB_SIZE);
1630 1631
//            float fY = splineInterpolate(R*C3 + G*C4 + B*C5, LabCbrtTab, LAB_CBRT_TAB_SIZE);
//            float fZ = splineInterpolate(R*C6 + G*C7 + B*C8, LabCbrtTab, LAB_CBRT_TAB_SIZE);
A
Andrey Morozov 已提交
1632
//
1633 1634 1635
//            float L = 116.f*fY - 16.f;
//            float a = 500.f*(fX - fY);
//            float b = 200.f*(fY - fZ);
A
Andrey Morozov 已提交
1636
//
1637 1638 1639
//            dst[i] = L; dst[i+1] = a; dst[i+2] = b;
//        }
//    }
A
Andrey Morozov 已提交
1640
//
1641 1642 1643 1644 1645
//    int srccn;
//    float coeffs[9];
//    bool srgb;
//};
//
A
Andrey Morozov 已提交
1646
//
1647 1648 1649
//struct Lab2RGB_f
//{
//    typedef float channel_type;
A
Andrey Morozov 已提交
1650
//
1651 1652 1653 1654 1655
//    Lab2RGB_f( int _dstcn, int blueIdx, const float* _coeffs,
//               const float* _whitept, bool _srgb )
//    : dstcn(_dstcn), srgb(_srgb)
//    {
//        initLabTabs();
A
Andrey Morozov 已提交
1656
//
1657 1658
//        if(!_coeffs) _coeffs = XYZ2sRGB_D65;
//        if(!_whitept) _whitept = D65;
A
Andrey Morozov 已提交
1659
//
1660 1661 1662 1663 1664 1665 1666
//        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];
//        }
//    }
A
Andrey Morozov 已提交
1667
//
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
//    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();
//        n *= 3;
A
Andrey Morozov 已提交
1678
//
1679 1680 1681 1682 1683 1684 1685 1686 1687
//        for( i = 0; i < n; i += 3, dst += dcn )
//        {
//            float L = src[i], a = src[i+1], b = src[i+2];
//            float Y = (L + 16.f)*(1.f/116.f);
//            float X = (Y + a*0.002f);
//            float Z = (Y - b*0.005f);
//            Y = Y*Y*Y;
//            X = X*X*X;
//            Z = Z*Z*Z;
A
Andrey Morozov 已提交
1688
//
1689 1690 1691
//            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 Morozov 已提交
1692
//
1693 1694 1695 1696 1697 1698
//            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 Morozov 已提交
1699
//
1700 1701 1702 1703 1704
//            dst[0] = R; dst[1] = G; dst[2] = B;
//            if( dcn == 4 )
//                dst[3] = alpha;
//        }
//    }
A
Andrey Morozov 已提交
1705
//
1706 1707 1708 1709 1710
//    int dstcn;
//    float coeffs[9];
//    bool srgb;
//};
//
A
Andrey Morozov 已提交
1711
//
1712 1713 1714
//struct Lab2RGB_b
//{
//    typedef uchar channel_type;
A
Andrey Morozov 已提交
1715
//
1716 1717 1718
//    Lab2RGB_b( int _dstcn, int blueIdx, const float* _coeffs,
//               const float* _whitept, bool _srgb )
//    : dstcn(_dstcn), cvt(3, blueIdx, _coeffs, _whitept, _srgb ) {}
A
Andrey Morozov 已提交
1719
//
1720 1721 1722 1723 1724
//    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 Morozov 已提交
1725
//
1726 1727 1728
//        for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 )
//        {
//            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Morozov 已提交
1729
//
1730 1731 1732 1733 1734 1735 1736
//            for( j = 0; j < dn*3; j += 3 )
//            {
//                buf[j] = src[j]*(100.f/255.f);
//                buf[j+1] = (float)(src[j+1] - 128);
//                buf[j+2] = (float)(src[j+2] - 128);
//            }
//            cvt(buf, buf, dn);
A
Andrey Morozov 已提交
1737
//
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
//            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 Morozov 已提交
1748
//
1749 1750 1751
//    int dstcn;
//    Lab2RGB_f cvt;
//};
A
Andrey Morozov 已提交
1752 1753
//
//
1754 1755 1756 1757 1758
/////////////////////////////////////// RGB <-> L*u*v* /////////////////////////////////////
//
//struct RGB2Luv_f
//{
//    typedef float channel_type;
A
Andrey Morozov 已提交
1759
//
1760 1761 1762 1763 1764
//    RGB2Luv_f( int _srccn, int blueIdx, const float* _coeffs,
//               const float* whitept, bool _srgb )
//    : srccn(_srccn), srgb(_srgb)
//    {
//        initLabTabs();
A
Andrey Morozov 已提交
1765
//
1766 1767
//        if(!_coeffs) _coeffs = sRGB2XYZ_D65;
//        if(!whitept) whitept = D65;
A
Andrey Morozov 已提交
1768
//
1769 1770 1771 1772 1773 1774 1775 1776
//        for( int i = 0; i < 3; i++ )
//        {
//            coeffs[i*3+(blueIdx^2)] = _coeffs[i*3];
//            coeffs[i*3+1] = _coeffs[i*3+1];
//            coeffs[i*3+blueIdx] = _coeffs[i*3+2];
//            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 );
//        }
A
Andrey Morozov 已提交
1777
//
1778 1779 1780
//        float d = 1.f/(whitept[0] + whitept[1]*15 + whitept[2]*3);
//        un = 4*whitept[0]*d;
//        vn = 9*whitept[1]*d;
A
Andrey Morozov 已提交
1781
//
1782 1783
//        CV_Assert(whitept[1] == 1.f);
//    }
A
Andrey Morozov 已提交
1784
//
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
//    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 Morozov 已提交
1795
//
1796 1797 1798 1799 1800 1801 1802 1803 1804
//        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 Morozov 已提交
1805
//
1806 1807 1808
//            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 Morozov 已提交
1809
//
1810 1811
//            float L = splineInterpolate(Y*LabCbrtTabScale, LabCbrtTab, LAB_CBRT_TAB_SIZE);
//            L = 116.f*L - 16.f;
A
Andrey Morozov 已提交
1812 1813
//
//            float d = (4*13) / std::max(X + 15 * Y + 3 * Z, FLT_EPSILON);
1814 1815
//            float u = L*(X*d - _un);
//            float v = L*((9*0.25)*Y*d - _vn);
A
Andrey Morozov 已提交
1816
//
1817 1818 1819
//            dst[i] = L; dst[i+1] = u; dst[i+2] = v;
//        }
//    }
A
Andrey Morozov 已提交
1820
//
1821 1822 1823 1824 1825
//    int srccn;
//    float coeffs[9], un, vn;
//    bool srgb;
//};
//
A
Andrey Morozov 已提交
1826
//
1827 1828 1829
//struct Luv2RGB_f
//{
//    typedef float channel_type;
A
Andrey Morozov 已提交
1830
//
1831 1832 1833 1834 1835
//    Luv2RGB_f( int _dstcn, int blueIdx, const float* _coeffs,
//              const float* whitept, bool _srgb )
//    : dstcn(_dstcn), srgb(_srgb)
//    {
//        initLabTabs();
A
Andrey Morozov 已提交
1836
//
1837 1838
//        if(!_coeffs) _coeffs = XYZ2sRGB_D65;
//        if(!whitept) whitept = D65;
A
Andrey Morozov 已提交
1839
//
1840 1841 1842 1843 1844 1845
//        for( int i = 0; i < 3; i++ )
//        {
//            coeffs[i+(blueIdx^2)*3] = _coeffs[i];
//            coeffs[i+3] = _coeffs[i+3];
//            coeffs[i+blueIdx*3] = _coeffs[i+6];
//        }
A
Andrey Morozov 已提交
1846
//
1847 1848 1849
//        float d = 1.f/(whitept[0] + whitept[1]*15 + whitept[2]*3);
//        un = 4*whitept[0]*d;
//        vn = 9*whitept[1]*d;
A
Andrey Morozov 已提交
1850
//
1851 1852
//        CV_Assert(whitept[1] == 1.f);
//    }
A
Andrey Morozov 已提交
1853
//
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
//    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 Morozov 已提交
1865
//
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
//        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 Morozov 已提交
1876 1877
//            Z = (12 - 3 * u - 20 * v) * Y * 0.25 * iv;
//
1878 1879 1880
//            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 Morozov 已提交
1881
//
1882 1883 1884 1885 1886 1887
//            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 Morozov 已提交
1888
//
1889 1890 1891 1892 1893
//            dst[0] = R; dst[1] = G; dst[2] = B;
//            if( dcn == 4 )
//                dst[3] = alpha;
//        }
//    }
A
Andrey Morozov 已提交
1894
//
1895 1896 1897 1898 1899
//    int dstcn;
//    float coeffs[9], un, vn;
//    bool srgb;
//};
//
A
Andrey Morozov 已提交
1900
//
1901 1902 1903
//struct RGB2Luv_b
//{
//    typedef uchar channel_type;
A
Andrey Morozov 已提交
1904
//
1905 1906 1907
//    RGB2Luv_b( int _srccn, int blueIdx, const float* _coeffs,
//               const float* _whitept, bool _srgb )
//    : srccn(_srccn), cvt(3, blueIdx, _coeffs, _whitept, _srgb) {}
A
Andrey Morozov 已提交
1908
//
1909 1910 1911 1912
//    void operator()(const uchar* src, uchar* dst, int n) const
//    {
//        int i, j, scn = srccn;
//        float buf[3*BLOCK_SIZE];
A
Andrey Morozov 已提交
1913
//
1914 1915 1916
//        for( i = 0; i < n; i += BLOCK_SIZE, dst += BLOCK_SIZE*3 )
//        {
//            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Morozov 已提交
1917
//
1918 1919 1920 1921 1922 1923 1924
//            for( j = 0; j < dn*3; j += 3, src += scn )
//            {
//                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));
//            }
//            cvt(buf, buf, dn);
A
Andrey Morozov 已提交
1925
//
1926 1927 1928 1929 1930 1931 1932 1933
//            for( j = 0; j < dn*3; j += 3 )
//            {
//                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);
//            }
//        }
//    }
A
Andrey Morozov 已提交
1934
//
1935 1936 1937
//    int srccn;
//    RGB2Luv_f cvt;
//};
A
Andrey Morozov 已提交
1938
//
1939 1940 1941 1942
//
//struct Luv2RGB_b
//{
//    typedef uchar channel_type;
A
Andrey Morozov 已提交
1943
//
1944 1945 1946
//    Luv2RGB_b( int _dstcn, int blueIdx, const float* _coeffs,
//               const float* _whitept, bool _srgb )
//    : dstcn(_dstcn), cvt(3, blueIdx, _coeffs, _whitept, _srgb ) {}
A
Andrey Morozov 已提交
1947
//
1948 1949 1950 1951 1952
//    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 Morozov 已提交
1953
//
1954 1955 1956
//        for( i = 0; i < n; i += BLOCK_SIZE, src += BLOCK_SIZE*3 )
//        {
//            int dn = std::min(n - i, (int)BLOCK_SIZE);
A
Andrey Morozov 已提交
1957
//
1958 1959 1960 1961 1962 1963 1964
//            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 Morozov 已提交
1965
//
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
//            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 Morozov 已提交
1976
//
1977 1978 1979 1980
//    int dstcn;
//    Luv2RGB_f cvt;
//};
//
A
Andrey Morozov 已提交
1981
//
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
////////////////////////////// Bayer Pattern -> RGB conversion /////////////////////////////
//
//static void Bayer2RGB_8u( const Mat& srcmat, Mat& dstmat, int code )
//{
//    const uchar* bayer0 = srcmat.data;
//    int bayer_step = (int)srcmat.step;
//    uchar* dst0 = dstmat.data;
//    int dst_step = (int)dstmat.step;
//    Size size = srcmat.size();
//    int blue = code == CV_BayerBG2BGR || code == CV_BayerGB2BGR ? -1 : 1;
//    int start_with_green = code == CV_BayerGB2BGR || code == CV_BayerGR2BGR;
//
//    memset( dst0, 0, size.width*3*sizeof(dst0[0]) );
//    memset( dst0 + (size.height - 1)*dst_step, 0, size.width*3*sizeof(dst0[0]) );
//    dst0 += dst_step + 3 + 1;
//    size.height -= 2;
//    size.width -= 2;
//
//    for( ; size.height-- > 0; bayer0 += bayer_step, dst0 += dst_step )
//    {
//        int t0, t1;
//        const uchar* bayer = bayer0;
//        uchar* dst = dst0;
//        const uchar* bayer_end = bayer + size.width;
//
//        dst[-4] = dst[-3] = dst[-2] = dst[size.width*3-1] =
//            dst[size.width*3] = dst[size.width*3+1] = 0;
//
//        if( size.width <= 0 )
//            continue;
//
//        if( start_with_green )
//        {
//            t0 = (bayer[1] + bayer[bayer_step*2+1] + 1) >> 1;
//            t1 = (bayer[bayer_step] + bayer[bayer_step+2] + 1) >> 1;
//            dst[-blue] = (uchar)t0;
//            dst[0] = bayer[bayer_step+1];
//            dst[blue] = (uchar)t1;
//            bayer++;
//            dst += 3;
//        }
//
//        if( blue > 0 )
//        {
//            for( ; bayer <= bayer_end - 2; bayer += 2, dst += 6 )
//            {
//                t0 = (bayer[0] + bayer[2] + bayer[bayer_step*2] +
//                      bayer[bayer_step*2+2] + 2) >> 2;
//                t1 = (bayer[1] + bayer[bayer_step] +
//                      bayer[bayer_step+2] + bayer[bayer_step*2+1]+2) >> 2;
//                dst[-1] = (uchar)t0;
//                dst[0] = (uchar)t1;
//                dst[1] = bayer[bayer_step+1];
//
//                t0 = (bayer[2] + bayer[bayer_step*2+2] + 1) >> 1;
//                t1 = (bayer[bayer_step+1] + bayer[bayer_step+3] + 1) >> 1;
//                dst[2] = (uchar)t0;
//                dst[3] = bayer[bayer_step+2];
//                dst[4] = (uchar)t1;
//            }
//        }
//        else
//        {
//            for( ; bayer <= bayer_end - 2; bayer += 2, dst += 6 )
//            {
//                t0 = (bayer[0] + bayer[2] + bayer[bayer_step*2] +
//                      bayer[bayer_step*2+2] + 2) >> 2;
//                t1 = (bayer[1] + bayer[bayer_step] +
//                      bayer[bayer_step+2] + bayer[bayer_step*2+1]+2) >> 2;
//                dst[1] = (uchar)t0;
//                dst[0] = (uchar)t1;
//                dst[-1] = bayer[bayer_step+1];
//
//                t0 = (bayer[2] + bayer[bayer_step*2+2] + 1) >> 1;
//                t1 = (bayer[bayer_step+1] + bayer[bayer_step+3] + 1) >> 1;
//                dst[4] = (uchar)t0;
//                dst[3] = bayer[bayer_step+2];
//                dst[2] = (uchar)t1;
//            }
//        }
//
//        if( bayer < bayer_end )
//        {
//            t0 = (bayer[0] + bayer[2] + bayer[bayer_step*2] +
//                  bayer[bayer_step*2+2] + 2) >> 2;
//            t1 = (bayer[1] + bayer[bayer_step] +
//                  bayer[bayer_step+2] + bayer[bayer_step*2+1]+2) >> 2;
//            dst[-blue] = (uchar)t0;
//            dst[0] = (uchar)t1;
//            dst[blue] = bayer[bayer_step+1];
//            bayer++;
//            dst += 3;
//        }
//
//        blue = -blue;
//        start_with_green = !start_with_green;
//    }
//}
//
A
Andrey Morozov 已提交
2081
//
2082
///////////////////// Demosaicing using Variable Number of Gradients ///////////////////////
A
Andrey Morozov 已提交
2083
//
2084 2085 2086 2087 2088 2089 2090
//static void Bayer2RGB_VNG_8u( const Mat& srcmat, Mat& dstmat, int code )
//{
//    const uchar* bayer = srcmat.data;
//    int bstep = (int)srcmat.step;
//    uchar* dst = dstmat.data;
//    int dststep = (int)dstmat.step;
//    Size size = srcmat.size();
A
Andrey Morozov 已提交
2091
//
2092 2093
//    int blueIdx = code == CV_BayerBG2BGR_VNG || code == CV_BayerGB2BGR_VNG ? 0 : 2;
//    bool greenCell0 = code != CV_BayerBG2BGR_VNG && code != CV_BayerRG2BGR_VNG;
A
Andrey Morozov 已提交
2094
//
2095 2096 2097 2098 2099 2100
//    // for too small images use the simple interpolation algorithm
//    if( MIN(size.width, size.height) < 8 )
//    {
//        Bayer2RGB_8u( srcmat, dstmat, code );
//        return;
//    }
A
Andrey Morozov 已提交
2101
//
2102
//    const int brows = 3, bcn = 7;
A
Andrey Morozov 已提交
2103
//    int N = size.width, N2 = N*2, N3 = N*3, N4 = N*4, N5 = N*5, N6 = N*6, N7 = N*7;
2104
//    int i, bufstep = N7*bcn;
A
Andrey Morozov 已提交
2105 2106 2107
//    cv::AutoBuffer<unsigned short> _buf(bufstep*brows);
//    unsigned short* buf = (unsigned short*)_buf;
//
2108
//    bayer += bstep*2;
A
Andrey Morozov 已提交
2109
//
2110 2111 2112 2113
//#if CV_SSE2
//    bool haveSSE = cv::checkHardwareSupport(CV_CPU_SSE2);
//    #define _mm_absdiff_epu16(a,b) _mm_adds_epu16(_mm_subs_epu16(a, b), _mm_subs_epu16(b, a))
//#endif
A
Andrey Morozov 已提交
2114
//
2115 2116 2117 2118
//    for( int y = 2; y < size.height - 4; y++ )
//    {
//        uchar* dstrow = dst + dststep*y + 6;
//        const uchar* srow;
A
Andrey Morozov 已提交
2119
//
2120 2121
//        for( int dy = (y == 2 ? -1 : 1); dy <= 1; dy++ )
//        {
A
Andrey Morozov 已提交
2122
//            unsigned short* brow = buf + ((y + dy - 1)%brows)*bufstep + 1;
2123
//            srow = bayer + (y+dy)*bstep + 1;
A
Andrey Morozov 已提交
2124
//
2125 2126
//            for( i = 0; i < bcn; i++ )
//                brow[N*i-1] = brow[(N-2) + N*i] = 0;
A
Andrey Morozov 已提交
2127
//
2128
//            i = 1;
A
Andrey Morozov 已提交
2129
//
2130 2131 2132 2133 2134 2135 2136
//#if CV_SSE2
//            if( haveSSE )
//            {
//                __m128i z = _mm_setzero_si128();
//                for( ; i <= N-9; i += 8, srow += 8, brow += 8 )
//                {
//                    __m128i s1, s2, s3, s4, s6, s7, s8, s9;
A
Andrey Morozov 已提交
2137
//
2138 2139 2140
//                    s1 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-1-bstep)),z);
//                    s2 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep)),z);
//                    s3 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+1-bstep)),z);
A
Andrey Morozov 已提交
2141
//
2142 2143
//                    s4 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-1)),z);
//                    s6 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+1)),z);
A
Andrey Morozov 已提交
2144
//
2145 2146 2147
//                    s7 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-1+bstep)),z);
//                    s8 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep)),z);
//                    s9 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+1+bstep)),z);
A
Andrey Morozov 已提交
2148
//
2149
//                    __m128i b0, b1, b2, b3, b4, b5, b6;
A
Andrey Morozov 已提交
2150
//
2151 2152 2153 2154 2155 2156 2157 2158
//                    b0 = _mm_adds_epu16(_mm_slli_epi16(_mm_absdiff_epu16(s2,s8),1),
//                                        _mm_adds_epu16(_mm_absdiff_epu16(s1, s7),
//                                                       _mm_absdiff_epu16(s3, s9)));
//                    b1 = _mm_adds_epu16(_mm_slli_epi16(_mm_absdiff_epu16(s4,s6),1),
//                                        _mm_adds_epu16(_mm_absdiff_epu16(s1, s3),
//                                                       _mm_absdiff_epu16(s7, s9)));
//                    b2 = _mm_slli_epi16(_mm_absdiff_epu16(s3,s7),1);
//                    b3 = _mm_slli_epi16(_mm_absdiff_epu16(s1,s9),1);
A
Andrey Morozov 已提交
2159
//
2160 2161 2162 2163
//                    _mm_storeu_si128((__m128i*)brow, b0);
//                    _mm_storeu_si128((__m128i*)(brow + N), b1);
//                    _mm_storeu_si128((__m128i*)(brow + N2), b2);
//                    _mm_storeu_si128((__m128i*)(brow + N3), b3);
A
Andrey Morozov 已提交
2164
//
2165 2166 2167 2168 2169 2170
//                    b4 = _mm_adds_epu16(b2,_mm_adds_epu16(_mm_absdiff_epu16(s2, s4),
//                                                          _mm_absdiff_epu16(s6, s8)));
//                    b5 = _mm_adds_epu16(b3,_mm_adds_epu16(_mm_absdiff_epu16(s2, s6),
//                                                          _mm_absdiff_epu16(s4, s8)));
//                    b6 = _mm_adds_epu16(_mm_adds_epu16(s2, s4), _mm_adds_epu16(s6, s8));
//                    b6 = _mm_srli_epi16(b6, 1);
A
Andrey Morozov 已提交
2171
//
2172 2173 2174 2175 2176 2177
//                    _mm_storeu_si128((__m128i*)(brow + N4), b4);
//                    _mm_storeu_si128((__m128i*)(brow + N5), b5);
//                    _mm_storeu_si128((__m128i*)(brow + N6), b6);
//                }
//            }
//#endif
A
Andrey Morozov 已提交
2178
//
2179 2180
//            for( ; i < N-1; i++, srow++, brow++ )
//            {
A
Andrey Morozov 已提交
2181
//                brow[0] = (unsigned short)(std::abs(srow[-1-bstep] - srow[-1+bstep]) +
2182 2183
//                                   std::abs(srow[-bstep] - srow[+bstep])*2 +
//                                   std::abs(srow[1-bstep] - srow[1+bstep]));
A
Andrey Morozov 已提交
2184
//                brow[N] = (unsigned short)(std::abs(srow[-1-bstep] - srow[1-bstep]) +
2185 2186
//                                   std::abs(srow[-1] - srow[1])*2 +
//                                   std::abs(srow[-1+bstep] - srow[1+bstep]));
A
Andrey Morozov 已提交
2187 2188 2189
//                brow[N2] = (unsigned short)(std::abs(srow[+1-bstep] - srow[-1+bstep])*2);
//                brow[N3] = (unsigned short)(std::abs(srow[-1-bstep] - srow[1+bstep])*2);
//                brow[N4] = (unsigned short)(brow[N2] + std::abs(srow[-bstep] - srow[-1]) +
2190
//                                    std::abs(srow[+bstep] - srow[1]));
A
Andrey Morozov 已提交
2191
//                brow[N5] = (unsigned short)(brow[N3] + std::abs(srow[-bstep] - srow[1]) +
2192
//                                    std::abs(srow[+bstep] - srow[-1]));
A
Andrey Morozov 已提交
2193
//                brow[N6] = (unsigned short)((srow[-bstep] + srow[-1] + srow[1] + srow[+bstep])>>1);
2194 2195
//            }
//        }
A
Andrey Morozov 已提交
2196 2197 2198 2199
//
//        const unsigned short* brow0 = buf + ((y - 2) % brows)*bufstep + 2;
//        const unsigned short* brow1 = buf + ((y - 1) % brows)*bufstep + 2;
//        const unsigned short* brow2 = buf + (y % brows)*bufstep + 2;
2200 2201 2202
//        static const float scale[] = { 0.f, 0.5f, 0.25f, 0.1666666666667f, 0.125f, 0.1f, 0.08333333333f, 0.0714286f, 0.0625f };
//        srow = bayer + y*bstep + 2;
//        bool greenCell = greenCell0;
A
Andrey Morozov 已提交
2203
//
2204
//        i = 2;
A
Andrey Morozov 已提交
2205
//#if CV_SSE2
2206 2207 2208 2209
//        int limit = !haveSSE ? N-2 : greenCell ? std::min(3, N-2) : 2;
//#else
//        int limit = N - 2;
//#endif
A
Andrey Morozov 已提交
2210
//
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
//        do
//        {
//            for( ; i < limit; i++, srow++, brow0++, brow1++, brow2++, dstrow += 3 )
//            {
//                int gradN = brow0[0] + brow1[0];
//                int gradS = brow1[0] + brow2[0];
//                int gradW = brow1[N-1] + brow1[N];
//                int gradE = brow1[N] + brow1[N+1];
//                int minGrad = std::min(std::min(std::min(gradN, gradS), gradW), gradE);
//                int maxGrad = std::max(std::max(std::max(gradN, gradS), gradW), gradE);
//                int R, G, B;
A
Andrey Morozov 已提交
2222
//
2223 2224 2225 2226 2227 2228
//                if( !greenCell )
//                {
//                    int gradNE = brow0[N4+1] + brow1[N4];
//                    int gradSW = brow1[N4] + brow2[N4-1];
//                    int gradNW = brow0[N5-1] + brow1[N5];
//                    int gradSE = brow1[N5] + brow2[N5+1];
A
Andrey Morozov 已提交
2229
//
2230 2231 2232
//                    minGrad = std::min(std::min(std::min(std::min(minGrad, gradNE), gradSW), gradNW), gradSE);
//                    maxGrad = std::max(std::max(std::max(std::max(maxGrad, gradNE), gradSW), gradNW), gradSE);
//                    int T = minGrad + maxGrad/2;
A
Andrey Morozov 已提交
2233
//
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
//                    int Rs = 0, Gs = 0, Bs = 0, ng = 0;
//                    if( gradN < T )
//                    {
//                        Rs += srow[-bstep*2] + srow[0];
//                        Gs += srow[-bstep]*2;
//                        Bs += srow[-bstep-1] + srow[-bstep+1];
//                        ng++;
//                    }
//                    if( gradS < T )
//                    {
//                        Rs += srow[bstep*2] + srow[0];
//                        Gs += srow[bstep]*2;
//                        Bs += srow[bstep-1] + srow[bstep+1];
//                        ng++;
//                    }
//                    if( gradW < T )
//                    {
//                        Rs += srow[-2] + srow[0];
//                        Gs += srow[-1]*2;
//                        Bs += srow[-bstep-1] + srow[bstep-1];
//                        ng++;
//                    }
//                    if( gradE < T )
//                    {
//                        Rs += srow[2] + srow[0];
//                        Gs += srow[1]*2;
//                        Bs += srow[-bstep+1] + srow[bstep+1];
//                        ng++;
//                    }
//                    if( gradNE < T )
//                    {
//                        Rs += srow[-bstep*2+2] + srow[0];
//                        Gs += brow0[N6+1];
//                        Bs += srow[-bstep+1]*2;
//                        ng++;
//                    }
//                    if( gradSW < T )
//                    {
//                        Rs += srow[bstep*2-2] + srow[0];
//                        Gs += brow2[N6-1];
//                        Bs += srow[bstep-1]*2;
//                        ng++;
//                    }
//                    if( gradNW < T )
//                    {
//                        Rs += srow[-bstep*2-2] + srow[0];
//                        Gs += brow0[N6-1];
//                        Bs += srow[-bstep+1]*2;
//                        ng++;
//                    }
//                    if( gradSE < T )
//                    {
//                        Rs += srow[bstep*2+2] + srow[0];
//                        Gs += brow2[N6+1];
//                        Bs += srow[-bstep+1]*2;
//                        ng++;
//                    }
//                    R = srow[0];
//                    G = R + cvRound((Gs - Rs)*scale[ng]);
A
Andrey Morozov 已提交
2293
//                    B = R + cvRound((Bs - Rs)*scale[ng]);
2294 2295 2296 2297 2298 2299 2300
//                }
//                else
//                {
//                    int gradNE = brow0[N2] + brow0[N2+1] + brow1[N2] + brow1[N2+1];
//                    int gradSW = brow1[N2] + brow1[N2-1] + brow2[N2] + brow2[N2-1];
//                    int gradNW = brow0[N3] + brow0[N3-1] + brow1[N3] + brow1[N3-1];
//                    int gradSE = brow1[N3] + brow1[N3+1] + brow2[N3] + brow2[N3+1];
A
Andrey Morozov 已提交
2301
//
2302 2303 2304
//                    minGrad = std::min(std::min(std::min(std::min(minGrad, gradNE), gradSW), gradNW), gradSE);
//                    maxGrad = std::max(std::max(std::max(std::max(maxGrad, gradNE), gradSW), gradNW), gradSE);
//                    int T = minGrad + maxGrad/2;
A
Andrey Morozov 已提交
2305
//
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
//                    int Rs = 0, Gs = 0, Bs = 0, ng = 0;
//                    if( gradN < T )
//                    {
//                        Rs += srow[-bstep*2-1] + srow[-bstep*2+1];
//                        Gs += srow[-bstep*2] + srow[0];
//                        Bs += srow[-bstep]*2;
//                        ng++;
//                    }
//                    if( gradS < T )
//                    {
//                        Rs += srow[bstep*2-1] + srow[bstep*2+1];
//                        Gs += srow[bstep*2] + srow[0];
//                        Bs += srow[bstep]*2;
//                        ng++;
//                    }
//                    if( gradW < T )
//                    {
//                        Rs += srow[-1]*2;
//                        Gs += srow[-2] + srow[0];
//                        Bs += srow[-bstep-2]+srow[bstep-2];
//                        ng++;
//                    }
//                    if( gradE < T )
//                    {
//                        Rs += srow[1]*2;
//                        Gs += srow[2] + srow[0];
//                        Bs += srow[-bstep+2]+srow[bstep+2];
//                        ng++;
//                    }
//                    if( gradNE < T )
//                    {
//                        Rs += srow[-bstep*2+1] + srow[1];
//                        Gs += srow[-bstep+1]*2;
//                        Bs += srow[-bstep] + srow[-bstep+2];
//                        ng++;
//                    }
//                    if( gradSW < T )
//                    {
//                        Rs += srow[bstep*2-1] + srow[-1];
//                        Gs += srow[bstep-1]*2;
//                        Bs += srow[bstep] + srow[bstep-2];
//                        ng++;
//                    }
//                    if( gradNW < T )
//                    {
//                        Rs += srow[-bstep*2-1] + srow[-1];
//                        Gs += srow[-bstep-1]*2;
//                        Bs += srow[-bstep-2]+srow[-bstep];
//                        ng++;
//                    }
//                    if( gradSE < T )
//                    {
//                        Rs += srow[bstep*2+1] + srow[1];
//                        Gs += srow[bstep+1]*2;
//                        Bs += srow[bstep+2]+srow[bstep];
//                        ng++;
//                    }
//                    G = srow[0];
//                    R = G + cvRound((Rs - Gs)*scale[ng]);
//                    B = G + cvRound((Bs - Gs)*scale[ng]);
//                }
//                dstrow[blueIdx] = CV_CAST_8U(B);
//                dstrow[1] = CV_CAST_8U(G);
//                dstrow[blueIdx^2] = CV_CAST_8U(R);
//                greenCell = !greenCell;
//            }
A
Andrey Morozov 已提交
2372
//
2373 2374 2375
//#if CV_SSE2
//            if( !haveSSE )
//                break;
A
Andrey Morozov 已提交
2376
//
2377 2378 2379 2380
//            __m128i emask = _mm_set1_epi32(0x0000ffff),
//            omask = _mm_set1_epi32(0xffff0000),
//            z = _mm_setzero_si128();
//            __m128 _0_5 = _mm_set1_ps(0.5f);
A
Andrey Morozov 已提交
2381 2382 2383 2384 2385
//
//            #define _mm_merge_epi16(a, b) _mm_or_si128(_mm_and_si128(a, emask), _mm_and_si128(b, omask))
//            #define _mm_cvtloepi16_ps(a)  _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(a,a), 16))
//            #define _mm_cvthiepi16_ps(a)  _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpackhi_epi16(a,a), 16))
//
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
//            // process 8 pixels at once
//            for( ; i <= N - 10; i += 8, srow += 8, brow0 += 8, brow1 += 8, brow2 += 8 )
//            {
//                __m128i gradN, gradS, gradW, gradE, gradNE, gradSW, gradNW, gradSE;
//                gradN = _mm_adds_epu16(_mm_loadu_si128((__m128i*)brow0),
//                                       _mm_loadu_si128((__m128i*)brow1));
//                gradS = _mm_adds_epu16(_mm_loadu_si128((__m128i*)brow1),
//                                       _mm_loadu_si128((__m128i*)brow2));
//                gradW = _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow1+N-1)),
//                                       _mm_loadu_si128((__m128i*)(brow1+N)));
//                gradE = _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow1+N+1)),
//                                       _mm_loadu_si128((__m128i*)(brow1+N)));
A
Andrey Morozov 已提交
2398
//
2399 2400 2401
//                __m128i minGrad, maxGrad, T;
//                minGrad = _mm_min_epi16(_mm_min_epi16(_mm_min_epi16(gradN, gradS), gradW), gradE);
//                maxGrad = _mm_max_epi16(_mm_max_epi16(_mm_max_epi16(gradN, gradS), gradW), gradE);
A
Andrey Morozov 已提交
2402
//
2403
//                __m128i grad0, grad1;
A
Andrey Morozov 已提交
2404
//
2405 2406 2407 2408 2409 2410 2411
//                grad0 = _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow0+N4+1)),
//                                       _mm_loadu_si128((__m128i*)(brow1+N4)));
//                grad1 = _mm_adds_epu16(_mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow0+N2)),
//                                                      _mm_loadu_si128((__m128i*)(brow0+N2+1))),
//                                       _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow1+N2)),
//                                                      _mm_loadu_si128((__m128i*)(brow1+N2+1))));
//                gradNE = _mm_srli_epi16(_mm_merge_epi16(grad0, grad1), 1);
A
Andrey Morozov 已提交
2412
//
2413 2414 2415 2416 2417 2418 2419
//                grad0 = _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow2+N4-1)),
//                                       _mm_loadu_si128((__m128i*)(brow1+N4)));
//                grad1 = _mm_adds_epu16(_mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow2+N2)),
//                                                      _mm_loadu_si128((__m128i*)(brow2+N2-1))),
//                                       _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow1+N2)),
//                                                      _mm_loadu_si128((__m128i*)(brow1+N2-1))));
//                gradSW = _mm_srli_epi16(_mm_merge_epi16(grad0, grad1), 1);
A
Andrey Morozov 已提交
2420
//
2421 2422
//                minGrad = _mm_min_epi16(_mm_min_epi16(minGrad, gradNE), gradSW);
//                maxGrad = _mm_max_epi16(_mm_max_epi16(maxGrad, gradNE), gradSW);
A
Andrey Morozov 已提交
2423
//
2424 2425 2426 2427 2428 2429 2430
//                grad0 = _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow0+N5-1)),
//                                       _mm_loadu_si128((__m128i*)(brow1+N5)));
//                grad1 = _mm_adds_epu16(_mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow0+N3)),
//                                                      _mm_loadu_si128((__m128i*)(brow0+N3-1))),
//                                       _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow1+N3)),
//                                                      _mm_loadu_si128((__m128i*)(brow1+N3-1))));
//                gradNW = _mm_srli_epi16(_mm_merge_epi16(grad0, grad1), 1);
A
Andrey Morozov 已提交
2431
//
2432 2433 2434 2435 2436 2437 2438
//                grad0 = _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow2+N5+1)),
//                                       _mm_loadu_si128((__m128i*)(brow1+N5)));
//                grad1 = _mm_adds_epu16(_mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow2+N3)),
//                                                      _mm_loadu_si128((__m128i*)(brow2+N3+1))),
//                                       _mm_adds_epu16(_mm_loadu_si128((__m128i*)(brow1+N3)),
//                                                      _mm_loadu_si128((__m128i*)(brow1+N3+1))));
//                gradSE = _mm_srli_epi16(_mm_merge_epi16(grad0, grad1), 1);
A
Andrey Morozov 已提交
2439
//
2440 2441
//                minGrad = _mm_min_epi16(_mm_min_epi16(minGrad, gradNW), gradSE);
//                maxGrad = _mm_max_epi16(_mm_max_epi16(maxGrad, gradNW), gradSE);
A
Andrey Morozov 已提交
2442
//
2443 2444
//                T = _mm_add_epi16(_mm_srli_epi16(maxGrad, 1), minGrad);
//                __m128i RGs = z, GRs = z, Bs = z, ng = z, mask;
A
Andrey Morozov 已提交
2445
//
2446 2447
//                __m128i t0, t1, x0, x1, x2, x3, x4, x5, x6, x7, x8,
//                x9, x10, x11, x12, x13, x14, x15, x16;
A
Andrey Morozov 已提交
2448
//
2449
//                x0 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)srow), z);
A
Andrey Morozov 已提交
2450
//
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
//                x1 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep-1)), z);
//                x2 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep*2-1)), z);
//                x3 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep)), z);
//                x4 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep*2+1)), z);
//                x5 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep+1)), z);
//                x6 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep+2)), z);
//                x7 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+1)), z);
//                x8 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep+2)), z);
//                x9 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep+1)), z);
//                x10 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep*2+1)), z);
//                x11 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep)), z);
//                x12 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep*2-1)), z);
//                x13 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep-1)), z);
//                x14 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep-2)), z);
//                x15 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-1)), z);
//                x16 = _mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep-2)), z);
A
Andrey Morozov 已提交
2467
//
2468 2469 2470
//                // gradN
//                mask = _mm_cmpgt_epi16(T, gradN);
//                ng = _mm_sub_epi16(ng, mask);
A
Andrey Morozov 已提交
2471
//
2472 2473
//                t0 = _mm_slli_epi16(x3, 1);
//                t1 = _mm_adds_epu16(_mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep*2)), z), x0);
A
Andrey Morozov 已提交
2474
//
2475 2476 2477
//                RGs = _mm_adds_epu16(RGs, _mm_and_si128(t1, mask));
//                GRs = _mm_adds_epu16(GRs, _mm_and_si128(_mm_merge_epi16(t0, _mm_adds_epu16(x2,x4)), mask));
//                Bs = _mm_adds_epu16(Bs, _mm_and_si128(_mm_merge_epi16(_mm_adds_epu16(x1,x5), t0), mask));
A
Andrey Morozov 已提交
2478
//
2479 2480 2481
//                // gradNE
//                mask = _mm_cmpgt_epi16(T, gradNE);
//                ng = _mm_sub_epi16(ng, mask);
A
Andrey Morozov 已提交
2482
//
2483 2484
//                t0 = _mm_slli_epi16(x5, 1);
//                t1 = _mm_adds_epu16(_mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep*2+2)), z), x0);
A
Andrey Morozov 已提交
2485
//
2486 2487 2488 2489
//                RGs = _mm_adds_epu16(RGs, _mm_and_si128(_mm_merge_epi16(t1, t0), mask));
//                GRs = _mm_adds_epu16(GRs, _mm_and_si128(_mm_merge_epi16(_mm_loadu_si128((__m128i*)(brow0+N6+1)),
//                                                                        _mm_adds_epu16(x4,x7)), mask));
//                Bs = _mm_adds_epu16(Bs, _mm_and_si128(_mm_merge_epi16(t0,_mm_adds_epu16(x3,x6)), mask));
A
Andrey Morozov 已提交
2490
//
2491 2492 2493
//                // gradE
//                mask = _mm_cmpgt_epi16(T, gradE);
//                ng = _mm_sub_epi16(ng, mask);
A
Andrey Morozov 已提交
2494
//
2495 2496
//                t0 = _mm_slli_epi16(x7, 1);
//                t1 = _mm_adds_epu16(_mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+2)), z), x0);
A
Andrey Morozov 已提交
2497
//
2498 2499 2500 2501
//                RGs = _mm_adds_epu16(RGs, _mm_and_si128(t1, mask));
//                GRs = _mm_adds_epu16(GRs, _mm_and_si128(t0, mask));
//                Bs = _mm_adds_epu16(Bs, _mm_and_si128(_mm_merge_epi16(_mm_adds_epu16(x5,x9),
//                                                                      _mm_adds_epu16(x6,x8)), mask));
A
Andrey Morozov 已提交
2502
//
2503 2504 2505
//                // gradSE
//                mask = _mm_cmpgt_epi16(T, gradSE);
//                ng = _mm_sub_epi16(ng, mask);
A
Andrey Morozov 已提交
2506
//
2507 2508
//                t0 = _mm_slli_epi16(x9, 1);
//                t1 = _mm_adds_epu16(_mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep*2+2)), z), x0);
A
Andrey Morozov 已提交
2509
//
2510 2511 2512 2513
//                RGs = _mm_adds_epu16(RGs, _mm_and_si128(_mm_merge_epi16(t1, t0), mask));
//                GRs = _mm_adds_epu16(GRs, _mm_and_si128(_mm_merge_epi16(_mm_loadu_si128((__m128i*)(brow2+N6+1)),
//                                                                        _mm_adds_epu16(x7,x10)), mask));
//                Bs = _mm_adds_epu16(Bs, _mm_and_si128(_mm_merge_epi16(t0, _mm_adds_epu16(x8,x11)), mask));
A
Andrey Morozov 已提交
2514
//
2515 2516 2517
//                // gradS
//                mask = _mm_cmpgt_epi16(T, gradS);
//                ng = _mm_sub_epi16(ng, mask);
A
Andrey Morozov 已提交
2518
//
2519 2520
//                t0 = _mm_slli_epi16(x11, 1);
//                t1 = _mm_adds_epu16(_mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep*2)), z), x0);
A
Andrey Morozov 已提交
2521
//
2522 2523 2524
//                RGs = _mm_adds_epu16(RGs, _mm_and_si128(t1, mask));
//                GRs = _mm_adds_epu16(GRs, _mm_and_si128(_mm_merge_epi16(t0, _mm_adds_epu16(x10,x12)), mask));
//                Bs = _mm_adds_epu16(Bs, _mm_and_si128(_mm_merge_epi16(_mm_adds_epu16(x9,x13), t0), mask));
A
Andrey Morozov 已提交
2525
//
2526 2527 2528
//                // gradSW
//                mask = _mm_cmpgt_epi16(T, gradSW);
//                ng = _mm_sub_epi16(ng, mask);
A
Andrey Morozov 已提交
2529
//
2530 2531
//                t0 = _mm_slli_epi16(x13, 1);
//                t1 = _mm_adds_epu16(_mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow+bstep*2-2)), z), x0);
A
Andrey Morozov 已提交
2532
//
2533 2534 2535 2536
//                RGs = _mm_adds_epu16(RGs, _mm_and_si128(_mm_merge_epi16(t1, t0), mask));
//                GRs = _mm_adds_epu16(GRs, _mm_and_si128(_mm_merge_epi16(_mm_loadu_si128((__m128i*)(brow2+N6-1)),
//                                                                        _mm_adds_epu16(x12,x15)), mask));
//                Bs = _mm_adds_epu16(Bs, _mm_and_si128(_mm_merge_epi16(t0,_mm_adds_epu16(x11,x14)), mask));
A
Andrey Morozov 已提交
2537
//
2538 2539 2540
//                // gradW
//                mask = _mm_cmpgt_epi16(T, gradW);
//                ng = _mm_sub_epi16(ng, mask);
A
Andrey Morozov 已提交
2541
//
2542 2543
//                t0 = _mm_slli_epi16(x15, 1);
//                t1 = _mm_adds_epu16(_mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-2)), z), x0);
A
Andrey Morozov 已提交
2544
//
2545 2546 2547 2548
//                RGs = _mm_adds_epu16(RGs, _mm_and_si128(t1, mask));
//                GRs = _mm_adds_epu16(GRs, _mm_and_si128(t0, mask));
//                Bs = _mm_adds_epu16(Bs, _mm_and_si128(_mm_merge_epi16(_mm_adds_epu16(x1,x13),
//                                                                      _mm_adds_epu16(x14,x16)), mask));
A
Andrey Morozov 已提交
2549
//
2550 2551 2552
//                // gradNW
//                mask = _mm_cmpgt_epi16(T, gradNW);
//                ng = _mm_sub_epi16(ng, mask);
A
Andrey Morozov 已提交
2553
//
2554 2555 2556
//                __m128 ngf0, ngf1;
//                ngf0 = _mm_div_ps(_0_5, _mm_cvtloepi16_ps(ng));
//                ngf1 = _mm_div_ps(_0_5, _mm_cvthiepi16_ps(ng));
A
Andrey Morozov 已提交
2557
//
2558 2559
//                t0 = _mm_slli_epi16(x1, 1);
//                t1 = _mm_adds_epu16(_mm_unpacklo_epi8(_mm_loadl_epi64((__m128i*)(srow-bstep*2-2)), z), x0);
A
Andrey Morozov 已提交
2560
//
2561 2562 2563 2564
//                RGs = _mm_adds_epu16(RGs, _mm_and_si128(_mm_merge_epi16(t1, t0), mask));
//                GRs = _mm_adds_epu16(GRs, _mm_and_si128(_mm_merge_epi16(_mm_loadu_si128((__m128i*)(brow0+N6-1)),
//                                                                        _mm_adds_epu16(x2,x15)), mask));
//                Bs = _mm_adds_epu16(Bs, _mm_and_si128(_mm_merge_epi16(t0,_mm_adds_epu16(x3,x16)), mask));
A
Andrey Morozov 已提交
2565
//
2566 2567 2568
//                // now interpolate r, g & b
//                t0 = _mm_sub_epi16(GRs, RGs);
//                t1 = _mm_sub_epi16(Bs, RGs);
A
Andrey Morozov 已提交
2569
//
2570 2571 2572
//                t0 = _mm_add_epi16(x0, _mm_packs_epi32(
//                                                       _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtloepi16_ps(t0), ngf0)),
//                                                       _mm_cvtps_epi32(_mm_mul_ps(_mm_cvthiepi16_ps(t0), ngf1))));
A
Andrey Morozov 已提交
2573
//
2574 2575 2576
//                t1 = _mm_add_epi16(x0, _mm_packs_epi32(
//                                                       _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtloepi16_ps(t1), ngf0)),
//                                                       _mm_cvtps_epi32(_mm_mul_ps(_mm_cvthiepi16_ps(t1), ngf1))));
A
Andrey Morozov 已提交
2577
//
2578 2579
//                x1 = _mm_merge_epi16(x0, t0);
//                x2 = _mm_merge_epi16(t0, x0);
A
Andrey Morozov 已提交
2580
//
2581
//                uchar R[8], G[8], B[8];
A
Andrey Morozov 已提交
2582
//
2583 2584 2585
//                _mm_storel_epi64(blueIdx ? (__m128i*)B : (__m128i*)R, _mm_packus_epi16(x1, z));
//                _mm_storel_epi64((__m128i*)G, _mm_packus_epi16(x2, z));
//                _mm_storel_epi64(blueIdx ? (__m128i*)R : (__m128i*)B, _mm_packus_epi16(t1, z));
A
Andrey Morozov 已提交
2586
//
2587 2588 2589 2590 2591 2592
//                for( int j = 0; j < 8; j++, dstrow += 3 )
//                {
//                    dstrow[0] = B[j]; dstrow[1] = G[j]; dstrow[2] = R[j];
//                }
//            }
//#endif
A
Andrey Morozov 已提交
2593
//
2594 2595 2596
//            limit = N - 2;
//        }
//        while( i < N - 2 );
A
Andrey Morozov 已提交
2597
//
2598 2599 2600 2601 2602
//        for( i = 0; i < 6; i++ )
//        {
//            dst[dststep*y + 5 - i] = dst[dststep*y + 8 - i];
//            dst[dststep*y + (N - 2)*3 + i] = dst[dststep*y + (N - 3)*3 + i];
//        }
A
Andrey Morozov 已提交
2603
//
2604 2605 2606
//        greenCell0 = !greenCell0;
//        blueIdx ^= 2;
//    }
A
Andrey Morozov 已提交
2607
//
2608 2609 2610 2611 2612 2613 2614 2615 2616
//    for( i = 0; i < size.width*3; i++ )
//    {
//        dst[i] = dst[i + dststep] = dst[i + dststep*2];
//        dst[i + dststep*(size.height-4)] =
//        dst[i + dststep*(size.height-3)] =
//        dst[i + dststep*(size.height-2)] =
//        dst[i + dststep*(size.height-1)] = dst[i + dststep*(size.height-5)];
//    }
//}