sumpixels.cpp 12 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
wester committed
44 45 46
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
static IppStatus sts = ippInit();
#endif
wester committed
47 48 49 50 51 52 53

namespace cv
{

template<typename T, typename ST, typename QT>
void integral_( const T* src, size_t _srcstep, ST* sum, size_t _sumstep,
                QT* sqsum, size_t _sqsumstep, ST* tilted, size_t _tiltedstep,
a  
Kai Westerkamp committed
54
                Size size, int cn )
wester committed
55 56 57 58 59 60 61 62
{
    int x, y, k;

    int srcstep = (int)(_srcstep/sizeof(T));
    int sumstep = (int)(_sumstep/sizeof(ST));
    int tiltedstep = (int)(_tiltedstep/sizeof(ST));
    int sqsumstep = (int)(_sqsumstep/sizeof(QT));

a  
Kai Westerkamp committed
63
    size.width *= cn;
wester committed
64

a  
Kai Westerkamp committed
65
    memset( sum, 0, (size.width+cn)*sizeof(sum[0]));
wester committed
66 67 68 69
    sum += sumstep + cn;

    if( sqsum )
    {
a  
Kai Westerkamp committed
70
        memset( sqsum, 0, (size.width+cn)*sizeof(sqsum[0]));
wester committed
71 72 73 74 75
        sqsum += sqsumstep + cn;
    }

    if( tilted )
    {
a  
Kai Westerkamp committed
76
        memset( tilted, 0, (size.width+cn)*sizeof(tilted[0]));
wester committed
77 78 79 80 81
        tilted += tiltedstep + cn;
    }

    if( sqsum == 0 && tilted == 0 )
    {
a  
Kai Westerkamp committed
82
        for( y = 0; y < size.height; y++, src += srcstep - cn, sum += sumstep - cn )
wester committed
83 84 85 86
        {
            for( k = 0; k < cn; k++, src++, sum++ )
            {
                ST s = sum[-cn] = 0;
a  
Kai Westerkamp committed
87
                for( x = 0; x < size.width; x += cn )
wester committed
88 89 90 91 92 93 94 95 96
                {
                    s += src[x];
                    sum[x] = sum[x - sumstep] + s;
                }
            }
        }
    }
    else if( tilted == 0 )
    {
a  
Kai Westerkamp committed
97
        for( y = 0; y < size.height; y++, src += srcstep - cn,
wester committed
98 99 100 101 102 103
                        sum += sumstep - cn, sqsum += sqsumstep - cn )
        {
            for( k = 0; k < cn; k++, src++, sum++, sqsum++ )
            {
                ST s = sum[-cn] = 0;
                QT sq = sqsum[-cn] = 0;
a  
Kai Westerkamp committed
104
                for( x = 0; x < size.width; x += cn )
wester committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
                {
                    T it = src[x];
                    s += it;
                    sq += (QT)it*it;
                    ST t = sum[x - sumstep] + s;
                    QT tq = sqsum[x - sqsumstep] + sq;
                    sum[x] = t;
                    sqsum[x] = tq;
                }
            }
        }
    }
    else
    {
a  
Kai Westerkamp committed
119
        AutoBuffer<ST> _buf(size.width+cn);
wester committed
120 121 122 123 124 125 126
        ST* buf = _buf;
        ST s;
        QT sq;
        for( k = 0; k < cn; k++, src++, sum++, tilted++, buf++ )
        {
            sum[-cn] = tilted[-cn] = 0;

a  
Kai Westerkamp committed
127
            for( x = 0, s = 0, sq = 0; x < size.width; x += cn )
wester committed
128 129 130 131 132 133 134 135 136 137
            {
                T it = src[x];
                buf[x] = tilted[x] = it;
                s += it;
                sq += (QT)it*it;
                sum[x] = s;
                if( sqsum )
                    sqsum[x] = sq;
            }

a  
Kai Westerkamp committed
138
            if( size.width == cn )
wester committed
139 140 141 142 143 144 145 146 147
                buf[cn] = 0;

            if( sqsum )
            {
                sqsum[-cn] = 0;
                sqsum++;
            }
        }

a  
Kai Westerkamp committed
148
        for( y = 1; y < size.height; y++ )
wester committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        {
            src += srcstep - cn;
            sum += sumstep - cn;
            tilted += tiltedstep - cn;
            buf += -cn;

            if( sqsum )
                sqsum += sqsumstep - cn;

            for( k = 0; k < cn; k++, src++, sum++, tilted++, buf++ )
            {
                T it = src[0];
                ST t0 = s = it;
                QT tq0 = sq = (QT)it*it;

                sum[-cn] = 0;
                if( sqsum )
                    sqsum[-cn] = 0;
                tilted[-cn] = tilted[-tiltedstep];

                sum[0] = sum[-sumstep] + t0;
                if( sqsum )
                    sqsum[0] = sqsum[-sqsumstep] + tq0;
                tilted[0] = tilted[-tiltedstep] + t0 + buf[cn];

a  
Kai Westerkamp committed
174
                for( x = cn; x < size.width - cn; x += cn )
wester committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
                {
                    ST t1 = buf[x];
                    buf[x - cn] = t1 + t0;
                    t0 = it = src[x];
                    tq0 = (QT)it*it;
                    s += t0;
                    sq += tq0;
                    sum[x] = sum[x - sumstep] + s;
                    if( sqsum )
                        sqsum[x] = sqsum[x - sqsumstep] + sq;
                    t1 += buf[x + cn] + t0 + tilted[x - tiltedstep - cn];
                    tilted[x] = t1;
                }

a  
Kai Westerkamp committed
189
                if( size.width > cn )
wester committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
                {
                    ST t1 = buf[x];
                    buf[x - cn] = t1 + t0;
                    t0 = it = src[x];
                    tq0 = (QT)it*it;
                    s += t0;
                    sq += tq0;
                    sum[x] = sum[x - sumstep] + s;
                    if( sqsum )
                        sqsum[x] = sqsum[x - sqsumstep] + sq;
                    tilted[x] = t0 + t1 + tilted[x - tiltedstep - cn];
                    buf[x] = t0;
                }

                if( sqsum )
                    sqsum++;
            }
        }
    }
}


a  
Kai Westerkamp committed
212 213 214 215 216 217
#define DEF_INTEGRAL_FUNC(suffix, T, ST, QT) \
static void integral_##suffix( T* src, size_t srcstep, ST* sum, size_t sumstep, QT* sqsum, size_t sqsumstep, \
                              ST* tilted, size_t tiltedstep, Size size, int cn ) \
{ integral_(src, srcstep, sum, sumstep, sqsum, sqsumstep, tilted, tiltedstep, size, cn); }

