summaryrefslogtreecommitdiff
path: root/vendor/chillerlan/php-qrcode/src/Data/MaskPatternTester.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/chillerlan/php-qrcode/src/Data/MaskPatternTester.php')
-rw-r--r--vendor/chillerlan/php-qrcode/src/Data/MaskPatternTester.php84
1 files changed, 42 insertions, 42 deletions
diff --git a/vendor/chillerlan/php-qrcode/src/Data/MaskPatternTester.php b/vendor/chillerlan/php-qrcode/src/Data/MaskPatternTester.php
index 8dfd24180..7874cb53d 100644
--- a/vendor/chillerlan/php-qrcode/src/Data/MaskPatternTester.php
+++ b/vendor/chillerlan/php-qrcode/src/Data/MaskPatternTester.php
@@ -8,41 +8,51 @@
* @author Smiley <[email protected]>
* @copyright 2017 Smiley
* @license MIT
+ *
+ * @noinspection PhpUnused
*/
namespace chillerlan\QRCode\Data;
-use function abs, call_user_func_array;
+use function abs, array_search, call_user_func_array, min;
/**
- * The sole purpose of this class is to receive a QRMatrix object and run the pattern tests on it.
+ * Receives a QRDataInterface object and runs the mask pattern tests on it.
+ *
+ * ISO/IEC 18004:2000 Section 8.8.2 - Evaluation of masking results
*
- * @link http://www.thonky.com/qr-code-tutorial/data-masking
+ * @see http://www.thonky.com/qr-code-tutorial/data-masking
*/
-class MaskPatternTester{
-
- /**
- * @var \chillerlan\QRCode\Data\QRMatrix
- */
- protected $matrix;
+final class MaskPatternTester{
/**
- * @var int
+ * The data interface that contains the data matrix to test
*/
- protected $moduleCount;
+ protected QRDataInterface $dataInterface;
/**
- * Receives the matrix an sets the module count
+ * Receives the QRDataInterface
*
* @see \chillerlan\QRCode\QROptions::$maskPattern
* @see \chillerlan\QRCode\Data\QRMatrix::$maskPattern
- * @see \chillerlan\QRCode\QRCode::getBestMaskPattern()
+ */
+ public function __construct(QRDataInterface $dataInterface){
+ $this->dataInterface = $dataInterface;
+ }
+
+ /**
+ * shoves a QRMatrix through the MaskPatternTester to find the lowest penalty mask pattern
*
- * @param \chillerlan\QRCode\Data\QRMatrix $matrix
+ * @see \chillerlan\QRCode\Data\MaskPatternTester
*/
- public function __construct(QRMatrix $matrix){
- $this->matrix = $matrix;
- $this->moduleCount = $this->matrix->size();
+ public function getBestMaskPattern():int{
+ $penalties = [];
+
+ for($pattern = 0; $pattern < 8; $pattern++){
+ $penalties[$pattern] = $this->testPattern($pattern);
+ }
+
+ return array_search(min($penalties), $penalties, true);
}
/**
@@ -50,15 +60,13 @@ class MaskPatternTester{
*
* @see \chillerlan\QRCode\QROptions::$maskPattern
* @see \chillerlan\QRCode\Data\QRMatrix::$maskPattern
- * @see \chillerlan\QRCode\QRCode::getBestMaskPattern()
- *
- * @return int
*/
- public function testPattern():int{
- $penalty = 0;
+ public function testPattern(int $pattern):int{
+ $matrix = $this->dataInterface->initMatrix($pattern, true);
+ $penalty = 0;
for($level = 1; $level <= 4; $level++){
- $penalty += call_user_func_array([$this, 'testLevel'.$level], [$this->matrix->matrix(true)]);
+ $penalty += call_user_func_array([$this, 'testLevel'.$level], [$matrix->matrix(true), $matrix->size()]);
}
return (int)$penalty;
@@ -66,10 +74,8 @@ class MaskPatternTester{
/**
* Checks for each group of five or more same-colored modules in a row (or column)
- *
- * @return int
*/
- protected function testLevel1(array $m):int{
+ protected function testLevel1(array $m, int $size):int{
$penalty = 0;
foreach($m as $y => $row){
@@ -78,13 +84,13 @@ class MaskPatternTester{
for($ry = -1; $ry <= 1; $ry++){
- if($y + $ry < 0 || $this->moduleCount <= $y + $ry){
+ if($y + $ry < 0 || $size <= $y + $ry){
continue;
}
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 || $size <= ($x + $rx))){
continue;
}
@@ -107,21 +113,19 @@ class MaskPatternTester{
/**
* Checks for each 2x2 area of same-colored modules in the matrix
- *
- * @return int
*/
- protected function testLevel2(array $m):int{
+ protected function testLevel2(array $m, int $size):int{
$penalty = 0;
foreach($m as $y => $row){
- if($y > ($this->moduleCount - 2)){
+ if($y > $size - 2){
break;
}
foreach($row as $x => $val){
- if($x > ($this->moduleCount - 2)){
+ if($x > $size - 2){
break;
}
@@ -140,17 +144,15 @@ class MaskPatternTester{
/**
* Checks if there are patterns that look similar to the finder patterns (1:1:3:1:1 ratio)
- *
- * @return int
*/
- protected function testLevel3(array $m):int{
+ protected function testLevel3(array $m, int $size):int{
$penalties = 0;
foreach($m as $y => $row){
foreach($row as $x => $val){
if(
- ($x + 6) < $this->moduleCount
+ $x + 6 < $size
&& $val
&& !$m[$y][$x + 1]
&& $m[$y][$x + 2]
@@ -163,7 +165,7 @@ class MaskPatternTester{
}
if(
- ($y + 6) < $this->moduleCount
+ $y + 6 < $size
&& $val
&& !$m[$y + 1][$x]
&& $m[$y + 2][$x]
@@ -183,10 +185,8 @@ class MaskPatternTester{
/**
* Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference
- *
- * @return float
*/
- protected function testLevel4(array $m):float{
+ protected function testLevel4(array $m, int $size):float{
$count = 0;
foreach($m as $y => $row){
@@ -197,7 +197,7 @@ class MaskPatternTester{
}
}
- return (abs(100 * $count / $this->moduleCount / $this->moduleCount - 50) / 5) * 10;
+ return (abs(100 * $count / $size / $size - 50) / 5) * 10;
}
}