Example #1
0
        private void SetupPositionAdjustPattern()
        {
            var pos = QRUtil.GetPatternPosition(_typeNumber);

            for (var i = 0; i < pos.Length; i++)
            {
                for (var j = 0; j < pos.Length; j++)
                {
                    var row = pos[i];
                    var col = pos[j];

                    if (_modules[row][col] != null)
                    {
                        continue;
                    }

                    for (var r = -2; r <= 2; r++)
                    {
                        for (var c = -2; c <= 2; c++)
                        {
                            if (r == -2 || r == 2 || c == -2 || c == 2 ||
                                (r == 0 && c == 0))
                            {
                                _modules[row + r][col + c] = true;
                            }
                            else
                            {
                                _modules[row + r][col + c] = false;
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        private void MapData(DataCache data, QRMaskPattern maskPattern)
        {
            var inc       = -1;
            var row       = _moduleCount - 1;
            var bitIndex  = 7;
            var byteIndex = 0;

            for (var col = _moduleCount - 1; col > 0; col -= 2)
            {
                if (col == 6)
                {
                    col--;
                }

                while (true)
                {
                    for (int c = 0; c < 2; c++)
                    {
                        if (_modules[row][col - c] == null)
                        {
                            var dark = false;

                            if (byteIndex < data.Count)
                            {
                                dark = (((Convert.ToUInt32(data[byteIndex]) >> bitIndex) & 1) == 1);
                            }

                            var mask = QRUtil.GetMask(maskPattern, row, col - c);

                            if (mask)
                            {
                                dark = !dark;
                            }

                            _modules[row][col - c] = dark;
                            bitIndex--;

                            if (bitIndex == -1)
                            {
                                byteIndex++;
                                bitIndex = 7;
                            }
                        }
                    }

                    row += inc;

                    if (row < 0 || _moduleCount <= row)
                    {
                        row -= inc;
                        inc  = -inc;
                        break;
                    }
                }
            }
        }
Example #3
0
        internal static int GetBCHTypeNumber(int data)
        {
            var d = data << 12;

            while (QRUtil.GetBCHDigit(d) - QRUtil.GetBCHDigit(QRUtil.G18) >= 0)
            {
                d ^= (QRUtil.G18 << (QRUtil.GetBCHDigit(d) - QRUtil.GetBCHDigit(QRUtil.G18)));
            }

            return((data << 12) | d);
        }
Example #4
0
        internal static int GetBCHTypeInfo(int data)
        {
            var d = (data << 10);
            var s = 0;

            while ((s = (int)(QRUtil.GetBCHDigit(d) - QRUtil.GetBCHDigit(QRUtil.G15))) >= 0)
            {
                d ^= (Convert.ToInt32(QRUtil.G15) << s);
            }

            return(((data << 10) | d) ^ QRUtil.G15_MASK);
        }
Example #5
0
        private void SetupTypeNumber(bool test)
        {
            var bits = QRUtil.GetBCHTypeNumber(_typeNumber);

            for (var i = 0; i < 18; i++)
            {
                var mod = (!test && ((bits >> i) & 1) == 1);
                _modules[(int)Math.Floor(Convert.ToDouble(i) / 3.0)][i % 3 + _moduleCount - 8 - 3] = mod;
            }

            for (var i = 0; i < 18; i++)
            {
                var mod = (!test && ((bits >> i) & 1) == 1);
                _modules[i % 3 + _moduleCount - 8 - 3][(int)Math.Floor(Convert.ToDouble(i) / 3.0)] = mod;
            }
        }
Example #6
0
        private void SetupTypeInfo(bool test, QRMaskPattern maskPattern)
        {
            var data = ((int)_errorCorrectLevel << 3) | (int)maskPattern;
            var bits = QRUtil.GetBCHTypeInfo(data);

            // vertical
            for (var i = 0; i < 15; i++)
            {
                var mod = (!test && ((bits >> i) & 1) == 1);

                if (i < 6)
                {
                    _modules[i][8] = mod;
                }
                else if (i < 8)
                {
                    _modules[i + 1][8] = mod;
                }
                else
                {
                    _modules[_moduleCount - 15 + i][8] = mod;
                }
            }

            // horizontal
            for (var i = 0; i < 15; i++)
            {
                var mod = (!test && ((bits >> i) & 1) == 1);

                if (i < 8)
                {
                    _modules[8][_moduleCount - i - 1] = mod;
                }
                else if (i < 9)
                {
                    _modules[8][15 - i - 1 + 1] = mod;
                }
                else
                {
                    _modules[8][15 - i - 1] = mod;
                }
            }

            // fixed module
            _modules[_moduleCount - 8][8] = (!test);
        }
Example #7
0
        private QRMaskPattern GetBestMaskPattern()
        {
            var minLostPoint = 0.0;
            var pattern      = QRMaskPattern.PATTERN000;

            for (var i = 0; i < 8; i++)
            {
                MakeImpl(true, (QRMaskPattern)i);
                var lostPoint = QRUtil.GetLostPoint(this);

                if (i == 0 || minLostPoint > lostPoint)
                {
                    minLostPoint = lostPoint;
                    pattern      = (QRMaskPattern)i;
                }
            }

            return(pattern);
        }
Example #8
0
        private DataCache CreateBytes(QRBitBuffer buffer, List <QRRSBlock> rsBlocks)
        {
            var offset     = 0;
            var maxDcCount = 0;
            var maxEcCount = 0;
            var dcdata     = new DataCache[(rsBlocks.Count)];
            var ecdata     = new DataCache[(rsBlocks.Count)];

            for (var r = 0; r < rsBlocks.Count; r++)
            {
                var dcCount = rsBlocks[r].DataCount;
                var ecCount = rsBlocks[r].TotalCount - dcCount;
                maxDcCount = Math.Max(maxDcCount, dcCount);
                maxEcCount = Math.Max(maxEcCount, ecCount);
                dcdata[r]  = new DataCache(dcCount);

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

                offset += dcCount;
                var rsPoly  = QRUtil.GetErrorCorrectPolynomial(ecCount);
                var rawPoly = new QRPolynomial(dcdata[r], rsPoly.GetLength() - 1);
                var modPoly = rawPoly.Mod(rsPoly);
                ecdata[r] = new DataCache(rsPoly.GetLength() - 1);

                for (var i = 0; i < ecdata[r].Count; i++)
                {
                    int modIndex = i + modPoly.GetLength() - ecdata[r].Count;
                    ecdata[r][i] = (modIndex >= 0) ? modPoly.Get(modIndex) : 0;
                }
            }

            var totalCodeCount = 0;

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

            var data  = new DataCache(totalCodeCount);
            var index = 0;

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

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

            return(data);
        }
Example #9
0
        private DataCache CreateData(int typeNumber, QRErrorCorrectLevel errorCorrectLevel, List <QR8bitByte> dataList)
        {
            var rsBlocks = QRRSBlock.GetRSBlocks(typeNumber, errorCorrectLevel);
            var buffer   = new QRBitBuffer();

            for (var i = 0; i < dataList.Count; i++)
            {
                QR8bitByte data = dataList[i];
                buffer.Put((int)data.Mode, 4);
                buffer.Put(data.Length, QRUtil.GetLengthInBits(data.Mode, typeNumber));
                data.Write(buffer);
            }

            // calc num max data.
            int totalDataCount = 0;

            for (var i = 0; i < rsBlocks.Count; i++)
            {
                totalDataCount += rsBlocks[i].DataCount;
            }

            if (buffer.GetLengthInBits() > totalDataCount * 8)
            {
                throw new ErrorException(string.Format(
                                             "code length overflow ({0} > {1})",
                                             buffer.GetLengthInBits(),
                                             totalDataCount * 8), ErrorCode.CodeLengthOverflow);
            }

            // end code
            if (buffer.GetLengthInBits() + 4 <= totalDataCount * 8)
            {
                buffer.Put(0, 4);
            }

            // padding
            while (buffer.GetLengthInBits() % 8 != 0)
            {
                buffer.PutBit(false);
            }

            // padding
            while (true)
            {
                if (buffer.GetLengthInBits() >= totalDataCount * 8)
                {
                    break;
                }

                buffer.Put(QRCode.PAD0, 8);

                if (buffer.GetLengthInBits() >= totalDataCount * 8)
                {
                    break;
                }

                buffer.Put(QRCode.PAD1, 8);
            }

            return(CreateBytes(buffer, rsBlocks));
        }