color.cpp 77.8 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
/*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 "precomp.hpp"

using namespace cv;
46
using namespace cv::cuda;
47 48 49

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)

50
void cv::cuda::cvtColor(InputArray, OutputArray, int, int, Stream&) { throw_no_cuda(); }
51

52
void cv::cuda::demosaicing(InputArray, OutputArray, int, int, Stream&) { throw_no_cuda(); }
53

54
void cv::cuda::swapChannels(InputOutputArray, const int[], Stream&) { throw_no_cuda(); }
55

56
void cv::cuda::gammaCorrection(InputArray, OutputArray, bool, Stream&) { throw_no_cuda(); }
57

58
void cv::cuda::alphaComp(InputArray, InputArray, OutputArray, int, Stream&) { throw_no_cuda(); }
59

60

61 62
#else /* !defined (HAVE_CUDA) */

63
#include "cvt_color_internal.h"
64

65
namespace cv { namespace cuda {
66
    namespace device
67 68 69 70 71
    {
        template <int cn>
        void Bayer2BGR_8u_gpu(PtrStepSzb src, PtrStepSzb dst, bool blue_last, bool start_with_green, cudaStream_t stream);
        template <int cn>
        void Bayer2BGR_16u_gpu(PtrStepSzb src, PtrStepSzb dst, bool blue_last, bool start_with_green, cudaStream_t stream);
72 73 74

        template <int cn>
        void MHCdemosaic(PtrStepSzb src, int2 sourceOffset, PtrStepSzb dst, int2 firstRed, cudaStream_t stream);
75 76 77
    }
}}

78
using namespace ::cv::cuda::device;
79 80 81

namespace
{
82
    typedef void (*gpu_func_t)(const GpuMat& _src, GpuMat& _dst, Stream& stream);
83

84
    void BGR_to_RGB(InputArray _src, OutputArray _dst, int, Stream& stream)
85
    {
86
        using namespace cv::cuda::device;
87
        static const gpu_func_t funcs[] = {BGR_to_RGB_8u, 0, BGR_to_RGB_16u, 0, 0, BGR_to_RGB_32f};
88

89 90 91 92
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 );
93

94 95
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 3));
        GpuMat dst = _dst.getGpuMat();
96

97
        funcs[src.depth()](src, dst, stream);
98 99
    }

100
    void BGR_to_BGRA(InputArray _src, OutputArray _dst, int, Stream& stream)
101
    {
102
        using namespace cv::cuda::device;
103
        static const gpu_func_t funcs[] = {BGR_to_BGRA_8u, 0, BGR_to_BGRA_16u, 0, 0, BGR_to_BGRA_32f};
104

105 106 107 108
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 );
109

110 111
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 4));
        GpuMat dst = _dst.getGpuMat();
112

113
        funcs[src.depth()](src, dst, stream);
114 115
    }

116
    void BGR_to_RGBA(InputArray _src, OutputArray _dst, int, Stream& stream)
117
    {
118
        using namespace cv::cuda::device;
119
        static const gpu_func_t funcs[] = {BGR_to_RGBA_8u, 0, BGR_to_RGBA_16u, 0, 0, BGR_to_RGBA_32f};
120

121
        GpuMat src = _src.getGpuMat();
122

123 124 125 126 127
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 4));
        GpuMat dst = _dst.getGpuMat();
128

129
        funcs[src.depth()](src, dst, stream);
130 131
    }

132
    void BGRA_to_BGR(InputArray _src, OutputArray _dst, int, Stream& stream)
133
    {
134
        using namespace cv::cuda::device;
135
        static const gpu_func_t funcs[] = {BGRA_to_BGR_8u, 0, BGRA_to_BGR_16u, 0, 0, BGRA_to_BGR_32f};
136

137 138 139 140
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 4 );
141

142 143
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 3));
        GpuMat dst = _dst.getGpuMat();
144

145
        funcs[src.depth()](src, dst, stream);
146 147
    }

148
    void BGRA_to_RGB(InputArray _src, OutputArray _dst, int, Stream& stream)
149
    {
150
        using namespace cv::cuda::device;
151
        static const gpu_func_t funcs[] = {BGRA_to_RGB_8u, 0, BGRA_to_RGB_16u, 0, 0, BGRA_to_RGB_32f};
152

153
        GpuMat src = _src.getGpuMat();
154

155 156 157 158 159
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 3));
        GpuMat dst = _dst.getGpuMat();
160

161
        funcs[src.depth()](src, dst, stream);
162 163
    }

164
    void BGRA_to_RGBA(InputArray _src, OutputArray _dst, int, Stream& stream)
165
    {
166
        using namespace cv::cuda::device;
167
        static const gpu_func_t funcs[] = {BGRA_to_RGBA_8u, 0, BGRA_to_RGBA_16u, 0, 0, BGRA_to_RGBA_32f};
168

169 170 171 172
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 4 );
173

174 175
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 4));
        GpuMat dst = _dst.getGpuMat();
176

177
        funcs[src.depth()](src, dst, stream);
178 179
    }

180
    void BGR_to_BGR555(InputArray _src, OutputArray _dst, int, Stream& stream)
181
    {
182
        GpuMat src = _src.getGpuMat();
183

184 185 186 187 188
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 3 );

        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
189

190
        cv::cuda::device::BGR_to_BGR555(src, dst, stream);
191 192
    }

193
    void BGR_to_BGR565(InputArray _src, OutputArray _dst, int, Stream& stream)
194
    {
195 196 197 198
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 3 );
199

200 201
        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
202

203
        cv::cuda::device::BGR_to_BGR565(src, dst, stream);
204 205
    }

206
    void RGB_to_BGR555(InputArray _src, OutputArray _dst, int, Stream& stream)
207
    {
208
        GpuMat src = _src.getGpuMat();
209

210 211 212 213 214
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 3 );

        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
215

216
        cv::cuda::device::RGB_to_BGR555(src, dst, stream);
217 218
    }

219
    void RGB_to_BGR565(InputArray _src, OutputArray _dst, int, Stream& stream)
220
    {
221 222 223 224
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 3 );
225

226 227
        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
228

229
        cv::cuda::device::RGB_to_BGR565(src, dst, stream);
230 231
    }

232
    void BGRA_to_BGR555(InputArray _src, OutputArray _dst, int, Stream& stream)
233
    {
234
        GpuMat src = _src.getGpuMat();
235

236 237 238 239 240
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 4 );

        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
241

242
        cv::cuda::device::BGRA_to_BGR555(src, dst, stream);
243 244
    }

245
    void BGRA_to_BGR565(InputArray _src, OutputArray _dst, int, Stream& stream)
246
    {
247 248 249 250
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 4 );
251

252 253
        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
254

255
        cv::cuda::device::BGRA_to_BGR565(src, dst, stream);
256 257
    }

258
    void RGBA_to_BGR555(InputArray _src, OutputArray _dst, int, Stream& stream)
259
    {
260
        GpuMat src = _src.getGpuMat();
261

262 263 264 265 266
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 4 );

        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
267

268
        cv::cuda::device::RGBA_to_BGR555(src, dst, stream);
269 270
    }

271
    void RGBA_to_BGR565(InputArray _src, OutputArray _dst, int, Stream& stream)
272
    {
273 274 275 276
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 4 );
277

278 279
        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
280

281
        cv::cuda::device::RGBA_to_BGR565(src, dst, stream);
282 283
    }

284
    void BGR555_to_RGB(InputArray _src, OutputArray _dst, int, Stream& stream)
285
    {
286
        GpuMat src = _src.getGpuMat();
287

288 289 290 291 292
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );

        _dst.create(src.size(), CV_8UC3);
        GpuMat dst = _dst.getGpuMat();
293

294
        cv::cuda::device::BGR555_to_RGB(src, dst, stream);
295 296
    }

297
    void BGR565_to_RGB(InputArray _src, OutputArray _dst, int, Stream& stream)
298
    {
299 300 301 302
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );
303

304 305
        _dst.create(src.size(), CV_8UC3);
        GpuMat dst = _dst.getGpuMat();
306

307
        cv::cuda::device::BGR565_to_RGB(src, dst, stream);
308 309
    }

310
    void BGR555_to_BGR(InputArray _src, OutputArray _dst, int, Stream& stream)
311
    {
312
        GpuMat src = _src.getGpuMat();
313

314 315 316 317 318
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );

        _dst.create(src.size(), CV_8UC3);
        GpuMat dst = _dst.getGpuMat();
319

320
        cv::cuda::device::BGR555_to_BGR(src, dst, stream);
321 322
    }

323
    void BGR565_to_BGR(InputArray _src, OutputArray _dst, int, Stream& stream)
324
    {
325 326 327 328
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );
329

330 331
        _dst.create(src.size(), CV_8UC3);
        GpuMat dst = _dst.getGpuMat();
332

333
        cv::cuda::device::BGR565_to_BGR(src, dst, stream);
334 335
    }

336
    void BGR555_to_RGBA(InputArray _src, OutputArray _dst, int, Stream& stream)
337
    {
338 339 340 341
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );
342

343 344
        _dst.create(src.size(), CV_8UC4);
        GpuMat dst = _dst.getGpuMat();
345

346
        cv::cuda::device::BGR555_to_RGBA(src, dst, stream);
347 348
    }

349
    void BGR565_to_RGBA(InputArray _src, OutputArray _dst, int, Stream& stream)
350
    {
351
        GpuMat src = _src.getGpuMat();
352

353 354 355 356 357
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );

        _dst.create(src.size(), CV_8UC4);
        GpuMat dst = _dst.getGpuMat();
358

359
        cv::cuda::device::BGR565_to_RGBA(src, dst, stream);
360 361
    }

362
    void BGR555_to_BGRA(InputArray _src, OutputArray _dst, int, Stream& stream)
363
    {
364 365 366 367
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );
368

369 370
        _dst.create(src.size(), CV_8UC4);
        GpuMat dst = _dst.getGpuMat();
371

372
        cv::cuda::device::BGR555_to_BGRA(src, dst, stream);
