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_ResultPoint.h" | |
25 #include "include/BC_PDF417BarcodeMetadata.h" | |
26 #include "include/BC_PDF417BoundingBox.h" | |
27 #include "include/BC_PDF417Codeword.h" | |
28 #include "include/BC_PDF417BarcodeValue.h" | |
29 #include "include/BC_PDF417Common.h" | |
30 #include "include/BC_PDF417DetectionResultColumn.h" | |
31 #include "include/BC_PDF417DetectionResultRowIndicatorColumn.h" | |
32 CBC_DetectionResultRowIndicatorColumn::CBC_DetectionResultRowIndicatorColumn(CBC
_BoundingBox* boundingBox, FX_BOOL isLeft) | |
33 : CBC_DetectionResultColumn(boundingBox) | |
34 { | |
35 m_isLeft = isLeft; | |
36 } | |
37 CBC_DetectionResultRowIndicatorColumn::~CBC_DetectionResultRowIndicatorColumn() | |
38 { | |
39 } | |
40 void CBC_DetectionResultRowIndicatorColumn::setRowNumbers() | |
41 { | |
42 for (FX_INT32 i = 0; i < m_codewords->GetSize(); i++) { | |
43 CBC_Codeword * codeword = (CBC_Codeword*)m_codewords->GetAt(i); | |
44 if (codeword != NULL) { | |
45 codeword->setRowNumberAsRowIndicatorColumn(); | |
46 } | |
47 } | |
48 } | |
49 FX_INT32 CBC_DetectionResultRowIndicatorColumn::adjustCompleteIndicatorColumnRow
Numbers(CBC_BarcodeMetadata barcodeMetadata) | |
50 { | |
51 CFX_PtrArray* codewords = getCodewords(); | |
52 setRowNumbers(); | |
53 removeIncorrectCodewords(codewords, barcodeMetadata); | |
54 CBC_BoundingBox* boundingBox = getBoundingBox(); | |
55 CBC_ResultPoint* top = m_isLeft ? boundingBox->getTopLeft() : boundingBox->g
etTopRight(); | |
56 CBC_ResultPoint* bottom = m_isLeft ? boundingBox->getBottomLeft() : bounding
Box->getBottomRight(); | |
57 FX_INT32 firstRow = imageRowToCodewordIndex((FX_INT32) top->GetY()); | |
58 FX_INT32 lastRow = imageRowToCodewordIndex((FX_INT32) bottom->GetY()); | |
59 FX_FLOAT averageRowHeight = (lastRow - firstRow) / (FX_FLOAT) barcodeMetadat
a.getRowCount(); | |
60 FX_INT32 barcodeRow = -1; | |
61 FX_INT32 maxRowHeight = 1; | |
62 FX_INT32 currentRowHeight = 0; | |
63 for (FX_INT32 codewordsRow = firstRow; codewordsRow < lastRow; codewordsRow+
+) { | |
64 if (codewords->GetAt(codewordsRow) == NULL) { | |
65 continue; | |
66 } | |
67 CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(codewordsRow); | |
68 FX_INT32 rowDifference = codeword->getRowNumber() - barcodeRow; | |
69 if (rowDifference == 0) { | |
70 currentRowHeight++; | |
71 } else if (rowDifference == 1) { | |
72 maxRowHeight = maxRowHeight > currentRowHeight ? maxRowHeight : curr
entRowHeight; | |
73 currentRowHeight = 1; | |
74 barcodeRow = codeword->getRowNumber(); | |
75 } else if (rowDifference < 0) { | |
76 codewords->SetAt(codewordsRow, NULL); | |
77 } else if (codeword->getRowNumber() >= barcodeMetadata.getRowCount()) { | |
78 codewords->SetAt(codewordsRow, NULL); | |
79 } else if (rowDifference > codewordsRow) { | |
80 codewords->SetAt(codewordsRow, NULL); | |
81 } else { | |
82 FX_INT32 checkedRows; | |
83 if (maxRowHeight > 2) { | |
84 checkedRows = (maxRowHeight - 2) * rowDifference; | |
85 } else { | |
86 checkedRows = rowDifference; | |
87 } | |
88 FX_BOOL closePreviousCodewordFound = checkedRows >= codewordsRow; | |
89 for (FX_INT32 i = 1; i <= checkedRows && !closePreviousCodewordFound
; i++) { | |
90 closePreviousCodewordFound = codewords->GetAt(codewordsRow - i)
!= NULL; | |
91 } | |
92 if (closePreviousCodewordFound) { | |
93 codewords->SetAt(codewordsRow, NULL); | |
94 } else { | |
95 barcodeRow = codeword->getRowNumber(); | |
96 currentRowHeight = 1; | |
97 } | |
98 } | |
99 } | |
100 return (FX_INT32) (averageRowHeight + 0.5); | |
101 } | |
102 CFX_Int32Array* CBC_DetectionResultRowIndicatorColumn::getRowHeights(FX_INT32 &e
) | |
103 { | |
104 CBC_BarcodeMetadata* barcodeMetadata = getBarcodeMetadata(); | |
105 if (barcodeMetadata == NULL) { | |
106 e = BCExceptionCannotMetadata; | |
107 return NULL; | |
108 } | |
109 adjustIncompleteIndicatorColumnRowNumbers(*barcodeMetadata); | |
110 CFX_Int32Array* result = FX_NEW CFX_Int32Array; | |
111 result->SetSize(barcodeMetadata->getRowCount()); | |
112 for (FX_INT32 i = 0; i < getCodewords()->GetSize(); i++) { | |
113 CBC_Codeword* codeword = (CBC_Codeword*)getCodewords()->GetAt(i); | |
114 if (codeword != NULL) { | |
115 result->SetAt(codeword->getRowNumber(), result->GetAt(codeword->get
RowNumber()) + 1); | |
116 } | |
117 } | |
118 return result; | |
119 } | |
120 FX_INT32 CBC_DetectionResultRowIndicatorColumn::adjustIncompleteIndicatorColumnR
owNumbers(CBC_BarcodeMetadata barcodeMetadata) | |
121 { | |
122 CBC_BoundingBox* boundingBox = getBoundingBox(); | |
123 CBC_ResultPoint* top = m_isLeft ? boundingBox->getTopLeft() : boundingBox->g
etTopRight(); | |
124 CBC_ResultPoint* bottom = m_isLeft ? boundingBox->getBottomLeft() : bounding
Box->getBottomRight(); | |
125 FX_INT32 firstRow = imageRowToCodewordIndex((FX_INT32) top->GetY()); | |
126 FX_INT32 lastRow = imageRowToCodewordIndex((FX_INT32) bottom->GetY()); | |
127 FX_FLOAT averageRowHeight = (lastRow - firstRow) / (FX_FLOAT) barcodeMetadat
a.getRowCount(); | |
128 CFX_PtrArray* codewords = getCodewords(); | |
129 FX_INT32 barcodeRow = -1; | |
130 FX_INT32 maxRowHeight = 1; | |
131 FX_INT32 currentRowHeight = 0; | |
132 for (FX_INT32 codewordsRow = firstRow; codewordsRow < lastRow; codewordsRow+
+) { | |
133 if (codewords->GetAt(codewordsRow) == NULL) { | |
134 continue; | |
135 } | |
136 CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(codewordsRow); | |
137 codeword->setRowNumberAsRowIndicatorColumn(); | |
138 FX_INT32 rowDifference = codeword->getRowNumber() - barcodeRow; | |
139 if (rowDifference == 0) { | |
140 currentRowHeight++; | |
141 } else if (rowDifference == 1) { | |
142 maxRowHeight = maxRowHeight > currentRowHeight ? maxRowHeight : curr
entRowHeight; | |
143 currentRowHeight = 1; | |
144 barcodeRow = codeword->getRowNumber(); | |
145 } else if (codeword->getRowNumber() >= barcodeMetadata.getRowCount()) { | |
146 codewords->SetAt(codewordsRow, NULL); | |
147 } else { | |
148 barcodeRow = codeword->getRowNumber(); | |
149 currentRowHeight = 1; | |
150 } | |
151 } | |
152 return (FX_INT32) (averageRowHeight + 0.5); | |
153 } | |
154 CBC_BarcodeMetadata* CBC_DetectionResultRowIndicatorColumn::getBarcodeMetadata() | |
155 { | |
156 CFX_PtrArray* codewords = getCodewords(); | |
157 CBC_BarcodeValue barcodeColumnCount; | |
158 CBC_BarcodeValue barcodeRowCountUpperPart; | |
159 CBC_BarcodeValue barcodeRowCountLowerPart; | |
160 CBC_BarcodeValue barcodeECLevel; | |
161 for (FX_INT32 i = 0; i < codewords->GetSize(); i++) { | |
162 CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(i); | |
163 if (codeword == NULL) { | |
164 continue; | |
165 } | |
166 codeword->setRowNumberAsRowIndicatorColumn(); | |
167 FX_INT32 rowIndicatorValue = codeword->getValue() % 30; | |
168 FX_INT32 codewordRowNumber = codeword->getRowNumber(); | |
169 if (!m_isLeft) { | |
170 codewordRowNumber += 2; | |
171 } | |
172 switch (codewordRowNumber % 3) { | |
173 case 0: | |
174 barcodeRowCountUpperPart.setValue(rowIndicatorValue * 3 + 1); | |
175 break; | |
176 case 1: | |
177 barcodeECLevel.setValue(rowIndicatorValue / 3); | |
178 barcodeRowCountLowerPart.setValue(rowIndicatorValue % 3); | |
179 break; | |
180 case 2: | |
181 barcodeColumnCount.setValue(rowIndicatorValue + 1); | |
182 break; | |
183 } | |
184 } | |
185 if ((barcodeColumnCount.getValue()->GetSize() == 0) || | |
186 (barcodeRowCountUpperPart.getValue()->GetSize() == 0) || | |
187 (barcodeRowCountLowerPart.getValue()->GetSize() == 0) || | |
188 (barcodeECLevel.getValue()->GetSize() == 0) || | |
189 barcodeColumnCount.getValue()->GetAt(0) < 1 || | |
190 barcodeRowCountUpperPart.getValue()->GetAt(0) + barcodeRowCountLower
Part.getValue()->GetAt(0) < CBC_PDF417Common::MIN_ROWS_IN_BARCODE || | |
191 barcodeRowCountUpperPart.getValue()->GetAt(0) + barcodeRowCountLower
Part.getValue()->GetAt(0) > CBC_PDF417Common::MAX_ROWS_IN_BARCODE) { | |
192 return NULL; | |
193 } | |
194 CBC_BarcodeMetadata* barcodeMetadata = FX_NEW CBC_BarcodeMetadata(barcodeCol
umnCount.getValue()->GetAt(0), barcodeRowCountUpperPart.getValue()->GetAt(0), ba
rcodeRowCountLowerPart.getValue()->GetAt(0), barcodeECLevel.getValue()->GetAt(0)
); | |
195 removeIncorrectCodewords(codewords, *barcodeMetadata); | |
196 return barcodeMetadata; | |
197 } | |
198 FX_BOOL CBC_DetectionResultRowIndicatorColumn::isLeft() | |
199 { | |
200 return m_isLeft; | |
201 } | |
202 CFX_ByteString CBC_DetectionResultRowIndicatorColumn::toString() | |
203 { | |
204 return (CFX_ByteString)"IsLeft: " + (CFX_ByteString)m_isLeft + '\n' + CBC_De
tectionResultColumn::toString(); | |
205 } | |
206 void CBC_DetectionResultRowIndicatorColumn::removeIncorrectCodewords(CFX_PtrArra
y* codewords, CBC_BarcodeMetadata barcodeMetadata) | |
207 { | |
208 for (FX_INT32 codewordRow = 0; codewordRow < codewords->GetSize(); codewordR
ow++) { | |
209 CBC_Codeword* codeword = (CBC_Codeword*)codewords->GetAt(codewordRow); | |
210 if (codeword == NULL) { | |
211 continue; | |
212 } | |
213 FX_INT32 rowIndicatorValue = codeword->getValue() % 30; | |
214 FX_INT32 codewordRowNumber = codeword->getRowNumber(); | |
215 if (codewordRowNumber > barcodeMetadata.getRowCount()) { | |
216 codewords->SetAt(codewordRow, NULL); | |
217 continue; | |
218 } | |
219 if (!m_isLeft) { | |
220 codewordRowNumber += 2; | |
221 } | |
222 switch (codewordRowNumber % 3) { | |
223 case 0: | |
224 if (rowIndicatorValue * 3 + 1 != barcodeMetadata.getRowCountUppe
rPart()) { | |
225 codewords->SetAt(codewordRow, NULL); | |
226 } | |
227 break; | |
228 case 1: | |
229 if (rowIndicatorValue / 3 != barcodeMetadata.getErrorCorrectionL
evel() || | |
230 rowIndicatorValue % 3 != barcodeMetadata.getRowCountLowe
rPart()) { | |
231 codewords->SetAt(codewordRow, NULL); | |
232 } | |
233 break; | |
234 case 2: | |
235 if (rowIndicatorValue + 1 != barcodeMetadata.getColumnCount()) { | |
236 codewords->SetAt(codewordRow, NULL); | |
237 } | |
238 break; | |
239 } | |
240 } | |
241 } | |
OLD | NEW |