public OverrideByteSymbolCodeValuesBitIterator(CodeSymbolCode <T> setOriginalCode, int[] setNewValues)
 {
     this.bitPosition  = 0;
     this.bitCount     = setOriginalCode.BitCount;
     this.originalCode = setOriginalCode;
     this.newValues    = setNewValues;
 }
Beispiel #2
0
        private void ParseEncodingMessage(IBitIterator it, MessageMode mode)
        {
            if (!it.EndReached)
            {
                this.charCountIndicatorSymbolCode = CodeSymbolCode <CharCountIndicatorSymbol> .BuildInstance(it, new CharCountIndicatorSymbolFactory(mode));

                this.characterCount = charCountIndicatorSymbolCode.GetSymbolAt(0).GetCharacterCount();

                if (!it.EndReached)
                {
                    switch (mode.Mode)
                    {
                    case MessageMode.EncodingMode.Byte:
                        this.Message = CodeSymbolCode <ByteEncodingSymbol> .BuildInstance(it, new ByteEncodingSymbolFactory(characterCount));

                        break;

                    case MessageMode.EncodingMode.Alphanumeric:
                        this.Message = CodeSymbolCode <AlphaNumericEncodingSymbol> .BuildInstance(it, new AlphaNumericEncodingSymbolFactory(characterCount));

                        break;

                    case MessageMode.EncodingMode.Numeric:
                        this.Message = CodeSymbolCode <NumericEncodingSymbol> .BuildInstance(it, new NumericEncodingSymbolFactory(characterCount));

                        break;

                    default:
                        throw new NotImplementedException($"{mode.Mode} decoding not implemented");
                    }
                }
            }
        }
Beispiel #3
0
        private static int[] ConvertToIntArray(CodeSymbolCode <RawCodeByte> code)
        {
            var symbolsAsInts = new int[code.SymbolCount];

            for (int i = 0; i < code.SymbolCount; i++)
            {
                code.GetSymbolAt(i).TryGetAsByte(out var symbolAsByte);
                symbolsAsInts[i] = symbolAsByte;
            }
            return(symbolsAsInts);
        }
Beispiel #4
0
        private void RepairBlock()
        {
            int[] dataWithECC = new int[this.preRepairData.SymbolCount + this.preRepairECC.SymbolCount];
            ECCBlock.ConvertToIntArray(preRepairData).CopyTo(dataWithECC, 0);
            ECCBlock.ConvertToIntArray(preRepairECC).CopyTo(dataWithECC, this.preRepairData.SymbolCount);

            var rsDecoder = new ReedSolomonDecoder(GenericGF.QR_CODE_FIELD_256);

            this.RepairSuccess = rsDecoder.decode(dataWithECC, this.preRepairECC.SymbolCount);


            if (dataWithECC.All(x => x == 0))   // TODO determine better way to detect zeroing out of block
            {
                this.RepairSuccess = false;
            }


            if (this.RepairSuccess)
            {
                var dataArr = new int[this.preRepairData.SymbolCount];
                var eccArr  = new int[this.preRepairECC.SymbolCount];
                Array.Copy(dataWithECC, 0, dataArr, 0, dataArr.Length);
                Array.Copy(dataWithECC, dataArr.Length, eccArr, 0, eccArr.Length);
                var dataIt  = new OverrideByteSymbolCodeValuesBitIterator <RawCodeByte>(this.preRepairData, dataArr);
                var eccIt   = new OverrideByteSymbolCodeValuesBitIterator <RawCodeByte>(this.preRepairECC, eccArr);
                var factory = new RawCodeByteFactory();
                this.postRepairData = CodeSymbolCode <RawCodeByte> .BuildInstance(dataIt, factory);

                this.postRepairECC = CodeSymbolCode <RawCodeByte> .BuildInstance(eccIt, factory);
            }
            else
            {
                this.postRepairData = this.preRepairData;
                this.postRepairECC  = this.preRepairECC;
            }
        }
