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 2008 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/oned/BC_OneDimReader.h" | |
24 | |
25 #include <memory> | |
26 | |
27 #include "xfa/src/fxbarcode/BC_Reader.h" | |
28 #include "xfa/src/fxbarcode/common/BC_CommonBitArray.h" | |
29 #include "xfa/src/fxbarcode/oned/BC_OneDReader.h" | |
30 #include "xfa/src/fxbarcode/utils.h" | |
31 | |
32 const int32_t CBC_OneDimReader::START_END_PATTERN[3] = {1, 1, 1}; | |
33 const int32_t CBC_OneDimReader::MIDDLE_PATTERN[5] = {1, 1, 1, 1, 1}; | |
34 const int32_t CBC_OneDimReader::L_PATTERNS[10][4] = { | |
35 {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2}, | |
36 {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}}; | |
37 const int32_t CBC_OneDimReader::L_AND_G_PATTERNS[20][4] = { | |
38 {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2}, | |
39 {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}, | |
40 {1, 1, 2, 3}, {1, 2, 2, 2}, {2, 2, 1, 2}, {1, 1, 4, 1}, {2, 3, 1, 1}, | |
41 {1, 3, 2, 1}, {4, 1, 1, 1}, {2, 1, 3, 1}, {3, 1, 2, 1}, {2, 1, 1, 3}}; | |
42 | |
43 CBC_OneDimReader::CBC_OneDimReader() {} | |
44 CBC_OneDimReader::~CBC_OneDimReader() {} | |
45 CFX_Int32Array* CBC_OneDimReader::FindStartGuardPattern(CBC_CommonBitArray* row, | |
46 int32_t& e) { | |
47 FX_BOOL foundStart = FALSE; | |
48 CFX_Int32Array* startRange = NULL; | |
49 CFX_Int32Array startEndPattern; | |
50 startEndPattern.SetSize(3); | |
51 startEndPattern[0] = START_END_PATTERN[0]; | |
52 startEndPattern[1] = START_END_PATTERN[1]; | |
53 startEndPattern[2] = START_END_PATTERN[2]; | |
54 int32_t nextStart = 0; | |
55 while (!foundStart) { | |
56 delete startRange; | |
57 startRange = FindGuardPattern(row, nextStart, FALSE, &startEndPattern, e); | |
58 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
59 int32_t start = (*startRange)[0]; | |
60 nextStart = (*startRange)[1]; | |
61 if (start <= 1) { | |
62 break; | |
63 } | |
64 int32_t quietStart = start - (nextStart - start); | |
65 if (quietStart >= 0) { | |
66 FX_BOOL booT = row->IsRange(quietStart, start, FALSE, e); | |
67 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
68 foundStart = booT; | |
69 } | |
70 } | |
71 return startRange; | |
72 } | |
73 CFX_ByteString CBC_OneDimReader::DecodeRow(int32_t rowNumber, | |
74 CBC_CommonBitArray* row, | |
75 int32_t hints, | |
76 int32_t& e) { | |
77 std::unique_ptr<CFX_Int32Array> result(FindStartGuardPattern(row, e)); | |
78 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
79 CFX_ByteString temp = DecodeRow(rowNumber, row, result.get(), hints, e); | |
80 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
81 return temp; | |
82 } | |
83 CFX_ByteString CBC_OneDimReader::DecodeRow(int32_t rowNumber, | |
84 CBC_CommonBitArray* row, | |
85 CFX_Int32Array* startGuardRange, | |
86 int32_t hints, | |
87 int32_t& e) { | |
88 CFX_ByteString result; | |
89 DecodeMiddle(row, startGuardRange, result, e); | |
90 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
91 FX_BOOL b = CheckChecksum(result, e); | |
92 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
93 if (!b) { | |
94 e = BCExceptionChecksumException; | |
95 return ""; | |
96 } | |
97 return result; | |
98 } | |
99 FX_BOOL CBC_OneDimReader::CheckChecksum(CFX_ByteString& s, int32_t& e) { | |
100 FX_BOOL temp = CheckStandardUPCEANChecksum(s, e); | |
101 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); | |
102 return temp; | |
103 } | |
104 FX_BOOL CBC_OneDimReader::CheckStandardUPCEANChecksum(CFX_ByteString& s, | |
105 int32_t& e) { | |
106 int32_t length = s.GetLength(); | |
107 if (length == 0) { | |
108 return FALSE; | |
109 } | |
110 int32_t sum = 0; | |
111 for (int32_t i = length - 2; i >= 0; i -= 2) { | |
112 int32_t digit = (int32_t)s[i] - (int32_t)'0'; | |
113 if (digit < 0 || digit > 9) { | |
114 e = BCExceptionFormatException; | |
115 return FALSE; | |
116 } | |
117 sum += digit; | |
118 } | |
119 sum *= 3; | |
120 for (int32_t j = length - 1; j >= 0; j -= 2) { | |
121 int32_t digit = (int32_t)s[j] - (int32_t)'0'; | |
122 if (digit < 0 || digit > 9) { | |
123 e = BCExceptionFormatException; | |
124 return FALSE; | |
125 } | |
126 sum += digit; | |
127 } | |
128 return sum % 10 == 0; | |
129 } | |
130 CFX_Int32Array* CBC_OneDimReader::DecodeEnd(CBC_CommonBitArray* row, | |
131 int32_t endStart, | |
132 int32_t& e) { | |
133 CFX_Int32Array startEndPattern; | |
134 startEndPattern.Add(START_END_PATTERN[0]); | |
135 startEndPattern.Add(START_END_PATTERN[1]); | |
136 startEndPattern.Add(START_END_PATTERN[2]); | |
137 CFX_Int32Array* FindGuard = | |
138 FindGuardPattern(row, endStart, FALSE, &startEndPattern, e); | |
139 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
140 return FindGuard; | |
141 } | |
142 CFX_Int32Array* CBC_OneDimReader::FindGuardPattern(CBC_CommonBitArray* row, | |
143 int32_t rowOffset, | |
144 FX_BOOL whiteFirst, | |
145 CFX_Int32Array* pattern, | |
146 int32_t& e) { | |
147 int32_t patternLength = pattern->GetSize(); | |
148 CFX_Int32Array counters; | |
149 counters.SetSize(patternLength); | |
150 int32_t width = row->GetSize(); | |
151 FX_BOOL isWhite = FALSE; | |
152 while (rowOffset < width) { | |
153 isWhite = !row->Get(rowOffset); | |
154 if (whiteFirst == isWhite) { | |
155 break; | |
156 } | |
157 rowOffset++; | |
158 } | |
159 int32_t counterPosition = 0; | |
160 int32_t patternStart = rowOffset; | |
161 for (int32_t x = rowOffset; x < width; x++) { | |
162 FX_BOOL pixel = row->Get(x); | |
163 if (pixel ^ isWhite) { | |
164 counters[counterPosition]++; | |
165 } else { | |
166 if (counterPosition == patternLength - 1) { | |
167 if (PatternMatchVariance(&counters, &(*pattern)[0], | |
168 MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) { | |
169 CFX_Int32Array* result = new CFX_Int32Array(); | |
170 result->SetSize(2); | |
171 (*result)[0] = patternStart; | |
172 (*result)[1] = x; | |
173 return result; | |
174 } | |
175 patternStart += counters[0] + counters[1]; | |
176 for (int32_t y = 2; y < patternLength; y++) { | |
177 counters[y - 2] = counters[y]; | |
178 } | |
179 counters[patternLength - 2] = 0; | |
180 counters[patternLength - 1] = 0; | |
181 counterPosition--; | |
182 } else { | |
183 counterPosition++; | |
184 } | |
185 counters[counterPosition] = 1; | |
186 isWhite = !isWhite; | |
187 } | |
188 } | |
189 e = BCExceptionNotFound; | |
190 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
191 return NULL; | |
192 } | |
193 int32_t CBC_OneDimReader::DecodeDigit(CBC_CommonBitArray* row, | |
194 CFX_Int32Array* counters, | |
195 int32_t rowOffset, | |
196 const int32_t* patterns, | |
197 int32_t patternLength, | |
198 int32_t& e) { | |
199 RecordPattern(row, rowOffset, counters, e); | |
200 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | |
201 int32_t bestVariance = MAX_AVG_VARIANCE; | |
202 int32_t bestMatch = -1; | |
203 int32_t max = patternLength; | |
204 for (int32_t i = 0; i < max; i++) { | |
205 int32_t variance = PatternMatchVariance(counters, &patterns[i * 4], | |
206 MAX_INDIVIDUAL_VARIANCE); | |
207 if (variance < bestVariance) { | |
208 bestVariance = variance; | |
209 bestMatch = i; | |
210 } | |
211 } | |
212 if (bestMatch >= 0) { | |
213 return bestMatch; | |
214 } else { | |
215 e = BCExceptionNotFound; | |
216 return 0; | |
217 } | |
218 return 0; | |
219 } | |
OLD | NEW |