1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "precomp.hpp"
44 #if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
45 static IppStatus sts = ippInit();
51 template<typename T, typename ST, typename QT>
52 void integral_( const T* src, size_t _srcstep, ST* sum, size_t _sumstep,
53 QT* sqsum, size_t _sqsumstep, ST* tilted, size_t _tiltedstep,
58 int srcstep = (int)(_srcstep/sizeof(T));
59 int sumstep = (int)(_sumstep/sizeof(ST));
60 int tiltedstep = (int)(_tiltedstep/sizeof(ST));
61 int sqsumstep = (int)(_sqsumstep/sizeof(QT));
65 memset( sum, 0, (size.width+cn)*sizeof(sum[0]));
70 memset( sqsum, 0, (size.width+cn)*sizeof(sqsum[0]));
71 sqsum += sqsumstep + cn;
76 memset( tilted, 0, (size.width+cn)*sizeof(tilted[0]));
77 tilted += tiltedstep + cn;
80 if( sqsum == 0 && tilted == 0 )
82 for( y = 0; y < size.height; y++, src += srcstep - cn, sum += sumstep - cn )
84 for( k = 0; k < cn; k++, src++, sum++ )
87 for( x = 0; x < size.width; x += cn )
90 sum[x] = sum[x - sumstep] + s;
95 else if( tilted == 0 )
97 for( y = 0; y < size.height; y++, src += srcstep - cn,
98 sum += sumstep - cn, sqsum += sqsumstep - cn )
100 for( k = 0; k < cn; k++, src++, sum++, sqsum++ )
103 QT sq = sqsum[-cn] = 0;
104 for( x = 0; x < size.width; x += cn )
109 ST t = sum[x - sumstep] + s;
110 QT tq = sqsum[x - sqsumstep] + sq;
119 AutoBuffer<ST> _buf(size.width+cn);
123 for( k = 0; k < cn; k++, src++, sum++, tilted++, buf++ )
125 sum[-cn] = tilted[-cn] = 0;
127 for( x = 0, s = 0, sq = 0; x < size.width; x += cn )
130 buf[x] = tilted[x] = it;
138 if( size.width == cn )
148 for( y = 1; y < size.height; y++ )
152 tilted += tiltedstep - cn;
156 sqsum += sqsumstep - cn;
158 for( k = 0; k < cn; k++, src++, sum++, tilted++, buf++ )
162 QT tq0 = sq = (QT)it*it;
167 tilted[-cn] = tilted[-tiltedstep];
169 sum[0] = sum[-sumstep] + t0;
171 sqsum[0] = sqsum[-sqsumstep] + tq0;
172 tilted[0] = tilted[-tiltedstep] + t0 + buf[cn];
174 for( x = cn; x < size.width - cn; x += cn )
177 buf[x - cn] = t1 + t0;
182 sum[x] = sum[x - sumstep] + s;
184 sqsum[x] = sqsum[x - sqsumstep] + sq;
185 t1 += buf[x + cn] + t0 + tilted[x - tiltedstep - cn];
189 if( size.width > cn )
192 buf[x - cn] = t1 + t0;
197 sum[x] = sum[x - sumstep] + s;
199 sqsum[x] = sqsum[x - sqsumstep] + sq;
200 tilted[x] = t0 + t1 + tilted[x - tiltedstep - cn];
212 #define DEF_INTEGRAL_FUNC(suffix, T, ST, QT) \
213 static void integral_##suffix( T* src, size_t srcstep, ST* sum, size_t sumstep, QT* sqsum, size_t sqsumstep, \
214 ST* tilted, size_t tiltedstep, Size size, int cn ) \
215 { integral_(src, srcstep, sum, sumstep, sqsum, sqsumstep, tilted, tiltedstep, size, cn); }
217 DEF_INTEGRAL_FUNC(8u32s, uchar, int, double)
218 DEF_INTEGRAL_FUNC(8u32f, uchar, float, double)
219 DEF_INTEGRAL_FUNC(8u64f, uchar, double, double)
220 DEF_INTEGRAL_FUNC(32f, float, float, double)
221 DEF_INTEGRAL_FUNC(32f64f, float, double, double)
222 DEF_INTEGRAL_FUNC(64f, double, double, double)
224 typedef void (*IntegralFunc)(const uchar* src, size_t srcstep, uchar* sum, size_t sumstep,
225 uchar* sqsum, size_t sqsumstep, uchar* tilted, size_t tstep,
231 void cv::integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, OutputArray _tilted, int sdepth )
233 Mat src = _src.getMat(), sum, sqsum, tilted;
234 int depth = src.depth(), cn = src.channels();
235 Size isize(src.cols + 1, src.rows+1);
238 sdepth = depth == CV_8U ? CV_32S : CV_64F;
239 sdepth = CV_MAT_DEPTH(sdepth);
241 #if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
242 if( ( depth == CV_8U ) && ( !_tilted.needed() ) )
244 if( sdepth == CV_32F )
248 IppiSize srcRoiSize = ippiSize( src.cols, src.rows );
249 _sum.create( isize, CV_MAKETYPE( sdepth, cn ) );
251 if( _sqsum.needed() )
253 _sqsum.create( isize, CV_MAKETYPE( CV_64F, cn ) );
254 sqsum = _sqsum.getMat();
255 ippiSqrIntegral_8u32f64f_C1R( (const Ipp8u*)src.data, src.step, (Ipp32f*)sum.data, sum.step, (Ipp64f*)sqsum.data, sqsum.step, srcRoiSize, 0, 0 );
259 ippiIntegral_8u32f_C1R( (const Ipp8u*)src.data, src.step, (Ipp32f*)sum.data, sum.step, srcRoiSize, 0 );
264 if( sdepth == CV_32S )
268 IppiSize srcRoiSize = ippiSize( src.cols, src.rows );
269 _sum.create( isize, CV_MAKETYPE( sdepth, cn ) );
271 if( _sqsum.needed() )
273 _sqsum.create( isize, CV_MAKETYPE( CV_64F, cn ) );
274 sqsum = _sqsum.getMat();
275 ippiSqrIntegral_8u32s64f_C1R( (const Ipp8u*)src.data, src.step, (Ipp32s*)sum.data, sum.step, (Ipp64f*)sqsum.data, sqsum.step, srcRoiSize, 0, 0 );
279 ippiIntegral_8u32s_C1R( (const Ipp8u*)src.data, src.step, (Ipp32s*)sum.data, sum.step, srcRoiSize, 0 );
287 _sum.create( isize, CV_MAKETYPE(sdepth, cn) );
290 if( _tilted.needed() )
292 _tilted.create( isize, CV_MAKETYPE(sdepth, cn) );
293 tilted = _tilted.getMat();
296 if( _sqsum.needed() )
298 _sqsum.create( isize, CV_MAKETYPE(CV_64F, cn) );
299 sqsum = _sqsum.getMat();
302 IntegralFunc func = 0;
304 if( depth == CV_8U && sdepth == CV_32S )
305 func = (IntegralFunc)GET_OPTIMIZED(integral_8u32s);
306 else if( depth == CV_8U && sdepth == CV_32F )
307 func = (IntegralFunc)integral_8u32f;
308 else if( depth == CV_8U && sdepth == CV_64F )
309 func = (IntegralFunc)integral_8u64f;
310 else if( depth == CV_32F && sdepth == CV_32F )
311 func = (IntegralFunc)integral_32f;
312 else if( depth == CV_32F && sdepth == CV_64F )
313 func = (IntegralFunc)integral_32f64f;
314 else if( depth == CV_64F && sdepth == CV_64F )
315 func = (IntegralFunc)integral_64f;
317 CV_Error( CV_StsUnsupportedFormat, "" );
319 func( src.data, src.step, sum.data, sum.step, sqsum.data, sqsum.step,
320 tilted.data, tilted.step, src.size(), cn );
323 void cv::integral( InputArray src, OutputArray sum, int sdepth )
325 integral( src, sum, noArray(), noArray(), sdepth );
328 void cv::integral( InputArray src, OutputArray sum, OutputArray sqsum, int sdepth )
330 integral( src, sum, sqsum, noArray(), sdepth );
335 cvIntegral( const CvArr* image, CvArr* sumImage,
336 CvArr* sumSqImage, CvArr* tiltedSumImage )
338 cv::Mat src = cv::cvarrToMat(image), sum = cv::cvarrToMat(sumImage), sum0 = sum;
339 cv::Mat sqsum0, sqsum, tilted0, tilted;
340 cv::Mat *psqsum = 0, *ptilted = 0;
344 sqsum0 = sqsum = cv::cvarrToMat(sumSqImage);
350 tilted0 = tilted = cv::cvarrToMat(tiltedSumImage);
353 cv::integral( src, sum, psqsum ? cv::_OutputArray(*psqsum) : cv::_OutputArray(),
354 ptilted ? cv::_OutputArray(*ptilted) : cv::_OutputArray(), sum.depth() );
356 CV_Assert( sum.data == sum0.data && sqsum.data == sqsum0.data && tilted.data == tilted0.data );