Passed
Push — main ( b62539...4e03cd )
by smiley
01:53
created

BitMatrix::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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