From c9108cde7d913c308eb5d72b2187eac7d3887c7e Mon Sep 17 00:00:00 2001 From: Mattias Geniar Date: Tue, 3 Nov 2026 14:31:38 +0000 Subject: [PATCH 1/3] Read the matrix rows directly while embedding data bits --- src/Encoder/MatrixUtil.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Encoder/MatrixUtil.php b/src/Encoder/MatrixUtil.php index 0967e29..8158ab5 100644 --- a/src/Encoder/MatrixUtil.php +++ b/src/Encoder/MatrixUtil.php @@ -459,10 +459,13 @@ private static function embedDataBits(BitArray $dataBits, int $maskPattern, Byte { $bitIndex = 0; $direction = -1; + $numBits = $dataBits->getSize(); + $height = $matrix->getHeight(); + $rows = $matrix->getArray(); // Start from the right bottom cell. $x = $matrix->getWidth() - 1; - $y = $matrix->getHeight() - 1; + $y = $height - 1; while ($x > 0) { // Skip vertical timing pattern. @@ -470,16 +473,18 @@ private static function embedDataBits(BitArray $dataBits, int $maskPattern, Byte --$x; } - while ($y >= 0 && $y < $matrix->getHeight()) { + while ($y >= 0 && $y < $height) { + $row = $rows[$y]; + for ($i = 0; $i < 2; $i++) { $xx = $x - $i; // Skip the cell if it's not empty. - if (-1 !== $matrix->get($xx, $y)) { + if (-1 !== $row[$xx]) { continue; } - if ($bitIndex < $dataBits->getSize()) { + if ($bitIndex < $numBits) { $bit = $dataBits->get($bitIndex); ++$bitIndex; } else { @@ -494,7 +499,7 @@ private static function embedDataBits(BitArray $dataBits, int $maskPattern, Byte $bit = ! $bit; } - $matrix->set($xx, $y, (int) $bit); + $row[$xx] = (int) $bit; } $y += $direction; From fe2e266131ae24eea7d301fd05906c7c310dc6fb Mon Sep 17 00:00:00 2001 From: Mattias Geniar Date: Tue, 3 Nov 2026 14:32:02 +0000 Subject: [PATCH 2/3] Clear the matrix row by row --- src/Encoder/ByteMatrix.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Encoder/ByteMatrix.php b/src/Encoder/ByteMatrix.php index eefcf1c..ae8b972 100644 --- a/src/Encoder/ByteMatrix.php +++ b/src/Encoder/ByteMatrix.php @@ -86,9 +86,9 @@ public function set(int $x, int $y, int $value) : void */ public function clear(int $value) : void { - for ($y = 0; $y < $this->height; ++$y) { + foreach ($this->bytes as $row) { for ($x = 0; $x < $this->width; ++$x) { - $this->bytes[$y][$x] = $value; + $row[$x] = $value; } } } From 0cbbb2fd9daf6991c1d5323302ac498eb9ad5507 Mon Sep 17 00:00:00 2001 From: Mattias Geniar Date: Tue, 3 Nov 2026 14:32:42 +0000 Subject: [PATCH 3/3] Read each row once in mask penalty rules 2 and 3 --- src/Encoder/MaskUtil.php | 45 ++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Encoder/MaskUtil.php b/src/Encoder/MaskUtil.php index cc85a40..1ae1fbc 100644 --- a/src/Encoder/MaskUtil.php +++ b/src/Encoder/MaskUtil.php @@ -51,12 +51,15 @@ public static function applyMaskPenaltyRule2(ByteMatrix $matrix) : int $height = $matrix->getHeight(); for ($y = 0; $y < $height - 1; ++$y) { + $row = $array[$y]; + $nextRow = $array[$y + 1]; + for ($x = 0; $x < $width - 1; ++$x) { - $value = $array[$y][$x]; + $value = $row[$x]; - if ($value === $array[$y][$x + 1] - && $value === $array[$y + 1][$x] - && $value === $array[$y + 1][$x + 1] + if ($value === $row[$x + 1] + && $value === $nextRow[$x] + && $value === $nextRow[$x + 1] ) { ++$penalty; } @@ -81,29 +84,31 @@ public static function applyMaskPenaltyRule3(ByteMatrix $matrix) : int $height = $matrix->getHeight(); for ($y = 0; $y < $height; ++$y) { + $row = $array[$y]; + for ($x = 0; $x < $width; ++$x) { if ($x + 6 < $width - && 1 === $array[$y][$x] - && 0 === $array[$y][$x + 1] - && 1 === $array[$y][$x + 2] - && 1 === $array[$y][$x + 3] - && 1 === $array[$y][$x + 4] - && 0 === $array[$y][$x + 5] - && 1 === $array[$y][$x + 6] + && 1 === $row[$x] + && 0 === $row[$x + 1] + && 1 === $row[$x + 2] + && 1 === $row[$x + 3] + && 1 === $row[$x + 4] + && 0 === $row[$x + 5] + && 1 === $row[$x + 6] && ( ( $x + 10 < $width - && 0 === $array[$y][$x + 7] - && 0 === $array[$y][$x + 8] - && 0 === $array[$y][$x + 9] - && 0 === $array[$y][$x + 10] + && 0 === $row[$x + 7] + && 0 === $row[$x + 8] + && 0 === $row[$x + 9] + && 0 === $row[$x + 10] ) || ( $x - 4 >= 0 - && 0 === $array[$y][$x - 1] - && 0 === $array[$y][$x - 2] - && 0 === $array[$y][$x - 3] - && 0 === $array[$y][$x - 4] + && 0 === $row[$x - 1] + && 0 === $row[$x - 2] + && 0 === $row[$x - 3] + && 0 === $row[$x - 4] ) ) ) { @@ -111,7 +116,7 @@ public static function applyMaskPenaltyRule3(ByteMatrix $matrix) : int } if ($y + 6 < $height - && 1 === $array[$y][$x] + && 1 === $row[$x] && 0 === $array[$y + 1][$x] && 1 === $array[$y + 2][$x] && 1 === $array[$y + 3][$x]