eec103a6dc9a8eef1abdb25a1715d1e47ffb43c2
[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 materials 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 #include "opencl_kernels.hpp"
49
50 using namespace cv;
51 using namespace cv::ocl;
52
53 #ifndef CV_DESCALE
54 #define CV_DESCALE(x, n) (((x) + (1 << ((n)-1))) >> (n))
55 #endif
56
57 #ifndef FLT_EPSILON
58 #define FLT_EPSILON     1.192092896e-07F
59 #endif
60
61 namespace
62 {
63
64 void RGB2Gray_caller(const oclMat &src, oclMat &dst, int bidx)
65 {
66     int channels = src.oclchannels();
67     int src_offset = src.offset / src.elemSize1(), src_step = src.step1();
68     int dst_offset = dst.offset / dst.elemSize1(), dst_step = dst.step1();
69
70     String build_options = format("-D DEPTH_%d", src.depth());
71
72     std::vector<std::pair<size_t , const void *> > args;
73     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols));
74     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows));
75     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_step));
76     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_step));
77     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&channels));
78     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&bidx));
79     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data));
80     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
81     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_offset ));
82     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_offset ));
83
84     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
85     openCLExecuteKernel(src.clCxt, &cvt_color, "RGB2Gray", gt, lt, args, -1, -1, build_options.c_str());
86 }
87
88 void Gray2RGB_caller(const oclMat &src, oclMat &dst)
89 {
90     String build_options = format("-D DEPTH_%d", src.depth());
91     int src_offset = src.offset / src.elemSize1(), src_step = src.step1();
92     int dst_offset = dst.offset / dst.elemSize1(), dst_step = dst.step1();
93
94     std::vector<std::pair<size_t , const void *> > args;
95     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols));
96     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows));
97     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_step));
98     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_step));
99     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data));
100     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
101     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_offset ));
102     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_offset ));
103
104     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
105     openCLExecuteKernel(src.clCxt, &cvt_color, "Gray2RGB", gt, lt, args, -1, -1, build_options.c_str());
106 }
107
108 void RGB2YUV_caller(const oclMat &src, oclMat &dst, int bidx)
109 {
110     int channels = src.oclchannels();
111     String build_options = format("-D DEPTH_%d", src.depth());
112     int src_offset = src.offset / src.elemSize1(), src_step = src.step1();
113     int dst_offset = dst.offset / dst.elemSize1(), dst_step = dst.step1();
114
115     std::vector<std::pair<size_t , const void *> > args;
116     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols));
117     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows));
118     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_step));
119     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_step));
120     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&channels));
121     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&bidx));
122     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data));
123     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
124     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_offset ));
125     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_offset ));
126
127     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
128     openCLExecuteKernel(src.clCxt, &cvt_color, "RGB2YUV", gt, lt, args, -1, -1, build_options.c_str());
129 }
130
131 void YUV2RGB_caller(const oclMat &src, oclMat &dst, int bidx)
132 {
133     int channels = src.oclchannels();
134     int src_offset = src.offset / src.elemSize1(), src_step = src.step1();
135     int dst_offset = dst.offset / dst.elemSize1(), dst_step = dst.step1();
136
137     String buildOptions = format("-D DEPTH_%d", src.depth());
138
139     std::vector<std::pair<size_t , const void *> > args;
140     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols));
141     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows));
142     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_step));
143     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_step));
144     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&channels));
145     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&bidx));
146     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data));
147     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
148     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_offset ));
149     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_offset ));
150
151     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
152     openCLExecuteKernel(src.clCxt, &cvt_color, "YUV2RGB", gt, lt, args, -1, -1, buildOptions.c_str());
153 }
154
155 void YUV2RGB_NV12_caller(const oclMat &src, oclMat &dst, int bidx)
156 {
157     String build_options = format("-D DEPTH_%d", src.depth());
158     int src_offset = src.offset / src.elemSize1(), src_step = src.step1();
159     int dst_offset = dst.offset / dst.elemSize1(), dst_step = dst.step1();
160
161     std::vector<std::pair<size_t , const void *> > args;
162     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols));
163     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows));
164     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_step));
165     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_step));
166     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&bidx));
167     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.cols));
168     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.rows));
169     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data));
170     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
171     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_offset ));
172     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_offset ));
173
174     size_t gt[3] = {dst.cols / 2, dst.rows / 2, 1}, lt[3] = {16, 16, 1};
175     openCLExecuteKernel(src.clCxt, &cvt_color, "YUV2RGBA_NV12", gt, lt, args, -1, -1, build_options.c_str());
176 }
177
178 void RGB2YCrCb_caller(const oclMat &src, oclMat &dst, int bidx)
179 {
180     int channels = src.oclchannels();
181     String build_options = format("-D DEPTH_%d", src.depth());
182     int src_offset = src.offset / src.elemSize1(), src_step = src.step1();
183     int dst_offset = dst.offset / dst.elemSize1(), dst_step = dst.step1();
184
185     std::vector<std::pair<size_t , const void *> > args;
186     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols));
187     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows));
188     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_step));
189     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_step));
190     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&channels));
191     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&bidx));
192     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data));
193     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
194     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src_offset ));
195     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst_offset ));
196
197     size_t gt[3] = {src.cols, src.rows, 1}, lt[3] = {16, 16, 1};
198     openCLExecuteKernel(src.clCxt, &cvt_color, "RGB2YCrCb", gt, lt, args, -1, -1, build_options.c_str());
199 }
200
201 void cvtColor_caller(const oclMat &src, oclMat &dst, int code, int dcn)
202 {
203     Size sz = src.size();
204     int scn = src.oclchannels(), depth = src.depth(), bidx;
205
206     CV_Assert(depth == CV_8U || depth == CV_16U || depth == CV_32F);
207
208     switch (code)
209     {
210         /*
211         case COLOR_BGR2BGRA: case COLOR_RGB2BGRA: case COLOR_BGRA2BGR:
212         case COLOR_RGBA2BGR: case COLOR_RGB2BGR: case COLOR_BGRA2RGBA:
213         case COLOR_BGR2BGR565: case COLOR_BGR2BGR555: case COLOR_RGB2BGR565: case COLOR_RGB2BGR555:
214         case COLOR_BGRA2BGR565: case COLOR_BGRA2BGR555: case COLOR_RGBA2BGR565: case COLOR_RGBA2BGR555:
215         case COLOR_BGR5652BGR: case COLOR_BGR5552BGR: case COLOR_BGR5652RGB: case COLOR_BGR5552RGB:
216         case COLOR_BGR5652BGRA: case COLOR_BGR5552BGRA: case COLOR_BGR5652RGBA: case COLOR_BGR5552RGBA:
217         */
218     case COLOR_BGR2GRAY:
219     case COLOR_BGRA2GRAY:
220     case COLOR_RGB2GRAY:
221     case COLOR_RGBA2GRAY:
222     {
223         CV_Assert(scn == 3 || scn == 4);
224         bidx = code == COLOR_BGR2GRAY || code == COLOR_BGRA2GRAY ? 0 : 2;
225         dst.create(sz, CV_MAKETYPE(depth, 1));
226         RGB2Gray_caller(src, dst, bidx);
227         break;
228     }
229     case COLOR_GRAY2BGR:
230     case COLOR_GRAY2BGRA:
231     {
232         CV_Assert(scn == 1);
233         dcn  = code == COLOR_GRAY2BGRA ? 4 : 3;
234         dst.create(sz, CV_MAKETYPE(depth, dcn));
235         Gray2RGB_caller(src, dst);
236         break;
237     }
238     case COLOR_BGR2YUV:
239     case COLOR_RGB2YUV:
240     {
241         CV_Assert(scn == 3 || scn == 4);
242         bidx = code == COLOR_RGB2YUV ? 0 : 2;
243         dst.create(sz, CV_MAKETYPE(depth, 3));
244         RGB2YUV_caller(src, dst, bidx);
245         break;
246     }
247     case COLOR_YUV2BGR:
248     case COLOR_YUV2RGB:
249     {
250         CV_Assert(scn == 3 || scn == 4);
251         bidx = code == COLOR_YUV2RGB ? 0 : 2;
252         dst.create(sz, CV_MAKETYPE(depth, 3));
253         YUV2RGB_caller(src, dst, bidx);
254         break;
255     }
256     case COLOR_YUV2RGB_NV12:
257     case COLOR_YUV2BGR_NV12:
258     case COLOR_YUV2RGBA_NV12:
259     case COLOR_YUV2BGRA_NV12:
260     {
261         CV_Assert(scn == 1);
262         CV_Assert( sz.width % 2 == 0 && sz.height % 3 == 0 && depth == CV_8U );
263         dcn  = code == COLOR_YUV2BGRA_NV12 || code == COLOR_YUV2RGBA_NV12 ? 4 : 3;
264         bidx = code == COLOR_YUV2BGRA_NV12 || code == COLOR_YUV2BGR_NV12 ? 0 : 2;
265
266         Size dstSz(sz.width, sz.height * 2 / 3);
267         dst.create(dstSz, CV_MAKETYPE(depth, dcn));
268         YUV2RGB_NV12_caller(src, dst, bidx);
269         break;
270     }
271     case COLOR_BGR2YCrCb:
272     case COLOR_RGB2YCrCb:
273     {
274         CV_Assert(scn == 3 || scn == 4);
275         bidx = code == COLOR_BGR2YCrCb ? 0 : 2;
276         dst.create(sz, CV_MAKETYPE(depth, 3));
277         RGB2YCrCb_caller(src, dst, bidx);
278         break;
279     }
280     case COLOR_YCrCb2BGR:
281     case COLOR_YCrCb2RGB:
282     {
283         break;
284     }
285     /*
286     case COLOR_BGR5652GRAY: case COLOR_BGR5552GRAY:
287     case COLOR_GRAY2BGR565: case COLOR_GRAY2BGR555:
288     case COLOR_BGR2YCrCb: case COLOR_RGB2YCrCb:
289     case COLOR_BGR2XYZ: case COLOR_RGB2XYZ:
290     case COLOR_XYZ2BGR: case COLOR_XYZ2RGB:
291     case COLOR_BGR2HSV: case COLOR_RGB2HSV: case COLOR_BGR2HSV_FULL: case COLOR_RGB2HSV_FULL:
292     case COLOR_BGR2HLS: case COLOR_RGB2HLS: case COLOR_BGR2HLS_FULL: case COLOR_RGB2HLS_FULL:
293     case COLOR_HSV2BGR: case COLOR_HSV2RGB: case COLOR_HSV2BGR_FULL: case COLOR_HSV2RGB_FULL:
294     case COLOR_HLS2BGR: case COLOR_HLS2RGB: case COLOR_HLS2BGR_FULL: case COLOR_HLS2RGB_FULL:
295     */
296     default:
297         CV_Error(Error::StsBadFlag, "Unknown/unsupported color conversion code" );
298     }
299 }
300 }
301
302 void cv::ocl::cvtColor(const oclMat &src, oclMat &dst, int code, int dcn)
303 {
304     cvtColor_caller(src, dst, code, dcn);
305 }