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_OnedCode39Reader.h" | 27 #include "BC_OnedCode39Reader.h" |
28 FX_LPCSTR CBC_OnedCode39Reader::ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRS
TUVWXYZ-. *$/+%"; | 28 FX_LPCSTR CBC_OnedCode39Reader::ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRS
TUVWXYZ-. *$/+%"; |
29 FX_LPCSTR CBC_OnedCode39Reader::CHECKSUM_STRING = "0123456789ABCDEFGHIJKLMNOPQRS
TUVWXYZ-. $/+%"; | 29 FX_LPCSTR CBC_OnedCode39Reader::CHECKSUM_STRING = "0123456789ABCDEFGHIJKLMNOPQRS
TUVWXYZ-. $/+%"; |
30 const FX_INT32 CBC_OnedCode39Reader::CHARACTER_ENCODINGS[44] = { | 30 const int32_t CBC_OnedCode39Reader::CHARACTER_ENCODINGS[44] = { |
31 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, | 31 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, |
32 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, | 32 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, |
33 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, | 33 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, |
34 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x094, | 34 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x094, |
35 0x0A8, 0x0A2, 0x08A, 0x02A | 35 0x0A8, 0x0A2, 0x08A, 0x02A |
36 }; | 36 }; |
37 const FX_INT32 CBC_OnedCode39Reader::ASTERISK_ENCODING = 0x094; | 37 const int32_t CBC_OnedCode39Reader::ASTERISK_ENCODING = 0x094; |
38 CBC_OnedCode39Reader::CBC_OnedCode39Reader(): m_extendedMode(FALSE), m_usingChec
kDigit(FALSE) | 38 CBC_OnedCode39Reader::CBC_OnedCode39Reader(): m_extendedMode(FALSE), m_usingChec
kDigit(FALSE) |
39 { | 39 { |
40 } | 40 } |
41 CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit) | 41 CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit) |
42 { | 42 { |
43 m_usingCheckDigit = usingCheckDigit; | 43 m_usingCheckDigit = usingCheckDigit; |
44 m_extendedMode = FALSE; | 44 m_extendedMode = FALSE; |
45 } | 45 } |
46 CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit, FX_BOOL exte
ndedMode) | 46 CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit, FX_BOOL exte
ndedMode) |
47 { | 47 { |
48 m_extendedMode = extendedMode; | 48 m_extendedMode = extendedMode; |
49 m_usingCheckDigit = usingCheckDigit; | 49 m_usingCheckDigit = usingCheckDigit; |
50 } | 50 } |
51 CBC_OnedCode39Reader::~CBC_OnedCode39Reader() | 51 CBC_OnedCode39Reader::~CBC_OnedCode39Reader() |
52 { | 52 { |
53 } | 53 } |
54 CFX_ByteString CBC_OnedCode39Reader::DecodeRow(FX_INT32 rowNumber, CBC_CommonBit
Array *row, FX_INT32 hints, FX_INT32 &e) | 54 CFX_ByteString CBC_OnedCode39Reader::DecodeRow(int32_t rowNumber, CBC_CommonBitA
rray *row, int32_t hints, int32_t &e) |
55 { | 55 { |
56 CFX_Int32Array *start = FindAsteriskPattern(row, e); | 56 CFX_Int32Array *start = FindAsteriskPattern(row, e); |
57 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | 57 BC_EXCEPTION_CHECK_ReturnValue(e, ""); |
58 FX_INT32 nextStart = (*start)[1]; | 58 int32_t nextStart = (*start)[1]; |
59 if(start != NULL) { | 59 if(start != NULL) { |
60 delete start; | 60 delete start; |
61 start = NULL; | 61 start = NULL; |
62 } | 62 } |
63 FX_INT32 end = row->GetSize(); | 63 int32_t end = row->GetSize(); |
64 while (nextStart < end && !row->Get(nextStart)) { | 64 while (nextStart < end && !row->Get(nextStart)) { |
65 nextStart++; | 65 nextStart++; |
66 } | 66 } |
67 CFX_ByteString result; | 67 CFX_ByteString result; |
68 CFX_Int32Array counters; | 68 CFX_Int32Array counters; |
69 counters.SetSize(9); | 69 counters.SetSize(9); |
70 FX_CHAR decodedChar; | 70 FX_CHAR decodedChar; |
71 FX_INT32 lastStart; | 71 int32_t lastStart; |
72 do { | 72 do { |
73 RecordPattern(row, nextStart, &counters, e); | 73 RecordPattern(row, nextStart, &counters, e); |
74 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | 74 BC_EXCEPTION_CHECK_ReturnValue(e, ""); |
75 FX_INT32 pattern = ToNarrowWidePattern(&counters); | 75 int32_t pattern = ToNarrowWidePattern(&counters); |
76 if (pattern < 0) { | 76 if (pattern < 0) { |
77 e = BCExceptionNotFound; | 77 e = BCExceptionNotFound; |
78 return ""; | 78 return ""; |
79 } | 79 } |
80 decodedChar = PatternToChar(pattern, e); | 80 decodedChar = PatternToChar(pattern, e); |
81 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | 81 BC_EXCEPTION_CHECK_ReturnValue(e, ""); |
82 result += decodedChar; | 82 result += decodedChar; |
83 lastStart = nextStart; | 83 lastStart = nextStart; |
84 for (FX_INT32 i = 0; i < counters.GetSize(); i++) { | 84 for (int32_t i = 0; i < counters.GetSize(); i++) { |
85 nextStart += counters[i]; | 85 nextStart += counters[i]; |
86 } | 86 } |
87 while (nextStart < end && !row->Get(nextStart)) { | 87 while (nextStart < end && !row->Get(nextStart)) { |
88 nextStart++; | 88 nextStart++; |
89 } | 89 } |
90 } while (decodedChar != '*'); | 90 } while (decodedChar != '*'); |
91 result = result.Mid(0, result.GetLength() - 1); | 91 result = result.Mid(0, result.GetLength() - 1); |
92 FX_INT32 lastPatternSize = 0; | 92 int32_t lastPatternSize = 0; |
93 for (FX_INT32 j = 0; j < counters.GetSize(); j++) { | 93 for (int32_t j = 0; j < counters.GetSize(); j++) { |
94 lastPatternSize += counters[j]; | 94 lastPatternSize += counters[j]; |
95 } | 95 } |
96 FX_INT32 whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize; | 96 int32_t whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize; |
97 if(m_usingCheckDigit) { | 97 if(m_usingCheckDigit) { |
98 FX_INT32 max = result.GetLength() - 1; | 98 int32_t max = result.GetLength() - 1; |
99 FX_INT32 total = 0; | 99 int32_t total = 0; |
100 FX_INT32 len = (FX_INT32)strlen(ALPHABET_STRING); | 100 int32_t len = (int32_t)strlen(ALPHABET_STRING); |
101 for (FX_INT32 k = 0; k < max; k++) { | 101 for (int32_t k = 0; k < max; k++) { |
102 for (FX_INT32 j = 0; j < len; j++) | 102 for (int32_t j = 0; j < len; j++) |
103 if (ALPHABET_STRING[j] == result[k]) { | 103 if (ALPHABET_STRING[j] == result[k]) { |
104 total += j; | 104 total += j; |
105 } | 105 } |
106 } | 106 } |
107 if (result[max] != (ALPHABET_STRING)[total % 43]) { | 107 if (result[max] != (ALPHABET_STRING)[total % 43]) { |
108 e = BCExceptionChecksumException; | 108 e = BCExceptionChecksumException; |
109 return ""; | 109 return ""; |
110 } | 110 } |
111 result = result.Mid(0, result.GetLength() - 1); | 111 result = result.Mid(0, result.GetLength() - 1); |
112 } | 112 } |
113 if (result.GetLength() == 0) { | 113 if (result.GetLength() == 0) { |
114 e = BCExceptionNotFound; | 114 e = BCExceptionNotFound; |
115 return ""; | 115 return ""; |
116 } | 116 } |
117 if(m_extendedMode) { | 117 if(m_extendedMode) { |
118 CFX_ByteString bytestr = DecodeExtended(result, e); | 118 CFX_ByteString bytestr = DecodeExtended(result, e); |
119 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | 119 BC_EXCEPTION_CHECK_ReturnValue(e, ""); |
120 return bytestr; | 120 return bytestr; |
121 } else { | 121 } else { |
122 return result; | 122 return result; |
123 } | 123 } |
124 } | 124 } |
125 CFX_Int32Array *CBC_OnedCode39Reader::FindAsteriskPattern(CBC_CommonBitArray *ro
w, FX_INT32 &e) | 125 CFX_Int32Array *CBC_OnedCode39Reader::FindAsteriskPattern(CBC_CommonBitArray *ro
w, int32_t &e) |
126 { | 126 { |
127 FX_INT32 width = row->GetSize(); | 127 int32_t width = row->GetSize(); |
128 FX_INT32 rowOffset = 0; | 128 int32_t rowOffset = 0; |
129 while (rowOffset < width) { | 129 while (rowOffset < width) { |
130 if (row->Get(rowOffset)) { | 130 if (row->Get(rowOffset)) { |
131 break; | 131 break; |
132 } | 132 } |
133 rowOffset++; | 133 rowOffset++; |
134 } | 134 } |
135 FX_INT32 counterPosition = 0; | 135 int32_t counterPosition = 0; |
136 CFX_Int32Array counters; | 136 CFX_Int32Array counters; |
137 counters.SetSize(9); | 137 counters.SetSize(9); |
138 FX_INT32 patternStart = rowOffset; | 138 int32_t patternStart = rowOffset; |
139 FX_BOOL isWhite = FALSE; | 139 FX_BOOL isWhite = FALSE; |
140 FX_INT32 patternLength = counters.GetSize(); | 140 int32_t patternLength = counters.GetSize(); |
141 for (FX_INT32 i = rowOffset; i < width; i++) { | 141 for (int32_t i = rowOffset; i < width; i++) { |
142 FX_BOOL pixel = row->Get(i); | 142 FX_BOOL pixel = row->Get(i); |
143 if (pixel ^ isWhite) { | 143 if (pixel ^ isWhite) { |
144 counters[counterPosition]++; | 144 counters[counterPosition]++; |
145 } else { | 145 } else { |
146 if (counterPosition == patternLength - 1) { | 146 if (counterPosition == patternLength - 1) { |
147 if (ToNarrowWidePattern(&counters) == ASTERISK_ENCODING) { | 147 if (ToNarrowWidePattern(&counters) == ASTERISK_ENCODING) { |
148 FX_BOOL bT1 = row->IsRange(FX_MAX(0, patternStart - (i - pa
tternStart) / 2), patternStart, FALSE, e); | 148 FX_BOOL bT1 = row->IsRange(FX_MAX(0, patternStart - (i - pa
tternStart) / 2), patternStart, FALSE, e); |
149 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | 149 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); |
150 if (bT1) { | 150 if (bT1) { |
151 CFX_Int32Array *result = FX_NEW CFX_Int32Array; | 151 CFX_Int32Array *result = FX_NEW CFX_Int32Array; |
152 result->SetSize(2); | 152 result->SetSize(2); |
153 (*result)[0] = patternStart; | 153 (*result)[0] = patternStart; |
154 (*result)[1] = i; | 154 (*result)[1] = i; |
155 return result; | 155 return result; |
156 } | 156 } |
157 } | 157 } |
158 patternStart += counters[0] + counters[1]; | 158 patternStart += counters[0] + counters[1]; |
159 for (FX_INT32 y = 2; y < patternLength; y++) { | 159 for (int32_t y = 2; y < patternLength; y++) { |
160 counters[y - 2] = counters[y]; | 160 counters[y - 2] = counters[y]; |
161 } | 161 } |
162 counters[patternLength - 2] = 0; | 162 counters[patternLength - 2] = 0; |
163 counters[patternLength - 1] = 0; | 163 counters[patternLength - 1] = 0; |
164 counterPosition--; | 164 counterPosition--; |
165 } else { | 165 } else { |
166 counterPosition++; | 166 counterPosition++; |
167 } | 167 } |
168 counters[counterPosition] = 1; | 168 counters[counterPosition] = 1; |
169 isWhite = !isWhite; | 169 isWhite = !isWhite; |
170 } | 170 } |
171 } | 171 } |
172 e = BCExceptionNotFound; | 172 e = BCExceptionNotFound; |
173 return NULL; | 173 return NULL; |
174 } | 174 } |
175 FX_INT32 CBC_OnedCode39Reader::ToNarrowWidePattern(CFX_Int32Array *counters) | 175 int32_t CBC_OnedCode39Reader::ToNarrowWidePattern(CFX_Int32Array *counters) |
176 { | 176 { |
177 FX_INT32 numCounters = counters->GetSize(); | 177 int32_t numCounters = counters->GetSize(); |
178 FX_INT32 maxNarrowCounter = 0; | 178 int32_t maxNarrowCounter = 0; |
179 FX_INT32 wideCounters; | 179 int32_t wideCounters; |
180 do { | 180 do { |
181 #undef max | 181 #undef max |
182 FX_INT32 minCounter = FXSYS_IntMax; | 182 int32_t minCounter = FXSYS_IntMax; |
183 for (FX_INT32 i = 0; i < numCounters; i++) { | 183 for (int32_t i = 0; i < numCounters; i++) { |
184 FX_INT32 counter = (*counters)[i]; | 184 int32_t counter = (*counters)[i]; |
185 if (counter < minCounter && counter > maxNarrowCounter) { | 185 if (counter < minCounter && counter > maxNarrowCounter) { |
186 minCounter = counter; | 186 minCounter = counter; |
187 } | 187 } |
188 } | 188 } |
189 maxNarrowCounter = minCounter; | 189 maxNarrowCounter = minCounter; |
190 wideCounters = 0; | 190 wideCounters = 0; |
191 FX_INT32 totalWideCountersWidth = 0; | 191 int32_t totalWideCountersWidth = 0; |
192 FX_INT32 pattern = 0; | 192 int32_t pattern = 0; |
193 for (FX_INT32 j = 0; j < numCounters; j++) { | 193 for (int32_t j = 0; j < numCounters; j++) { |
194 FX_INT32 counter = (*counters)[j]; | 194 int32_t counter = (*counters)[j]; |
195 if ((*counters)[j] > maxNarrowCounter) { | 195 if ((*counters)[j] > maxNarrowCounter) { |
196 pattern |= 1 << (numCounters - 1 - j); | 196 pattern |= 1 << (numCounters - 1 - j); |
197 wideCounters++; | 197 wideCounters++; |
198 totalWideCountersWidth += counter; | 198 totalWideCountersWidth += counter; |
199 } | 199 } |
200 } | 200 } |
201 if (wideCounters == 3) { | 201 if (wideCounters == 3) { |
202 for (FX_INT32 k = 0; k < numCounters && wideCounters > 0; k++) { | 202 for (int32_t k = 0; k < numCounters && wideCounters > 0; k++) { |
203 FX_INT32 counter = (*counters)[k]; | 203 int32_t counter = (*counters)[k]; |
204 if ((*counters)[k] > maxNarrowCounter) { | 204 if ((*counters)[k] > maxNarrowCounter) { |
205 wideCounters--; | 205 wideCounters--; |
206 if ((counter << 1) >= totalWideCountersWidth) { | 206 if ((counter << 1) >= totalWideCountersWidth) { |
207 return -1; | 207 return -1; |
208 } | 208 } |
209 } | 209 } |
210 } | 210 } |
211 return pattern; | 211 return pattern; |
212 } | 212 } |
213 } while (wideCounters > 3); | 213 } while (wideCounters > 3); |
214 return -1; | 214 return -1; |
215 } | 215 } |
216 FX_CHAR CBC_OnedCode39Reader::PatternToChar(FX_INT32 pattern, FX_INT32 &e) | 216 FX_CHAR CBC_OnedCode39Reader::PatternToChar(int32_t pattern, int32_t &e) |
217 { | 217 { |
218 for (FX_INT32 i = 0; i < 44; i++) { | 218 for (int32_t i = 0; i < 44; i++) { |
219 if (CHARACTER_ENCODINGS[i] == pattern) { | 219 if (CHARACTER_ENCODINGS[i] == pattern) { |
220 return (ALPHABET_STRING)[i]; | 220 return (ALPHABET_STRING)[i]; |
221 } | 221 } |
222 } | 222 } |
223 e = BCExceptionNotFound; | 223 e = BCExceptionNotFound; |
224 return 0; | 224 return 0; |
225 } | 225 } |
226 CFX_ByteString CBC_OnedCode39Reader::DecodeExtended(CFX_ByteString &encoded, FX_
INT32 &e) | 226 CFX_ByteString CBC_OnedCode39Reader::DecodeExtended(CFX_ByteString &encoded, int
32_t &e) |
227 { | 227 { |
228 FX_INT32 length = encoded.GetLength(); | 228 int32_t length = encoded.GetLength(); |
229 CFX_ByteString decoded; | 229 CFX_ByteString decoded; |
230 FX_CHAR c, next; | 230 FX_CHAR c, next; |
231 for(FX_INT32 i = 0; i < length; i++) { | 231 for(int32_t i = 0; i < length; i++) { |
232 c = encoded[i]; | 232 c = encoded[i]; |
233 if(c == '+' || c == '$' || c == '%' || c == '/') { | 233 if(c == '+' || c == '$' || c == '%' || c == '/') { |
234 next = encoded[i + 1]; | 234 next = encoded[i + 1]; |
235 FX_CHAR decodedChar = '\0'; | 235 FX_CHAR decodedChar = '\0'; |
236 switch (c) { | 236 switch (c) { |
237 case '+': | 237 case '+': |
238 if (next >= 'A' && next <= 'Z') { | 238 if (next >= 'A' && next <= 'Z') { |
239 decodedChar = (FX_CHAR) (next + 32); | 239 decodedChar = (FX_CHAR) (next + 32); |
240 } else { | 240 } else { |
241 e = BCExceptionFormatException; | 241 e = BCExceptionFormatException; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 break; | 284 break; |
285 } | 285 } |
286 decoded += decodedChar; | 286 decoded += decodedChar; |
287 i++; | 287 i++; |
288 } else { | 288 } else { |
289 decoded += c; | 289 decoded += c; |
290 } | 290 } |
291 } | 291 } |
292 return decoded; | 292 return decoded; |
293 } | 293 } |
OLD | NEW |