GridSampler::checkAndNudgePoints()   D
last analyzed

Complexity

Conditions 21
Paths 111

Size

Total Lines 65
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 21
eloc 41
c 1
b 0
f 0
nc 111
nop 2
dl 0
loc 65
rs 4.075

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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