Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(707)

Side by Side Diff: xfa/src/fxbarcode/qrcode/BC_QRFinderPatternFinder.cpp

Issue 1803723002: Move xfa/src up to xfa/. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Rebase to master Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/qrcode/BC_QRFinderPatternFinder.h"
24
25 #include <memory>
26
27 #include "core/include/fxcrt/fx_basic.h"
28 #include "xfa/src/fxbarcode/BC_ResultPoint.h"
29 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
30 #include "xfa/src/fxbarcode/qrcode/BC_FinderPatternInfo.h"
31 #include "xfa/src/fxbarcode/qrcode/BC_QRFinderPattern.h"
32 #include "xfa/src/fxbarcode/utils.h"
33
34 const int32_t CBC_QRFinderPatternFinder::CENTER_QUORUM = 2;
35 const int32_t CBC_QRFinderPatternFinder::MIN_SKIP = 3;
36 const int32_t CBC_QRFinderPatternFinder::MAX_MODULES = 57;
37 const int32_t CBC_QRFinderPatternFinder::INTEGER_MATH_SHIFT = 8;
38
39 CBC_QRFinderPatternFinder::CBC_QRFinderPatternFinder(
40 CBC_CommonBitMatrix* image) {
41 m_image = image;
42 m_crossCheckStateCount.SetSize(5);
43 m_hasSkipped = FALSE;
44 }
45 CBC_QRFinderPatternFinder::~CBC_QRFinderPatternFinder() {
46 for (int32_t i = 0; i < m_possibleCenters.GetSize(); i++) {
47 delete (CBC_QRFinderPattern*)m_possibleCenters[i];
48 }
49 m_possibleCenters.RemoveAll();
50 }
51 class ClosestToAverageComparator {
52 private:
53 FX_FLOAT m_averageModuleSize;
54
55 public:
56 ClosestToAverageComparator(FX_FLOAT averageModuleSize)
57 : m_averageModuleSize(averageModuleSize) {}
58 int32_t operator()(FinderPattern* a, FinderPattern* b) {
59 FX_FLOAT dA =
60 (FX_FLOAT)fabs(a->GetEstimatedModuleSize() - m_averageModuleSize);
61 FX_FLOAT dB =
62 (FX_FLOAT)fabs(b->GetEstimatedModuleSize() - m_averageModuleSize);
63 return dA < dB ? -1 : dA > dB ? 1 : 0;
64 }
65 };
66 class CenterComparator {
67 public:
68 int32_t operator()(FinderPattern* a, FinderPattern* b) {
69 return b->GetCount() - a->GetCount();
70 }
71 };
72 CBC_CommonBitMatrix* CBC_QRFinderPatternFinder::GetImage() {
73 return m_image;
74 }
75 CFX_Int32Array& CBC_QRFinderPatternFinder::GetCrossCheckStateCount() {
76 m_crossCheckStateCount[0] = 0;
77 m_crossCheckStateCount[1] = 0;
78 m_crossCheckStateCount[2] = 0;
79 m_crossCheckStateCount[3] = 0;
80 m_crossCheckStateCount[4] = 0;
81 return m_crossCheckStateCount;
82 }
83 CFX_PtrArray* CBC_QRFinderPatternFinder::GetPossibleCenters() {
84 return &m_possibleCenters;
85 }
86 CBC_QRFinderPatternInfo* CBC_QRFinderPatternFinder::Find(int32_t hint,
87 int32_t& e) {
88 int32_t maxI = m_image->GetHeight();
89 int32_t maxJ = m_image->GetWidth();
90 int32_t iSkip = (3 * maxI) / (4 * MAX_MODULES);
91 if (iSkip < MIN_SKIP || 0) {
92 iSkip = MIN_SKIP;
93 }
94 FX_BOOL done = FALSE;
95 CFX_Int32Array stateCount;
96 stateCount.SetSize(5);
97 for (int32_t i = iSkip - 1; i < maxI && !done; i += iSkip) {
98 stateCount[0] = 0;
99 stateCount[1] = 0;
100 stateCount[2] = 0;
101 stateCount[3] = 0;
102 stateCount[4] = 0;
103 int32_t currentState = 0;
104 for (int32_t j = 0; j < maxJ; j++) {
105 if (m_image->Get(j, i)) {
106 if ((currentState & 1) == 1) {
107 currentState++;
108 }
109 stateCount[currentState]++;
110 } else {
111 if ((currentState & 1) == 0) {
112 if (currentState == 4) {
113 if (FoundPatternCross(stateCount)) {
114 FX_BOOL confirmed = HandlePossibleCenter(stateCount, i, j);
115 if (confirmed) {
116 iSkip = 2;
117 if (m_hasSkipped) {
118 done = HaveMultiplyConfirmedCenters();
119 } else {
120 int32_t rowSkip = FindRowSkip();
121 if (rowSkip > stateCount[2]) {
122 i += rowSkip - stateCount[2] - iSkip;
123 j = maxJ - 1;
124 }
125 }
126 } else {
127 do {
128 j++;
129 } while (j < maxJ && !m_image->Get(j, i));
130 j--;
131 }
132 currentState = 0;
133 stateCount[0] = 0;
134 stateCount[1] = 0;
135 stateCount[2] = 0;
136 stateCount[3] = 0;
137 stateCount[4] = 0;
138 } else {
139 stateCount[0] = stateCount[2];
140 stateCount[1] = stateCount[3];
141 stateCount[2] = stateCount[4];
142 stateCount[3] = 1;
143 stateCount[4] = 0;
144 currentState = 3;
145 }
146 } else {
147 stateCount[++currentState]++;
148 }
149 } else {
150 stateCount[currentState]++;
151 }
152 }
153 }
154 if (FoundPatternCross(stateCount)) {
155 FX_BOOL confirmed = HandlePossibleCenter(stateCount, i, maxJ);
156 if (confirmed) {
157 iSkip = stateCount[0];
158 if (m_hasSkipped) {
159 done = HaveMultiplyConfirmedCenters();
160 }
161 }
162 }
163 }
164 std::unique_ptr<CFX_PtrArray> patternInfo(SelectBestpatterns(e));
165 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
166 OrderBestPatterns(patternInfo.get());
167 return new CBC_QRFinderPatternInfo(patternInfo.get());
168 }
169 void CBC_QRFinderPatternFinder::OrderBestPatterns(CFX_PtrArray* patterns) {
170 FX_FLOAT abDistance = Distance((CBC_ResultPoint*)(*patterns)[0],
171 (CBC_ResultPoint*)(*patterns)[1]);
172 FX_FLOAT bcDistance = Distance((CBC_ResultPoint*)(*patterns)[1],
173 (CBC_ResultPoint*)(*patterns)[2]);
174 FX_FLOAT acDistance = Distance((CBC_ResultPoint*)(*patterns)[0],
175 (CBC_ResultPoint*)(*patterns)[2]);
176 CBC_QRFinderPattern *topLeft, *topRight, *bottomLeft;
177 if (bcDistance >= abDistance && bcDistance >= acDistance) {
178 topLeft = (CBC_QRFinderPattern*)(*patterns)[0];
179 topRight = (CBC_QRFinderPattern*)(*patterns)[1];
180 bottomLeft = (CBC_QRFinderPattern*)(*patterns)[2];
181 } else if (acDistance >= bcDistance && acDistance >= abDistance) {
182 topLeft = (CBC_QRFinderPattern*)(*patterns)[1];
183 topRight = (CBC_QRFinderPattern*)(*patterns)[0];
184 bottomLeft = (CBC_QRFinderPattern*)(*patterns)[2];
185 } else {
186 topLeft = (CBC_QRFinderPattern*)(*patterns)[2];
187 topRight = (CBC_QRFinderPattern*)(*patterns)[0];
188 bottomLeft = (CBC_QRFinderPattern*)(*patterns)[1];
189 }
190 if ((bottomLeft->GetY() - topLeft->GetY()) *
191 (topRight->GetX() - topLeft->GetX()) <
192 (bottomLeft->GetX() - topLeft->GetX()) *
193 (topRight->GetY() - topLeft->GetY())) {
194 CBC_QRFinderPattern* temp = topRight;
195 topRight = bottomLeft;
196 bottomLeft = temp;
197 }
198 (*patterns)[0] = bottomLeft;
199 (*patterns)[1] = topLeft;
200 (*patterns)[2] = topRight;
201 }
202 FX_FLOAT CBC_QRFinderPatternFinder::Distance(CBC_ResultPoint* point1,
203 CBC_ResultPoint* point2) {
204 FX_FLOAT dx = point1->GetX() - point2->GetX();
205 FX_FLOAT dy = point1->GetY() - point2->GetY();
206 return (FX_FLOAT)FXSYS_sqrt(dx * dx + dy * dy);
207 }
208 FX_FLOAT CBC_QRFinderPatternFinder::CenterFromEnd(
209 const CFX_Int32Array& stateCount,
210 int32_t end) {
211 return (FX_FLOAT)(end - stateCount[4] - stateCount[3]) - stateCount[2] / 2.0f;
212 }
213 FX_BOOL CBC_QRFinderPatternFinder::FoundPatternCross(
214 const CFX_Int32Array& stateCount) {
215 int32_t totalModuleSize = 0;
216 for (int32_t i = 0; i < 5; i++) {
217 int32_t count = stateCount[i];
218 if (count == 0) {
219 return FALSE;
220 }
221 totalModuleSize += count;
222 }
223 if (totalModuleSize < 7) {
224 return FALSE;
225 }
226 int32_t moduleSize = (totalModuleSize << INTEGER_MATH_SHIFT) / 7;
227 int32_t maxVariance = moduleSize / 2;
228 return FXSYS_abs(moduleSize - (stateCount[0] << INTEGER_MATH_SHIFT)) <
229 maxVariance &&
230 FXSYS_abs(moduleSize - (stateCount[1] << INTEGER_MATH_SHIFT)) <
231 maxVariance &&
232 FXSYS_abs(3 * moduleSize - (stateCount[2] << INTEGER_MATH_SHIFT)) <
233 3 * maxVariance &&
234 FXSYS_abs(moduleSize - (stateCount[3] << INTEGER_MATH_SHIFT)) <
235 maxVariance &&
236 FXSYS_abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) <
237 maxVariance;
238 }
239 FX_FLOAT CBC_QRFinderPatternFinder::CrossCheckVertical(
240 int32_t startI,
241 int32_t centerJ,
242 int32_t maxCount,
243 int32_t originalStateCountTotal) {
244 CBC_CommonBitMatrix* image = m_image;
245 int32_t maxI = image->GetHeight();
246 CFX_Int32Array& stateCount = GetCrossCheckStateCount();
247 int32_t i = startI;
248 while (i >= 0 && image->Get(centerJ, i)) {
249 stateCount[2]++;
250 i--;
251 }
252 if (i < 0) {
253 return FXSYS_nan();
254 }
255 while (i >= 0 && !image->Get(centerJ, i) && stateCount[1] <= maxCount) {
256 stateCount[1]++;
257 i--;
258 }
259 if (i < 0 || stateCount[1] > maxCount) {
260 return FXSYS_nan();
261 }
262 while (i >= 0 && image->Get(centerJ, i) && stateCount[0] <= maxCount) {
263 stateCount[0]++;
264 i--;
265 }
266 if (stateCount[0] > maxCount) {
267 return FXSYS_nan();
268 }
269 i = startI + 1;
270 while (i < maxI && image->Get(centerJ, i)) {
271 stateCount[2]++;
272 i++;
273 }
274 if (i == maxI) {
275 return FXSYS_nan();
276 }
277 while (i < maxI && !image->Get(centerJ, i) && stateCount[3] < maxCount) {
278 stateCount[3]++;
279 i++;
280 }
281 if (i == maxI || stateCount[3] >= maxCount) {
282 return FXSYS_nan();
283 }
284 while (i < maxI && image->Get(centerJ, i) && stateCount[4] < maxCount) {
285 stateCount[4]++;
286 i++;
287 }
288 if (stateCount[4] >= maxCount) {
289 return FXSYS_nan();
290 }
291 int32_t stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] +
292 stateCount[3] + stateCount[4];
293 if (5 * FXSYS_abs(stateCountTotal - originalStateCountTotal) >=
294 originalStateCountTotal) {
295 return FXSYS_nan();
296 }
297 return FoundPatternCross(stateCount) ? CenterFromEnd(stateCount, i)
298 : FXSYS_nan();
299 }
300 FX_FLOAT CBC_QRFinderPatternFinder::CrossCheckHorizontal(
301 int32_t startJ,
302 int32_t centerI,
303 int32_t maxCount,
304 int32_t originalStateCountTotal) {
305 CBC_CommonBitMatrix* image = m_image;
306 int32_t maxJ = image->GetWidth();
307 CFX_Int32Array& stateCount = GetCrossCheckStateCount();
308 int32_t j = startJ;
309 while (j >= 0 && image->Get(j, centerI)) {
310 stateCount[2]++;
311 j--;
312 }
313 if (j < 0) {
314 return FXSYS_nan();
315 }
316 while (j >= 0 && !image->Get(j, centerI) && stateCount[1] <= maxCount) {
317 stateCount[1]++;
318 j--;
319 }
320 if (j < 0 || stateCount[1] > maxCount) {
321 return FXSYS_nan();
322 }
323 while (j >= 0 && image->Get(j, centerI) && stateCount[0] <= maxCount) {
324 stateCount[0]++;
325 j--;
326 }
327 if (stateCount[0] > maxCount) {
328 return FXSYS_nan();
329 }
330 j = startJ + 1;
331 while (j < maxJ && image->Get(j, centerI)) {
332 stateCount[2]++;
333 j++;
334 }
335 if (j == maxJ) {
336 return FXSYS_nan();
337 }
338 while (j < maxJ && !image->Get(j, centerI) && stateCount[3] < maxCount) {
339 stateCount[3]++;
340 j++;
341 }
342 if (j == maxJ || stateCount[3] >= maxCount) {
343 return FXSYS_nan();
344 }
345 while (j < maxJ && image->Get(j, centerI) && stateCount[4] < maxCount) {
346 stateCount[4]++;
347 j++;
348 }
349 if (stateCount[4] >= maxCount) {
350 return FXSYS_nan();
351 }
352 int32_t stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] +
353 stateCount[3] + stateCount[4];
354 if (5 * FXSYS_abs(stateCountTotal - originalStateCountTotal) >=
355 originalStateCountTotal) {
356 return FXSYS_nan();
357 }
358 return FoundPatternCross(stateCount) ? CenterFromEnd(stateCount, j)
359 : FXSYS_nan();
360 }
361 FX_BOOL CBC_QRFinderPatternFinder::HandlePossibleCenter(
362 const CFX_Int32Array& stateCount,
363 int32_t i,
364 int32_t j) {
365 int32_t stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] +
366 stateCount[3] + stateCount[4];
367 FX_FLOAT centerJ = CenterFromEnd(stateCount, j);
368 FX_FLOAT centerI =
369 CrossCheckVertical(i, (int32_t)centerJ, stateCount[2], stateCountTotal);
370 if (!FXSYS_isnan(centerI)) {
371 centerJ = CrossCheckHorizontal((int32_t)centerJ, (int32_t)centerI,
372 stateCount[2], stateCountTotal);
373 if (!FXSYS_isnan(centerJ)) {
374 FX_FLOAT estimatedModuleSize = (FX_FLOAT)stateCountTotal / 7.0f;
375 FX_BOOL found = FALSE;
376 int32_t max = m_possibleCenters.GetSize();
377 for (int32_t index = 0; index < max; index++) {
378 CBC_QRFinderPattern* center =
379 (CBC_QRFinderPattern*)(m_possibleCenters[index]);
380 if (center->AboutEquals(estimatedModuleSize, centerI, centerJ)) {
381 center->IncrementCount();
382 found = TRUE;
383 break;
384 }
385 }
386 if (!found) {
387 m_possibleCenters.Add(
388 new CBC_QRFinderPattern(centerJ, centerI, estimatedModuleSize));
389 }
390 return TRUE;
391 }
392 }
393 return FALSE;
394 }
395 int32_t CBC_QRFinderPatternFinder::FindRowSkip() {
396 int32_t max = m_possibleCenters.GetSize();
397 if (max <= 1) {
398 return 0;
399 }
400 FinderPattern* firstConfirmedCenter = NULL;
401 for (int32_t i = 0; i < max; i++) {
402 CBC_QRFinderPattern* center = (CBC_QRFinderPattern*)m_possibleCenters[i];
403 if (center->GetCount() >= CENTER_QUORUM) {
404 if (firstConfirmedCenter == NULL) {
405 firstConfirmedCenter = center;
406 } else {
407 m_hasSkipped = TRUE;
408 return (int32_t)((fabs(firstConfirmedCenter->GetX() - center->GetX()) -
409 fabs(firstConfirmedCenter->GetY() - center->GetY())) /
410 2);
411 }
412 }
413 }
414 return 0;
415 }
416 FX_BOOL CBC_QRFinderPatternFinder::HaveMultiplyConfirmedCenters() {
417 int32_t confirmedCount = 0;
418 FX_FLOAT totalModuleSize = 0.0f;
419 int32_t max = m_possibleCenters.GetSize();
420 int32_t i;
421 for (i = 0; i < max; i++) {
422 CBC_QRFinderPattern* pattern = (CBC_QRFinderPattern*)m_possibleCenters[i];
423 if (pattern->GetCount() >= CENTER_QUORUM) {
424 confirmedCount++;
425 totalModuleSize += pattern->GetEstimatedModuleSize();
426 }
427 }
428 if (confirmedCount < 3) {
429 return FALSE;
430 }
431 FX_FLOAT average = totalModuleSize / (FX_FLOAT)max;
432 FX_FLOAT totalDeviation = 0.0f;
433 for (i = 0; i < max; i++) {
434 CBC_QRFinderPattern* pattern = (CBC_QRFinderPattern*)m_possibleCenters[i];
435 totalDeviation += fabs(pattern->GetEstimatedModuleSize() - average);
436 }
437 return totalDeviation <= 0.05f * totalModuleSize;
438 }
439 inline FX_BOOL centerComparator(void* a, void* b) {
440 return ((CBC_QRFinderPattern*)b)->GetCount() <
441 ((CBC_QRFinderPattern*)a)->GetCount();
442 }
443 CFX_PtrArray* CBC_QRFinderPatternFinder::SelectBestpatterns(int32_t& e) {
444 int32_t startSize = m_possibleCenters.GetSize();
445 if (m_possibleCenters.GetSize() < 3) {
446 e = BCExceptionRead;
447 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
448 }
449 FX_FLOAT average = 0.0f;
450 if (startSize > 3) {
451 FX_FLOAT totalModuleSize = 0.0f;
452 for (int32_t i = 0; i < startSize; i++) {
453 totalModuleSize += ((CBC_QRFinderPattern*)m_possibleCenters[i])
454 ->GetEstimatedModuleSize();
455 }
456 average = totalModuleSize / (FX_FLOAT)startSize;
457 for (int32_t j = 0;
458 j < m_possibleCenters.GetSize() && m_possibleCenters.GetSize() > 3;
459 j++) {
460 CBC_QRFinderPattern* pattern = (CBC_QRFinderPattern*)m_possibleCenters[j];
461 if (fabs(pattern->GetEstimatedModuleSize() - average) > 0.2f * average) {
462 delete pattern;
463 m_possibleCenters.RemoveAt(j);
464 j--;
465 }
466 }
467 }
468 if (m_possibleCenters.GetSize() > 3) {
469 BC_FX_PtrArray_Sort(m_possibleCenters, centerComparator);
470 }
471 CFX_PtrArray* vec = new CFX_PtrArray();
472 vec->SetSize(3);
473 (*vec)[0] = ((CBC_QRFinderPattern*)m_possibleCenters[0])->Clone();
474 (*vec)[1] = ((CBC_QRFinderPattern*)m_possibleCenters[1])->Clone();
475 (*vec)[2] = ((CBC_QRFinderPattern*)m_possibleCenters[2])->Clone();
476 return vec;
477 }
OLDNEW
« no previous file with comments | « xfa/src/fxbarcode/qrcode/BC_QRFinderPatternFinder.h ('k') | xfa/src/fxbarcode/qrcode/BC_QRGridSampler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698