DEF_INTEGRAL_FUNC(8u32s, uchar, int, double)
wester committed
218 219 220 221 222
DEF_INTEGRAL_FUNC(8u32f, uchar, float, double)
DEF_INTEGRAL_FUNC(8u64f, uchar, double, double)
DEF_INTEGRAL_FUNC(32f, float, float, double)
DEF_INTEGRAL_FUNC(32f64f, float, double, double)
DEF_INTEGRAL_FUNC(64f, double, double, double)
a  
Kai Westerkamp committed
223 224 225 226 227

typedef void (*IntegralFunc)(const uchar* src, size_t srcstep, uchar* sum, size_t sumstep,
                             uchar* sqsum, size_t sqsumstep, uchar* tilted, size_t tstep,
                             Size size, int cn );

wester committed
228 229 230
}


wester committed
231
void cv::integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, OutputArray _tilted, int sdepth )
wester committed
232
{
wester committed
233 234 235 236
    Mat src = _src.getMat(), sum, sqsum, tilted;
    int depth = src.depth(), cn = src.channels();
    Size isize(src.cols + 1, src.rows+1);

a  
Kai Westerkamp committed
237 238
    if( sdepth <= 0 )
        sdepth = depth == CV_8U ? CV_32S : CV_64F;
wester committed
239
    sdepth = CV_MAT_DEPTH(sdepth);
a  
Kai Westerkamp committed
240

wester committed
241 242
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
    if( ( depth == CV_8U ) && ( !_tilted.needed() ) )
wester committed
243
    {
a  
Kai Westerkamp committed
244 245
        if( sdepth == CV_32F )
        {
wester committed
246
            if( cn == 1 )
a  
Kai Westerkamp committed
247
            {
wester committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261
                IppiSize srcRoiSize = ippiSize( src.cols, src.rows );
                _sum.create( isize, CV_MAKETYPE( sdepth, cn ) );
                sum = _sum.getMat();
                if( _sqsum.needed() )
                {
                    _sqsum.create( isize, CV_MAKETYPE( CV_64F, cn ) );
                    sqsum = _sqsum.getMat();
                    ippiSqrIntegral_8u32f64f_C1R( (const Ipp8u*)src.data, (int)src.step, (Ipp32f*)sum.data, (int)sum.step, (Ipp64f*)sqsum.data, (int)sqsum.step, srcRoiSize, 0, 0 );
                }
                else
                {
                    ippiIntegral_8u32f_C1R( (const Ipp8u*)src.data, (int)src.step, (Ipp32f*)sum.data, (int)sum.step, srcRoiSize, 0 );
                }
                return;
a  
Kai Westerkamp committed
262 263
            }
        }
wester committed
264
        if( sdepth == CV_32S )
a  
Kai Westerkamp committed
265
        {
wester committed
266
            if( cn == 1 )
a  
Kai Westerkamp committed
267
            {
wester committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281
                IppiSize srcRoiSize = ippiSize( src.cols, src.rows );
                _sum.create( isize, CV_MAKETYPE( sdepth, cn ) );
                sum = _sum.getMat();
                if( _sqsum.needed() )
                {
                    _sqsum.create( isize, CV_MAKETYPE( CV_64F, cn ) );
                    sqsum = _sqsum.getMat();
                    ippiSqrIntegral_8u32s64f_C1R( (const Ipp8u*)src.data, (int)src.step, (Ipp32s*)sum.data, (int)sum.step, (Ipp64f*)sqsum.data, (int)sqsum.step, srcRoiSize, 0, 0 );
                }
                else
                {
                    ippiIntegral_8u32s_C1R( (const Ipp8u*)src.data, (int)src.step, (Ipp32s*)sum.data, (int)sum.step, srcRoiSize, 0 );
                }
                return;
a  
Kai Westerkamp committed
282 283
            }
        }
wester committed
284 285 286
    }
#endif

wester committed
287 288
    _sum.create( isize, CV_MAKETYPE(sdepth, cn) );
    sum = _sum.getMat();
wester committed
289

wester committed
290
    if( _tilted.needed() )
wester committed
291
    {
wester committed
292 293
        _tilted.create( isize, CV_MAKETYPE(sdepth, cn) );
        tilted = _tilted.getMat();
wester committed
294 295 296 297
    }

    if( _sqsum.needed() )
    {
wester committed
298
        _sqsum.create( isize, CV_MAKETYPE(CV_64F, cn) );
wester committed
299 300 301
        sqsum = _sqsum.getMat();
    }

a  
Kai Westerkamp committed
302
    IntegralFunc func = 0;
wester committed
303 304

    if( depth == CV_8U && sdepth == CV_32S )
a  
Kai Westerkamp committed
305
        func = (IntegralFunc)GET_OPTIMIZED(integral_8u32s);
wester committed
306 307 308 309 310 311 312 313 314 315
    else if( depth == CV_8U && sdepth == CV_32F )
        func = (IntegralFunc)integral_8u32f;
    else if( depth == CV_8U && sdepth == CV_64F )
        func = (IntegralFunc)integral_8u64f;
    else if( depth == CV_32F && sdepth == CV_32F )
        func = (IntegralFunc)integral_32f;
    else if( depth == CV_32F && sdepth == CV_64F )
        func = (IntegralFunc)integral_32f64f;
    else if( depth == CV_64F && sdepth == CV_64F )
        func = (IntegralFunc)integral_64f;
a  
Kai Westerkamp committed
316 317 318
    else
        CV_Error( CV_StsUnsupportedFormat, "" );

wester committed
319 320
    func( src.data, src.step, sum.data, sum.step, sqsum.data, sqsum.step,
          tilted.data, tilted.step, src.size(), cn );
wester committed
321 322 323 324 325 326 327
}

