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