373 374
    }

375
    void BGR565_to_BGRA(InputArray _src, OutputArray _dst, int, Stream& stream)
376
    {
377
        GpuMat src = _src.getGpuMat();
378

379 380 381 382 383
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );

        _dst.create(src.size(), CV_8UC4);
        GpuMat dst = _dst.getGpuMat();
384

385
        cv::cuda::device::BGR565_to_BGRA(src, dst, stream);
386 387
    }

388
    void GRAY_to_BGR(InputArray _src, OutputArray _dst, int, Stream& stream)
389
    {
390
        using namespace cv::cuda::device;
391
        static const gpu_func_t funcs[] = {GRAY_to_BGR_8u, 0, GRAY_to_BGR_16u, 0, 0, GRAY_to_BGR_32f};
392

393 394 395 396
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 1 );
397

398 399
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 3));
        GpuMat dst = _dst.getGpuMat();
400

401
        funcs[src.depth()](src, dst, stream);
402 403
    }

404
    void GRAY_to_BGRA(InputArray _src, OutputArray _dst, int, Stream& stream)
405
    {
406
        using namespace cv::cuda::device;
407
        static const gpu_func_t funcs[] = {GRAY_to_BGRA_8u, 0, GRAY_to_BGRA_16u, 0, 0, GRAY_to_BGRA_32f};
408

409
        GpuMat src = _src.getGpuMat();
410

411 412 413 414 415
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 1 );

        _dst.create(src.size(), CV_MAKETYPE(src.depth(), 4));
        GpuMat dst = _dst.getGpuMat();
416

417
        funcs[src.depth()](src, dst, stream);
418 419
    }

420
    void GRAY_to_BGR555(InputArray _src, OutputArray _dst, int, Stream& stream)
421
    {
422 423 424 425
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 1 );
426

427 428
        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
429

430
        cv::cuda::device::GRAY_to_BGR555(src, dst, stream);
431 432
    }

433
    void GRAY_to_BGR565(InputArray _src, OutputArray _dst, int, Stream& stream)
434
    {
435
        GpuMat src = _src.getGpuMat();
436

437 438 439 440 441
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 1 );

        _dst.create(src.size(), CV_8UC2);
        GpuMat dst = _dst.getGpuMat();
442

443
        cv::cuda::device::GRAY_to_BGR565(src, dst, stream);
444 445
    }

446
    void BGR555_to_GRAY(InputArray _src, OutputArray _dst, int, Stream& stream)
447
    {
448 449 450 451
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );
452

453 454
        _dst.create(src.size(), CV_8UC1);
        GpuMat dst = _dst.getGpuMat();
455

456
        cv::cuda::device::BGR555_to_GRAY(src, dst, stream);
457 458
    }

459
    void BGR565_to_GRAY(InputArray _src, OutputArray _dst, int, Stream& stream)
460
    {
461
        GpuMat src = _src.getGpuMat();
462

463 464 465 466 467
        CV_Assert( src.depth() == CV_8U );
        CV_Assert( src.channels() == 2 );

        _dst.create(src.size(), CV_8UC1);
        GpuMat dst = _dst.getGpuMat();
468

469
        cv::cuda::device::BGR565_to_GRAY(src, dst, stream);
470 471
    }

472
    void RGB_to_GRAY(InputArray _src, OutputArray _dst, int, Stream& stream)
473
    {
474
        using namespace cv::cuda::device;
475
        static const gpu_func_t funcs[] = {RGB_to_GRAY_8u, 0, RGB_to_GRAY_16u, 0, 0, RGB_to_GRAY_32f};
476

477 478 479 480
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 );
481

482 483
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 1));
        GpuMat dst = _dst.getGpuMat();
484

485
        funcs[src.depth()](src, dst, stream);
486 487
    }

488
    void BGR_to_GRAY(InputArray _src, OutputArray _dst, int, Stream& stream)
489
    {
490
        using namespace cv::cuda::device;
491
        static const gpu_func_t funcs[] = {BGR_to_GRAY_8u, 0, BGR_to_GRAY_16u, 0, 0, BGR_to_GRAY_32f};
492

493
        GpuMat src = _src.getGpuMat();
494

495 496 497 498 499
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 1));
        GpuMat dst = _dst.getGpuMat();
500

501
        funcs[src.depth()](src, dst, stream);
502 503
    }

504
    void RGBA_to_GRAY(InputArray _src, OutputArray _dst, int, Stream& stream)
505
    {
506
        using namespace cv::cuda::device;
507
        static const gpu_func_t funcs[] = {RGBA_to_GRAY_8u, 0, RGBA_to_GRAY_16u, 0, 0, RGBA_to_GRAY_32f};
508

509 510 511 512
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 4 );
513

514 515
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 1));
        GpuMat dst = _dst.getGpuMat();
516

517
        funcs[src.depth()](src, dst, stream);
518 519
    }

520
    void BGRA_to_GRAY(InputArray _src, OutputArray _dst, int, Stream& stream)
521
    {
522
        using namespace cv::cuda::device;
523
        static const gpu_func_t funcs[] = {BGRA_to_GRAY_8u, 0, BGRA_to_GRAY_16u, 0, 0, BGRA_to_GRAY_32f};
524

525
        GpuMat src = _src.getGpuMat();
526

527 528 529 530 531
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 1));
        GpuMat dst = _dst.getGpuMat();
532

533
        funcs[src.depth()](src, dst, stream);
534 535
    }

