|
| 1 | +/* |
| 2 | + * QR code generator library (.NET) |
| 3 | + * |
| 4 | + * Copyright (c) Manuel Bleichenbacher (MIT License) |
| 5 | + * https://github.com/manuelbl/QrCodeGenerator |
| 6 | + */ |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | + |
| 11 | +namespace Net.Codecrete.QrCodeGenerator |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Turns data segments into the codeword stream the matrix is filled with: |
| 15 | + /// first the data codewords (terminator + padding), then the Reed-Solomon error |
| 16 | + /// correction codewords, interleaved per specification. |
| 17 | + /// </summary> |
| 18 | + internal static class Codewords |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Builds the data codewords for the given data segments. |
| 22 | + /// <para> |
| 23 | + /// The result includes the terminator and the padding for the |
| 24 | + /// given QR code version and error correction level. |
| 25 | + /// </para> |
| 26 | + /// </summary> |
| 27 | + /// <param name="dataSegments">The data segments to encode.</param> |
| 28 | + /// <param name="version">The QR code version.</param> |
| 29 | + /// <param name="ecc">The error correction level.</param> |
| 30 | + /// <returns>The data codewords.</returns> |
| 31 | + internal static byte[] BuildData(List<DataSegment> dataSegments, int version, int ecc) |
| 32 | + { |
| 33 | + var capacity = QrCodeParameters.GetCodewordDataCapacity(version, ecc); |
| 34 | + var bitStream = DataSegment.CreateBitStream(dataSegments, version, capacity); |
| 35 | + var bitstreamLength = bitStream.Length; |
| 36 | + |
| 37 | + // TODO: avoid allocation of another array |
| 38 | + var result = new byte[capacity]; |
| 39 | + bitStream.CopyCodewords(result, 0); |
| 40 | + |
| 41 | + // add padding |
| 42 | + for (var index = (bitstreamLength + 7) / 8; index < capacity; index += 2) |
| 43 | + { |
| 44 | + result[index] = 0b1110_1100; |
| 45 | + } |
| 46 | + for (var index = (bitstreamLength + 15) / 8; index < capacity; index += 2) |
| 47 | + { |
| 48 | + result[index] = 0b0001_0001; |
| 49 | + } |
| 50 | + |
| 51 | + return result; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Adds the error correction to the given codewords and returns the combined result. |
| 56 | + /// <para> |
| 57 | + /// The result is transposed and interleaved as per specification. |
| 58 | + /// </para> |
| 59 | + /// </summary> |
| 60 | + /// <param name="codewords">The data codewords.</param> |
| 61 | + /// <param name="version">The QR code version.</param> |
| 62 | + /// <param name="ecc">The error correction level.</param> |
| 63 | + /// <returns>The combined result of data and error correction codewords.</returns> |
| 64 | + internal static byte[] AddErrorCorrection(byte[] codewords, int version, int ecc) |
| 65 | + { |
| 66 | + var numDataCodewords = codewords.Length; |
| 67 | + var numBlocks = QrCodeParameters.GetNumBlocks(version, ecc); |
| 68 | + var smallBlockDataLength = numDataCodewords / numBlocks; |
| 69 | + var eccBlockLength = (QrCodeParameters.GetCodewordCapacity(version) - numDataCodewords) / numBlocks; |
| 70 | + var numLargeBlocks = numDataCodewords % numBlocks; |
| 71 | + var numSmallBlocks = numBlocks - numLargeBlocks; |
| 72 | + |
| 73 | + var result = new byte[QrCodeParameters.GetCodewordCapacity(version)]; |
| 74 | + var dataOffset = 0; |
| 75 | + var reedSolomon = ReedSolomon.GeneratorForCapacity(eccBlockLength); |
| 76 | + |
| 77 | + // split into blocks and process each block separately |
| 78 | + for (var block = 0; block < numBlocks; block += 1) |
| 79 | + { |
| 80 | + var dataLength = block < numSmallBlocks ? smallBlockDataLength : smallBlockDataLength + 1; |
| 81 | + |
| 82 | + // compute the error correction codewords |
| 83 | + var eccCodewords = reedSolomon.ComputeErrorCorrection(new ArraySegment<byte>(codewords, dataOffset, dataLength)); |
| 84 | + |
| 85 | + // copy the data and error correction codewords to the transposed result |
| 86 | + for (var i = 0; i < smallBlockDataLength; i += 1) |
| 87 | + result[i * numBlocks + block] = codewords[dataOffset + i]; |
| 88 | + if (block >= numSmallBlocks) |
| 89 | + result[numBlocks * smallBlockDataLength + block - numSmallBlocks] = codewords[dataOffset + dataLength - 1]; |
| 90 | + for (var i = 0; i < eccBlockLength; i += 1) |
| 91 | + result[numDataCodewords + i * numBlocks + block] = eccCodewords[i]; |
| 92 | + |
| 93 | + dataOffset += dataLength; |
| 94 | + } |
| 95 | + |
| 96 | + return result; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments