comparison mupdf-source/thirdparty/zxing-cpp/core/src/GridSampler.cpp @ 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 2016 Nu-book Inc.
3 * Copyright 2016 ZXing authors
4 * Copyright 2020 Axel Waggershauser
5 */
6 // SPDX-License-Identifier: Apache-2.0
7
8 #include "GridSampler.h"
9
10 #ifdef PRINT_DEBUG
11 #include "LogMatrix.h"
12 #include "BitMatrixIO.h"
13 #endif
14
15 namespace ZXing {
16
17 #ifdef PRINT_DEBUG
18 LogMatrix log;
19 #endif
20
21 DetectorResult SampleGrid(const BitMatrix& image, int width, int height, const PerspectiveTransform& mod2Pix)
22 {
23 return SampleGrid(image, width, height, {ROI{0, width, 0, height, mod2Pix}});
24 }
25
26 DetectorResult SampleGrid(const BitMatrix& image, int width, int height, const ROIs& rois)
27 {
28 #ifdef PRINT_DEBUG
29 LogMatrix log;
30 static int i = 0;
31 LogMatrixWriter lmw(log, image, 5, "grid" + std::to_string(i++) + ".pnm");
32 #endif
33 if (width <= 0 || height <= 0)
34 return {};
35
36 for (auto&& [x0, x1, y0, y1, mod2Pix] : rois) {
37 // Precheck the corners of every roi to bail out early if the grid is "obviously" not completely inside the image
38 auto isInside = [&mod2Pix = mod2Pix, &image](int x, int y) { return image.isIn(mod2Pix(centered(PointI(x, y)))); };
39 if (!mod2Pix.isValid() || !isInside(x0, y0) || !isInside(x1 - 1, y0) || !isInside(x1 - 1, y1 - 1) || !isInside(x0, y1 - 1))
40 return {};
41 }
42
43 BitMatrix res(width, height);
44 for (auto&& [x0, x1, y0, y1, mod2Pix] : rois) {
45 for (int y = y0; y < y1; ++y)
46 for (int x = x0; x < x1; ++x) {
47 auto p = mod2Pix(centered(PointI{x, y}));
48 // Due to a "numerical instability" in the PerspectiveTransform generation/application it has been observed
49 // that even though all boundary grid points get projected inside the image, it can still happen that an
50 // inner grid points is not. See #563. A true perspective transformation cannot have this property.
51 // The following check takes 100% care of the issue and turned out to be less of a performance impact than feared.
52 // TODO: Check some mathematical/numercial property of mod2Pix to determine if it is a perspective transforation.
53 if (!image.isIn(p))
54 return {};
55
56 #ifdef PRINT_DEBUG
57 log(p, 3);
58 #endif
59 #if 0
60 int sum = 0;
61 for (int dy = -1; dy <= 1; ++dy)
62 for (int dx = -1; dx <= 1; ++dx)
63 sum += image.get(p + PointF(dx, dy));
64 if (sum >= 5)
65 #else
66 if (image.get(p))
67 #endif
68 res.set(x, y);
69 }
70 }
71
72 #ifdef PRINT_DEBUG
73 printf("width: %d, height: %d\n", width, height);
74 // printf("%s", ToString(res).c_str());
75 #endif
76
77 auto projectCorner = [&](PointI p) {
78 for (auto&& [x0, x1, y0, y1, mod2Pix] : rois)
79 if (x0 <= p.x && p.x <= x1 && y0 <= p.y && p.y <= y1)
80 return PointI(mod2Pix(PointF(p)) + PointF(0.5, 0.5));
81
82 return PointI();
83 };
84
85 return {std::move(res),
86 {projectCorner({0, 0}), projectCorner({width, 0}), projectCorner({width, height}), projectCorner({0, height})}};
87 }
88
89 } // ZXing