Skip to content

Commit 4bdfa77

Browse files
committed
:octocat: BitMatrix: simplify mirror method
1 parent 5c37096 commit 4bdfa77

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

src/Decoder/BitMatrix.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
1515
use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
16-
use function array_fill, count;
16+
use function array_fill, array_map, array_reverse, count;
1717
use const PHP_INT_MAX, PHP_INT_SIZE;
1818

1919
/**
@@ -63,6 +63,10 @@ final class BitMatrix extends QRMatrix{
6363

6464
private const FORMAT_INFO_MASK_QR = 0x5412; // 0101010000010010
6565

66+
/**
67+
* This flag has effect only on the copyVersionBit() method.
68+
* Before proceeding with readCodewords() the resetInfo() method should be called.
69+
*/
6670
private bool $mirror = false;
6771

6872
/**
@@ -74,33 +78,26 @@ public function __construct(int $dimension){
7478
}
7579

7680
/**
77-
* Prepare the parser for a mirrored operation.
78-
* This flag has effect only on the readFormatInformation() and the
79-
* readVersion() methods. Before proceeding with readCodewords() the
80-
* mirror() method should be called.
81+
* Resets the current version info in order to attempt another reading
8182
*/
82-
public function setMirror(bool $mirror):self{
83+
public function resetVersionInfo():self{
8384
$this->version = null;
8485
$this->eccLevel = null;
8586
$this->maskPattern = null;
86-
$this->mirror = $mirror;
8787

8888
return $this;
8989
}
9090

9191
/**
92-
* Mirror the bit matrix in order to attempt a second reading.
92+
* Mirror the bit matrix diagonally in order to attempt a second reading.
9393
*/
94-
public function mirror():self{
94+
public function mirrorDiagonal():self{
95+
$this->mirror = !$this->mirror;
9596

96-
for($x = 0; $x < $this->moduleCount; $x++){
97-
for($y = ($x + 1); $y < $this->moduleCount; $y++){
98-
if($this->get($x, $y) !== $this->get($y, $x)){
99-
$this->flip($y, $x);
100-
$this->flip($x, $y);
101-
}
102-
}
103-
}
97+
// mirror vertically
98+
$matrix = array_reverse($this->matrix);
99+
// rotate by 90 degrees clockwise
100+
$this->matrix = array_map(fn(...$a) => array_reverse($a), ...$matrix);
104101

105102
return $this;
106103
}

src/Decoder/Decoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function decode(LuminanceSourceInterface $source):DecoderResult{
5454
* that the QR code may be mirrored, and we should try once more with a
5555
* mirrored content.
5656
*/
57-
return $this->decodeMatrix($matrix->setMirror(true)->mirror());
57+
return $this->decodeMatrix($matrix->resetVersionInfo()->mirrorDiagonal());
5858
}
5959
catch(Throwable $f){
6060
// Throw the exception from the original reading

0 commit comments

Comments
 (0)