Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Encoder/ByteMatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
45 changes: 25 additions & 20 deletions src/Encoder/MaskUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -81,37 +84,39 @@ 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]
)
)
) {
$penalty += self::N3;
}

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]
Expand Down
15 changes: 10 additions & 5 deletions src/Encoder/MatrixUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,27 +459,32 @@ 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.
if (6 === $x) {
--$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 {
Expand All @@ -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;
Expand Down