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