| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class MaskPatternTester |
| 5 |
* |
| 6 |
* @filesource MaskPatternTester.php |
| 7 |
* @created 22.11.2017 |
| 8 |
* @package chillerlan\QRCode\Data |
| 9 |
* @author Smiley <smiley@chillerlan.net> |
| 10 |
* @copyright 2017 Smiley |
| 11 |
* @license MIT |
| 12 |
*/ |
| 13 |
namespace YoastSEO_Vendor\chillerlan\QRCode\Data; |
| 14 |
|
| 15 |
/** |
| 16 |
* The sole purpose of this class is to receive a QRMatrix object and run the pattern tests on it. |
| 17 |
* |
| 18 |
* @link http://www.thonky.com/qr-code-tutorial/data-masking |
| 19 |
*/ |
| 20 |
class MaskPatternTester |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var \chillerlan\QRCode\Data\QRMatrix |
| 24 |
*/ |
| 25 |
protected $matrix; |
| 26 |
/** |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
protected $moduleCount; |
| 30 |
/** |
| 31 |
* Receives the matrix an sets the module count |
| 32 |
* |
| 33 |
* @see \chillerlan\QRCode\QROptions::$maskPattern |
| 34 |
* @see \chillerlan\QRCode\Data\QRMatrix::$maskPattern |
| 35 |
* @see \chillerlan\QRCode\QRCode::getBestMaskPattern() |
| 36 |
* |
| 37 |
* @param \chillerlan\QRCode\Data\QRMatrix $matrix |
| 38 |
* |
| 39 |
* @return \chillerlan\QRCode\Data\MaskPatternTester |
| 40 |
*/ |
| 41 |
public function setMatrix(\YoastSEO_Vendor\chillerlan\QRCode\Data\QRMatrix $matrix) |
| 42 |
{ |
| 43 |
$this->matrix = $matrix; |
| 44 |
$this->moduleCount = $this->matrix->size(); |
| 45 |
return $this; |
| 46 |
} |
| 47 |
/** |
| 48 |
* Returns the penalty for the given mask pattern |
| 49 |
* |
| 50 |
* @see \chillerlan\QRCode\QROptions::$maskPattern |
| 51 |
* @see \chillerlan\QRCode\Data\QRMatrix::$maskPattern |
| 52 |
* @see \chillerlan\QRCode\QRCode::getBestMaskPattern() |
| 53 |
* |
| 54 |
* @return int |
| 55 |
*/ |
| 56 |
public function testPattern() |
| 57 |
{ |
| 58 |
$penalty = 0; |
| 59 |
for ($level = 1; $level <= 4; $level++) { |
| 60 |
$penalty += \call_user_func_array([$this, 'testLevel' . $level], [$this->matrix->matrix(\true)]); |
| 61 |
} |
| 62 |
return (int) $penalty; |
| 63 |
} |
| 64 |
/** |
| 65 |
* Checks for each group of five or more same-colored modules in a row (or column) |
| 66 |
* |
| 67 |
* @return int |
| 68 |
*/ |
| 69 |
protected function testLevel1(array $m) |
| 70 |
{ |
| 71 |
$penalty = 0; |
| 72 |
foreach ($m as $y => $row) { |
| 73 |
foreach ($row as $x => $val) { |
| 74 |
$count = 0; |
| 75 |
for ($ry = -1; $ry <= 1; $ry++) { |
| 76 |
if ($y + $ry < 0 || $this->moduleCount <= $y + $ry) { |
| 77 |
continue; |
| 78 |
} |
| 79 |
for ($rx = -1; $rx <= 1; $rx++) { |
| 80 |
if ($ry === 0 && $rx === 0 || ($x + $rx < 0 || $this->moduleCount <= $x + $rx)) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
if ($m[$y + $ry][$x + $rx] === $val) { |
| 84 |
$count++; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
if ($count > 5) { |
| 89 |
$penalty += 3 + $count - 5; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return $penalty; |
| 94 |
} |
| 95 |
/** |
| 96 |
* Checks for each 2x2 area of same-colored modules in the matrix |
| 97 |
* |
| 98 |
* @return int |
| 99 |
*/ |
| 100 |
protected function testLevel2(array $m) |
| 101 |
{ |
| 102 |
$penalty = 0; |
| 103 |
foreach ($m as $y => $row) { |
| 104 |
if ($y > $this->moduleCount - 2) { |
| 105 |
break; |
| 106 |
} |
| 107 |
foreach ($row as $x => $val) { |
| 108 |
if ($x > $this->moduleCount - 2) { |
| 109 |
break; |
| 110 |
} |
| 111 |
if ($val === $m[$y][$x + 1] && $val === $m[$y + 1][$x] && $val === $m[$y + 1][$x + 1]) { |
| 112 |
$penalty++; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
return 3 * $penalty; |
| 117 |
} |
| 118 |
/** |
| 119 |
* Checks if there are patterns that look similar to the finder patterns (1:1:3:1:1 ratio) |
| 120 |
* |
| 121 |
* @return int |
| 122 |
*/ |
| 123 |
protected function testLevel3(array $m) |
| 124 |
{ |
| 125 |
$penalties = 0; |
| 126 |
foreach ($m as $y => $row) { |
| 127 |
foreach ($row as $x => $val) { |
| 128 |
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]) { |
| 129 |
$penalties++; |
| 130 |
} |
| 131 |
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]) { |
| 132 |
$penalties++; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
return $penalties * 40; |
| 137 |
} |
| 138 |
/** |
| 139 |
* Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference |
| 140 |
* |
| 141 |
* @return float |
| 142 |
*/ |
| 143 |
protected function testLevel4(array $m) |
| 144 |
{ |
| 145 |
$count = 0; |
| 146 |
foreach ($m as $y => $row) { |
| 147 |
foreach ($row as $x => $val) { |
| 148 |
if ($val) { |
| 149 |
$count++; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
return \abs(100 * $count / $this->moduleCount / $this->moduleCount - 50) / 5 * 10; |
| 154 |
} |
| 155 |
} |
| 156 |
|