| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class ReedSolomonEncoder |
| 4 |
* |
| 5 |
* @created 07.01.2021 |
| 6 |
* @author smiley <[email protected]> |
| 7 |
* @copyright 2021 smiley |
| 8 |
* @license MIT |
| 9 |
*/ |
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
namespace chillerlan\QRCode\Data; |
| 13 |
|
| 14 |
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, GenericGFPoly, GF256, Version}; |
| 15 |
use function array_fill, array_merge, count, max; |
| 16 |
|
| 17 |
/** |
| 18 |
* Reed-Solomon encoding - ISO/IEC 18004:2000 Section 8.5 ff |
| 19 |
* |
| 20 |
* @see http://www.thonky.com/qr-code-tutorial/error-correction-coding |
| 21 |
*/ |
| 22 |
final class ReedSolomonEncoder{ |
| 23 |
|
| 24 |
private Version $version; |
| 25 |
private EccLevel $eccLevel; |
| 26 |
/** @var int[] */ |
| 27 |
private array $interleavedData; |
| 28 |
private int $interleavedDataIndex; |
| 29 |
|
| 30 |
/** |
| 31 |
* ReedSolomonDecoder constructor |
| 32 |
*/ |
| 33 |
public function __construct(Version $version, EccLevel $eccLevel){ |
| 34 |
$this->version = $version; |
| 35 |
$this->eccLevel = $eccLevel; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* ECC encoding and interleaving |
| 40 |
* |
| 41 |
* @return int[] |
| 42 |
* @throws \chillerlan\QRCode\QRCodeException |
| 43 |
*/ |
| 44 |
public function interleaveEcBytes(BitBuffer $bitBuffer):array{ |
| 45 |
[$numEccCodewords, [[$l1, $b1], [$l2, $b2]]] = $this->version->getRSBlocks($this->eccLevel); |
| 46 |
|
| 47 |
$rsBlocks = array_fill(0, $l1, [($numEccCodewords + $b1), $b1]); |
| 48 |
|
| 49 |
if($l2 > 0){ |
| 50 |
$rsBlocks = array_merge($rsBlocks, array_fill(0, $l2, [($numEccCodewords + $b2), $b2])); |
| 51 |
} |
| 52 |
|
| 53 |
$bitBufferData = $bitBuffer->getBuffer(); |
| 54 |
$dataBytes = []; |
| 55 |
$ecBytes = []; |
| 56 |
$maxDataBytes = 0; |
| 57 |
$maxEcBytes = 0; |
| 58 |
$dataByteOffset = 0; |
| 59 |
|
| 60 |
foreach($rsBlocks as $key => [$rsBlockTotal, $dataByteCount]){ |
| 61 |
$dataBytes[$key] = []; |
| 62 |
|
| 63 |
for($i = 0; $i < $dataByteCount; $i++){ |
| 64 |
$dataBytes[$key][$i] = ($bitBufferData[($i + $dataByteOffset)] & 0xff); |
| 65 |
} |
| 66 |
|
| 67 |
$ecByteCount = ($rsBlockTotal - $dataByteCount); |
| 68 |
$ecBytes[$key] = $this->encode($dataBytes[$key], $ecByteCount); |
| 69 |
$maxDataBytes = max($maxDataBytes, $dataByteCount); |
| 70 |
$maxEcBytes = max($maxEcBytes, $ecByteCount); |
| 71 |
$dataByteOffset += $dataByteCount; |
| 72 |
} |
| 73 |
|
| 74 |
$this->interleavedData = array_fill(0, $this->version->getTotalCodewords(), 0); |
| 75 |
$this->interleavedDataIndex = 0; |
| 76 |
$numRsBlocks = ($l1 + $l2); |
| 77 |
|
| 78 |
$this->interleave($dataBytes, $maxDataBytes, $numRsBlocks); |
| 79 |
$this->interleave($ecBytes, $maxEcBytes, $numRsBlocks); |
| 80 |
|
| 81 |
return $this->interleavedData; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param int[] $dataBytes |
| 86 |
* @return int[] |
| 87 |
*/ |
| 88 |
private function encode(array $dataBytes, int $ecByteCount):array{ |
| 89 |
$rsPoly = new GenericGFPoly([1]); |
| 90 |
|
| 91 |
for($i = 0; $i < $ecByteCount; $i++){ |
| 92 |
$rsPoly = $rsPoly->multiply(new GenericGFPoly([1, GF256::exp($i)])); |
| 93 |
} |
| 94 |
|
| 95 |
$rsPolyDegree = $rsPoly->getDegree(); |
| 96 |
|
| 97 |
$modCoefficients = (new GenericGFPoly($dataBytes, $rsPolyDegree)) |
| 98 |
->mod($rsPoly) |
| 99 |
->getCoefficients() |
| 100 |
; |
| 101 |
|
| 102 |
$ecBytes = array_fill(0, $rsPolyDegree, 0); |
| 103 |
$count = (count($modCoefficients) - $rsPolyDegree); |
| 104 |
|
| 105 |
foreach($ecBytes as $i => &$val){ |
| 106 |
$modIndex = ($i + $count); |
| 107 |
$val = 0; |
| 108 |
|
| 109 |
if($modIndex >= 0){ |
| 110 |
$val = $modCoefficients[$modIndex]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $ecBytes; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @param int[][] $byteArray |
| 119 |
*/ |
| 120 |
private function interleave(array $byteArray, int $maxBytes, int $numRsBlocks):void{ |
| 121 |
for($x = 0; $x < $maxBytes; $x++){ |
| 122 |
for($y = 0; $y < $numRsBlocks; $y++){ |
| 123 |
if($x < count($byteArray[$y])){ |
| 124 |
$this->interleavedData[$this->interleavedDataIndex++] = $byteArray[$y][$x]; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
} |