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