| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class MaskPattern |
| 4 |
* |
| 5 |
* @created 19.01.2021 |
| 6 |
* @author ZXing Authors |
| 7 |
* @author Smiley <[email protected]> |
| 8 |
* @copyright 2021 Smiley |
| 9 |
* @license Apache-2.0 |
| 10 |
*/ |
| 11 |
declare(strict_types=1); |
| 12 |
|
| 13 |
namespace chillerlan\QRCode\Common; |
| 14 |
|
| 15 |
use chillerlan\QRCode\QRCodeException; |
| 16 |
use chillerlan\QRCode\Data\QRMatrix; |
| 17 |
use Closure; |
| 18 |
use function abs, array_column, array_search, intdiv, min; |
| 19 |
|
| 20 |
/** |
| 21 |
* ISO/IEC 18004:2000 Section 8.8.1 |
| 22 |
* ISO/IEC 18004:2000 Section 8.8.2 - Evaluation of masking results |
| 23 |
* |
| 24 |
* @see http://www.thonky.com/qr-code-tutorial/data-masking |
| 25 |
* @see https://github.com/zxing/zxing/blob/e9e2bd280bcaeabd59d0f955798384fe6c018a6c/core/src/main/java/com/google/zxing/qrcode/encoder/MaskUtil.java |
| 26 |
*/ |
| 27 |
final class MaskPattern{ |
| 28 |
|
| 29 |
/** |
| 30 |
* @see \chillerlan\QRCode\QROptionsTrait::$maskPattern |
| 31 |
* |
| 32 |
* @var int |
| 33 |
*/ |
| 34 |
public const AUTO = -1; |
| 35 |
|
| 36 |
public const PATTERN_000 = 0b000; |
| 37 |
public const PATTERN_001 = 0b001; |
| 38 |
public const PATTERN_010 = 0b010; |
| 39 |
public const PATTERN_011 = 0b011; |
| 40 |
public const PATTERN_100 = 0b100; |
| 41 |
public const PATTERN_101 = 0b101; |
| 42 |
public const PATTERN_110 = 0b110; |
| 43 |
public const PATTERN_111 = 0b111; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var int[] |
| 47 |
*/ |
| 48 |
public const PATTERNS = [ |
| 49 |
self::PATTERN_000, |
| 50 |
self::PATTERN_001, |
| 51 |
self::PATTERN_010, |
| 52 |
self::PATTERN_011, |
| 53 |
self::PATTERN_100, |
| 54 |
self::PATTERN_101, |
| 55 |
self::PATTERN_110, |
| 56 |
self::PATTERN_111, |
| 57 |
]; |
| 58 |
|
| 59 |
/* |
| 60 |
* Penalty scores |
| 61 |
* |
| 62 |
* ISO/IEC 18004:2000 Section 8.8.1 - Table 24 |
| 63 |
*/ |
| 64 |
private const PENALTY_N1 = 3; |
| 65 |
private const PENALTY_N2 = 3; |
| 66 |
private const PENALTY_N3 = 40; |
| 67 |
private const PENALTY_N4 = 10; |
| 68 |
|
| 69 |
/** |
| 70 |
* The current mask pattern value (0-7) |
| 71 |
*/ |
| 72 |
private int $currentPattern; |
| 73 |
|
| 74 |
/** |
| 75 |
* MaskPattern constructor. |
| 76 |
* |
| 77 |
* @throws \chillerlan\QRCode\QRCodeException |
| 78 |
*/ |
| 79 |
public function __construct(int $maskPattern){ |
| 80 |
|
| 81 |
if(($maskPattern & 0b111) !== $maskPattern){ |
| 82 |
throw new QRCodeException('invalid mask pattern'); |
| 83 |
} |
| 84 |
|
| 85 |
$this->currentPattern = $maskPattern; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the current mask pattern |
| 90 |
* |
| 91 |
* @deprecated 6.0.1 This method will be removed. In v7, use the property "MaskPattern::$currentPattern" instead. |
| 92 |
*/ |
| 93 |
public function getPattern():int{ |
| 94 |
return $this->currentPattern; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns a closure that applies the mask for the chosen mask pattern. |
| 99 |
* |
| 100 |
* Note that the diagram in section 6.8.1 is misleading since it indicates that $i is column position |
| 101 |
* and $j is row position. In fact, as the text says, $i is row position and $j is column position. |
| 102 |
* |
| 103 |
* @see https://www.thonky.com/qr-code-tutorial/mask-patterns |
| 104 |
* @see https://github.com/zxing/zxing/blob/e9e2bd280bcaeabd59d0f955798384fe6c018a6c/core/src/main/java/com/google/zxing/qrcode/decoder/DataMask.java#L32-L117 |
| 105 |
*/ |
| 106 |
public function getMask():Closure{ |
| 107 |
// $x = column (width), $y = row (height) |
| 108 |
return [ |
| 109 |
self::PATTERN_000 => fn(int $x, int $y):bool => (($x + $y) % 2) === 0, |
| 110 |
self::PATTERN_001 => fn(int $x, int $y):bool => ($y % 2) === 0, |
| 111 |
self::PATTERN_010 => fn(int $x, int $y):bool => ($x % 3) === 0, |
| 112 |
self::PATTERN_011 => fn(int $x, int $y):bool => (($x + $y) % 3) === 0, |
| 113 |
self::PATTERN_100 => fn(int $x, int $y):bool => ((intdiv($y, 2) + intdiv($x, 3)) % 2) === 0, |
| 114 |
self::PATTERN_101 => fn(int $x, int $y):bool => (($x * $y) % 6) === 0, |
| 115 |
self::PATTERN_110 => fn(int $x, int $y):bool => (($x * $y) % 6) < 3, |
| 116 |
self::PATTERN_111 => fn(int $x, int $y):bool => (($x + $y + (($x * $y) % 3)) % 2) === 0, |
| 117 |
][$this->currentPattern]; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Evaluates the matrix of the given data interface and returns a new mask pattern instance for the best result |
| 122 |
*/ |
| 123 |
public static function getBestPattern(QRMatrix $QRMatrix):self{ |
| 124 |
$penalties = []; |
| 125 |
$size = $QRMatrix->getSize(); |
| 126 |
|
| 127 |
foreach(self::PATTERNS as $pattern){ |
| 128 |
$mp = new self($pattern); |
| 129 |
$matrix = (clone $QRMatrix)->setFormatInfo($mp)->mask($mp)->getBooleanMatrix(); |
| 130 |
$penalty = 0; |
| 131 |
|
| 132 |
for($level = 1; $level <= 4; $level++){ |
| 133 |
$penalty += self::{'testRule'.$level}($matrix, $size, $size); |
| 134 |
} |
| 135 |
|
| 136 |
$penalties[$pattern] = (int)$penalty; |
| 137 |
} |
| 138 |
|
| 139 |
return new self(array_search(min($penalties), $penalties, true)); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Apply mask penalty rule 1 and return the penalty. Find repetitive cells with the same color and |
| 144 |
* give penalty to them. Example: 00000 or 11111. |
| 145 |
* |
| 146 |
* @param bool[][] $matrix |
| 147 |
*/ |
| 148 |
public static function testRule1(array $matrix, int $height, int $width):int{ |
| 149 |
$penalty = 0; |
| 150 |
|
| 151 |
// horizontal |
| 152 |
foreach($matrix as $row){ |
| 153 |
$penalty += self::applyRule1($row); |
| 154 |
} |
| 155 |
|
| 156 |
// vertical |
| 157 |
for($x = 0; $x < $width; $x++){ |
| 158 |
$penalty += self::applyRule1(array_column($matrix, $x)); |
| 159 |
} |
| 160 |
|
| 161 |
return $penalty; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* @param bool[] $rc |
| 166 |
*/ |
| 167 |
private static function applyRule1(array $rc):int{ |
| 168 |
$penalty = 0; |
| 169 |
$numSameBitCells = 0; |
| 170 |
$prevBit = null; |
| 171 |
|
| 172 |
foreach($rc as $val){ |
| 173 |
|
| 174 |
if($val === $prevBit){ |
| 175 |
$numSameBitCells++; |
| 176 |
} |
| 177 |
else{ |
| 178 |
|
| 179 |
if($numSameBitCells >= 5){ |
| 180 |
$penalty += (self::PENALTY_N1 + $numSameBitCells - 5); |
| 181 |
} |
| 182 |
|
| 183 |
$numSameBitCells = 1; // Include the cell itself. |
| 184 |
$prevBit = $val; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if($numSameBitCells >= 5){ |
| 189 |
$penalty += (self::PENALTY_N1 + $numSameBitCells - 5); |
| 190 |
} |
| 191 |
|
| 192 |
return $penalty; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give |
| 197 |
* penalty to them. This is actually equivalent to the spec's rule, which is to find MxN blocks and give a |
| 198 |
* penalty proportional to (M-1)x(N-1), because this is the number of 2x2 blocks inside such a block. |
| 199 |
* |
| 200 |
* @param bool[][] $matrix |
| 201 |
*/ |
| 202 |
public static function testRule2(array $matrix, int $height, int $width):int{ |
| 203 |
$penalty = 0; |
| 204 |
|
| 205 |
foreach($matrix as $y => $row){ |
| 206 |
|
| 207 |
if($y > ($height - 2)){ |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
foreach($row as $x => $val){ |
| 212 |
|
| 213 |
if($x > ($width - 2)){ |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
if( |
| 218 |
$val === $row[($x + 1)] |
| 219 |
&& $val === $matrix[($y + 1)][$x] |
| 220 |
&& $val === $matrix[($y + 1)][($x + 1)] |
| 221 |
){ |
| 222 |
$penalty++; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return (self::PENALTY_N2 * $penalty); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Apply mask penalty rule 3 and return the penalty. Find consecutive runs of 1:1:3:1:1:4 |
| 232 |
* starting with black, or 4:1:1:3:1:1 starting with white, and give penalty to them. If we |
| 233 |
* find patterns like 000010111010000, we give penalty once. |
| 234 |
* |
| 235 |
* @param bool[][] $matrix |
| 236 |
*/ |
| 237 |
public static function testRule3(array $matrix, int $height, int $width):int{ |
| 238 |
$penalties = 0; |
| 239 |
|
| 240 |
foreach($matrix as $y => $row){ |
| 241 |
foreach($row as $x => $val){ |
| 242 |
|
| 243 |
if( |
| 244 |
($x + 6) < $width |
| 245 |
&& $val |
| 246 |
&& !$row[($x + 1)] |
| 247 |
&& $row[($x + 2)] |
| 248 |
&& $row[($x + 3)] |
| 249 |
&& $row[($x + 4)] |
| 250 |
&& !$row[($x + 5)] |
| 251 |
&& $row[($x + 6)] |
| 252 |
&& ( |
| 253 |
self::isWhiteHorizontal($row, $width, ($x - 4), $x) |
| 254 |
|| self::isWhiteHorizontal($row, $width, ($x + 7), ($x + 11)) |
| 255 |
) |
| 256 |
){ |
| 257 |
$penalties++; |
| 258 |
} |
| 259 |
|
| 260 |
if( |
| 261 |
($y + 6) < $height |
| 262 |
&& $val |
| 263 |
&& !$matrix[($y + 1)][$x] |
| 264 |
&& $matrix[($y + 2)][$x] |
| 265 |
&& $matrix[($y + 3)][$x] |
| 266 |
&& $matrix[($y + 4)][$x] |
| 267 |
&& !$matrix[($y + 5)][$x] |
| 268 |
&& $matrix[($y + 6)][$x] |
| 269 |
&& ( |
| 270 |
self::isWhiteVertical($matrix, $height, $x, ($y - 4), $y) |
| 271 |
|| self::isWhiteVertical($matrix, $height, $x, ($y + 7), ($y + 11)) |
| 272 |
) |
| 273 |
){ |
| 274 |
$penalties++; |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
return ($penalties * self::PENALTY_N3); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* @param bool[] $row |
| 285 |
*/ |
| 286 |
private static function isWhiteHorizontal(array $row, int $width, int $from, int $to):bool{ |
| 287 |
|
| 288 |
if($from < 0 || $width < $to){ |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
for($x = $from; $x < $to; $x++){ |
| 293 |
if($row[$x]){ |
| 294 |
return false; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
return true; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* @param bool[][] $matrix |
| 303 |
*/ |
| 304 |
private static function isWhiteVertical(array $matrix, int $height, int $x, int $from, int $to):bool{ |
| 305 |
|
| 306 |
if($from < 0 || $height < $to){ |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
for($y = $from; $y < $to; $y++){ |
| 311 |
if($matrix[$y][$x] === true){ |
| 312 |
return false; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
return true; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give |
| 321 |
* penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance. |
| 322 |
* |
| 323 |
* @param bool[][] $matrix |
| 324 |
*/ |
| 325 |
public static function testRule4(array $matrix, int $height, int $width):int{ |
| 326 |
$darkCells = 0; |
| 327 |
$totalCells = ($height * $width); |
| 328 |
|
| 329 |
foreach($matrix as $row){ |
| 330 |
foreach($row as $val){ |
| 331 |
if($val === true){ |
| 332 |
$darkCells++; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
return (intdiv((abs($darkCells * 2 - $totalCells) * 10), $totalCells) * self::PENALTY_N4); |
| 338 |
} |
| 339 |
|
| 340 |
} |