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_CommonBitArray.h" | |
27 #include "include/BC_OneDimReader.h" | |
28 const FX_INT32 CBC_OneDimReader::MAX_AVG_VARIANCE = (FX_INT32)(256 * 0.48f); | |
29 const FX_INT32 CBC_OneDimReader::MAX_INDIVIDUAL_VARIANCE = (FX_INT32)(256 * 0.7f
); | |
30 const FX_INT32 CBC_OneDimReader::START_END_PATTERN[3] = {1, 1, 1}; | |
31 const FX_INT32 CBC_OneDimReader::MIDDLE_PATTERN[5] = {1, 1, 1, 1, 1}; | |
32 const FX_INT32 CBC_OneDimReader::L_PATTERNS[10][4] = { | |
33 {3, 2, 1, 1}, | |
34 {2, 2, 2, 1}, | |
35 {2, 1, 2, 2}, | |
36 {1, 4, 1, 1}, | |
37 {1, 1, 3, 2}, | |
38 {1, 2, 3, 1}, | |
39 {1, 1, 1, 4}, | |
40 {1, 3, 1, 2}, | |
41 {1, 2, 1, 3}, | |
42 {3, 1, 1, 2} | |
43 }; | |
44 const FX_INT32 CBC_OneDimReader::L_AND_G_PATTERNS[20][4] = { | |
45 {3, 2, 1, 1}, | |
46 {2, 2, 2, 1}, | |
47 {2, 1, 2, 2}, | |
48 {1, 4, 1, 1}, | |
49 {1, 1, 3, 2}, | |
50 {1, 2, 3, 1}, | |
51 {1, 1, 1, 4}, | |
52 {1, 3, 1, 2}, | |
53 {1, 2, 1, 3}, | |
54 {3, 1, 1, 2}, | |
55 {1, 1, 2, 3}, | |
56 {1, 2, 2, 2}, | |
57 {2, 2, 1, 2}, | |
58 {1, 1, 4, 1}, | |
59 {2, 3, 1, 1}, | |
60 {1, 3, 2, 1}, | |
61 {4, 1, 1, 1}, | |
62 {2, 1, 3, 1}, | |
63 {3, 1, 2, 1}, | |
64 {2, 1, 1, 3} | |
65 }; | |
66 CBC_OneDimReader::CBC_OneDimReader() | |
67 { | |
68 } | |
69 CBC_OneDimReader::~CBC_OneDimReader() | |
70 { | |
71 } | |
72 CFX_Int32Array *CBC_OneDimReader::FindStartGuardPattern(CBC_CommonBitArray *row,
FX_INT32 &e) | |
73 { | |
74 FX_BOOL foundStart = FALSE; | |
75 CFX_Int32Array *startRange = NULL; | |
76 CFX_Int32Array startEndPattern; | |
77 startEndPattern.SetSize(3); | |
78 startEndPattern[0] = START_END_PATTERN[0]; | |
79 startEndPattern[1] = START_END_PATTERN[1]; | |
80 startEndPattern[2] = START_END_PATTERN[2]; | |
81 FX_INT32 nextStart = 0; | |
82 while (!foundStart) { | |
83 if(startRange != NULL) { | |
84 delete startRange; | |
85 startRange = NULL; | |
86 } | |
87 startRange = FindGuardPattern(row, nextStart, FALSE, &startEndPattern, e
); | |
88 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
89 FX_INT32 start = (*startRange)[0]; | |
90 nextStart = (*startRange)[1]; | |
91 if (start <= 1) { | |
92 break; | |
93 } | |
94 FX_INT32 quietStart = start - (nextStart - start); | |
95 if (quietStart >= 0) { | |
96 FX_BOOL booT = row->IsRange(quietStart, start, FALSE, e); | |
97 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
98 foundStart = booT; | |
99 } | |
100 } | |
101 return startRange; | |
102 } | |
103 CFX_ByteString CBC_OneDimReader::DecodeRow(FX_INT32 rowNumber, CBC_CommonBitArra
y *row, FX_INT32 hints, FX_INT32 &e) | |
104 { | |
105 CFX_Int32Array* StartPattern = FindStartGuardPattern(row, e); | |
106 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
107 CBC_AutoPtr<CFX_Int32Array > result(StartPattern); | |
108 CFX_ByteString temp = DecodeRow(rowNumber, row, result.get(), hints, e); | |
109 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
110 return temp; | |
111 } | |
112 CFX_ByteString CBC_OneDimReader::DecodeRow(FX_INT32 rowNumber, CBC_CommonBitArra
y *row, CFX_Int32Array *startGuardRange, FX_INT32 hints, FX_INT32 &e) | |
113 { | |
114 CFX_ByteString result; | |
115 FX_INT32 endStart = DecodeMiddle(row, startGuardRange, result, e); | |
116 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
117 FX_BOOL b = CheckChecksum(result, e); | |
118 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
119 if (!b) { | |
120 e = BCExceptionChecksumException; | |
121 return ""; | |
122 } | |
123 return result; | |
124 } | |
125 FX_BOOL CBC_OneDimReader::CheckChecksum(CFX_ByteString &s, FX_INT32 &e) | |
126 { | |
127 FX_BOOL temp = CheckStandardUPCEANChecksum(s, e); | |
128 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); | |
129 return temp; | |
130 } | |
131 FX_BOOL CBC_OneDimReader::CheckStandardUPCEANChecksum(CFX_ByteString &s, FX_INT3
2 &e) | |
132 { | |
133 FX_INT32 length = s.GetLength(); | |
134 if (length == 0) { | |
135 return FALSE; | |
136 } | |
137 FX_INT32 sum = 0; | |
138 for (FX_INT32 i = length - 2; i >= 0; i -= 2) { | |
139 FX_INT32 digit = (FX_INT32) s[i] - (FX_INT32) '0'; | |
140 if (digit < 0 || digit > 9) { | |
141 e = BCExceptionFormatException; | |
142 return FALSE; | |
143 } | |
144 sum += digit; | |
145 } | |
146 sum *= 3; | |
147 for (FX_INT32 j = length - 1; j >= 0; j -= 2) { | |
148 FX_INT32 digit = (FX_INT32) s[j] - (FX_INT32) '0'; | |
149 if (digit < 0 || digit > 9) { | |
150 e = BCExceptionFormatException; | |
151 return FALSE; | |
152 } | |
153 sum += digit; | |
154 } | |
155 return sum % 10 == 0; | |
156 } | |
157 CFX_Int32Array *CBC_OneDimReader::DecodeEnd(CBC_CommonBitArray* row, FX_INT32 en
dStart, FX_INT32 &e) | |
158 { | |
159 CFX_Int32Array startEndPattern; | |
160 startEndPattern.Add(START_END_PATTERN[0]); | |
161 startEndPattern.Add(START_END_PATTERN[1]); | |
162 startEndPattern.Add(START_END_PATTERN[2]); | |
163 CFX_Int32Array* FindGuard = FindGuardPattern(row, endStart, FALSE, &startEnd
Pattern, e); | |
164 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
165 return FindGuard; | |
166 } | |
167 CFX_Int32Array *CBC_OneDimReader::FindGuardPattern(CBC_CommonBitArray *row, FX_I
NT32 rowOffset, FX_BOOL whiteFirst, CFX_Int32Array *pattern, FX_INT32 &e) | |
168 { | |
169 FX_INT32 patternLength = pattern->GetSize(); | |
170 CFX_Int32Array counters; | |
171 counters.SetSize(patternLength); | |
172 FX_INT32 width = row->GetSize(); | |
173 FX_BOOL isWhite = FALSE; | |
174 while (rowOffset < width) { | |
175 isWhite = !row->Get(rowOffset); | |
176 if (whiteFirst == isWhite) { | |
177 break; | |
178 } | |
179 rowOffset++; | |
180 } | |
181 FX_INT32 counterPosition = 0; | |
182 FX_INT32 patternStart = rowOffset; | |
183 for (FX_INT32 x = rowOffset; x < width; x++) { | |
184 FX_BOOL pixel = row->Get(x); | |
185 if (pixel ^ isWhite) { | |
186 counters[counterPosition]++; | |
187 } else { | |
188 if (counterPosition == patternLength - 1) { | |
189 if (PatternMatchVariance(&counters, &(*pattern)[0], MAX_INDIVIDU
AL_VARIANCE) < MAX_AVG_VARIANCE) { | |
190 CFX_Int32Array *result = FX_NEW CFX_Int32Array(); | |
191 result->SetSize(2); | |
192 (*result)[0] = patternStart; | |
193 (*result)[1] = x; | |
194 return result; | |
195 } | |
196 patternStart += counters[0] + counters[1]; | |
197 for (FX_INT32 y = 2; y < patternLength; y++) { | |
198 counters[y - 2] = counters[y]; | |
199 } | |
200 counters[patternLength - 2] = 0; | |
201 counters[patternLength - 1] = 0; | |
202 counterPosition--; | |
203 } else { | |
204 counterPosition++; | |
205 } | |
206 counters[counterPosition] = 1; | |
207 isWhite = !isWhite; | |
208 } | |
209 } | |
210 e = BCExceptionNotFound; | |
211 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
212 return NULL; | |
213 } | |
214 FX_INT32 CBC_OneDimReader::DecodeDigit(CBC_CommonBitArray *row, CFX_Int32Array *
counters, FX_INT32 rowOffset, const FX_INT32* patterns, FX_INT32 patternLength,
FX_INT32 &e) | |
215 { | |
216 RecordPattern(row, rowOffset, counters, e); | |
217 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | |
218 FX_INT32 bestVariance = MAX_AVG_VARIANCE; | |
219 FX_INT32 bestMatch = -1; | |
220 FX_INT32 max = patternLength; | |
221 for (FX_INT32 i = 0; i < max; i++) { | |
222 FX_INT32 variance = PatternMatchVariance(counters, &patterns[i * 4], MAX
_INDIVIDUAL_VARIANCE); | |
223 if (variance < bestVariance) { | |
224 bestVariance = variance; | |
225 bestMatch = i; | |
226 } | |
227 } | |
228 if (bestMatch >= 0) { | |
229 return bestMatch; | |
230 } else { | |
231 e = BCExceptionNotFound; | |
232 return 0; | |
233 } | |
234 return 0; | |
235 } | |
OLD | NEW |