color.cu 60.9 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
/*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*/

43 44 45
#include "internal_shared.hpp"
#include "opencv2/gpu/device/saturate_cast.hpp"
#include "opencv2/gpu/device/vecmath.hpp"
46 47

using namespace cv::gpu;
48
using namespace cv::gpu::device;
49 50

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

54
#ifndef FLT_EPSILON
55
    #define FLT_EPSILON     1.192092896e-07F
56 57
#endif

58
namespace cv { namespace gpu { namespace color
A
Andrey Morozov 已提交
59
{
60
    template<typename T> struct ColorChannel {};
61 62 63
    template<> struct ColorChannel<uchar>
    {
        typedef float worktype_f;
64 65
        static __device__ uchar max() { return UCHAR_MAX; }
        static __device__ uchar half() { return (uchar)(max()/2 + 1); }
66
    };
67
    template<> struct ColorChannel<ushort>
68 69
    {
        typedef float worktype_f;
70 71
        static __device__ ushort max() { return USHRT_MAX; }
        static __device__ ushort half() { return (ushort)(max()/2 + 1); }
72 73 74 75 76 77
    };
    template<> struct ColorChannel<float>
    {
        typedef float worktype_f;
        static __device__ float max() { return 1.f; }
        static __device__ float half() { return 0.5f; }
78 79 80
    };

    template <typename T>
81
    __device__ void setAlpha(typename TypeVec<T, 3>::vec_t& vec, T val)
82 83 84
    {
    }
    template <typename T>
85
    __device__ void setAlpha(typename TypeVec<T, 4>::vec_t& vec, T val)
86 87 88
    {
        vec.w = val;
    }
89 90
    template <typename T>
    __device__ T getAlpha(const typename TypeVec<T, 3>::vec_t& vec)
91
    {
92
        return ColorChannel<T>::max();
93
    }
94 95
    template <typename T>
    __device__ T getAlpha(const typename TypeVec<T, 4>::vec_t& vec)
96
    {
97
        return vec.w;
98
    }
A
Andrey Morozov 已提交
99

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

102
    template <int SRCCN, int DSTCN, typename T>
103
    __global__ void RGB2RGB(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
104 105 106
    {
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;
107 108 109 110 111 112

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

        if (y < rows && x < cols)
        {
113
            src_t src = *(const src_t*)(src_ + y * src_step + x * SRCCN * sizeof(T));
114
            dst_t dst;
115

116
            dst.x = (&src.x)[bidx];
117
            dst.y = src.y;
118
            dst.z = (&src.x)[bidx ^ 2];
119
            setAlpha(dst, getAlpha<T>(src));
120
            
121
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
A
Andrey Morozov 已提交
122
        }
123
    }
124

125 126
    template <typename T, int SRCCN, int DSTCN>
    void RGB2RGB_caller(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream)
A
Andrey Morozov 已提交
127
    {
128 129 130 131 132 133
        dim3 threads(32, 8, 1);
        dim3 grid(1, 1, 1);

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

134 135
        RGB2RGB<SRCCN, DSTCN, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols, bidx);
136 137 138 139 140

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

141
    void RGB2RGB_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, cudaStream_t stream)
142
    {
143 144 145 146 147 148 149 150
        typedef void (*RGB2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
        static const RGB2RGB_caller_t RGB2RGB_callers[2][2] = 
        {
            {RGB2RGB_caller<uchar, 3, 3>, RGB2RGB_caller<uchar, 3, 4>}, 
            {RGB2RGB_caller<uchar, 4, 3>, RGB2RGB_caller<uchar, 4, 4>}
        };

        RGB2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
151 152
    }

153
    void RGB2RGB_gpu_16u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, cudaStream_t stream)
154
    {
155 156 157
        typedef void (*RGB2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
        static const RGB2RGB_caller_t RGB2RGB_callers[2][2] = 
        {
158 159
            {RGB2RGB_caller<ushort, 3, 3>, RGB2RGB_caller<ushort, 3, 4>}, 
            {RGB2RGB_caller<ushort, 4, 3>, RGB2RGB_caller<ushort, 4, 4>}
160 161 162
        };

        RGB2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
163 164
    }

165
    void RGB2RGB_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, cudaStream_t stream)
166
    {
167 168 169 170 171 172 173 174
        typedef void (*RGB2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
        static const RGB2RGB_caller_t RGB2RGB_callers[2][2] = 
        {
            {RGB2RGB_caller<float, 3, 3>, RGB2RGB_caller<float, 3, 4>}, 
            {RGB2RGB_caller<float, 4, 3>, RGB2RGB_caller<float, 4, 4>}
        };

        RGB2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
175
    }
A
Andrey Morozov 已提交
176

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

179
    template <int GREEN_BITS, int DSTCN> struct RGB5x52RGBConverter {};    
180
    template <int DSTCN> struct RGB5x52RGBConverter<5, DSTCN>
181
    {
182 183
        typedef typename TypeVec<uchar, DSTCN>::vec_t dst_t;

184
        static __device__ dst_t cvt(uint src, int bidx)
185 186 187
        {
            dst_t dst;
            
188
            (&dst.x)[bidx] = (uchar)(src << 3);
189
            dst.y = (uchar)((src >> 2) & ~7);
190
            (&dst.x)[bidx ^ 2] = (uchar)((src >> 7) & ~7);
191
            setAlpha(dst, (uchar)(src & 0x8000 ? 255 : 0));
192 193 194 195 196 197 198 199

            return dst;
        }
    };
    template <int DSTCN> struct RGB5x52RGBConverter<6, DSTCN>
    {
        typedef typename TypeVec<uchar, DSTCN>::vec_t dst_t;

200
        static __device__ dst_t cvt(uint src, int bidx)
201 202 203
        {
            dst_t dst;
            
204
            (&dst.x)[bidx] = (uchar)(src << 3);
205
            dst.y = (uchar)((src >> 3) & ~3);
206
            (&dst.x)[bidx ^ 2] = (uchar)((src >> 8) & ~7);
207
            setAlpha(dst, (uchar)(255));
208 209 210 211 212 213 214 215 216 217

            return dst;
        }
    };

    template <int GREEN_BITS, int DSTCN>
    __global__ void RGB5x52RGB(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        typedef typename TypeVec<uchar, DSTCN>::vec_t dst_t;

218 219
        const int x = blockDim.x * blockIdx.x + threadIdx.x;
        const int y = blockDim.y * blockIdx.y + threadIdx.y;
220 221 222

        if (y < rows && x < cols)
        {
223
            uint src = *(const ushort*)(src_ + y * src_step + (x << 1));
224 225
            
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN) = RGB5x52RGBConverter<GREEN_BITS, DSTCN>::cvt(src, bidx);
226 227 228
        }
    }

229 230
    template <int SRCCN, int GREEN_BITS> struct RGB2RGB5x5Converter {};
    template<int SRCCN> struct RGB2RGB5x5Converter<SRCCN, 6> 
231
    {
232
        static __device__ ushort cvt(const uchar* src, int bidx)
233
        {
234
            return (ushort)((src[bidx] >> 3) | ((src[1] & ~3) << 3) | ((src[bidx^2] & ~7) << 8));
235 236 237 238
        }
    };
    template<> struct RGB2RGB5x5Converter<3, 5> 
    {
239
        static __device__ ushort cvt(const uchar* src, int bidx)
240
        {
241
            return (ushort)((src[bidx] >> 3) | ((src[1] & ~7) << 2) | ((src[bidx^2] & ~7) << 7));
242 243 244 245
        }
    };
    template<> struct RGB2RGB5x5Converter<4, 5> 
    {
246
        static __device__ ushort cvt(const uchar* src, int bidx)
247
        {
248
            return (ushort)((src[bidx] >> 3) | ((src[1] & ~7) << 2) | ((src[bidx^2] & ~7) << 7) | (src[3] ? 0x8000 : 0));
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        }
    };    

    template<int SRCCN, int GREEN_BITS>
    __global__ void RGB2RGB5x5(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        typedef typename TypeVec<uchar, SRCCN>::vec_t src_t;

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

        if (y < rows && x < cols)
        {
            src_t src = *(src_t*)(src_ + y * src_step + x * SRCCN);

264
            *(ushort*)(dst_ + y * dst_step + (x << 1)) = RGB2RGB5x5Converter<SRCCN, GREEN_BITS>::cvt(&src.x, bidx);
265 266 267 268 269 270 271 272 273 274 275 276
        }
    }

    template <int GREEN_BITS, int DSTCN>
    void RGB5x52RGB_caller(const DevMem2D& src, 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);
        grid.y = divUp(src.rows, threads.y);

277 278
        RGB5x52RGB<GREEN_BITS, DSTCN><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols, bidx);
279 280 281 282 283 284 285 286 287 288 289 290 291 292

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

