コード例 #1
0
ファイル: QRCodeInternal.cs プロジェクト: triplelog/qr-art
        public static QRPolynomial GetErrorCorrectPolynomial(int errorCorrectLength)
        {
            var a = new QRPolynomial(new int[] { 1 }, 0);

            for (int i = 0; i < errorCorrectLength; i++)
            {
                a = a.Multiply(new QRPolynomial(new int[] { 1, QRMath.GExp(i) }, 0));
            }
            return(a);
        }
コード例 #2
0
ファイル: QRCodeInternal.cs プロジェクト: triplelog/qr-art
        public QRPolynomial Multiply(QRPolynomial other)
        {
            var num = new int[Length + other.Length - 1];

            for (int i = 0; i < Length; i++)
            {
                for (int j = 0; j < other.Length; j++)
                {
                    num[i + j] ^= QRMath.GExp(QRMath.GLog(this[i]) + QRMath.GLog(other[j]));
                }
            }

            return(new QRPolynomial(num, 0));
        }
コード例 #3
0
ファイル: QRCodeInternal.cs プロジェクト: triplelog/qr-art
        public QRPolynomial Mod(QRPolynomial other)
        {
            if (Length - other.Length < 0)
            {
                return(this);
            }

            var ratio = QRMath.GLog(this[0]) - QRMath.GLog(other[0]);

            var num = new int[Length];

            Array.Copy(_num, num, Length);
            for (int i = 0; i < other.Length; i++)
            {
                num[i] ^= QRMath.GExp(QRMath.GLog(other[i]) + ratio);
            }

            return(new QRPolynomial(num, 0).Mod(other));
        }
コード例 #4
0
ファイル: QRCodeInternal.cs プロジェクト: triplelog/qr-art
        private byte[] CreateBytes(QRBitBuffer buffer, QRRSBlock[] rsBlocks)
        {
            var offset = 0;

            var maxDcCount = 0;
            var maxEcCount = 0;

            var dcdata = new int[rsBlocks.Length][];
            var ecdata = new int[rsBlocks.Length][];

            for (var r = 0; r < rsBlocks.Length; r += 1)
            {
                var dcCount = rsBlocks[r].DataCount;
                var ecCount = rsBlocks[r].TotalCount - dcCount;

                maxDcCount = Math.Max(maxDcCount, dcCount);
                maxEcCount = Math.Max(maxEcCount, ecCount);

                dcdata[r] = new int[dcCount];

                for (var i = 0; i < dcdata[r].Length; i += 1)
                {
                    dcdata[r][i] = 0xff & buffer.GetByte(i + offset);
                }

                offset += dcCount;

                var rsPoly  = QRUtil.GetErrorCorrectPolynomial(ecCount);
                var rawPoly = new QRPolynomial(dcdata[r], rsPoly.Length - 1);

                var modPoly = rawPoly.Mod(rsPoly);
                ecdata[r] = new int[rsPoly.Length - 1];
                for (var i = 0; i < ecdata[r].Length; i += 1)
                {
                    var modIndex = i + modPoly.Length - ecdata[r].Length;
                    ecdata[r][i] = (modIndex >= 0) ? modPoly[modIndex] : 0;
                }
            }

            var totalCodeCount = 0;

            for (var i = 0; i < rsBlocks.Length; i += 1)
            {
                totalCodeCount += rsBlocks[i].TotalCount;
            }


            var data  = new byte[totalCodeCount];
            var index = 0;

            for (var i = 0; i < maxDcCount; i += 1)
            {
                for (var r = 0; r < rsBlocks.Length; r += 1)
                {
                    if (i < dcdata[r].Length)
                    {
                        data[index] = (byte)dcdata[r][i];
                        index      += 1;
                    }
                }
            }

            for (var i = 0; i < maxEcCount; i += 1)
            {
                for (var r = 0; r < rsBlocks.Length; r += 1)
                {
                    if (i < ecdata[r].Length)
                    {
                        data[index] = (byte)ecdata[r][i];
                        index      += 1;
                    }
                }
            }

            return(data);
        }