示例#1
0
        public static IEnumerable<long> GetPrimesWithErastosthenesUpTill(long max)
        {
            long topNumber = max;
            BitList numbers = new BitList(topNumber, 255);

            for (int i = 2; i < topNumber; i++)
                if (numbers.GetBit(i)) {
                    for (int j = i * 2; j < topNumber; j += i)
                        numbers.SetBit(j, false);
                }

            for (int i = 1; i < topNumber; i++)
                if (numbers.GetBit(i)) {
                    yield return i;
                }
        }
示例#2
0
        private static BitList StuffBits(BitList bits, int wordSize)
        {
            var  result = new BitList();
            int  n      = bits.Length;
            uint mask   = (uint)((1 << wordSize) - 2);

            for (int i = 0; i < n; i += wordSize)
            {
                uint word = 0;
                for (int j = 0; j < wordSize; j++)
                {
                    if (i + j >= n || bits.GetBit(i + j))
                    {
                        word |= (uint)(1 << (wordSize - 1 - j));
                    }
                }

                if ((word & mask) == mask)
                {
                    result.AddBits(word & mask, (byte)wordSize);
                    i--;
                }
                else if ((word & mask) == 0)
                {
                    result.AddBits(word | 1, (byte)wordSize);
                    i--;
                }
                else
                {
                    result.AddBits(word, (byte)wordSize);
                }
            }

            return(result);
        }
示例#3
0
        private static void DrawModeMessage(AztecCode matrix, bool compact, int matrixSize, BitList modeMessage)
        {
            int center = matrixSize / 2;

            if (compact)
            {
                for (int i = 0; i < 7; i++)
                {
                    int offset = center - 3 + i;
                    if (modeMessage.GetBit(i))
                    {
                        matrix.Set(offset, center - 5);
                    }

                    if (modeMessage.GetBit(i + 7))
                    {
                        matrix.Set(center + 5, offset);
                    }

                    if (modeMessage.GetBit(20 - i))
                    {
                        matrix.Set(offset, center + 5);
                    }

                    if (modeMessage.GetBit(27 - i))
                    {
                        matrix.Set(center - 5, offset);
                    }
                }
            }
            else
            {
                for (int i = 0; i < 10; i++)
                {
                    int offset = center - 5 + i + i / 5;
                    if (modeMessage.GetBit(i))
                    {
                        matrix.Set(offset, center - 7);
                    }

                    if (modeMessage.GetBit(i + 10))
                    {
                        matrix.Set(center + 7, offset);
                    }

                    if (modeMessage.GetBit(29 - i))
                    {
                        matrix.Set(offset, center + 7);
                    }

                    if (modeMessage.GetBit(39 - i))
                    {
                        matrix.Set(center - 7, offset);
                    }
                }
            }
        }
示例#4
0
 public bool At(int x, int y)
 {
     if (y != 0)
     {
         throw new ArgumentException("Should be 0 for 1D barcode", nameof(y));
     }
     if (x < 0 || x >= _bitList.Length)
     {
         throw new IndexOutOfRangeException($"Value of x out of range");
     }
     return(_bitList.GetBit(x));
 }
示例#5
0
        private static int[] BitsToWords(BitList stuffedBits, int wordSize, int wordCount)
        {
            var message = new int[wordCount];

            for (int i = 0; i < wordCount; i++)
            {
                int value = 0;
                for (int j = 0; j < wordSize; j++)
                {
                    if (stuffedBits.GetBit(i * wordSize + j))
                    {
                        value |= (1 << (wordSize - j - 1));
                    }
                }

                message[i] = value;
            }

            return(message);
        }
