|
|
@@ -13,6 +13,7 @@
|
|
|
namespace chillerlan\QRCode\Data;
|
|
|
|
|
|
use chillerlan\QRCode\QRCode;
|
|
|
+use Closure;
|
|
|
|
|
|
use function array_fill, array_key_exists, array_push, array_unshift, count, floor, in_array, max, min, range;
|
|
|
|
|
|
@@ -547,6 +548,7 @@ class QRMatrix{
|
|
|
$this->maskPattern = $maskPattern;
|
|
|
$byteCount = count($data);
|
|
|
$size = $this->moduleCount - 1;
|
|
|
+ $mask = $this->getMask($this->maskPattern);
|
|
|
|
|
|
for($i = $size, $y = $size, $inc = -1, $byteIndex = 0, $bitIndex = 7; $i > 0; $i -= 2){
|
|
|
|
|
|
@@ -565,7 +567,7 @@ class QRMatrix{
|
|
|
$v = (($data[$byteIndex] >> $bitIndex) & 1) === 1;
|
|
|
}
|
|
|
|
|
|
- if($this->getMask($x, $y, $maskPattern) === 0){
|
|
|
+ if($mask($x, $y) === 0){
|
|
|
$v = !$v;
|
|
|
}
|
|
|
|
|
|
@@ -602,32 +604,27 @@ class QRMatrix{
|
|
|
*
|
|
|
* @internal
|
|
|
*
|
|
|
- * @param int $x
|
|
|
- * @param int $y
|
|
|
* @param int $maskPattern
|
|
|
*
|
|
|
- * @return int
|
|
|
+ * @return \Closure
|
|
|
* @throws \chillerlan\QRCode\Data\QRCodeDataException
|
|
|
*/
|
|
|
- protected function getMask(int $x, int $y, int $maskPattern):int{
|
|
|
- $a = $y + $x;
|
|
|
- $m = $y * $x;
|
|
|
-
|
|
|
- if($maskPattern >= 0 && $maskPattern < 8){
|
|
|
- // this is literally the same as the stupid switch...
|
|
|
- return [
|
|
|
- $a % 2,
|
|
|
- $x % 2,
|
|
|
- $y % 3,
|
|
|
- $a % 3,
|
|
|
- ((int)($y / 2) + (int)($x / 3)) % 2,
|
|
|
- $m % 2 + $m % 3,
|
|
|
- ($m % 2 + $m % 3) % 2,
|
|
|
- ($m % 3 + $a % 2) % 2
|
|
|
- ][$maskPattern];
|
|
|
+ protected function getMask(int $maskPattern):Closure{
|
|
|
+
|
|
|
+ if((0b111 & $maskPattern) !== $maskPattern){
|
|
|
+ throw new QRCodeDataException('invalid mask pattern'); // @codeCoverageIgnore
|
|
|
}
|
|
|
|
|
|
- throw new QRCodeDataException('invalid mask pattern'); // @codeCoverageIgnore
|
|
|
+ return [
|
|
|
+ 0b000 => function($x, $y):int{ return ($x + $y) % 2; },
|
|
|
+ 0b001 => function($x, $y):int{ return $x % 2; },
|
|
|
+ 0b010 => function($x, $y):int{ return $y % 3; },
|
|
|
+ 0b011 => function($x, $y):int{ return ($x + $y) % 3; },
|
|
|
+ 0b100 => function($x, $y):int{ return ((int)($x / 2) + (int)($y / 3)) % 2; },
|
|
|
+ 0b101 => function($x, $y):int{ return (($x * $y) % 2) + (($x * $y) % 3); },
|
|
|
+ 0b110 => function($x, $y):int{ return ((($x * $y) % 2) + (($x * $y) % 3)) % 2; },
|
|
|
+ 0b111 => function($x, $y):int{ return ((($x * $y) % 3) + (($x + $y) % 2)) % 2; },
|
|
|
+ ][$maskPattern];
|
|
|
}
|
|
|
|
|
|
}
|