🛀 cleanup

This commit is contained in:
codemasher
2021-06-05 01:16:21 +02:00
parent d60a4e0f55
commit f4401a4b2b
10 changed files with 72 additions and 42 deletions
+2 -2
View File
@@ -36,8 +36,8 @@ final class Binarizer{
// This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
// So this is the smallest dimension in each axis we can accept.
private const BLOCK_SIZE_POWER = 3;
private const BLOCK_SIZE = 8; // ...0100...00
private const BLOCK_SIZE_MASK = 7; // ...0011...11
private const BLOCK_SIZE = 8; // ...0100...00
private const BLOCK_SIZE_MASK = 7; // ...0011...11
private const MINIMUM_DIMENSION = 40;
private const MIN_DYNAMIC_RANGE = 24;
+1 -2
View File
@@ -13,7 +13,6 @@ namespace chillerlan\QRCode\Decoder;
use chillerlan\QRCode\Common\{MaskPattern, Version};
use InvalidArgumentException;
use function chillerlan\QRCode\Common\uRShift;
use function array_fill, count;
final class BitMatrix{
@@ -110,7 +109,7 @@ final class BitMatrix{
$this->bits[$offset] ??= 0;
return (uRShift($this->bits[$offset], ($x & 0x1f)) & 1) !== 0;
return (BitMatrixParser::uRShift($this->bits[$offset], ($x & 0x1f)) & 1) !== 0;
}
/**
+28 -5
View File
@@ -13,8 +13,7 @@ namespace chillerlan\QRCode\Decoder;
use RuntimeException;
use chillerlan\QRCode\Common\{Version, FormatInformation};
use function chillerlan\QRCode\Common\numBitsDiffering;
use const PHP_INT_MAX;
use const PHP_INT_MAX, PHP_INT_SIZE;
/**
* @author Sean Owen
@@ -219,7 +218,7 @@ final class BitMatrixParser{
return new FormatInformation($maskedBits);
}
$bitsDifference = numBitsDiffering($maskedFormatInfo1, $dataBits);
$bitsDifference = self::numBitsDiffering($maskedFormatInfo1, $dataBits);
if($bitsDifference < $bestDifference){
$bestFormatInfo = $maskedBits;
@@ -228,7 +227,7 @@ final class BitMatrixParser{
if($maskedFormatInfo1 !== $maskedFormatInfo2){
// also try the other option
$bitsDifference = numBitsDiffering($maskedFormatInfo2, $dataBits);
$bitsDifference = self::numBitsDiffering($maskedFormatInfo2, $dataBits);
if($bitsDifference < $bestDifference){
$bestFormatInfo = $maskedBits;
@@ -319,7 +318,7 @@ final class BitMatrixParser{
// Otherwise see if this is the closest to a real version info bit string
// we have seen so far
/** @phan-suppress-next-line PhanTypeMismatchArgumentNullable ($targetVersionPattern is never null here) */
$bitsDifference = numBitsDiffering($versionBits, $targetVersionPattern);
$bitsDifference = self::numBitsDiffering($versionBits, $targetVersionPattern);
if($bitsDifference < $bestDifference){
$bestVersion = $i;
@@ -336,4 +335,28 @@ final class BitMatrixParser{
return null;
}
public static function uRShift(int $a, int $b):int{
if($b === 0){
return $a;
}
return ($a >> $b) & ~((1 << (8 * PHP_INT_SIZE - 1)) >> ($b - 1));
}
private static function numBitsDiffering(int $a, int $b):int{
// a now has a 1 bit exactly where its bit differs with b's
$a ^= $b;
// Offset i holds the number of 1 bits in the binary representation of i
$BITS_SET_IN_HALF_BYTE = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4];
// Count bits set quickly with a series of lookups:
$count = 0;
for($i = 0; $i < 32; $i += 4){
$count += $BITS_SET_IN_HALF_BYTE[self::uRShift($a, $i) & 0x0F];
}
return $count;
}
}
+6 -2
View File
@@ -12,7 +12,7 @@
namespace chillerlan\QRCode\Decoder;
use InvalidArgumentException;
use function chillerlan\QRCode\Common\arraycopy;
use function array_slice, array_splice;
/**
* The purpose of this class hierarchy is to abstract different bitmap implementations across
@@ -82,7 +82,11 @@ abstract class LuminanceSource{
throw new InvalidArgumentException('Requested row is outside the image: '.$y);
}
return arraycopy($this->luminances, $y * $this->width, [], 0, $this->width);
$arr = [];
array_splice($arr, 0, $this->width, array_slice($this->luminances, $y * $this->width, $this->width));
return $arr;
}
/**