536
    void RGB_to_YUV(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
537
    {
538
        using namespace cv::cuda::device;
539 540 541
        static const gpu_func_t funcs[2][2][6] =
        {
            {
542 543
                {RGB_to_YUV_8u, 0, RGB_to_YUV_16u, 0, 0, RGB_to_YUV_32f},
                {RGBA_to_YUV_8u, 0, RGBA_to_YUV_16u, 0, 0, RGBA_to_YUV_32f}
544 545
            },
            {
546 547
                {RGB_to_YUV4_8u, 0, RGB_to_YUV4_16u, 0, 0, RGB_to_YUV4_32f},
                {RGBA_to_YUV4_8u, 0, RGBA_to_YUV4_16u, 0, 0, RGBA_to_YUV4_32f}
548 549 550 551 552
            }
        };

        if (dcn <= 0) dcn = 3;

553 554 555 556 557
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
558

559 560
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
561

562
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
563 564
    }

565
    void BGR_to_YUV(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
566
    {
567
        using namespace cv::cuda::device;
568 569 570
        static const gpu_func_t funcs[2][2][6] =
        {
            {
571 572
                {BGR_to_YUV_8u, 0, BGR_to_YUV_16u, 0, 0, BGR_to_YUV_32f},
                {BGRA_to_YUV_8u, 0, BGRA_to_YUV_16u, 0, 0, BGRA_to_YUV_32f}
573 574
            },
            {
575 576
                {BGR_to_YUV4_8u, 0, BGR_to_YUV4_16u, 0, 0, BGR_to_YUV4_32f},
                {BGRA_to_YUV4_8u, 0, BGRA_to_YUV4_16u, 0, 0, BGRA_to_YUV4_32f}
577 578 579 580 581
            }
        };

        if (dcn <= 0) dcn = 3;

582 583 584 585 586
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
587

588 589
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
590

591
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
592 593
    }

594
    void YUV_to_RGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
595
    {
596
        using namespace cv::cuda::device;
597 598 599
        static const gpu_func_t funcs[2][2][6] =
        {
            {
600 601
                {YUV_to_RGB_8u, 0, YUV_to_RGB_16u, 0, 0, YUV_to_RGB_32f},
                {YUV4_to_RGB_8u, 0, YUV4_to_RGB_16u, 0, 0, YUV4_to_RGB_32f}
602 603
            },
            {
604 605
                {YUV_to_RGBA_8u, 0, YUV_to_RGBA_16u, 0, 0, YUV_to_RGBA_32f},
                {YUV4_to_RGBA_8u, 0, YUV4_to_RGBA_16u, 0, 0, YUV4_to_RGBA_32f}
606 607 608 609 610
            }
        };

        if (dcn <= 0) dcn = 3;

611 612 613 614 615
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
616

617 618
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
619

620
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
621 622
    }

623
    void YUV_to_BGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
624
    {
625
        using namespace cv::cuda::device;
626 627 628
        static const gpu_func_t funcs[2][2][6] =
        {
            {
629 630
                {YUV_to_BGR_8u, 0, YUV_to_BGR_16u, 0, 0, YUV_to_BGR_32f},
                {YUV4_to_BGR_8u, 0, YUV4_to_BGR_16u, 0, 0, YUV4_to_BGR_32f}
631 632
            },
            {
633 634
                {YUV_to_BGRA_8u, 0, YUV_to_BGRA_16u, 0, 0, YUV_to_BGRA_32f},
                {YUV4_to_BGRA_8u, 0, YUV4_to_BGRA_16u, 0, 0, YUV4_to_BGRA_32f}
635 636 637 638 639
            }
        };

        if (dcn <= 0) dcn = 3;

640
        GpuMat src = _src.getGpuMat();
641

642 643 644 645 646 647
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
648

649
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
650 651
    }

652
    void RGB_to_YCrCb(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
653
    {
654
        using namespace cv::cuda::device;
655 656 657
        static const gpu_func_t funcs[2][2][6] =
        {
            {
658 659
                {RGB_to_YCrCb_8u, 0, RGB_to_YCrCb_16u, 0, 0, RGB_to_YCrCb_32f},
                {RGBA_to_YCrCb_8u, 0, RGBA_to_YCrCb_16u, 0, 0, RGBA_to_YCrCb_32f}
660 661
            },
            {
662 663
                {RGB_to_YCrCb4_8u, 0, RGB_to_YCrCb4_16u, 0, 0, RGB_to_YCrCb4_32f},
                {RGBA_to_YCrCb4_8u, 0, RGBA_to_YCrCb4_16u, 0, 0, RGBA_to_YCrCb4_32f}
664 665 666 667 668
            }
        };

        if (dcn <= 0) dcn = 3;

669 670 671 672 673
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
674

675 676
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
677

678
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
679 680
    }

681
    void BGR_to_YCrCb(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
682
    {
683
        using namespace cv::cuda::device;
684 685 686
        static const gpu_func_t funcs[2][2][6] =
        {
            {
687 688
                {BGR_to_YCrCb_8u, 0, BGR_to_YCrCb_16u, 0, 0, BGR_to_YCrCb_32f},
                {BGRA_to_YCrCb_8u, 0, BGRA_to_YCrCb_16u, 0, 0, BGRA_to_YCrCb_32f}
689 690
            },
            {
691 692
                {BGR_to_YCrCb4_8u, 0, BGR_to_YCrCb4_16u, 0, 0, BGR_to_YCrCb4_32f},
                {BGRA_to_YCrCb4_8u, 0, BGRA_to_YCrCb4_16u, 0, 0, BGRA_to_YCrCb4_32f}
693 694 695 696 697
            }
        };

        if (dcn <= 0) dcn = 3;

698
        GpuMat src = _src.getGpuMat();
699

700 701 702 703 704 705
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
706

707
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
708 709
    }

710
    void YCrCb_to_RGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
711
    {
712
        using namespace cv::cuda::device;
713 714 715
        static const gpu_func_t funcs[2][2][6] =
        {
            {
716 717
                {YCrCb_to_RGB_8u, 0, YCrCb_to_RGB_16u, 0, 0, YCrCb_to_RGB_32f},
                {YCrCb4_to_RGB_8u, 0, YCrCb4_to_RGB_16u, 0, 0, YCrCb4_to_RGB_32f}
718 719
            },
            {
720 721
                {YCrCb_to_RGBA_8u, 0, YCrCb_to_RGBA_16u, 0, 0, YCrCb_to_RGBA_32f},
                {YCrCb4_to_RGBA_8u, 0, YCrCb4_to_RGBA_16u, 0, 0, YCrCb4_to_RGBA_32f}
722 723 724 725 726
            }
        };

        if (dcn <= 0) dcn = 3;

727 728 729 730 731
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
732

733 734
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
735

736
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
737 738
    }

739
    void YCrCb_to_BGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
740
    {
741
        using namespace cv::cuda::device;
742 743 744
        static const gpu_func_t funcs[2][2][6] =
        {
            {
745 746
                {YCrCb_to_BGR_8u, 0, YCrCb_to_BGR_16u, 0, 0, YCrCb_to_BGR_32f},
                {YCrCb4_to_BGR_8u, 0, YCrCb4_to_BGR_16u, 0, 0, YCrCb4_to_BGR_32f}
747 748
            },
            {
749 750
                {YCrCb_to_BGRA_8u, 0, YCrCb_to_BGRA_16u, 0, 0, YCrCb_to_BGRA_32f},
                {YCrCb4_to_BGRA_8u, 0, YCrCb4_to_BGRA_16u, 0, 0, YCrCb4_to_BGRA_32f}
751 752 753 754 755
            }
        };

        if (dcn <= 0) dcn = 3;

756
        GpuMat src = _src.getGpuMat();
757

758 759 760 761 762 763
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
764

765
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
766 767
    }

768
    void RGB_to_XYZ(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
769
    {
770
        using namespace cv::cuda::device;
771 772 773
        static const gpu_func_t funcs[2][2][6] =
        {
            {
774 775
                {RGB_to_XYZ_8u, 0, RGB_to_XYZ_16u, 0, 0, RGB_to_XYZ_32f},
                {RGBA_to_XYZ_8u, 0, RGBA_to_XYZ_16u, 0, 0, RGBA_to_XYZ_32f}
776 777
            },
            {
778 779
                {RGB_to_XYZ4_8u, 0, RGB_to_XYZ4_16u, 0, 0, RGB_to_XYZ4_32f},
                {RGBA_to_XYZ4_8u, 0, RGBA_to_XYZ4_16u, 0, 0, RGBA_to_XYZ4_32f}
780 781 782 783 784
            }
        };

        if (dcn <= 0) dcn = 3;

785 786 787 788 789
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
790

791 792
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
793

794
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
795 796
    }

797
    void BGR_to_XYZ(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
798
    {
799
        using namespace cv::cuda::device;
800 801 802
        static const gpu_func_t funcs[2][2][6] =
        {
            {
803 804
                {BGR_to_XYZ_8u, 0, BGR_to_XYZ_16u, 0, 0, BGR_to_XYZ_32f},
                {BGRA_to_XYZ_8u, 0, BGRA_to_XYZ_16u, 0, 0, BGRA_to_XYZ_32f}
805 806
            },
            {
807 808
                {BGR_to_XYZ4_8u, 0, BGR_to_XYZ4_16u, 0, 0, BGR_to_XYZ4_32f},
                {BGRA_to_XYZ4_8u, 0, BGRA_to_XYZ4_16u, 0, 0, BGRA_to_XYZ4_32f}
809 810 811 812 813
            }
        };

        if (dcn <= 0) dcn = 3;

814
        GpuMat src = _src.getGpuMat();
815

816 817 818 819 820 821
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
822

823
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
824 825
    }

826
    void XYZ_to_RGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
827
    {
828
        using namespace cv::cuda::device;
829 830 831
        static const gpu_func_t funcs[2][2][6] =
        {
            {
832 833
                {XYZ_to_RGB_8u, 0, XYZ_to_RGB_16u, 0, 0, XYZ_to_RGB_32f},
                {XYZ4_to_RGB_8u, 0, XYZ4_to_RGB_16u, 0, 0, XYZ4_to_RGB_32f}
834 835
            },
            {
836 837
                {XYZ_to_RGBA_8u, 0, XYZ_to_RGBA_16u, 0, 0, XYZ_to_RGBA_32f},
                {XYZ4_to_RGBA_8u, 0, XYZ4_to_RGBA_16u, 0, 0, XYZ4_to_RGBA_32f}
838 839 840 841 842
            }
        };

        if (dcn <= 0) dcn = 3;

843
        GpuMat src = _src.getGpuMat();
844

845 846 847 848 849 850
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
851

852
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
853 854
    }

855
    void XYZ_to_BGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
856
    {
857
        using namespace cv::cuda::device;
858 859 860
        static const gpu_func_t funcs[2][2][6] =
        {
            {
861 862
                {XYZ_to_BGR_8u, 0, XYZ_to_BGR_16u, 0, 0, XYZ_to_BGR_32f},
                {XYZ4_to_BGR_8u, 0, XYZ4_to_BGR_16u, 0, 0, XYZ4_to_BGR_32f}
863 864
            },
            {
865 866
                {XYZ_to_BGRA_8u, 0, XYZ_to_BGRA_16u, 0, 0, XYZ_to_BGRA_32f},
                {XYZ4_to_BGRA_8u, 0, XYZ4_to_BGRA_16u, 0, 0, XYZ4_to_BGRA_32f}
867 868 869 870 871
            }
        };

        if (dcn <= 0) dcn = 3;

872
        GpuMat src = _src.getGpuMat();
873

874 875 876 877 878 879
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_16U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
880

881
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
882 883
    }

884
    void RGB_to_HSV(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
885
    {
886
        using namespace cv::cuda::device;
887 888 889
        static const gpu_func_t funcs[2][2][6] =
        {
            {
890 891
                {RGB_to_HSV_8u, 0, 0, 0, 0, RGB_to_HSV_32f},
                {RGBA_to_HSV_8u, 0, 0, 0, 0, RGBA_to_HSV_32f},
892 893
            },
            {
894 895
                {RGB_to_HSV4_8u, 0, 0, 0, 0, RGB_to_HSV4_32f},
                {RGBA_to_HSV4_8u, 0, 0, 0, 0, RGBA_to_HSV4_32f},
896 897 898 899 900
            }
        };

        if (dcn <= 0) dcn = 3;

901 902 903 904 905
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
906

907 908
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
909

910
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
911 912
    }

913
    void BGR_to_HSV(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
914
    {
915
        using namespace cv::cuda::device;
916 917 918
        static const gpu_func_t funcs[2][2][6] =
        {
            {
919 920
                {BGR_to_HSV_8u, 0, 0, 0, 0, BGR_to_HSV_32f},
                {BGRA_to_HSV_8u, 0, 0, 0, 0, BGRA_to_HSV_32f}
921 922
            },
            {
923 924
                {BGR_to_HSV4_8u, 0, 0, 0, 0, BGR_to_HSV4_32f},
                {BGRA_to_HSV4_8u, 0, 0, 0, 0, BGRA_to_HSV4_32f}
925 926 927 928 929
            }
        };

        if (dcn <= 0) dcn = 3;

930
        GpuMat src = _src.getGpuMat();
931

932 933 934 935 936 937
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
938

939
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
940 941
    }

942
    void HSV_to_RGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
943
    {
944
        using namespace cv::cuda::device;
945 946 947
        static const gpu_func_t funcs[2][2][6] =
        {
            {
948 949
                {HSV_to_RGB_8u, 0, 0, 0, 0, HSV_to_RGB_32f},
                {HSV4_to_RGB_8u, 0, 0, 0, 0, HSV4_to_RGB_32f}
950 951
            },
            {
952 953
                {HSV_to_RGBA_8u, 0, 0, 0, 0, HSV_to_RGBA_32f},
                {HSV4_to_RGBA_8u, 0, 0, 0, 0, HSV4_to_RGBA_32f}
954 955 956 957 958
            }
        };

        if (dcn <= 0) dcn = 3;

959 960 961 962 963
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
964

965 966
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
967

968
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
969 970
    }

971
    void HSV_to_BGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
972
    {
973
        using namespace cv::cuda::device;
974 975 976
        static const gpu_func_t funcs[2][2][6] =
        {
            {
977 978
                {HSV_to_BGR_8u, 0, 0, 0, 0, HSV_to_BGR_32f},
                {HSV4_to_BGR_8u, 0, 0, 0, 0, HSV4_to_BGR_32f}
979 980
            },
            {
981 982
                {HSV_to_BGRA_8u, 0, 0, 0, 0, HSV_to_BGRA_32f},
                {HSV4_to_BGRA_8u, 0, 0, 0, 0, HSV4_to_BGRA_32f}
983 984 985 986 987
            }
        };

        if (dcn <= 0) dcn = 3;

988 989 990 991 992
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
993

994 995
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
996

997
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
998 999
    }

1000
    void RGB_to_HLS(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1001
    {
1002
        using namespace cv::cuda::device;
1003 1004 1005
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1006 1007
                {RGB_to_HLS_8u, 0, 0, 0, 0, RGB_to_HLS_32f},
                {RGBA_to_HLS_8u, 0, 0, 0, 0, RGBA_to_HLS_32f},
1008 1009
            },
            {
1010 1011
                {RGB_to_HLS4_8u, 0, 0, 0, 0, RGB_to_HLS4_32f},
                {RGBA_to_HLS4_8u, 0, 0, 0, 0, RGBA_to_HLS4_32f},
1012 1013 1014 1015 1016
            }
        };

        if (dcn <= 0) dcn = 3;

1017 1018 1019 1020 1021
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1022

1023 1024
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1025

1026
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1027 1028
    }

1029
    void BGR_to_HLS(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1030
    {
1031
        using namespace cv::cuda::device;
1032 1033 1034
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1035 1036
                {BGR_to_HLS_8u, 0, 0, 0, 0, BGR_to_HLS_32f},
                {BGRA_to_HLS_8u, 0, 0, 0, 0, BGRA_to_HLS_32f}
1037 1038
            },
            {
1039 1040
                {BGR_to_HLS4_8u, 0, 0, 0, 0, BGR_to_HLS4_32f},
                {BGRA_to_HLS4_8u, 0, 0, 0, 0, BGRA_to_HLS4_32f}
1041 1042 1043 1044 1045
            }
        };

        if (dcn <= 0) dcn = 3;

1046
        GpuMat src = _src.getGpuMat();
1047

1048 1049 1050 1051 1052 1053
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1054

1055
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1056 1057
    }

1058
    void HLS_to_RGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1059
    {
1060
        using namespace cv::cuda::device;
1061 1062 1063
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1064 1065
                {HLS_to_RGB_8u, 0, 0, 0, 0, HLS_to_RGB_32f},
                {HLS4_to_RGB_8u, 0, 0, 0, 0, HLS4_to_RGB_32f}
1066 1067
            },
            {
1068 1069
                {HLS_to_RGBA_8u, 0, 0, 0, 0, HLS_to_RGBA_32f},
                {HLS4_to_RGBA_8u, 0, 0, 0, 0, HLS4_to_RGBA_32f}
1070 1071 1072 1073 1074
            }
        };

        if (dcn <= 0) dcn = 3;

1075 1076 1077 1078 1079
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1080

1081 1082
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1083

1084
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1085 1086
    }

1087
    void HLS_to_BGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1088
    {
1089
        using namespace cv::cuda::device;
1090 1091 1092
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1093 1094
                {HLS_to_BGR_8u, 0, 0, 0, 0, HLS_to_BGR_32f},
                {HLS4_to_BGR_8u, 0, 0, 0, 0, HLS4_to_BGR_32f}
1095 1096
            },
            {
1097 1098
                {HLS_to_BGRA_8u, 0, 0, 0, 0, HLS_to_BGRA_32f},
                {HLS4_to_BGRA_8u, 0, 0, 0, 0, HLS4_to_BGRA_32f}
1099 1100 1101 1102 1103
            }
        };

        if (dcn <= 0) dcn = 3;

1104 1105 1106 1107 1108
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1109

1110 1111
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1112

1113
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1114 1115
    }

1116
    void RGB_to_HSV_FULL(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1117
    {
1118
        using namespace cv::cuda::device;
1119 1120 1121
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1122 1123
                {RGB_to_HSV_FULL_8u, 0, 0, 0, 0, RGB_to_HSV_FULL_32f},
                {RGBA_to_HSV_FULL_8u, 0, 0, 0, 0, RGBA_to_HSV_FULL_32f},
1124 1125
            },
            {
1126 1127
                {RGB_to_HSV4_FULL_8u, 0, 0, 0, 0, RGB_to_HSV4_FULL_32f},
                {RGBA_to_HSV4_FULL_8u, 0, 0, 0, 0, RGBA_to_HSV4_FULL_32f},
1128 1129 1130 1131 1132
            }
        };

        if (dcn <= 0) dcn = 3;

1133
        GpuMat src = _src.getGpuMat();
1134

1135 1136 1137 1138 1139 1140
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1141

1142
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1143 1144
    }

1145
    void BGR_to_HSV_FULL(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1146
    {
1147
        using namespace cv::cuda::device;
1148 1149 1150
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1151 1152
                {BGR_to_HSV_FULL_8u, 0, 0, 0, 0, BGR_to_HSV_FULL_32f},
                {BGRA_to_HSV_FULL_8u, 0, 0, 0, 0, BGRA_to_HSV_FULL_32f}
1153 1154
            },
            {
1155 1156
                {BGR_to_HSV4_FULL_8u, 0, 0, 0, 0, BGR_to_HSV4_FULL_32f},
                {BGRA_to_HSV4_FULL_8u, 0, 0, 0, 0, BGRA_to_HSV4_FULL_32f}
1157 1158 1159 1160 1161
            }
        };

        if (dcn <= 0) dcn = 3;

1162 1163 1164 1165 1166
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1167

1168 1169
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1170

1171
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1172 1173
    }

