PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.18
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.18
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / vendor_prefixed / chillerlan / php-qrcode / src / Detector / GridSampler.php

GridSampler.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.18, at vendor_prefixed/chillerlan/php-qrcode/src/Detector/GridSampler.php

150 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class GridSampler
5 *
6 * @created 17.01.2021
7 * @author ZXing Authors
8 * @author Smiley <smiley@chillerlan.net>
9 * @copyright 2021 Smiley
10 * @license Apache-2.0
11 */
12 namespace WCPOS\Vendor\chillerlan\QRCode\Detector;
13
14 use WCPOS\Vendor\chillerlan\QRCode\Data\QRMatrix;
15 use WCPOS\Vendor\chillerlan\QRCode\Decoder\BitMatrix;
16 use function array_fill, count, intdiv, sprintf;
17 /**
18 * Implementations of this class can, given locations of finder patterns for a QR code in an
19 * image, sample the right points in the image to reconstruct the QR code, accounting for
20 * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
21 * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
22 * Imaging library, but which may not be available in other environments such as J2ME, and vice
23 * versa.
24 *
25 * The implementation used can be controlled by calling #setGridSampler(GridSampler)
26 * with an instance of a class which implements this interface.
27 *
28 * @author Sean Owen
29 */
30 final class GridSampler
31 {
32 private array $points;
33 /**
34 * Checks a set of points that have been transformed to sample points on an image against
35 * the image's dimensions to see if the point are even within the image.
36 *
37 * This method will actually "nudge" the endpoints back onto the image if they are found to be
38 * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
39 * patterns in an image where the QR Code runs all the way to the image border.
40 *
41 * For efficiency, the method will check points from either end of the line until one is found
42 * to be within the image. Because the set of points are assumed to be linear, this is valid.
43 *
44 * @param int $dimension matrix width/height
45 *
46 * @throws \chillerlan\QRCode\Detector\QRCodeDetectorException if an endpoint is lies outside the image boundaries
47 */
48 private function checkAndNudgePoints(int $dimension) : void
49 {
50 $nudged = \true;
51 $max = count($this->points);
52 // Check and nudge points from start until we see some that are OK:
53 for ($offset = 0; $offset < $max && $nudged; $offset += 2) {
54 $x = (int) $this->points[$offset];
55 $y = (int) $this->points[$offset + 1];
56 if ($x < -1 || $x > $dimension || $y < -1 || $y > $dimension) {
57 throw new QRCodeDetectorException(sprintf('checkAndNudgePoints 1, x: %s, y: %s, d: %s', $x, $y, $dimension));
58 }
59 $nudged = \false;
60 if ($x === -1) {
61 $this->points[$offset] = 0.0;
62 $nudged = \true;
63 } elseif ($x === $dimension) {
64 $this->points[$offset] = $dimension - 1;
65 $nudged = \true;
66 }
67 if ($y === -1) {
68 $this->points[$offset + 1] = 0.0;
69 $nudged = \true;
70 } elseif ($y === $dimension) {
71 $this->points[$offset + 1] = $dimension - 1;
72 $nudged = \true;
73 }
74 }
75 // Check and nudge points from end:
76 $nudged = \true;
77 for ($offset = $max - 2; $offset >= 0 && $nudged; $offset -= 2) {
78 $x = (int) $this->points[$offset];
79 $y = (int) $this->points[$offset + 1];
80 if ($x < -1 || $x > $dimension || $y < -1 || $y > $dimension) {
81 throw new QRCodeDetectorException(sprintf('checkAndNudgePoints 2, x: %s, y: %s, d: %s', $x, $y, $dimension));
82 }
83 $nudged = \false;
84 if ($x === -1) {
85 $this->points[$offset] = 0.0;
86 $nudged = \true;
87 } elseif ($x === $dimension) {
88 $this->points[$offset] = $dimension - 1;
89 $nudged = \true;
90 }
91 if ($y === -1) {
92 $this->points[$offset + 1] = 0.0;
93 $nudged = \true;
94 } elseif ($y === $dimension) {
95 $this->points[$offset + 1] = $dimension - 1;
96 $nudged = \true;
97 }
98 }
99 }
100 /**
101 * Samples an image for a rectangular matrix of bits of the given dimension. The sampling
102 * transformation is determined by the coordinates of 4 points, in the original and transformed
103 * image space.
104 *
105 * @return \chillerlan\QRCode\Decoder\BitMatrix representing a grid of points sampled from the image within a region
106 * defined by the "from" parameters
107 * @throws \chillerlan\QRCode\Detector\QRCodeDetectorException if image can't be sampled, for example, if the transformation defined
108 * by the given points is invalid or results in sampling outside the image boundaries
109 */
110 public function sampleGrid(BitMatrix $matrix, int $dimension, PerspectiveTransform $transform) : BitMatrix
111 {
112 if ($dimension <= 0) {
113 throw new QRCodeDetectorException('invalid matrix size');
114 }
115 $bits = new BitMatrix($dimension);
116 $this->points = array_fill(0, 2 * $dimension, 0.0);
117 for ($y = 0; $y < $dimension; $y++) {
118 $max = count($this->points);
119 $iValue = $y + 0.5;
120 for ($x = 0; $x < $max; $x += 2) {
121 $this->points[$x] = $x / 2 + 0.5;
122 $this->points[$x + 1] = $iValue;
123 }
124 // phpcs:ignore
125 [$this->points] = $transform->transformPoints($this->points);
126 // Quick check to see if points transformed to something inside the image;
127 // sufficient to check the endpoints
128 $this->checkAndNudgePoints($matrix->getSize());
129 // no need to try/catch as QRMatrix::set() will silently discard out of bounds values
130 # try{
131 for ($x = 0; $x < $max; $x += 2) {
132 // Black(-ish) pixel
133 $bits->set(intdiv($x, 2), $y, $matrix->check((int) $this->points[$x], (int) $this->points[$x + 1]), QRMatrix::M_DATA);
134 }
135 # }
136 # catch(\Throwable $aioobe){//ArrayIndexOutOfBoundsException
137 // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
138 // transform gets "twisted" such that it maps a straight line of points to a set of points
139 // whose endpoints are in bounds, but others are not. There is probably some mathematical
140 // way to detect this about the transformation that I don't know yet.
141 // This results in an ugly runtime exception despite our clever checks above -- can't have
142 // that. We could check each point's coordinates but that feels duplicative. We settle for
143 // catching and wrapping ArrayIndexOutOfBoundsException.
144 # throw new QRCodeDetectorException('ArrayIndexOutOfBoundsException');
145 # }
146 }
147 return $bits;
148 }
149 }
150