Passed
Push — gh-pages ( 20c441...dd59e5 )
by
unknown
02:54 queued 01:00
created

BitMatrix   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
dl 0
loc 182
rs 9.92
c 1
b 0
f 0
wmc 31

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDimension() 0 2 1
A unmask() 0 7 4
A flip() 0 4 1
A get() 0 6 1
A __construct() 0 4 1
A mirror() 0 7 4
A set() 0 5 1
B setRegion() 0 23 9
B buildFunctionPattern() 0 42 9
1
<?php
2
/**
3
 * Class BitMatrix
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\Decoder;
13
14
use chillerlan\QRCode\Common\{MaskPattern, Version};
15
use InvalidArgumentException;
16
use function array_fill, count;
17
18
/**
19
 *
20
 */
21
final class BitMatrix{
22
23
	private int   $dimension;
24
	private int   $rowSize;
25
	private array $bits;
26
27
	/**
28
	 *
29
	 */
30
	public function __construct(int $dimension){
31
		$this->dimension = $dimension;
32
		$this->rowSize   = ((int)(($this->dimension + 0x1f) / 0x20));
33
		$this->bits      = array_fill(0, $this->rowSize * $this->dimension, 0);
34
	}
35
36
	/**
37
	 * <p>Sets the given bit to true.</p>
38
	 *
39
	 * @param int $x ;  The horizontal component (i.e. which column)
40
	 * @param int $y ;  The vertical component (i.e. which row)
41
	 */
42
	public function set(int $x, int $y):void{
43
		$offset = (int)($y * $this->rowSize + ($x / 0x20));
44
45
		$this->bits[$offset] ??= 0;
46
		$this->bits[$offset] |= ($this->bits[$offset] |= 1 << ($x & 0x1f));
47
	}
48
49
	/**
50
	 * <p>Flips the given bit. 1 << (0xf9 & 0x1f)</p>
51
	 *
52
	 * @param int $x ;  The horizontal component (i.e. which column)
53
	 * @param int $y ;  The vertical component (i.e. which row)
54
	 */
55
	public function flip(int $x, int $y):void{
56
		$offset = $y * $this->rowSize + (int)($x / 0x20);
57
58
		$this->bits[$offset] = ($this->bits[$offset] ^ (1 << ($x & 0x1f)));
59
	}
60
61
	/**
62
	 * <p>Sets a square region of the bit matrix to true.</p>
63
	 *
64
	 * @param int $left   ;  The horizontal position to begin at (inclusive)
65
	 * @param int $top    ;  The vertical position to begin at (inclusive)
66
	 * @param int $width  ;  The width of the region
67
	 * @param int $height ;  The height of the region
68
	 *
69
	 * @throws \InvalidArgumentException
70
	 */
71
	public function setRegion(int $left, int $top, int $width, int $height):void{
72
73
		if($top < 0 || $left < 0){
74
			throw new InvalidArgumentException('Left and top must be nonnegative');
75
		}
76
77
		if($height < 1 || $width < 1){
78
			throw new InvalidArgumentException('Height and width must be at least 1');
79
		}
80
81
		$right  = $left + $width;
82
		$bottom = $top + $height;
83
84
		if($bottom > $this->dimension || $right > $this->dimension){
85
			throw new InvalidArgumentException('The region must fit inside the matrix');
86
		}
87
88
		for($y = $top; $y < $bottom; $y++){
89
			$yOffset = $y * $this->rowSize;
90
91
			for($x = $left; $x < $right; $x++){
92
				$xOffset              = $yOffset + (int)($x / 0x20);
93
				$this->bits[$xOffset] = ($this->bits[$xOffset] |= 1 << ($x & 0x1f));
94
			}
95
		}
96
	}
97
98
	/**
99
	 * @return int The dimension (width/height) of the matrix
100
	 */
101
	public function getDimension():int{
102
		return $this->dimension;
103
	}
104
105
	/**
106
	 * <p>Gets the requested bit, where true means black.</p>
107
	 *
108
	 * @param int $x The horizontal component (i.e. which column)
109
	 * @param int $y The vertical component (i.e. which row)
110
	 *
111
	 * @return bool value of given bit in matrix
112
	 */
113
	public function get(int $x, int $y):bool{
114
		$offset = (int)($y * $this->rowSize + ($x / 0x20));
115
116
		$this->bits[$offset] ??= 0;
117
118
		return (BitMatrixParser::uRShift($this->bits[$offset], ($x & 0x1f)) & 1) !== 0;
119
	}
120
121
	/**
122
	 * See ISO 18004:2006 Annex E
123
	 */
124
	public function buildFunctionPattern(Version $version):BitMatrix{
125
		$dimension = $version->getDimension();
126
		// @todo
127
		$bitMatrix = new self($dimension);
128
129
		// Top left finder pattern + separator + format
130
		$bitMatrix->setRegion(0, 0, 9, 9);
131
		// Top right finder pattern + separator + format
132
		$bitMatrix->setRegion($dimension - 8, 0, 8, 9);
133
		// Bottom left finder pattern + separator + format
134
		$bitMatrix->setRegion(0, $dimension - 8, 9, 8);
135
136
		// Alignment patterns
137
		$apc = $version->getAlignmentPattern();
138
		$max = count($apc);
139
140
		for($x = 0; $x < $max; $x++){
141
			$i = $apc[$x] - 2;
142
143
			for($y = 0; $y < $max; $y++){
144
				if(($x === 0 && ($y === 0 || $y === $max - 1)) || ($x === $max - 1 && $y === 0)){
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($x === 0 && $y === 0 ||...== $max - 1 && $y === 0, Probably Intended Meaning: $x === 0 && ($y === 0 ||...= $max - 1 && $y === 0)
Loading history...
145
					// No alignment patterns near the three finder paterns
146
					continue;
147
				}
148
149
				$bitMatrix->setRegion($apc[$y] - 2, $i, 5, 5);
150
			}
151
		}
152
153
		// Vertical timing pattern
154
		$bitMatrix->setRegion(6, 9, 1, $dimension - 17);
155
		// Horizontal timing pattern
156
		$bitMatrix->setRegion(9, 6, $dimension - 17, 1);
157
158
		if($version->getVersionNumber() > 6){
159
			// Version info, top right
160
			$bitMatrix->setRegion($dimension - 11, 0, 3, 6);
161
			// Version info, bottom left
162
			$bitMatrix->setRegion(0, $dimension - 11, 6, 3);
163
		}
164
165
		return $bitMatrix;
166
	}
167
168
	/**
169
	 * Mirror the bit matrix in order to attempt a second reading.
170
	 */
171
	public function mirror():void{
172
173
		for($x = 0; $x < $this->dimension; $x++){
174
			for($y = $x + 1; $y < $this->dimension; $y++){
175
				if($this->get($x, $y) !== $this->get($y, $x)){
176
					$this->flip($y, $x);
177
					$this->flip($x, $y);
178
				}
179
			}
180
		}
181
182
	}
183
184
	/**
185
	 * <p>Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations
186
	 * of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix,
187
	 * including areas used for finder patterns, timing patterns, etc. These areas should be unused
188
	 * after the point they are unmasked anyway.</p>
189
	 *
190
	 * <p>Note that the diagram in section 6.8.1 is misleading since it indicates that i is column position
191
	 * and j is row position. In fact, as the text says, i is row position and j is column position.</p>
192
	 *
193
	 * <p>Implementations of this method reverse the data masking process applied to a QR Code and
194
	 * make its bits ready to read.</p>
195
	 */
196
	public function unmask(int $dimension, MaskPattern $maskPattern):void{
197
		$mask = $maskPattern->getMask();
198
199
		for($y = 0; $y < $dimension; $y++){
200
			for($x = 0; $x < $dimension; $x++){
201
				if($mask($x, $y) === 0){
202
					$this->flip($x, $y);
203
				}
204
			}
205
		}
206
207
	}
208
209
}
210