示例#6
0
        public static IBarcode Encode(string content, int minimumEccPercentage = 23, int userSpecifiedLayers = 0)
        {
            BitList bits    = HighLevelEncoding.Encode(content.Select(x => (byte)x).ToArray());
            int     eccBits = ((bits.Length * minimumEccPercentage) / 100) + 11;

            int     totalSizeBits = bits.Length + eccBits;
            int     layers, totalBitsInLayer, wordSize;
            bool    compact;
            BitList stuffedBits;

            if (userSpecifiedLayers != 0)
            {
                compact = userSpecifiedLayers < 0;
                layers  = compact ? -userSpecifiedLayers : userSpecifiedLayers;

                if ((compact && layers > MaximumNumberOfCompactBits) || (!compact && layers > MaximumNumberOfBits))
                {
                    throw new InvalidOperationException($"Illegal value {userSpecifiedLayers} for layers");
                }

                totalBitsInLayer = GetTotalBitsInLayer(layers, compact);
                wordSize         = WordSize[layers];
                int usableBitsInLayer = totalBitsInLayer - (totalBitsInLayer % wordSize);
                stuffedBits = StuffBits(bits, wordSize);

                if (stuffedBits.Length + eccBits > usableBitsInLayer)
                {
                    throw new InvalidOperationException("Data too large for user specified layer");
                }

                if (compact && stuffedBits.Length > wordSize * 64)
                {
                    throw new InvalidOperationException("Data too large for user specified layer");
                }
            }
            else
            {
                stuffedBits = new BitList();
                wordSize    = 0;

                // We look at the possible table sizes in the order Compact1, Compact2, Compact3,
                // Compact4, Normal4,...  Normal(i) for i < 4 isn't typically used since Compact(i+1)
                // is the same size, but has more data.
                for (int i = 0; ; i++)
                {
                    if (i > MaximumNumberOfBits)
                    {
                        throw new InvalidOperationException("Data too large for an aztec code");
                    }

                    compact = i <= 3;
                    layers  = compact ? i + 1 : i;

                    totalBitsInLayer = GetTotalBitsInLayer(layers, compact);
                    if (totalSizeBits > totalBitsInLayer)
                    {
                        continue;
                    }

                    // [Re]stuff the bits if this is the first opportunity, or if the
                    // wordSize has changed
                    if (wordSize != WordSize[layers])
                    {
                        wordSize    = WordSize[layers];
                        stuffedBits = StuffBits(bits, wordSize);
                    }

                    int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize);

                    if (compact && stuffedBits.Length > wordSize * 64)
                    {
                        continue; // Compact format only allows 64 data words, though C4 can hold more words than that
                    }
                    if (stuffedBits.Length + eccBits <= usableBitsInLayers)
                    {
                        break;
                    }
                }
            }
            BitList messageBits        = ErrorCorrection.GenerateCheckWords(stuffedBits, totalBitsInLayer, wordSize);
            int     messageSizeInWords = stuffedBits.Length / wordSize;
            BitList modeMessage        = GenerateModeMessage(compact, layers, messageSizeInWords);

            // allocate symbol
            int baseMatrixSize = compact
                ? 11 + layers * 4
                : 14 + layers * 4;
            var alignmentMap = new int[baseMatrixSize];
            int matrixSize;

            if (compact)
            {
                // no alignment marks in compact mode, alignmentMap is a no-op
                matrixSize = baseMatrixSize;
                for (int i = 0; i < alignmentMap.Length; i++)
                {
                    alignmentMap[i] = i;
                }
            }
            else
            {
                matrixSize = baseMatrixSize + 1 + 2 * ((baseMatrixSize / 2 - 1) / 15);
                int origCenter = baseMatrixSize / 2;
                int center     = matrixSize / 2;
                for (int i = 0; i < origCenter; i++)
                {
                    int newOffset = i + i / 15;
                    alignmentMap[origCenter - i - 1] = center - newOffset - 1;
                    alignmentMap[origCenter + i]     = center + newOffset + 1;
                }
            }
            var code = new AztecCode(matrixSize);

            code.Content = content;

            // draw data bits
            for (int i = 0, rowOffset = 0; i < layers; i++)
            {
                int rowSize = (layers - i) * 4;
                if (compact)
                {
                    rowSize += 9;
                }
                else
                {
                    rowSize += 12;
                }

                for (int j = 0; j < rowSize; j++)
                {
                    int columnOffset = j * 2;
                    for (int k = 0; k < 2; k++)
                    {
                        if (messageBits.GetBit(rowOffset + columnOffset + k))
                        {
                            code.Set(alignmentMap[i * 2 + k], alignmentMap[i * 2 + j]);
                        }

                        if (messageBits.GetBit(rowOffset + rowSize * 2 + columnOffset + k))
                        {
                            code.Set(alignmentMap[i * 2 + j], alignmentMap[baseMatrixSize - 1 - i * 2 - k]);
                        }

                        if (messageBits.GetBit(rowOffset + rowSize * 4 + columnOffset + k))
                        {
                            code.Set(alignmentMap[baseMatrixSize - 1 - i * 2 - k], alignmentMap[baseMatrixSize - 1 - i * 2 - j]);
                        }

                        if (messageBits.GetBit(rowOffset + rowSize * 6 + columnOffset + k))
                        {
                            code.Set(alignmentMap[baseMatrixSize - 1 - i * 2 - j], alignmentMap[i * 2 + k]);
                        }
                    }
                }

                rowOffset += rowSize * 8;
            }

            // draw mode message
            DrawModeMessage(code, compact, matrixSize, modeMessage);

            // draw alignment marks
            if (compact)
            {
                DrawBullsEye(code, matrixSize / 2, 5);
            }
            else
            {
                DrawBullsEye(code, matrixSize / 2, 7);
                for (int i = 0, j = 0; i < baseMatrixSize / 2 - 1; i += 15, j += 16)
                {
                    for (int k = (matrixSize / 2) & 1; k < matrixSize; k += 2)
                    {
                        code.Set(matrixSize / 2 - j, k);
                        code.Set(matrixSize / 2 + j, k);
                        code.Set(k, matrixSize / 2 - j);
                        code.Set(k, matrixSize / 2 + j);
                    }
                }
            }

            return(code);
        }
示例#7
0
 internal bool Get(int x, int y)
 => _data.GetBit(x * Dimension + y);
示例#8
0
 internal bool Get(int x, int y)
 => _data.GetBit(x * _size.Rows + y);
示例#9
0
 public bool At(int x, int y) => _data.GetBit(y * _width + x);
示例#10
0
 public bool At(int x, int y)
 => _data.GetBit((y / Dimensions.ModuleHeight) * _width + x);