예제 #1
0
        /// <summary>
        /// Embed "dataBits" using "getMaskPattern". On success, modify the matrix and return true.
        /// For debugging purposes, it skips masking process if "getMaskPattern" is -1.
        /// See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
        /// </summary>
        /// <param name="dataBits">The data bits.</param>
        /// <param name="maskPattern">The mask pattern.</param>
        /// <param name="matrix">The matrix.</param>
        public static void EmbedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix)
        {
            int bitIndex  = 0;
            int direction = -1;
            // Start from the right bottom cell.
            int x = matrix.Width - 1;
            int y = matrix.Height - 1;

            while (x > 0)
            {
                // Skip the vertical timing pattern.
                if (x == 6)
                {
                    x -= 1;
                }
                while (y >= 0 && y < matrix.Height)
                {
                    for (int i = 0; i < 2; ++i)
                    {
                        int xx = x - i;
                        // Skip the cell if it's not empty.
                        if (!IsEmpty(matrix[xx, y]))
                        {
                            continue;
                        }
                        int bit;
                        if (bitIndex < dataBits.Size)
                        {
                            bit = dataBits[bitIndex] ? 1 : 0;
                            ++bitIndex;
                        }
                        else
                        {
                            // Padding bit. If there is no bit left, we'll fill the left cells with 0, as described
                            // in 8.4.9 of JISX0510:2004 (p. 24).
                            bit = 0;
                        }

                        // Skip masking if mask_pattern is -1.
                        if (maskPattern != -1)
                        {
                            if (MaskUtil.GetDataMaskBit(maskPattern, xx, y))
                            {
                                bit ^= 0x1;
                            }
                        }
                        matrix[xx, y] = bit;
                    }
                    y += direction;
                }
                direction = -direction; // Reverse the direction.
                y        += direction;
                x        -= 2;          // Move to the left.
            }
            // All bits should be consumed.
            if (bitIndex != dataBits.Size)
            {
                throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.Size);
            }
        }
예제 #2
0
 // The mask penalty calculation is complicated.  See Table 21 of JISX0510:2004 (p.45) for details.
 // Basically it applies four rules and summate all penalties.
 private static int CalculateMaskPenalty(ByteMatrix matrix)
 {
     return MaskUtil.ApplyMaskPenaltyRule1(matrix)
             + MaskUtil.ApplyMaskPenaltyRule2(matrix)
             + MaskUtil.ApplyMaskPenaltyRule3(matrix)
             + MaskUtil.ApplyMaskPenaltyRule4(matrix);
 }