1174
    void HSV_to_RGB_FULL(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1175
    {
1176
        using namespace cv::cuda::device;
1177 1178 1179
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1180 1181
                {HSV_to_RGB_FULL_8u, 0, 0, 0, 0, HSV_to_RGB_FULL_32f},
                {HSV4_to_RGB_FULL_8u, 0, 0, 0, 0, HSV4_to_RGB_FULL_32f}
1182 1183
            },
            {
1184 1185
                {HSV_to_RGBA_FULL_8u, 0, 0, 0, 0, HSV_to_RGBA_FULL_32f},
                {HSV4_to_RGBA_FULL_8u, 0, 0, 0, 0, HSV4_to_RGBA_FULL_32f}
1186 1187 1188 1189 1190
            }
        };

        if (dcn <= 0) dcn = 3;

1191
        GpuMat src = _src.getGpuMat();
1192

1193 1194 1195 1196 1197 1198
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1199

1200
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1201 1202
    }

1203
    void HSV_to_BGR_FULL(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1204
    {
1205
        using namespace cv::cuda::device;
1206 1207 1208
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1209 1210
                {HSV_to_BGR_FULL_8u, 0, 0, 0, 0, HSV_to_BGR_FULL_32f},
                {HSV4_to_BGR_FULL_8u, 0, 0, 0, 0, HSV4_to_BGR_FULL_32f}
1211 1212
            },
            {
1213 1214
                {HSV_to_BGRA_FULL_8u, 0, 0, 0, 0, HSV_to_BGRA_FULL_32f},
                {HSV4_to_BGRA_FULL_8u, 0, 0, 0, 0, HSV4_to_BGRA_FULL_32f}
1215 1216 1217 1218 1219
            }
        };

        if (dcn <= 0) dcn = 3;

1220 1221 1222 1223 1224
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1225

1226 1227
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1228

1229
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1230 1231
    }

1232
    void RGB_to_HLS_FULL(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1233
    {
1234
        using namespace cv::cuda::device;
1235 1236 1237
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1238 1239
                {RGB_to_HLS_FULL_8u, 0, 0, 0, 0, RGB_to_HLS_FULL_32f},
                {RGBA_to_HLS_FULL_8u, 0, 0, 0, 0, RGBA_to_HLS_FULL_32f},
1240 1241
            },
            {
1242 1243
                {RGB_to_HLS4_FULL_8u, 0, 0, 0, 0, RGB_to_HLS4_FULL_32f},
                {RGBA_to_HLS4_FULL_8u, 0, 0, 0, 0, RGBA_to_HLS4_FULL_32f},
1244 1245 1246 1247 1248
            }
        };

        if (dcn <= 0) dcn = 3;

1249
        GpuMat src = _src.getGpuMat();
1250

1251 1252 1253 1254 1255 1256
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1257

1258
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1259 1260
    }

1261
    void BGR_to_HLS_FULL(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1262
    {
1263
        using namespace cv::cuda::device;
1264 1265 1266
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1267 1268
                {BGR_to_HLS_FULL_8u, 0, 0, 0, 0, BGR_to_HLS_FULL_32f},
                {BGRA_to_HLS_FULL_8u, 0, 0, 0, 0, BGRA_to_HLS_FULL_32f}
1269 1270
            },
            {
1271 1272
                {BGR_to_HLS4_FULL_8u, 0, 0, 0, 0, BGR_to_HLS4_FULL_32f},
                {BGRA_to_HLS4_FULL_8u, 0, 0, 0, 0, BGRA_to_HLS4_FULL_32f}
1273 1274 1275 1276 1277
            }
        };

        if (dcn <= 0) dcn = 3;

1278 1279 1280 1281 1282
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1283

1284 1285
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1286

1287
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1288 1289
    }

1290
    void HLS_to_RGB_FULL(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1291
    {
1292
        using namespace cv::cuda::device;
1293 1294 1295
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1296 1297
                {HLS_to_RGB_FULL_8u, 0, 0, 0, 0, HLS_to_RGB_FULL_32f},
                {HLS4_to_RGB_FULL_8u, 0, 0, 0, 0, HLS4_to_RGB_FULL_32f}
1298 1299
            },
            {
1300 1301
                {HLS_to_RGBA_FULL_8u, 0, 0, 0, 0, HLS_to_RGBA_FULL_32f},
                {HLS4_to_RGBA_FULL_8u, 0, 0, 0, 0, HLS4_to_RGBA_FULL_32f}
1302 1303 1304 1305 1306
            }
        };

        if (dcn <= 0) dcn = 3;

1307
        GpuMat src = _src.getGpuMat();
1308

