Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: xfa/src/fxbarcode/src/BC_OnedCode39Reader.cpp

Issue 842043002: Organize barcode codes into modules. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "barcode.h"
24 #include "include/BC_Reader.h"
25 #include "include/BC_OneDReader.h"
26 #include "include/BC_CommonBitArray.h"
27 #include "include/BC_OnedCode39Reader.h"
28 FX_LPCSTR CBC_OnedCode39Reader::ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRS TUVWXYZ-. *$/+%";
29 FX_LPCSTR CBC_OnedCode39Reader::CHECKSUM_STRING = "0123456789ABCDEFGHIJKLMNOPQRS TUVWXYZ-. $/+%";
30 const FX_INT32 CBC_OnedCode39Reader::CHARACTER_ENCODINGS[44] = {
31 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064,
32 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C,
33 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016,
34 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x094,
35 0x0A8, 0x0A2, 0x08A, 0x02A
36 };
37 const FX_INT32 CBC_OnedCode39Reader::ASTERISK_ENCODING = 0x094;
38 CBC_OnedCode39Reader::CBC_OnedCode39Reader(): m_extendedMode(FALSE), m_usingChec kDigit(FALSE)
39 {
40 }
41 CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit)
42 {
43 m_usingCheckDigit = usingCheckDigit;
44 m_extendedMode = FALSE;
45 }
46 CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit, FX_BOOL exte ndedMode)
47 {
48 m_extendedMode = extendedMode;
49 m_usingCheckDigit = usingCheckDigit;
50 }
51 CBC_OnedCode39Reader::~CBC_OnedCode39Reader()
52 {
53 }
54 CFX_ByteString CBC_OnedCode39Reader::DecodeRow(FX_INT32 rowNumber, CBC_CommonBit Array *row, FX_INT32 hints, FX_INT32 &e)
55 {
56 CFX_Int32Array *start = FindAsteriskPattern(row, e);
57 BC_EXCEPTION_CHECK_ReturnValue(e, "");
58 FX_INT32 nextStart = (*start)[1];
59 if(start != NULL) {
60 delete start;
61 start = NULL;
62 }
63 FX_INT32 end = row->GetSize();
64 while (nextStart < end && !row->Get(nextStart)) {
65 nextStart++;
66 }
67 CFX_ByteString result;
68 CFX_Int32Array counters;
69 counters.SetSize(9);
70 FX_CHAR decodedChar;
71 FX_INT32 lastStart;
72 do {
73 RecordPattern(row, nextStart, &counters, e);
74 BC_EXCEPTION_CHECK_ReturnValue(e, "");
75 FX_INT32 pattern = ToNarrowWidePattern(&counters);
76 if (pattern < 0) {
77 e = BCExceptionNotFound;
78 return "";
79 }
80 decodedChar = PatternToChar(pattern, e);
81 BC_EXCEPTION_CHECK_ReturnValue(e, "");
82 result += decodedChar;
83 lastStart = nextStart;
84 for (FX_INT32 i = 0; i < counters.GetSize(); i++) {
85 nextStart += counters[i];
86 }
87 while (nextStart < end && !row->Get(nextStart)) {
88 nextStart++;
89 }
90 } while (decodedChar != '*');
91 result = result.Mid(0, result.GetLength() - 1);
92 FX_INT32 lastPatternSize = 0;
93 for (FX_INT32 j = 0; j < counters.GetSize(); j++) {
94 lastPatternSize += counters[j];
95 }
96 FX_INT32 whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
97 if(m_usingCheckDigit) {
98 FX_INT32 max = result.GetLength() - 1;
99 FX_INT32 total = 0;
100 FX_INT32 len = (FX_INT32)strlen(ALPHABET_STRING);
101 for (FX_INT32 k = 0; k < max; k++) {
102 for (FX_INT32 j = 0; j < len; j++)
103 if (ALPHABET_STRING[j] == result[k]) {
104 total += j;
105 }
106 }
107 if (result[max] != (ALPHABET_STRING)[total % 43]) {
108 e = BCExceptionChecksumException;
109 return "";
110 }
111 result = result.Mid(0, result.GetLength() - 1);
112 }
113 if (result.GetLength() == 0) {
114 e = BCExceptionNotFound;
115 return "";
116 }
117 if(m_extendedMode) {
118 CFX_ByteString bytestr = DecodeExtended(result, e);
119 BC_EXCEPTION_CHECK_ReturnValue(e, "");
120 return bytestr;
121 } else {
122 return result;
123 }
124 }
125 CFX_Int32Array *CBC_OnedCode39Reader::FindAsteriskPattern(CBC_CommonBitArray *ro w, FX_INT32 &e)
126 {
127 FX_INT32 width = row->GetSize();
128 FX_INT32 rowOffset = 0;
129 while (rowOffset < width) {
130 if (row->Get(rowOffset)) {
131 break;
132 }
133 rowOffset++;
134 }
135 FX_INT32 counterPosition = 0;
136 CFX_Int32Array counters;
137 counters.SetSize(9);
138 FX_INT32 patternStart = rowOffset;
139 FX_BOOL isWhite = FALSE;
140 FX_INT32 patternLength = counters.GetSize();
141 for (FX_INT32 i = rowOffset; i < width; i++) {
142 FX_BOOL pixel = row->Get(i);
143 if (pixel ^ isWhite) {
144 counters[counterPosition]++;
145 } else {
146 if (counterPosition == patternLength - 1) {
147 if (ToNarrowWidePattern(&counters) == ASTERISK_ENCODING) {
148 FX_BOOL bT1 = row->IsRange(FX_MAX(0, patternStart - (i - pa tternStart) / 2), patternStart, FALSE, e);
149 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
150 if (bT1) {
151 CFX_Int32Array *result = FX_NEW CFX_Int32Array;
152 result->SetSize(2);
153 (*result)[0] = patternStart;
154 (*result)[1] = i;
155 return result;
156 }
157 }
158 patternStart += counters[0] + counters[1];
159 for (FX_INT32 y = 2; y < patternLength; y++) {
160 counters[y - 2] = counters[y];
161 }
162 counters[patternLength - 2] = 0;
163 counters[patternLength - 1] = 0;
164 counterPosition--;
165 } else {
166 counterPosition++;
167 }
168 counters[counterPosition] = 1;
169 isWhite = !isWhite;
170 }
171 }
172 e = BCExceptionNotFound;
173 return NULL;
174 }
175 FX_INT32 CBC_OnedCode39Reader::ToNarrowWidePattern(CFX_Int32Array *counters)
176 {
177 FX_INT32 numCounters = counters->GetSize();
178 FX_INT32 maxNarrowCounter = 0;
179 FX_INT32 wideCounters;
180 do {
181 #undef max
182 FX_INT32 minCounter = FXSYS_IntMax;
183 for (FX_INT32 i = 0; i < numCounters; i++) {
184 FX_INT32 counter = (*counters)[i];
185 if (counter < minCounter && counter > maxNarrowCounter) {
186 minCounter = counter;
187 }
188 }
189 maxNarrowCounter = minCounter;
190 wideCounters = 0;
191 FX_INT32 totalWideCountersWidth = 0;
192 FX_INT32 pattern = 0;
193 for (FX_INT32 j = 0; j < numCounters; j++) {
194 FX_INT32 counter = (*counters)[j];
195 if ((*counters)[j] > maxNarrowCounter) {
196 pattern |= 1 << (numCounters - 1 - j);
197 wideCounters++;
198 totalWideCountersWidth += counter;
199 }
200 }
201 if (wideCounters == 3) {
202 for (FX_INT32 k = 0; k < numCounters && wideCounters > 0; k++) {
203 FX_INT32 counter = (*counters)[k];
204 if ((*counters)[k] > maxNarrowCounter) {
205 wideCounters--;
206 if ((counter << 1) >= totalWideCountersWidth) {
207 return -1;
208 }
209 }
210 }
211 return pattern;
212 }
213 } while (wideCounters > 3);
214 return -1;
215 }
216 FX_CHAR CBC_OnedCode39Reader::PatternToChar(FX_INT32 pattern, FX_INT32 &e)
217 {
218 for (FX_INT32 i = 0; i < 44; i++) {
219 if (CHARACTER_ENCODINGS[i] == pattern) {
220 return (ALPHABET_STRING)[i];
221 }
222 }
223 e = BCExceptionNotFound;
224 return 0;
225 }
226 CFX_ByteString CBC_OnedCode39Reader::DecodeExtended(CFX_ByteString &encoded, FX_ INT32 &e)
227 {
228 FX_INT32 length = encoded.GetLength();
229 CFX_ByteString decoded;
230 FX_CHAR c, next;
231 for(FX_INT32 i = 0; i < length; i++) {
232 c = encoded[i];
233 if(c == '+' || c == '$' || c == '%' || c == '/') {
234 next = encoded[i + 1];
235 FX_CHAR decodedChar = '\0';
236 switch (c) {
237 case '+':
238 if (next >= 'A' && next <= 'Z') {
239 decodedChar = (FX_CHAR) (next + 32);
240 } else {
241 e = BCExceptionFormatException;
242 return "";
243 }
244 break;
245 case '$':
246 if (next >= 'A' && next <= 'Z') {
247 decodedChar = (FX_CHAR) (next - 64);
248 } else {
249 e = BCExceptionFormatException;
250 return "";
251 }
252 break;
253 case '%':
254 if (next >= 'A' && next <= 'E') {
255 decodedChar = (FX_CHAR) (next - 38);
256 } else if (next >= 'F' && next <= 'J') {
257 decodedChar = (FX_CHAR) (next - 11);
258 } else if (next >= 'K' && next <= 'O' && next != 'M' && next != 'N') {
259 decodedChar = (FX_CHAR) (next + 16);
260 } else if (next >= 'P' && next <= 'S') {
261 decodedChar = (FX_CHAR) (next + 43);
262 } else if (next == 'U') {
263 decodedChar = (FX_CHAR) 0;
264 } else if (next == 'V') {
265 decodedChar = (FX_CHAR) 64;
266 } else if (next == 'W') {
267 decodedChar = (FX_CHAR) 96;
268 } else if (next == 'T' || next == 'X' || next == 'Y' || next == 'Z') {
269 decodedChar = (FX_CHAR) 127;
270 } else {
271 e = BCExceptionFormatException;
272 return "";
273 }
274 break;
275 case '/':
276 if (next >= 'A' && next <= 'O') {
277 decodedChar = (FX_CHAR) (next - 32);
278 } else if (next == 'Z') {
279 decodedChar = ':';
280 } else {
281 e = BCExceptionFormatException;
282 return "";
283 }
284 break;
285 }
286 decoded += decodedChar;
287 i++;
288 } else {
289 decoded += c;
290 }
291 }
292 return decoded;
293 }
OLDNEW
« no previous file with comments | « xfa/src/fxbarcode/src/BC_OnedCode128Writer.cpp ('k') | xfa/src/fxbarcode/src/BC_OnedCode39Writer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698