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