1309 1310 1311 1312 1313 1314
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1315

1316
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1317 1318
    }

1319
    void HLS_to_BGR_FULL(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1320
    {
1321
        using namespace cv::cuda::device;
1322 1323 1324
        static const gpu_func_t funcs[2][2][6] =
        {
            {
1325 1326
                {HLS_to_BGR_FULL_8u, 0, 0, 0, 0, HLS_to_BGR_FULL_32f},
                {HLS4_to_BGR_FULL_8u, 0, 0, 0, 0, HLS4_to_BGR_FULL_32f}
1327 1328
            },
            {
1329 1330
                {HLS_to_BGRA_FULL_8u, 0, 0, 0, 0, HLS_to_BGRA_FULL_32f},
                {HLS4_to_BGRA_FULL_8u, 0, 0, 0, 0, HLS4_to_BGRA_FULL_32f}
1331 1332 1333 1334 1335
            }
        };

        if (dcn <= 0) dcn = 3;

1336
        GpuMat src = _src.getGpuMat();
1337

1338 1339 1340 1341 1342 1343
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1344

1345
        funcs[dcn == 4][src.channels() == 4][src.depth()](src, dst, stream);
1346 1347
    }

1348
    void BGR_to_Lab(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1349
    {
1350
        using namespace cv::cuda::device;
1351 1352 1353
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1354 1355
                {BGR_to_Lab_8u, BGR_to_Lab_32f},
                {BGRA_to_Lab_8u, BGRA_to_Lab_32f}
1356 1357
            },
            {
1358 1359
                {BGR_to_Lab4_8u, BGR_to_Lab4_32f},
                {BGRA_to_Lab4_8u, BGRA_to_Lab4_32f}
1360 1361 1362 1363 1364
            }
        };

        if (dcn <= 0) dcn = 3;

1365
        GpuMat src = _src.getGpuMat();
1366

1367 1368 1369 1370 1371 1372
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1373

1374
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1375 1376
    }

1377
    void RGB_to_Lab(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1378
    {
1379
        using namespace cv::cuda::device;
1380 1381 1382
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1383 1384
                {RGB_to_Lab_8u, RGB_to_Lab_32f},
                {RGBA_to_Lab_8u, RGBA_to_Lab_32f}
1385 1386
            },
            {
1387 1388
                {RGB_to_Lab4_8u, RGB_to_Lab4_32f},
                {RGBA_to_Lab4_8u, RGBA_to_Lab4_32f}
1389 1390 1391 1392 1393
            }
        };

        if (dcn <= 0) dcn = 3;

1394 1395 1396 1397 1398
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1399

1400 1401
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1402

1403
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1404 1405
    }

1406
    void LBGR_to_Lab(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1407
    {
1408
        using namespace cv::cuda::device;
1409 1410 1411
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1412 1413
                {LBGR_to_Lab_8u, LBGR_to_Lab_32f},
                {LBGRA_to_Lab_8u, LBGRA_to_Lab_32f}
1414 1415
            },
            {
1416 1417
                {LBGR_to_Lab4_8u, LBGR_to_Lab4_32f},
                {LBGRA_to_Lab4_8u, LBGRA_to_Lab4_32f}
1418 1419 1420 1421 1422
            }
        };

        if (dcn <= 0) dcn = 3;

1423
        GpuMat src = _src.getGpuMat();
1424

1425 1426 1427 1428 1429 1430
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1431

1432
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1433 1434
    }

1435
    void LRGB_to_Lab(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1436
    {
1437
        using namespace cv::cuda::device;
1438 1439 1440
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1441 1442
                {LRGB_to_Lab_8u, LRGB_to_Lab_32f},
                {LRGBA_to_Lab_8u, LRGBA_to_Lab_32f}
1443 1444
            },
            {
1445 1446
                {LRGB_to_Lab4_8u, LRGB_to_Lab4_32f},
                {LRGBA_to_Lab4_8u, LRGBA_to_Lab4_32f}
1447 1448 1449 1450 1451
            }
        };

        if (dcn <= 0) dcn = 3;

1452 1453 1454 1455 1456
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1457

1458 1459
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1460

1461
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1462 1463
    }

1464
    void Lab_to_BGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1465
    {
1466
        using namespace cv::cuda::device;
1467 1468 1469
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1470 1471
                {Lab_to_BGR_8u, Lab_to_BGR_32f},
                {Lab4_to_BGR_8u, Lab4_to_BGR_32f}
1472 1473
            },
            {
1474 1475
                {Lab_to_BGRA_8u, Lab_to_BGRA_32f},
                {Lab4_to_BGRA_8u, Lab4_to_BGRA_32f}
1476 1477
            }
        };
1478

1479
        if (dcn <= 0) dcn = 3;
1480

1481 1482 1483 1484 1485
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1486

1487 1488
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1489

1490
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1491 1492
    }

1493
    void Lab_to_RGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1494
    {
1495
        using namespace cv::cuda::device;
1496 1497 1498
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1499 1500
                {Lab_to_RGB_8u, Lab_to_RGB_32f},
                {Lab4_to_RGB_8u, Lab4_to_RGB_32f}
1501 1502
            },
            {
1503 1504
                {Lab_to_RGBA_8u, Lab_to_RGBA_32f},
                {Lab4_to_RGBA_8u, Lab4_to_RGBA_32f}
1505 1506
            }
        };
1507

1508
        if (dcn <= 0) dcn = 3;
1509

1510 1511 1512 1513 1514
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1515

1516 1517
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1518

1519
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1520 1521
    }

1522
    void Lab_to_LBGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1523
    {
1524
        using namespace cv::cuda::device;
1525 1526 1527
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1528 1529
                {Lab_to_LBGR_8u, Lab_to_LBGR_32f},
                {Lab4_to_LBGR_8u, Lab4_to_LBGR_32f}
1530 1531
            },
            {
1532 1533
                {Lab_to_LBGRA_8u, Lab_to_LBGRA_32f},
                {Lab4_to_LBGRA_8u, Lab4_to_LBGRA_32f}
1534 1535 1536 1537 1538
            }
        };

        if (dcn <= 0) dcn = 3;

1539
        GpuMat src = _src.getGpuMat();
1540

1541 1542 1543 1544 1545 1546
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1547

1548
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1549 1550
    }

1551
    void Lab_to_LRGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1552
    {
1553
        using namespace cv::cuda::device;
1554 1555 1556
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1557 1558
                {Lab_to_LRGB_8u, Lab_to_LRGB_32f},
                {Lab4_to_LRGB_8u, Lab4_to_LRGB_32f}
1559 1560
            },
            {
1561 1562
                {Lab_to_LRGBA_8u, Lab_to_LRGBA_32f},
                {Lab4_to_LRGBA_8u, Lab4_to_LRGBA_32f}
1563 1564 1565 1566 1567
            }
        };

        if (dcn <= 0) dcn = 3;

1568 1569 1570 1571 1572
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1573

1574 1575
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1576

1577
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1578 1579
    }

1580
    void BGR_to_Luv(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1581
    {
1582
        using namespace cv::cuda::device;
1583 1584 1585
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1586 1587
                {BGR_to_Luv_8u, BGR_to_Luv_32f},
                {BGRA_to_Luv_8u, BGRA_to_Luv_32f}
1588 1589
            },
            {
1590 1591
                {BGR_to_Luv4_8u, BGR_to_Luv4_32f},
                {BGRA_to_Luv4_8u, BGRA_to_Luv4_32f}
1592 1593
            }
        };
1594

1595
        if (dcn <= 0) dcn = 3;
1596

1597 1598 1599 1600 1601
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1602

1603 1604
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1605

1606
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1607 1608
    }

1609
    void RGB_to_Luv(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1610
    {
1611
        using namespace cv::cuda::device;
1612 1613 1614
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1615 1616
                {RGB_to_Luv_8u, RGB_to_Luv_32f},
                {RGBA_to_Luv_8u, RGBA_to_Luv_32f}
1617 1618
            },
            {
1619 1620
                {RGB_to_Luv4_8u, RGB_to_Luv4_32f},
                {RGBA_to_Luv4_8u, RGBA_to_Luv4_32f}
1621 1622 1623 1624 1625
            }
        };

        if (dcn <= 0) dcn = 3;

1626
        GpuMat src = _src.getGpuMat();
1627

1628 1629 1630 1631 1632 1633
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1634

1635
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1636 1637
    }

1638
    void LBGR_to_Luv(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1639
    {
1640
        using namespace cv::cuda::device;
1641 1642 1643
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1644 1645
                {LBGR_to_Luv_8u, LBGR_to_Luv_32f},
                {LBGRA_to_Luv_8u, LBGRA_to_Luv_32f}
1646 1647
            },
            {
1648 1649
                {LBGR_to_Luv4_8u, LBGR_to_Luv4_32f},
                {LBGRA_to_Luv4_8u, LBGRA_to_Luv4_32f}
1650 1651 1652 1653
            }
        };

        if (dcn <= 0) dcn = 3;
1654

1655 1656 1657 1658 1659
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1660

1661 1662
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1663

1664
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1665
    }
1666

1667
    void LRGB_to_Luv(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1668
    {
1669
        using namespace cv::cuda::device;
1670 1671 1672
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1673 1674
                {LRGB_to_Luv_8u, LRGB_to_Luv_32f},
                {LRGBA_to_Luv_8u, LRGBA_to_Luv_32f}
1675 1676
            },
            {
1677 1678
                {LRGB_to_Luv4_8u, LRGB_to_Luv4_32f},
                {LRGBA_to_Luv4_8u, LRGBA_to_Luv4_32f}
1679 1680 1681 1682
            }
        };

        if (dcn <= 0) dcn = 3;
1683

1684
        GpuMat src = _src.getGpuMat();
1685

1686 1687 1688 1689 1690 1691
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1692

1693
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1694 1695
    }

