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 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_OnedCode39Reader.h" | |
27 #include "include/BC_CommonBitArray.h" | |
28 #include "include/BC_OnedCodaBarReader.h" | |
29 FX_LPCSTR CBC_OnedCodaBarReader::ALPHABET_STRING = "0123456789-$:/.+ABCDTN"; | |
30 const FX_INT32 CBC_OnedCodaBarReader::CHARACTER_ENCODINGS[22] = { | |
31 0x003, 0x006, 0x009, 0x060, 0x012, 0x042, 0x021, 0x024, 0x030, 0x048, | |
32 0x00c, 0x018, 0x045, 0x051, 0x054, 0x015, 0x01A, 0x029, 0x00B, 0x00E, | |
33 0x01A, 0x029 | |
34 }; | |
35 const FX_INT32 CBC_OnedCodaBarReader::minCharacterLength = 3; | |
36 const FX_CHAR CBC_OnedCodaBarReader::STARTEND_ENCODING[8] = {'E', '*', 'A', 'B',
'C', 'D', 'T', 'N'}; | |
37 CBC_OnedCodaBarReader::CBC_OnedCodaBarReader() | |
38 { | |
39 } | |
40 CBC_OnedCodaBarReader::~CBC_OnedCodaBarReader() | |
41 { | |
42 } | |
43 CFX_ByteString CBC_OnedCodaBarReader::DecodeRow(FX_INT32 rowNumber, CBC_CommonBi
tArray *row, FX_INT32 hints, FX_INT32 &e) | |
44 { | |
45 CFX_Int32Array *int32Ptr = FindAsteriskPattern(row, e); | |
46 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
47 CBC_AutoPtr<CFX_Int32Array> start(int32Ptr); | |
48 (*start)[1] = 0; | |
49 FX_INT32 nextStart = (*start)[1]; | |
50 FX_INT32 end = row->GetSize(); | |
51 while (nextStart < end && !row->Get(nextStart)) { | |
52 nextStart++; | |
53 } | |
54 CFX_ByteString result; | |
55 CFX_Int32Array counters; | |
56 counters.SetSize(7); | |
57 FX_CHAR decodedChar; | |
58 FX_INT32 lastStart; | |
59 do { | |
60 RecordPattern(row, nextStart, &counters, e); | |
61 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
62 decodedChar = ToNarrowWidePattern(&counters); | |
63 if (decodedChar == '!') { | |
64 e = BCExceptionNotFound; | |
65 return ""; | |
66 } | |
67 result += decodedChar; | |
68 lastStart = nextStart; | |
69 for (FX_INT32 i = 0; i < counters.GetSize(); i++) { | |
70 nextStart += counters[i]; | |
71 } | |
72 while (nextStart < end && !row->Get(nextStart)) { | |
73 nextStart++; | |
74 } | |
75 } while (nextStart < end); | |
76 FX_INT32 lastPatternSize = 0; | |
77 for (FX_INT32 j = 0; j < counters.GetSize(); j++) { | |
78 lastPatternSize += counters[j]; | |
79 } | |
80 FX_INT32 whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize; | |
81 if (nextStart != end && (whiteSpaceAfterEnd / 2 < lastPatternSize)) { | |
82 e = BCExceptionNotFound; | |
83 return ""; | |
84 } | |
85 if (result.GetLength() < 2) { | |
86 e = BCExceptionNotFound; | |
87 return ""; | |
88 } | |
89 FX_CHAR startchar = result[0]; | |
90 if (!ArrayContains(STARTEND_ENCODING, startchar)) { | |
91 e = BCExceptionNotFound; | |
92 return ""; | |
93 } | |
94 FX_INT32 len = result.GetLength(); | |
95 CFX_ByteString temp = result; | |
96 for (FX_INT32 k = 1; k < result.GetLength(); k++) { | |
97 if (ArrayContains(STARTEND_ENCODING, result[k])) { | |
98 if ((k + 1) != result.GetLength()) { | |
99 result.Delete(1, k); | |
100 k = 1; | |
101 } | |
102 } | |
103 } | |
104 if (result.GetLength() < 5) { | |
105 FX_INT32 index = temp.Find(result.Mid(1, result.GetLength() - 1)); | |
106 if (index == len - (result.GetLength() - 1)) { | |
107 e = BCExceptionNotFound; | |
108 return ""; | |
109 } | |
110 } | |
111 if (result.GetLength() > minCharacterLength) { | |
112 result = result.Mid(1, result.GetLength() - 2); | |
113 } else { | |
114 e = BCExceptionNotFound; | |
115 return ""; | |
116 } | |
117 return result; | |
118 } | |
119 CFX_Int32Array *CBC_OnedCodaBarReader::FindAsteriskPattern(CBC_CommonBitArray *r
ow, FX_INT32 &e) | |
120 { | |
121 FX_INT32 width = row->GetSize(); | |
122 FX_INT32 rowOffset = 0; | |
123 while (rowOffset < width) { | |
124 if (row->Get(rowOffset)) { | |
125 break; | |
126 } | |
127 rowOffset++; | |
128 } | |
129 FX_INT32 counterPosition = 0; | |
130 CFX_Int32Array counters; | |
131 counters.SetSize(7); | |
132 FX_INT32 patternStart = rowOffset; | |
133 FX_BOOL isWhite = FALSE; | |
134 FX_INT32 patternLength = counters.GetSize(); | |
135 for (FX_INT32 i = rowOffset; i < width; i++) { | |
136 FX_BOOL pixel = row->Get(i); | |
137 if (pixel ^ isWhite) { | |
138 counters[counterPosition]++; | |
139 } else { | |
140 if (counterPosition == patternLength - 1) { | |
141 if (ArrayContains(STARTEND_ENCODING, ToNarrowWidePattern(&counte
rs))) { | |
142 FX_BOOL btemp3 = row->IsRange(FX_MAX(0, patternStart - (i -
patternStart) / 2), patternStart, FALSE, e); | |
143 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
144 if (btemp3) { | |
145 CFX_Int32Array *result = FX_NEW CFX_Int32Array(); | |
146 result->SetSize(2); | |
147 (*result)[0] = patternStart; | |
148 (*result)[1] = i; | |
149 return result; | |
150 } | |
151 } | |
152 patternStart += counters[0] + counters[1]; | |
153 for (FX_INT32 y = 2; y < patternLength; y++) { | |
154 counters[y - 2] = counters[y]; | |
155 } | |
156 counters[patternLength - 2] = 0; | |
157 counters[patternLength - 1] = 0; | |
158 counterPosition--; | |
159 } else { | |
160 counterPosition++; | |
161 } | |
162 counters[counterPosition] = 1; | |
163 isWhite = !isWhite; | |
164 } | |
165 } | |
166 e = BCExceptionNotFound; | |
167 return NULL; | |
168 } | |
169 FX_BOOL CBC_OnedCodaBarReader::ArrayContains(const FX_CHAR array[], FX_CHAR key) | |
170 { | |
171 for(FX_INT32 i = 0; i < 8; i++) { | |
172 if(array[i] == key) { | |
173 return TRUE; | |
174 } | |
175 } | |
176 return FALSE; | |
177 } | |
178 FX_CHAR CBC_OnedCodaBarReader::ToNarrowWidePattern(CFX_Int32Array *counter) | |
179 { | |
180 FX_INT32 numCounters = counter->GetSize(); | |
181 if (numCounters < 1) { | |
182 return '!'; | |
183 } | |
184 FX_INT32 averageCounter = 0; | |
185 FX_INT32 totalCounters = 0; | |
186 for (FX_INT32 i = 0; i < numCounters; i++) { | |
187 totalCounters += (*counter)[i]; | |
188 } | |
189 averageCounter = totalCounters / numCounters; | |
190 FX_INT32 pattern = 0; | |
191 FX_INT32 wideCounters = 0; | |
192 for (FX_INT32 j = 0; j < numCounters; j++) { | |
193 if ((*counter)[j] > averageCounter) { | |
194 pattern |= 1 << (numCounters - 1 - j); | |
195 wideCounters++; | |
196 } | |
197 } | |
198 if ((wideCounters == 2) || (wideCounters == 3)) { | |
199 for (FX_INT32 k = 0; k < 22; k++) { | |
200 if (CHARACTER_ENCODINGS[k] == pattern) { | |
201 return (ALPHABET_STRING)[k]; | |
202 } | |
203 } | |
204 } | |
205 return '!'; | |
206 } | |
OLD | NEW |