catching OpenCL double not supported exceptions
[profile/ivi/opencv.git] / modules / ocl / test / test_matrix_operation.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 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // @Authors
19 //    Jia Haipeng, jiahaipeng95@gmail.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 "test_precomp.hpp"
48
49 #ifdef HAVE_OPENCL
50
51 using namespace cvtest;
52 using namespace testing;
53 using namespace std;
54
55 ////////////////////////////////converto/////////////////////////////////////////////////
56
57 PARAM_TEST_CASE(ConvertToTestBase, MatType, MatType, int, bool)
58 {
59     int src_depth, dst_depth;
60     int cn, dst_type;
61     bool use_roi;
62
63     // src mat
64     cv::Mat mat;
65     cv::Mat dst;
66
67     // set up roi
68     int roicols, roirows;
69     int srcx, srcy;
70     int dstx, dsty;
71
72     // src mat with roi
73     cv::Mat mat_roi;
74     cv::Mat dst_roi;
75
76     // ocl dst mat for testing
77     cv::ocl::oclMat gdst_whole;
78
79     // ocl mat with roi
80     cv::ocl::oclMat gsrc;
81     cv::ocl::oclMat gdst;
82
83     virtual void SetUp()
84     {
85         src_depth = GET_PARAM(0);
86         dst_depth = GET_PARAM(1);
87         cn = GET_PARAM(2);
88         int src_type = CV_MAKE_TYPE(src_depth, cn);
89         dst_type = CV_MAKE_TYPE(dst_depth, cn);
90
91         use_roi = GET_PARAM(3);
92
93         mat = randomMat(randomSize(MIN_VALUE, MAX_VALUE), src_type, 5, 136, false);
94         dst = randomMat(use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : mat.size(), dst_type, 5, 136, false);
95     }
96
97     void random_roi()
98     {
99         if (use_roi)
100         {
101             // randomize ROI
102             roicols = rng.uniform(1, MIN_VALUE);
103             roirows = rng.uniform(1, MIN_VALUE);
104             srcx = rng.uniform(0, mat.cols - roicols);
105             srcy = rng.uniform(0, mat.rows - roirows);
106             dstx = rng.uniform(0, dst.cols - roicols);
107             dsty = rng.uniform(0, dst.rows - roirows);
108         }
109         else
110         {
111             roicols = mat.cols;
112             roirows = mat.rows;
113             srcx = srcy = 0;
114             dstx = dsty = 0;
115         }
116
117         mat_roi = mat(Rect(srcx, srcy, roicols, roirows));
118         dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
119
120         gdst_whole = dst;
121         gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
122
123         gsrc = mat_roi;
124     }
125 };
126
127 typedef ConvertToTestBase ConvertTo;
128
129 OCL_TEST_P(ConvertTo, Accuracy)
130 {
131     if((src_depth == CV_64F || dst_depth == CV_64F) &&
132             !cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
133     {
134         return; // returns silently
135     }
136     for (int j = 0; j < LOOP_TIMES; j++)
137     {
138         random_roi();
139
140         mat_roi.convertTo(dst_roi, dst_type);
141         gsrc.convertTo(gdst, dst_type);
142
143         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), src_depth == CV_64F ? 1.0 : 0.0);
144         EXPECT_MAT_NEAR(dst_roi, Mat(gdst), src_depth == CV_64F ? 1.0 : 0.0);
145     }
146 }
147
148 ///////////////////////////////////////////copyto/////////////////////////////////////////////////////////////
149
150 PARAM_TEST_CASE(CopyToTestBase, MatType, int, bool)
151 {
152     bool use_roi;
153
154     cv::Mat src, mask, dst;
155
156     // set up roi
157     int roicols,roirows;
158     int srcx, srcy;
159     int dstx, dsty;
160     int maskx,masky;
161
162     // src mat with roi
163     cv::Mat src_roi;
164     cv::Mat mask_roi;
165     cv::Mat dst_roi;
166
167     // ocl dst mat for testing
168     cv::ocl::oclMat gdst_whole;
169
170     // ocl mat with roi
171     cv::ocl::oclMat gsrc, gdst, gmask;
172
173     virtual void SetUp()
174     {
175         int type = CV_MAKETYPE(GET_PARAM(0), GET_PARAM(1));
176         use_roi = GET_PARAM(2);
177
178         src = randomMat(randomSize(MIN_VALUE, MAX_VALUE), type, 5, 16, false);
179         dst = randomMat(use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : src.size(), type, 5, 16, false);
180         mask = randomMat(use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : src.size(), CV_8UC1, 0, 2,  false);
181
182         cv::threshold(mask, mask, 0.5, 255., CV_8UC1);
183     }
184
185     void random_roi()
186     {
187         if (use_roi)
188         {
189             // randomize ROI
190             roicols = rng.uniform(1, MIN_VALUE);
191             roirows = rng.uniform(1, MIN_VALUE);
192             srcx = rng.uniform(0, src.cols - roicols);
193             srcy = rng.uniform(0, src.rows - roirows);
194             dstx = rng.uniform(0, dst.cols - roicols);
195             dsty = rng.uniform(0, dst.rows - roirows);
196             maskx = rng.uniform(0, mask.cols - roicols);
197             masky = rng.uniform(0, mask.rows - roirows);
198         }
199         else
200         {
201             roicols = src.cols;
202             roirows = src.rows;
203             srcx = srcy = 0;
204             dstx = dsty = 0;
205             maskx = masky = 0;
206         }
207
208         src_roi = src(Rect(srcx, srcy, roicols, roirows));
209         mask_roi = mask(Rect(maskx, masky, roicols, roirows));
210         dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
211
212         gdst_whole = dst;
213         gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
214
215         gsrc = src_roi;
216         gmask = mask_roi;
217     }
218 };
219
220 typedef CopyToTestBase CopyTo;
221
222 OCL_TEST_P(CopyTo, Without_mask)
223 {
224     if((src.depth() == CV_64F) &&
225             !cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
226     {
227         return; // returns silently
228     }
229     for (int j = 0; j < LOOP_TIMES; j++)
230     {
231         random_roi();
232
233         src_roi.copyTo(dst_roi);
234         gsrc.copyTo(gdst);
235
236         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
237     }
238 }
239
240 OCL_TEST_P(CopyTo, With_mask)
241 {
242     if(src.depth() == CV_64F &&
243         !cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
244     {
245         return; // returns silently
246     }
247     for (int j = 0; j < LOOP_TIMES; j++)
248     {
249         random_roi();
250
251         src_roi.copyTo(dst_roi, mask_roi);
252         gsrc.copyTo(gdst, gmask);
253
254         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
255     }
256 }
257
258 /////////////////////////////////////////// setTo /////////////////////////////////////////////////////////////
259
260 PARAM_TEST_CASE(SetToTestBase, MatType, int, bool)
261 {
262     int depth, channels;
263     bool use_roi;
264
265     cv::Scalar val;
266
267     cv::Mat src;
268     cv::Mat mask;
269
270     // set up roi
271     int roicols, roirows;
272     int srcx, srcy;
273     int maskx, masky;
274
275     // src mat with roi
276     cv::Mat src_roi;
277     cv::Mat mask_roi;
278
279     // ocl dst mat for testing
280     cv::ocl::oclMat gsrc_whole;
281
282     // ocl mat with roi
283     cv::ocl::oclMat gsrc;
284     cv::ocl::oclMat gmask;
285
286     virtual void SetUp()
287     {
288         depth = GET_PARAM(0);
289         channels = GET_PARAM(1);
290         use_roi = GET_PARAM(2);
291
292         int type = CV_MAKE_TYPE(depth, channels);
293
294         src = randomMat(randomSize(MIN_VALUE, MAX_VALUE), type, 5, 16, false);
295         mask = randomMat(use_roi ? randomSize(MIN_VALUE, MAX_VALUE) : src.size(), CV_8UC1, 0, 2,  false);
296
297         cv::threshold(mask, mask, 0.5, 255., CV_8UC1);
298         val = cv::Scalar(rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0),
299                          rng.uniform(-10.0, 10.0), rng.uniform(-10.0, 10.0));
300     }
301
302     void random_roi()
303     {
304         if (use_roi)
305         {
306             // randomize ROI
307             roicols = rng.uniform(1, MIN_VALUE);
308             roirows = rng.uniform(1, MIN_VALUE);
309             srcx = rng.uniform(0, src.cols - roicols);
310             srcy = rng.uniform(0, src.rows - roirows);
311             maskx = rng.uniform(0, mask.cols - roicols);
312             masky = rng.uniform(0, mask.rows - roirows);
313         }
314         else
315         {
316             roicols = src.cols;
317             roirows = src.rows;
318             srcx = srcy = 0;
319             maskx = masky = 0;
320         }
321
322         src_roi = src(Rect(srcx, srcy, roicols, roirows));
323         mask_roi = mask(Rect(maskx, masky, roicols, roirows));
324
325         gsrc_whole = src;
326         gsrc = gsrc_whole(Rect(srcx, srcy, roicols, roirows));
327
328         gmask = mask_roi;
329     }
330 };
331
332 typedef SetToTestBase SetTo;
333
334 OCL_TEST_P(SetTo, Without_mask)
335 {
336     if(depth == CV_64F &&
337             !cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
338     {
339         return; // returns silently
340     }
341     for (int j = 0; j < LOOP_TIMES; j++)
342     {
343         random_roi();
344
345         src_roi.setTo(val);
346         gsrc.setTo(val);
347
348         EXPECT_MAT_NEAR(src, Mat(gsrc_whole), 1.);
349     }
350 }
351
352 OCL_TEST_P(SetTo, With_mask)
353 {
354     if(depth == CV_64F &&
355             !cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
356     {
357         return; // returns silently
358     }
359     for (int j = 0; j < LOOP_TIMES; j++)
360     {
361         random_roi();
362
363         src_roi.setTo(val, mask_roi);
364         gsrc.setTo(val, gmask);
365
366         EXPECT_MAT_NEAR(src, Mat(gsrc_whole), 1.);
367     }
368 }
369
370 // convertC3C4
371
372 PARAM_TEST_CASE(convertC3C4, MatType, bool)
373 {
374     int depth;
375     bool use_roi;
376
377     //src mat
378     cv::Mat src;
379
380     // set up roi
381     int roicols, roirows;
382     int srcx, srcy;
383
384     //src mat with roi
385     cv::Mat src_roi;
386
387     //ocl mat with roi
388     cv::ocl::oclMat gsrc_roi;
389
390     virtual void SetUp()
391     {
392         depth = GET_PARAM(0);
393         use_roi = GET_PARAM(1);
394         int type = CV_MAKE_TYPE(depth, 3);
395
396         src = randomMat(randomSize(1, MAX_VALUE), type, 0, 40, false);
397     }
398
399     void random_roi()
400     {
401         if (use_roi)
402         {
403             //randomize ROI
404             roicols = rng.uniform(1, src.cols);
405             roirows = rng.uniform(1, src.rows);
406             srcx = rng.uniform(0, src.cols - roicols);
407             srcy = rng.uniform(0, src.rows - roirows);
408         }
409         else
410         {
411             roicols = src.cols;
412             roirows = src.rows;
413             srcx = srcy = 0;
414         }
415
416         src_roi = src(Rect(srcx, srcy, roicols, roirows));
417     }
418 };
419
420 OCL_TEST_P(convertC3C4, Accuracy)
421 {
422     if(depth == CV_64F &&
423         !cv::ocl::Context::getContext()->supportsFeature(cv::ocl::FEATURE_CL_DOUBLE))
424     {
425         return; // returns silently
426     }
427     for (int j = 0; j < LOOP_TIMES; j++)
428     {
429         random_roi();
430
431         gsrc_roi = src_roi;
432
433         EXPECT_MAT_NEAR(src_roi, Mat(gsrc_roi), 0.0);
434     }
435 }
436
437 INSTANTIATE_TEST_CASE_P(MatrixOperation, ConvertTo, Combine(
438                             Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
439                             Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
440                             Range(1, 5), Bool()));
441
442 INSTANTIATE_TEST_CASE_P(MatrixOperation, CopyTo, Combine(
443                             Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
444                             testing::Range(1, 5), Bool()));
445
446 INSTANTIATE_TEST_CASE_P(MatrixOperation, SetTo, Combine(
447                             Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
448                             testing::Range(1, 5), Bool()));
449
450 INSTANTIATE_TEST_CASE_P(MatrixOperation, convertC3C4, Combine(
451                             Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F),
452                             Bool()));
453 #endif