diff --git a/src/Data/MaskPatternTester.php b/src/Data/MaskPatternTester.php index c2a87205c..8dfd24180 100644 --- a/src/Data/MaskPatternTester.php +++ b/src/Data/MaskPatternTester.php @@ -12,7 +12,7 @@ namespace chillerlan\QRCode\Data; -use function abs, call_user_func; +use function abs, call_user_func_array; /** * The sole purpose of this class is to receive a QRMatrix object and run the pattern tests on it. @@ -58,7 +58,7 @@ class MaskPatternTester{ $penalty = 0; for($level = 1; $level <= 4; $level++){ - $penalty += call_user_func([$this, 'testLevel'.$level]); + $penalty += call_user_func_array([$this, 'testLevel'.$level], [$this->matrix->matrix(true)]); } return (int)$penalty; @@ -67,12 +67,12 @@ class MaskPatternTester{ /** * Checks for each group of five or more same-colored modules in a row (or column) * - * @return float + * @return int */ - protected function testLevel1():float{ + protected function testLevel1(array $m):int{ $penalty = 0; - foreach($this->matrix->matrix() as $y => $row){ + foreach($m as $y => $row){ foreach($row as $x => $val){ $count = 0; @@ -84,11 +84,11 @@ class MaskPatternTester{ for($rx = -1; $rx <= 1; $rx++){ - if(($ry === 0 && $rx === 0) || ($x + $rx < 0 || $this->moduleCount <= $x + $rx)){ + if(($ry === 0 && $rx === 0) || (($x + $rx) < 0 || $this->moduleCount <= ($x + $rx))){ continue; } - if($this->matrix->check($x + $rx, $y + $ry) === ($val >> 8 > 0)){ + if($m[$y + $ry][$x + $rx] === $val){ $count++; } @@ -108,94 +108,77 @@ class MaskPatternTester{ /** * Checks for each 2x2 area of same-colored modules in the matrix * - * @return float + * @return int */ - protected function testLevel2():float{ + protected function testLevel2(array $m):int{ $penalty = 0; - foreach($this->matrix->matrix() as $y => $row){ + foreach($m as $y => $row){ - if($y > $this->moduleCount - 2){ + if($y > ($this->moduleCount - 2)){ break; } foreach($row as $x => $val){ - if($x > $this->moduleCount - 2){ + if($x > ($this->moduleCount - 2)){ break; } - $count = 0; - - if($val >> 8 > 0){ - $count++; + if( + $val === $m[$y][$x + 1] + && $val === $m[$y + 1][$x] + && $val === $m[$y + 1][$x + 1] + ){ + $penalty++; } - - if($this->matrix->check($y, $x + 1)){ - $count++; - } - - if($this->matrix->check($y + 1, $x)){ - $count++; - } - - if($this->matrix->check($y + 1, $x + 1)){ - $count++; - } - - if($count === 0 || $count === 4){ - $penalty += 3; - } - } } - return $penalty; + return 3 * $penalty; } /** - * Checks if there are patterns that look similar to the finder patterns + * Checks if there are patterns that look similar to the finder patterns (1:1:3:1:1 ratio) * - * @return float + * @return int */ - protected function testLevel3():float{ - $penalty = 0; + protected function testLevel3(array $m):int{ + $penalties = 0; - foreach($this->matrix->matrix() as $y => $row){ + foreach($m as $y => $row){ foreach($row as $x => $val){ - if($x <= $this->moduleCount - 7){ - if( - $this->matrix->check($x , $y) - && !$this->matrix->check($x + 1, $y) - && $this->matrix->check($x + 2, $y) - && $this->matrix->check($x + 3, $y) - && $this->matrix->check($x + 4, $y) - && !$this->matrix->check($x + 5, $y) - && $this->matrix->check($x + 6, $y) - ){ - $penalty += 40; - } + if( + ($x + 6) < $this->moduleCount + && $val + && !$m[$y][$x + 1] + && $m[$y][$x + 2] + && $m[$y][$x + 3] + && $m[$y][$x + 4] + && !$m[$y][$x + 5] + && $m[$y][$x + 6] + ){ + $penalties++; } - if($y <= $this->moduleCount - 7){ - if( - $this->matrix->check($x, $y) - && !$this->matrix->check($x, $y + 1) - && $this->matrix->check($x, $y + 2) - && $this->matrix->check($x, $y + 3) - && $this->matrix->check($x, $y + 4) - && !$this->matrix->check($x, $y + 5) - && $this->matrix->check($x, $y + 6) - ){ - $penalty += 40; - } + if( + ($y + 6) < $this->moduleCount + && $val + && !$m[$y + 1][$x] + && $m[$y + 2][$x] + && $m[$y + 3][$x] + && $m[$y + 4][$x] + && !$m[$y + 5][$x] + && $m[$y + 6][$x] + ){ + $penalties++; } } } - return $penalty; + return $penalties * 40; } /** @@ -203,12 +186,12 @@ class MaskPatternTester{ * * @return float */ - protected function testLevel4():float{ + protected function testLevel4(array $m):float{ $count = 0; - foreach($this->matrix->matrix() as $y => $row){ + foreach($m as $y => $row){ foreach($row as $x => $val){ - if($val >> 8 > 0){ + if($val){ $count++; } } diff --git a/src/Data/QRMatrix.php b/src/Data/QRMatrix.php index 1c9c9b81d..98defe6b0 100755 --- a/src/Data/QRMatrix.php +++ b/src/Data/QRMatrix.php @@ -220,10 +220,27 @@ class QRMatrix{ } /** - * @return array + * Returns the data matrix, returns a pure boolean representation if $boolean is set to true + * + * @return int[][]|bool[][] */ - public function matrix():array{ - return $this->matrix; + public function matrix(bool $boolean = false):array{ + + if(!$boolean){ + return $this->matrix; + } + + $matrix = []; + + foreach($this->matrix as $y => $row){ + $matrix[$y] = []; + + foreach($row as $x => $val){ + $matrix[$y][$x] = ($val >> 8) > 0; + } + } + + return $matrix; } /** @@ -600,6 +617,9 @@ class QRMatrix{ /** * ISO/IEC 18004:2000 Section 8.8.1 * + * Note that some versions of the QR code standard have had errors in the section about mask patterns. + * The information below has been corrected. (https://www.thonky.com/qr-code-tutorial/mask-patterns) + * * @see \chillerlan\QRCode\QRMatrix::mapData() * * @internal @@ -617,10 +637,10 @@ class QRMatrix{ 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; }, + 0b001 => function($x, $y):int{ return $y % 2; }, + 0b010 => function($x, $y):int{ return $x % 3; }, 0b011 => function($x, $y):int{ return ($x + $y) % 3; }, - 0b100 => function($x, $y):int{ return ((int)($x / 2) + (int)($y / 3)) % 2; }, + 0b100 => function($x, $y):int{ return ((int)($y / 2) + (int)($x / 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; }, diff --git a/tests/Data/MaskPatternTesterTest.php b/tests/Data/MaskPatternTesterTest.php index 208ff4160..ae4af9f29 100644 --- a/tests/Data/MaskPatternTesterTest.php +++ b/tests/Data/MaskPatternTesterTest.php @@ -21,10 +21,9 @@ class MaskPatternTesterTest extends QRTestAbstract{ // coverage public function testMaskpattern(){ - $matrix = (new Byte(new QROptions(['version' => 10]), 'test'))->initMatrix(0, true); + $matrix = (new Byte(new QROptions(['version' => 10]), 'test'))->initMatrix(3, true); - $this->assertSame(6178, (new MaskPatternTester($matrix))->testPattern()); + $this->assertSame(4243, (new MaskPatternTester($matrix))->testPattern()); } - } diff --git a/tests/QRCodeTest.php b/tests/QRCodeTest.php index 3436c4c28..452cfacba 100755 --- a/tests/QRCodeTest.php +++ b/tests/QRCodeTest.php @@ -109,7 +109,7 @@ class QRCodeTest extends QRTestAbstract{ 'outputInterface' => MyCustomOutput::class, ]); - $expected = '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110101011110100001011110011111110000000010000010011111001010011111001010000010000000010111010111000001101011000001010111010000000010111010111010110000011010110010111010000000010111010100110101100000110101010111010000000010000010001101011000001101011010000010000000011111110101010101010101010101011111110000000000000000001010011111001010011000000000000000011110010101111010000101111010100111010000000001110001001011110100001011110110010010000000001100010011111001010011111001011110010000000011010000101000001101011000001011001010000000001101011010010110000011010110100000100000000000001001001110101100000110101101011100000000011100010100101011000001101011001100000000000000001000101100101001111100101111101010000000000111011111010111101000010111101100000000000001111000010000101111010000101101001110000000000100011110001111100101001111101000110000000010001001001101100000110101100110100010000000011100111001001101011000001101111011000000000010110101000000011010110000011011101100000000001111011110000110101100000110100001000000000010111100001111110010100111110100110100000000011001011111100001011110100001011010110000000000100101001101000010111101000000100110000000001011011100010100111110010100110011100000000010010101010011010110000011010000010010000000000111011101100000110101100001111110000000000000000000111011000001101011001000110110000000011111110000110000011010110011010111110000000010000010010010011111001010011000111100000000010111010010111010000101111011111101100000000010111010101011110100001011111100010010000000010111010111111001010011111011101010100000000010000010111000001101011000011001101000000000011111110111010110000011010110111100110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'; + $expected = '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110111010000101111010000011111110000000010000010111000001101011000001010000010000000010111010101101011000001101011010111010000000010111010110100111110010100111010111010000000010111010000001101011000001101010111010000000010000010100111110010100111110010000010000000011111110101010101010101010101011111110000000000000000010010100111110010100000000000000000011001110000101111010000101111001011110000000000000000111010000101111010000111100010000000001011010100111110010100111110011001010000000010000101111101011000001101011110011110000000000011010100011000001101011000101110100000000011001100001001101011000001101010011010000000010110111110000001101011000001100110100000000010000100100010100111110010100001100100000000011111110111101111010000101111010100110000000011010000111010000101111010000111100100000000010101111111111110010100111110011001000000000010110001110101011000001101011110011010000000001001111100011000001101011000101110010000000011000100110001101011000001101010011100000000001000011001000001101011000001100110000000000011101001011010100111110010100001100000000000010111010001101111010000101111010100110000000011100000001010000101111010000111100000000000000001110110111110010100111110011001000000000000011001011101011000001101011110011100000000011111110101011000001101011001111110110000000000000000110001101011000001101000111100000000011111110001000001101011000011010110000000000010000010101010100111110010101000100100000000010111010111101111010000101111111100110000000010111010011010000101111010001101100010000000010111010000111110010100111100101101100000000010000010101101011000001101001100111100000000011111110101011000001101011000110010110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'; $this->assertSame($expected, $this->reflection->newInstanceArgs([$options])->render('test')); }