Passed
Push — v5 ( 93618e...84eb31 )
by smiley
01:52
created

Detector::sizeOfBlackWhiteBlackRun()   F

Complexity

Conditions 12
Paths 432

Size

Total Lines 63
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 31
c 1
b 0
f 0
nc 432
nop 4
dl 0
loc 63
rs 3.5888

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 Detector
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 RuntimeException;
15
use chillerlan\QRCode\Common\Version;
16
use chillerlan\QRCode\Decoder\BitMatrix;
17
use function abs, is_nan, max, min, round;
18
use function chillerlan\QRCode\Common\distance;
19
use const NAN;
20
21
/**
22
 * <p>Encapsulates logic that can detect a QR Code in an image, even if the QR Code
23
 * is rotated or skewed, or partially obscured.</p>
24
 *
25
 * @author Sean Owen
26
 */
27
final class Detector{
28
29
	private BitMatrix $bitMatrix;
30
31
	/**
32
	 * Detector constructor.
33
	 */
34
	public function __construct(BitMatrix $image){
35
		$this->bitMatrix = $image;
36
	}
37
38
	/**
39
	 * <p>Detects a QR Code in an image.</p>
40
	 */
41
	public function detect():BitMatrix{
42
		[$bottomLeft, $topLeft, $topRight] = (new FinderPatternFinder($this->bitMatrix))->find();
43
44
		$moduleSize         = (float)$this->calculateModuleSize($topLeft, $topRight, $bottomLeft);
45
		$dimension          = $this->computeDimension($topLeft, $topRight, $bottomLeft, $moduleSize);
46
		$provisionalVersion = new Version((int)(($dimension - 17) / 4));
47
		$alignmentPattern   = null;
48
49
		// Anything above version 1 has an alignment pattern
50
		if(!empty($provisionalVersion->getAlignmentPattern())){
51
			// Guess where a "bottom right" finder pattern would have been
52
			$bottomRightX = $topRight->getX() - $topLeft->getX() + $bottomLeft->getX();
53
			$bottomRightY = $topRight->getY() - $topLeft->getY() + $bottomLeft->getY();
54
55
			// Estimate that alignment pattern is closer by 3 modules
56
			// from "bottom right" to known top left location
57
			$correctionToTopLeft = 1.0 - 3.0 / (float)($provisionalVersion->getDimension() - 7);
58
			$estAlignmentX       = (int)($topLeft->getX() + $correctionToTopLeft * ($bottomRightX - $topLeft->getX()));
59
			$estAlignmentY       = (int)($topLeft->getY() + $correctionToTopLeft * ($bottomRightY - $topLeft->getY()));
60
61
			// Kind of arbitrary -- expand search radius before giving up
62
			for($i = 4; $i <= 16; $i <<= 1){//??????????
63
				$alignmentPattern = $this->findAlignmentInRegion($moduleSize, $estAlignmentX, $estAlignmentY, (float)$i);
64
65
				if($alignmentPattern !== null){
66
					break;
67
				}
68
			}
69
			// If we didn't find alignment pattern... well try anyway without it
70
		}
71
72
		$transform = $this->createTransform($topLeft, $topRight, $bottomLeft, $dimension, $alignmentPattern);
73
74
		return (new GridSampler)->sampleGrid($this->bitMatrix, $dimension, $transform);
75
	}
76
77
	/**
78
	 * <p>Computes an average estimated module size based on estimated derived from the positions
79
	 * of the three finder patterns.</p>
80
	 *
81
	 * @throws \RuntimeException
82
	 */
83
	private function calculateModuleSize(FinderPattern $topLeft, FinderPattern $topRight, FinderPattern $bottomLeft):float{
84
		// Take the average
85
		$moduleSize = (
86
			$this->calculateModuleSizeOneWay($topLeft, $topRight) +
87
			$this->calculateModuleSizeOneWay($topLeft, $bottomLeft)
88
		) / 2.0;
89
90
		if($moduleSize < 1.0){
91
			throw new RuntimeException('module size < 1.0');
92
		}
93
94
		return $moduleSize;
95
	}
96
97
	/**
98
	 * <p>Estimates module size based on two finder patterns -- it uses
99
	 * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the
100
	 * width of each, measuring along the axis between their centers.</p>
101
	 */
102
	private function calculateModuleSizeOneWay(FinderPattern $pattern, FinderPattern $otherPattern):float{
103
104
		$moduleSizeEst1 = $this->sizeOfBlackWhiteBlackRunBothWays(
105
			$pattern->getX(),
106
			$pattern->getY(),
107
			$otherPattern->getX(),
108
			$otherPattern->getY()
109
		);
110
111
		$moduleSizeEst2 = $this->sizeOfBlackWhiteBlackRunBothWays(
112
			$otherPattern->getX(),
113
			$otherPattern->getY(),
114
			$pattern->getX(),
115
			$pattern->getY()
116
		);
117
118
		if(is_nan($moduleSizeEst1)){
119
			return $moduleSizeEst2 / 7.0;
120
		}
121
122
		if(is_nan($moduleSizeEst2)){
123
			return $moduleSizeEst1 / 7.0;
124
		}
125
		// Average them, and divide by 7 since we've counted the width of 3 black modules,
126
		// and 1 white and 1 black module on either side. Ergo, divide sum by 14.
127
		return ($moduleSizeEst1 + $moduleSizeEst2) / 14.0;
128
	}
129
130
	/**
131
	 * See {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)}; computes the total width of
132
	 * a finder pattern by looking for a black-white-black run from the center in the direction
133
	 * of another po$(another finder pattern center), and in the opposite direction too.</p>
134
	 */
135
	private function sizeOfBlackWhiteBlackRunBothWays(float $fromX, float $fromY, float $toX, float $toY):float{
136
		$result    = $this->sizeOfBlackWhiteBlackRun((int)$fromX, (int)$fromY, (int)$toX, (int)$toY);
137
		$dimension = $this->bitMatrix->getDimension();
138
		// Now count other way -- don't run off image though of course
139
		$scale     = 1.0;
140
		$otherToX  = $fromX - ($toX - $fromX);
141
142
		if($otherToX < 0){
143
			$scale    = $fromX / ($fromX - $otherToX);
144
			$otherToX = 0;
145
		}
146
		elseif($otherToX >= $dimension){
147
			$scale    = ($dimension - 1 - $fromX) / ($otherToX - $fromX);
148
			$otherToX = $dimension - 1;
149
		}
150
151
		$otherToY = (int)($fromY - ($toY - $fromY) * $scale);
152
		$scale    = 1.0;
153
154
		if($otherToY < 0){
155
			$scale    = $fromY / ($fromY - $otherToY);
156
			$otherToY = 0;
157
		}
158
		elseif($otherToY >= $dimension){
159
			$scale    = ($dimension - 1 - $fromY) / ($otherToY - $fromY);
160
			$otherToY = $dimension - 1;
161
		}
162
163
		$otherToX = (int)($fromX + ($otherToX - $fromX) * $scale);
164
		$result   += $this->sizeOfBlackWhiteBlackRun((int)$fromX, (int)$fromY, (int)$otherToX, (int)$otherToY);
165
166
		// Middle pixel is double-counted this way; subtract 1
167
		return $result - 1.0;
168
	}
169
170
	/**
171
	 * <p>This method traces a line from a po$in the image, in the direction towards another point.
172
	 * It begins in a black region, and keeps going until it finds white, then black, then white again.
173
	 * It reports the distance from the start to this point.</p>
174
	 *
175
	 * <p>This is used when figuring out how wide a finder pattern is, when the finder pattern
176
	 * may be skewed or rotated.</p>
177
	 */
178
	private function sizeOfBlackWhiteBlackRun(int $fromX, int $fromY, int $toX, int $toY):float{
179
		// Mild variant of Bresenham's algorithm;
180
		// see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
181
		$steep = abs($toY - $fromY) > abs($toX - $fromX);
182
183
		if($steep){
184
			$temp  = $fromX;
185
			$fromX = $fromY;
186
			$fromY = $temp;
187
			$temp  = $toX;
188
			$toX   = $toY;
189
			$toY   = $temp;
190
		}
191
192
		$dx    = abs($toX - $fromX);
193
		$dy    = abs($toY - $fromY);
194
		$error = -$dx / 2;
195
		$xstep = $fromX < $toX ? 1 : -1;
196
		$ystep = $fromY < $toY ? 1 : -1;
197
198
		// In black pixels, looking for white, first or second time.
199
		$state  = 0;
200
		// Loop up until x == toX, but not beyond
201
		$xLimit = $toX + $xstep;
202
203
		for($x = $fromX, $y = $fromY; $x !== $xLimit; $x += $xstep){
204
			$realX = $steep ? $y : $x;
205
			$realY = $steep ? $x : $y;
206
207
			// Does current pixel mean we have moved white to black or vice versa?
208
			// Scanning black in state 0,2 and white in state 1, so if we find the wrong
209
			// color, advance to next state or end if we are in state 2 already
210
			if(($state === 1) === $this->bitMatrix->get($realX, $realY)){
211
212
				if($state === 2){
213
					return distance($x, $y, $fromX, $fromY);
214
				}
215
216
				$state++;
217
			}
218
219
			$error += $dy;
220
221
			if($error > 0){
222
223
				if($y === $toY){
224
					break;
225
				}
226
227
				$y     += $ystep;
228
				$error -= $dx;
229
			}
230
		}
231
232
		// Found black-white-black; give the benefit of the doubt that the next pixel outside the image
233
		// is "white" so this last po$at (toX+xStep,toY) is the right ending. This is really a
234
		// small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
235
		if($state === 2){
236
			return distance($toX + $xstep, $toY, $fromX, $fromY);
237
		}
238
239
		// else we didn't find even black-white-black; no estimate is really possible
240
		return NAN;
241
	}
242
243
	/**
244
	 * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
245
	 * of the finder patterns and estimated module size.</p>
246
	 *
247
	 * @throws \RuntimeException
248
	 */
249
	private function computeDimension(
250
		FinderPattern $topLeft,
251
		FinderPattern $topRight,
252
		FinderPattern $bottomLeft,
253
		float $moduleSize
254
	):int{
255
		$tltrCentersDimension = (int)round($topLeft->distance($topRight) / $moduleSize);
256
		$tlblCentersDimension = (int)round($topLeft->distance($bottomLeft) / $moduleSize);
257
		$dimension            = (int)((($tltrCentersDimension + $tlblCentersDimension) / 2) + 7);
258
259
		switch($dimension % 4){
260
			case 0:
261
				$dimension++;
262
				break;
263
			// 1? do nothing
264
			case 2:
265
				$dimension--;
266
				break;
267
			case 3:
268
				throw new RuntimeException('estimated dimension: '.$dimension);
269
		}
270
271
		if($dimension % 4 !== 1){
272
			throw new RuntimeException('dimension mod 4 is not 1');
273
		}
274
275
		return $dimension;
276
	}
277
278
	/**
279
	 * <p>Attempts to locate an alignment pattern in a limited region of the image, which is
280
	 * guessed to contain it.</p>
281
	 *
282
	 * @param float $overallEstModuleSize estimated module size so far
283
	 * @param int   $estAlignmentX        x coordinate of center of area probably containing alignment pattern
284
	 * @param int   $estAlignmentY        y coordinate of above
285
	 * @param float $allowanceFactor      number of pixels in all directions to search from the center
286
	 *
287
	 * @return \chillerlan\QRCode\Detector\AlignmentPattern|null if found, or null otherwise
288
	 */
289
	private function findAlignmentInRegion(
290
		float $overallEstModuleSize,
291
		int $estAlignmentX,
292
		int $estAlignmentY,
293
		float $allowanceFactor
294
	):?AlignmentPattern{
295
		// Look for an alignment pattern (3 modules in size) around where it should be
296
		$dimension           = $this->bitMatrix->getDimension();
297
		$allowance           = (int)($allowanceFactor * $overallEstModuleSize);
298
		$alignmentAreaLeftX  = max(0, $estAlignmentX - $allowance);
299
		$alignmentAreaRightX = min($dimension - 1, $estAlignmentX + $allowance);
300
301
		if($alignmentAreaRightX - $alignmentAreaLeftX < $overallEstModuleSize * 3){
302
			return null;
303
		}
304
305
		$alignmentAreaTopY    = max(0, $estAlignmentY - $allowance);
306
		$alignmentAreaBottomY = min($dimension - 1, $estAlignmentY + $allowance);
307
308
		if($alignmentAreaBottomY - $alignmentAreaTopY < $overallEstModuleSize * 3){
309
			return null;
310
		}
311
312
		return (new AlignmentPatternFinder($this->bitMatrix, $overallEstModuleSize))->find(
313
			$alignmentAreaLeftX,
314
			$alignmentAreaTopY,
315
			$alignmentAreaRightX - $alignmentAreaLeftX,
316
			$alignmentAreaBottomY - $alignmentAreaTopY,
317
		);
318
	}
319
320
	/**
321
	 *
322
	 */
323
	private function createTransform(
324
		FinderPattern $topLeft,
325
		FinderPattern $topRight,
326
		FinderPattern $bottomLeft,
327
		int $dimension,
328
		AlignmentPattern $alignmentPattern = null
329
	):PerspectiveTransform{
330
		$dimMinusThree = (float)$dimension - 3.5;
331
332
		if($alignmentPattern instanceof AlignmentPattern){
333
			$bottomRightX       = $alignmentPattern->getX();
334
			$bottomRightY       = $alignmentPattern->getY();
335
			$sourceBottomRightX = $dimMinusThree - 3.0;
336
			$sourceBottomRightY = $sourceBottomRightX;
337
		}
338
		else{
339
			// Don't have an alignment pattern, just make up the bottom-right point
340
			$bottomRightX       = ($topRight->getX() - $topLeft->getX()) + $bottomLeft->getX();
341
			$bottomRightY       = ($topRight->getY() - $topLeft->getY()) + $bottomLeft->getY();
342
			$sourceBottomRightX = $dimMinusThree;
343
			$sourceBottomRightY = $dimMinusThree;
344
		}
345
346
		return PerspectiveTransform::quadrilateralToQuadrilateral(
347
			3.5, 3.5,
348
			$dimMinusThree, 3.5,
349
			$sourceBottomRightX, $sourceBottomRightY,
350
			3.5, $dimMinusThree,
351
			$topLeft->getX(), $topLeft->getY(),
352
			$topRight->getX(), $topRight->getY(),
353
			$bottomRightX, $bottomRightY,
354
			$bottomLeft->getX(), $bottomLeft->getY()
355
		);
356
	}
357
358
}
359