    void RGB5x52RGB_gpu(const DevMem2D& src, int green_bits, const DevMem2D& dst, int dstcn, int bidx, cudaStream_t stream)
    {
        typedef void (*RGB5x52RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
        static const RGB5x52RGB_caller_t RGB5x52RGB_callers[2][2] = 
        {
            {RGB5x52RGB_caller<5, 3>, RGB5x52RGB_caller<5, 4>},
            {RGB5x52RGB_caller<6, 3>, RGB5x52RGB_caller<6, 4>}
        };

293
        RGB5x52RGB_callers[green_bits - 5][dstcn - 3](src, dst, bidx, stream);
294 295 296 297 298 299 300 301 302 303 304
    }

    template <int SRCCN, int GREEN_BITS>
    void RGB2RGB5x5_caller(const DevMem2D& src, 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);
        grid.y = divUp(src.rows, threads.y);

305 306
        RGB2RGB5x5<SRCCN, GREEN_BITS><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols, bidx);
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

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

    void RGB2RGB5x5_gpu(const DevMem2D& src, int srccn, const DevMem2D& dst, int green_bits, int bidx, cudaStream_t stream)
    {
        typedef void (*RGB2RGB5x5_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
        static const RGB2RGB5x5_caller_t RGB2RGB5x5_callers[2][2] = 
        {
            {RGB2RGB5x5_caller<3, 5>, RGB2RGB5x5_caller<3, 6>},
            {RGB2RGB5x5_caller<4, 5>, RGB2RGB5x5_caller<4, 6>}
        };

        RGB2RGB5x5_callers[srccn - 3][green_bits - 5](src, dst, bidx, stream);
    }

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

    template <int DSTCN, typename T>
327
    __global__ void Gray2RGB(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols)
328 329
    {
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;
330 331 332 333 334 335

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

        if (y < rows && x < cols)
        {
336
            T src = *(const T*)(src_ + y * src_step + x * sizeof(T));
337
            dst_t dst;
338 339 340
            dst.x = src;
            dst.y = src;
            dst.z = src;
341
            setAlpha(dst, ColorChannel<T>::max());
342
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
343 344 345
        }
    }

346 347 348
    template <int GREEN_BITS> struct Gray2RGB5x5Converter {};
    template<> struct Gray2RGB5x5Converter<6> 
    {
349
        static __device__ ushort cvt(uint t)
350
        {
351
            return (ushort)((t >> 3) | ((t & ~3) << 3) | ((t & ~7) << 8));
352 353 354 355
        }
    };
    template<> struct Gray2RGB5x5Converter<5> 
    {
356
        static __device__ ushort cvt(uint t)
357 358
        {
            t >>= 3;
359
            return (ushort)(t | (t << 5) | (t << 10));
360
        }
361
    };
362 363 364 365 366 367 368 369 370

    template<int GREEN_BITS>
    __global__ void Gray2RGB5x5(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols)
    {
        const int x = blockDim.x * blockIdx.x + threadIdx.x;
        const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
371
            uint src = src_[y * src_step + x];
372

373
            *(ushort*)(dst_ + y * dst_step + (x << 1)) = Gray2RGB5x5Converter<GREEN_BITS>::cvt(src);
374 375
        }
    }
376

377
    template <typename T, int DSTCN>
378
    void Gray2RGB_caller(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream)
379 380 381 382 383 384 385
    {
        dim3 threads(32, 8, 1);
        dim3 grid(1, 1, 1);

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

386 387
        Gray2RGB<DSTCN, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols);
388 389 390 391 392

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

393
    void Gray2RGB_gpu_8u(const DevMem2D& src, const DevMem2D& dst, int dstcn, cudaStream_t stream)
394
    {
395 396 397 398
        typedef void (*Gray2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
        static const Gray2RGB_caller_t Gray2RGB_callers[] = {Gray2RGB_caller<uchar, 3>, Gray2RGB_caller<uchar, 4>};

        Gray2RGB_callers[dstcn - 3](src, dst, stream);
399 400
    }

401
    void Gray2RGB_gpu_16u(const DevMem2D& src, const DevMem2D& dst, int dstcn, cudaStream_t stream)
402
    {
403
        typedef void (*Gray2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
404
        static const Gray2RGB_caller_t Gray2RGB_callers[] = {Gray2RGB_caller<ushort, 3>, Gray2RGB_caller<ushort, 4>};
405 406

        Gray2RGB_callers[dstcn - 3](src, dst, stream);
407 408
    }

409
    void Gray2RGB_gpu_32f(const DevMem2D& src, const DevMem2D& dst, int dstcn, cudaStream_t stream)
410
    {
411
        typedef void (*Gray2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
412 413 414
        static const Gray2RGB_caller_t Gray2RGB_callers[] = {Gray2RGB_caller<float, 3>, Gray2RGB_caller<float, 4>};

        Gray2RGB_callers[dstcn - 3](src, dst, stream);
415
    }
416 417 418 419 420 421 422 423 424 425

    template <int GREEN_BITS>
    void Gray2RGB5x5_caller(const DevMem2D& src, const DevMem2D& dst, 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);

426 427
        Gray2RGB5x5<GREEN_BITS><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

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

    void Gray2RGB5x5_gpu(const DevMem2D& src, const DevMem2D& dst, int green_bits, cudaStream_t stream)
    {
        typedef void (*Gray2RGB5x5_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
        static const Gray2RGB5x5_caller_t Gray2RGB5x5_callers[2] = 
        {
            Gray2RGB5x5_caller<5>, Gray2RGB5x5_caller<6>
        };

        Gray2RGB5x5_callers[green_bits - 5](src, dst, stream);
    }
A
Andrey Morozov 已提交
443

444 445
///////////////////////////////// Color to Grayscale ////////////////////////////////

446 447 448 449 450 451 452 453 454 455 456 457 458
    #undef R2Y
    #undef G2Y
    #undef B2Y
    
    enum
    {
        yuv_shift  = 14,
        xyz_shift  = 12,
        R2Y        = 4899,
        G2Y        = 9617,
        B2Y        = 1868,
        BLOCK_SIZE = 256
    };
459

460 461 462
    template <int GREEN_BITS> struct RGB5x52GrayConverter {};
    template<> struct RGB5x52GrayConverter<6> 
    {
463
        static __device__ uchar cvt(uint t)
464
        {
465
            return (uchar)CV_DESCALE(((t << 3) & 0xf8) * B2Y + ((t >> 3) & 0xfc) * G2Y + ((t >> 8) & 0xf8) * R2Y, yuv_shift);
466 467 468
        }
    };
    template<> struct RGB5x52GrayConverter<5> 
469
    {
470
        static __device__ uchar cvt(uint t)
471
        {
472
            return (uchar)CV_DESCALE(((t << 3) & 0xf8) * B2Y + ((t >> 2) & 0xf8) * G2Y + ((t >> 7) & 0xf8) * R2Y, yuv_shift);
473 474
        }
    };   
A
Andrey Morozov 已提交
475

476 477 478 479 480 481 482 483
    template<int GREEN_BITS>
    __global__ void RGB5x52Gray(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols)
    {
        const int x = blockDim.x * blockIdx.x + threadIdx.x;
        const int y = blockDim.y * blockIdx.y + threadIdx.y;

        if (y < rows && x < cols)
        {
484
            uint src = *(ushort*)(src_ + y * src_step + (x << 1));
485 486 487 488 489

            dst_[y * dst_step + x] = RGB5x52GrayConverter<GREEN_BITS>::cvt(src);
        }
    }

490
    template <typename T> struct RGB2GrayConvertor 
491
    {
492
        static __device__ T cvt(const T* src, int bidx)
493
        {
494
            return (T)CV_DESCALE((unsigned)(src[bidx] * B2Y + src[1] * G2Y + src[bidx^2] * R2Y), yuv_shift);
495
        }
496 497
    };
    template <> struct RGB2GrayConvertor<float> 
498
    {
499
        static __device__ float cvt(const float* src, int bidx)
500
        {
501 502 503
            const float cr = 0.299f;
            const float cg = 0.587f;
            const float cb = 0.114f;
504

505
            return src[bidx] * cb + src[1] * cg + src[bidx^2] * cr;
506
        }
507
    };
508

509 510
    template <int SRCCN, typename T>
    __global__ void RGB2Gray(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
511
    {
512
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
A
Andrey Morozov 已提交
513

514 515
        const int x = blockDim.x * blockIdx.x + threadIdx.x;
        const int y = blockDim.y * blockIdx.y + threadIdx.y;
516 517 518

        if (y < rows && x < cols)
        {
519
            src_t src = *(const src_t*)(src_ + y * src_step + x * SRCCN * sizeof(T));
A
Andrey Morozov 已提交
520

521
            *(T*)(dst_ + y * dst_step + x * sizeof(T)) = RGB2GrayConvertor<T>::cvt(&src.x, bidx);
522
        }
523
    }   
524

525 526
    template <typename T, int SRCCN>
    void RGB2Gray_caller(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream)
527 528 529 530
    {
        dim3 threads(32, 8, 1);
        dim3 grid(1, 1, 1);

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

534 535
        RGB2Gray<SRCCN, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols, bidx);
536 537 538 539 540

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

541
    void RGB2Gray_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int bidx, cudaStream_t stream)
542
    {
543
        typedef void (*RGB2Gray_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
544
        RGB2Gray_caller_t RGB2Gray_callers[] = {RGB2Gray_caller<uchar, 3>, RGB2Gray_caller<uchar, 4>};
545

546
        RGB2Gray_callers[srccn - 3](src, dst, bidx, stream);
547 548
    }

549
    void RGB2Gray_gpu_16u(const DevMem2D& src, int srccn, const DevMem2D& dst, int bidx, cudaStream_t stream)
550
    {
551
        typedef void (*RGB2Gray_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
552
        RGB2Gray_caller_t RGB2Gray_callers[] = {RGB2Gray_caller<ushort, 3>, RGB2Gray_caller<ushort, 4>};
553

554 555
        RGB2Gray_callers[srccn - 3](src, dst, bidx, stream);
    }
556

557 558 559 560
    void RGB2Gray_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int bidx, cudaStream_t stream)
    {
        typedef void (*RGB2Gray_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
        RGB2Gray_caller_t RGB2Gray_callers[] = {RGB2Gray_caller<float, 3>, RGB2Gray_caller<float, 4>};
561

562 563
        RGB2Gray_callers[srccn - 3](src, dst, bidx, stream);
    }    
564 565 566 567 568 569 570 571 572 573

    template <int GREEN_BITS>
    void RGB5x52Gray_caller(const DevMem2D& src, const DevMem2D& dst, 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);

574 575
        RGB5x52Gray<GREEN_BITS><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols);
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590

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

    void RGB5x52Gray_gpu(const DevMem2D& src, int green_bits, const DevMem2D& dst, cudaStream_t stream)
    {
        typedef void (*RGB5x52Gray_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
        static const RGB5x52Gray_caller_t RGB5x52Gray_callers[2] = 
        {
            RGB5x52Gray_caller<5>, RGB5x52Gray_caller<6>
        };

        RGB5x52Gray_callers[green_bits - 5](src, dst, stream);
    }
A
Andrey Morozov 已提交
591

592 593
///////////////////////////////////// RGB <-> YCrCb //////////////////////////////////////

594 595 596 597 598
    __constant__ float cYCrCbCoeffs_f[5];
    __constant__ int cYCrCbCoeffs_i[5];

    template <typename T> struct RGB2YCrCbConverter 
    {
599 600
        template <typename D>
        static __device__ void cvt(const T* src, D& dst, int bidx)
601 602 603 604 605 606 607 608 609 610 611 612 613 614
        {
            const int delta = ColorChannel<T>::half() * (1 << yuv_shift);

            const int Y = CV_DESCALE(src[0] * cYCrCbCoeffs_i[0] + src[1] * cYCrCbCoeffs_i[1] + src[2] * cYCrCbCoeffs_i[2], yuv_shift);
            const int Cr = CV_DESCALE((src[bidx^2] - Y) * cYCrCbCoeffs_i[3] + delta, yuv_shift);
            const int Cb = CV_DESCALE((src[bidx] - Y) * cYCrCbCoeffs_i[4] + delta, yuv_shift);

            dst.x = saturate_cast<T>(Y);
            dst.y = saturate_cast<T>(Cr);
            dst.z = saturate_cast<T>(Cb);
        }
    };
    template<> struct RGB2YCrCbConverter<float>
    {
615 616
        template <typename D>
        static __device__ void cvt(const float* src, D& dst, int bidx)
617 618 619 620 621 622 623
        {
            dst.x = src[0] * cYCrCbCoeffs_f[0] + src[1] * cYCrCbCoeffs_f[1] + src[2] * cYCrCbCoeffs_f[2];
            dst.y = (src[bidx^2] - dst.x) * cYCrCbCoeffs_f[3] + ColorChannel<float>::half();
            dst.z = (src[bidx] - dst.x) * cYCrCbCoeffs_f[4] + ColorChannel<float>::half();
        }
    };

624
    template <int SRCCN, int DSTCN, typename T>
625 626 627
    __global__ void RGB2YCrCb(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
628
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;
629 630 631 632 633 634

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

        if (y < rows && x < cols)
        {
635
            src_t src = *(const src_t*)(src_ + y * src_step + x * SRCCN * sizeof(T));
636 637
            dst_t dst;

638
            RGB2YCrCbConverter<T>::cvt(&src.x, dst, bidx);
639
            
640
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
641 642 643
        }
    }

644
    template <typename D> struct YCrCb2RGBConvertor
645
    {
646 647
        template <typename T>
        static __device__ void cvt(const T& src, D* dst, int bidx)
648
        {
649 650 651
            const int b = src.x + CV_DESCALE((src.z - ColorChannel<D>::half()) * cYCrCbCoeffs_i[3], yuv_shift);
            const int g = src.x + CV_DESCALE((src.z - ColorChannel<D>::half()) * cYCrCbCoeffs_i[2] + (src.y - ColorChannel<D>::half()) * cYCrCbCoeffs_i[1], yuv_shift);
            const int r = src.x + CV_DESCALE((src.y - ColorChannel<D>::half()) * cYCrCbCoeffs_i[0], yuv_shift);
652

653 654 655
            dst[bidx] = saturate_cast<D>(b);
            dst[1] = saturate_cast<D>(g);
            dst[bidx^2] = saturate_cast<D>(r);
656 657 658 659
        }
    };
    template <> struct YCrCb2RGBConvertor<float>
    {
660 661
        template <typename T>
        static __device__ void cvt(const T& src, float* dst, int bidx)
662 663 664 665 666 667 668
        {
            dst[bidx] = src.x + (src.z - ColorChannel<float>::half()) * cYCrCbCoeffs_f[3];
            dst[1] = src.x + (src.z - ColorChannel<float>::half()) * cYCrCbCoeffs_f[2] + (src.y - ColorChannel<float>::half()) * cYCrCbCoeffs_f[1];
            dst[bidx^2] = src.x + (src.y - ColorChannel<float>::half()) * cYCrCbCoeffs_f[0];
        }
    };

669
    template <int SRCCN, int DSTCN, typename T>
670 671
    __global__ void YCrCb2RGB(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
672
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
673 674 675 676 677 678 679
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;

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

        if (y < rows && x < cols)
        {
680
            src_t src = *(const src_t*)(src_ + y * src_step + x * SRCCN * sizeof(T));
681 682
            dst_t dst;

683
            YCrCb2RGBConvertor<T>::cvt(src, &dst.x, bidx);
684 685
            setAlpha(dst, ColorChannel<T>::max());
            
686
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
687 688 689
        }
    }

690
    template <typename T, int SRCCN, int DSTCN>
691 692 693 694 695 696 697 698
    void RGB2YCrCb_caller(const DevMem2D& src, 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);
        grid.y = divUp(src.rows, threads.y);

699 700
        RGB2YCrCb<SRCCN, DSTCN, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols, bidx);
701 702 703 704 705

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

706
    void RGB2YCrCb_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, const void* coeffs, cudaStream_t stream)
707 708
    {
        typedef void (*RGB2YCrCb_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
709
        static const RGB2YCrCb_caller_t RGB2YCrCb_callers[2][2] = 
710
        {
711 712
            {RGB2YCrCb_caller<uchar, 3, 3>, RGB2YCrCb_caller<uchar, 3, 4>},
            {RGB2YCrCb_caller<uchar, 4, 3>, RGB2YCrCb_caller<uchar, 4, 4>}
713 714
        };

715
        cudaSafeCall( cudaMemcpyToSymbol(cYCrCbCoeffs_i, coeffs, 5 * sizeof(int)) );
716

717
        RGB2YCrCb_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
718 719
    }

720
    void RGB2YCrCb_gpu_16u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, const void* coeffs, cudaStream_t stream)
721 722
    {
        typedef void (*RGB2YCrCb_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
723
        static const RGB2YCrCb_caller_t RGB2YCrCb_callers[2][2] = 
724
        {
725 726
            {RGB2YCrCb_caller<ushort, 3, 3>, RGB2YCrCb_caller<ushort, 3, 4>},
            {RGB2YCrCb_caller<ushort, 4, 3>, RGB2YCrCb_caller<ushort, 4, 4>}
727 728
        };
        
729
        cudaSafeCall( cudaMemcpyToSymbol(cYCrCbCoeffs_i, coeffs, 5 * sizeof(int)) );
730

731
        RGB2YCrCb_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
732 733
    }

734
    void RGB2YCrCb_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, const void* coeffs, cudaStream_t stream)
735 736
    {
        typedef void (*RGB2YCrCb_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
737
        static const RGB2YCrCb_caller_t RGB2YCrCb_callers[2][2] = 
738
        {
739 740
            {RGB2YCrCb_caller<float, 3, 3>, RGB2YCrCb_caller<float, 3, 4>},
            {RGB2YCrCb_caller<float, 4, 3>, RGB2YCrCb_caller<float, 4, 4>}
741 742
        };
        
743
        cudaSafeCall( cudaMemcpyToSymbol(cYCrCbCoeffs_f, coeffs, 5 * sizeof(float)) );
744

745
        RGB2YCrCb_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
746 747
    }
    
748
    template <typename T, int SRCCN, int DSTCN>
749 750 751 752 753 754 755 756
    void YCrCb2RGB_caller(const DevMem2D& src, 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);
        grid.y = divUp(src.rows, threads.y);

757 758
        YCrCb2RGB<SRCCN, DSTCN, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols, bidx);
759 760 761 762 763

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

764
    void YCrCb2RGB_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, const void* coeffs, cudaStream_t stream)
765 766
    {
        typedef void (*YCrCb2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
767
        static const YCrCb2RGB_caller_t YCrCb2RGB_callers[2][2] = 
768
        {
769 770
            {YCrCb2RGB_caller<uchar, 3, 3>, YCrCb2RGB_caller<uchar, 3, 4>},
            {YCrCb2RGB_caller<uchar, 4, 3>, YCrCb2RGB_caller<uchar, 4, 4>}
771 772
        };

773
        cudaSafeCall( cudaMemcpyToSymbol(cYCrCbCoeffs_i, coeffs, 4 * sizeof(int)) );
774

775
        YCrCb2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
776 777
    }

778
    void YCrCb2RGB_gpu_16u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, const void* coeffs, cudaStream_t stream)
779 780
    {
        typedef void (*YCrCb2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
781
        static const YCrCb2RGB_caller_t YCrCb2RGB_callers[2][2] = 
782
        {
783 784
            {YCrCb2RGB_caller<ushort, 3, 3>, YCrCb2RGB_caller<ushort, 3, 4>},
            {YCrCb2RGB_caller<ushort, 4, 3>, YCrCb2RGB_caller<ushort, 4, 4>}
785 786
        };
        
787
        cudaSafeCall( cudaMemcpyToSymbol(cYCrCbCoeffs_i, coeffs, 4 * sizeof(int)) );
788

789
        YCrCb2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
790 791
    }

792
    void YCrCb2RGB_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, const void* coeffs, cudaStream_t stream)
793 794
    {
        typedef void (*YCrCb2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, cudaStream_t stream);
795
        static const YCrCb2RGB_caller_t YCrCb2RGB_callers[2][2] = 
796
        {
797 798
            {YCrCb2RGB_caller<float, 3, 3>, YCrCb2RGB_caller<float, 3, 4>},
            {YCrCb2RGB_caller<float, 4, 3>, YCrCb2RGB_caller<float, 4, 4>}
799 800
        };
        
801
        cudaSafeCall( cudaMemcpyToSymbol(cYCrCbCoeffs_f, coeffs, 4 * sizeof(float)) );
802

803
        YCrCb2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, stream);
804
    }
A
Andrey Morozov 已提交
805

806 807
////////////////////////////////////// RGB <-> XYZ ///////////////////////////////////////

808 809 810 811 812
    __constant__ float cXYZ_D65f[9];
    __constant__ int cXYZ_D65i[9];

    template <typename T> struct RGB2XYZConvertor
    {
813 814
        template <typename D>
        static __device__ void cvt(const T* src, D& dst)
815 816 817 818 819 820 821 822
        {
	        dst.x = saturate_cast<T>(CV_DESCALE(src[0] * cXYZ_D65i[0] + src[1] * cXYZ_D65i[1] + src[2] * cXYZ_D65i[2], xyz_shift));
	        dst.y = saturate_cast<T>(CV_DESCALE(src[0] * cXYZ_D65i[3] + src[1] * cXYZ_D65i[4] + src[2] * cXYZ_D65i[5], xyz_shift));
	        dst.z = saturate_cast<T>(CV_DESCALE(src[0] * cXYZ_D65i[6] + src[1] * cXYZ_D65i[7] + src[2] * cXYZ_D65i[8], xyz_shift));
        }
    };
    template <> struct RGB2XYZConvertor<float>
    {
823 824
        template <typename D>
        static __device__ void cvt(const float* src, D& dst)
825 826 827 828 829 830 831
        {
	        dst.x = src[0] * cXYZ_D65f[0] + src[1] * cXYZ_D65f[1] + src[2] * cXYZ_D65f[2];
	        dst.y = src[0] * cXYZ_D65f[3] + src[1] * cXYZ_D65f[4] + src[2] * cXYZ_D65f[5];
	        dst.z = src[0] * cXYZ_D65f[6] + src[1] * cXYZ_D65f[7] + src[2] * cXYZ_D65f[8];
        }
    };

832
    template <int SRCCN, int DSTCN, typename T>
833 834 835
    __global__ void RGB2XYZ(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols)
    {
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
836
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;
837 838 839 840 841 842 843

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

        if (y < rows && x < cols)
        {
            src_t src = *(const src_t*)(src_ + y * src_step + x * SRCCN * sizeof(T));
844 845

            dst_t dst;
846
            RGB2XYZConvertor<T>::cvt(&src.x, dst);
847
            
848
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
849 850 851
        }
    }

852
    template <typename D> struct XYZ2RGBConvertor
853
    {
854 855
        template <typename T>
        static __device__ void cvt(const T& src, D* dst)
856
        {
857 858 859
            dst[0] = saturate_cast<D>(CV_DESCALE(src.x * cXYZ_D65i[0] + src.y * cXYZ_D65i[1] + src.z * cXYZ_D65i[2], xyz_shift));
		    dst[1] = saturate_cast<D>(CV_DESCALE(src.x * cXYZ_D65i[3] + src.y * cXYZ_D65i[4] + src.z * cXYZ_D65i[5], xyz_shift));
		    dst[2] = saturate_cast<D>(CV_DESCALE(src.x * cXYZ_D65i[6] + src.y * cXYZ_D65i[7] + src.z * cXYZ_D65i[8], xyz_shift));
860 861 862 863
        }
    };
    template <> struct XYZ2RGBConvertor<float>
    {
864 865
        template <typename T>
        static __device__ void cvt(const T& src, float* dst)
866 867 868 869 870 871 872
        {
            dst[0] = src.x * cXYZ_D65f[0] + src.y * cXYZ_D65f[1] + src.z * cXYZ_D65f[2];
		    dst[1] = src.x * cXYZ_D65f[3] + src.y * cXYZ_D65f[4] + src.z * cXYZ_D65f[5];
		    dst[2] = src.x * cXYZ_D65f[6] + src.y * cXYZ_D65f[7] + src.z * cXYZ_D65f[8];
        }
    };

873
    template <int SRCCN, int DSTCN, typename T>
874 875
    __global__ void XYZ2RGB(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols)
    {
876
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
877 878 879 880 881 882 883
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;

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

        if (y < rows && x < cols)
        {
884
            src_t src = *(const src_t*)(src_ + y * src_step + x * SRCCN * sizeof(T));
885 886

            dst_t dst;
887
            XYZ2RGBConvertor<T>::cvt(src, &dst.x);
888 889 890 891 892 893
            setAlpha(dst, ColorChannel<T>::max());
            
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
        }
    }

894
    template <typename T, int SRCCN, int DSTCN>
895 896 897 898 899 900 901 902
    void RGB2XYZ_caller(const DevMem2D& src, const DevMem2D& dst, 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);

903 904
        RGB2XYZ<SRCCN, DSTCN, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols);
905 906 907 908 909

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

910
    void RGB2XYZ_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, const void* coeffs, cudaStream_t stream)
911 912
    {
        typedef void (*RGB2XYZ_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
913 914 915 916 917
        static const RGB2XYZ_caller_t RGB2XYZ_callers[2][2] = 
        {
            {RGB2XYZ_caller<uchar, 3, 3>, RGB2XYZ_caller<uchar, 3, 4>},
            {RGB2XYZ_caller<uchar, 4, 3>, RGB2XYZ_caller<uchar, 4, 4>}
        };
918

919
        cudaSafeCall( cudaMemcpyToSymbol(cXYZ_D65i, coeffs, 9 * sizeof(int)) );
920

921
        RGB2XYZ_callers[srccn-3][dstcn-3](src, dst, stream);
922 923
    }

924
    void RGB2XYZ_gpu_16u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, const void* coeffs, cudaStream_t stream)
925 926
    {
        typedef void (*RGB2XYZ_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
927 928
        static const RGB2XYZ_caller_t RGB2XYZ_callers[2][2] = 
        {
929 930
            {RGB2XYZ_caller<ushort, 3, 3>, RGB2XYZ_caller<ushort, 3, 4>},
            {RGB2XYZ_caller<ushort, 4, 3>, RGB2XYZ_caller<ushort, 4, 4>}
931
        };
932
        
933
        cudaSafeCall( cudaMemcpyToSymbol(cXYZ_D65i, coeffs, 9 * sizeof(int)) );
934

935
        RGB2XYZ_callers[srccn-3][dstcn-3](src, dst, stream);
936 937
    }

938
    void RGB2XYZ_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, const void* coeffs, cudaStream_t stream)
939 940
    {
        typedef void (*RGB2XYZ_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
941 942 943 944 945
        static const RGB2XYZ_caller_t RGB2XYZ_callers[2][2] = 
        {
            {RGB2XYZ_caller<float, 3, 3>, RGB2XYZ_caller<float, 3, 4>},
            {RGB2XYZ_caller<float, 4, 3>, RGB2XYZ_caller<float, 4, 4>}
        };
946
        
947
        cudaSafeCall( cudaMemcpyToSymbol(cXYZ_D65f, coeffs, 9 * sizeof(float)) );
948

949
        RGB2XYZ_callers[srccn-3][dstcn-3](src, dst, stream);
950 951
    }
    
952
    template <typename T, int SRCCN, int DSTCN>
953 954 955 956 957 958 959 960
    void XYZ2RGB_caller(const DevMem2D& src, const DevMem2D& dst, 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);

961 962
        XYZ2RGB<SRCCN, DSTCN, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
            dst.data, dst.step, src.rows, src.cols);
963 964 965 966 967

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

968
    void XYZ2RGB_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, const void* coeffs, cudaStream_t stream)
969 970
    {
        typedef void (*XYZ2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
971 972 973 974 975
        static const XYZ2RGB_caller_t XYZ2RGB_callers[2][2] = 
        {
            {XYZ2RGB_caller<uchar, 3, 3>, XYZ2RGB_caller<uchar, 3, 4>},
            {XYZ2RGB_caller<uchar, 4, 3>, XYZ2RGB_caller<uchar, 4, 4>}
        };
976

977
        cudaSafeCall( cudaMemcpyToSymbol(cXYZ_D65i, coeffs, 9 * sizeof(int)) );
978

979
        XYZ2RGB_callers[srccn-3][dstcn-3](src, dst, stream);
980 981
    }

982
    void XYZ2RGB_gpu_16u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, const void* coeffs, cudaStream_t stream)
983 984
    {
        typedef void (*XYZ2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
985 986
        static const XYZ2RGB_caller_t XYZ2RGB_callers[2][2] = 
        {
987 988
            {XYZ2RGB_caller<ushort, 3, 3>, XYZ2RGB_caller<ushort, 3, 4>},
            {XYZ2RGB_caller<ushort, 4, 3>, XYZ2RGB_caller<ushort, 4, 4>}
989
        };
990
        
991
        cudaSafeCall( cudaMemcpyToSymbol(cXYZ_D65i, coeffs, 9 * sizeof(int)) );
992

993
        XYZ2RGB_callers[srccn-3][dstcn-3](src, dst, stream);
994 995
    }

996
    void XYZ2RGB_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, const void* coeffs, cudaStream_t stream)
997 998
    {
        typedef void (*XYZ2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, cudaStream_t stream);
999 1000 1001 1002 1003
        static const XYZ2RGB_caller_t XYZ2RGB_callers[2][2] = 
        {
            {XYZ2RGB_caller<float, 3, 3>, XYZ2RGB_caller<float, 3, 4>},
            {XYZ2RGB_caller<float, 4, 3>, XYZ2RGB_caller<float, 4, 4>}
        };
1004
        
1005
        cudaSafeCall( cudaMemcpyToSymbol(cXYZ_D65f, coeffs, 9 * sizeof(float)) );
1006

1007
        XYZ2RGB_callers[srccn-3][dstcn-3](src, dst, stream);
1008
    }
1009 1010 1011

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

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
    __constant__ int cHsvDivTable[256];