1696
    void Luv_to_BGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1697
    {
1698
        using namespace cv::cuda::device;
1699 1700 1701
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1702 1703
                {Luv_to_BGR_8u, Luv_to_BGR_32f},
                {Luv4_to_BGR_8u, Luv4_to_BGR_32f}
1704 1705
            },
            {
1706 1707
                {Luv_to_BGRA_8u, Luv_to_BGRA_32f},
                {Luv4_to_BGRA_8u, Luv4_to_BGRA_32f}
1708 1709 1710 1711 1712
            }
        };

        if (dcn <= 0) dcn = 3;

1713 1714 1715 1716 1717
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1718

1719 1720
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1721

1722
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1723 1724
    }

1725
    void Luv_to_RGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1726
    {
1727
        using namespace cv::cuda::device;
1728 1729 1730
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1731 1732
                {Luv_to_RGB_8u, Luv_to_RGB_32f},
                {Luv4_to_RGB_8u, Luv4_to_RGB_32f}
1733 1734
            },
            {
1735 1736
                {Luv_to_RGBA_8u, Luv_to_RGBA_32f},
                {Luv4_to_RGBA_8u, Luv4_to_RGBA_32f}
1737 1738 1739 1740 1741
            }
        };

        if (dcn <= 0) dcn = 3;

1742
        GpuMat src = _src.getGpuMat();
1743

1744 1745 1746 1747 1748 1749
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1750

1751
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1752
    }
1753

1754
    void Luv_to_LBGR(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1755
    {
1756
        using namespace cv::cuda::device;
1757 1758 1759
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1760 1761
                {Luv_to_LBGR_8u, Luv_to_LBGR_32f},
                {Luv4_to_LBGR_8u, Luv4_to_LBGR_32f}
1762 1763
            },
            {
1764 1765
                {Luv_to_LBGRA_8u, Luv_to_LBGRA_32f},
                {Luv4_to_LBGRA_8u, Luv4_to_LBGRA_32f}
1766 1767 1768 1769
            }
        };

        if (dcn <= 0) dcn = 3;
1770

1771 1772 1773 1774 1775
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );
1776

1777 1778
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1779

1780
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1781 1782
    }

1783
    void Luv_to_LRGB(InputArray _src, OutputArray _dst, int dcn, Stream& stream)
1784
    {
1785
        using namespace cv::cuda::device;
1786 1787 1788
        static const gpu_func_t funcs[2][2][2] =
        {
            {
1789 1790
                {Luv_to_LRGB_8u, Luv_to_LRGB_32f},
                {Luv4_to_LRGB_8u, Luv4_to_LRGB_32f}
1791 1792
            },
            {
1793 1794
                {Luv_to_LRGBA_8u, Luv_to_LRGBA_32f},
                {Luv4_to_LRGBA_8u, Luv4_to_LRGBA_32f}
1795 1796 1797 1798 1799
            }
        };

        if (dcn <= 0) dcn = 3;

1800
        GpuMat src = _src.getGpuMat();
1801

1802 1803 1804 1805 1806 1807
        CV_Assert( src.depth() == CV_8U || src.depth() == CV_32F );
        CV_Assert( src.channels() == 3 || src.channels() == 4 );
        CV_Assert( dcn == 3 || dcn == 4 );

        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1808

1809
        funcs[dcn == 4][src.channels() == 4][src.depth() == CV_32F](src, dst, stream);
1810 1811
    }

1812
    void RGBA_to_mBGRA(InputArray _src, OutputArray _dst, int, Stream& _stream)
1813 1814
    {
    #if (CUDA_VERSION < 5000)
1815 1816 1817
        (void) _src;
        (void) _dst;
        (void) _stream;
1818
        CV_Error( Error::StsBadFlag, "Unknown/unsupported color conversion code" );
1819
    #else
1820
        GpuMat src = _src.getGpuMat();
1821

1822
        CV_Assert( src.type() == CV_8UC4 || src.type() == CV_16UC4 );
1823

1824 1825 1826 1827
        _dst.create(src.size(), src.type());
        GpuMat dst = _dst.getGpuMat();

        cudaStream_t stream = StreamAccessor::getStream(_stream);
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
        NppStreamHandler h(stream);

        NppiSize oSizeROI;
        oSizeROI.width = src.cols;
        oSizeROI.height = src.rows;

        if (src.depth() == CV_8U)
            nppSafeCall( nppiAlphaPremul_8u_AC4R(src.ptr<Npp8u>(), static_cast<int>(src.step), dst.ptr<Npp8u>(), static_cast<int>(dst.step), oSizeROI) );
        else
            nppSafeCall( nppiAlphaPremul_16u_AC4R(src.ptr<Npp16u>(), static_cast<int>(src.step), dst.ptr<Npp16u>(), static_cast<int>(dst.step), oSizeROI) );

        if (stream == 0)
1840
            cudaSafeCall( cudaDeviceSynchronize() );
1841 1842 1843
    #endif
    }

1844
    void bayer_to_BGR(InputArray _src, OutputArray _dst, int dcn, bool blue_last, bool start_with_green, Stream& stream)
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
    {
        typedef void (*func_t)(PtrStepSzb src, PtrStepSzb dst, bool blue_last, bool start_with_green, cudaStream_t stream);
        static const func_t funcs[3][4] =
        {
            {0,0,Bayer2BGR_8u_gpu<3>, Bayer2BGR_8u_gpu<4>},
            {0,0,0,0},
            {0,0,Bayer2BGR_16u_gpu<3>, Bayer2BGR_16u_gpu<4>}
        };

        if (dcn <= 0) dcn = 3;

1856 1857 1858 1859 1860
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.type() == CV_8UC1 || src.type() == CV_16UC1 );
        CV_Assert( src.rows > 2 && src.cols > 2 );
        CV_Assert( dcn == 3 || dcn == 4 );
1861

1862 1863
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), dcn));
        GpuMat dst = _dst.getGpuMat();
1864 1865 1866

        funcs[src.depth()][dcn - 1](src, dst, blue_last, start_with_green, StreamAccessor::getStream(stream));
    }
1867
    void bayerBG_to_BGR(InputArray src, OutputArray dst, int dcn, Stream& stream)
1868
    {
1869
        bayer_to_BGR(src, dst, dcn, false, false, stream);
1870
    }
1871
    void bayeRGB_to_BGR(InputArray src, OutputArray dst, int dcn, Stream& stream)
1872
    {
1873
        bayer_to_BGR(src, dst, dcn, false, true, stream);
1874
    }
1875
    void bayerRG_to_BGR(InputArray src, OutputArray dst, int dcn, Stream& stream)
1876
    {
1877
        bayer_to_BGR(src, dst, dcn, true, false, stream);
1878
    }
1879
    void bayerGR_to_BGR(InputArray src, OutputArray dst, int dcn, Stream& stream)
1880
    {
1881
        bayer_to_BGR(src, dst, dcn, true, true, stream);
1882
    }
1883

1884
    void bayer_to_gray(InputArray _src, OutputArray _dst, bool blue_last, bool start_with_green, Stream& stream)
1885 1886 1887 1888 1889 1890 1891 1892 1893
    {
        typedef void (*func_t)(PtrStepSzb src, PtrStepSzb dst, bool blue_last, bool start_with_green, cudaStream_t stream);
        static const func_t funcs[3] =
        {
            Bayer2BGR_8u_gpu<1>,
            0,
            Bayer2BGR_16u_gpu<1>,
        };

1894 1895 1896 1897
        GpuMat src = _src.getGpuMat();

        CV_Assert( src.type() == CV_8UC1 || src.type() == CV_16UC1 );
        CV_Assert( src.rows > 2 && src.cols > 2 );
1898

1899 1900
        _dst.create(src.size(), CV_MAKE_TYPE(src.depth(), 1));
        GpuMat dst = _dst.getGpuMat();
1901 1902 1903

        funcs[src.depth()](src, dst, blue_last, start_with_green, StreamAccessor::getStream(stream));
    }
1904
    void bayerBG_to_gray(InputArray src, OutputArray dst, int /*dcn*/, Stream& stream)
1905 1906 1907
    {
        bayer_to_gray(src, dst, false, false, stream);
    }
1908
    void bayeRGB_to_GRAY(InputArray src, OutputArray dst, int /*dcn*/, Stream& stream)
1909 1910 1911
    {
        bayer_to_gray(src, dst, false, true, stream);
    }
1912
    void bayerRG_to_gray(InputArray src, OutputArray dst, int /*dcn*/, Stream& stream)
1913 1914 1915
    {
        bayer_to_gray(src, dst, true, false, stream);
    }
1916
    void bayerGR_to_gray(InputArray src, OutputArray dst, int /*dcn*/, Stream& stream)
1917 1918 1919
    {
        bayer_to_gray(src, dst, true, true, stream);
    }
1920 1921
}

1922 1923 1924
////////////////////////////////////////////////////////////////////////
// cvtColor

