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

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

Issue 1927253002: Replace CFX_PtrArray with typesafe CFX_ArrayTemplate, Part 5 (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Nits. Created 4 years, 7 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
« no previous file with comments | « xfa/fxbarcode/qrcode/BC_QRFinderPatternFinder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 // Original code is licensed as follows: 6 // Original code is licensed as follows:
7 /* 7 /*
8 * Copyright 2007 ZXing authors 8 * Copyright 2007 ZXing authors
9 * 9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * Licensed under the Apache License, Version 2.0 (the "License");
(...skipping 24 matching lines...) Expand all
35 const int32_t CBC_QRFinderPatternFinder::MIN_SKIP = 3; 35 const int32_t CBC_QRFinderPatternFinder::MIN_SKIP = 3;
36 const int32_t CBC_QRFinderPatternFinder::MAX_MODULES = 57; 36 const int32_t CBC_QRFinderPatternFinder::MAX_MODULES = 57;
37 const int32_t CBC_QRFinderPatternFinder::INTEGER_MATH_SHIFT = 8; 37 const int32_t CBC_QRFinderPatternFinder::INTEGER_MATH_SHIFT = 8;
38 38
39 CBC_QRFinderPatternFinder::CBC_QRFinderPatternFinder( 39 CBC_QRFinderPatternFinder::CBC_QRFinderPatternFinder(
40 CBC_CommonBitMatrix* image) { 40 CBC_CommonBitMatrix* image) {
41 m_image = image; 41 m_image = image;
42 m_crossCheckStateCount.SetSize(5); 42 m_crossCheckStateCount.SetSize(5);
43 m_hasSkipped = FALSE; 43 m_hasSkipped = FALSE;
44 } 44 }
45
45 CBC_QRFinderPatternFinder::~CBC_QRFinderPatternFinder() { 46 CBC_QRFinderPatternFinder::~CBC_QRFinderPatternFinder() {
46 for (int32_t i = 0; i < m_possibleCenters.GetSize(); i++) { 47 for (int32_t i = 0; i < m_possibleCenters.GetSize(); i++)
47 delete (CBC_QRFinderPattern*)m_possibleCenters[i]; 48 delete m_possibleCenters[i];
48 }
49 m_possibleCenters.RemoveAll();
50 } 49 }
50
51 class ClosestToAverageComparator { 51 class ClosestToAverageComparator {
52 private: 52 private:
53 FX_FLOAT m_averageModuleSize; 53 FX_FLOAT m_averageModuleSize;
54 54
55 public: 55 public:
56 ClosestToAverageComparator(FX_FLOAT averageModuleSize) 56 ClosestToAverageComparator(FX_FLOAT averageModuleSize)
57 : m_averageModuleSize(averageModuleSize) {} 57 : m_averageModuleSize(averageModuleSize) {}
58 int32_t operator()(FinderPattern* a, FinderPattern* b) { 58 int32_t operator()(FinderPattern* a, FinderPattern* b) {
59 FX_FLOAT dA = 59 FX_FLOAT dA =
60 (FX_FLOAT)fabs(a->GetEstimatedModuleSize() - m_averageModuleSize); 60 (FX_FLOAT)fabs(a->GetEstimatedModuleSize() - m_averageModuleSize);
(...skipping 12 matching lines...) Expand all
73 return m_image; 73 return m_image;
74 } 74 }
75 CFX_Int32Array& CBC_QRFinderPatternFinder::GetCrossCheckStateCount() { 75 CFX_Int32Array& CBC_QRFinderPatternFinder::GetCrossCheckStateCount() {
76 m_crossCheckStateCount[0] = 0; 76 m_crossCheckStateCount[0] = 0;
77 m_crossCheckStateCount[1] = 0; 77 m_crossCheckStateCount[1] = 0;
78 m_crossCheckStateCount[2] = 0; 78 m_crossCheckStateCount[2] = 0;
79 m_crossCheckStateCount[3] = 0; 79 m_crossCheckStateCount[3] = 0;
80 m_crossCheckStateCount[4] = 0; 80 m_crossCheckStateCount[4] = 0;
81 return m_crossCheckStateCount; 81 return m_crossCheckStateCount;
82 } 82 }
83 CFX_PtrArray* CBC_QRFinderPatternFinder::GetPossibleCenters() { 83
84 CFX_ArrayTemplate<CBC_QRFinderPattern*>*
85 CBC_QRFinderPatternFinder::GetPossibleCenters() {
84 return &m_possibleCenters; 86 return &m_possibleCenters;
85 } 87 }
88
86 CBC_QRFinderPatternInfo* CBC_QRFinderPatternFinder::Find(int32_t hint, 89 CBC_QRFinderPatternInfo* CBC_QRFinderPatternFinder::Find(int32_t hint,
87 int32_t& e) { 90 int32_t& e) {
88 int32_t maxI = m_image->GetHeight(); 91 int32_t maxI = m_image->GetHeight();
89 int32_t maxJ = m_image->GetWidth(); 92 int32_t maxJ = m_image->GetWidth();
90 int32_t iSkip = (3 * maxI) / (4 * MAX_MODULES); 93 int32_t iSkip = (3 * maxI) / (4 * MAX_MODULES);
91 if (iSkip < MIN_SKIP || 0) { 94 if (iSkip < MIN_SKIP || 0) {
92 iSkip = MIN_SKIP; 95 iSkip = MIN_SKIP;
93 } 96 }
94 FX_BOOL done = FALSE; 97 FX_BOOL done = FALSE;
95 CFX_Int32Array stateCount; 98 CFX_Int32Array stateCount;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 if (FoundPatternCross(stateCount)) { 157 if (FoundPatternCross(stateCount)) {
155 FX_BOOL confirmed = HandlePossibleCenter(stateCount, i, maxJ); 158 FX_BOOL confirmed = HandlePossibleCenter(stateCount, i, maxJ);
156 if (confirmed) { 159 if (confirmed) {
157 iSkip = stateCount[0]; 160 iSkip = stateCount[0];
158 if (m_hasSkipped) { 161 if (m_hasSkipped) {
159 done = HaveMultiplyConfirmedCenters(); 162 done = HaveMultiplyConfirmedCenters();
160 } 163 }
161 } 164 }
162 } 165 }
163 } 166 }
164 std::unique_ptr<CFX_PtrArray> patternInfo(SelectBestpatterns(e)); 167 std::unique_ptr<CFX_ArrayTemplate<CBC_QRFinderPattern*>> patternInfo(
168 SelectBestpatterns(e));
165 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 169 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
166 OrderBestPatterns(patternInfo.get()); 170 OrderBestPatterns(patternInfo.get());
167 return new CBC_QRFinderPatternInfo(patternInfo.get()); 171 return new CBC_QRFinderPatternInfo(patternInfo.get());
168 } 172 }
169 void CBC_QRFinderPatternFinder::OrderBestPatterns(CFX_PtrArray* patterns) { 173 void CBC_QRFinderPatternFinder::OrderBestPatterns(
170 FX_FLOAT abDistance = Distance((CBC_ResultPoint*)(*patterns)[0], 174 CFX_ArrayTemplate<CBC_QRFinderPattern*>* patterns) {
171 (CBC_ResultPoint*)(*patterns)[1]); 175 FX_FLOAT abDistance = Distance((*patterns)[0], (*patterns)[1]);
172 FX_FLOAT bcDistance = Distance((CBC_ResultPoint*)(*patterns)[1], 176 FX_FLOAT bcDistance = Distance((*patterns)[1], (*patterns)[2]);
173 (CBC_ResultPoint*)(*patterns)[2]); 177 FX_FLOAT acDistance = Distance((*patterns)[0], (*patterns)[2]);
174 FX_FLOAT acDistance = Distance((CBC_ResultPoint*)(*patterns)[0], 178 CBC_QRFinderPattern* topLeft;
175 (CBC_ResultPoint*)(*patterns)[2]); 179 CBC_QRFinderPattern* topRight;
176 CBC_QRFinderPattern *topLeft, *topRight, *bottomLeft; 180 CBC_QRFinderPattern* bottomLeft;
177 if (bcDistance >= abDistance && bcDistance >= acDistance) { 181 if (bcDistance >= abDistance && bcDistance >= acDistance) {
178 topLeft = (CBC_QRFinderPattern*)(*patterns)[0]; 182 topLeft = (*patterns)[0];
179 topRight = (CBC_QRFinderPattern*)(*patterns)[1]; 183 topRight = (*patterns)[1];
180 bottomLeft = (CBC_QRFinderPattern*)(*patterns)[2]; 184 bottomLeft = (*patterns)[2];
181 } else if (acDistance >= bcDistance && acDistance >= abDistance) { 185 } else if (acDistance >= bcDistance && acDistance >= abDistance) {
182 topLeft = (CBC_QRFinderPattern*)(*patterns)[1]; 186 topLeft = (*patterns)[1];
183 topRight = (CBC_QRFinderPattern*)(*patterns)[0]; 187 topRight = (*patterns)[0];
184 bottomLeft = (CBC_QRFinderPattern*)(*patterns)[2]; 188 bottomLeft = (*patterns)[2];
185 } else { 189 } else {
186 topLeft = (CBC_QRFinderPattern*)(*patterns)[2]; 190 topLeft = (*patterns)[2];
187 topRight = (CBC_QRFinderPattern*)(*patterns)[0]; 191 topRight = (*patterns)[0];
188 bottomLeft = (CBC_QRFinderPattern*)(*patterns)[1]; 192 bottomLeft = (*patterns)[1];
189 } 193 }
190 if ((bottomLeft->GetY() - topLeft->GetY()) * 194 if ((bottomLeft->GetY() - topLeft->GetY()) *
191 (topRight->GetX() - topLeft->GetX()) < 195 (topRight->GetX() - topLeft->GetX()) <
192 (bottomLeft->GetX() - topLeft->GetX()) * 196 (bottomLeft->GetX() - topLeft->GetX()) *
193 (topRight->GetY() - topLeft->GetY())) { 197 (topRight->GetY() - topLeft->GetY())) {
194 CBC_QRFinderPattern* temp = topRight; 198 CBC_QRFinderPattern* temp = topRight;
195 topRight = bottomLeft; 199 topRight = bottomLeft;
196 bottomLeft = temp; 200 bottomLeft = temp;
197 } 201 }
198 (*patterns)[0] = bottomLeft; 202 (*patterns)[0] = bottomLeft;
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 FX_FLOAT centerI = 372 FX_FLOAT centerI =
369 CrossCheckVertical(i, (int32_t)centerJ, stateCount[2], stateCountTotal); 373 CrossCheckVertical(i, (int32_t)centerJ, stateCount[2], stateCountTotal);
370 if (!FXSYS_isnan(centerI)) { 374 if (!FXSYS_isnan(centerI)) {
371 centerJ = CrossCheckHorizontal((int32_t)centerJ, (int32_t)centerI, 375 centerJ = CrossCheckHorizontal((int32_t)centerJ, (int32_t)centerI,
372 stateCount[2], stateCountTotal); 376 stateCount[2], stateCountTotal);
373 if (!FXSYS_isnan(centerJ)) { 377 if (!FXSYS_isnan(centerJ)) {
374 FX_FLOAT estimatedModuleSize = (FX_FLOAT)stateCountTotal / 7.0f; 378 FX_FLOAT estimatedModuleSize = (FX_FLOAT)stateCountTotal / 7.0f;
375 FX_BOOL found = FALSE; 379 FX_BOOL found = FALSE;
376 int32_t max = m_possibleCenters.GetSize(); 380 int32_t max = m_possibleCenters.GetSize();
377 for (int32_t index = 0; index < max; index++) { 381 for (int32_t index = 0; index < max; index++) {
378 CBC_QRFinderPattern* center = 382 CBC_QRFinderPattern* center = m_possibleCenters[index];
379 (CBC_QRFinderPattern*)(m_possibleCenters[index]);
380 if (center->AboutEquals(estimatedModuleSize, centerI, centerJ)) { 383 if (center->AboutEquals(estimatedModuleSize, centerI, centerJ)) {
381 center->IncrementCount(); 384 center->IncrementCount();
382 found = TRUE; 385 found = TRUE;
383 break; 386 break;
384 } 387 }
385 } 388 }
386 if (!found) { 389 if (!found) {
387 m_possibleCenters.Add( 390 m_possibleCenters.Add(
388 new CBC_QRFinderPattern(centerJ, centerI, estimatedModuleSize)); 391 new CBC_QRFinderPattern(centerJ, centerI, estimatedModuleSize));
389 } 392 }
390 return TRUE; 393 return TRUE;
391 } 394 }
392 } 395 }
393 return FALSE; 396 return FALSE;
394 } 397 }
395 int32_t CBC_QRFinderPatternFinder::FindRowSkip() { 398 int32_t CBC_QRFinderPatternFinder::FindRowSkip() {
396 int32_t max = m_possibleCenters.GetSize(); 399 int32_t max = m_possibleCenters.GetSize();
397 if (max <= 1) { 400 if (max <= 1) {
398 return 0; 401 return 0;
399 } 402 }
400 FinderPattern* firstConfirmedCenter = NULL; 403 FinderPattern* firstConfirmedCenter = NULL;
401 for (int32_t i = 0; i < max; i++) { 404 for (int32_t i = 0; i < max; i++) {
402 CBC_QRFinderPattern* center = (CBC_QRFinderPattern*)m_possibleCenters[i]; 405 CBC_QRFinderPattern* center = m_possibleCenters[i];
403 if (center->GetCount() >= CENTER_QUORUM) { 406 if (center->GetCount() >= CENTER_QUORUM) {
404 if (firstConfirmedCenter == NULL) { 407 if (firstConfirmedCenter == NULL) {
405 firstConfirmedCenter = center; 408 firstConfirmedCenter = center;
406 } else { 409 } else {
407 m_hasSkipped = TRUE; 410 m_hasSkipped = TRUE;
408 return (int32_t)((fabs(firstConfirmedCenter->GetX() - center->GetX()) - 411 return (int32_t)((fabs(firstConfirmedCenter->GetX() - center->GetX()) -
409 fabs(firstConfirmedCenter->GetY() - center->GetY())) / 412 fabs(firstConfirmedCenter->GetY() - center->GetY())) /
410 2); 413 2);
411 } 414 }
412 } 415 }
413 } 416 }
414 return 0; 417 return 0;
415 } 418 }
416 FX_BOOL CBC_QRFinderPatternFinder::HaveMultiplyConfirmedCenters() { 419 FX_BOOL CBC_QRFinderPatternFinder::HaveMultiplyConfirmedCenters() {
417 int32_t confirmedCount = 0; 420 int32_t confirmedCount = 0;
418 FX_FLOAT totalModuleSize = 0.0f; 421 FX_FLOAT totalModuleSize = 0.0f;
419 int32_t max = m_possibleCenters.GetSize(); 422 int32_t max = m_possibleCenters.GetSize();
420 int32_t i; 423 int32_t i;
421 for (i = 0; i < max; i++) { 424 for (i = 0; i < max; i++) {
422 CBC_QRFinderPattern* pattern = (CBC_QRFinderPattern*)m_possibleCenters[i]; 425 CBC_QRFinderPattern* pattern = m_possibleCenters[i];
423 if (pattern->GetCount() >= CENTER_QUORUM) { 426 if (pattern->GetCount() >= CENTER_QUORUM) {
424 confirmedCount++; 427 confirmedCount++;
425 totalModuleSize += pattern->GetEstimatedModuleSize(); 428 totalModuleSize += pattern->GetEstimatedModuleSize();
426 } 429 }
427 } 430 }
428 if (confirmedCount < 3) { 431 if (confirmedCount < 3) {
429 return FALSE; 432 return FALSE;
430 } 433 }
431 FX_FLOAT average = totalModuleSize / (FX_FLOAT)max; 434 FX_FLOAT average = totalModuleSize / (FX_FLOAT)max;
432 FX_FLOAT totalDeviation = 0.0f; 435 FX_FLOAT totalDeviation = 0.0f;
433 for (i = 0; i < max; i++) { 436 for (i = 0; i < max; i++) {
434 CBC_QRFinderPattern* pattern = (CBC_QRFinderPattern*)m_possibleCenters[i]; 437 CBC_QRFinderPattern* pattern = m_possibleCenters[i];
435 totalDeviation += fabs(pattern->GetEstimatedModuleSize() - average); 438 totalDeviation += fabs(pattern->GetEstimatedModuleSize() - average);
436 } 439 }
437 return totalDeviation <= 0.05f * totalModuleSize; 440 return totalDeviation <= 0.05f * totalModuleSize;
438 } 441 }
439 inline FX_BOOL centerComparator(void* a, void* b) { 442
440 return ((CBC_QRFinderPattern*)b)->GetCount() < 443 CFX_ArrayTemplate<CBC_QRFinderPattern*>*
441 ((CBC_QRFinderPattern*)a)->GetCount(); 444 CBC_QRFinderPatternFinder::SelectBestpatterns(int32_t& e) {
442 }
443 CFX_PtrArray* CBC_QRFinderPatternFinder::SelectBestpatterns(int32_t& e) {
444 int32_t startSize = m_possibleCenters.GetSize(); 445 int32_t startSize = m_possibleCenters.GetSize();
445 if (m_possibleCenters.GetSize() < 3) { 446 if (m_possibleCenters.GetSize() < 3) {
446 e = BCExceptionRead; 447 e = BCExceptionRead;
447 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); 448 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
448 } 449 }
449 FX_FLOAT average = 0.0f; 450 FX_FLOAT average = 0.0f;
450 if (startSize > 3) { 451 if (startSize > 3) {
451 FX_FLOAT totalModuleSize = 0.0f; 452 FX_FLOAT totalModuleSize = 0.0f;
452 for (int32_t i = 0; i < startSize; i++) { 453 for (int32_t i = 0; i < startSize; i++)
453 totalModuleSize += ((CBC_QRFinderPattern*)m_possibleCenters[i]) 454 totalModuleSize += m_possibleCenters[i]->GetEstimatedModuleSize();
454 ->GetEstimatedModuleSize(); 455
455 }
456 average = totalModuleSize / (FX_FLOAT)startSize; 456 average = totalModuleSize / (FX_FLOAT)startSize;
457 for (int32_t j = 0; 457 for (int32_t j = 0;
458 j < m_possibleCenters.GetSize() && m_possibleCenters.GetSize() > 3; 458 j < m_possibleCenters.GetSize() && m_possibleCenters.GetSize() > 3;
459 j++) { 459 j++) {
460 CBC_QRFinderPattern* pattern = (CBC_QRFinderPattern*)m_possibleCenters[j]; 460 CBC_QRFinderPattern* pattern = m_possibleCenters[j];
461 if (fabs(pattern->GetEstimatedModuleSize() - average) > 0.2f * average) { 461 if (fabs(pattern->GetEstimatedModuleSize() - average) > 0.2f * average) {
462 delete pattern; 462 delete pattern;
463 m_possibleCenters.RemoveAt(j); 463 m_possibleCenters.RemoveAt(j);
464 j--; 464 j--;
465 } 465 }
466 } 466 }
467 } 467 }
468 if (m_possibleCenters.GetSize() > 3) { 468 if (m_possibleCenters.GetSize() > 3) {
469 BC_FX_PtrArray_Sort(m_possibleCenters, centerComparator); 469 std::sort(m_possibleCenters.GetData(),
470 m_possibleCenters.GetData() + m_possibleCenters.GetSize(),
471 [](const CBC_QRFinderPattern* a, CBC_QRFinderPattern* b) {
472 return a->GetCount() > b->GetCount(); // e.g. Descending.
473 });
470 } 474 }
471 CFX_PtrArray* vec = new CFX_PtrArray(); 475 CFX_ArrayTemplate<CBC_QRFinderPattern*>* vec =
476 new CFX_ArrayTemplate<CBC_QRFinderPattern*>();
472 vec->SetSize(3); 477 vec->SetSize(3);
473 (*vec)[0] = ((CBC_QRFinderPattern*)m_possibleCenters[0])->Clone(); 478 (*vec)[0] = m_possibleCenters[0]->Clone();
474 (*vec)[1] = ((CBC_QRFinderPattern*)m_possibleCenters[1])->Clone(); 479 (*vec)[1] = m_possibleCenters[1]->Clone();
475 (*vec)[2] = ((CBC_QRFinderPattern*)m_possibleCenters[2])->Clone(); 480 (*vec)[2] = m_possibleCenters[2]->Clone();
476 return vec; 481 return vec;
477 } 482 }
OLDNEW
« no previous file with comments | « xfa/fxbarcode/qrcode/BC_QRFinderPatternFinder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698