catching OpenCL double not supported exceptions
[profile/ivi/opencv.git] / modules / ocl / test / test_split_merge.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 #define MAX_CHANNELS 4
56
57 PARAM_TEST_CASE(MergeTestBase, MatType, int, bool)
58 {
59     int type;
60     int channels;
61     bool use_roi;
62
63     //src mat
64     cv::Mat mat[MAX_CHANNELS];
65     //dst mat
66     cv::Mat dst;
67
68     // set up roi
69     int roicols, roirows;
70     int srcx[MAX_CHANNELS];
71     int srcy[MAX_CHANNELS];
72     int dstx, dsty;
73
74     //src mat with roi
75     cv::Mat mat_roi[MAX_CHANNELS];
76
77     //dst mat with roi
78     cv::Mat dst_roi;
79
80     //ocl dst mat for testing
81     cv::ocl::oclMat gdst_whole;
82
83     //ocl mat with roi
84     cv::ocl::oclMat gmat[MAX_CHANNELS];
85     cv::ocl::oclMat gdst;
86
87     virtual void SetUp()
88     {
89         type = GET_PARAM(0);
90         channels = GET_PARAM(1);
91         use_roi = GET_PARAM(2);
92
93         cv::Size size(MWIDTH, MHEIGHT);
94
95         for (int i = 0; i < channels; ++i)
96             mat[i] = randomMat(size, CV_MAKETYPE(type, 1), 5, 16, false);
97         dst = randomMat(size, CV_MAKETYPE(type, channels), 5, 16, false);
98     }
99
100     void random_roi()
101     {
102         if (use_roi)
103         {
104             //randomize ROI
105             roicols = rng.uniform(1, mat[0].cols);
106             roirows = rng.uniform(1, mat[0].rows);
107
108             for (int i = 0; i < channels; ++i)
109             {
110                 srcx[i] = rng.uniform(0, mat[i].cols - roicols);
111                 srcy[i] = rng.uniform(0, mat[i].rows - roirows);
112             }
113
114             dstx = rng.uniform(0, dst.cols  - roicols);
115             dsty = rng.uniform(0, dst.rows  - roirows);
116         }
117         else
118         {
119             roicols = mat[0].cols;
120             roirows = mat[0].rows;
121             for (int i = 0; i < channels; ++i)
122                 srcx[i] = srcy[i] = 0;
123
124             dstx = dsty = 0;
125         }
126
127         for (int i = 0; i < channels; ++i)
128             mat_roi[i] = mat[i](Rect(srcx[i], srcy[i], roicols, roirows));
129
130         dst_roi = dst(Rect(dstx, dsty, roicols, roirows));
131
132         gdst_whole = dst;
133         gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));
134
135         for (int i = 0; i < channels; ++i)
136             gmat[i] = mat_roi[i];
137     }
138 };
139
140 struct Merge : MergeTestBase {};
141
142 OCL_TEST_P(Merge, Accuracy)
143 {
144     for(int j = 0; j < LOOP_TIMES; j++)
145     {
146         random_roi();
147
148         cv::merge(mat_roi, channels, dst_roi);
149         cv::ocl::merge(gmat, channels, gdst);
150
151         EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
152     }
153 }
154
155 PARAM_TEST_CASE(SplitTestBase, MatType, int, bool)
156 {
157     int type;
158     int channels;
159     bool use_roi;
160
161     //src mat
162     cv::Mat mat;
163
164     //dstmat
165     cv::Mat dst[MAX_CHANNELS];
166
167     // set up roi
168     int roicols, roirows;
169     int srcx, srcy;
170     int dstx[MAX_CHANNELS];
171     int dsty[MAX_CHANNELS];
172
173     //src mat with roi
174     cv::Mat mat_roi;
175
176     //dst mat with roi
177     cv::Mat dst_roi[MAX_CHANNELS];
178
179     //ocl dst mat for testing
180     cv::ocl::oclMat gdst_whole[MAX_CHANNELS];
181
182     //ocl mat with roi
183     cv::ocl::oclMat gmat;
184     cv::ocl::oclMat gdst[MAX_CHANNELS];
185
186     virtual void SetUp()
187     {
188         type = GET_PARAM(0);
189         channels = GET_PARAM(1);
190         use_roi = GET_PARAM(2);
191
192         cv::Size size(MWIDTH, MHEIGHT);
193
194         mat  = randomMat(size, CV_MAKETYPE(type, channels), 5, 16, false);
195         for (int i = 0; i < channels; ++i)
196             dst[i] = randomMat(size, CV_MAKETYPE(type, 1), 5, 16, false);    }
197
198     void random_roi()
199     {
200         if (use_roi)
201         {
202             //randomize ROI
203             roicols = rng.uniform(1, mat.cols);
204             roirows = rng.uniform(1, mat.rows);
205             srcx    = rng.uniform(0, mat.cols - roicols);
206             srcy    = rng.uniform(0, mat.rows - roirows);
207
208             for (int i = 0; i < channels; ++i)
209             {
210                 dstx[i] = rng.uniform(0, dst[i].cols  - roicols);
211                 dsty[i] = rng.uniform(0, dst[i].rows  - roirows);
212             }
213         }
214         else
215         {
216             roicols = mat.cols;
217             roirows = mat.rows;
218             srcx = srcy = 0;
219
220             for (int i = 0; i < channels; ++i)
221                 dstx[i] = dsty[i] = 0;
222         }
223
224         mat_roi = mat(Rect(srcx, srcy, roicols, roirows));
225
226         for (int i = 0; i < channels; ++i)
227             dst_roi[i] = dst[i](Rect(dstx[i], dsty[i], roicols, roirows));
228
229         for (int i = 0; i < channels; ++i)
230         {
231             gdst_whole[i] = dst[i];
232             gdst[i] = gdst_whole[i](Rect(dstx[i], dsty[i], roicols, roirows));
233         }
234
235         gmat = mat_roi;
236     }
237 };
238
239 struct Split : SplitTestBase {};
240
241 OCL_TEST_P(Split, Accuracy)
242 {
243     for(int j = 0; j < LOOP_TIMES; j++)
244     {
245         random_roi();
246
247         cv::split(mat_roi, dst_roi);
248         cv::ocl::split(gmat, gdst);
249
250         for (int i = 0; i < channels; ++i)
251             EXPECT_MAT_NEAR(dst[i], Mat(gdst_whole[i]), 0.0);
252     }
253 }
254
255
256 INSTANTIATE_TEST_CASE_P(SplitMerge, Merge, Combine(
257                             Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F), Range(1, 5), Bool()));
258
259
260 INSTANTIATE_TEST_CASE_P(SplitMerge, Split , Combine(
261                             Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F), Range(1, 5), Bool()));
262
263
264 #endif // HAVE_OPENCL