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