    template<typename T, int HR> struct RGB2HSVConvertor;
    template<int HR> struct RGB2HSVConvertor<uchar, HR>
    {
        template <typename D>
        static __device__ void cvt(const uchar* src, D& dst, int bidx)
        {
            const int hsv_shift = 12;
            const int hscale = HR == 180 ? 15 : 21;

            int b = src[bidx], g = src[1], r = src[bidx^2];
            int h, s, v = b;
            int vmin = b, diff;
            int vr, vg;

            v = max(v, g);
            v = max(v, r);
            vmin = min(vmin, g);
            vmin = min(vmin, r);

            diff = v - vmin;
            vr = v == r ? -1 : 0;
            vg = v == g ? -1 : 0;

            s = diff * cHsvDivTable[v] >> hsv_shift;
            h = (vr & (g - b)) + (~vr & ((vg & (b - r + 2 * diff)) + ((~vg) & (r - g + 4 * diff))));
            h = (h * cHsvDivTable[diff] * hscale + (1 << (hsv_shift + 6))) >> (7 + hsv_shift);
            h += h < 0 ? HR : 0;

            dst.x = (uchar)h;
            dst.y = (uchar)s;
            dst.z = (uchar)v;
        }
    };
    template<int HR> struct RGB2HSVConvertor<float, HR>
    {
        template <typename D>
        static __device__ void cvt(const float* src, D& dst, int bidx)
        {
            const float hscale = HR * (1.f / 360.f);

            float b = src[bidx], g = src[1], r = src[bidx^2];
            float h, s, v;

            float vmin, diff;

            v = vmin = r;
            v = fmax(v, g);
            v = fmax(v, b);
            vmin = fmin(vmin, g);
            vmin = fmin(vmin, b);

            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;

            if (h < 0) h += 360.f;

            dst.x = h * hscale;
            dst.y = s;
            dst.z = v;
        }
    };

