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