From 50179a97efadbcd24c7e5be85dfeaaa6775f7227 Mon Sep 17 00:00:00 2001 From: smiley Date: Tue, 4 Apr 2023 20:21:04 +0200 Subject: [PATCH] :octocat: move mask pattern parameter --- src/Data/QRData.php | 5 +++-- src/Data/QRMatrix.php | 32 +++++++++++++++++---------- src/Decoder/BitMatrix.php | 10 ++++++--- src/Decoder/DecoderResult.php | 5 +++-- tests/Data/QRMatrixTest.php | 10 ++++----- tests/Output/QROutputTestAbstract.php | 4 +--- 6 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/Data/QRData.php b/src/Data/QRData.php index 4b5a09212..18cbaf050 100644 --- a/src/Data/QRData.php +++ b/src/Data/QRData.php @@ -123,10 +123,11 @@ final class QRData{ * returns a fresh matrix object with the data written and masked with the given $maskPattern */ public function writeMatrix(MaskPattern $maskPattern):QRMatrix{ - return (new QRMatrix($this->version, $this->eccLevel, $maskPattern)) + return (new QRMatrix($this->version, $this->eccLevel)) ->initFunctionalPatterns() ->writeCodewords($this->bitBuffer) - ->mask() + ->setFormatInfo($maskPattern) + ->mask($maskPattern) ; } diff --git a/src/Data/QRMatrix.php b/src/Data/QRMatrix.php index c40264b5f..4a27ccbd1 100755 --- a/src/Data/QRMatrix.php +++ b/src/Data/QRMatrix.php @@ -83,19 +83,23 @@ class QRMatrix{ ]; /** - * the used mask pattern, set via QRMatrix::mask() + * the matrix version - always set in QRMatrix, may be null in BitMatrix */ - protected ?MaskPattern $maskPattern = null; + protected ?Version $version = null; /** - * the current ECC level + * the current ECC level - always set in QRMatrix, may be null in BitMatrix */ protected ?EccLevel $eccLevel = null; /** - * a Version instance + * the mask pattern that was used in the most recent operation, set via: + * + * - QRMatrix::setFormatInfo() + * - QRMatrix::mask() + * - BitMatrix::readFormatInformation() */ - protected ?Version $version = null; + protected ?MaskPattern $maskPattern = null; /** * the size (side length) of the matrix, including quiet zone (if created) @@ -112,10 +116,9 @@ class QRMatrix{ /** * QRMatrix constructor. */ - public function __construct(Version $version, EccLevel $eccLevel, MaskPattern $maskPattern){ + public function __construct(Version $version, EccLevel $eccLevel){ $this->version = $version; $this->eccLevel = $eccLevel; - $this->maskPattern = $maskPattern; $this->moduleCount = $this->version->getDimension(); $this->matrix = $this->createMatrix($this->moduleCount, $this::M_NULL); } @@ -502,12 +505,16 @@ class QRMatrix{ } /** - * Draws the format info along the finder patterns + * Draws the format info along the finder patterns. If no $maskPattern, all format info modules will be set to false. * * ISO/IEC 18004:2000 Section 8.9 */ - public function setFormatInfo():self{ - $bits = $this->eccLevel->getformatPattern($this->maskPattern); + public function setFormatInfo(MaskPattern $maskPattern = null):self{ + $this->maskPattern = $maskPattern; + + $bits = ($this->maskPattern instanceof MaskPattern) + ? $this->eccLevel->getformatPattern($this->maskPattern) + : 0; // sets all format fields to false (test mode) for($i = 0; $i < 15; $i++){ $v = (($bits >> $i) & 1) === 1; @@ -703,8 +710,9 @@ class QRMatrix{ * * ISO/IEC 18004:2000 Section 8.8.1 */ - public function mask():self{ - $mask = $this->maskPattern->getMask(); + public function mask(MaskPattern $maskPattern):self{ + $this->maskPattern = $maskPattern; + $mask = $this->maskPattern->getMask(); foreach($this->matrix as $y => $row){ foreach($row as $x => $val){ diff --git a/src/Decoder/BitMatrix.php b/src/Decoder/BitMatrix.php index a91877c0d..588564b1f 100644 --- a/src/Decoder/BitMatrix.php +++ b/src/Decoder/BitMatrix.php @@ -117,11 +117,15 @@ final class BitMatrix extends QRMatrix{ $this ->readFormatInformation() ->readVersion() - ->mask() // reverse the mask pattern + ->mask($this->maskPattern) // reverse the mask pattern ; // invoke a fresh matrix with only the function & format patterns to compare against - $fp = (new QRMatrix($this->version, $this->eccLevel, $this->maskPattern))->initFunctionalPatterns(); + $matrix = (new QRMatrix($this->version, $this->eccLevel)) + ->initFunctionalPatterns() + ->setFormatInfo($this->maskPattern) + ; + $result = []; $byte = 0; $bitsRead = 0; @@ -143,7 +147,7 @@ final class BitMatrix extends QRMatrix{ $x = ($i - $col); // Ignore bits covered by the function pattern - if($fp->get($x, $y) !== $this::M_NULL){ + if($matrix->get($x, $y) !== $this::M_NULL){ continue; } diff --git a/src/Decoder/DecoderResult.php b/src/Decoder/DecoderResult.php index 732c427c5..79486f13b 100644 --- a/src/Decoder/DecoderResult.php +++ b/src/Decoder/DecoderResult.php @@ -88,10 +88,11 @@ final class DecoderResult{ * Returns a QRMatrix instance with thesettings and data of the reader result */ public function getQRMatrix():QRMatrix{ - return (new QRMatrix($this->version, $this->eccLevel, $this->maskPattern)) + return (new QRMatrix($this->version, $this->eccLevel)) ->initFunctionalPatterns() ->writeCodewords($this->rawBytes) - ->mask() + ->setFormatInfo($this->maskPattern) + ->mask($this->maskPattern) ; } diff --git a/tests/Data/QRMatrixTest.php b/tests/Data/QRMatrixTest.php index b3b75184a..e8b19656b 100755 --- a/tests/Data/QRMatrixTest.php +++ b/tests/Data/QRMatrixTest.php @@ -32,8 +32,7 @@ final class QRMatrixTest extends TestCase{ protected function setUp():void{ $this->matrix = new QRMatrix( new Version($this::version), - new EccLevel(EccLevel::L), - new MaskPattern(MaskPattern::PATTERN_000) + new EccLevel(EccLevel::L) ); } @@ -175,11 +174,10 @@ final class QRMatrixTest extends TestCase{ * Version data provider for several pattern tests */ public static function matrixProvider():Generator{ - $ecc = new EccLevel(EccLevel::L); - $mask = new MaskPattern(MaskPattern::PATTERN_000); + $ecc = new EccLevel(EccLevel::L); foreach(range(1, 40) as $i){ - yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc, $mask)]; + yield 'version: '.$i => [new QRMatrix(new Version($i), $ecc)]; } } @@ -315,7 +313,7 @@ final class QRMatrixTest extends TestCase{ * @dataProvider matrixProvider */ public function testSetFormatInfo(QRMatrix $matrix):void{ - $matrix->setFormatInfo(); + $matrix->setFormatInfo(new MaskPattern(MaskPattern::PATTERN_000)); $this->dm($matrix); diff --git a/tests/Output/QROutputTestAbstract.php b/tests/Output/QROutputTestAbstract.php index 97487078b..2a2332054 100644 --- a/tests/Output/QROutputTestAbstract.php +++ b/tests/Output/QROutputTestAbstract.php @@ -43,9 +43,7 @@ abstract class QROutputTestAbstract extends TestCase{ $this->options = new QROptions; $this->options->outputType = $this->type; - - $this->matrix = (new QRData($this->options, [new Byte('testdata')])) - ->writeMatrix(new MaskPattern(MaskPattern::PATTERN_010)); + $this->matrix = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix(); $this->outputInterface = new $this->FQN($this->options, $this->matrix); }