    template <int SRCCN, int DSTCN, int HR, typename T>
    __global__ void RGB2HSV(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;

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

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

            dst_t dst;
            RGB2HSVConvertor<T, HR>::cvt(&src.x, dst, bidx);
            
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
        }
    }

    __constant__ int cHsvSectorData[6][3];

    template<typename T, int HR> struct HSV2RGBConvertor;    
    template<int HR> struct HSV2RGBConvertor<float, HR>
    {
        template <typename T>
        static __device__ void cvt(const T& src, float* dst, int bidx)
        {
            const float hscale = 6.f / HR;
            
            float h = src.x, s = src.y, v = src.z;
            float b, g, r;

            if( s == 0 )
                b = g = r = v;
            else
            {
                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 = __float2int_rd(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));

                b = tab[cHsvSectorData[sector][0]];
                g = tab[cHsvSectorData[sector][1]];
                r = tab[cHsvSectorData[sector][2]];
            }

            dst[bidx] = b;
            dst[1] = g;
            dst[bidx^2] = r;
        }
    };
    template<int HR> struct HSV2RGBConvertor<uchar, HR>
    {
        template <typename T>
        static __device__ void cvt(const T& src, uchar* dst, int bidx)
        {
            float3 buf;

            buf.x = src.x;
            buf.y = src.y * (1.f/255.f);
            buf.z = src.z * (1.f/255.f);

            HSV2RGBConvertor<float, HR>::cvt(buf, &buf.x, bidx);

            dst[0] = saturate_cast<uchar>(buf.x * 255.f);
            dst[1] = saturate_cast<uchar>(buf.y * 255.f);
            dst[2] = saturate_cast<uchar>(buf.z * 255.f);
        }
    };

    template <int SRCCN, int DSTCN, int HR, typename T>
    __global__ void HSV2RGB(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;

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

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

            dst_t dst;
            HSV2RGBConvertor<T, HR>::cvt(src, &dst.x, bidx);
            setAlpha(dst, ColorChannel<T>::max());
            
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
        }
    }

    template <typename T, int SRCCN, int DSTCN>
    void RGB2HSV_caller(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, 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);

        if (hrange == 180)