Beispiel #5
0
 internal EncodedMessage(IBitIterator it, MessageMode setMode, CodeSymbolCode <MessageModeSymbol> setMessageModeSymbolCode)
 {
     this.MessageModeSymbolCode = setMessageModeSymbolCode;
     this.mode = setMode;
     this.ParseEncodingMessage(it, mode);
 }
Beispiel #6
0
 internal ECCBlock(CodeSymbolCode <RawCodeByte> _preRepairData, CodeSymbolCode <RawCodeByte> _preRepairECC)
 {
     this.preRepairData = _preRepairData;
     this.preRepairECC  = _preRepairECC;
     this.RepairBlock();
 }
Beispiel #7
0
        public static List <ECCBlock> DeInterleave(CodeSymbolCode <RawCodeByte> interleavedCode,
                                                   ErrorCorrectionLevel eccLevel)
        {
            // a block consists of data and ecc codewords for that block
            // blocks are in groups of blocks with the same amount of data codewords (max 2 groups)
            // blocks for all groups have the same amount of ecc codewords
            // the number of data codewords can differ between groups but not between blocks of a group
            var  eccGroups            = eccLevel.GetECCGroups();
            int  numberOfGroups       = eccGroups.Length;
            uint numberOfBlocks       = 0;
            uint numberOfRawCodeBytes = 0;

            foreach (ECCGroupInfo g in eccGroups)
            {
                numberOfBlocks       += g.NumberOfBlocks;
                numberOfRawCodeBytes += g.NumberOfBlocks * (g.DataBytesPerBlock + eccLevel.ECCBytesPerBlock);
            }

            if (interleavedCode.SymbolCount != numberOfRawCodeBytes)
            {
                throw new ArgumentException("Invalid number of symbols in interleaved code.");
            }

            uint largestBlockLength = 0;

            foreach (ECCGroupInfo g in eccGroups)
            {
                largestBlockLength = Math.Max(largestBlockLength, g.DataBytesPerBlock);
            }
            var dataCodewords = new RawCodeByte[numberOfBlocks, largestBlockLength];
            var eccCodewords  = new RawCodeByte[numberOfBlocks, eccLevel.ECCBytesPerBlock];
            int pos           = 0; // position in interleavedCode

            // generate data block code
            for (int j = 0; j < largestBlockLength; j++)                // for each symbol in block
            {
                for (int i = 0; i < numberOfBlocks; i++)                // for each block
                {
                    if (j < DeInterleaver.GetBlockLength(i, eccGroups)) // skip block if it is already full
                    {
                        dataCodewords[i, j] = interleavedCode.GetSymbolAt(pos++);
                        System.Diagnostics.Debug.Assert(dataCodewords[i, j] != null);
                    }
                }
            }
            // generate ecc block code
            for (int j = 0; j < eccLevel.ECCBytesPerBlock; j++)     // for each symbol in block
            {
                for (int i = 0; i < numberOfBlocks; i++)            // for each block
                {
                    eccCodewords[i, j] = interleavedCode.GetSymbolAt(pos++);
                }
            }

            var eccBlockList = new List <ECCBlock>();
            int absBlockNo   = 0;

            for (int g = 0; g < eccGroups.Length; g++)                       // group
            {
                for (int b = 0; b < eccGroups[g].NumberOfBlocks; b++)        // block
                {
                    var blockData = new List <RawCodeByte>();
                    var blockECC  = new List <RawCodeByte>();
                    for (int s = 0; s < eccGroups[g].DataBytesPerBlock; s++) // data symbol
                    {
                        blockData.Add(dataCodewords[absBlockNo, s]);
                    }
                    for (int e = 0; e < eccLevel.ECCBytesPerBlock; e++)      // ecc symbol
                    {
                        blockECC.Add(eccCodewords[absBlockNo, e]);
                    }
                    eccBlockList.Add(new ECCBlock(new CodeSymbolCode <RawCodeByte>(blockData),
                                                  new CodeSymbolCode <RawCodeByte>(blockECC)));
                    absBlockNo++;
                }
            }
            return(eccBlockList);
        }