@@ -74,20 +74,22 @@ float* acosTable() {
7474
7575// compute gradient magnitude and orientation at each location (uses sse)
7676void gradMag ( float *I, float *M, float *O, int h, int w, int d, bool full ) {
77- int x, y, y1, c, h4, s; float *Gx, *Gy, *M2 ; __m128 *_Gx, *_Gy, *_M2, _m;
77+ int y;
78+ __m128 *_Gx, *_Gy, *_M2, _m;
7879 float *acost = acosTable (), acMult=10000 .0f ;
7980 // allocate memory for storing one column of output (padded so h4%4==0)
80- h4=(h%4 ==0 ) ? h : h-(h%4 )+4 ; s=d*h4*sizeof (float );
81- M2 =(float *) alMalloc (s,16 ); _M2=(__m128*) M2 ;
82- Gx=(float *) alMalloc (s,16 ); _Gx=(__m128*) Gx;
83- Gy=(float *) alMalloc (s,16 ); _Gy=(__m128*) Gy;
81+ int h4=(h%4 ==0 ) ? h : h-(h%4 )+4 ;
82+ int s = static_cast <size_t >(d) * static_cast <size_t >(h4) * sizeof (float );
83+ float * M2 =(float *) alMalloc (s,16 ); _M2=(__m128*) M2 ;
84+ float * Gx=(float *) alMalloc (s,16 ); _Gx=(__m128*) Gx;
85+ float * Gy=(float *) alMalloc (s,16 ); _Gy=(__m128*) Gy;
8486 // compute gradient magnitude and orientation for each column
85- for ( x=0 ; x<w; x++ ) {
87+ for (int x=0 ; x<w; x++ ) {
8688 // compute gradients (Gx, Gy) with maximum squared magnitude (M2)
87- for (c=0 ; c<d; c++) {
89+ for (int c=0 ; c<d; c++) {
8890 grad1 ( I+x*h+c*w*h, Gx+c*h4, Gy+c*h4, h, w, x );
8991 for ( y=0 ; y<h4/4 ; y++ ) {
90- y1=h4/4 *c+y;
92+ int y1=h4/4 *c+y;
9193 _M2[y1]=sse::ADD (sse::MUL (_Gx[y1],_Gx[y1]),sse::MUL (_Gy[y1],_Gy[y1]));
9294 if ( c==0 ) continue ; _m = sse::CMPGT ( _M2[y1], _M2[y] );
9395 _M2[y] = sse::OR ( sse::AND (_m,_M2[y1]), sse::ANDNOT (_m,_M2[y]) );
@@ -106,7 +108,7 @@ void gradMag( float *I, float *M, float *O, int h, int w, int d, bool full ) {
106108 // compute and store gradient orientation (O) via table lookup
107109 if ( O!=0 ) for ( y=0 ; y<h; y++ ) O[x*h+y] = acost[(int )Gx[y]];
108110 if ( O!=0 && full ) {
109- y1=((~size_t (O+x*h)+1 )&15 )/4 ; y=0 ;
111+ int y1=((~size_t (O+x*h)+1 )&15 )/4 ; y=0 ;
110112 for ( ; y<y1; y++ ) O[y+x*h]+=(Gy[y]<0 )*PI ;
111113 for ( ; y<h-4 ; y+=4 ) sse::STRu ( O[y+x*h],
112114 sse::ADD ( sse::LDu (O[y+x*h]), sse::AND (sse::CMPLT (sse::LDu (Gy[y]),sse::SET (0 .f )),sse::SET (PI )) ) );
@@ -256,13 +258,14 @@ void gradHist( float *M, float *O, float *H, int h, int w,
256258
257259// HOG helper: compute 2x2 block normalization values (padded by 1 pixel)
258260float * hogNormMatrix ( float *H, int nOrients, int hb, int wb, int bin ) {
259- float *N, * N1 , *n; int o, x, y, dx, dy, hb1=hb+1 , wb1=wb+1 ;
261+ int o, x, y, dx, dy, hb1=hb+1 , wb1=wb+1 ;
260262 float eps = 1e-4f /4 /bin/bin/bin/bin; // precise backward equality
261- N = (float *) wrCalloc (hb1*wb1,sizeof (float )); N1 =N+hb1+1 ;
263+ float * N = (float *) wrCalloc (static_cast <size_t >(hb1) * static_cast <size_t >(wb1), sizeof (float ));
264+ float * N1 =N+hb1+1 ;
262265 for ( o=0 ; o<nOrients; o++ ) for ( x=0 ; x<wb; x++ ) for ( y=0 ; y<hb; y++ )
263266 N1 [x*hb1+y] += H[o*wb*hb+x*hb+y]*H[o*wb*hb+x*hb+y];
264267 for ( x=0 ; x<wb-1 ; x++ ) for ( y=0 ; y<hb-1 ; y++ ) {
265- n=N1 +x*hb1+y; *n=1 /float (sqrt (n[0 ]+n[1 ]+n[hb1]+n[hb1+1 ]+eps)); }
268+ float * n=N1 +x*hb1+y; *n=1 /float (sqrt (n[0 ]+n[1 ]+n[hb1]+n[hb1+1 ]+eps)); }
266269 x=0 ; dx= 1 ; dy= 1 ; y=0 ; N[x*hb1+y]=N[(x+dx)*hb1+y+dy];
267270 x=0 ; dx= 1 ; dy= 0 ; for (y=0 ; y<hb1; y++) N[x*hb1+y]=N[(x+dx)*hb1+y+dy];
268271 x=0 ; dx= 1 ; dy=-1 ; y=hb1-1 ; N[x*hb1+y]=N[(x+dx)*hb1+y+dy];
@@ -322,16 +325,15 @@ void fhog( float *M, float *O, float *H, int h, int w, int binSize,
322325 int nOrients, int softBin, float clip )
323326{
324327 const int hb=h/binSize, wb=w/binSize, nb=hb*wb, nbo=nb*nOrients;
325- float *N, *R1 , *R2 ; int o, x;
326328 // compute unnormalized constrast sensitive histograms
327- R1 = (float *) wrCalloc (wb*hb* nOrients* 2 + 2 ,sizeof (float ));
329+ float * R1 = (float *) wrCalloc (static_cast < size_t >(wb) * static_cast < size_t >(hb) * static_cast < size_t >( nOrients) * 2 + 2 , sizeof (float ));
328330 gradHist ( M, O, R1 , h, w, binSize, nOrients*2 , softBin, true );
329331 // compute unnormalized contrast insensitive histograms
330- R2 = (float *) wrCalloc (wb*hb* nOrients, sizeof (float ));
331- for ( o=0 ; o<nOrients; o++ ) for ( x=0 ; x<nb; x++ )
332+ float * R2 = (float *) wrCalloc (static_cast < size_t >(wb) * static_cast < size_t >(hb) * static_cast < size_t >( nOrients), sizeof (float ));
333+ for (int o=0 ; o<nOrients; o++ ) for (int x=0 ; x<nb; x++ )
332334 R2 [o*nb+x] = R1 [o*nb+x]+R1 [(o+nOrients)*nb+x];
333335 // compute block normalization values
334- N = hogNormMatrix ( R2 , nOrients, hb, wb, binSize );
336+ float * N = hogNormMatrix ( R2 , nOrients, hb, wb, binSize );
335337 // normalized histograms and texture channels
336338 hogChannels ( H+nbo*0 , R1 , N, hb, wb, nOrients*2 , clip, 1 );
337339 hogChannels ( H+nbo*2 , R2 , N, hb, wb, nOrients*1 , clip, 1 );
@@ -456,9 +458,10 @@ float* fhog(float *M,float* O,int height,int width,int /*channel*/,int *h,int *w
456458 *h = height/binSize;
457459 *w = width/binSize;
458460 *d = nOrients*3 +5 ;
461+ const size_t allSize = static_cast <size_t >(*h) * static_cast <size_t >(*w) * static_cast <size_t >(*d);
459462
460- float * H = new float [(*h)*(*w)*(*d) ];
461- memset (H,0 ,(*h)*(*w)*(*d)* sizeof (float ));
463+ float * H = new float [allSize ];
464+ memset (H, 0 , allSize * sizeof (float ));
462465
463466 fhog ( M, O, H, height, width, binSize, nOrients, -1 , clip );
464467
0 commit comments