123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- <?php
- declare(strict_types = 1);
- namespace BaconQrCode\Encoder;
- use BaconQrCode\Common\BitArray;
- use BaconQrCode\Common\ErrorCorrectionLevel;
- use BaconQrCode\Common\Version;
- use BaconQrCode\Exception\RuntimeException;
- use BaconQrCode\Exception\WriterException;
- final class MatrixUtil
- {
-
- private const POSITION_DETECTION_PATTERN = [
- [1, 1, 1, 1, 1, 1, 1],
- [1, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 1, 1, 0, 1],
- [1, 0, 1, 1, 1, 0, 1],
- [1, 0, 1, 1, 1, 0, 1],
- [1, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1],
- ];
-
- private const POSITION_ADJUSTMENT_PATTERN = [
- [1, 1, 1, 1, 1],
- [1, 0, 0, 0, 1],
- [1, 0, 1, 0, 1],
- [1, 0, 0, 0, 1],
- [1, 1, 1, 1, 1],
- ];
-
- private const POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE = [
- [null, null, null, null, null, null, null],
- [ 6, 18, null, null, null, null, null],
- [ 6, 22, null, null, null, null, null],
- [ 6, 26, null, null, null, null, null],
- [ 6, 30, null, null, null, null, null],
- [ 6, 34, null, null, null, null, null],
- [ 6, 22, 38, null, null, null, null],
- [ 6, 24, 42, null, null, null, null],
- [ 6, 26, 46, null, null, null, null],
- [ 6, 28, 50, null, null, null, null],
- [ 6, 30, 54, null, null, null, null],
- [ 6, 32, 58, null, null, null, null],
- [ 6, 34, 62, null, null, null, null],
- [ 6, 26, 46, 66, null, null, null],
- [ 6, 26, 48, 70, null, null, null],
- [ 6, 26, 50, 74, null, null, null],
- [ 6, 30, 54, 78, null, null, null],
- [ 6, 30, 56, 82, null, null, null],
- [ 6, 30, 58, 86, null, null, null],
- [ 6, 34, 62, 90, null, null, null],
- [ 6, 28, 50, 72, 94, null, null],
- [ 6, 26, 50, 74, 98, null, null],
- [ 6, 30, 54, 78, 102, null, null],
- [ 6, 28, 54, 80, 106, null, null],
- [ 6, 32, 58, 84, 110, null, null],
- [ 6, 30, 58, 86, 114, null, null],
- [ 6, 34, 62, 90, 118, null, null],
- [ 6, 26, 50, 74, 98, 122, null],
- [ 6, 30, 54, 78, 102, 126, null],
- [ 6, 26, 52, 78, 104, 130, null],
- [ 6, 30, 56, 82, 108, 134, null],
- [ 6, 34, 60, 86, 112, 138, null],
- [ 6, 30, 58, 86, 114, 142, null],
- [ 6, 34, 62, 90, 118, 146, null],
- [ 6, 30, 54, 78, 102, 126, 150],
- [ 6, 24, 50, 76, 102, 128, 154],
- [ 6, 28, 54, 80, 106, 132, 158],
- [ 6, 32, 58, 84, 110, 136, 162],
- [ 6, 26, 54, 82, 110, 138, 166],
- [ 6, 30, 58, 86, 114, 142, 170],
- ];
-
- private const TYPE_INFO_COORDINATES = [
- [8, 0],
- [8, 1],
- [8, 2],
- [8, 3],
- [8, 4],
- [8, 5],
- [8, 7],
- [8, 8],
- [7, 8],
- [5, 8],
- [4, 8],
- [3, 8],
- [2, 8],
- [1, 8],
- [0, 8],
- ];
-
- private const VERSION_INFO_POLY = 0x1f25;
-
- private const TYPE_INFO_POLY = 0x537;
-
- private const TYPE_INFO_MASK_PATTERN = 0x5412;
-
- public static function clearMatrix(ByteMatrix $matrix) : void
- {
- $matrix->clear(-1);
- }
-
- public static function buildMatrix(
- BitArray $dataBits,
- ErrorCorrectionLevel $level,
- Version $version,
- int $maskPattern,
- ByteMatrix $matrix
- ) : void {
- self::clearMatrix($matrix);
- self::embedBasicPatterns($version, $matrix);
- self::embedTypeInfo($level, $maskPattern, $matrix);
- self::maybeEmbedVersionInfo($version, $matrix);
- self::embedDataBits($dataBits, $maskPattern, $matrix);
- }
-
- public static function removePositionDetectionPatterns(ByteMatrix $matrix) : void
- {
- $pdpWidth = count(self::POSITION_DETECTION_PATTERN[0]);
- self::removePositionDetectionPattern(0, 0, $matrix);
- self::removePositionDetectionPattern($matrix->getWidth() - $pdpWidth, 0, $matrix);
- self::removePositionDetectionPattern(0, $matrix->getWidth() - $pdpWidth, $matrix);
- }
-
- private static function embedTypeInfo(ErrorCorrectionLevel $level, int $maskPattern, ByteMatrix $matrix) : void
- {
- $typeInfoBits = new BitArray();
- self::makeTypeInfoBits($level, $maskPattern, $typeInfoBits);
- $typeInfoBitsSize = $typeInfoBits->getSize();
- for ($i = 0; $i < $typeInfoBitsSize; ++$i) {
- $bit = $typeInfoBits->get($typeInfoBitsSize - 1 - $i);
- $x1 = self::TYPE_INFO_COORDINATES[$i][0];
- $y1 = self::TYPE_INFO_COORDINATES[$i][1];
- $matrix->set($x1, $y1, (int) $bit);
- if ($i < 8) {
- $x2 = $matrix->getWidth() - $i - 1;
- $y2 = 8;
- } else {
- $x2 = 8;
- $y2 = $matrix->getHeight() - 7 + ($i - 8);
- }
- $matrix->set($x2, $y2, (int) $bit);
- }
- }
-
- private static function makeTypeInfoBits(ErrorCorrectionLevel $level, int $maskPattern, BitArray $bits) : void
- {
- $typeInfo = ($level->getBits() << 3) | $maskPattern;
- $bits->appendBits($typeInfo, 5);
- $bchCode = self::calculateBchCode($typeInfo, self::TYPE_INFO_POLY);
- $bits->appendBits($bchCode, 10);
- $maskBits = new BitArray();
- $maskBits->appendBits(self::TYPE_INFO_MASK_PATTERN, 15);
- $bits->xorBits($maskBits);
- if (15 !== $bits->getSize()) {
- throw new RuntimeException('Bit array resulted in invalid size: ' . $bits->getSize());
- }
- }
-
- private static function maybeEmbedVersionInfo(Version $version, ByteMatrix $matrix) : void
- {
- if ($version->getVersionNumber() < 7) {
- return;
- }
- $versionInfoBits = new BitArray();
- self::makeVersionInfoBits($version, $versionInfoBits);
- $bitIndex = 6 * 3 - 1;
- for ($i = 0; $i < 6; ++$i) {
- for ($j = 0; $j < 3; ++$j) {
- $bit = $versionInfoBits->get($bitIndex);
- --$bitIndex;
- $matrix->set($i, $matrix->getHeight() - 11 + $j, (int) $bit);
- $matrix->set($matrix->getHeight() - 11 + $j, $i, (int) $bit);
- }
- }
- }
-
- private static function makeVersionInfoBits(Version $version, BitArray $bits) : void
- {
- $bits->appendBits($version->getVersionNumber(), 6);
- $bchCode = self::calculateBchCode($version->getVersionNumber(), self::VERSION_INFO_POLY);
- $bits->appendBits($bchCode, 12);
- if (18 !== $bits->getSize()) {
- throw new RuntimeException('Bit array resulted in invalid size: ' . $bits->getSize());
- }
- }
-
- private static function calculateBchCode(int $value, int $poly) : int
- {
- $msbSetInPoly = self::findMsbSet($poly);
- $value <<= $msbSetInPoly - 1;
- while (self::findMsbSet($value) >= $msbSetInPoly) {
- $value ^= $poly << (self::findMsbSet($value) - $msbSetInPoly);
- }
- return $value;
- }
-
- private static function findMsbSet(int $value) : int
- {
- $numDigits = 0;
- while (0 !== $value) {
- $value >>= 1;
- ++$numDigits;
- }
- return $numDigits;
- }
-
- private static function embedBasicPatterns(Version $version, ByteMatrix $matrix) : void
- {
- self::embedPositionDetectionPatternsAndSeparators($matrix);
- self::embedDarkDotAtLeftBottomCorner($matrix);
- self::maybeEmbedPositionAdjustmentPatterns($version, $matrix);
- self::embedTimingPatterns($matrix);
- }
-
- private static function embedPositionDetectionPatternsAndSeparators(ByteMatrix $matrix) : void
- {
- $pdpWidth = count(self::POSITION_DETECTION_PATTERN[0]);
- self::embedPositionDetectionPattern(0, 0, $matrix);
- self::embedPositionDetectionPattern($matrix->getWidth() - $pdpWidth, 0, $matrix);
- self::embedPositionDetectionPattern(0, $matrix->getWidth() - $pdpWidth, $matrix);
- $hspWidth = 8;
- self::embedHorizontalSeparationPattern(0, $hspWidth - 1, $matrix);
- self::embedHorizontalSeparationPattern($matrix->getWidth() - $hspWidth, $hspWidth - 1, $matrix);
- self::embedHorizontalSeparationPattern(0, $matrix->getWidth() - $hspWidth, $matrix);
- $vspSize = 7;
- self::embedVerticalSeparationPattern($vspSize, 0, $matrix);
- self::embedVerticalSeparationPattern($matrix->getHeight() - $vspSize - 1, 0, $matrix);
- self::embedVerticalSeparationPattern($vspSize, $matrix->getHeight() - $vspSize, $matrix);
- }
-
- private static function embedPositionDetectionPattern(int $xStart, int $yStart, ByteMatrix $matrix) : void
- {
- for ($y = 0; $y < 7; ++$y) {
- for ($x = 0; $x < 7; ++$x) {
- $matrix->set($xStart + $x, $yStart + $y, self::POSITION_DETECTION_PATTERN[$y][$x]);
- }
- }
- }
- private static function removePositionDetectionPattern(int $xStart, int $yStart, ByteMatrix $matrix) : void
- {
- for ($y = 0; $y < 7; ++$y) {
- for ($x = 0; $x < 7; ++$x) {
- $matrix->set($xStart + $x, $yStart + $y, 0);
- }
- }
- }
-
- private static function embedHorizontalSeparationPattern(int $xStart, int $yStart, ByteMatrix $matrix) : void
- {
- for ($x = 0; $x < 8; $x++) {
- if (-1 !== $matrix->get($xStart + $x, $yStart)) {
- throw new RuntimeException('Byte already set');
- }
- $matrix->set($xStart + $x, $yStart, 0);
- }
- }
-
- private static function embedVerticalSeparationPattern(int $xStart, int $yStart, ByteMatrix $matrix) : void
- {
- for ($y = 0; $y < 7; $y++) {
- if (-1 !== $matrix->get($xStart, $yStart + $y)) {
- throw new RuntimeException('Byte already set');
- }
- $matrix->set($xStart, $yStart + $y, 0);
- }
- }
-
- private static function embedDarkDotAtLeftBottomCorner(ByteMatrix $matrix) : void
- {
- if (0 === $matrix->get(8, $matrix->getHeight() - 8)) {
- throw new RuntimeException('Byte already set to 0');
- }
- $matrix->set(8, $matrix->getHeight() - 8, 1);
- }
-
- private static function maybeEmbedPositionAdjustmentPatterns(Version $version, ByteMatrix $matrix) : void
- {
- if ($version->getVersionNumber() < 2) {
- return;
- }
- $index = $version->getVersionNumber() - 1;
- $coordinates = self::POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[$index];
- $numCoordinates = count($coordinates);
- for ($i = 0; $i < $numCoordinates; ++$i) {
- for ($j = 0; $j < $numCoordinates; ++$j) {
- $y = $coordinates[$i];
- $x = $coordinates[$j];
- if (null === $x || null === $y) {
- continue;
- }
- if (-1 === $matrix->get($x, $y)) {
- self::embedPositionAdjustmentPattern($x - 2, $y - 2, $matrix);
- }
- }
- }
- }
-
- private static function embedPositionAdjustmentPattern(int $xStart, int $yStart, ByteMatrix $matrix) : void
- {
- for ($y = 0; $y < 5; $y++) {
- for ($x = 0; $x < 5; $x++) {
- $matrix->set($xStart + $x, $yStart + $y, self::POSITION_ADJUSTMENT_PATTERN[$y][$x]);
- }
- }
- }
-
- private static function embedTimingPatterns(ByteMatrix $matrix) : void
- {
- $matrixWidth = $matrix->getWidth();
- for ($i = 8; $i < $matrixWidth - 8; ++$i) {
- $bit = ($i + 1) % 2;
- if (-1 === $matrix->get($i, 6)) {
- $matrix->set($i, 6, $bit);
- }
- if (-1 === $matrix->get(6, $i)) {
- $matrix->set(6, $i, $bit);
- }
- }
- }
-
- private static function embedDataBits(BitArray $dataBits, int $maskPattern, ByteMatrix $matrix) : void
- {
- $bitIndex = 0;
- $direction = -1;
-
- $x = $matrix->getWidth() - 1;
- $y = $matrix->getHeight() - 1;
- while ($x > 0) {
-
- if (6 === $x) {
- --$x;
- }
- while ($y >= 0 && $y < $matrix->getHeight()) {
- for ($i = 0; $i < 2; $i++) {
- $xx = $x - $i;
-
- if (-1 !== $matrix->get($xx, $y)) {
- continue;
- }
- if ($bitIndex < $dataBits->getSize()) {
- $bit = $dataBits->get($bitIndex);
- ++$bitIndex;
- } else {
-
-
-
- $bit = false;
- }
-
- if (-1 !== $maskPattern && MaskUtil::getDataMaskBit($maskPattern, $xx, $y)) {
- $bit = ! $bit;
- }
- $matrix->set($xx, $y, (int) $bit);
- }
- $y += $direction;
- }
- $direction = -$direction;
- $y += $direction;
- $x -= 2;
- }
-
- if ($dataBits->getSize() !== $bitIndex) {
- throw new WriterException('Not all bits consumed (' . $bitIndex . ' out of ' . $dataBits->getSize() .')');
- }
- }
- }
|