Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. ptrdiff_t BinarizeSavuola2Inplace ( quint8*   pageBits,
  2.                                     ptrdiff_t pageStride,
  3.                                     ptrdiff_t pageWidth,
  4.                                     ptrdiff_t pageHeight,
  5.                                     ptrdiff_t radius,
  6.                                     double    factor
  7.                                   ) throw() {
  8.   assert(pageBits);
  9.    
  10.   QVarLengthArray <double, 2048> rowSum ( 2 * pageWidth ); 
  11.  
  12.   double *pISum  = rowSum.data(),     // sums
  13.          *pISSum = pISum + pageWidth; // sums of squares
  14.  
  15.   memset( rowSum.data(), 0, 2 * pageWidth * sizeof(double) );
  16.    
  17.   ptrdiff_t swStride  = ( pageWidth + 15 ) & (~15); // align by 16 bytes each row
  18.   ptrdiff_t wmovesize = 2 * radius * swStride;
  19.  
  20.   quint8  *swindow_unaligned = static_cast<quint8*> ( malloc( swStride + wmovesize + 16 ) );
  21.   if (!swindow_unaligned)
  22.     return 0;
  23.  
  24.   quint8 *swindow     = (quint8*) ( (ptrdiff_t ( swindow_unaligned ) + 15) & ( ~15 ) );
  25.   quint8 *currWRow    = swindow + radius * swStride;
  26.   quint8 *bottomWRow  = swindow + wmovesize;
  27.    
  28.   // fill swindow
  29.   quint8* cwRow = swindow;
  30.  
  31.   // top part - is a copy of first row radius times
  32.   while ( cwRow != currWRow ) {
  33.     memcpy(cwRow, pageBits, pageWidth );
  34.     cwRow += swStride;
  35.   }
  36.  
  37.   // bottom part - is a copy of input image
  38.   quint8 *srcRow = pageBits;
  39.   while ( cwRow != (bottomWRow + swStride) ) {
  40.     memcpy(cwRow, srcRow, pageWidth );
  41.     cwRow  += swStride;
  42.     srcRow += pageStride;
  43.   }
  44.  
  45.   ptrdiff_t y,x, ymin,ymax, xmin, xmax;
  46.   size_t    cpix;
  47.  
  48.   double mean, std, area, wsum, wssum, factor1 = 1. - factor;
  49.  
  50.   // initialize rowSum array
  51.   for ( y = 0, srcRow = pageBits; y < radius; ++y, srcRow += pageStride )
  52.     for ( x = 0; x < pageWidth; ++x ) {
  53.       cpix = srcRow [ x ];
  54.       pISum [ x ] += cpix;
  55.       pISSum[ x ] += cpix * cpix;
  56.     }
  57.  
  58.   quint8*   lRow = pageBits + pageStride * qMin( pageHeight-1, radius );
  59.  
  60.   for ( y = 0, srcRow = pageBits; y < pageHeight; ++y, srcRow += pageStride ) {
  61.     ymin = qMax(ptrdiff_t(0),y-radius);
  62.     ymax = qMin(pageHeight-1,y+radius);
  63.    
  64.     // initialize window sums
  65.     for ( wsum = wssum = 0., x = 0; x < radius; ++x ) {
  66.       wsum  += pISum [ x ];
  67.       wssum += pISSum[ x ];
  68.     }
  69.    
  70.     // loop on row
  71.     for ( x = 0; x < pageWidth; ++x ) {
  72.       xmin = qMax(ptrdiff_t(0),x-radius);
  73.       xmax = qMin(pageWidth-1, x+radius);
  74.      
  75.       area = (xmax-xmin+1.)*(ymax-ymin+1.);
  76.      
  77.       mean = wsum / area;
  78.       std  = sqrt((wssum - wsum*wsum/area)/area);
  79.      
  80.       // threshold the current pixel
  81.       srcRow [ x ] = currWRow [ x ] < mean * ( factor1 + factor *  std * .0078125 )  ? 0 : 255;
  82.      
  83.       // Housekeeping for left and right boundary cases
  84.       if ( xmin ) {
  85.         // Subtract leftmost column from window sums
  86.         wsum  -= pISum [ xmin ];
  87.         wssum -= pISSum[ xmin ];
  88.       }
  89.      
  90.       if ( xmax < pageWidth - 1 ) {
  91.         // Add rightmost collumn to window histogram
  92.         wsum  += pISum [ xmax ];
  93.         wssum += pISSum[ xmax ];
  94.       }
  95.     }
  96.    
  97.     // Maintain sliding window
  98.     memmove ( swindow,    swindow + swStride, wmovesize );
  99.     memcpy  ( bottomWRow, lRow,               pageWidth );
  100.    
  101.     // Housekeeping for top and bottom boundary cases
  102.     if ( ymin ) {
  103.       // Subtract top row from row sums
  104.       for ( x = 0; x < pageWidth; ++x ) {
  105.         cpix = swindow [ x ];
  106.         pISum [ x ] -= cpix;
  107.         pISSum[ x ] -= cpix * cpix;
  108.       }
  109.     }
  110.    
  111.     if ( ymax < pageHeight - 1 ) {
  112.       // Add bottom collumn to row sums
  113.       for ( x = 0; x < pageWidth; ++x ) {
  114.         cpix = bottomWRow [ x ];
  115.         pISum [ x ] += cpix;
  116.         pISSum[ x ] += cpix * cpix;
  117.       }
  118.       // housekeeping for last row
  119.       lRow += pageStride;
  120.     }
  121.   }
  122.  
  123.   free(swindow_unaligned);
  124.  
  125.   return 1;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement