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