1196 1197
            RGB2HSV<SRCCN, DSTCN, 180, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
                dst.data, dst.step, src.rows, src.cols, bidx);
1198
        else
1199 1200
            RGB2HSV<SRCCN, DSTCN, 255, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
                dst.data, dst.step, src.rows, src.cols, bidx);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249

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

    void RGB2HSV_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, int hrange, cudaStream_t stream)
    {
        typedef void (*RGB2HSV_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, cudaStream_t stream);
        static const RGB2HSV_caller_t RGB2HSV_callers[2][2] = 
        {
            {RGB2HSV_caller<uchar, 3, 3>, RGB2HSV_caller<uchar, 3, 4>},
            {RGB2HSV_caller<uchar, 4, 3>, RGB2HSV_caller<uchar, 4, 4>}
        };

        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
        };
1250
        cudaSafeCall( cudaMemcpyToSymbol(cHsvDivTable, div_table, sizeof(div_table)) );
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277

        RGB2HSV_callers[srccn-3][dstcn-3](src, dst, bidx, hrange, stream);
    }

    void RGB2HSV_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, int hrange, cudaStream_t stream)
    {
        typedef void (*RGB2HSV_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, cudaStream_t stream);
        static const RGB2HSV_caller_t RGB2HSV_callers[2][2] = 
        {
            {RGB2HSV_caller<float, 3, 3>, RGB2HSV_caller<float, 3, 4>},
            {RGB2HSV_caller<float, 4, 3>, RGB2HSV_caller<float, 4, 4>}
        };
        
        RGB2HSV_callers[srccn-3][dstcn-3](src, dst, bidx, hrange, stream);
    }

    
    template <typename T, int SRCCN, int DSTCN>
    void HSV2RGB_caller(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, 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);

        if (hrange == 180)
