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 <memory> |
| 24 |
23 #include "xfa/fxbarcode/BC_BinaryBitmap.h" | 25 #include "xfa/fxbarcode/BC_BinaryBitmap.h" |
24 #include "xfa/fxbarcode/BC_ResultPoint.h" | 26 #include "xfa/fxbarcode/BC_ResultPoint.h" |
25 #include "xfa/fxbarcode/common/BC_CommonBitArray.h" | 27 #include "xfa/fxbarcode/common/BC_CommonBitArray.h" |
26 #include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" | 28 #include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" |
27 #include "xfa/fxbarcode/pdf417/BC_PDF417Detector.h" | 29 #include "xfa/fxbarcode/pdf417/BC_PDF417Detector.h" |
28 #include "xfa/fxbarcode/pdf417/BC_PDF417DetectorResult.h" | 30 #include "xfa/fxbarcode/pdf417/BC_PDF417DetectorResult.h" |
29 #include "xfa/fxbarcode/utils.h" | 31 #include "xfa/fxbarcode/utils.h" |
30 | 32 |
31 #define INTEGER_MAX 2147483647 | 33 #define INTEGER_MAX 2147483647 |
32 | 34 |
33 int32_t CBC_Detector::INDEXES_START_PATTERN[] = {0, 4, 1, 5}; | 35 int32_t CBC_Detector::INDEXES_START_PATTERN[] = {0, 4, 1, 5}; |
34 int32_t CBC_Detector::INDEXES_STOP_PATTERN[] = {6, 2, 7, 3}; | 36 int32_t CBC_Detector::INDEXES_STOP_PATTERN[] = {6, 2, 7, 3}; |
35 int32_t CBC_Detector::INTEGER_MATH_SHIFT = 8; | 37 int32_t CBC_Detector::INTEGER_MATH_SHIFT = 8; |
36 int32_t CBC_Detector::PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 | 38 int32_t CBC_Detector::PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 |
37 << INTEGER_MATH_SHIFT; | 39 << INTEGER_MATH_SHIFT; |
38 int32_t CBC_Detector::MAX_AVG_VARIANCE = | 40 int32_t CBC_Detector::MAX_AVG_VARIANCE = |
39 (int32_t)(PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f); | 41 (int32_t)(PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f); |
40 int32_t CBC_Detector::MAX_INDIVIDUAL_VARIANCE = | 42 int32_t CBC_Detector::MAX_INDIVIDUAL_VARIANCE = |
41 (int32_t)(PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.8f); | 43 (int32_t)(PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.8f); |
42 int32_t CBC_Detector::START_PATTERN[] = {8, 1, 1, 1, 1, 1, 1, 3}; | 44 int32_t CBC_Detector::START_PATTERN[] = {8, 1, 1, 1, 1, 1, 1, 3}; |
43 int32_t CBC_Detector::STOP_PATTERN[] = {7, 1, 1, 3, 1, 1, 1, 2, 1}; | 45 int32_t CBC_Detector::STOP_PATTERN[] = {7, 1, 1, 3, 1, 1, 1, 2, 1}; |
44 int32_t CBC_Detector::MAX_PIXEL_DRIFT = 3; | 46 int32_t CBC_Detector::MAX_PIXEL_DRIFT = 3; |
45 int32_t CBC_Detector::MAX_PATTERN_DRIFT = 5; | 47 int32_t CBC_Detector::MAX_PATTERN_DRIFT = 5; |
46 int32_t CBC_Detector::SKIPPED_ROW_COUNT_MAX = 25; | 48 int32_t CBC_Detector::SKIPPED_ROW_COUNT_MAX = 25; |
47 int32_t CBC_Detector::ROW_STEP = 5; | 49 int32_t CBC_Detector::ROW_STEP = 5; |
48 int32_t CBC_Detector::BARCODE_MIN_HEIGHT = 10; | 50 int32_t CBC_Detector::BARCODE_MIN_HEIGHT = 10; |
49 | 51 |
50 CBC_Detector::CBC_Detector() {} | 52 CBC_Detector::CBC_Detector() {} |
51 CBC_Detector::~CBC_Detector() {} | 53 CBC_Detector::~CBC_Detector() {} |
| 54 |
52 CBC_PDF417DetectorResult* CBC_Detector::detect(CBC_BinaryBitmap* image, | 55 CBC_PDF417DetectorResult* CBC_Detector::detect(CBC_BinaryBitmap* image, |
53 int32_t hints, | 56 int32_t hints, |
54 FX_BOOL multiple, | 57 FX_BOOL multiple, |
55 int32_t& e) { | 58 int32_t& e) { |
56 CBC_CommonBitMatrix* bitMatrix = image->GetBlackMatrix(e); | 59 CBC_CommonBitMatrix* bitMatrix = image->GetBlackMatrix(e); |
57 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 60 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
58 CFX_PtrArray* barcodeCoordinates = detect(multiple, bitMatrix); | 61 CBC_ResultPointArrayArray* barcodeCoordinates = detect(multiple, bitMatrix); |
59 if (barcodeCoordinates->GetSize() == 0) { | 62 if (barcodeCoordinates->GetSize() == 0) { |
60 rotate180(bitMatrix); | 63 rotate180(bitMatrix); |
61 barcodeCoordinates = detect(multiple, bitMatrix); | 64 barcodeCoordinates = detect(multiple, bitMatrix); |
62 } | 65 } |
63 if (barcodeCoordinates->GetSize() == 0) { | 66 if (barcodeCoordinates->GetSize() == 0) { |
64 e = BCExceptionUnSupportedBarcode; | 67 e = BCExceptionUnSupportedBarcode; |
65 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 68 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
66 } | 69 } |
67 CBC_PDF417DetectorResult* detectorResult = | 70 return new CBC_PDF417DetectorResult(bitMatrix, barcodeCoordinates); |
68 new CBC_PDF417DetectorResult(bitMatrix, barcodeCoordinates); | |
69 return detectorResult; | |
70 } | 71 } |
71 void CBC_Detector::rotate180(CBC_CommonBitMatrix* bitMatrix) { | 72 void CBC_Detector::rotate180(CBC_CommonBitMatrix* bitMatrix) { |
72 int32_t width = bitMatrix->GetWidth(); | 73 int32_t width = bitMatrix->GetWidth(); |
73 int32_t height = bitMatrix->GetHeight(); | 74 int32_t height = bitMatrix->GetHeight(); |
74 CBC_CommonBitArray* firstRowBitArray = new CBC_CommonBitArray(width); | 75 CBC_CommonBitArray* firstRowBitArray = new CBC_CommonBitArray(width); |
75 CBC_CommonBitArray* secondRowBitArray = new CBC_CommonBitArray(width); | 76 CBC_CommonBitArray* secondRowBitArray = new CBC_CommonBitArray(width); |
76 CBC_CommonBitArray* tmpBitArray = new CBC_CommonBitArray(width); | 77 CBC_CommonBitArray* tmpBitArray = new CBC_CommonBitArray(width); |
77 for (int32_t y = 0; y<(height + 1)>> 1; y++) { | 78 for (int32_t y = 0; y<(height + 1)>> 1; y++) { |
78 CBC_CommonBitArray* temp = | 79 CBC_CommonBitArray* temp = |
79 bitMatrix->GetRow(height - 1 - y, secondRowBitArray); | 80 bitMatrix->GetRow(height - 1 - y, secondRowBitArray); |
(...skipping 17 matching lines...) Expand all Loading... |
97 CBC_CommonBitArray* array = new CBC_CommonBitArray(result->GetSize()); | 98 CBC_CommonBitArray* array = new CBC_CommonBitArray(result->GetSize()); |
98 array->Clear(); | 99 array->Clear(); |
99 int32_t size = input->GetSize(); | 100 int32_t size = input->GetSize(); |
100 for (int32_t i = 0; i < size; i++) { | 101 for (int32_t i = 0; i < size; i++) { |
101 if (input->Get(i)) { | 102 if (input->Get(i)) { |
102 array->Set(size - 1 - i); | 103 array->Set(size - 1 - i); |
103 } | 104 } |
104 } | 105 } |
105 return array; | 106 return array; |
106 } | 107 } |
107 CFX_PtrArray* CBC_Detector::detect(FX_BOOL multiple, | 108 |
108 CBC_CommonBitMatrix* bitMatrix) { | 109 CBC_ResultPointArrayArray* CBC_Detector::detect( |
109 CFX_PtrArray* barcodeCoordinates = new CFX_PtrArray; | 110 FX_BOOL multiple, |
| 111 CBC_CommonBitMatrix* bitMatrix) { |
| 112 CBC_ResultPointArrayArray* barcodeCoordinates = new CBC_ResultPointArrayArray; |
110 int32_t row = 0; | 113 int32_t row = 0; |
111 int32_t column = 0; | 114 int32_t column = 0; |
112 FX_BOOL foundBarcodeInRow = FALSE; | 115 FX_BOOL foundBarcodeInRow = FALSE; |
113 while (row < bitMatrix->GetHeight()) { | 116 while (row < bitMatrix->GetHeight()) { |
114 CFX_PtrArray* vertices = findVertices(bitMatrix, row, column); | 117 CBC_ResultPointArray* vertices = findVertices(bitMatrix, row, column); |
115 if (vertices->GetAt(0) == NULL && vertices->GetAt(3) == NULL) { | 118 if (vertices->GetAt(0) == NULL && vertices->GetAt(3) == NULL) { |
116 if (!foundBarcodeInRow) { | 119 if (!foundBarcodeInRow) { |
117 delete vertices; | 120 delete vertices; |
118 break; | 121 break; |
119 } | 122 } |
120 foundBarcodeInRow = FALSE; | 123 foundBarcodeInRow = FALSE; |
121 column = 0; | 124 column = 0; |
122 for (int32_t i = 0; i < barcodeCoordinates->GetSize(); i++) { | 125 for (int32_t i = 0; i < barcodeCoordinates->GetSize(); i++) { |
123 CFX_PtrArray* barcodeCoordinate = | 126 CBC_ResultPointArray* barcodeCoordinate = barcodeCoordinates->GetAt(i); |
124 (CFX_PtrArray*)barcodeCoordinates->GetAt(i); | |
125 if (barcodeCoordinate->GetAt(1)) { | 127 if (barcodeCoordinate->GetAt(1)) { |
126 row = row > ((CBC_ResultPoint*)barcodeCoordinate->GetAt(1))->GetY(); | 128 row = row > barcodeCoordinate->GetAt(1)->GetY(); |
127 } | 129 } |
128 if (barcodeCoordinate->GetAt(3)) { | 130 if (barcodeCoordinate->GetAt(3)) { |
129 row = row > ((CBC_ResultPoint*)barcodeCoordinate->GetAt(3))->GetY(); | 131 row = row > barcodeCoordinate->GetAt(3)->GetY(); |
130 } | 132 } |
131 } | 133 } |
132 row += ROW_STEP; | 134 row += ROW_STEP; |
133 delete vertices; | 135 delete vertices; |
134 continue; | 136 continue; |
135 } | 137 } |
136 foundBarcodeInRow = TRUE; | 138 foundBarcodeInRow = TRUE; |
137 barcodeCoordinates->Add(vertices); | 139 barcodeCoordinates->Add(vertices); |
138 if (!multiple) { | 140 if (!multiple) { |
139 break; | 141 break; |
140 } | 142 } |
141 if (vertices->GetAt(2)) { | 143 if (vertices->GetAt(2)) { |
142 column = (int32_t)((CBC_ResultPoint*)vertices->GetAt(2))->GetX(); | 144 column = (int32_t)vertices->GetAt(2)->GetX(); |
143 row = (int32_t)((CBC_ResultPoint*)vertices->GetAt(2))->GetY(); | 145 row = (int32_t)vertices->GetAt(2)->GetY(); |
144 } else { | 146 } else { |
145 column = (int32_t)((CBC_ResultPoint*)vertices->GetAt(4))->GetX(); | 147 column = (int32_t)vertices->GetAt(4)->GetX(); |
146 row = (int32_t)((CBC_ResultPoint*)vertices->GetAt(4))->GetY(); | 148 row = (int32_t)vertices->GetAt(4)->GetY(); |
147 } | 149 } |
148 } | 150 } |
149 return barcodeCoordinates; | 151 return barcodeCoordinates; |
150 } | 152 } |
151 CFX_PtrArray* CBC_Detector::findVertices(CBC_CommonBitMatrix* matrix, | 153 |
152 int32_t startRow, | 154 CBC_ResultPointArray* CBC_Detector::findVertices(CBC_CommonBitMatrix* matrix, |
153 int32_t startColumn) { | 155 int32_t startRow, |
| 156 int32_t startColumn) { |
154 int32_t height = matrix->GetHeight(); | 157 int32_t height = matrix->GetHeight(); |
155 int32_t width = matrix->GetWidth(); | 158 int32_t width = matrix->GetWidth(); |
156 CFX_PtrArray* result = new CFX_PtrArray; | 159 CBC_ResultPointArray* result = new CBC_ResultPointArray; |
157 result->SetSize(8); | 160 result->SetSize(8); |
158 CFX_PtrArray* startptr = findRowsWithPattern( | 161 std::unique_ptr<CBC_ResultPointArray> startptr( |
159 matrix, height, width, startRow, startColumn, START_PATTERN, | 162 findRowsWithPattern(matrix, height, width, startRow, startColumn, |
160 sizeof(START_PATTERN) / sizeof(START_PATTERN[0])); | 163 START_PATTERN, FX_ArraySize(START_PATTERN))); |
161 copyToResult( | 164 copyToResult(result, startptr.get(), INDEXES_START_PATTERN, |
162 result, startptr, INDEXES_START_PATTERN, | 165 FX_ArraySize(INDEXES_START_PATTERN)); |
163 sizeof(INDEXES_START_PATTERN) / sizeof(INDEXES_START_PATTERN[0])); | |
164 startptr->RemoveAll(); | |
165 delete startptr; | |
166 if (result->GetAt(4)) { | 166 if (result->GetAt(4)) { |
167 startColumn = (int32_t)((CBC_ResultPoint*)result->GetAt(4))->GetX(); | 167 startColumn = (int32_t)result->GetAt(4)->GetX(); |
168 startRow = (int32_t)((CBC_ResultPoint*)result->GetAt(4))->GetY(); | 168 startRow = (int32_t)result->GetAt(4)->GetY(); |
169 } | 169 } |
170 CFX_PtrArray* stopptr = findRowsWithPattern( | 170 std::unique_ptr<CBC_ResultPointArray> stopptr( |
171 matrix, height, width, startRow, startColumn, STOP_PATTERN, | 171 findRowsWithPattern(matrix, height, width, startRow, startColumn, |
172 sizeof(STOP_PATTERN) / sizeof(STOP_PATTERN[0])); | 172 STOP_PATTERN, FX_ArraySize(STOP_PATTERN))); |
173 copyToResult(result, stopptr, INDEXES_STOP_PATTERN, | 173 copyToResult(result, stopptr.get(), INDEXES_STOP_PATTERN, |
174 sizeof(INDEXES_STOP_PATTERN) / sizeof(INDEXES_STOP_PATTERN[0])); | 174 FX_ArraySize(INDEXES_STOP_PATTERN)); |
175 stopptr->RemoveAll(); | |
176 delete stopptr; | |
177 return result; | 175 return result; |
178 } | 176 } |
179 void CBC_Detector::copyToResult(CFX_PtrArray* result, | 177 |
180 CFX_PtrArray* tmpResult, | 178 void CBC_Detector::copyToResult(CBC_ResultPointArray* result, |
| 179 CBC_ResultPointArray* tmpResult, |
181 int32_t* destinationIndexes, | 180 int32_t* destinationIndexes, |
182 int32_t destinationLength) { | 181 int32_t destinationLength) { |
183 for (int32_t i = 0; i < destinationLength; i++) { | 182 for (int32_t i = 0; i < destinationLength; i++) { |
184 result->SetAt(destinationIndexes[i], tmpResult->GetAt(i)); | 183 result->SetAt(destinationIndexes[i], tmpResult->GetAt(i)); |
185 } | 184 } |
186 } | 185 } |
187 CFX_PtrArray* CBC_Detector::findRowsWithPattern(CBC_CommonBitMatrix* matrix, | 186 |
188 int32_t height, | 187 CBC_ResultPointArray* CBC_Detector::findRowsWithPattern( |
189 int32_t width, | 188 CBC_CommonBitMatrix* matrix, |
190 int32_t startRow, | 189 int32_t height, |
191 int32_t startColumn, | 190 int32_t width, |
192 int32_t* pattern, | 191 int32_t startRow, |
193 int32_t patternLength) { | 192 int32_t startColumn, |
194 CFX_PtrArray* result = new CFX_PtrArray; | 193 int32_t* pattern, |
| 194 int32_t patternLength) { |
| 195 CBC_ResultPointArray* result = new CBC_ResultPointArray; |
195 result->SetSize(4); | 196 result->SetSize(4); |
196 FX_BOOL found = FALSE; | 197 FX_BOOL found = FALSE; |
197 CFX_Int32Array counters; | 198 CFX_Int32Array counters; |
198 counters.SetSize(patternLength); | 199 counters.SetSize(patternLength); |
199 for (; startRow < height; startRow += ROW_STEP) { | 200 for (; startRow < height; startRow += ROW_STEP) { |
200 CFX_Int32Array* loc = | 201 CFX_Int32Array* loc = |
201 findGuardPattern(matrix, startColumn, startRow, width, FALSE, pattern, | 202 findGuardPattern(matrix, startColumn, startRow, width, FALSE, pattern, |
202 patternLength, counters); | 203 patternLength, counters); |
203 if (loc) { | 204 if (loc) { |
204 while (startRow > 0) { | 205 while (startRow > 0) { |
(...skipping 14 matching lines...) Expand all Loading... |
219 1, new CBC_ResultPoint((FX_FLOAT)loc->GetAt(1), (FX_FLOAT)startRow)); | 220 1, new CBC_ResultPoint((FX_FLOAT)loc->GetAt(1), (FX_FLOAT)startRow)); |
220 found = TRUE; | 221 found = TRUE; |
221 delete loc; | 222 delete loc; |
222 break; | 223 break; |
223 } | 224 } |
224 } | 225 } |
225 int32_t stopRow = startRow + 1; | 226 int32_t stopRow = startRow + 1; |
226 if (found) { | 227 if (found) { |
227 int32_t skippedRowCount = 0; | 228 int32_t skippedRowCount = 0; |
228 CFX_Int32Array previousRowLoc; | 229 CFX_Int32Array previousRowLoc; |
229 previousRowLoc.Add((int32_t)((CBC_ResultPoint*)result->GetAt(0))->GetX()); | 230 previousRowLoc.Add((int32_t)result->GetAt(0)->GetX()); |
230 previousRowLoc.Add((int32_t)((CBC_ResultPoint*)result->GetAt(1))->GetX()); | 231 previousRowLoc.Add((int32_t)result->GetAt(1)->GetX()); |
231 for (; stopRow < height; stopRow++) { | 232 for (; stopRow < height; stopRow++) { |
232 CFX_Int32Array* loc = | 233 CFX_Int32Array* loc = |
233 findGuardPattern(matrix, previousRowLoc[0], stopRow, width, FALSE, | 234 findGuardPattern(matrix, previousRowLoc[0], stopRow, width, FALSE, |
234 pattern, patternLength, counters); | 235 pattern, patternLength, counters); |
235 if (loc && abs(previousRowLoc[0] - loc->GetAt(0)) < MAX_PATTERN_DRIFT && | 236 if (loc && abs(previousRowLoc[0] - loc->GetAt(0)) < MAX_PATTERN_DRIFT && |
236 abs(previousRowLoc[1] - loc->GetAt(1)) < MAX_PATTERN_DRIFT) { | 237 abs(previousRowLoc[1] - loc->GetAt(1)) < MAX_PATTERN_DRIFT) { |
237 previousRowLoc.Copy(*loc); | 238 previousRowLoc.Copy(*loc); |
238 skippedRowCount = 0; | 239 skippedRowCount = 0; |
239 } else { | 240 } else { |
240 if (skippedRowCount > SKIPPED_ROW_COUNT_MAX) { | 241 if (skippedRowCount > SKIPPED_ROW_COUNT_MAX) { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 int32_t scaledPattern = pattern[x] * unitBarWidth; | 340 int32_t scaledPattern = pattern[x] * unitBarWidth; |
340 int32_t variance = counter > scaledPattern ? counter - scaledPattern | 341 int32_t variance = counter > scaledPattern ? counter - scaledPattern |
341 : scaledPattern - counter; | 342 : scaledPattern - counter; |
342 if (variance > maxIndividualVariance) { | 343 if (variance > maxIndividualVariance) { |
343 return INTEGER_MAX; | 344 return INTEGER_MAX; |
344 } | 345 } |
345 totalVariance += variance; | 346 totalVariance += variance; |
346 } | 347 } |
347 return totalVariance / total; | 348 return totalVariance / total; |
348 } | 349 } |
OLD | NEW |