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 2008 ZXing authors | 8 * Copyright 2008 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 "../barcode.h" | 23 #include "../barcode.h" |
24 #include "../BC_Reader.h" | 24 #include "../BC_Reader.h" |
25 #include "../common/BC_CommonBitArray.h" | 25 #include "../common/BC_CommonBitArray.h" |
26 #include "BC_OneDReader.h" | 26 #include "BC_OneDReader.h" |
27 #include "BC_OnedCode128Reader.h" | 27 #include "BC_OnedCode128Reader.h" |
28 const FX_INT32 CBC_OnedCode128Reader::CODE_PATTERNS[107][7] = { | 28 const int32_t CBC_OnedCode128Reader::CODE_PATTERNS[107][7] = { |
29 {2, 1, 2, 2, 2, 2, 0}, | 29 {2, 1, 2, 2, 2, 2, 0}, |
30 {2, 2, 2, 1, 2, 2, 0}, | 30 {2, 2, 2, 1, 2, 2, 0}, |
31 {2, 2, 2, 2, 2, 1, 0}, | 31 {2, 2, 2, 2, 2, 1, 0}, |
32 {1, 2, 1, 2, 2, 3, 0}, | 32 {1, 2, 1, 2, 2, 3, 0}, |
33 {1, 2, 1, 3, 2, 2, 0}, | 33 {1, 2, 1, 3, 2, 2, 0}, |
34 {1, 3, 1, 2, 2, 2, 0}, | 34 {1, 3, 1, 2, 2, 2, 0}, |
35 {1, 2, 2, 2, 1, 3, 0}, | 35 {1, 2, 2, 2, 1, 3, 0}, |
36 {1, 2, 2, 3, 1, 2, 0}, | 36 {1, 2, 2, 3, 1, 2, 0}, |
37 {1, 3, 2, 2, 1, 2, 0}, | 37 {1, 3, 2, 2, 1, 2, 0}, |
38 {2, 2, 1, 2, 1, 3, 0}, | 38 {2, 2, 1, 2, 1, 3, 0}, |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 {4, 1, 1, 3, 1, 1, 0}, | 127 {4, 1, 1, 3, 1, 1, 0}, |
128 {1, 1, 3, 1, 4, 1, 0}, | 128 {1, 1, 3, 1, 4, 1, 0}, |
129 {1, 1, 4, 1, 3, 1, 0}, | 129 {1, 1, 4, 1, 3, 1, 0}, |
130 {3, 1, 1, 1, 4, 1, 0}, | 130 {3, 1, 1, 1, 4, 1, 0}, |
131 {4, 1, 1, 1, 3, 1, 0}, | 131 {4, 1, 1, 1, 3, 1, 0}, |
132 {2, 1, 1, 4, 1, 2, 0}, | 132 {2, 1, 1, 4, 1, 2, 0}, |
133 {2, 1, 1, 2, 1, 4, 0}, | 133 {2, 1, 1, 2, 1, 4, 0}, |
134 {2, 1, 1, 2, 3, 2, 0}, | 134 {2, 1, 1, 2, 3, 2, 0}, |
135 {2, 3, 3, 1, 1, 1, 2} | 135 {2, 3, 3, 1, 1, 1, 2} |
136 }; | 136 }; |
137 const FX_INT32 CBC_OnedCode128Reader::MAX_AVG_VARIANCE = (FX_INT32) (256 * 0.25f
); | 137 const int32_t CBC_OnedCode128Reader::MAX_AVG_VARIANCE = (int32_t) (256 * 0.25f); |
138 const FX_INT32 CBC_OnedCode128Reader::MAX_INDIVIDUAL_VARIANCE = (FX_INT32) (256
* 0.7f); | 138 const int32_t CBC_OnedCode128Reader::MAX_INDIVIDUAL_VARIANCE = (int32_t) (256 *
0.7f); |
139 const FX_INT32 CBC_OnedCode128Reader::CODE_SHIFT = 98; | 139 const int32_t CBC_OnedCode128Reader::CODE_SHIFT = 98; |
140 const FX_INT32 CBC_OnedCode128Reader::CODE_CODE_C = 99; | 140 const int32_t CBC_OnedCode128Reader::CODE_CODE_C = 99; |
141 const FX_INT32 CBC_OnedCode128Reader::CODE_CODE_B = 100; | 141 const int32_t CBC_OnedCode128Reader::CODE_CODE_B = 100; |
142 const FX_INT32 CBC_OnedCode128Reader::CODE_CODE_A = 101; | 142 const int32_t CBC_OnedCode128Reader::CODE_CODE_A = 101; |
143 const FX_INT32 CBC_OnedCode128Reader::CODE_FNC_1 = 102; | 143 const int32_t CBC_OnedCode128Reader::CODE_FNC_1 = 102; |
144 const FX_INT32 CBC_OnedCode128Reader::CODE_FNC_2 = 97; | 144 const int32_t CBC_OnedCode128Reader::CODE_FNC_2 = 97; |
145 const FX_INT32 CBC_OnedCode128Reader::CODE_FNC_3 = 96; | 145 const int32_t CBC_OnedCode128Reader::CODE_FNC_3 = 96; |
146 const FX_INT32 CBC_OnedCode128Reader::CODE_FNC_4_A = 101; | 146 const int32_t CBC_OnedCode128Reader::CODE_FNC_4_A = 101; |
147 const FX_INT32 CBC_OnedCode128Reader::CODE_FNC_4_B = 100; | 147 const int32_t CBC_OnedCode128Reader::CODE_FNC_4_B = 100; |
148 const FX_INT32 CBC_OnedCode128Reader::CODE_START_A = 103; | 148 const int32_t CBC_OnedCode128Reader::CODE_START_A = 103; |
149 const FX_INT32 CBC_OnedCode128Reader::CODE_START_B = 104; | 149 const int32_t CBC_OnedCode128Reader::CODE_START_B = 104; |
150 const FX_INT32 CBC_OnedCode128Reader::CODE_START_C = 105; | 150 const int32_t CBC_OnedCode128Reader::CODE_START_C = 105; |
151 const FX_INT32 CBC_OnedCode128Reader::CODE_STOP = 106; | 151 const int32_t CBC_OnedCode128Reader::CODE_STOP = 106; |
152 CBC_OnedCode128Reader::CBC_OnedCode128Reader() | 152 CBC_OnedCode128Reader::CBC_OnedCode128Reader() |
153 { | 153 { |
154 } | 154 } |
155 CBC_OnedCode128Reader::~CBC_OnedCode128Reader() | 155 CBC_OnedCode128Reader::~CBC_OnedCode128Reader() |
156 { | 156 { |
157 } | 157 } |
158 CFX_Int32Array *CBC_OnedCode128Reader::FindStartPattern(CBC_CommonBitArray *row,
FX_INT32 &e) | 158 CFX_Int32Array *CBC_OnedCode128Reader::FindStartPattern(CBC_CommonBitArray *row,
int32_t &e) |
159 { | 159 { |
160 FX_INT32 width = row->GetSize(); | 160 int32_t width = row->GetSize(); |
161 FX_INT32 rowOffset = 0; | 161 int32_t rowOffset = 0; |
162 while (rowOffset < width) { | 162 while (rowOffset < width) { |
163 if (row->Get(rowOffset)) { | 163 if (row->Get(rowOffset)) { |
164 break; | 164 break; |
165 } | 165 } |
166 rowOffset++; | 166 rowOffset++; |
167 } | 167 } |
168 FX_INT32 counterPosition = 0; | 168 int32_t counterPosition = 0; |
169 CFX_Int32Array counters; | 169 CFX_Int32Array counters; |
170 counters.SetSize(6); | 170 counters.SetSize(6); |
171 FX_INT32 patternStart = rowOffset; | 171 int32_t patternStart = rowOffset; |
172 FX_BOOL isWhite = FALSE; | 172 FX_BOOL isWhite = FALSE; |
173 FX_INT32 patternLength = counters.GetSize(); | 173 int32_t patternLength = counters.GetSize(); |
174 for (FX_INT32 i = rowOffset; i < width; i++) { | 174 for (int32_t i = rowOffset; i < width; i++) { |
175 FX_BOOL pixel = row->Get(i); | 175 FX_BOOL pixel = row->Get(i); |
176 if (pixel ^ isWhite) { | 176 if (pixel ^ isWhite) { |
177 counters[counterPosition]++; | 177 counters[counterPosition]++; |
178 } else { | 178 } else { |
179 if (counterPosition == patternLength - 1) { | 179 if (counterPosition == patternLength - 1) { |
180 FX_INT32 bestVariance = MAX_AVG_VARIANCE; | 180 int32_t bestVariance = MAX_AVG_VARIANCE; |
181 FX_INT32 bestMatch = -1; | 181 int32_t bestMatch = -1; |
182 for (FX_INT32 startCode = CODE_START_A; startCode <= CODE_START_
C; startCode++) { | 182 for (int32_t startCode = CODE_START_A; startCode <= CODE_START_C
; startCode++) { |
183 FX_INT32 variance = PatternMatchVariance(&counters, &CODE_PA
TTERNS[startCode][0], MAX_INDIVIDUAL_VARIANCE); | 183 int32_t variance = PatternMatchVariance(&counters, &CODE_PAT
TERNS[startCode][0], MAX_INDIVIDUAL_VARIANCE); |
184 if (variance < bestVariance) { | 184 if (variance < bestVariance) { |
185 bestVariance = variance; | 185 bestVariance = variance; |
186 bestMatch = startCode; | 186 bestMatch = startCode; |
187 } | 187 } |
188 } | 188 } |
189 if (bestMatch >= 0) { | 189 if (bestMatch >= 0) { |
190 FX_BOOL btemp2 = row->IsRange(FX_MAX(0, patternStart - (i -
patternStart) / 2), patternStart, FALSE, e); | 190 FX_BOOL btemp2 = row->IsRange(FX_MAX(0, patternStart - (i -
patternStart) / 2), patternStart, FALSE, e); |
191 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 191 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
192 if (btemp2) { | 192 if (btemp2) { |
193 CFX_Int32Array *result = FX_NEW CFX_Int32Array; | 193 CFX_Int32Array *result = FX_NEW CFX_Int32Array; |
194 result->SetSize(3); | 194 result->SetSize(3); |
195 (*result)[0] = patternStart; | 195 (*result)[0] = patternStart; |
196 (*result)[1] = i; | 196 (*result)[1] = i; |
197 (*result)[2] = bestMatch; | 197 (*result)[2] = bestMatch; |
198 return result; | 198 return result; |
199 } | 199 } |
200 } | 200 } |
201 patternStart += counters[0] + counters[1]; | 201 patternStart += counters[0] + counters[1]; |
202 for (FX_INT32 y = 2; y < patternLength; y++) { | 202 for (int32_t y = 2; y < patternLength; y++) { |
203 counters[y - 2] = counters[y]; | 203 counters[y - 2] = counters[y]; |
204 } | 204 } |
205 counters[patternLength - 2] = 0; | 205 counters[patternLength - 2] = 0; |
206 counters[patternLength - 1] = 0; | 206 counters[patternLength - 1] = 0; |
207 counterPosition--; | 207 counterPosition--; |
208 } else { | 208 } else { |
209 counterPosition++; | 209 counterPosition++; |
210 } | 210 } |
211 counters[counterPosition] = 1; | 211 counters[counterPosition] = 1; |
212 isWhite = !isWhite; | 212 isWhite = !isWhite; |
213 } | 213 } |
214 } | 214 } |
215 e = BCExceptionNotFound; | 215 e = BCExceptionNotFound; |
216 return NULL; | 216 return NULL; |
217 } | 217 } |
218 FX_INT32 CBC_OnedCode128Reader::DecodeCode(CBC_CommonBitArray *row, CFX_Int32Arr
ay *counters, FX_INT32 rowOffset, FX_INT32 &e) | 218 int32_t CBC_OnedCode128Reader::DecodeCode(CBC_CommonBitArray *row, CFX_Int32Arra
y *counters, int32_t rowOffset, int32_t &e) |
219 { | 219 { |
220 RecordPattern(row, rowOffset, counters, e); | 220 RecordPattern(row, rowOffset, counters, e); |
221 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | 221 BC_EXCEPTION_CHECK_ReturnValue(e, 0); |
222 FX_INT32 bestVariance = MAX_AVG_VARIANCE; | 222 int32_t bestVariance = MAX_AVG_VARIANCE; |
223 FX_INT32 bestMatch = -1; | 223 int32_t bestMatch = -1; |
224 for (FX_INT32 d = 0; d < 107; d++) { | 224 for (int32_t d = 0; d < 107; d++) { |
225 FX_INT32 variance = PatternMatchVariance(counters, &CODE_PATTERNS[d][0],
MAX_INDIVIDUAL_VARIANCE); | 225 int32_t variance = PatternMatchVariance(counters, &CODE_PATTERNS[d][0],
MAX_INDIVIDUAL_VARIANCE); |
226 if (variance < bestVariance) { | 226 if (variance < bestVariance) { |
227 bestVariance = variance; | 227 bestVariance = variance; |
228 bestMatch = d; | 228 bestMatch = d; |
229 } | 229 } |
230 } | 230 } |
231 if (bestMatch >= 0) { | 231 if (bestMatch >= 0) { |
232 return bestMatch; | 232 return bestMatch; |
233 } else { | 233 } else { |
234 e = BCExceptionNotFound; | 234 e = BCExceptionNotFound; |
235 return 0; | 235 return 0; |
236 } | 236 } |
237 return 0; | 237 return 0; |
238 } | 238 } |
239 CFX_ByteString CBC_OnedCode128Reader::DecodeRow(FX_INT32 rowNumber, CBC_CommonBi
tArray *row, FX_INT32 hints, FX_INT32 &e) | 239 CFX_ByteString CBC_OnedCode128Reader::DecodeRow(int32_t rowNumber, CBC_CommonBit
Array *row, int32_t hints, int32_t &e) |
240 { | 240 { |
241 CFX_Int32Array *startPatternInfo = FindStartPattern(row, e); | 241 CFX_Int32Array *startPatternInfo = FindStartPattern(row, e); |
242 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | 242 BC_EXCEPTION_CHECK_ReturnValue(e, ""); |
243 FX_INT32 startCode = (*startPatternInfo)[2]; | 243 int32_t startCode = (*startPatternInfo)[2]; |
244 FX_INT32 codeSet; | 244 int32_t codeSet; |
245 switch (startCode) { | 245 switch (startCode) { |
246 case 103: | 246 case 103: |
247 codeSet = CODE_CODE_A; | 247 codeSet = CODE_CODE_A; |
248 break; | 248 break; |
249 case 104: | 249 case 104: |
250 codeSet = CODE_CODE_B; | 250 codeSet = CODE_CODE_B; |
251 break; | 251 break; |
252 case 105: | 252 case 105: |
253 codeSet = CODE_CODE_C; | 253 codeSet = CODE_CODE_C; |
254 break; | 254 break; |
255 default: | 255 default: |
256 if(startPatternInfo != NULL) { | 256 if(startPatternInfo != NULL) { |
257 startPatternInfo->RemoveAll(); | 257 startPatternInfo->RemoveAll(); |
258 delete startPatternInfo; | 258 delete startPatternInfo; |
259 startPatternInfo = NULL; | 259 startPatternInfo = NULL; |
260 } | 260 } |
261 e = BCExceptionFormatException; | 261 e = BCExceptionFormatException; |
262 return ""; | 262 return ""; |
263 } | 263 } |
264 FX_BOOL done = FALSE; | 264 FX_BOOL done = FALSE; |
265 FX_BOOL isNextShifted = FALSE; | 265 FX_BOOL isNextShifted = FALSE; |
266 CFX_ByteString result; | 266 CFX_ByteString result; |
267 FX_INT32 lastStart = (*startPatternInfo)[0]; | 267 int32_t lastStart = (*startPatternInfo)[0]; |
268 FX_INT32 nextStart = (*startPatternInfo)[1]; | 268 int32_t nextStart = (*startPatternInfo)[1]; |
269 if(startPatternInfo != NULL) { | 269 if(startPatternInfo != NULL) { |
270 startPatternInfo->RemoveAll(); | 270 startPatternInfo->RemoveAll(); |
271 delete startPatternInfo; | 271 delete startPatternInfo; |
272 startPatternInfo = NULL; | 272 startPatternInfo = NULL; |
273 } | 273 } |
274 CFX_Int32Array counters; | 274 CFX_Int32Array counters; |
275 counters.SetSize(6); | 275 counters.SetSize(6); |
276 FX_INT32 lastCode = 0; | 276 int32_t lastCode = 0; |
277 FX_INT32 code = 0; | 277 int32_t code = 0; |
278 FX_INT32 checksumTotal = startCode; | 278 int32_t checksumTotal = startCode; |
279 FX_INT32 multiplier = 0; | 279 int32_t multiplier = 0; |
280 FX_BOOL lastCharacterWasPrintable = TRUE; | 280 FX_BOOL lastCharacterWasPrintable = TRUE; |
281 while (!done) { | 281 while (!done) { |
282 FX_BOOL unshift = isNextShifted; | 282 FX_BOOL unshift = isNextShifted; |
283 isNextShifted = FALSE; | 283 isNextShifted = FALSE; |
284 lastCode = code; | 284 lastCode = code; |
285 code = DecodeCode(row, &counters, nextStart, e); | 285 code = DecodeCode(row, &counters, nextStart, e); |
286 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | 286 BC_EXCEPTION_CHECK_ReturnValue(e, ""); |
287 if (code != CODE_STOP) { | 287 if (code != CODE_STOP) { |
288 lastCharacterWasPrintable = TRUE; | 288 lastCharacterWasPrintable = TRUE; |
289 } | 289 } |
290 if (code != CODE_STOP) { | 290 if (code != CODE_STOP) { |
291 multiplier++; | 291 multiplier++; |
292 checksumTotal += multiplier * code; | 292 checksumTotal += multiplier * code; |
293 } | 293 } |
294 lastStart = nextStart; | 294 lastStart = nextStart; |
295 for (FX_INT32 i = 0; i < counters.GetSize(); i++) { | 295 for (int32_t i = 0; i < counters.GetSize(); i++) { |
296 nextStart += counters[i]; | 296 nextStart += counters[i]; |
297 } | 297 } |
298 switch (code) { | 298 switch (code) { |
299 case 103: | 299 case 103: |
300 case 104: | 300 case 104: |
301 case 105: | 301 case 105: |
302 e = BCExceptionFormatException; | 302 e = BCExceptionFormatException; |
303 return ""; | 303 return ""; |
304 } | 304 } |
305 switch (codeSet) { | 305 switch (codeSet) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 done = TRUE; | 392 done = TRUE; |
393 break; | 393 break; |
394 } | 394 } |
395 } | 395 } |
396 break; | 396 break; |
397 } | 397 } |
398 if (unshift) { | 398 if (unshift) { |
399 codeSet = codeSet == CODE_CODE_A ? CODE_CODE_B : CODE_CODE_A; | 399 codeSet = codeSet == CODE_CODE_A ? CODE_CODE_B : CODE_CODE_A; |
400 } | 400 } |
401 } | 401 } |
402 FX_INT32 width = row->GetSize(); | 402 int32_t width = row->GetSize(); |
403 while (nextStart < width && row->Get(nextStart)) { | 403 while (nextStart < width && row->Get(nextStart)) { |
404 nextStart++; | 404 nextStart++; |
405 } | 405 } |
406 FX_BOOL boolT1 = row->IsRange(nextStart, FX_MIN(width, nextStart + (nextStar
t - lastStart) / 2), FALSE, e); | 406 FX_BOOL boolT1 = row->IsRange(nextStart, FX_MIN(width, nextStart + (nextStar
t - lastStart) / 2), FALSE, e); |
407 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | 407 BC_EXCEPTION_CHECK_ReturnValue(e, ""); |
408 if (!boolT1) { | 408 if (!boolT1) { |
409 e = BCExceptionNotFound; | 409 e = BCExceptionNotFound; |
410 return ""; | 410 return ""; |
411 } | 411 } |
412 checksumTotal -= multiplier * lastCode; | 412 checksumTotal -= multiplier * lastCode; |
413 if (checksumTotal % 103 != lastCode) { | 413 if (checksumTotal % 103 != lastCode) { |
414 e = BCExceptionChecksumException; | 414 e = BCExceptionChecksumException; |
415 return ""; | 415 return ""; |
416 } | 416 } |
417 FX_INT32 resultLength = result.GetLength(); | 417 int32_t resultLength = result.GetLength(); |
418 if (resultLength > 0 && lastCharacterWasPrintable) { | 418 if (resultLength > 0 && lastCharacterWasPrintable) { |
419 if (codeSet == CODE_CODE_C) { | 419 if (codeSet == CODE_CODE_C) { |
420 result = result.Mid(0, result.GetLength() - 2); | 420 result = result.Mid(0, result.GetLength() - 2); |
421 } else { | 421 } else { |
422 result = result.Mid(0, result.GetLength() - 1); | 422 result = result.Mid(0, result.GetLength() - 1); |
423 } | 423 } |
424 } | 424 } |
425 if (result.GetLength() == 0) { | 425 if (result.GetLength() == 0) { |
426 e = BCExceptionFormatException; | 426 e = BCExceptionFormatException; |
427 return ""; | 427 return ""; |
428 } | 428 } |
429 return result; | 429 return result; |
430 } | 430 } |
OLD | NEW |