@@ -55,7 +55,9 @@ Ref<Binarizer> HybridBinarizer::createBinarizer(Ref<LuminanceSource> source) {
5555}
5656
5757int HybridBinarizer::initBlockIntegral () {
58- blockIntegral_ = new Array<int >(width * height);
58+ blockIntegralWidth = subWidth_ + 1 ;
59+ blockIntegralHeight = subHeight_ + 1 ;
60+ blockIntegral_ = new Array<int >(blockIntegralWidth * blockIntegralHeight);
5961
6062 int * integral = blockIntegral_->data ();
6163
@@ -64,34 +66,27 @@ int HybridBinarizer::initBlockIntegral() {
6466 // first row only
6567 int rs = 0 ;
6668
67- for (int j = 0 ; j < width ; j++) {
69+ for (int j = 0 ; j < blockIntegralWidth ; j++) {
6870 integral[j] = 0 ;
6971 }
7072
71- for (int i = 0 ; i < height; i++) {
72- integral[i * width] = 0 ;
73- }
74-
75- for (int j = 0 ; j < subWidth_; j++) {
76- rs += blocks_[j].threshold ;
77- integral[width + j + 1 ] = rs;
73+ for (int i = 0 ; i < blockIntegralHeight; i++) {
74+ integral[i * blockIntegralWidth] = 0 ;
7875 }
7976
8077 // remaining cells are sum above and to the left
81- int offset = width;
8278 int offsetBlock = 0 ;
79+ int offsetIntegral = 0 ;
8380
84- for (int i = 1 ; i < subHeight_; ++i) {
81+ for (int i = 0 ; i < subHeight_; ++i) {
8582 // therow = grayByte_->getByteRow(i);
8683 offsetBlock = i * subWidth_;
87-
84+ offsetIntegral = (i + 1 ) * blockIntegralWidth;
8885 rs = 0 ;
8986
90- offset += width;
91-
9287 for (int j = 0 ; j < subWidth_; ++j) {
9388 rs += blocks_[offsetBlock + j].threshold ;
94- integral[offset + j + 1 ] = rs + integral[offset - width + j + 1 ];
89+ integral[offsetIntegral + j + 1 ] = rs + integral[offsetIntegral - blockIntegralWidth + j + 1 ];
9590 }
9691 }
9792
@@ -201,8 +196,8 @@ void HybridBinarizer::calculateThresholdForBlock(Ref<ByteMatrix>& _luminances, i
201196 int sum = 0 ;
202197 // int sum2 = 0;
203198
204- int offset1 = (top - THRES_BLOCKSIZE) * (subWidth + 1 ) + left - THRES_BLOCKSIZE;
205- int offset2 = (top + THRES_BLOCKSIZE + 1 ) * (subWidth + 1 ) + left - THRES_BLOCKSIZE;
199+ int offset1 = (top - THRES_BLOCKSIZE) * blockIntegralWidth + left - THRES_BLOCKSIZE;
200+ int offset2 = (top + THRES_BLOCKSIZE + 1 ) * blockIntegralWidth + left - THRES_BLOCKSIZE;
206201
207202 int blocksize = THRES_BLOCKSIZE * 2 + 1 ;
208203
0 commit comments