Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zxing-cpp/core/src/ConcentricFinder.h @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 /* | |
| 2 * Copyright 2020 Axel Waggershauser | |
| 3 */ | |
| 4 // SPDX-License-Identifier: Apache-2.0 | |
| 5 | |
| 6 #pragma once | |
| 7 | |
| 8 #include "BitMatrixCursor.h" | |
| 9 #include "Pattern.h" | |
| 10 #include "Quadrilateral.h" | |
| 11 #include "ZXAlgorithms.h" | |
| 12 | |
| 13 #include <optional> | |
| 14 | |
| 15 namespace ZXing { | |
| 16 | |
| 17 template <typename T, size_t N> | |
| 18 static float CenterFromEnd(const std::array<T, N>& pattern, float end) | |
| 19 { | |
| 20 if (N == 5) { | |
| 21 float a = pattern[4] + pattern[3] + pattern[2] / 2.f; | |
| 22 float b = pattern[4] + (pattern[3] + pattern[2] + pattern[1]) / 2.f; | |
| 23 float c = (pattern[4] + pattern[3] + pattern[2] + pattern[1] + pattern[0]) / 2.f; | |
| 24 return end - (2 * a + b + c) / 4; | |
| 25 } else if (N == 3) { | |
| 26 float a = pattern[2] + pattern[1] / 2.f; | |
| 27 float b = (pattern[2] + pattern[1] + pattern[0]) / 2.f; | |
| 28 return end - (2 * a + b) / 3; | |
| 29 } else { // aztec | |
| 30 auto a = Reduce(pattern.begin() + (N/2 + 1), pattern.end(), pattern[N/2] / 2.f); | |
| 31 return end - a; | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 template<int N, typename Cursor> | |
| 36 std::optional<Pattern<N>> ReadSymmetricPattern(Cursor& cur, int range) | |
| 37 { | |
| 38 static_assert(N % 2 == 1); | |
| 39 assert(range > 0); | |
| 40 Pattern<N> res = {}; | |
| 41 auto constexpr s_2 = Size(res)/2; | |
| 42 auto cuo = cur.turnedBack(); | |
| 43 | |
| 44 auto next = [&](auto& cur, int i) { | |
| 45 auto v = cur.stepToEdge(1, range); | |
| 46 res[s_2 + i] += v; | |
| 47 if (range) | |
| 48 range -= v; | |
| 49 return v; | |
| 50 }; | |
| 51 | |
| 52 for (int i = 0; i <= s_2; ++i) { | |
| 53 if (!next(cur, i) || !next(cuo, -i)) | |
| 54 return {}; | |
| 55 } | |
| 56 res[s_2]--; // the starting pixel has been counted twice, fix this | |
| 57 | |
| 58 return res; | |
| 59 } | |
| 60 | |
| 61 template<bool RELAXED_THRESHOLD = false, typename PATTERN> | |
| 62 int CheckSymmetricPattern(BitMatrixCursorI& cur, PATTERN pattern, int range, bool updatePosition) | |
| 63 { | |
| 64 FastEdgeToEdgeCounter curFwd(cur), curBwd(cur.turnedBack()); | |
| 65 | |
| 66 int centerFwd = curFwd.stepToNextEdge(range); | |
| 67 if (!centerFwd) | |
| 68 return 0; | |
| 69 int centerBwd = curBwd.stepToNextEdge(range); | |
| 70 if (!centerBwd) | |
| 71 return 0; | |
| 72 | |
| 73 assert(range > 0); | |
| 74 Pattern<pattern.size()> res = {}; | |
| 75 auto constexpr s_2 = Size(res)/2; | |
| 76 res[s_2] = centerFwd + centerBwd - 1; // -1 because the starting pixel is counted twice | |
| 77 range -= res[s_2]; | |
| 78 | |
| 79 auto next = [&](auto& cur, int i) { | |
| 80 auto v = cur.stepToNextEdge(range); | |
| 81 res[s_2 + i] = v; | |
| 82 range -= v; | |
| 83 return v; | |
| 84 }; | |
| 85 | |
| 86 for (int i = 1; i <= s_2; ++i) { | |
| 87 if (!next(curFwd, i) || !next(curBwd, -i)) | |
| 88 return 0; | |
| 89 } | |
| 90 | |
| 91 if (!IsPattern<RELAXED_THRESHOLD>(res, pattern)) | |
| 92 return 0; | |
| 93 | |
| 94 if (updatePosition) | |
| 95 cur.step(res[s_2] / 2 - (centerBwd - 1)); | |
| 96 | |
| 97 return Reduce(res); | |
| 98 } | |
| 99 | |
| 100 std::optional<PointF> CenterOfRing(const BitMatrix& image, PointI center, int range, int nth, bool requireCircle = true); | |
| 101 | |
| 102 std::optional<PointF> FinetuneConcentricPatternCenter(const BitMatrix& image, PointF center, int range, int finderPatternSize); | |
| 103 | |
| 104 std::optional<QuadrilateralF> FindConcentricPatternCorners(const BitMatrix& image, PointF center, int range, int ringIndex); | |
| 105 | |
| 106 struct ConcentricPattern : public PointF | |
| 107 { | |
| 108 int size = 0; | |
| 109 }; | |
| 110 | |
| 111 template <bool E2E = false, typename PATTERN> | |
| 112 std::optional<ConcentricPattern> LocateConcentricPattern(const BitMatrix& image, PATTERN pattern, PointF center, int range) | |
| 113 { | |
| 114 auto cur = BitMatrixCursor(image, PointI(center), {}); | |
| 115 int minSpread = image.width(), maxSpread = 0; | |
| 116 // TODO: setting maxError to 1 can subtantially help with detecting symbols with low print quality resulting in damaged | |
| 117 // finder patterns, but it sutantially increases the runtime (approx. 20% slower for the falsepositive images). | |
| 118 int maxError = 0; | |
| 119 for (auto d : {PointI{0, 1}, {1, 0}}) { | |
| 120 int spread = CheckSymmetricPattern<E2E>(cur.setDirection(d), pattern, range, true); | |
| 121 if (spread) | |
| 122 UpdateMinMax(minSpread, maxSpread, spread); | |
| 123 else if (--maxError < 0) | |
| 124 return {}; | |
| 125 } | |
| 126 | |
| 127 #if 1 | |
| 128 for (auto d : {PointI{1, 1}, {1, -1}}) { | |
| 129 int spread = CheckSymmetricPattern<true>(cur.setDirection(d), pattern, range * 2, false); | |
| 130 if (spread) | |
| 131 UpdateMinMax(minSpread, maxSpread, spread); | |
| 132 else if (--maxError < 0) | |
| 133 return {}; | |
| 134 } | |
| 135 #endif | |
| 136 | |
| 137 if (maxSpread > 5 * minSpread) | |
| 138 return {}; | |
| 139 | |
| 140 auto newCenter = FinetuneConcentricPatternCenter(image, PointF(cur.p), range, pattern.size()); | |
| 141 if (!newCenter) | |
| 142 return {}; | |
| 143 | |
| 144 return ConcentricPattern{*newCenter, (maxSpread + minSpread) / 2}; | |
| 145 } | |
| 146 | |
| 147 } // ZXing | |
| 148 |
