codemasher 5 лет назад
Родитель
Сommit
bf0382aaf2

+ 49 - 66
src/Data/MaskPatternTester.php

@@ -56,7 +56,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;
@@ -65,12 +65,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;
 
@@ -82,11 +82,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++;
 						}
 
@@ -106,94 +106,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($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(
+					   $val === $m[$y][$x + 1]
+					&& $val === $m[$y + 1][$x]
+					&& $val === $m[$y + 1][$x + 1]
+				){
+					$penalty++;
 				}
-
-				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;
 	}
 
 	/**
@@ -201,12 +184,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++;
 				}
 			}

+ 26 - 6
src/Data/QRMatrix.php

@@ -150,10 +150,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;
 	}
 
 	/**
@@ -532,6 +549,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
@@ -549,10 +569,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; },

+ 2 - 2
tests/Data/MaskPatternTesterTest.php

@@ -23,9 +23,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());
 	}
 
 

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
tests/QRCodeTest.php


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
tests/Traits/QRAuthenticatorTest.php


Некоторые файлы не были показаны из-за большого количества измененных файлов