1278 1279
            HSV2RGB<SRCCN, DSTCN, 180, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
                dst.data, dst.step, src.rows, src.cols, bidx);
1280
        else
1281 1282
            HSV2RGB<SRCCN, DSTCN, 255, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
                dst.data, dst.step, src.rows, src.cols, bidx);
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

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

    void HSV2RGB_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, int hrange, cudaStream_t stream)
    {
        typedef void (*HSV2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, cudaStream_t stream);
        static const HSV2RGB_caller_t HSV2RGB_callers[2][2] = 
        {
            {HSV2RGB_caller<uchar, 3, 3>, HSV2RGB_caller<uchar, 3, 4>},
            {HSV2RGB_caller<uchar, 4, 3>, HSV2RGB_caller<uchar, 4, 4>}
        };

        static const int sector_data[][3] =
            {{1,3,0}, {1,0,2}, {3,0,1}, {0,2,1}, {0,1,3}, {2,1,0}};

1300
        cudaSafeCall( cudaMemcpyToSymbol(cHsvSectorData, sector_data, sizeof(sector_data)) );
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

        HSV2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, hrange, stream);
    }

    void HSV2RGB_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, int hrange, cudaStream_t stream)
    {
        typedef void (*HSV2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, cudaStream_t stream);
        static const HSV2RGB_caller_t HSV2RGB_callers[2][2] = 
        {
            {HSV2RGB_caller<float, 3, 3>, HSV2RGB_caller<float, 3, 4>},
            {HSV2RGB_caller<float, 4, 3>, HSV2RGB_caller<float, 4, 4>}
        };
        
        static const int sector_data[][3] =
            {{1,3,0}, {1,0,2}, {3,0,1}, {0,2,1}, {0,1,3}, {2,1,0}};

1317
        cudaSafeCall( cudaMemcpyToSymbol(cHsvSectorData, sector_data, sizeof(sector_data)) );
1318 1319 1320 1321
        
        HSV2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, hrange, stream);
    }

