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_PDF417Common.h" | |
25 #include "include/BC_PDF417CodewordDecoder.h" | |
26 #define SYMBOL_TABLE_Length 2787 | |
27 #define Float_MAX_VALUE 2147483647 | |
28 FX_FLOAT CBC_PDF417CodewordDecoder::RATIOS_TABLE[2787][8] = {0}; | |
29 CBC_PDF417CodewordDecoder::CBC_PDF417CodewordDecoder() | |
30 { | |
31 } | |
32 CBC_PDF417CodewordDecoder::~CBC_PDF417CodewordDecoder() | |
33 { | |
34 } | |
35 void CBC_PDF417CodewordDecoder::Initialize() | |
36 { | |
37 for (FX_INT32 i = 0; i < SYMBOL_TABLE_Length; i++) { | |
38 FX_INT32 currentSymbol = CBC_PDF417Common::SYMBOL_TABLE[i]; | |
39 FX_INT32 currentBit = currentSymbol & 0x1; | |
40 for (FX_INT32 j = 0; j < CBC_PDF417Common::BARS_IN_MODULE; j++) { | |
41 FX_FLOAT size = 0.0f; | |
42 while ((currentSymbol & 0x1) == currentBit) { | |
43 size += 1.0f; | |
44 currentSymbol >>= 1; | |
45 } | |
46 currentBit = currentSymbol & 0x1; | |
47 RATIOS_TABLE[i][CBC_PDF417Common::BARS_IN_MODULE - j - 1] = size / C
BC_PDF417Common::MODULES_IN_CODEWORD; | |
48 } | |
49 } | |
50 } | |
51 void CBC_PDF417CodewordDecoder::Finalize() | |
52 { | |
53 } | |
54 FX_INT32 CBC_PDF417CodewordDecoder::getDecodedValue(CFX_Int32Array& moduleBitCou
nt) | |
55 { | |
56 CFX_Int32Array* array = sampleBitCounts(moduleBitCount); | |
57 FX_INT32 decodedValue = getDecodedCodewordValue(*array); | |
58 delete array; | |
59 if (decodedValue != -1) { | |
60 return decodedValue; | |
61 } | |
62 return getClosestDecodedValue(moduleBitCount); | |
63 } | |
64 CFX_Int32Array* CBC_PDF417CodewordDecoder::sampleBitCounts(CFX_Int32Array& modul
eBitCount) | |
65 { | |
66 FX_FLOAT bitCountSum = (FX_FLOAT)CBC_PDF417Common::getBitCountSum(moduleBitC
ount); | |
67 CFX_Int32Array* bitCount = FX_NEW CFX_Int32Array(); | |
68 bitCount->SetSize(CBC_PDF417Common::BARS_IN_MODULE); | |
69 FX_INT32 bitCountIndex = 0; | |
70 FX_INT32 sumPreviousBits = 0; | |
71 for (FX_INT32 i = 0; i < CBC_PDF417Common::MODULES_IN_CODEWORD; i++) { | |
72 FX_FLOAT sampleIndex = bitCountSum / (2 * CBC_PDF417Common::MODULES_IN_C
ODEWORD) + (i * bitCountSum) / CBC_PDF417Common::MODULES_IN_CODEWORD; | |
73 if (sumPreviousBits + moduleBitCount.GetAt(bitCountIndex) <= sampleIndex
) { | |
74 sumPreviousBits += moduleBitCount.GetAt(bitCountIndex); | |
75 bitCountIndex++; | |
76 } | |
77 bitCount->SetAt(bitCountIndex, bitCount->GetAt(bitCountIndex) + 1); | |
78 } | |
79 return bitCount; | |
80 } | |
81 FX_INT32 CBC_PDF417CodewordDecoder::getDecodedCodewordValue(CFX_Int32Array& modu
leBitCount) | |
82 { | |
83 FX_INT32 decodedValue = getBitValue(moduleBitCount); | |
84 return CBC_PDF417Common::getCodeword(decodedValue) == -1 ? -1 : decodedValue
; | |
85 } | |
86 FX_INT32 CBC_PDF417CodewordDecoder::getBitValue(CFX_Int32Array& moduleBitCount) | |
87 { | |
88 FX_INT64 result = 0; | |
89 for (FX_INT32 i = 0; i < moduleBitCount.GetSize(); i++) { | |
90 for (FX_INT32 bit = 0; bit < moduleBitCount.GetAt(i); bit++) { | |
91 result = (result << 1) | (i % 2 == 0 ? 1 : 0); | |
92 } | |
93 } | |
94 return (FX_INT32) result; | |
95 } | |
96 FX_INT32 CBC_PDF417CodewordDecoder::getClosestDecodedValue(CFX_Int32Array& modul
eBitCount) | |
97 { | |
98 FX_INT32 bitCountSum = CBC_PDF417Common::getBitCountSum(moduleBitCount); | |
99 CFX_FloatArray bitCountRatios; | |
100 bitCountRatios.SetSize(CBC_PDF417Common::BARS_IN_MODULE); | |
101 for (FX_INT32 i = 0; i < bitCountRatios.GetSize(); i++) { | |
102 bitCountRatios[i] = moduleBitCount.GetAt(i) / (FX_FLOAT) bitCountSum; | |
103 } | |
104 FX_FLOAT bestMatchError = (FX_FLOAT)Float_MAX_VALUE; | |
105 FX_INT32 bestMatch = -1; | |
106 for (FX_INT32 j = 0; j < SYMBOL_TABLE_Length; j++) { | |
107 FX_FLOAT error = 0.0f; | |
108 for (FX_INT32 k = 0; k < CBC_PDF417Common::BARS_IN_MODULE; k++) { | |
109 FX_FLOAT diff = RATIOS_TABLE[j][k] - bitCountRatios[k]; | |
110 error += diff * diff; | |
111 } | |
112 if (error < bestMatchError) { | |
113 bestMatchError = error; | |
114 bestMatch = CBC_PDF417Common::SYMBOL_TABLE[j]; | |
115 } | |
116 } | |
117 return bestMatch; | |
118 } | |
OLD | NEW |