| 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 2007 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/BC_ResultPoint.h" | |
| 24 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h" | |
| 25 #include "xfa/src/fxbarcode/qrcode/BC_QRAlignmentPattern.h" | |
| 26 #include "xfa/src/fxbarcode/qrcode/BC_QRAlignmentPatternFinder.h" | |
| 27 | |
| 28 CBC_QRAlignmentPatternFinder::CBC_QRAlignmentPatternFinder( | |
| 29 CBC_CommonBitMatrix* image, | |
| 30 int32_t startX, | |
| 31 int32_t startY, | |
| 32 int32_t width, | |
| 33 int32_t height, | |
| 34 FX_FLOAT moduleSize) | |
| 35 : m_image(image), | |
| 36 m_startX(startX), | |
| 37 m_startY(startY), | |
| 38 m_width(width), | |
| 39 m_height(height), | |
| 40 m_moduleSize(moduleSize) { | |
| 41 m_crossCheckStateCount.SetSize(3); | |
| 42 } | |
| 43 CBC_QRAlignmentPatternFinder::~CBC_QRAlignmentPatternFinder() { | |
| 44 for (int32_t i = 0; i < m_possibleCenters.GetSize(); i++) { | |
| 45 delete (CBC_QRAlignmentPattern*)m_possibleCenters[i]; | |
| 46 } | |
| 47 m_possibleCenters.RemoveAll(); | |
| 48 } | |
| 49 CBC_QRAlignmentPattern* CBC_QRAlignmentPatternFinder::Find(int32_t& e) { | |
| 50 int32_t startX = m_startX; | |
| 51 int32_t height = m_height; | |
| 52 int32_t maxJ = startX + m_width; | |
| 53 int32_t middleI = m_startY + (height >> 1); | |
| 54 CFX_Int32Array stateCount; | |
| 55 stateCount.SetSize(3); | |
| 56 for (int32_t iGen = 0; iGen < height; iGen++) { | |
| 57 int32_t i = | |
| 58 middleI + ((iGen & 0x01) == 0 ? ((iGen + 1) >> 1) : -((iGen + 1) >> 1)); | |
| 59 stateCount[0] = 0; | |
| 60 stateCount[1] = 0; | |
| 61 stateCount[2] = 0; | |
| 62 int32_t j = startX; | |
| 63 while (j < maxJ && !m_image->Get(j, i)) { | |
| 64 j++; | |
| 65 } | |
| 66 int32_t currentState = 0; | |
| 67 while (j < maxJ) { | |
| 68 if (m_image->Get(j, i)) { | |
| 69 if (currentState == 1) { | |
| 70 stateCount[currentState]++; | |
| 71 } else { | |
| 72 if (currentState == 2) { | |
| 73 if (FoundPatternCross(stateCount)) { | |
| 74 CBC_QRAlignmentPattern* confirmed = | |
| 75 HandlePossibleCenter(stateCount, i, j); | |
| 76 if (confirmed) { | |
| 77 return confirmed; | |
| 78 } | |
| 79 } | |
| 80 stateCount[0] = stateCount[2]; | |
| 81 stateCount[1] = 1; | |
| 82 stateCount[2] = 0; | |
| 83 currentState = 1; | |
| 84 } else { | |
| 85 stateCount[++currentState]++; | |
| 86 } | |
| 87 } | |
| 88 } else { | |
| 89 if (currentState == 1) { | |
| 90 currentState++; | |
| 91 } | |
| 92 stateCount[currentState]++; | |
| 93 } | |
| 94 j++; | |
| 95 } | |
| 96 if (FoundPatternCross(stateCount)) { | |
| 97 CBC_QRAlignmentPattern* confirmed = | |
| 98 HandlePossibleCenter(stateCount, i, maxJ); | |
| 99 if (confirmed) { | |
| 100 return confirmed; | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 if (m_possibleCenters.GetSize() != 0) { | |
| 105 return ((CBC_QRAlignmentPattern*)(m_possibleCenters[0]))->Clone(); | |
| 106 } | |
| 107 e = BCExceptionRead; | |
| 108 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
| 109 return NULL; | |
| 110 } | |
| 111 FX_FLOAT CBC_QRAlignmentPatternFinder::CenterFromEnd( | |
| 112 const CFX_Int32Array& stateCount, | |
| 113 int32_t end) { | |
| 114 return (FX_FLOAT)(end - stateCount[2]) - stateCount[1] / 2.0f; | |
| 115 } | |
| 116 FX_BOOL CBC_QRAlignmentPatternFinder::FoundPatternCross( | |
| 117 const CFX_Int32Array& stateCount) { | |
| 118 FX_FLOAT moduleSize = m_moduleSize; | |
| 119 FX_FLOAT maxVariance = moduleSize / 2.0f; | |
| 120 for (int32_t i = 0; i < 3; i++) { | |
| 121 if (fabs(moduleSize - stateCount[i]) >= maxVariance) { | |
| 122 return false; | |
| 123 } | |
| 124 } | |
| 125 return TRUE; | |
| 126 } | |
| 127 FX_FLOAT CBC_QRAlignmentPatternFinder::CrossCheckVertical( | |
| 128 int32_t startI, | |
| 129 int32_t centerJ, | |
| 130 int32_t maxCount, | |
| 131 int32_t originalStateCountTotal) { | |
| 132 int32_t maxI = m_image->GetHeight(); | |
| 133 CFX_Int32Array stateCount; | |
| 134 stateCount.Copy(m_crossCheckStateCount); | |
| 135 stateCount[0] = 0; | |
| 136 stateCount[1] = 0; | |
| 137 stateCount[2] = 0; | |
| 138 int32_t i = startI; | |
| 139 while (i >= 0 && m_image->Get(centerJ, i) && stateCount[1] <= maxCount) { | |
| 140 stateCount[1]++; | |
| 141 i--; | |
| 142 } | |
| 143 if (i < 0 || stateCount[1] > maxCount) { | |
| 144 return FXSYS_nan(); | |
| 145 } | |
| 146 while (i >= 0 && !m_image->Get(centerJ, i) && stateCount[0] <= maxCount) { | |
| 147 stateCount[0]++; | |
| 148 i--; | |
| 149 } | |
| 150 if (stateCount[0] > maxCount) { | |
| 151 return FXSYS_nan(); | |
| 152 } | |
| 153 i = startI + 1; | |
| 154 while (i < maxI && m_image->Get(centerJ, i) && stateCount[1] <= maxCount) { | |
| 155 stateCount[1]++; | |
| 156 i++; | |
| 157 } | |
| 158 if (i == maxI || stateCount[1] > maxCount) { | |
| 159 return FXSYS_nan(); | |
| 160 } | |
| 161 while (i < maxI && !m_image->Get(centerJ, i) && stateCount[2] <= maxCount) { | |
| 162 stateCount[2]++; | |
| 163 i++; | |
| 164 } | |
| 165 if (stateCount[2] > maxCount) { | |
| 166 return FXSYS_nan(); | |
| 167 } | |
| 168 int32_t stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; | |
| 169 if (5 * abs(stateCountTotal - originalStateCountTotal) >= | |
| 170 originalStateCountTotal) { | |
| 171 return FXSYS_nan(); | |
| 172 } | |
| 173 return FoundPatternCross(stateCount) ? CenterFromEnd(stateCount, i) | |
| 174 : FXSYS_nan(); | |
| 175 } | |
| 176 CBC_QRAlignmentPattern* CBC_QRAlignmentPatternFinder::HandlePossibleCenter( | |
| 177 const CFX_Int32Array& stateCount, | |
| 178 int32_t i, | |
| 179 int32_t j) { | |
| 180 int32_t stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; | |
| 181 FX_FLOAT centerJ = CenterFromEnd(stateCount, j); | |
| 182 FX_FLOAT centerI = CrossCheckVertical(i, (int32_t)centerJ, 2 * stateCount[1], | |
| 183 stateCountTotal); | |
| 184 if (!FXSYS_isnan(centerI)) { | |
| 185 FX_FLOAT estimatedModuleSize = | |
| 186 (FX_FLOAT)(stateCount[0] + stateCount[1] + stateCount[2]) / 3.0f; | |
| 187 int32_t max = m_possibleCenters.GetSize(); | |
| 188 for (int32_t index = 0; index < max; index++) { | |
| 189 CBC_QRAlignmentPattern* center = | |
| 190 (CBC_QRAlignmentPattern*)(m_possibleCenters[index]); | |
| 191 if (center->AboutEquals(estimatedModuleSize, centerI, centerJ)) { | |
| 192 return new CBC_QRAlignmentPattern(centerJ, centerI, | |
| 193 estimatedModuleSize); | |
| 194 } | |
| 195 } | |
| 196 m_possibleCenters.Add( | |
| 197 new CBC_QRAlignmentPattern(centerJ, centerI, estimatedModuleSize)); | |
| 198 } | |
| 199 return NULL; | |
| 200 } | |
| OLD | NEW |