1322
/////////////////////////////////////// RGB <-> HLS ////////////////////////////////////////
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

    template<typename T, int HR> struct RGB2HLSConvertor;
    template<int HR> struct RGB2HLSConvertor<float, HR>
    {
        template <typename D>
        static __device__ void cvt(const float* src, D& dst, int bidx)
        {
            const float hscale = HR * (1.f/360.f);

            float b = src[bidx], g = src[1], r = src[bidx^2];
            float h = 0.f, s = 0.f, l;
            float vmin, vmax, diff;

            vmax = vmin = r;
            vmax = fmax(vmax, g);
            vmax = fmax(vmax, b);
            vmin = fmin(vmin, g);
            vmin = fmin(vmin, b);

            diff = vmax - vmin;
            l = (vmax + vmin) * 0.5f;

            if (diff > FLT_EPSILON)
            {
                s = l < 0.5f ? diff / (vmax + vmin) : diff / (2.0f - vmax - vmin);
                diff = 60.f / diff;

                if (vmax == r)
                    h = (g - b)*diff;
                else if (vmax == g)
                    h = (b - r)*diff + 120.f;
                else
                    h = (r - g)*diff + 240.f;

                if (h < 0.f) h += 360.f;
            }

            dst.x = h * hscale;
            dst.y = l;
            dst.z = s;
        }
    };
    template<int HR> struct RGB2HLSConvertor<uchar, HR>
    {
        template <typename D>
        static __device__ void cvt(const uchar* src, D& dst, int bidx)
        {
            float3 buf;

            buf.x = src[0]*(1.f/255.f);
            buf.y = src[1]*(1.f/255.f);
            buf.z = src[2]*(1.f/255.f);

            RGB2HLSConvertor<float, HR>::cvt(&buf.x, buf, bidx);

            dst.x = saturate_cast<uchar>(buf.x);
            dst.y = saturate_cast<uchar>(buf.y*255.f);
            dst.z = saturate_cast<uchar>(buf.z*255.f);
        }
    };

    template <int SRCCN, int DSTCN, int HR, typename T>
    __global__ void RGB2HLS(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;

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

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

            dst_t dst;
            RGB2HLSConvertor<T, HR>::cvt(&src.x, dst, bidx);
            
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
        }
    }
    
    __constant__ int cHlsSectorData[6][3];

    template<typename T, int HR> struct HLS2RGBConvertor;    
    template<int HR> struct HLS2RGBConvertor<float, HR>
    {
        template <typename T>
        static __device__ void cvt(const T& src, float* dst, int bidx)
        {
            const float hscale = 6.0f / HR;

            float h = src.x, l = src.y, s = src.z;
            float b, g, r;

            if (s == 0)
                b = g = r = l;
            else
            {
                float tab[4];
                int sector;

                float p2 = l <= 0.5f ? l * (1 + s) : l + s - l * s;
                float p1 = 2 * l - p2;

                h *= hscale;

                if( h < 0 )
                    do h += 6; while( h < 0 );
                else if( h >= 6 )
                    do h -= 6; while( h >= 6 );

                sector = __float2int_rd(h);
                h -= sector;

                tab[0] = p2;
                tab[1] = p1;
                tab[2] = p1 + (p2 - p1) * (1 - h);
                tab[3] = p1 + (p2 - p1) * h;

                b = tab[cHlsSectorData[sector][0]];
                g = tab[cHlsSectorData[sector][1]];
                r = tab[cHlsSectorData[sector][2]];
            }

            dst[bidx] = b;
            dst[1] = g;
            dst[bidx^2] = r;
        }
    };
    template<int HR> struct HLS2RGBConvertor<uchar, HR>
    {
        template <typename T>
        static __device__ void cvt(const T& src, uchar* dst, int bidx)
        {
            float3 buf;

            buf.x = src.x;
            buf.y = src.y*(1.f/255.f);
            buf.z = src.z*(1.f/255.f);

            HLS2RGBConvertor<float, HR>::cvt(buf, &buf.x, bidx);

            dst[0] = saturate_cast<uchar>(buf.x*255.f);
            dst[1] = saturate_cast<uchar>(buf.y*255.f);
            dst[2] = saturate_cast<uchar>(buf.z*255.f);
        }
    };

    template <int SRCCN, int DSTCN, int HR, typename T>
    __global__ void HLS2RGB(const uchar* src_, size_t src_step, uchar* dst_, size_t dst_step, int rows, int cols, int bidx)
    {
        typedef typename TypeVec<T, SRCCN>::vec_t src_t;
        typedef typename TypeVec<T, DSTCN>::vec_t dst_t;

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

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

            dst_t dst;
            HLS2RGBConvertor<T, HR>::cvt(src, &dst.x, bidx);
            setAlpha(dst, ColorChannel<T>::max());
            
            *(dst_t*)(dst_ + y * dst_step + x * DSTCN * sizeof(T)) = dst;
        }
    }

    template <typename T, int SRCCN, int DSTCN>
    void RGB2HLS_caller(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, 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);

        if (hrange == 180)
