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