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 2013 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 "barcode.h" | |
24 #include "include/BC_PDF417Codeword.h" | |
25 #include "include/BC_PDF417BarcodeMetadata.h" | |
26 #include "include/BC_PDF417BoundingBox.h" | |
27 #include "include/BC_PDF417DetectionResultColumn.h" | |
28 #include "include/BC_PDF417Common.h" | |
29 #include "include/BC_PDF417DetectionResultRowIndicatorColumn.h" | |
30 #include "include/BC_PDF417DetectionResult.h" | |
31 FX_INT32 CBC_DetectionResult::ADJUST_ROW_NUMBER_SKIP = 2; | |
32 CBC_DetectionResult::CBC_DetectionResult(CBC_BarcodeMetadata* barcodeMetadata, C
BC_BoundingBox* boundingBox) | |
33 { | |
34 m_barcodeMetadata = barcodeMetadata; | |
35 m_barcodeColumnCount = barcodeMetadata->getColumnCount(); | |
36 m_boundingBox = boundingBox; | |
37 m_detectionResultColumns.SetSize(m_barcodeColumnCount + 2); | |
38 for (FX_INT32 i = 0; i < m_barcodeColumnCount + 2; i++) { | |
39 m_detectionResultColumns[i] = NULL; | |
40 } | |
41 } | |
42 CBC_DetectionResult::~CBC_DetectionResult() | |
43 { | |
44 delete m_boundingBox; | |
45 delete m_barcodeMetadata; | |
46 m_detectionResultColumns.RemoveAll(); | |
47 } | |
48 CFX_PtrArray& CBC_DetectionResult::getDetectionResultColumns() | |
49 { | |
50 adjustIndicatorColumnRowNumbers((CBC_DetectionResultColumn*)m_detectionResul
tColumns.GetAt(0)); | |
51 adjustIndicatorColumnRowNumbers((CBC_DetectionResultColumn*)m_detectionResul
tColumns.GetAt(m_barcodeColumnCount + 1)); | |
52 FX_INT32 unadjustedCodewordCount = CBC_PDF417Common::MAX_CODEWORDS_IN_BARCOD
E; | |
53 FX_INT32 previousUnadjustedCount; | |
54 do { | |
55 previousUnadjustedCount = unadjustedCodewordCount; | |
56 unadjustedCodewordCount = adjustRowNumbers(); | |
57 } while (unadjustedCodewordCount > 0 && unadjustedCodewordCount < previousUn
adjustedCount); | |
58 return m_detectionResultColumns; | |
59 } | |
60 void CBC_DetectionResult::setBoundingBox(CBC_BoundingBox* boundingBox) | |
61 { | |
62 m_boundingBox = boundingBox; | |
63 } | |
64 CBC_BoundingBox* CBC_DetectionResult::getBoundingBox() | |
65 { | |
66 return m_boundingBox; | |
67 } | |
68 void CBC_DetectionResult::setDetectionResultColumn(FX_INT32 barcodeColumn, CBC_D
etectionResultColumn* detectionResultColumn) | |
69 { | |
70 m_detectionResultColumns[barcodeColumn] = detectionResultColumn; | |
71 } | |
72 CBC_DetectionResultColumn* CBC_DetectionResult::getDetectionResultColumn(FX_INT3
2 barcodeColumn) | |
73 { | |
74 return (CBC_DetectionResultColumn*)m_detectionResultColumns[barcodeColumn]; | |
75 } | |
76 CFX_ByteString CBC_DetectionResult::toString() | |
77 { | |
78 CBC_DetectionResultColumn* rowIndicatorColumn = (CBC_DetectionResultColumn*)
m_detectionResultColumns[0]; | |
79 if (rowIndicatorColumn == NULL) { | |
80 rowIndicatorColumn = (CBC_DetectionResultColumn*)m_detectionResultColumn
s[m_barcodeColumnCount + 1]; | |
81 } | |
82 CFX_ByteString result; | |
83 for (FX_INT32 codewordsRow = 0; codewordsRow < rowIndicatorColumn->getCodewo
rds()->GetSize(); codewordsRow++) { | |
84 result += (FX_CHAR) codewordsRow; | |
85 for (FX_INT32 barcodeColumn = 0; barcodeColumn < m_barcodeColumnCount +
2; barcodeColumn++) { | |
86 if (m_detectionResultColumns[barcodeColumn] == NULL) { | |
87 result += " | "; | |
88 continue; | |
89 } | |
90 CBC_Codeword* codeword = (CBC_Codeword*)((CBC_DetectionResultColumn*
)m_detectionResultColumns[barcodeColumn])->getCodewords()->GetAt(codewordsRow); | |
91 if (codeword == NULL) { | |
92 result += " | "; | |
93 continue; | |
94 } | |
95 result += codeword->getRowNumber(); | |
96 result += codeword->getValue(); | |
97 } | |
98 } | |
99 return result; | |
100 } | |
101 void CBC_DetectionResult::adjustIndicatorColumnRowNumbers(CBC_DetectionResultCol
umn* detectionResultColumn) | |
102 { | |
103 if (detectionResultColumn != NULL) { | |
104 ((CBC_DetectionResultRowIndicatorColumn*)detectionResultColumn)->adjustC
ompleteIndicatorColumnRowNumbers(*m_barcodeMetadata); | |
105 } | |
106 } | |
107 FX_INT32 CBC_DetectionResult::adjustRowNumbers() | |
108 { | |
109 FX_INT32 unadjustedCount = adjustRowNumbersByRow(); | |
110 if (unadjustedCount == 0) { | |
111 return 0; | |
112 } | |
113 for (FX_INT32 barcodeColumn = 1; barcodeColumn < m_barcodeColumnCount + 1; b
arcodeColumn++) { | |
114 CFX_PtrArray* codewords = ((CBC_DetectionResultColumn*)m_detectionResult
Columns[barcodeColumn])->getCodewords(); | |
115 for (FX_INT32 codewordsRow = 0; codewordsRow < codewords->GetSize(); cod
ewordsRow++) { | |
116 if (codewords->GetAt(codewordsRow) == NULL) { | |
117 continue; | |
118 } | |
119 if (!((CBC_Codeword*)codewords->GetAt(codewordsRow))->hasValidRowNum
ber()) { | |
120 adjustRowNumbers(barcodeColumn, codewordsRow, codewords); | |
121 } | |
122 } | |
123 } | |
124 return unadjustedCount; | |
125 } | |
126 FX_INT32 CBC_DetectionResult::adjustRowNumbersByRow() | |
127 { | |
128 adjustRowNumbersFromBothRI(); | |
129 FX_INT32 unadjustedCount = adjustRowNumbersFromLRI(); | |
130 return unadjustedCount + adjustRowNumbersFromRRI(); | |
131 } | |
132 FX_INT32 CBC_DetectionResult::adjustRowNumbersFromBothRI() | |
133 { | |
134 if (m_detectionResultColumns[0] == NULL || m_detectionResultColumns[m_barcod
eColumnCount + 1] == NULL) { | |
135 return 0; | |
136 } | |
137 CFX_PtrArray* LRIcodewords = ((CBC_DetectionResultColumn*)m_detectionResultC
olumns[0])->getCodewords(); | |
138 CFX_PtrArray* RRIcodewords = ((CBC_DetectionResultColumn*)m_detectionResultC
olumns[m_barcodeColumnCount + 1])->getCodewords(); | |
139 for (FX_INT32 codewordsRow = 0; codewordsRow < LRIcodewords->GetSize(); code
wordsRow++) { | |
140 if (LRIcodewords->GetAt(codewordsRow) != NULL && | |
141 RRIcodewords->GetAt(codewordsRow) != NULL && | |
142 ((CBC_Codeword*)LRIcodewords->GetAt(codewordsRow))->getRowNumber
() == ((CBC_Codeword*)RRIcodewords->GetAt(codewordsRow))->getRowNumber()) { | |
143 for (FX_INT32 barcodeColumn = 1; barcodeColumn <= m_barcodeColumnCou
nt; barcodeColumn++) { | |
144 CBC_Codeword* codeword = (CBC_Codeword*)((CBC_DetectionResultCol
umn*)m_detectionResultColumns[barcodeColumn])->getCodewords()->GetAt(codewordsRo
w); | |
145 if (codeword == NULL) { | |
146 continue; | |
147 } | |
148 codeword->setRowNumber(((CBC_Codeword*)LRIcodewords->GetAt(codew
ordsRow))->getRowNumber()); | |
149 if (!codeword->hasValidRowNumber()) { | |
150 ((CBC_DetectionResultColumn*)m_detectionResultColumns[barcod
eColumn])->getCodewords()->SetAt(codewordsRow, NULL); | |
151 } | |
152 } | |
153 } | |
154 } | |
155 return 0; | |
156 } | |
157 FX_INT32 CBC_DetectionResult::adjustRowNumbersFromRRI() | |
158 { | |
159 if (m_detectionResultColumns[m_barcodeColumnCount + 1] == NULL) { | |
160 return 0; | |
161 } | |
162 FX_INT32 unadjustedCount = 0; | |
163 CFX_PtrArray* codewords = ((CBC_DetectionResultColumn*) m_detectionResultCol
umns.GetAt(m_barcodeColumnCount + 1))->getCodewords(); | |
164 for (FX_INT32 codewordsRow = 0; codewordsRow < codewords->GetSize(); codewor
dsRow++) { | |
165 if (codewords->GetAt(codewordsRow) == NULL) { | |
166 continue; | |
167 } | |
168 FX_INT32 rowIndicatorRowNumber = ((CBC_Codeword*)codewords->GetAt(codewo
rdsRow))->getRowNumber(); | |
169 FX_INT32 invalidRowCounts = 0; | |
170 for (FX_INT32 barcodeColumn = m_barcodeColumnCount + 1; barcodeColumn >
0 && invalidRowCounts < ADJUST_ROW_NUMBER_SKIP; barcodeColumn--) { | |
171 CBC_Codeword* codeword = (CBC_Codeword*)((CBC_DetectionResultColumn*
)m_detectionResultColumns.GetAt(barcodeColumn))->getCodewords()->GetAt(codewords
Row); | |
172 if (codeword != NULL) { | |
173 invalidRowCounts = adjustRowNumberIfValid(rowIndicatorRowNumber,
invalidRowCounts, codeword); | |
174 if (!codeword->hasValidRowNumber()) { | |
175 unadjustedCount++; | |
176 } | |
177 } | |
178 } | |
179 } | |
180 return unadjustedCount; | |
181 } | |
182 FX_INT32 CBC_DetectionResult::adjustRowNumbersFromLRI() | |
183 { | |
184 if (m_detectionResultColumns[0] == NULL) { | |
185 return 0; | |
186 } | |
187 FX_INT32 unadjustedCount = 0; | |
188 CFX_PtrArray* codewords = ((CBC_DetectionResultColumn*)m_detectionResultColu
mns.GetAt(0))->getCodewords(); | |
189 for (FX_INT32 codewordsRow = 0; codewordsRow < codewords->GetSize(); codewor
dsRow++) { | |
190 if (codewords->GetAt(codewordsRow) == NULL) { | |
191 continue; | |
192 } | |
193 FX_INT32 rowIndicatorRowNumber = ((CBC_Codeword*)codewords->GetAt(codewo
rdsRow))->getRowNumber(); | |
194 FX_INT32 invalidRowCounts = 0; | |
195 for (FX_INT32 barcodeColumn = 1; barcodeColumn < m_barcodeColumnCount +
1 && invalidRowCounts < ADJUST_ROW_NUMBER_SKIP; barcodeColumn++) { | |
196 CBC_Codeword* codeword = (CBC_Codeword*)((CBC_DetectionResultColumn*
)m_detectionResultColumns[barcodeColumn])->getCodewords()->GetAt(codewordsRow); | |
197 if (codeword != NULL) { | |
198 invalidRowCounts = adjustRowNumberIfValid(rowIndicatorRowNumber,
invalidRowCounts, codeword); | |
199 if (!codeword->hasValidRowNumber()) { | |
200 unadjustedCount++; | |
201 } | |
202 } | |
203 } | |
204 } | |
205 return unadjustedCount; | |
206 } | |
207 FX_INT32 CBC_DetectionResult::adjustRowNumberIfValid(FX_INT32 rowIndicatorRowNum
ber, FX_INT32 invalidRowCounts, CBC_Codeword* codeword) | |
208 { | |
209 if (codeword == NULL) { | |
210 return invalidRowCounts; | |
211 } | |
212 if (!codeword->hasValidRowNumber()) { | |
213 if (codeword->isValidRowNumber(rowIndicatorRowNumber)) { | |
214 codeword->setRowNumber(rowIndicatorRowNumber); | |
215 invalidRowCounts = 0; | |
216 } else { | |
217 ++invalidRowCounts; | |
218 } | |
219 } | |
220 return invalidRowCounts; | |
221 } | |
222 void CBC_DetectionResult::adjustRowNumbers(FX_INT32 barcodeColumn, FX_INT32 code
wordsRow, CFX_PtrArray* codewords) | |
223 { | |
224 CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(codewordsRow); | |
225 CFX_PtrArray* previousColumnCodewords = ((CBC_DetectionResultColumn*)m_detec
tionResultColumns.GetAt(barcodeColumn - 1))->getCodewords(); | |
226 CFX_PtrArray* nextColumnCodewords = previousColumnCodewords; | |
227 if (m_detectionResultColumns[barcodeColumn + 1] != NULL) { | |
228 nextColumnCodewords = ((CBC_DetectionResultColumn*)m_detectionResultColu
mns[barcodeColumn + 1])->getCodewords(); | |
229 } | |
230 CFX_PtrArray otherCodewords; | |
231 otherCodewords.SetSize(14); | |
232 otherCodewords[2] = previousColumnCodewords->GetAt(codewordsRow); | |
233 otherCodewords[3] = nextColumnCodewords->GetAt(codewordsRow); | |
234 if (codewordsRow > 0) { | |
235 otherCodewords[0] = codewords->GetAt(codewordsRow - 1); | |
236 otherCodewords[4] = previousColumnCodewords->GetAt(codewordsRow - 1); | |
237 otherCodewords[5] = nextColumnCodewords->GetAt(codewordsRow - 1); | |
238 } | |
239 if (codewordsRow > 1) { | |
240 otherCodewords[8] = codewords->GetAt(codewordsRow - 2); | |
241 otherCodewords[10] = previousColumnCodewords->GetAt(codewordsRow - 2); | |
242 otherCodewords[11] = nextColumnCodewords->GetAt(codewordsRow - 2); | |
243 } | |
244 if (codewordsRow < codewords->GetSize() - 1) { | |
245 otherCodewords[1] = codewords->GetAt(codewordsRow + 1); | |
246 otherCodewords[6] = previousColumnCodewords->GetAt(codewordsRow + 1); | |
247 otherCodewords[7] = nextColumnCodewords->GetAt(codewordsRow + 1); | |
248 } | |
249 if (codewordsRow < codewords->GetSize() - 2) { | |
250 otherCodewords[9] = codewords->GetAt(codewordsRow + 2); | |
251 otherCodewords[12] = previousColumnCodewords->GetAt(codewordsRow + 2); | |
252 otherCodewords[13] = nextColumnCodewords->GetAt(codewordsRow + 2); | |
253 } | |
254 for (FX_INT32 i = 0; i < otherCodewords.GetSize(); i++) { | |
255 CBC_Codeword* otherCodeword = (CBC_Codeword*)otherCodewords.GetAt(i); | |
256 if (adjustRowNumber(codeword, otherCodeword)) { | |
257 return; | |
258 } | |
259 } | |
260 } | |
261 FX_BOOL CBC_DetectionResult::adjustRowNumber(CBC_Codeword* codeword, CBC_Codewor
d* otherCodeword) | |
262 { | |
263 if (otherCodeword == NULL) { | |
264 return FALSE; | |
265 } | |
266 if (otherCodeword->hasValidRowNumber() && otherCodeword->getBucket() == code
word->getBucket()) { | |
267 codeword->setRowNumber(otherCodeword->getRowNumber()); | |
268 return TRUE; | |
269 } | |
270 return FALSE; | |
271 } | |
272 FX_INT32 CBC_DetectionResult::getBarcodeColumnCount() | |
273 { | |
274 return m_barcodeColumnCount; | |
275 } | |
276 FX_INT32 CBC_DetectionResult::getBarcodeRowCount() | |
277 { | |
278 return m_barcodeMetadata->getRowCount(); | |
279 } | |
280 FX_INT32 CBC_DetectionResult::getBarcodeECLevel() | |
281 { | |
282 return m_barcodeMetadata->getErrorCorrectionLevel(); | |
283 } | |
OLD | NEW |