:octocat: spimplifications & cleanup

This commit is contained in:
smiley
2024-01-06 17:32:32 +01:00
parent 1a0208a3e8
commit e9743c00f0
4 changed files with 51 additions and 59 deletions
+2 -7
View File
@@ -94,11 +94,6 @@ final class MaskPattern{
/**
* Returns a closure that applies the mask for the chosen mask pattern.
*
* Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations
* of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix,
* including areas used for finder patterns, timing patterns, etc. These areas should be unused
* after the point they are unmasked anyway.
*
* Note that the diagram in section 6.8.1 is misleading since it indicates that $i is column position
* and $j is row position. In fact, as the text says, $i is row position and $j is column position.
*
@@ -304,7 +299,7 @@ final class MaskPattern{
}
for($y = $from; $y < $to; $y++){
if($matrix[$y][$x]){
if($matrix[$y][$x] === true){
return false;
}
}
@@ -322,7 +317,7 @@ final class MaskPattern{
foreach($matrix as $row){
foreach($row as $val){
if($val){
if($val === true){
$darkCells++;
}
}
+21 -17
View File
@@ -241,7 +241,14 @@ class QRMatrix{
public function set(int $x, int $y, bool $value, int $M_TYPE):static{
if(isset($this->matrix[$y][$x])){
$this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0));
// we don't know whether the input is dark, so we remove the dark bit
$M_TYPE &= ~$this::IS_DARK;
if($value === true){
$M_TYPE |= $this::IS_DARK;
}
$this->matrix[$y][$x] = $M_TYPE;
}
return $this;
@@ -277,15 +284,16 @@ class QRMatrix{
* Checks whether the module at ($x, $y) is of the given $M_TYPE
*
* true => $value & $M_TYPE === $M_TYPE
*
* Also, returns false if the given coordinates are out of range.
*/
public function checkType(int $x, int $y, int $M_TYPE):bool{
$val = $this->get($x, $y);
if($val === -1){
return false;
if(isset($this->matrix[$y][$x])){
return ($this->matrix[$y][$x] & $M_TYPE) === $M_TYPE;
}
return ($val & $M_TYPE) === $M_TYPE;
return false;
}
/**
@@ -305,14 +313,16 @@ class QRMatrix{
/**
* Checks whether the module at ($x, $y) is true (dark) or false (light)
*
* Also, returns false if the given coordinates are out of range.
*/
public function check(int $x, int $y):bool{
if(!isset($this->matrix[$y][$x])){
return false;
if(isset($this->matrix[$y][$x])){
return $this->isDark($this->matrix[$y][$x]);
}
return $this->isDark($this->matrix[$y][$x]);
return false;
}
/**
@@ -714,14 +724,12 @@ class QRMatrix{
continue;
}
$value = 0;
$this->matrix[$y][$x] = $this::M_DATA;
if($iByte < $byteCount && (($data[$iByte] >> $iBit--) & 1) === 1){
$value = $this::IS_DARK;
$this->matrix[$y][$x] |= $this::IS_DARK;
}
$this->matrix[$y][$x] = ($this::M_DATA | $value);
if($iBit === -1){
$iByte++;
$iBit = 7;
@@ -747,11 +755,7 @@ class QRMatrix{
foreach($this->matrix as $y => $row){
foreach($row as $x => $val){
// skip non-data modules
if(($val & $this::M_DATA) !== $this::M_DATA){
continue;
}
if($mask($x, $y)){
if(($val & $this::M_DATA) === $this::M_DATA && $mask($x, $y)){
$this->flip($x, $y);
}
}
+19 -19
View File
@@ -129,36 +129,36 @@ final class MaskPatternTest extends TestCase{
public function testPenaltyRule1():void{
// horizontal
$this::assertSame(0, MaskPattern::testRule1([[0, 0, 0, 0]], 1, 4));
$this::assertSame(3, MaskPattern::testRule1([[0, 0, 0, 0, 0, 1]], 1, 6));
$this::assertSame(4, MaskPattern::testRule1([[0, 0, 0, 0, 0, 0]], 1, 6));
$this::assertSame(0, MaskPattern::testRule1([[false, false, false, false]], 1, 4));
$this::assertSame(3, MaskPattern::testRule1([[false, false, false, false, false, true]], 1, 6));
$this::assertSame(4, MaskPattern::testRule1([[false, false, false, false, false, false]], 1, 6));
// vertical
$this::assertSame(0, MaskPattern::testRule1([[0], [0], [0], [0]], 4, 1));
$this::assertSame(3, MaskPattern::testRule1([[0], [0], [0], [0], [0], [1]], 6, 1));
$this::assertSame(4, MaskPattern::testRule1([[0], [0], [0], [0], [0], [0]], 6, 1));
$this::assertSame(0, MaskPattern::testRule1([[false], [false], [false], [false]], 4, 1));
$this::assertSame(3, MaskPattern::testRule1([[false], [false], [false], [false], [false], [true]], 6, 1));
$this::assertSame(4, MaskPattern::testRule1([[false], [false], [false], [false], [false], [false]], 6, 1));
}
public function testPenaltyRule2():void{
$this::assertSame(0, MaskPattern::testRule2([[0]], 1, 1));
$this::assertSame(0, MaskPattern::testRule2([[0, 0], [0, 1]], 2, 2));
$this::assertSame(3, MaskPattern::testRule2([[0, 0], [0, 0]], 2, 2));
$this::assertSame(12, MaskPattern::testRule2([[0, 0, 0], [0, 0, 0], [0, 0, 0]], 3, 3));
$this::assertSame(0, MaskPattern::testRule2([[false]], 1, 1));
$this::assertSame(0, MaskPattern::testRule2([[false, false], [false, true]], 2, 2));
$this::assertSame(3, MaskPattern::testRule2([[false, false], [false, false]], 2, 2));
$this::assertSame(12, MaskPattern::testRule2([[false, false, false], [false, false, false], [false, false, false]], 3, 3));
}
public function testPenaltyRule3():void{
// horizontal
$this::assertSame(40, MaskPattern::testRule3([[0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1]], 1, 11));
$this::assertSame(40, MaskPattern::testRule3([[1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0]], 1, 11));
$this::assertSame(0, MaskPattern::testRule3([[1, 0, 1, 1, 1, 0, 1]], 1, 7));
$this::assertSame(40, MaskPattern::testRule3([[false, false, false, false, true, false, true, true, true, false, true]], 1, 11));
$this::assertSame(40, MaskPattern::testRule3([[true, false, true, true, true, false, true, false, false, false, false]], 1, 11));
$this::assertSame(0, MaskPattern::testRule3([[true, false, true, true, true, false, true]], 1, 7));
// vertical
$this::assertSame(40, MaskPattern::testRule3([[0], [0], [0], [0], [1], [0], [1], [1], [1], [0], [1]], 11, 1));
$this::assertSame(40, MaskPattern::testRule3([[1], [0], [1], [1], [1], [0], [1], [0], [0], [0], [0]], 11, 1));
$this::assertSame(0, MaskPattern::testRule3([[1], [0], [1], [1], [1], [0], [1]], 7, 1));
$this::assertSame(40, MaskPattern::testRule3([[false], [false], [false], [false], [true], [false], [true], [true], [true], [false], [true]], 11, 1));
$this::assertSame(40, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true], [false], [false], [false], [false]], 11, 1));
$this::assertSame(0, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true]], 7, 1));
}
public function testPenaltyRule4():void{
$this::assertSame(100, MaskPattern::testRule4([[0]], 1, 1));
$this::assertSame(0, MaskPattern::testRule4([[0, 1]], 1, 2));
$this::assertSame(30, MaskPattern::testRule4([[0, 1, 1, 1, 1, 0]], 1, 6));
$this::assertSame(100, MaskPattern::testRule4([[false]], 1, 1));
$this::assertSame(0, MaskPattern::testRule4([[false, true]], 1, 2));
$this::assertSame(30, MaskPattern::testRule4([[false, true, true, true, true, false]], 1, 6));
}
}
+9 -16
View File
@@ -37,38 +37,31 @@ final class QRMatrixTest extends TestCase{
);
}
/**
* Validates the QRMatrix instance
*/
public function testInstance():void{
$this::assertInstanceOf(QRMatrix::class, $this->matrix);
}
/**
* Tests if size() returns the actual matrix size/count
*/
public function testSize():void{
public function testGetSize():void{
$this::assertCount($this->matrix->getSize(), $this->matrix->getMatrix(true));
}
/**
* Tests if version() returns the current (given) version
*/
public function testVersion():void{
public function testGetVersion():void{
$this::assertSame($this::version, $this->matrix->getVersion()->getVersionNumber());
}
/**
* Tests if eccLevel() returns the current (given) ECC level
*/
public function testECC():void{
public function testGetECC():void{
$this::assertSame(EccLevel::L, $this->matrix->getEccLevel()->getLevel());
}
/**
* Tests if maskPattern() returns the current (or default) mask pattern
*/
public function testMaskPattern():void{
public function testGetMaskPattern():void{
// set via matrix evaluation
$matrix = (new QRCode)->addByteSegment('testdata')->getQRMatrix();
@@ -326,7 +319,7 @@ final class QRMatrixTest extends TestCase{
/**
* Tests if the logo space is drawn square if one of the dimensions is omitted
*/
public function testSetLogoSpaceOmitHeight():void{
public function testSetLogoSpaceOmitDimension():void{
$o = new QROptions;
$o->version = 2;
$o->eccLevel = EccLevel::H;
@@ -336,7 +329,7 @@ final class QRMatrixTest extends TestCase{
$matrix = (new QRCode($o))->addByteSegment('testdata')->getQRMatrix();
$this::debugMatrix($matrix);
$this->debugMatrix($matrix);
$this::assertFalse($matrix->checkType(9, 9, QRMatrix::M_LOGO));
$this::assertTrue($matrix->checkType(10, 10, QRMatrix::M_LOGO));
@@ -358,7 +351,7 @@ final class QRMatrixTest extends TestCase{
// also testing size adjustment to uneven numbers
$matrix->setLogoSpace(20, 14);
$this::debugMatrix($matrix);
$this->debugMatrix($matrix);
// NW corner
$this::assertFalse($matrix->checkType(17, 20, QRMatrix::M_LOGO));
@@ -382,7 +375,7 @@ final class QRMatrixTest extends TestCase{
$matrix = (new QRCode($o))->addByteSegment('testdata')->getQRMatrix();
$matrix->setLogoSpace(21, 21, -10, -10);
$this::debugMatrix($matrix);
$this->debugMatrix($matrix);
$this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(9, 9));
$this::assertSame(QRMatrix::M_LOGO, $matrix->get(10, 10));
$this::assertSame(QRMatrix::M_LOGO, $matrix->get(20, 20));
@@ -391,7 +384,7 @@ final class QRMatrixTest extends TestCase{
// I just realized that setLogoSpace() could be called multiple times
// on the same instance, and I'm not going to do anything about it :P
$matrix->setLogoSpace(21, 21, 45, 45);
$this::debugMatrix($matrix);
$this->debugMatrix($matrix);
$this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(54, 54));
$this::assertSame(QRMatrix::M_LOGO, $matrix->get(55, 55));
$this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(67, 67));