| 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 "xfa/src/fxbarcode/common/BC_GlobalHistogramBinarizer.h" |
| 24 |
| 25 #include <memory> |
| 26 |
| 23 #include "xfa/src/fxbarcode/BC_Binarizer.h" | 27 #include "xfa/src/fxbarcode/BC_Binarizer.h" |
| 24 #include "xfa/src/fxbarcode/BC_LuminanceSource.h" | 28 #include "xfa/src/fxbarcode/BC_LuminanceSource.h" |
| 25 #include "xfa/src/fxbarcode/common/BC_CommonBitArray.h" | 29 #include "xfa/src/fxbarcode/common/BC_CommonBitArray.h" |
| 26 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h" | 30 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h" |
| 27 #include "xfa/src/fxbarcode/common/BC_GlobalHistogramBinarizer.h" | |
| 28 #include "xfa/src/fxbarcode/utils.h" | 31 #include "xfa/src/fxbarcode/utils.h" |
| 29 | 32 |
| 30 const int32_t LUMINANCE_BITS = 5; | 33 const int32_t LUMINANCE_BITS = 5; |
| 31 const int32_t LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; | 34 const int32_t LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; |
| 32 const int32_t LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; | 35 const int32_t LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; |
| 33 | 36 |
| 34 CBC_GlobalHistogramBinarizer::CBC_GlobalHistogramBinarizer( | 37 CBC_GlobalHistogramBinarizer::CBC_GlobalHistogramBinarizer( |
| 35 CBC_LuminanceSource* source) | 38 CBC_LuminanceSource* source) |
| 36 : CBC_Binarizer(source) {} | 39 : CBC_Binarizer(source) {} |
| 37 CBC_GlobalHistogramBinarizer::~CBC_GlobalHistogramBinarizer() {} | 40 CBC_GlobalHistogramBinarizer::~CBC_GlobalHistogramBinarizer() {} |
| 38 CBC_CommonBitArray* CBC_GlobalHistogramBinarizer::GetBlackRow( | 41 CBC_CommonBitArray* CBC_GlobalHistogramBinarizer::GetBlackRow( |
| 39 int32_t y, | 42 int32_t y, |
| 40 CBC_CommonBitArray* row, | 43 CBC_CommonBitArray* row, |
| 41 int32_t& e) { | 44 int32_t& e) { |
| 42 CBC_LuminanceSource* source = GetLuminanceSource(); | 45 CBC_LuminanceSource* source = GetLuminanceSource(); |
| 43 int32_t width = source->GetWidth(); | 46 int32_t width = source->GetWidth(); |
| 44 CBC_AutoPtr<CBC_CommonBitArray> result(new CBC_CommonBitArray(width)); | 47 std::unique_ptr<CBC_CommonBitArray> result(new CBC_CommonBitArray(width)); |
| 45 InitArrays(width); | 48 InitArrays(width); |
| 46 CFX_ByteArray* localLuminances = source->GetRow(y, m_luminance, e); | 49 CFX_ByteArray* localLuminances = source->GetRow(y, m_luminance, e); |
| 47 if (e != BCExceptionNO) { | 50 if (e != BCExceptionNO) { |
| 48 return result.release(); | 51 return result.release(); |
| 49 } | 52 } |
| 50 CFX_Int32Array localBuckets; | 53 CFX_Int32Array localBuckets; |
| 51 localBuckets.Copy(m_buckets); | 54 localBuckets.Copy(m_buckets); |
| 52 int32_t x; | 55 int32_t x; |
| 53 for (x = 0; x < width; x++) { | 56 for (x = 0; x < width; x++) { |
| 54 int32_t pixel = (*localLuminances)[x] & 0xff; | 57 int32_t pixel = (*localLuminances)[x] & 0xff; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 68 } | 71 } |
| 69 left = center; | 72 left = center; |
| 70 center = right; | 73 center = right; |
| 71 } | 74 } |
| 72 return result.release(); | 75 return result.release(); |
| 73 } | 76 } |
| 74 CBC_CommonBitMatrix* CBC_GlobalHistogramBinarizer::GetBlackMatrix(int32_t& e) { | 77 CBC_CommonBitMatrix* CBC_GlobalHistogramBinarizer::GetBlackMatrix(int32_t& e) { |
| 75 CBC_LuminanceSource* source = GetLuminanceSource(); | 78 CBC_LuminanceSource* source = GetLuminanceSource(); |
| 76 int32_t width = source->GetWidth(); | 79 int32_t width = source->GetWidth(); |
| 77 int32_t height = source->GetHeight(); | 80 int32_t height = source->GetHeight(); |
| 78 CBC_CommonBitMatrix* BitMatrixTemp = new CBC_CommonBitMatrix(); | 81 std::unique_ptr<CBC_CommonBitMatrix> matrix(new CBC_CommonBitMatrix()); |
| 79 BitMatrixTemp->Init(width, height); | 82 matrix->Init(width, height); |
| 80 CBC_AutoPtr<CBC_CommonBitMatrix> matrix(BitMatrixTemp); | |
| 81 InitArrays(width); | 83 InitArrays(width); |
| 82 CFX_Int32Array localBuckets; | 84 CFX_Int32Array localBuckets; |
| 83 localBuckets.Copy(m_buckets); | 85 localBuckets.Copy(m_buckets); |
| 84 int32_t y; | 86 int32_t y; |
| 85 for (y = 1; y < 5; y++) { | 87 for (y = 1; y < 5; y++) { |
| 86 int32_t row = height * y / 5; | 88 int32_t row = height * y / 5; |
| 87 CFX_ByteArray* localLuminances = source->GetRow(row, m_luminance, e); | 89 CFX_ByteArray* localLuminances = source->GetRow(row, m_luminance, e); |
| 88 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 90 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
| 89 int32_t right = (width << 2) / 5; | 91 int32_t right = (width << 2) / 5; |
| 90 int32_t x; | 92 int32_t x; |
| 91 for (x = width / 5; x < right; x++) { | 93 for (x = width / 5; x < right; x++) { |
| 92 int32_t pixel = (*localLuminances)[x] & 0xff; | 94 int32_t pixel = (*localLuminances)[x] & 0xff; |
| 93 localBuckets[pixel >> LUMINANCE_SHIFT]++; | 95 localBuckets[pixel >> LUMINANCE_SHIFT]++; |
| 94 } | 96 } |
| 95 } | 97 } |
| 96 int32_t blackPoint = EstimateBlackPoint(localBuckets, e); | 98 int32_t blackPoint = EstimateBlackPoint(localBuckets, e); |
| 97 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 99 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
| 98 CBC_AutoPtr<CFX_ByteArray> localLuminances(source->GetMatrix()); | 100 std::unique_ptr<CFX_ByteArray> localLuminances(source->GetMatrix()); |
| 99 for (y = 0; y < height; y++) { | 101 for (y = 0; y < height; y++) { |
| 100 int32_t offset = y * width; | 102 int32_t offset = y * width; |
| 101 for (int32_t x = 0; x < width; x++) { | 103 for (int32_t x = 0; x < width; x++) { |
| 102 int32_t pixel = (*localLuminances)[offset + x] & 0xff; | 104 int32_t pixel = (*localLuminances)[offset + x] & 0xff; |
| 103 if (pixel < blackPoint) { | 105 if (pixel < blackPoint) { |
| 104 matrix->Set(x, y); | 106 matrix->Set(x, y); |
| 105 } | 107 } |
| 106 } | 108 } |
| 107 } | 109 } |
| 108 return matrix.release(); | 110 return matrix.release(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 int32_t fromFirst = x - firstPeak; | 164 int32_t fromFirst = x - firstPeak; |
| 163 int32_t score = fromFirst * fromFirst * (secondPeak - x) * | 165 int32_t score = fromFirst * fromFirst * (secondPeak - x) * |
| 164 (maxBucketCount - buckets[x]); | 166 (maxBucketCount - buckets[x]); |
| 165 if (score > bestValleyScore) { | 167 if (score > bestValleyScore) { |
| 166 bestValley = x; | 168 bestValley = x; |
| 167 bestValleyScore = score; | 169 bestValleyScore = score; |
| 168 } | 170 } |
| 169 } | 171 } |
| 170 return bestValley << LUMINANCE_SHIFT; | 172 return bestValley << LUMINANCE_SHIFT; |
| 171 } | 173 } |
| OLD | NEW |