void cv::integral( InputArray src, OutputArray sum, int sdepth )
{
    integral( src, sum, noArray(), noArray(), sdepth );
}

wester committed
328
void cv::integral( InputArray src, OutputArray sum, OutputArray sqsum, int sdepth )
wester committed
329
{
wester committed
330
    integral( src, sum, sqsum, noArray(), sdepth );
wester committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
}


CV_IMPL void
cvIntegral( const CvArr* image, CvArr* sumImage,
            CvArr* sumSqImage, CvArr* tiltedSumImage )
{
    cv::Mat src = cv::cvarrToMat(image), sum = cv::cvarrToMat(sumImage), sum0 = sum;
    cv::Mat sqsum0, sqsum, tilted0, tilted;
    cv::Mat *psqsum = 0, *ptilted = 0;

    if( sumSqImage )
    {
        sqsum0 = sqsum = cv::cvarrToMat(sumSqImage);
        psqsum = &sqsum;
    }

    if( tiltedSumImage )
    {
        tilted0 = tilted = cv::cvarrToMat(tiltedSumImage);
        ptilted = &tilted;
    }
    cv::integral( src, sum, psqsum ? cv::_OutputArray(*psqsum) : cv::_OutputArray(),
                  ptilted ? cv::_OutputArray(*ptilted) : cv::_OutputArray(), sum.depth() );

    CV_Assert( sum.data == sum0.data && sqsum.data == sqsum0.data && tilted.data == tilted0.data );
}

/* End of file. */