| 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 <algorithm> | |
| 24 #include <memory> | |
| 25 | |
| 26 #include "xfa/src/fxbarcode/BC_BinaryBitmap.h" | |
| 27 #include "xfa/src/fxbarcode/BC_Reader.h" | |
| 28 #include "xfa/src/fxbarcode/common/BC_CommonBitArray.h" | |
| 29 #include "xfa/src/fxbarcode/oned/BC_OneDReader.h" | |
| 30 #include "xfa/src/fxbarcode/utils.h" | |
| 31 | |
| 32 CBC_OneDReader::CBC_OneDReader() {} | |
| 33 CBC_OneDReader::~CBC_OneDReader() {} | |
| 34 CFX_ByteString CBC_OneDReader::Decode(CBC_BinaryBitmap* image, int32_t& e) { | |
| 35 CFX_ByteString strtemp = Decode(image, 0, e); | |
| 36 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
| 37 return strtemp; | |
| 38 } | |
| 39 CFX_ByteString CBC_OneDReader::Decode(CBC_BinaryBitmap* image, | |
| 40 int32_t hints, | |
| 41 int32_t& e) { | |
| 42 CFX_ByteString strtemp = DeDecode(image, hints, e); | |
| 43 BC_EXCEPTION_CHECK_ReturnValue(e, ""); | |
| 44 return strtemp; | |
| 45 } | |
| 46 CFX_ByteString CBC_OneDReader::DeDecode(CBC_BinaryBitmap* image, | |
| 47 int32_t hints, | |
| 48 int32_t& e) { | |
| 49 int32_t height = image->GetHeight(); | |
| 50 int32_t middle = height >> 1; | |
| 51 FX_BOOL tryHarder = FALSE; | |
| 52 int32_t rowStep = std::max(1, height >> (tryHarder ? 8 : 5)); | |
| 53 int32_t maxLines; | |
| 54 if (tryHarder) { | |
| 55 maxLines = height; | |
| 56 } else { | |
| 57 maxLines = 15; | |
| 58 } | |
| 59 for (int32_t x = 0; x < maxLines; x++) { | |
| 60 int32_t rowStepsAboveOrBelow = (x + 1) >> 1; | |
| 61 const bool isAbove = (x & 0x01) == 0; | |
| 62 int32_t rowNumber = | |
| 63 middle + | |
| 64 rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow); | |
| 65 if (rowNumber < 0 || rowNumber >= height) { | |
| 66 break; | |
| 67 } | |
| 68 std::unique_ptr<CBC_CommonBitArray> row( | |
| 69 image->GetBlackRow(rowNumber, nullptr, e)); | |
| 70 if (e != BCExceptionNO) { | |
| 71 e = BCExceptionNO; | |
| 72 continue; | |
| 73 } | |
| 74 for (int32_t attempt = 0; attempt < 2; attempt++) { | |
| 75 if (attempt == 1) { | |
| 76 row->Reverse(); | |
| 77 } | |
| 78 CFX_ByteString result = DecodeRow(rowNumber, row.get(), hints, e); | |
| 79 if (e != BCExceptionNO) { | |
| 80 e = BCExceptionNO; | |
| 81 continue; | |
| 82 } | |
| 83 return result; | |
| 84 } | |
| 85 } | |
| 86 e = BCExceptionNotFound; | |
| 87 return ""; | |
| 88 } | |
| 89 void CBC_OneDReader::RecordPattern(CBC_CommonBitArray* row, | |
| 90 int32_t start, | |
| 91 CFX_Int32Array* counters, | |
| 92 int32_t& e) { | |
| 93 int32_t numCounters = counters->GetSize(); | |
| 94 for (int32_t i = 0; i < numCounters; i++) { | |
| 95 (*counters)[i] = 0; | |
| 96 } | |
| 97 int32_t end = row->GetSize(); | |
| 98 if (start >= end) { | |
| 99 e = BCExceptionNotFound; | |
| 100 return; | |
| 101 } | |
| 102 FX_BOOL isWhite = !row->Get(start); | |
| 103 int32_t counterPosition = 0; | |
| 104 int32_t j = start; | |
| 105 while (j < end) { | |
| 106 FX_BOOL pixel = row->Get(j); | |
| 107 if (pixel ^ isWhite) { | |
| 108 (*counters)[counterPosition]++; | |
| 109 } else { | |
| 110 counterPosition++; | |
| 111 if (counterPosition == numCounters) { | |
| 112 break; | |
| 113 } else { | |
| 114 (*counters)[counterPosition] = 1; | |
| 115 isWhite = !isWhite; | |
| 116 } | |
| 117 } | |
| 118 j++; | |
| 119 } | |
| 120 if (!(counterPosition == numCounters || | |
| 121 (counterPosition == numCounters - 1 && j == end))) { | |
| 122 e = BCExceptionNotFound; | |
| 123 return; | |
| 124 } | |
| 125 } | |
| 126 void CBC_OneDReader::RecordPatternInReverse(CBC_CommonBitArray* row, | |
| 127 int32_t start, | |
| 128 CFX_Int32Array* counters, | |
| 129 int32_t& e) { | |
| 130 int32_t numTransitionsLeft = counters->GetSize(); | |
| 131 FX_BOOL last = row->Get(start); | |
| 132 while (start > 0 && numTransitionsLeft >= 0) { | |
| 133 if (row->Get(--start) != last) { | |
| 134 numTransitionsLeft--; | |
| 135 last = !last; | |
| 136 } | |
| 137 } | |
| 138 if (numTransitionsLeft >= 0) { | |
| 139 e = BCExceptionNotFound; | |
| 140 return; | |
| 141 } | |
| 142 RecordPattern(row, start + 1, counters, e); | |
| 143 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
| 144 } | |
| 145 int32_t CBC_OneDReader::PatternMatchVariance(CFX_Int32Array* counters, | |
| 146 const int32_t* pattern, | |
| 147 int32_t maxIndividualVariance) { | |
| 148 int32_t numCounters = counters->GetSize(); | |
| 149 int32_t total = 0; | |
| 150 int32_t patternLength = 0; | |
| 151 for (int32_t i = 0; i < numCounters; i++) { | |
| 152 total += (*counters)[i]; | |
| 153 patternLength += pattern[i]; | |
| 154 } | |
| 155 if (total < patternLength) { | |
| 156 #undef max | |
| 157 return FXSYS_IntMax; | |
| 158 } | |
| 159 int32_t unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength; | |
| 160 maxIndividualVariance = | |
| 161 (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT; | |
| 162 int32_t totalVariance = 0; | |
| 163 for (int32_t x = 0; x < numCounters; x++) { | |
| 164 int32_t counter = (*counters)[x] << INTEGER_MATH_SHIFT; | |
| 165 int32_t scaledPattern = pattern[x] * unitBarWidth; | |
| 166 int32_t variance = counter > scaledPattern ? counter - scaledPattern | |
| 167 : scaledPattern - counter; | |
| 168 if (variance > maxIndividualVariance) { | |
| 169 #undef max | |
| 170 return FXSYS_IntMax; | |
| 171 } | |
| 172 totalVariance += variance; | |
| 173 } | |
| 174 return totalVariance / total; | |
| 175 } | |
| OLD | NEW |