1925
void cv::cuda::cvtColor(InputArray src, OutputArray dst, int code, int dcn, Stream& stream)
1926
{
1927
    typedef void (*func_t)(InputArray src, OutputArray dst, int dcn, Stream& stream);
1928 1929
    static const func_t funcs[] =
    {
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
        BGR_to_BGRA,            // CV_BGR2BGRA    =0
        BGRA_to_BGR,            // CV_BGRA2BGR    =1
        BGR_to_RGBA,            // CV_BGR2RGBA    =2
        BGRA_to_RGB,            // CV_RGBA2BGR    =3
        BGR_to_RGB,             // CV_BGR2RGB     =4
        BGRA_to_RGBA,           // CV_BGRA2RGBA   =5

        BGR_to_GRAY,            // CV_BGR2GRAY    =6
        RGB_to_GRAY,            // CV_RGB2GRAY    =7
        GRAY_to_BGR,            // CV_GRAY2BGR    =8
        GRAY_to_BGRA,           // CV_GRAY2BGRA   =9
        BGRA_to_GRAY,           // CV_BGRA2GRAY   =10
        RGBA_to_GRAY,           // CV_RGBA2GRAY   =11

        BGR_to_BGR565,          // CV_BGR2BGR565  =12
        RGB_to_BGR565,          // CV_RGB2BGR565  =13
        BGR565_to_BGR,          // CV_BGR5652BGR  =14
        BGR565_to_RGB,          // CV_BGR5652RGB  =15
        BGRA_to_BGR565,         // CV_BGRA2BGR565 =16
        RGBA_to_BGR565,         // CV_RGBA2BGR565 =17
        BGR565_to_BGRA,         // CV_BGR5652BGRA =18
        BGR565_to_RGBA,         // CV_BGR5652RGBA =19

        GRAY_to_BGR565,         // CV_GRAY2BGR565 =20
        BGR565_to_GRAY,         // CV_BGR5652GRAY =21

        BGR_to_BGR555,          // CV_BGR2BGR555  =22
        RGB_to_BGR555,          // CV_RGB2BGR555  =23
        BGR555_to_BGR,          // CV_BGR5552BGR  =24
        BGR555_to_RGB,          // CV_BGR5552RGB  =25
        BGRA_to_BGR555,         // CV_BGRA2BGR555 =26
        RGBA_to_BGR555,         // CV_RGBA2BGR555 =27
        BGR555_to_BGRA,         // CV_BGR5552BGRA =28
        BGR555_to_RGBA,         // CV_BGR5552RGBA =29

        GRAY_to_BGR555,         // CV_GRAY2BGR555 =30
        BGR555_to_GRAY,         // CV_BGR5552GRAY =31

        BGR_to_XYZ,             // CV_BGR2XYZ     =32
        RGB_to_XYZ,             // CV_RGB2XYZ     =33
        XYZ_to_BGR,             // CV_XYZ2BGR     =34
        XYZ_to_RGB,             // CV_XYZ2RGB     =35

        BGR_to_YCrCb,           // CV_BGR2YCrCb   =36
        RGB_to_YCrCb,           // CV_RGB2YCrCb   =37
        YCrCb_to_BGR,           // CV_YCrCb2BGR   =38
        YCrCb_to_RGB,           // CV_YCrCb2RGB   =39

        BGR_to_HSV,             // CV_BGR2HSV     =40
        RGB_to_HSV,             // CV_RGB2HSV     =41
1980 1981 1982 1983

        0,                      //                =42
        0,                      //                =43

1984 1985
        BGR_to_Lab,             // CV_BGR2Lab     =44
        RGB_to_Lab,             // CV_RGB2Lab     =45
1986

1987 1988 1989 1990
        bayerBG_to_BGR,         // CV_BayerBG2BGR =46
        bayeRGB_to_BGR,         // CV_BayeRGB2BGR =47
        bayerRG_to_BGR,         // CV_BayerRG2BGR =48
        bayerGR_to_BGR,         // CV_BayerGR2BGR =49
1991

1992 1993
        BGR_to_Luv,             // CV_BGR2Luv     =50
        RGB_to_Luv,             // CV_RGB2Luv     =51
1994

1995 1996
        BGR_to_HLS,             // CV_BGR2HLS     =52
        RGB_to_HLS,             // CV_RGB2HLS     =53
1997

1998 1999
        HSV_to_BGR,             // CV_HSV2BGR     =54
        HSV_to_RGB,             // CV_HSV2RGB     =55
2000

2001 2002 2003 2004
        Lab_to_BGR,             // CV_Lab2BGR     =56
        Lab_to_RGB,             // CV_Lab2RGB     =57
        Luv_to_BGR,             // CV_Luv2BGR     =58
        Luv_to_RGB,             // CV_Luv2RGB     =59
2005

2006 2007
        HLS_to_BGR,             // CV_HLS2BGR     =60
        HLS_to_RGB,             // CV_HLS2RGB     =61
2008 2009

        0,                      // CV_BayerBG2BGR_VNG =62
2010
        0,                      // CV_BayeRGB2BGR_VNG =63
2011 2012 2013
        0,                      // CV_BayerRG2BGR_VNG =64
        0,                      // CV_BayerGR2BGR_VNG =65

2014 2015 2016 2017
        BGR_to_HSV_FULL,        // CV_BGR2HSV_FULL = 66
        RGB_to_HSV_FULL,        // CV_RGB2HSV_FULL = 67
        BGR_to_HLS_FULL,        // CV_BGR2HLS_FULL = 68
        RGB_to_HLS_FULL,        // CV_RGB2HLS_FULL = 69
2018

2019 2020 2021 2022
        HSV_to_BGR_FULL,        // CV_HSV2BGR_FULL = 70
        HSV_to_RGB_FULL,        // CV_HSV2RGB_FULL = 71
        HLS_to_BGR_FULL,        // CV_HLS2BGR_FULL = 72
        HLS_to_RGB_FULL,        // CV_HLS2RGB_FULL = 73
2023

2024 2025 2026 2027
        LBGR_to_Lab,            // CV_LBGR2Lab     = 74
        LRGB_to_Lab,            // CV_LRGB2Lab     = 75
        LBGR_to_Luv,            // CV_LBGR2Luv     = 76
        LRGB_to_Luv,            // CV_LRGB2Luv     = 77
2028

2029 2030 2031 2032
        Lab_to_LBGR,            // CV_Lab2LBGR     = 78
        Lab_to_LRGB,            // CV_Lab2LRGB     = 79
        Luv_to_LBGR,            // CV_Luv2LBGR     = 80
        Luv_to_LRGB,            // CV_Luv2LRGB     = 81
2033

2034 2035 2036 2037
        BGR_to_YUV,             // CV_BGR2YUV      = 82
        RGB_to_YUV,             // CV_RGB2YUV      = 83
        YUV_to_BGR,             // CV_YUV2BGR      = 84
        YUV_to_RGB,             // CV_YUV2RGB      = 85
2038

2039
        bayerBG_to_gray,        // CV_BayerBG2GRAY = 86
2040
        bayeRGB_to_GRAY,        // CV_BayeRGB2GRAY = 87
2041 2042
        bayerRG_to_gray,        // CV_BayerRG2GRAY = 88
        bayerGR_to_gray,        // CV_BayerGR2GRAY = 89
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 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091

        //YUV 4:2:0 formats family
        0,                      // CV_YUV2RGB_NV12 = 90,
        0,                      // CV_YUV2BGR_NV12 = 91,
        0,                      // CV_YUV2RGB_NV21 = 92,
        0,                      // CV_YUV2BGR_NV21 = 93,

        0,                      // CV_YUV2RGBA_NV12 = 94,
        0,                      // CV_YUV2BGRA_NV12 = 95,
        0,                      // CV_YUV2RGBA_NV21 = 96,
        0,                      // CV_YUV2BGRA_NV21 = 97,

        0,                      // CV_YUV2RGB_YV12 = 98,
        0,                      // CV_YUV2BGR_YV12 = 99,
        0,                      // CV_YUV2RGB_IYUV = 100,
        0,                      // CV_YUV2BGR_IYUV = 101,

        0,                      // CV_YUV2RGBA_YV12 = 102,
        0,                      // CV_YUV2BGRA_YV12 = 103,
        0,                      // CV_YUV2RGBA_IYUV = 104,
        0,                      // CV_YUV2BGRA_IYUV = 105,

        0,                      // CV_YUV2GRAY_420 = 106,

        //YUV 4:2:2 formats family
        0,                      // CV_YUV2RGB_UYVY = 107,
        0,                      // CV_YUV2BGR_UYVY = 108,
        0,                      // //CV_YUV2RGB_VYUY = 109,
        0,                      // //CV_YUV2BGR_VYUY = 110,

        0,                      // CV_YUV2RGBA_UYVY = 111,
        0,                      // CV_YUV2BGRA_UYVY = 112,
        0,                      // //CV_YUV2RGBA_VYUY = 113,
        0,                      // //CV_YUV2BGRA_VYUY = 114,

        0,                      // CV_YUV2RGB_YUY2 = 115,
        0,                      // CV_YUV2BGR_YUY2 = 116,
        0,                      // CV_YUV2RGB_YVYU = 117,
        0,                      // CV_YUV2BGR_YVYU = 118,

        0,                      // CV_YUV2RGBA_YUY2 = 119,
        0,                      // CV_YUV2BGRA_YUY2 = 120,
        0,                      // CV_YUV2RGBA_YVYU = 121,
        0,                      // CV_YUV2BGRA_YVYU = 122,

        0,                      // CV_YUV2GRAY_UYVY = 123,
        0,                      // CV_YUV2GRAY_YUY2 = 124,

        // alpha premultiplication
2092
        RGBA_to_mBGRA,          // CV_RGBA2mRGBA = 125,
2093 2094 2095 2096 2097
        0,                      // CV_mRGBA2RGBA = 126,

        0,                      // CV_COLORCVT_MAX  = 127
    };

2098
    CV_Assert( code < 128 );
2099 2100 2101 2102

    func_t func = funcs[code];

    if (func == 0)
2103
        CV_Error(Error::StsBadFlag, "Unknown/unsupported color conversion code");
2104 2105 2106 2107

    func(src, dst, dcn, stream);
}

2108 2109 2110
////////////////////////////////////////////////////////////////////////
// demosaicing

