:octocat: BitMatrix: simplify mirror method

This commit is contained in:
smiley
2023-04-10 01:21:40 +02:00
parent 5c37096c9d
commit 4bdfa772d5
2 changed files with 15 additions and 18 deletions
+14 -17
View File
@@ -13,7 +13,7 @@ namespace chillerlan\QRCode\Decoder;
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
use function array_fill, count;
use function array_fill, array_map, array_reverse, count;
use const PHP_INT_MAX, PHP_INT_SIZE;
/**
@@ -63,6 +63,10 @@ final class BitMatrix extends QRMatrix{
private const FORMAT_INFO_MASK_QR = 0x5412; // 0101010000010010
/**
* This flag has effect only on the copyVersionBit() method.
* Before proceeding with readCodewords() the resetInfo() method should be called.
*/
private bool $mirror = false;
/**
@@ -74,33 +78,26 @@ final class BitMatrix extends QRMatrix{
}
/**
* Prepare the parser for a mirrored operation.
* This flag has effect only on the readFormatInformation() and the
* readVersion() methods. Before proceeding with readCodewords() the
* mirror() method should be called.
* Resets the current version info in order to attempt another reading
*/
public function setMirror(bool $mirror):self{
public function resetVersionInfo():self{
$this->version = null;
$this->eccLevel = null;
$this->maskPattern = null;
$this->mirror = $mirror;
return $this;
}
/**
* Mirror the bit matrix in order to attempt a second reading.
* Mirror the bit matrix diagonally in order to attempt a second reading.
*/
public function mirror():self{
public function mirrorDiagonal():self{
$this->mirror = !$this->mirror;
for($x = 0; $x < $this->moduleCount; $x++){
for($y = ($x + 1); $y < $this->moduleCount; $y++){
if($this->get($x, $y) !== $this->get($y, $x)){
$this->flip($y, $x);
$this->flip($x, $y);
}
}
}
// mirror vertically
$matrix = array_reverse($this->matrix);
// rotate by 90 degrees clockwise
$this->matrix = array_map(fn(...$a) => array_reverse($a), ...$matrix);
return $this;
}
+1 -1
View File
@@ -54,7 +54,7 @@ final class Decoder{
* that the QR code may be mirrored, and we should try once more with a
* mirrored content.
*/
return $this->decodeMatrix($matrix->setMirror(true)->mirror());
return $this->decodeMatrix($matrix->resetVersionInfo()->mirrorDiagonal());
}
catch(Throwable $f){
// Throw the exception from the original reading