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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "opencv2/opencv_modules.hpp"
44
45 #ifndef HAVE_OPENCV_CUDEV
46
47 #error "opencv_cudev is required"
48
49 #else
50
51 #include "opencv2/cudaarithm.hpp"
52 #include "opencv2/cudev.hpp"
53 #include "opencv2/core/private.cuda.hpp"
54
55 using namespace cv;
56 using namespace cv::cuda;
57 using namespace cv::cudev;
58
59 ////////////////////////////////////////////////////////////////////////
60 /// merge
61
62 namespace
63 {
64 template <int cn, typename T> struct MergeFunc;
65
66 template <typename T> struct MergeFunc<2, T>
67 {
call__anon9c08af3b0111::MergeFunc68 static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
69 {
70 gridMerge(zipPtr(globPtr<T>(src[0]), globPtr<T>(src[1])),
71 globPtr<typename MakeVec<T, 2>::type>(dst),
72 stream);
73 }
74 };
75
76 template <typename T> struct MergeFunc<3, T>
77 {
call__anon9c08af3b0111::MergeFunc78 static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
79 {
80 gridMerge(zipPtr(globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2])),
81 globPtr<typename MakeVec<T, 3>::type>(dst),
82 stream);
83 }
84 };
85
86 template <typename T> struct MergeFunc<4, T>
87 {
call__anon9c08af3b0111::MergeFunc88 static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
89 {
90 gridMerge(zipPtr(globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2]), globPtr<T>(src[3])),
91 globPtr<typename MakeVec<T, 4>::type>(dst),
92 stream);
93 }
94 };
95
mergeImpl(const GpuMat * src,size_t n,cv::OutputArray _dst,Stream & stream)96 void mergeImpl(const GpuMat* src, size_t n, cv::OutputArray _dst, Stream& stream)
97 {
98 CV_Assert( src != 0 );
99 CV_Assert( n > 0 && n <= 4 );
100
101 const int depth = src[0].depth();
102 const cv::Size size = src[0].size();
103
104 for (size_t i = 0; i < n; ++i)
105 {
106 CV_Assert( src[i].size() == size );
107 CV_Assert( src[i].depth() == depth );
108 CV_Assert( src[i].channels() == 1 );
109 }
110
111 if (n == 1)
112 {
113 src[0].copyTo(_dst, stream);
114 }
115 else
116 {
117 typedef void (*func_t)(const GpuMat* src, GpuMat& dst, Stream& stream);
118 static const func_t funcs[3][5] =
119 {
120 {MergeFunc<2, uchar>::call, MergeFunc<2, ushort>::call, MergeFunc<2, int>::call, 0, MergeFunc<2, double>::call},
121 {MergeFunc<3, uchar>::call, MergeFunc<3, ushort>::call, MergeFunc<3, int>::call, 0, MergeFunc<3, double>::call},
122 {MergeFunc<4, uchar>::call, MergeFunc<4, ushort>::call, MergeFunc<4, int>::call, 0, MergeFunc<4, double>::call}
123 };
124
125 const int channels = static_cast<int>(n);
126
127 GpuMat dst = getOutputMat(_dst, size, CV_MAKE_TYPE(depth, channels), stream);
128
129 const func_t func = funcs[channels - 2][CV_ELEM_SIZE(depth) / 2];
130
131 if (func == 0)
132 CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported channel count or data type");
133
134 func(src, dst, stream);
135
136 syncOutput(dst, _dst, stream);
137 }
138 }
139 }
140
merge(const GpuMat * src,size_t n,OutputArray dst,Stream & stream)141 void cv::cuda::merge(const GpuMat* src, size_t n, OutputArray dst, Stream& stream)
142 {
143 mergeImpl(src, n, dst, stream);
144 }
145
146
merge(const std::vector<GpuMat> & src,OutputArray dst,Stream & stream)147 void cv::cuda::merge(const std::vector<GpuMat>& src, OutputArray dst, Stream& stream)
148 {
149 mergeImpl(&src[0], src.size(), dst, stream);
150 }
151
152 ////////////////////////////////////////////////////////////////////////
153 /// split
154
155 namespace
156 {
157 template <int cn, typename T> struct SplitFunc;
158
159 template <typename T> struct SplitFunc<2, T>
160 {
call__anon9c08af3b0211::SplitFunc161 static void call(const GpuMat& src, GpuMat* dst, Stream& stream)
162 {
163 GlobPtrSz<T> dstarr[2] =
164 {
165 globPtr<T>(dst[0]), globPtr<T>(dst[1])
166 };
167
168 gridSplit(globPtr<typename MakeVec<T, 2>::type>(src), dstarr, stream);
169 }
170 };
171
172 template <typename T> struct SplitFunc<3, T>
173 {
call__anon9c08af3b0211::SplitFunc174 static void call(const GpuMat& src, GpuMat* dst, Stream& stream)
175 {
176 GlobPtrSz<T> dstarr[3] =
177 {
178 globPtr<T>(dst[0]), globPtr<T>(dst[1]), globPtr<T>(dst[2])
179 };
180
181 gridSplit(globPtr<typename MakeVec<T, 3>::type>(src), dstarr, stream);
182 }
183 };
184
185 template <typename T> struct SplitFunc<4, T>
186 {
call__anon9c08af3b0211::SplitFunc187 static void call(const GpuMat& src, GpuMat* dst, Stream& stream)
188 {
189 GlobPtrSz<T> dstarr[4] =
190 {
191 globPtr<T>(dst[0]), globPtr<T>(dst[1]), globPtr<T>(dst[2]), globPtr<T>(dst[3])
192 };
193
194 gridSplit(globPtr<typename MakeVec<T, 4>::type>(src), dstarr, stream);
195 }
196 };
197
splitImpl(const GpuMat & src,GpuMat * dst,Stream & stream)198 void splitImpl(const GpuMat& src, GpuMat* dst, Stream& stream)
199 {
200 typedef void (*func_t)(const GpuMat& src, GpuMat* dst, Stream& stream);
201 static const func_t funcs[3][5] =
202 {
203 {SplitFunc<2, uchar>::call, SplitFunc<2, ushort>::call, SplitFunc<2, int>::call, 0, SplitFunc<2, double>::call},
204 {SplitFunc<3, uchar>::call, SplitFunc<3, ushort>::call, SplitFunc<3, int>::call, 0, SplitFunc<3, double>::call},
205 {SplitFunc<4, uchar>::call, SplitFunc<4, ushort>::call, SplitFunc<4, int>::call, 0, SplitFunc<4, double>::call}
206 };
207
208 CV_Assert( dst != 0 );
209
210 const int depth = src.depth();
211 const int channels = src.channels();
212
213 CV_Assert( channels <= 4 );
214
215 if (channels == 0)
216 return;
217
218 if (channels == 1)
219 {
220 src.copyTo(dst[0], stream);
221 return;
222 }
223
224 for (int i = 0; i < channels; ++i)
225 dst[i].create(src.size(), depth);
226
227 const func_t func = funcs[channels - 2][CV_ELEM_SIZE(depth) / 2];
228
229 if (func == 0)
230 CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported channel count or data type");
231
232 func(src, dst, stream);
233 }
234 }
235
split(InputArray _src,GpuMat * dst,Stream & stream)236 void cv::cuda::split(InputArray _src, GpuMat* dst, Stream& stream)
237 {
238 GpuMat src = getInputMat(_src, stream);
239 splitImpl(src, dst, stream);
240 }
241
split(InputArray _src,std::vector<GpuMat> & dst,Stream & stream)242 void cv::cuda::split(InputArray _src, std::vector<GpuMat>& dst, Stream& stream)
243 {
244 GpuMat src = getInputMat(_src, stream);
245 dst.resize(src.channels());
246 if (src.channels() > 0)
247 splitImpl(src, &dst[0], stream);
248 }
249
250 #endif
251