| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 // Original code is licensed as follows: | 6 // Original code is licensed as follows: |
| 7 /* | 7 /* |
| 8 * Copyright 2009 ZXing authors | 8 * Copyright 2009 ZXing authors |
| 9 * | 9 * |
| 10 * Licensed under the Apache License, Version 2.0 (the "License"); | 10 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 * you may not use this file except in compliance with the License. | 11 * you may not use this file except in compliance with the License. |
| 12 * You may obtain a copy of the License at | 12 * You may obtain a copy of the License at |
| 13 * | 13 * |
| 14 * http://www.apache.org/licenses/LICENSE-2.0 | 14 * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 * | 15 * |
| 16 * Unless required by applicable law or agreed to in writing, software | 16 * Unless required by applicable law or agreed to in writing, software |
| 17 * distributed under the License is distributed on an "AS IS" BASIS, | 17 * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 * See the License for the specific language governing permissions and | 19 * See the License for the specific language governing permissions and |
| 20 * limitations under the License. | 20 * limitations under the License. |
| 21 */ | 21 */ |
| 22 | 22 |
| 23 #include "../barcode.h" | 23 #include "../barcode.h" |
| 24 #include "../BC_Binarizer.h" | 24 #include "../BC_Binarizer.h" |
| 25 #include "../BC_LuminanceSource.h" | 25 #include "../BC_LuminanceSource.h" |
| 26 #include "BC_CommonBitMatrix.h" | 26 #include "BC_CommonBitMatrix.h" |
| 27 #include "BC_CommonBitArray.h" | 27 #include "BC_CommonBitArray.h" |
| 28 #include "BC_GlobalHistogramBinarizer.h" | 28 #include "BC_GlobalHistogramBinarizer.h" |
| 29 const FX_INT32 LUMINANCE_BITS = 5; | 29 const int32_t LUMINANCE_BITS = 5; |
| 30 const FX_INT32 LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; | 30 const int32_t LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; |
| 31 const FX_INT32 LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; | 31 const int32_t LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; |
| 32 CBC_GlobalHistogramBinarizer::CBC_GlobalHistogramBinarizer(CBC_LuminanceSource *
source): CBC_Binarizer(source) | 32 CBC_GlobalHistogramBinarizer::CBC_GlobalHistogramBinarizer(CBC_LuminanceSource *
source): CBC_Binarizer(source) |
| 33 { | 33 { |
| 34 } | 34 } |
| 35 CBC_GlobalHistogramBinarizer::~CBC_GlobalHistogramBinarizer() | 35 CBC_GlobalHistogramBinarizer::~CBC_GlobalHistogramBinarizer() |
| 36 { | 36 { |
| 37 } | 37 } |
| 38 CBC_CommonBitArray *CBC_GlobalHistogramBinarizer::GetBlackRow(FX_INT32 y, CBC_Co
mmonBitArray *row, FX_INT32 &e) | 38 CBC_CommonBitArray *CBC_GlobalHistogramBinarizer::GetBlackRow(int32_t y, CBC_Com
monBitArray *row, int32_t &e) |
| 39 { | 39 { |
| 40 CBC_LuminanceSource *source = GetLuminanceSource(); | 40 CBC_LuminanceSource *source = GetLuminanceSource(); |
| 41 FX_INT32 width = source->GetWidth(); | 41 int32_t width = source->GetWidth(); |
| 42 CBC_AutoPtr<CBC_CommonBitArray> result(FX_NEW CBC_CommonBitArray(width)); | 42 CBC_AutoPtr<CBC_CommonBitArray> result(FX_NEW CBC_CommonBitArray(width)); |
| 43 InitArrays(width); | 43 InitArrays(width); |
| 44 CFX_ByteArray *localLuminances = source->GetRow(y, m_luminance, e); | 44 CFX_ByteArray *localLuminances = source->GetRow(y, m_luminance, e); |
| 45 if (e != BCExceptionNO) { | 45 if (e != BCExceptionNO) { |
| 46 return result.release(); | 46 return result.release(); |
| 47 } | 47 } |
| 48 CFX_Int32Array localBuckets; | 48 CFX_Int32Array localBuckets; |
| 49 localBuckets.Copy(m_buckets); | 49 localBuckets.Copy(m_buckets); |
| 50 FX_INT32 x; | 50 int32_t x; |
| 51 for (x = 0; x < width; x++) { | 51 for (x = 0; x < width; x++) { |
| 52 FX_INT32 pixel = (*localLuminances)[x] & 0xff; | 52 int32_t pixel = (*localLuminances)[x] & 0xff; |
| 53 localBuckets[pixel >> LUMINANCE_SHIFT]++; | 53 localBuckets[pixel >> LUMINANCE_SHIFT]++; |
| 54 } | 54 } |
| 55 FX_INT32 blackPoint = EstimateBlackPoint(localBuckets, e); | 55 int32_t blackPoint = EstimateBlackPoint(localBuckets, e); |
| 56 if (e != BCExceptionNO) { | 56 if (e != BCExceptionNO) { |
| 57 return result.release(); | 57 return result.release(); |
| 58 } | 58 } |
| 59 FX_INT32 left = (*localLuminances)[0] & 0xff; | 59 int32_t left = (*localLuminances)[0] & 0xff; |
| 60 FX_INT32 center = (*localLuminances)[1] & 0xff; | 60 int32_t center = (*localLuminances)[1] & 0xff; |
| 61 for (x = 1; x < width - 1; x++) { | 61 for (x = 1; x < width - 1; x++) { |
| 62 FX_INT32 right = (*localLuminances)[x + 1] & 0xff; | 62 int32_t right = (*localLuminances)[x + 1] & 0xff; |
| 63 FX_INT32 luminance = ((center << 2) - left - right) >> 1; | 63 int32_t luminance = ((center << 2) - left - right) >> 1; |
| 64 if (luminance < blackPoint) { | 64 if (luminance < blackPoint) { |
| 65 result->Set(x); | 65 result->Set(x); |
| 66 } | 66 } |
| 67 left = center; | 67 left = center; |
| 68 center = right; | 68 center = right; |
| 69 } | 69 } |
| 70 return result.release(); | 70 return result.release(); |
| 71 } | 71 } |
| 72 CBC_CommonBitMatrix *CBC_GlobalHistogramBinarizer::GetBlackMatrix(FX_INT32 &e) | 72 CBC_CommonBitMatrix *CBC_GlobalHistogramBinarizer::GetBlackMatrix(int32_t &e) |
| 73 { | 73 { |
| 74 CBC_LuminanceSource *source = GetLuminanceSource(); | 74 CBC_LuminanceSource *source = GetLuminanceSource(); |
| 75 FX_INT32 width = source->GetWidth(); | 75 int32_t width = source->GetWidth(); |
| 76 FX_INT32 height = source->GetHeight(); | 76 int32_t height = source->GetHeight(); |
| 77 CBC_CommonBitMatrix *BitMatrixTemp = FX_NEW CBC_CommonBitMatrix(); | 77 CBC_CommonBitMatrix *BitMatrixTemp = FX_NEW CBC_CommonBitMatrix(); |
| 78 BitMatrixTemp->Init(width, height); | 78 BitMatrixTemp->Init(width, height); |
| 79 CBC_AutoPtr<CBC_CommonBitMatrix> matrix(BitMatrixTemp); | 79 CBC_AutoPtr<CBC_CommonBitMatrix> matrix(BitMatrixTemp); |
| 80 InitArrays(width); | 80 InitArrays(width); |
| 81 CFX_Int32Array localBuckets; | 81 CFX_Int32Array localBuckets; |
| 82 localBuckets.Copy(m_buckets); | 82 localBuckets.Copy(m_buckets); |
| 83 FX_INT32 y; | 83 int32_t y; |
| 84 for (y = 1; y < 5; y++) { | 84 for (y = 1; y < 5; y++) { |
| 85 FX_INT32 row = height * y / 5; | 85 int32_t row = height * y / 5; |
| 86 CFX_ByteArray *localLuminances = source->GetRow(row, m_luminance, e); | 86 CFX_ByteArray *localLuminances = source->GetRow(row, m_luminance, e); |
| 87 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 87 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
| 88 FX_INT32 right = (width << 2) / 5; | 88 int32_t right = (width << 2) / 5; |
| 89 FX_INT32 x; | 89 int32_t x; |
| 90 for (x = width / 5; x < right; x++) { | 90 for (x = width / 5; x < right; x++) { |
| 91 FX_INT32 pixel = (*localLuminances)[x] & 0xff; | 91 int32_t pixel = (*localLuminances)[x] & 0xff; |
| 92 localBuckets[pixel >> LUMINANCE_SHIFT]++; | 92 localBuckets[pixel >> LUMINANCE_SHIFT]++; |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 FX_INT32 blackPoint = EstimateBlackPoint(localBuckets, e); | 95 int32_t blackPoint = EstimateBlackPoint(localBuckets, e); |
| 96 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 96 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
| 97 CBC_AutoPtr<CFX_ByteArray> localLuminances(source->GetMatrix()); | 97 CBC_AutoPtr<CFX_ByteArray> localLuminances(source->GetMatrix()); |
| 98 for (y = 0; y < height; y++) { | 98 for (y = 0; y < height; y++) { |
| 99 FX_INT32 offset = y * width; | 99 int32_t offset = y * width; |
| 100 for (FX_INT32 x = 0; x < width; x++) { | 100 for (int32_t x = 0; x < width; x++) { |
| 101 FX_INT32 pixel = (*localLuminances)[offset + x] & 0xff; | 101 int32_t pixel = (*localLuminances)[offset + x] & 0xff; |
| 102 if (pixel < blackPoint) { | 102 if (pixel < blackPoint) { |
| 103 matrix->Set(x, y); | 103 matrix->Set(x, y); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 return matrix.release(); | 107 return matrix.release(); |
| 108 } | 108 } |
| 109 void CBC_GlobalHistogramBinarizer::InitArrays(FX_INT32 luminanceSize) | 109 void CBC_GlobalHistogramBinarizer::InitArrays(int32_t luminanceSize) |
| 110 { | 110 { |
| 111 if(m_luminance.GetSize() < luminanceSize) { | 111 if(m_luminance.GetSize() < luminanceSize) { |
| 112 m_luminance.SetSize(luminanceSize); | 112 m_luminance.SetSize(luminanceSize); |
| 113 } | 113 } |
| 114 if(m_buckets.GetSize() <= 0) { | 114 if(m_buckets.GetSize() <= 0) { |
| 115 m_buckets.SetSize(LUMINANCE_BUCKETS); | 115 m_buckets.SetSize(LUMINANCE_BUCKETS); |
| 116 } else { | 116 } else { |
| 117 FX_INT32 x; | 117 int32_t x; |
| 118 for(x = 0; x < LUMINANCE_BUCKETS; x++) { | 118 for(x = 0; x < LUMINANCE_BUCKETS; x++) { |
| 119 m_buckets[x] = 0; | 119 m_buckets[x] = 0; |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 FX_INT32 CBC_GlobalHistogramBinarizer::EstimateBlackPoint(CFX_Int32Array &bucket
s, FX_INT32 &e) | 123 int32_t CBC_GlobalHistogramBinarizer::EstimateBlackPoint(CFX_Int32Array &buckets
, int32_t &e) |
| 124 { | 124 { |
| 125 FX_INT32 numBuckets = buckets.GetSize(); | 125 int32_t numBuckets = buckets.GetSize(); |
| 126 FX_INT32 maxBucketCount = 0; | 126 int32_t maxBucketCount = 0; |
| 127 FX_INT32 firstPeak = 0; | 127 int32_t firstPeak = 0; |
| 128 FX_INT32 firstPeakSize = 0; | 128 int32_t firstPeakSize = 0; |
| 129 FX_INT32 x; | 129 int32_t x; |
| 130 for (x = 0; x < numBuckets; x++) { | 130 for (x = 0; x < numBuckets; x++) { |
| 131 if (buckets[x] > firstPeakSize) { | 131 if (buckets[x] > firstPeakSize) { |
| 132 firstPeak = x; | 132 firstPeak = x; |
| 133 firstPeakSize = buckets[x]; | 133 firstPeakSize = buckets[x]; |
| 134 } | 134 } |
| 135 if (buckets[x] > maxBucketCount) { | 135 if (buckets[x] > maxBucketCount) { |
| 136 maxBucketCount = buckets[x]; | 136 maxBucketCount = buckets[x]; |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 FX_INT32 secondPeak = 0; | 139 int32_t secondPeak = 0; |
| 140 FX_INT32 secondPeakScore = 0; | 140 int32_t secondPeakScore = 0; |
| 141 for (x = 0; x < numBuckets; x++) { | 141 for (x = 0; x < numBuckets; x++) { |
| 142 FX_INT32 distanceToBiggest = x - firstPeak; | 142 int32_t distanceToBiggest = x - firstPeak; |
| 143 FX_INT32 score = buckets[x] * distanceToBiggest * distanceToBiggest; | 143 int32_t score = buckets[x] * distanceToBiggest * distanceToBiggest; |
| 144 if (score > secondPeakScore) { | 144 if (score > secondPeakScore) { |
| 145 secondPeak = x; | 145 secondPeak = x; |
| 146 secondPeakScore = score; | 146 secondPeakScore = score; |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 if (firstPeak > secondPeak) { | 149 if (firstPeak > secondPeak) { |
| 150 FX_INT32 temp = firstPeak; | 150 int32_t temp = firstPeak; |
| 151 firstPeak = secondPeak; | 151 firstPeak = secondPeak; |
| 152 secondPeak = temp; | 152 secondPeak = temp; |
| 153 } | 153 } |
| 154 if (secondPeak - firstPeak <= numBuckets >> 4) { | 154 if (secondPeak - firstPeak <= numBuckets >> 4) { |
| 155 e = BCExceptionRead; | 155 e = BCExceptionRead; |
| 156 return 0; | 156 return 0; |
| 157 } | 157 } |
| 158 FX_INT32 bestValley = secondPeak - 1; | 158 int32_t bestValley = secondPeak - 1; |
| 159 FX_INT32 bestValleyScore = -1; | 159 int32_t bestValleyScore = -1; |
| 160 for (x = secondPeak - 1; x > firstPeak; x--) { | 160 for (x = secondPeak - 1; x > firstPeak; x--) { |
| 161 FX_INT32 fromFirst = x - firstPeak; | 161 int32_t fromFirst = x - firstPeak; |
| 162 FX_INT32 score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCo
unt - buckets[x]); | 162 int32_t score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCou
nt - buckets[x]); |
| 163 if (score > bestValleyScore) { | 163 if (score > bestValleyScore) { |
| 164 bestValley = x; | 164 bestValley = x; |
| 165 bestValleyScore = score; | 165 bestValleyScore = score; |
| 166 } | 166 } |
| 167 } | 167 } |
| 168 return bestValley << LUMINANCE_SHIFT; | 168 return bestValley << LUMINANCE_SHIFT; |
| 169 } | 169 } |
| OLD | NEW |