1502 1503
            RGB2HLS<SRCCN, DSTCN, 180, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
                dst.data, dst.step, src.rows, src.cols, bidx);
1504
        else
1505 1506
            RGB2HLS<SRCCN, DSTCN, 255, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
                dst.data, dst.step, src.rows, src.cols, bidx);
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546

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

    void RGB2HLS_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, int hrange, cudaStream_t stream)
    {
        typedef void (*RGB2HLS_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, cudaStream_t stream);
        static const RGB2HLS_caller_t RGB2HLS_callers[2][2] = 
        {
            {RGB2HLS_caller<uchar, 3, 3>, RGB2HLS_caller<uchar, 3, 4>},
            {RGB2HLS_caller<uchar, 4, 3>, RGB2HLS_caller<uchar, 4, 4>}
        };

        RGB2HLS_callers[srccn-3][dstcn-3](src, dst, bidx, hrange, stream);
    }

    void RGB2HLS_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, int hrange, cudaStream_t stream)
    {
        typedef void (*RGB2HLS_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, cudaStream_t stream);
        static const RGB2HLS_caller_t RGB2HLS_callers[2][2] = 
        {
            {RGB2HLS_caller<float, 3, 3>, RGB2HLS_caller<float, 3, 4>},
            {RGB2HLS_caller<float, 4, 3>, RGB2HLS_caller<float, 4, 4>}
        };
        
        RGB2HLS_callers[srccn-3][dstcn-3](src, dst, bidx, hrange, stream);
    }

    
    template <typename T, int SRCCN, int DSTCN>
    void HLS2RGB_caller(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, 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);

        if (hrange == 180)
1547 1548
            HLS2RGB<SRCCN, DSTCN, 180, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
                dst.data, dst.step, src.rows, src.cols, bidx);
1549
        else
1550 1551
            HLS2RGB<SRCCN, DSTCN, 255, T><<<grid, threads, 0, stream>>>(src.data, src.step, 
                dst.data, dst.step, src.rows, src.cols, bidx);
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568

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

    void HLS2RGB_gpu_8u(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, int hrange, cudaStream_t stream)
    {
        typedef void (*HLS2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, cudaStream_t stream);
        static const HLS2RGB_caller_t HLS2RGB_callers[2][2] = 
        {
            {HLS2RGB_caller<uchar, 3, 3>, HLS2RGB_caller<uchar, 3, 4>},
            {HLS2RGB_caller<uchar, 4, 3>, HLS2RGB_caller<uchar, 4, 4>}
        };
        
        static const int sector_data[][3]=
            {{1,3,0}, {1,0,2}, {3,0,1}, {0,2,1}, {0,1,3}, {2,1,0}};

1569
        cudaSafeCall( cudaMemcpyToSymbol(cHlsSectorData, sector_data, sizeof(sector_data)) );
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585

        HLS2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, hrange, stream);
    }

    void HLS2RGB_gpu_32f(const DevMem2D& src, int srccn, const DevMem2D& dst, int dstcn, int bidx, int hrange, cudaStream_t stream)
    {
        typedef void (*HLS2RGB_caller_t)(const DevMem2D& src, const DevMem2D& dst, int bidx, int hrange, cudaStream_t stream);
        static const HLS2RGB_caller_t HLS2RGB_callers[2][2] = 
        {
            {HLS2RGB_caller<float, 3, 3>, HLS2RGB_caller<float, 3, 4>},
            {HLS2RGB_caller<float, 4, 3>, HLS2RGB_caller<float, 4, 4>}
        };
        
        static const int sector_data[][3]=
            {{1,3,0}, {1,0,2}, {3,0,1}, {0,2,1}, {0,1,3}, {2,1,0}};

1586
        cudaSafeCall( cudaMemcpyToSymbol(cHlsSectorData, sector_data, sizeof(sector_data)) );
1587 1588 1589 1590
                
        HLS2RGB_callers[srccn-3][dstcn-3](src, dst, bidx, hrange, stream);
    }
}}}