QRMatrix.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. <?php
  2. /**
  3. * Class QRMatrix
  4. *
  5. * @created 15.11.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Data;
  11. use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, ReedSolomonEncoder, Version};
  12. use function array_fill, count, floor, range;
  13. /**
  14. * Holds an array representation of the final QR Code that contains numerical values for later output modifications;
  15. * maps the ECC coded binary data and applies the mask pattern
  16. *
  17. * @see http://www.thonky.com/qr-code-tutorial/format-version-information
  18. */
  19. class QRMatrix{
  20. /** @var int */
  21. public const IS_DARK = 0b100000000000;
  22. /** @var int */
  23. public const M_NULL = 0b000000000000;
  24. /** @var int */
  25. public const M_DARKMODULE = 0b100000000001;
  26. /** @var int */
  27. public const M_DATA = 0b000000000010;
  28. /** @var int */
  29. public const M_DATA_DARK = 0b100000000010;
  30. /** @var int */
  31. public const M_FINDER = 0b000000000100;
  32. /** @var int */
  33. public const M_FINDER_DARK = 0b100000000100;
  34. /** @var int */
  35. public const M_SEPARATOR = 0b000000001000;
  36. /** @var int */
  37. public const M_ALIGNMENT = 0b000000010000;
  38. /** @var int */
  39. public const M_ALIGNMENT_DARK = 0b100000010000;
  40. /** @var int */
  41. public const M_TIMING = 0b000000100000;
  42. /** @var int */
  43. public const M_TIMING_DARK = 0b100000100000;
  44. /** @var int */
  45. public const M_FORMAT = 0b000001000000;
  46. /** @var int */
  47. public const M_FORMAT_DARK = 0b100001000000;
  48. /** @var int */
  49. public const M_VERSION = 0b000010000000;
  50. /** @var int */
  51. public const M_VERSION_DARK = 0b100010000000;
  52. /** @var int */
  53. public const M_QUIETZONE = 0b000100000000;
  54. /** @var int */
  55. public const M_LOGO = 0b001000000000;
  56. /** @var int */
  57. public const M_FINDER_DOT = 0b110000000000;
  58. /** @var int */
  59. public const M_TEST = 0b011111111111;
  60. /** @var int */
  61. public const M_TEST_DARK = 0b111111111111;
  62. /**
  63. * Map of flag => coord
  64. *
  65. * @see \chillerlan\QRCode\Data\QRMatrix::checkNeighbours()
  66. *
  67. * @var array
  68. */
  69. protected const neighbours = [
  70. 0b00000001 => [-1, -1],
  71. 0b00000010 => [ 0, -1],
  72. 0b00000100 => [ 1, -1],
  73. 0b00001000 => [ 1, 0],
  74. 0b00010000 => [ 1, 1],
  75. 0b00100000 => [ 0, 1],
  76. 0b01000000 => [-1, 1],
  77. 0b10000000 => [-1, 0],
  78. ];
  79. /**
  80. * the matrix version - always set in QRMatrix, may be null in BitMatrix
  81. */
  82. protected ?Version $version = null;
  83. /**
  84. * the current ECC level - always set in QRMatrix, may be null in BitMatrix
  85. */
  86. protected ?EccLevel $eccLevel = null;
  87. /**
  88. * the mask pattern that was used in the most recent operation, set via:
  89. *
  90. * - QRMatrix::setFormatInfo()
  91. * - QRMatrix::mask()
  92. * - BitMatrix::readFormatInformation()
  93. */
  94. protected ?MaskPattern $maskPattern = null;
  95. /**
  96. * the size (side length) of the matrix, including quiet zone (if created)
  97. */
  98. protected int $moduleCount;
  99. /**
  100. * the actual matrix data array
  101. *
  102. * @var int[][]
  103. */
  104. protected array $matrix;
  105. /**
  106. * QRMatrix constructor.
  107. */
  108. public function __construct(Version $version, EccLevel $eccLevel){
  109. $this->version = $version;
  110. $this->eccLevel = $eccLevel;
  111. $this->moduleCount = $this->version->getDimension();
  112. $this->matrix = $this->createMatrix($this->moduleCount, $this::M_NULL);
  113. }
  114. /**
  115. * Creates a 2-dimensional array (square) of the given $size
  116. */
  117. protected function createMatrix(int $size, int $value):array{
  118. return array_fill(0, $size, array_fill(0, $size, $value));
  119. }
  120. /**
  121. * shortcut to initialize the functional patterns
  122. */
  123. public function initFunctionalPatterns():self{
  124. return $this
  125. ->setFinderPattern()
  126. ->setSeparators()
  127. ->setAlignmentPattern()
  128. ->setTimingPattern()
  129. ->setDarkModule()
  130. ->setVersionNumber()
  131. ->setFormatInfo()
  132. ;
  133. }
  134. /**
  135. * Returns the data matrix, returns a pure boolean representation if $boolean is set to true
  136. *
  137. * @return int[][]|bool[][]
  138. */
  139. public function getMatrix(bool $boolean = null):array{
  140. $matrix = [];
  141. foreach($this->matrix as $y => $row){
  142. $matrix[$y] = [];
  143. foreach($row as $x => $val){
  144. $matrix[$y][$x] = $boolean === true
  145. ? $this->checkType($x, $y, $this::IS_DARK)
  146. : $this->get($x, $y);
  147. }
  148. }
  149. return $matrix;
  150. }
  151. /**
  152. * @deprecated 5.0.0 use QRMatrix::getMatrix() instead
  153. * @see \chillerlan\QRCode\Data\QRMatrix::getMatrix()
  154. * @codeCoverageIgnore
  155. */
  156. public function matrix(bool $boolean = null):array{
  157. return $this->getMatrix($boolean);
  158. }
  159. /**
  160. * Returns the current version number
  161. */
  162. public function getVersion():?Version{
  163. return $this->version;
  164. }
  165. /**
  166. * @deprecated 5.0.0 use QRMatrix::getVersion() instead
  167. * @see \chillerlan\QRCode\Data\QRMatrix::getVersion()
  168. * @codeCoverageIgnore
  169. */
  170. public function version():?Version{
  171. return $this->getVersion();
  172. }
  173. /**
  174. * Returns the current ECC level
  175. */
  176. public function getEccLevel():?EccLevel{
  177. return $this->eccLevel;
  178. }
  179. /**
  180. * @deprecated 5.0.0 use QRMatrix::getEccLevel() instead
  181. * @see \chillerlan\QRCode\Data\QRMatrix::getEccLevel()
  182. * @codeCoverageIgnore
  183. */
  184. public function eccLevel():?EccLevel{
  185. return $this->getEccLevel();
  186. }
  187. /**
  188. * Returns the current mask pattern
  189. */
  190. public function getMaskPattern():?MaskPattern{
  191. return $this->maskPattern;
  192. }
  193. /**
  194. * @deprecated 5.0.0 use QRMatrix::getMaskPattern() instead
  195. * @see \chillerlan\QRCode\Data\QRMatrix::getMaskPattern()
  196. * @codeCoverageIgnore
  197. */
  198. public function maskPattern():?MaskPattern{
  199. return $this->getMaskPattern();
  200. }
  201. /**
  202. * Returns the absoulute size of the matrix, including quiet zone (after setting it).
  203. *
  204. * size = version * 4 + 17 [ + 2 * quietzone size]
  205. */
  206. public function getSize():int{
  207. return $this->moduleCount;
  208. }
  209. /**
  210. * @deprecated 5.0.0 use QRMatrix::getSize() instead
  211. * @see \chillerlan\QRCode\Data\QRMatrix::getSize()
  212. * @codeCoverageIgnore
  213. */
  214. public function size():int{
  215. return $this->getSize();
  216. }
  217. /**
  218. * Returns the value of the module at position [$x, $y] or -1 if the coordinate is outside the matrix
  219. */
  220. public function get(int $x, int $y):int{
  221. if(!isset($this->matrix[$y][$x])){
  222. return -1;
  223. }
  224. return $this->matrix[$y][$x];
  225. }
  226. /**
  227. * Sets the $M_TYPE value for the module at position [$x, $y]
  228. *
  229. * true => $M_TYPE | 0x800
  230. * false => $M_TYPE
  231. */
  232. public function set(int $x, int $y, bool $value, int $M_TYPE):self{
  233. if(isset($this->matrix[$y][$x])){
  234. $this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0));
  235. }
  236. return $this;
  237. }
  238. /**
  239. * Fills an area of $width * $height, from the given starting point [$startX, $startY] (top left) with $value for $M_TYPE.
  240. */
  241. public function setArea(int $startX, int $startY, int $width, int $height, bool $value, int $M_TYPE):self{
  242. for($y = $startY; $y < ($startY + $height); $y++){
  243. for($x = $startX; $x < ($startX + $width); $x++){
  244. $this->set($x, $y, $value, $M_TYPE);
  245. }
  246. }
  247. return $this;
  248. }
  249. /**
  250. * Flips the value of the module at ($x, $y)
  251. */
  252. public function flip(int $x, int $y):self{
  253. if(isset($this->matrix[$y][$x])){
  254. $this->matrix[$y][$x] ^= $this::IS_DARK;
  255. }
  256. return $this;
  257. }
  258. /**
  259. * Checks whether the module at ($x, $y) is of the given $M_TYPE
  260. *
  261. * true => $value & $M_TYPE === $M_TYPE
  262. */
  263. public function checkType(int $x, int $y, int $M_TYPE):bool{
  264. $val = $this->get($x, $y);
  265. if($val === -1){
  266. return false;
  267. }
  268. return ($val & $M_TYPE) === $M_TYPE;
  269. }
  270. /**
  271. * checks whether the module at ($x, $y) is in the given array of $M_TYPES,
  272. * returns true if a match is found, otherwise false.
  273. */
  274. public function checkTypeIn(int $x, int $y, array $M_TYPES):bool{
  275. foreach($M_TYPES as $type){
  276. if($this->checkType($x, $y, $type)){
  277. return true;
  278. }
  279. }
  280. return false;
  281. }
  282. /**
  283. * Checks whether the module at ($x, $y) is true (dark) or false (light)
  284. */
  285. public function check(int $x, int $y):bool{
  286. return $this->checkType($x, $y, $this::IS_DARK);
  287. }
  288. /**
  289. * Checks the status of the neighbouring modules for the module at ($x, $y) and returns a bitmask with the results.
  290. *
  291. * The 8 flags of the bitmask represent the status of each of the neighbouring fields,
  292. * starting with the lowest bit for top left, going clockwise:
  293. *
  294. * 1 2 3
  295. * 8 # 4
  296. * 7 6 5
  297. */
  298. public function checkNeighbours(int $x, int $y, int $M_TYPE = null):int{
  299. $bits = 0;
  300. foreach($this::neighbours as $bit => $coord){
  301. [$ix, $iy] = $coord;
  302. $ix += $x;
  303. $iy += $y;
  304. // $M_TYPE is given, skip if the field is not the same type
  305. if($M_TYPE !== null && !$this->checkType($ix, $iy, $M_TYPE)){
  306. continue;
  307. }
  308. if($this->checkType($ix, $iy, $this::IS_DARK)){
  309. $bits |= $bit;
  310. }
  311. }
  312. return $bits;
  313. }
  314. /**
  315. * Sets the "dark module", that is always on the same position 1x1px away from the bottom left finder
  316. *
  317. * 4 * version + 9 or moduleCount - 8
  318. */
  319. public function setDarkModule():self{
  320. $this->set(8, ($this->moduleCount - 8), true, $this::M_DARKMODULE);
  321. return $this;
  322. }
  323. /**
  324. * Draws the 7x7 finder patterns in the corners top left/right and bottom left
  325. *
  326. * ISO/IEC 18004:2000 Section 7.3.2
  327. */
  328. public function setFinderPattern():self{
  329. $pos = [
  330. [0, 0], // top left
  331. [($this->moduleCount - 7), 0], // top right
  332. [0, ($this->moduleCount - 7)], // bottom left
  333. ];
  334. foreach($pos as $c){
  335. $this
  336. ->setArea($c[0], $c[1], 7, 7, true, $this::M_FINDER)
  337. ->setArea(($c[0] + 1), ($c[1] + 1), 5, 5, false, $this::M_FINDER)
  338. ->setArea(($c[0] + 2), ($c[1] + 2), 3, 3, true, $this::M_FINDER_DOT)
  339. ;
  340. }
  341. return $this;
  342. }
  343. /**
  344. * Draws the separator lines around the finder patterns
  345. *
  346. * ISO/IEC 18004:2000 Section 7.3.3
  347. */
  348. public function setSeparators():self{
  349. $h = [
  350. [7, 0],
  351. [($this->moduleCount - 8), 0],
  352. [7, ($this->moduleCount - 8)],
  353. ];
  354. $v = [
  355. [7, 7],
  356. [($this->moduleCount - 1), 7],
  357. [7, ($this->moduleCount - 8)],
  358. ];
  359. for($c = 0; $c < 3; $c++){
  360. for($i = 0; $i < 8; $i++){
  361. $this->set($h[$c][0] , ($h[$c][1] + $i), false, $this::M_SEPARATOR);
  362. $this->set(($v[$c][0] - $i), $v[$c][1] , false, $this::M_SEPARATOR);
  363. }
  364. }
  365. return $this;
  366. }
  367. /**
  368. * Draws the 5x5 alignment patterns
  369. *
  370. * ISO/IEC 18004:2000 Section 7.3.5
  371. */
  372. public function setAlignmentPattern():self{
  373. $alignmentPattern = $this->version->getAlignmentPattern();
  374. foreach($alignmentPattern as $y){
  375. foreach($alignmentPattern as $x){
  376. // skip existing patterns
  377. if($this->matrix[$y][$x] !== $this::M_NULL){
  378. continue;
  379. }
  380. $this
  381. ->setArea(($x - 2), ($y - 2), 5, 5, true, $this::M_ALIGNMENT)
  382. ->setArea(($x - 1), ($y - 1), 3, 3, false, $this::M_ALIGNMENT)
  383. ->set($x, $y, true, $this::M_ALIGNMENT)
  384. ;
  385. }
  386. }
  387. return $this;
  388. }
  389. /**
  390. * Draws the timing pattern (h/v checkered line between the finder patterns)
  391. *
  392. * ISO/IEC 18004:2000 Section 7.3.4
  393. */
  394. public function setTimingPattern():self{
  395. foreach(range(8, ($this->moduleCount - 8 - 1)) as $i){
  396. if($this->matrix[6][$i] !== $this::M_NULL || $this->matrix[$i][6] !== $this::M_NULL){
  397. continue;
  398. }
  399. $v = ($i % 2) === 0;
  400. $this->set($i, 6, $v, $this::M_TIMING); // h
  401. $this->set(6, $i, $v, $this::M_TIMING); // v
  402. }
  403. return $this;
  404. }
  405. /**
  406. * Draws the version information, 2x 3x6 pixel
  407. *
  408. * ISO/IEC 18004:2000 Section 8.10
  409. */
  410. public function setVersionNumber():self{
  411. $bits = $this->version->getVersionPattern();
  412. if($bits !== null){
  413. for($i = 0; $i < 18; $i++){
  414. $a = (int)($i / 3);
  415. $b = (($i % 3) + ($this->moduleCount - 8 - 3));
  416. $v = (($bits >> $i) & 1) === 1;
  417. $this->set($b, $a, $v, $this::M_VERSION); // ne
  418. $this->set($a, $b, $v, $this::M_VERSION); // sw
  419. }
  420. }
  421. return $this;
  422. }
  423. /**
  424. * Draws the format info along the finder patterns. If no $maskPattern, all format info modules will be set to false.
  425. *
  426. * ISO/IEC 18004:2000 Section 8.9
  427. */
  428. public function setFormatInfo(MaskPattern $maskPattern = null):self{
  429. $this->maskPattern = $maskPattern;
  430. $bits = ($this->maskPattern instanceof MaskPattern)
  431. ? $this->eccLevel->getformatPattern($this->maskPattern)
  432. : 0; // sets all format fields to false (test mode)
  433. for($i = 0; $i < 15; $i++){
  434. $v = (($bits >> $i) & 1) === 1;
  435. if($i < 6){
  436. $this->set(8, $i, $v, $this::M_FORMAT);
  437. }
  438. elseif($i < 8){
  439. $this->set(8, ($i + 1), $v, $this::M_FORMAT);
  440. }
  441. else{
  442. $this->set(8, ($this->moduleCount - 15 + $i), $v, $this::M_FORMAT);
  443. }
  444. if($i < 8){
  445. $this->set(($this->moduleCount - $i - 1), 8, $v, $this::M_FORMAT);
  446. }
  447. elseif($i < 9){
  448. $this->set(((15 - $i)), 8, $v, $this::M_FORMAT);
  449. }
  450. else{
  451. $this->set((15 - $i - 1), 8, $v, $this::M_FORMAT);
  452. }
  453. }
  454. return $this;
  455. }
  456. /**
  457. * Draws the "quiet zone" of $size around the matrix
  458. *
  459. * ISO/IEC 18004:2000 Section 7.3.7
  460. *
  461. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  462. */
  463. public function setQuietZone(int $quietZoneSize):self{
  464. if($this->matrix[($this->moduleCount - 1)][($this->moduleCount - 1)] === $this::M_NULL){
  465. throw new QRCodeDataException('use only after writing data');
  466. }
  467. // create a matrix with the new size
  468. $newSize = ($this->moduleCount + ($quietZoneSize * 2));
  469. $newMatrix = $this->createMatrix($newSize, $this::M_QUIETZONE);
  470. // copy over the current matrix
  471. for($y = 0; $y < $this->moduleCount; $y++){
  472. for($x = 0; $x < $this->moduleCount; $x++){
  473. $newMatrix[($y + $quietZoneSize)][($x + $quietZoneSize)] = $this->matrix[$y][$x];
  474. }
  475. }
  476. // set the new values
  477. $this->moduleCount = $newSize;
  478. $this->matrix = $newMatrix;
  479. return $this;
  480. }
  481. /**
  482. * Clears a space of $width * $height in order to add a logo or text.
  483. * If no $height is given, the space will be assumed a square of $width.
  484. *
  485. * Additionally, the logo space can be positioned within the QR Code - respecting the main functional patterns -
  486. * using $startX and $startY. If either of these are null, the logo space will be centered in that direction.
  487. * ECC level "H" (30%) is required.
  488. *
  489. * The coordinates of $startX and $startY do not include the quiet zone:
  490. * [0, 0] is always the top left module of the top left finder pattern, negative values go into the quiet zone top and left.
  491. *
  492. * Please note that adding a logo space minimizes the error correction capacity of the QR Code and
  493. * created images may become unreadable, especially when printed with a chance to receive damage.
  494. * Please test thoroughly before using this feature in production.
  495. *
  496. * This method should be called from within an output module (after the matrix has been filled with data).
  497. * Note that there is no restiction on how many times this method could be called on the same matrix instance.
  498. *
  499. * @link https://github.com/chillerlan/php-qrcode/issues/52
  500. *
  501. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  502. */
  503. public function setLogoSpace(int $width, int $height = null, int $startX = null, int $startY = null):self{
  504. // for logos, we operate in ECC H (30%) only
  505. if($this->eccLevel->getLevel() !== EccLevel::H){
  506. throw new QRCodeDataException('ECC level "H" required to add logo space');
  507. }
  508. if($height === null){
  509. $height = $width;
  510. }
  511. // if width and height happen to be negative or 0 (default value), just return - nothing to do
  512. if($width <= 0 || $height <= 0){
  513. return $this; // @codeCoverageIgnore
  514. }
  515. // $this->moduleCount includes the quiet zone (if created), we need the QR size here
  516. $length = $this->version->getDimension();
  517. // throw if the size is exceeds the qrcode size
  518. if($width > $length || $height > $length){
  519. throw new QRCodeDataException('logo dimensions exceed matrix size');
  520. }
  521. // we need uneven sizes to center the logo space, adjust if needed
  522. if($startX === null && ($width % 2) === 0){
  523. $width++;
  524. }
  525. if($startY === null && ($height % 2) === 0){
  526. $height++;
  527. }
  528. // throw if the logo space exceeds the maximum error correction capacity
  529. if(($width * $height) > floor($length * $length * 0.2)){
  530. throw new QRCodeDataException('logo space exceeds the maximum error correction capacity');
  531. }
  532. // quiet zone size
  533. $qz = (($this->moduleCount - $length) / 2);
  534. // skip quiet zone and the first 9 rows/columns (finder-, mode-, version- and timing patterns)
  535. $start = ($qz + 9);
  536. // skip quiet zone
  537. $end = ($this->moduleCount - $qz);
  538. // determine start coordinates
  539. $startX = ((($startX !== null) ? $startX : ($length - $width) / 2) + $qz);
  540. $startY = ((($startY !== null) ? $startY : ($length - $height) / 2) + $qz);
  541. $endX = ($startX + $width);
  542. $endY = ($startY + $height);
  543. // clear the space
  544. for($y = $startY; $y < $endY; $y++){
  545. for($x = $startX; $x < $endX; $x++){
  546. // out of bounds, skip
  547. if($x < $start || $y < $start ||$x >= $end || $y >= $end){
  548. continue;
  549. }
  550. $this->set($x, $y, false, $this::M_LOGO);
  551. }
  552. }
  553. return $this;
  554. }
  555. /**
  556. * Maps the interleaved binary $data on the matrix
  557. */
  558. public function writeCodewords(BitBuffer $bitBuffer):self{
  559. $data = (new ReedSolomonEncoder($this->version, $this->eccLevel))->interleaveEcBytes($bitBuffer);
  560. $byteCount = count($data);
  561. $iByte = 0;
  562. $iBit = 7;
  563. $direction = true;
  564. for($i = ($this->moduleCount - 1); $i > 0; $i -= 2){
  565. // skip vertical alignment pattern
  566. if($i === 6){
  567. $i--;
  568. }
  569. for($count = 0; $count < $this->moduleCount; $count++){
  570. $y = ($direction) ? ($this->moduleCount - 1 - $count) : $count;
  571. for($col = 0; $col < 2; $col++){
  572. $x = ($i - $col);
  573. // skip functional patterns
  574. if($this->get($x, $y) !== $this::M_NULL){
  575. continue;
  576. }
  577. $v = $iByte < $byteCount && (($data[$iByte] >> $iBit--) & 1) === 1;
  578. $this->set($x, $y, $v, $this::M_DATA);
  579. if($iBit === -1){
  580. $iByte++;
  581. $iBit = 7;
  582. }
  583. }
  584. }
  585. $direction = !$direction; // switch directions
  586. }
  587. return $this;
  588. }
  589. /**
  590. * Applies/reverses the mask pattern
  591. *
  592. * ISO/IEC 18004:2000 Section 8.8.1
  593. */
  594. public function mask(MaskPattern $maskPattern):self{
  595. $this->maskPattern = $maskPattern;
  596. $mask = $this->maskPattern->getMask();
  597. foreach($this->matrix as $y => $row){
  598. foreach($row as $x => $val){
  599. if($mask($x, $y) && ($val & $this::M_DATA) === $this::M_DATA){
  600. $this->flip($x, $y);
  601. }
  602. }
  603. }
  604. return $this;
  605. }
  606. }