2111
void cv::cuda::demosaicing(InputArray _src, OutputArray _dst, int code, int dcn, Stream& stream)
2112
{
2113
    CV_Assert( !_src.empty() );
2114 2115 2116

    switch (code)
    {
2117
    case cv::COLOR_BayerBG2GRAY: case cv::COLOR_BayerGB2GRAY: case cv::COLOR_BayerRG2GRAY: case cv::COLOR_BayerGR2GRAY:
2118
        bayer_to_gray(_src, _dst, code == cv::COLOR_BayerBG2GRAY || code == cv::COLOR_BayerGB2GRAY, code == cv::COLOR_BayerGB2GRAY || code == cv::COLOR_BayerGR2GRAY, stream);
2119 2120
        break;

2121
    case cv::COLOR_BayerBG2BGR: case cv::COLOR_BayerGB2BGR: case cv::COLOR_BayerRG2BGR: case cv::COLOR_BayerGR2BGR:
2122
        bayer_to_BGR(_src, _dst, dcn, code == cv::COLOR_BayerBG2BGR || code == cv::COLOR_BayerGB2BGR, code == cv::COLOR_BayerGB2BGR || code == cv::COLOR_BayerGR2BGR, stream);
2123 2124 2125 2126
        break;

    case COLOR_BayerBG2BGR_MHT: case COLOR_BayerGB2BGR_MHT: case COLOR_BayerRG2BGR_MHT: case COLOR_BayerGR2BGR_MHT:
    {
2127 2128 2129 2130
        if (dcn <= 0) dcn = 3;

        GpuMat src = _src.getGpuMat();
        const int depth = _src.depth();
2131 2132

        CV_Assert( depth == CV_8U );
2133
        CV_Assert( src.channels() == 1 );
2134 2135
        CV_Assert( dcn == 3 || dcn == 4 );

2136 2137 2138 2139
        _dst.create(_src.size(), CV_MAKE_TYPE(depth, dcn));
        GpuMat dst = _dst.getGpuMat();

        dst.setTo(Scalar::all(0), stream);
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149

        Size wholeSize;
        Point ofs;
        src.locateROI(wholeSize, ofs);
        PtrStepSzb srcWhole(wholeSize.height, wholeSize.width, src.datastart, src.step);

        const int2 firstRed = make_int2(code == COLOR_BayerRG2BGR_MHT || code == COLOR_BayerGB2BGR_MHT ? 0 : 1,
                                        code == COLOR_BayerRG2BGR_MHT || code == COLOR_BayerGR2BGR_MHT ? 0 : 1);

        if (dcn == 3)
2150
            cv::cuda::device::MHCdemosaic<3>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2151
        else
2152
            cv::cuda::device::MHCdemosaic<4>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2153 2154 2155 2156 2157 2158

        break;
    }

    case COLOR_BayerBG2GRAY_MHT: case COLOR_BayerGB2GRAY_MHT: case COLOR_BayerRG2GRAY_MHT: case COLOR_BayerGR2GRAY_MHT:
    {
2159 2160 2161
        GpuMat src = _src.getGpuMat();
        const int depth = _src.depth();

2162 2163
        CV_Assert( depth == CV_8U );

2164 2165 2166 2167
        _dst.create(_src.size(), CV_MAKE_TYPE(depth, 1));
        GpuMat dst = _dst.getGpuMat();

        dst.setTo(Scalar::all(0), stream);
2168 2169 2170 2171 2172 2173 2174 2175 2176

        Size wholeSize;
        Point ofs;
        src.locateROI(wholeSize, ofs);
        PtrStepSzb srcWhole(wholeSize.height, wholeSize.width, src.datastart, src.step);

        const int2 firstRed = make_int2(code == COLOR_BayerRG2BGR_MHT || code == COLOR_BayerGB2BGR_MHT ? 0 : 1,
                                        code == COLOR_BayerRG2BGR_MHT || code == COLOR_BayerGR2BGR_MHT ? 0 : 1);

2177
        cv::cuda::device::MHCdemosaic<1>(srcWhole, make_int2(ofs.x, ofs.y), dst, firstRed, StreamAccessor::getStream(stream));
2178 2179 2180 2181 2182

        break;
    }

    default:
2183
        CV_Error(Error::StsBadFlag, "Unknown / unsupported color conversion code");
2184 2185 2186
    }
}

2187 2188 2189
////////////////////////////////////////////////////////////////////////
// swapChannels

2190
void cv::cuda::swapChannels(InputOutputArray _image, const int dstOrder[4], Stream& _stream)
2191
{
2192
    GpuMat image = _image.getGpuMat();
2193

2194
    CV_Assert( image.type() == CV_8UC4 );
2195

2196
    cudaStream_t stream = StreamAccessor::getStream(_stream);
2197 2198 2199 2200 2201 2202 2203 2204 2205
    NppStreamHandler h(stream);

    NppiSize sz;
    sz.width  = image.cols;
    sz.height = image.rows;

    nppSafeCall( nppiSwapChannels_8u_C4IR(image.ptr<Npp8u>(), static_cast<int>(image.step), sz, dstOrder) );

    if (stream == 0)
2206
        cudaSafeCall( cudaDeviceSynchronize() );
2207 2208
}

2209 2210 2211
////////////////////////////////////////////////////////////////////////
// gammaCorrection

2212
void cv::cuda::gammaCorrection(InputArray _src, OutputArray _dst, bool forward, Stream& stream)
2213 2214
{
#if (CUDA_VERSION < 5000)
2215 2216 2217 2218 2219
    (void) _src;
    (void) _dst;
    (void) forward;
    (void) stream;
    CV_Error(Error::StsNotImplemented, "This function works only with CUDA 5.0 or higher");
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
#else
    typedef NppStatus (*func_t)(const Npp8u* pSrc, int nSrcStep, Npp8u* pDst, int nDstStep, NppiSize oSizeROI);
    typedef NppStatus (*func_inplace_t)(Npp8u* pSrcDst, int nSrcDstStep, NppiSize oSizeROI);

    static const func_t funcs[2][5] =
    {
        {0, 0, 0, nppiGammaInv_8u_C3R, nppiGammaInv_8u_AC4R},
        {0, 0, 0, nppiGammaFwd_8u_C3R, nppiGammaFwd_8u_AC4R}
    };
    static const func_inplace_t funcs_inplace[2][5] =
    {
        {0, 0, 0, nppiGammaInv_8u_C3IR, nppiGammaInv_8u_AC4IR},
        {0, 0, 0, nppiGammaFwd_8u_C3IR, nppiGammaFwd_8u_AC4IR}
    };

2235 2236 2237
    GpuMat src = _src.getGpuMat();

    CV_Assert( src.type() == CV_8UC3 || src.type() == CV_8UC4 );
2238

2239 2240
    _dst.create(src.size(), src.type());
    GpuMat dst = _dst.getGpuMat();
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255

    NppStreamHandler h(StreamAccessor::getStream(stream));

    NppiSize oSizeROI;
    oSizeROI.width = src.cols;
    oSizeROI.height = src.rows;

    if (dst.data == src.data)
        funcs_inplace[forward][src.channels()](dst.ptr<Npp8u>(), static_cast<int>(src.step), oSizeROI);
    else
        funcs[forward][src.channels()](src.ptr<Npp8u>(), static_cast<int>(src.step), dst.ptr<Npp8u>(), static_cast<int>(dst.step), oSizeROI);

#endif
}

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
////////////////////////////////////////////////////////////////////////
// alphaComp

namespace
{
    template <int DEPTH> struct NppAlphaCompFunc
    {
        typedef typename NPPTypeTraits<DEPTH>::npp_type npp_t;

        typedef NppStatus (*func_t)(const npp_t* pSrc1, int nSrc1Step, const npp_t* pSrc2, int nSrc2Step, npp_t* pDst, int nDstStep, NppiSize oSizeROI, NppiAlphaOp eAlphaOp);
    };

    template <int DEPTH, typename NppAlphaCompFunc<DEPTH>::func_t func> struct NppAlphaComp
    {
        typedef typename NPPTypeTraits<DEPTH>::npp_type npp_t;

        static void call(const GpuMat& img1, const GpuMat& img2, GpuMat& dst, NppiAlphaOp eAlphaOp, cudaStream_t stream)
        {
            NppStreamHandler h(stream);

            NppiSize oSizeROI;
            oSizeROI.width = img1.cols;
            oSizeROI.height = img2.rows;

            nppSafeCall( func(img1.ptr<npp_t>(), static_cast<int>(img1.step), img2.ptr<npp_t>(), static_cast<int>(img2.step),
                              dst.ptr<npp_t>(), static_cast<int>(dst.step), oSizeROI, eAlphaOp) );

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

2289
void cv::cuda::alphaComp(InputArray _img1, InputArray _img2, OutputArray _dst, int alpha_op, Stream& stream)
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
{
    static const NppiAlphaOp npp_alpha_ops[] = {
        NPPI_OP_ALPHA_OVER,
        NPPI_OP_ALPHA_IN,
        NPPI_OP_ALPHA_OUT,
        NPPI_OP_ALPHA_ATOP,
        NPPI_OP_ALPHA_XOR,
        NPPI_OP_ALPHA_PLUS,
        NPPI_OP_ALPHA_OVER_PREMUL,
        NPPI_OP_ALPHA_IN_PREMUL,
        NPPI_OP_ALPHA_OUT_PREMUL,
        NPPI_OP_ALPHA_ATOP_PREMUL,
        NPPI_OP_ALPHA_XOR_PREMUL,
        NPPI_OP_ALPHA_PLUS_PREMUL,
        NPPI_OP_ALPHA_PREMUL
    };

    typedef void (*func_t)(const GpuMat& img1, const GpuMat& img2, GpuMat& dst, NppiAlphaOp eAlphaOp, cudaStream_t stream);
    static const func_t funcs[] =
    {
        NppAlphaComp<CV_8U, nppiAlphaComp_8u_AC4R>::call,
        0,
        NppAlphaComp<CV_16U, nppiAlphaComp_16u_AC4R>::call,
        0,
        NppAlphaComp<CV_32S, nppiAlphaComp_32s_AC4R>::call,
        NppAlphaComp<CV_32F, nppiAlphaComp_32f_AC4R>::call
    };

2318 2319 2320
    GpuMat img1 = _img1.getGpuMat();
    GpuMat img2 = _img2.getGpuMat();

2321 2322 2323
    CV_Assert( img1.type() == CV_8UC4 || img1.type() == CV_16UC4 || img1.type() == CV_32SC4 || img1.type() == CV_32FC4 );
    CV_Assert( img1.size() == img2.size() && img1.type() == img2.type() );

2324 2325
    _dst.create(img1.size(), img1.type());
    GpuMat dst = _dst.getGpuMat();
2326 2327 2328 2329 2330 2331

    const func_t func = funcs[img1.depth()];

    func(img1, img2, dst, npp_alpha_ops[alpha_op], StreamAccessor::getStream(stream));
}

2332
#endif /* !defined (HAVE_CUDA) */