CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / src / color.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Wang Weiyan, wangweiyanster@gmail.com
19 //    Peng Xiao, pengxiao@multicorewareinc.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other oclMaterials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors "as is" and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46
47 #include "precomp.hpp"
48
49 using namespace cv;
50 using namespace cv::ocl;
51
52 #ifndef CV_DESCALE
53 #define CV_DESCALE(x, n) (((x) + (1 << ((n)-1))) >> (n))
54 #endif
55
56 #ifndef FLT_EPSILON
57 #define FLT_EPSILON     1.192092896e-07F
58 #endif
59
60 namespace cv
61 {
62 namespace ocl
63 {
64 extern const char *cvt_color;
65 }
66 }
67
68 namespace
69 {
70 void RGB2Gray_caller(const oclMat &src, oclMat &dst, int bidx)
71 {
72     vector<pair<size_t , const void *> > args;
73     int channels = src.oclchannels();
74     char build_options[50];
75     sprintf(build_options, "-D DEPTH_%d", src.depth());
76     //printf("depth:%d,channels:%d,bidx:%d\n",src.depth(),src.oclchannels(),bidx);
77     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols));
78     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows));
79     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step));
80     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.step));
81     args.push_back( make_pair( sizeof(cl_int) , (void *)&channels));
82     args.push_back( make_pair( sizeof(cl_int) , (void *)&bidx));
83     args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
84     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data));
85     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
86     openCLExecuteKernel(src.clCxt, &cvt_color, "RGB2Gray", gt, lt, args, -1, -1, build_options);
87 }
88 void Gray2RGB_caller(const oclMat &src, oclMat &dst)
89 {
90     vector<pair<size_t , const void *> > args;
91     char build_options[50];
92     sprintf(build_options, "-D DEPTH_%d", src.depth());
93     //printf("depth:%d,channels:%d,bidx:%d\n",src.depth(),src.oclchannels(),bidx);
94     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols));
95     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows));
96     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step));
97     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.step));
98     args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
99     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data));
100     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
101     openCLExecuteKernel(src.clCxt, &cvt_color, "Gray2RGB", gt, lt, args, -1, -1, build_options);
102 }
103 void RGB2YUV_caller(const oclMat &src, oclMat &dst, int bidx)
104 {
105     vector<pair<size_t , const void *> > args;
106     int channels = src.oclchannels();
107     char build_options[50];
108     sprintf(build_options, "-D DEPTH_%d", src.depth());
109     //printf("depth:%d,channels:%d,bidx:%d\n",src.depth(),src.oclchannels(),bidx);
110     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols));
111     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows));
112     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step));
113     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.step));
114     args.push_back( make_pair( sizeof(cl_int) , (void *)&channels));
115     args.push_back( make_pair( sizeof(cl_int) , (void *)&bidx));
116     args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
117     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data));
118     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
119     openCLExecuteKernel(src.clCxt, &cvt_color, "RGB2YUV", gt, lt, args, -1, -1, build_options);
120 }
121 void YUV2RGB_caller(const oclMat &src, oclMat &dst, int bidx)
122 {
123     vector<pair<size_t , const void *> > args;
124     int channels = src.oclchannels();
125     char build_options[50];
126     sprintf(build_options, "-D DEPTH_%d", src.depth());
127     //printf("depth:%d,channels:%d,bidx:%d\n",src.depth(),src.oclchannels(),bidx);
128     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols));
129     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows));
130     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step));
131     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.step));
132     args.push_back( make_pair( sizeof(cl_int) , (void *)&channels));
133     args.push_back( make_pair( sizeof(cl_int) , (void *)&bidx));
134     args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
135     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data));
136     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
137     openCLExecuteKernel(src.clCxt, &cvt_color, "YUV2RGB", gt, lt, args, -1, -1, build_options);
138 }
139 void YUV2RGB_NV12_caller(const oclMat &src, oclMat &dst, int bidx)
140 {
141     vector<pair<size_t , const void *> > args;
142     char build_options[50];
143     sprintf(build_options, "-D DEPTH_%d", src.depth());
144     //printf("depth:%d,channels:%d,bidx:%d\n",src.depth(),src.oclchannels(),bidx);
145     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols));
146     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows));
147     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step));
148     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.step));
149     args.push_back( make_pair( sizeof(cl_int) , (void *)&bidx));
150     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols));
151     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows));
152     args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
153     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data));
154     size_t gt[3] = {dst.cols / 2, dst.rows / 2, 1}, lt[3] = {16, 16, 1};
155     openCLExecuteKernel(src.clCxt, &cvt_color, "YUV2RGBA_NV12", gt, lt, args, -1, -1, build_options);
156 }
157 void RGB2YCrCb_caller(const oclMat &src, oclMat &dst, int bidx)
158 {
159     vector<pair<size_t , const void *> > args;
160     int channels = src.oclchannels();
161     char build_options[50];
162     sprintf(build_options, "-D DEPTH_%d", src.depth());
163     //printf("depth:%d,channels:%d,bidx:%d\n",src.depth(),src.oclchannels(),bidx);
164     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols));
165     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows));
166     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step));
167     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.step));
168     args.push_back( make_pair( sizeof(cl_int) , (void *)&channels));
169     args.push_back( make_pair( sizeof(cl_int) , (void *)&bidx));
170     args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
171     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data));
172     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
173     openCLExecuteKernel(src.clCxt, &cvt_color, "RGB2YCrCb", gt, lt, args, -1, -1, build_options);
174 }
175 void cvtColor_caller(const oclMat &src, oclMat &dst, int code, int dcn)
176 {
177     Size sz = src.size();
178     int scn = src.oclchannels(), depth = src.depth(), bidx;
179
180     CV_Assert(depth == CV_8U || depth == CV_16U || depth == CV_32F);
181
182     switch (code)
183     {
184         /*
185         case CV_BGR2BGRA: case CV_RGB2BGRA: case CV_BGRA2BGR:
186         case CV_RGBA2BGR: case CV_RGB2BGR: case CV_BGRA2RGBA:
187         case CV_BGR2BGR565: case CV_BGR2BGR555: case CV_RGB2BGR565: case CV_RGB2BGR555:
188         case CV_BGRA2BGR565: case CV_BGRA2BGR555: case CV_RGBA2BGR565: case CV_RGBA2BGR555:
189         case CV_BGR5652BGR: case CV_BGR5552BGR: case CV_BGR5652RGB: case CV_BGR5552RGB:
190         case CV_BGR5652BGRA: case CV_BGR5552BGRA: case CV_BGR5652RGBA: case CV_BGR5552RGBA:
191         */
192     case CV_BGR2GRAY:
193     case CV_BGRA2GRAY:
194     case CV_RGB2GRAY:
195     case CV_RGBA2GRAY:
196     {
197         CV_Assert(scn == 3 || scn == 4);
198         bidx = code == CV_BGR2GRAY || code == CV_BGRA2GRAY ? 0 : 2;
199         dst.create(sz, CV_MAKETYPE(depth, 1));
200         RGB2Gray_caller(src, dst, bidx);
201         break;
202     }
203     case CV_GRAY2BGR:
204     case CV_GRAY2BGRA:
205     {
206         CV_Assert(scn == 1);
207         dcn  = code == CV_GRAY2BGRA ? 4 : 3;
208         dst.create(sz, CV_MAKETYPE(depth, dcn));
209         Gray2RGB_caller(src, dst);
210         break;
211     }
212     case CV_BGR2YUV:
213     case CV_RGB2YUV:
214     {
215         CV_Assert(scn == 3 || scn == 4);
216         bidx = code == CV_BGR2YUV ? 0 : 2;
217         dst.create(sz, CV_MAKETYPE(depth, 3));
218         RGB2YUV_caller(src, dst, bidx);
219         break;
220     }
221     case CV_YUV2BGR:
222     case CV_YUV2RGB:
223     {
224         CV_Assert(scn == 3 || scn == 4);
225         bidx = code == CV_YUV2BGR ? 0 : 2;
226         dst.create(sz, CV_MAKETYPE(depth, 3));
227         YUV2RGB_caller(src, dst, bidx);
228         break;
229     }
230     case CV_YUV2RGB_NV12:
231     case CV_YUV2BGR_NV12:
232     case CV_YUV2RGBA_NV12:
233     case CV_YUV2BGRA_NV12:
234     {
235         CV_Assert(scn == 1);
236         CV_Assert( sz.width % 2 == 0 && sz.height % 3 == 0 && depth == CV_8U );
237         dcn  = code == CV_YUV2BGRA_NV12 || code == CV_YUV2RGBA_NV12 ? 4 : 3;
238         bidx = code == CV_YUV2BGRA_NV12 || code == CV_YUV2BGR_NV12 ? 0 : 2;
239
240         Size dstSz(sz.width, sz.height * 2 / 3);
241         dst.create(dstSz, CV_MAKETYPE(depth, dcn));
242         YUV2RGB_NV12_caller(src, dst, bidx);
243         break;
244     }
245     case CV_BGR2YCrCb:
246     case CV_RGB2YCrCb:
247     {
248         CV_Assert(scn == 3 || scn == 4);
249         bidx = code == CV_BGR2YCrCb ? 0 : 2;
250         dst.create(sz, CV_MAKETYPE(depth, 3));
251         RGB2YCrCb_caller(src, dst, bidx);
252         break;
253     }
254     case CV_YCrCb2BGR:
255     case CV_YCrCb2RGB:
256     {
257         break;
258     }
259     /*
260     case CV_BGR5652GRAY: case CV_BGR5552GRAY:
261     case CV_GRAY2BGR565: case CV_GRAY2BGR555:
262     case CV_BGR2YCrCb: case CV_RGB2YCrCb:
263     case CV_BGR2XYZ: case CV_RGB2XYZ:
264     case CV_XYZ2BGR: case CV_XYZ2RGB:
265     case CV_BGR2HSV: case CV_RGB2HSV: case CV_BGR2HSV_FULL: case CV_RGB2HSV_FULL:
266     case CV_BGR2HLS: case CV_RGB2HLS: case CV_BGR2HLS_FULL: case CV_RGB2HLS_FULL:
267     case CV_HSV2BGR: case CV_HSV2RGB: case CV_HSV2BGR_FULL: case CV_HSV2RGB_FULL:
268     case CV_HLS2BGR: case CV_HLS2RGB: case CV_HLS2BGR_FULL: case CV_HLS2RGB_FULL:
269     */
270     default:
271         CV_Error( CV_StsBadFlag, "Unknown/unsupported color conversion code" );
272     }
273 }
274 }
275
276 void cv::ocl::cvtColor(const oclMat &src, oclMat &dst, int code, int dcn)
277 {
278     cvtColor_caller(src, dst, code, dcn);
279 }