public void Test_against_reference_implementation(int version, TriStateMatrix expected)
   {
 
       TriStateMatrix target = new TriStateMatrix(expected.Width);
       PositioninngPatternBuilder.EmbedBasicPatterns(version, target);
       expected.AssertEquals(target);
   }
Esempio n. 2
0
 internal static void EmbedBasicPatterns(int version, TriStateMatrix matrix)
 {
     new PositionDetectionPattern(version).ApplyTo(matrix);
     new DarkDotAtLeftBottom(version).ApplyTo(matrix);
     new AlignmentPattern(version).ApplyTo(matrix);
     new TimingPattern(version).ApplyTo(matrix);
 }
Esempio n. 3
0
        private static TriStateMatrix XorMatrix(TriStateMatrix first, BitMatrix second)
        {
            int            width        = first.Width;
            TriStateMatrix maskedMatrix = new TriStateMatrix(width);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < width; y++)
                {
                    MatrixStatus states = first.MStatus(x, y);
                    switch (states)
                    {
                    case MatrixStatus.NoMask:
                        maskedMatrix[x, y, MatrixStatus.NoMask] = first[x, y];
                        break;

                    case MatrixStatus.Data:
                        maskedMatrix[x, y, MatrixStatus.Data] = first[x, y] ^ second[x, y];
                        break;

                    default:
                        throw new ArgumentException("TristateMatrix has None value cell.", "first");
                    }
                }
            }

            return(maskedMatrix);
        }
        public static void AssertEquals(this TriStateMatrix expected, TriStateMatrix actual)
        {
            if (expected == null) throw new ArgumentNullException("expected");
            if (actual == null) throw new ArgumentNullException("actual");

            if (expected.Width != actual.Width)
            {
                Assert.Fail("Mtrix must have same size. Expected {0}, Actual {1}", expected.Width, actual.Width);
            }

            
            for (int i = 0; i < expected.Width; i++)
                for (int j = 0; j < expected.Width; j++)
                {
                    if (expected.MStatus(i, j) != MatrixStatus.None && actual.MStatus(i, j) != MatrixStatus.None && expected[i, j] != actual[i, j])
                    {
                        Assert.Fail("Matrces are different.\nExpected:{0}Actual:{1}.", TriStateMatrixToGraphicExtensions.ToGraphicString(expected), TriStateMatrixToGraphicExtensions.ToGraphicString(actual));
                    }

                    if (expected.MStatus(i, j) == MatrixStatus.None && actual.MStatus(i, j) != MatrixStatus.None)
                    {
                        Assert.Fail("Matrces are different.\nExpected:{0}Actual:{1}.", TriStateMatrixToGraphicExtensions.ToGraphicString(expected), TriStateMatrixToGraphicExtensions.ToGraphicString(actual));
                    }
                    if (expected.MStatus(i, j) != MatrixStatus.None && actual.MStatus(i, j) == MatrixStatus.None)
                    {
                        Assert.Fail("Matrces are different.\nExpected:{0}Actual:{1}.", TriStateMatrixToGraphicExtensions.ToGraphicString(expected), TriStateMatrixToGraphicExtensions.ToGraphicString(actual));
                    }
                }
        }
Esempio n. 5
0
        public void Test_against_DataSet(int version, TriStateMatrix expected)
        {
            TriStateMatrix target = new TriStateMatrix(expected.Width);

            PositioninngPatternBuilder.EmbedBasicPatterns(version, target);
            expected.AssertEquals(target);
        }
Esempio n. 6
0
        public static TriStateMatrix FromBase64(string base64String)
        {
            byte[]         bytes  = Convert.FromBase64String(base64String);
            TriStateMatrix matrix = FromBytes(bytes);

            return(matrix);
        }
 internal static void EmbedBasicPatterns(int version, TriStateMatrix matrix)
 {
     new PositionDetectionPattern(version).ApplyTo(matrix);
     new DarkDotAtLeftBottom(version).ApplyTo(matrix);
     new AlignmentPattern(version).ApplyTo(matrix);
     new TimingPattern(version).ApplyTo(matrix);
 }
Esempio n. 8
0
        public static TriStateMatrix Xor(this TriStateMatrix first, Pattern second, ErrorCorrectionLevel errorlevel)
        {
            TriStateMatrix result = XorMatrix(first, second);

            result.EmbedFormatInformation(errorlevel, second);
            return(result);
        }
Esempio n. 9
0
 public override void ApplyTo(TriStateMatrix matrix)
 {
     foreach (MatrixPoint coordinatePair in GetNonColidingCoordinatePairs(matrix))
     {
         this.CopyTo(matrix, coordinatePair, MatrixStatus.NoMask);
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Embed version information to Matrix
        /// Only for version greater than or equal to 7
        /// </summary>
        /// <param name="tsMatrix">Matrix</param>
        /// <param name="version">version number</param>
        internal static void EmbedVersionInformation(this TriStateMatrix tsMatrix, int version)
        {
            if (version < 7)
            {
                return;
            }

            BitList versionInfo = VersionInfoBitList(version);

            int matrixWidth = tsMatrix.Width;
            // 1 cell between version info and position stencil
            int shiftLength = QRCodeConstantVariable.PositionStencilWidth + VIRectangleHeight + 1;
            // Reverse order input
            int viIndex = LengthDataBits + LengthECBits - 1;

            for (int viWidth = 0; viWidth < VIRectangleWidth; viWidth++)
            {
                for (int viHeight = 0; viHeight < VIRectangleHeight; viHeight++)
                {
                    bool bit = versionInfo[viIndex];
                    viIndex--;
                    // Bottom left
                    tsMatrix[viWidth, (matrixWidth - shiftLength + viHeight), MatrixStatus.NoMask] = bit;
                    // Top right
                    tsMatrix[(matrixWidth - shiftLength + viHeight), viWidth, MatrixStatus.NoMask] = bit;
                }
            }
        }
Esempio n. 11
0
 public override void ApplyTo(TriStateMatrix matrix)
 {
     foreach (MatrixPoint coordinatePair in GetNonColidingCoordinatePairs(matrix))
     {
         CopyTo(matrix, coordinatePair, MatrixStatus.NoMask);
     }
 }
Esempio n. 12
0
        private static TriStateMatrix FromBytes(byte[] bytes)
        {
            int length = BitConverter.ToInt32(bytes, 0);

            byte[] data = new byte[(bytes.Length - sizeof(int)) * sizeof(byte)];
            Array.Copy(bytes, sizeof(int), data, 0, data.Length);

            int width = (int)Math.Sqrt(length);

            if (width * width != length)
            {
                throw new ArgumentException(string.Format("The header containsinvalid length value. Length must be a square of width.{0}", length), "bytes");
            }

            BitArray       bitArray = new BitArray(data);
            TriStateMatrix matrix   = new TriStateMatrix(width);

            for (int index = 0; index < length; index++)
            {
                int i = index % width;
                int j = index / width;

                matrix[i, j, MatrixStatus.Data] = bitArray[index];
            }
            return(matrix);
        }
Esempio n. 13
0
        /// <summary>
        /// Xors the matrix.
        /// </summary>
        /// <param name="first">The first.</param>
        /// <param name="second">The second.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private static TriStateMatrix XorMatrix(TriStateMatrix first, BitMatrix second)
        {
            int width = first.Width;
            var maskedMatrix = new TriStateMatrix(width);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < width; y++)
                {
                    MatrixStatus states = first.MStatus(x, y);
                    switch (states)
                    {
                        case MatrixStatus.NoMask:
                            maskedMatrix[x, y, MatrixStatus.NoMask] = first[x, y];
                            break;
                        case MatrixStatus.Data:
                            maskedMatrix[x, y, MatrixStatus.Data] = first[x, y] ^ second[x, y];
                            break;
                        default:
                            throw new ArgumentException("TristateMatrix has None value cell.", "first");
                    }
                }
            }

            return maskedMatrix;
        }
Esempio n. 14
0
 internal static TriStateMatrix Embed(this TriStateMatrix matrix, BitMatrix stencil, IEnumerable <MatrixPoint> locations)
 {
     foreach (MatrixPoint location in locations)
     {
         Embed(matrix, stencil, location);
     }
     return(matrix);
 }
Esempio n. 15
0
        public void Test_against_DataSet(TriStateMatrix input, MaskPatternType patternType, BitMatrix expected)
        {
            Pattern pattern = new PatternFactory().CreateByType(patternType);

            BitMatrix result = input.Apply(pattern, ErrorCorrectionLevel.H);

            expected.AssertEquals(result);
        }
Esempio n. 16
0
 private void Test_One_Case(int version, TriStateMatrix expected)
 {
 	TriStateMatrix target = new TriStateMatrix(expected.Width);
     PositioninngPatternBuilder.EmbedBasicPatterns(version, target);
     target.EmbedVersionInformation(version);
     
 	expected.AssertEquals(target);
 }
Esempio n. 17
0
        public void Test_against_DataSet(TriStateMatrix input, MaskPatternType patternType, BitMatrix expected)
        {
            Pattern pattern = new PatternFactory().CreateByType(patternType);

            BitMatrix result = input.Apply(pattern, ErrorCorrectionLevel.H);

            expected.AssertEquals(result);
        }
Esempio n. 18
0
        private void Test_One_Case(int version, TriStateMatrix expected)
        {
            TriStateMatrix target = new TriStateMatrix(expected.Width);

            PositioninngPatternBuilder.EmbedBasicPatterns(version, target);
            target.EmbedVersionInformation(version);

            expected.AssertEquals(target);
        }
Esempio n. 19
0
        internal static void TryEmbedCodewords(this TriStateMatrix tsMatrix, BitList codewords)
        {
            int sWidth        = tsMatrix.Width;
            int codewordsSize = codewords.Count;

            int bitIndex    = 0;
            int directionUp = -1;

            int x = sWidth - 1;
            int y = sWidth - 1;

            while (x > 0)
            {
                //Skip vertical timing pattern
                if (x == 6)
                {
                    x -= 1;
                }

                while (y >= 0 && y < sWidth)
                {
                    for (int xOffset = 0; xOffset < 2; xOffset++)
                    {
                        int xPos = x - xOffset;
                        if (tsMatrix.MStatus(xPos, y) != MatrixStatus.None)
                        {
                            continue;
                        }
                        else
                        {
                            bool bit;
                            if (bitIndex < codewordsSize)
                            {
                                bit = codewords[bitIndex];
                                bitIndex++;
                            }
                            else
                            {
                                bit = false;
                            }

                            tsMatrix[xPos, y, MatrixStatus.Data] = bit;
                        }
                    }
                    y = NextY(y, directionUp);
                }
                directionUp = ChangeDirection(directionUp);
                y           = NextY(y, directionUp);
                x          -= 2;
            }

            if (bitIndex != codewordsSize)
            {
                throw new Exception($"Not all bits from codewords consumed by matrix: {bitIndex} / {codewordsSize}");
            }
        }
Esempio n. 20
0
 private void Test_One_Case(int version, MaskPatternType patternType, TriStateMatrix expected)
 {
 	TriStateMatrix target = new TriStateMatrix(expected.Width);
     PositioninngPatternBuilder.EmbedBasicPatterns(version, target);
     PatternFactory pf = new PatternFactory();
     Pattern pt = pf.CreateByType(patternType);
     target.EmbedFormatInformation(ErrorCorrectionLevel.H, pt);
     
 	expected.AssertEquals(target);
 }
Esempio n. 21
0
        private TestCaseData GenerateRandomTestCaseData(int matrixSize, Random randomizer, int pattern)
        {
            ByteMatrix     matrix;
            TriStateMatrix input = GetOriginal(matrixSize, randomizer, out matrix);

            ApplyPattern(matrix, pattern);
            BitMatrix expected = matrix.ToBitMatrix();

            return(new TestCaseData(input, pattern, expected));
        }
Esempio n. 22
0
        internal static string ToGraphicString(this TriStateMatrix matrix)
        {
            StringBuilder result = new StringBuilder(matrix.Width * matrix.Width + matrix.Width + 5);

            using (StringWriter writer = new StringWriter(result))
            {
                matrix.ToGraphic(writer);
            }
            return(result.ToString());
        }
Esempio n. 23
0
 private void Test_One_Case(int version, TriStateMatrix expected, IEnumerable<bool> codewords)
 {
 	BitList dcList = new BitList();
 	dcList.Add(codewords);
 	
 	TriStateMatrix target = new TriStateMatrix(expected.Width);
     PositioninngPatternBuilder.EmbedBasicPatterns(version, target);
     target.TryEmbedCodewords(dcList);
     
 	expected.AssertEquals(target);
 }
Esempio n. 24
0
 internal void CopyTo(TriStateMatrix target, MatrixRectangle sourceArea, MatrixPoint targetPoint, MatrixStatus mstatus)
 {
     for (int j = 0; j < sourceArea.Size.Height; j++)
     {
         for (int i = 0; i < sourceArea.Size.Width; i++)
         {
             bool value = this[sourceArea.Location.X + i, sourceArea.Location.Y + j];
             target[targetPoint.X + i, targetPoint.Y + j, mstatus] = value;
         }
     }
 }
 private static BitMatrix FromGraphics(string[] lines)
 {
     var matrix = new TriStateMatrix(lines.Length);
     for (int j = 0; j < matrix.Width; j++)
     {
         for (int i = 0; i < matrix.Width; i++)
         {
             matrix[i, j, MatrixStatus.Data] = lines[j][i] == s_1Char;
         }
     }
     return matrix;
 }
Esempio n. 26
0
 /// <summary>
 /// Copies to.
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="sourceArea">The source area.</param>
 /// <param name="targetPoint">The target point.</param>
 /// <param name="mstatus">The mstatus.</param>
 /// <remarks></remarks>
 internal void CopyTo(TriStateMatrix target, MatrixRectangle sourceArea, MatrixPoint targetPoint,
                      MatrixStatus mstatus)
 {
     for (int j = 0; j < sourceArea.Size.Height; j++)
     {
         for (int i = 0; i < sourceArea.Size.Width; i++)
         {
             bool value = this[sourceArea.Location.X + i, sourceArea.Location.Y + j];
             target[targetPoint.X + i, targetPoint.Y + j, mstatus] = value;
         }
     }
 }
Esempio n. 27
0
        private void Test_One_Case(int version, MaskPatternType patternType, TriStateMatrix expected)
        {
            TriStateMatrix target = new TriStateMatrix(expected.Width);

            PositioninngPatternBuilder.EmbedBasicPatterns(version, target);
            PatternFactory pf = new PatternFactory();
            Pattern        pt = pf.CreateByType(patternType);

            target.EmbedFormatInformation(ErrorCorrectionLevel.H, pt);

            expected.AssertEquals(target);
        }
        public override void ApplyTo(TriStateMatrix matrix)
        {
            MatrixSize size = GetSizeOfSquareWithSeparators();

            MatrixPoint leftTopCorner = new MatrixPoint(0, 0);
            this.CopyTo(matrix, new MatrixRectangle(new MatrixPoint(1, 1), size), leftTopCorner, MatrixStatus.NoMask);

            MatrixPoint rightTopCorner = new MatrixPoint(matrix.Width - this.Width + 1, 0);
            this.CopyTo(matrix, new MatrixRectangle(new MatrixPoint(0, 1), size), rightTopCorner, MatrixStatus.NoMask);

            MatrixPoint leftBottomCorner = new MatrixPoint(0, matrix.Width - this.Width + 1);
            this.CopyTo(matrix, new MatrixRectangle(new MatrixPoint(1, 0), size), leftBottomCorner, MatrixStatus.NoMask);
        }
Esempio n. 29
0
        private void Test_One_Case(int version, TriStateMatrix expected, IEnumerable <bool> codewords)
        {
            BitList dcList = new BitList();

            dcList.Add(codewords);

            TriStateMatrix target = new TriStateMatrix(expected.Width);

            PositioninngPatternBuilder.EmbedBasicPatterns(version, target);
            target.TryEmbedCodewords(dcList);

            expected.AssertEquals(target);
        }
 public static TriStateMatrix ToTriStateMatrix(this BitMatrix input)
 {
 	int width = input.Width;
 	TriStateMatrix result = new TriStateMatrix(width);
 	for(int x = 0; x < width; x++)
 	{
 		for(int y = 0; y < width; y++)
 		{
 			result[x, y, MatrixStatus.Data] = input[x, y];
 		}
 	}
 	return result;
 }
Esempio n. 31
0
        private static BitMatrix FromGraphics(string[] lines)
        {
            var matrix = new TriStateMatrix(lines.Length);

            for (int j = 0; j < matrix.Width; j++)
            {
                for (int i = 0; i < matrix.Width; i++)
                {
                    matrix[i, j, MatrixStatus.Data] = lines[j][i] == s_1Char;
                }
            }
            return(matrix);
        }
Esempio n. 32
0
        public static TriStateMatrix ToTriStateMatrix(this BitMatrix input)
        {
            int            width  = input.Width;
            TriStateMatrix result = new TriStateMatrix(width);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < width; y++)
                {
                    result[x, y, MatrixStatus.Data] = input[x, y];
                }
            }
            return(result);
        }
Esempio n. 33
0
 internal static TriStateMatrix ToBitMatrix(this Common.ByteMatrix byteMatrix) 
 {
     TriStateMatrix matrix = new TriStateMatrix(byteMatrix.Width);
     for (int i = 0; i < byteMatrix.Width; i++)
     {
         for (int j = 0; j < byteMatrix.Height; j++)
         {
             if (byteMatrix[j, i] != -1)
             {
                 matrix[i, j, MatrixStatus.NoMask] = byteMatrix[j, i] != 0;
             }
         }
     }
     return matrix;
 }
 /// <summary>
 /// Toes the pattern bit matrix.
 /// </summary>
 /// <param name="byteMatrix">The byte matrix.</param>
 /// <returns></returns>
 /// <remarks></remarks>
 internal static TriStateMatrix ToPatternBitMatrix(this ByteMatrix byteMatrix)
 {
     var matrix = new TriStateMatrix(byteMatrix.Width);
     for (int i = 0; i < byteMatrix.Width; i++)
     {
         for (int j = 0; j < byteMatrix.Height; j++)
         {
             if (byteMatrix[j, i] != -1)
             {
                 matrix[i, j, MatrixStatus.Data] = byteMatrix[j, i] != 0;
             }
         }
     }
     return matrix;
 }
Esempio n. 35
0
        public override void ApplyTo(TriStateMatrix matrix)
        {
            MatrixSize size = GetSizeOfSquareWithSeparators();

            MatrixPoint leftTopCorner = new MatrixPoint(0, 0);

            CopyTo(matrix, new MatrixRectangle(new MatrixPoint(1, 1), size), leftTopCorner, MatrixStatus.NoMask);

            MatrixPoint rightTopCorner = new MatrixPoint(matrix.Width - Width + 1, 0);

            CopyTo(matrix, new MatrixRectangle(new MatrixPoint(0, 1), size), rightTopCorner, MatrixStatus.NoMask);

            MatrixPoint leftBottomCorner = new MatrixPoint(0, matrix.Width - Width + 1);

            CopyTo(matrix, new MatrixRectangle(new MatrixPoint(1, 0), size), leftBottomCorner, MatrixStatus.NoMask);
        }
Esempio n. 36
0
        /// <summary>
        /// Toes the pattern bit matrix.
        /// </summary>
        /// <param name="byteMatrix">The byte matrix.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        internal static TriStateMatrix ToPatternBitMatrix(this ByteMatrix byteMatrix)
        {
            var matrix = new TriStateMatrix(byteMatrix.Width);

            for (int i = 0; i < byteMatrix.Width; i++)
            {
                for (int j = 0; j < byteMatrix.Height; j++)
                {
                    if (byteMatrix[j, i] != -1)
                    {
                        matrix[i, j, MatrixStatus.Data] = byteMatrix[j, i] != 0;
                    }
                }
            }
            return(matrix);
        }
        internal static TriStateMatrix ToBitMatrix(this Common.ByteMatrix byteMatrix)
        {
            TriStateMatrix matrix = new TriStateMatrix(byteMatrix.Width);

            for (int i = 0; i < byteMatrix.Width; i++)
            {
                for (int j = 0; j < byteMatrix.Height; j++)
                {
                    if (byteMatrix[j, i] != -1)
                    {
                        matrix[i, j, MatrixStatus.NoMask] = byteMatrix[j, i] != 0;
                    }
                }
            }
            return(matrix);
        }
 private static TriStateMatrix FromGraphic(string[] lines)
 {
     var matrix = new TriStateMatrix(lines.Length);
     for (int j = 0; j < matrix.Width; j++)
     {
         for (int i = 0; i < matrix.Width; i++)
         {
             if (lines[j][i]==s_0Char)
             {
                 matrix[i, j, MatrixStatus.NoMask] = false;
             }
             else if (lines[j][i]==s_1Char)
             {
                 matrix[i, j, MatrixStatus.NoMask] = true;
             }
         }
     }
     return matrix;
 }
Esempio n. 39
0
        private static TriStateMatrix FromGraphic(string[] lines)
        {
            var matrix = new TriStateMatrix(lines.Length);

            for (int j = 0; j < matrix.Width; j++)
            {
                for (int i = 0; i < matrix.Width; i++)
                {
                    if (lines[j][i] == s_0Char)
                    {
                        matrix[i, j, MatrixStatus.NoMask] = false;
                    }
                    else if (lines[j][i] == s_1Char)
                    {
                        matrix[i, j, MatrixStatus.NoMask] = true;
                    }
                }
            }
            return(matrix);
        }
Esempio n. 40
0
 internal static BitMatrix GetLowestPenaltyMatrix(this TriStateMatrix matrix, ErrorCorrectionLevel errorlevel)
 {
     PatternFactory patternFactory = new PatternFactory();
     int score = int.MaxValue;
     int tempScore;
     TriStateMatrix result = new TriStateMatrix(matrix.Width);
     TriStateMatrix triMatrix;
     foreach(Pattern pattern in patternFactory.AllPatterns())
     {
     	triMatrix = matrix.Apply(pattern, errorlevel);
     	tempScore = triMatrix.PenaltyScore();
     	if(tempScore < score)
     	{
     		score = tempScore;
     		result = triMatrix;
     	}
     }
     
     return result;
 }
Esempio n. 41
0
 /// <summary>
 /// Get Qr BitMatrix as two dimentional bool array.
 /// </summary>
 /// <returns>null if matrix is null, else full matrix</returns>
 public BitMatrix GetQrMatrix()
 {
     if (m_QrCode.Matrix == null)
     {
         return(null);
     }
     else
     {
         BitMatrix      matrix = m_QrCode.Matrix;
         TriStateMatrix clone  = new TriStateMatrix(matrix.Width);
         for (int x = 0; x < matrix.Width; x++)
         {
             for (int y = 0; y < matrix.Width; y++)
             {
                 clone[x, y, MatrixStatus.NoMask] = matrix[x, y];
             }
         }
         return(clone);
     }
 }
Esempio n. 42
0
        internal static BitMatrix GetLowestPenaltyMatrix(this TriStateMatrix matrix, ErrorCorrectionLevel errorLevel)
        {
            PatternFactory patternFactory = new();
            int            score          = int.MaxValue;
            int            tempScore;
            TriStateMatrix result = new(matrix.Width);
            TriStateMatrix triMatrix;

            foreach (Pattern pattern in patternFactory.AllPatterns())
            {
                triMatrix = matrix.Apply(pattern, errorLevel);
                tempScore = triMatrix.PenaltyScore();
                if (tempScore < score)
                {
                    score  = tempScore;
                    result = triMatrix;
                }
            }

            return(result);
        }
Esempio n. 43
0
 public static void ToGraphic(this TriStateMatrix matrix, TextWriter output)
 {
     output.WriteLine(matrix.Width.ToString());
     for (int j = 0; j < matrix.Width; j++)
     {
         for (int i = 0; i < matrix.Width; i++)
         {
             char charToPrint;
             if (matrix.MStatus(i, j) == MatrixStatus.None)
             {
                 charToPrint = s_EmptyChar;
             }
             else
             {
                 charToPrint = matrix[i, j] ? s_1Char : s_0Char;
             }
             output.Write(charToPrint);
         }
         output.WriteLine();
     }
 }
Esempio n. 44
0
        public override void ApplyTo(TriStateMatrix matrix)
        {
            // -8 is for skipping position detection patterns (size 7), and two horizontal/vertical
            // separation patterns (size 1). Thus, 8 = 7 + 1.
            for (int i = 8; i < matrix.Width - 8; ++i)
            {
                bool value = (sbyte)((i + 1) % 2) == 1;
                // Horizontal line.

                if (matrix.MStatus(6, i) == MatrixStatus.None)
                {
                    matrix[6, i, MatrixStatus.NoMask] = value;
                }

                // Vertical line.
                if (matrix.MStatus(i, 6) == MatrixStatus.None)
                {
                    matrix[i, 6, MatrixStatus.NoMask] = value;
                }
            }
        }
Esempio n. 45
0
        public override void ApplyTo(TriStateMatrix matrix)
        {
            // -8 is for skipping position detection patterns (size 7), and two horizontal/vertical
            // separation patterns (size 1). Thus, 8 = 7 + 1.
            for (int i = 8; i < matrix.Width - 8; ++i)
            {
                bool value = (sbyte)((i + 1) % 2) == 1;
                // Horizontal line.

                if (matrix.MStatus(6, i) == MatrixStatus.None)
                {
                    matrix[6, i, MatrixStatus.NoMask] = value;
                }

                // Vertical line.
                if (matrix.MStatus(i, 6) == MatrixStatus.None)
                {
                    matrix[i, 6, MatrixStatus.NoMask] = value;
                }
            }
        }
Esempio n. 46
0
        /// <summary>
        /// Embed format information to tristatematrix.
        /// Process combination of create info bits, BCH error correction bits calculation, embed towards matrix.
        /// </summary>
        /// <remarks>ISO/IEC 18004:2000 Chapter 8.9 Page 53</remarks>
        internal static void EmbedFormatInformation(this TriStateMatrix triMatrix, ErrorCorrectionLevel errorLevel, Pattern pattern)
        {
            BitList formatInfo = GetFormatInfoBits(errorLevel, pattern);
            int     width      = triMatrix.Width;

            for (int index = 0; index < 15; index++)
            {
                MatrixPoint point = PointForInfo1(index);
                bool        bit   = formatInfo[index];
                triMatrix[point.X, point.Y, MatrixStatus.NoMask] = bit;

                if (index < 7)
                {
                    triMatrix[8, width - 1 - index, MatrixStatus.NoMask] = bit;
                }
                else
                {
                    triMatrix[width - 8 + (index - 7), 8, MatrixStatus.NoMask] = bit;
                }
            }
        }
Esempio n. 47
0
        private void Encode(string content, int option)
        {
            ErrorCorrectionLevel errorLevel = ErrorCorrectionLevel.H;

            EncodationStruct encodeStruct = DataEncode.Encode(content, errorLevel);

            BitList codewords = ECGenerator.FillECCodewords(encodeStruct.DataCodewords, encodeStruct.VersionDetail);

            if (option == 3)
            {
                return;
            }

            TriStateMatrix triMatrix = new TriStateMatrix(encodeStruct.VersionDetail.MatrixWidth);

            PositioninngPatternBuilder.EmbedBasicPatterns(encodeStruct.VersionDetail.Version, triMatrix);

            triMatrix.EmbedVersionInformation(encodeStruct.VersionDetail.Version);
            triMatrix.EmbedFormatInformation(errorLevel, new Pattern0());
            triMatrix.TryEmbedCodewords(codewords);

            triMatrix.GetLowestPenaltyMatrix(errorLevel);
        }
Esempio n. 48
0
        private static TriStateMatrix FromBytes(byte[] bytes)
        {
            int length = BitConverter.ToInt32(bytes, 0);
            byte[] data = new byte[(bytes.Length - sizeof(int)) * sizeof(byte)];
            Array.Copy(bytes, sizeof(int), data, 0, data.Length);

            int width = (int)Math.Sqrt(length);
            if (width * width != length)
            {
                throw new ArgumentException(string.Format("The header containsinvalid length value. Length must be a square of width.{0}", length), "bytes");
            }

            BitArray bitArray = new BitArray(data);
            TriStateMatrix matrix = new TriStateMatrix(width);
            for (int index = 0; index < length; index++)
            {
                int i = index % width;
                int j = index / width;

                matrix[i, j, MatrixStatus.Data] = bitArray[index];
            }
            return matrix;
        }
        public static void AssertEquals(this TriStateMatrix expected, TriStateMatrix actual)
        {
            if (expected == null)
            {
                throw new ArgumentNullException("expected");
            }
            if (actual == null)
            {
                throw new ArgumentNullException("actual");
            }

            if (expected.Width != actual.Width)
            {
                Assert.Fail("Mtrix must have same size. Expected {0}, Actual {1}", expected.Width, actual.Width);
            }


            for (int i = 0; i < expected.Width; i++)
            {
                for (int j = 0; j < expected.Width; j++)
                {
                    if (expected.MStatus(i, j) != MatrixStatus.None && actual.MStatus(i, j) != MatrixStatus.None && expected[i, j] != actual[i, j])
                    {
                        Assert.Fail("Matrces are different.\nExpected:{0}Actual:{1}.", TriStateMatrixToGraphicExtensions.ToGraphicString(expected), TriStateMatrixToGraphicExtensions.ToGraphicString(actual));
                    }

                    if (expected.MStatus(i, j) == MatrixStatus.None && actual.MStatus(i, j) != MatrixStatus.None)
                    {
                        Assert.Fail("Matrces are different.\nExpected:{0}Actual:{1}.", TriStateMatrixToGraphicExtensions.ToGraphicString(expected), TriStateMatrixToGraphicExtensions.ToGraphicString(actual));
                    }
                    if (expected.MStatus(i, j) != MatrixStatus.None && actual.MStatus(i, j) == MatrixStatus.None)
                    {
                        Assert.Fail("Matrces are different.\nExpected:{0}Actual:{1}.", TriStateMatrixToGraphicExtensions.ToGraphicString(expected), TriStateMatrixToGraphicExtensions.ToGraphicString(actual));
                    }
                }
            }
        }
Esempio n. 50
0
 public void Test_against_reference_implementation(int version, MaskPatternType patternType, TriStateMatrix expected)
 {
 	Test_One_Case(version, patternType,  expected);
 }
Esempio n. 51
0
 public void Test_against_DataSet(int version, MaskPatternType patternType, TriStateMatrix expected)
 {
 	Test_One_Case(version, patternType, expected);
 }
Esempio n. 52
0
 /// <summary>
 /// Copies to.
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="targetPoint">The target point.</param>
 /// <param name="mstatus">The mstatus.</param>
 /// <remarks></remarks>
 internal void CopyTo(TriStateMatrix target, MatrixPoint targetPoint, MatrixStatus mstatus)
 {
     CopyTo(target, new MatrixRectangle(new MatrixPoint(0, 0), new MatrixSize(Width, Height)), targetPoint,
            mstatus);
 }
Esempio n. 53
0
 public override void ApplyTo(TriStateMatrix matrix)
 {
     matrix[8, matrix.Width - 8, MatrixStatus.NoMask] = true;
 }
Esempio n. 54
0
 public void Test_against_DataSet(int version, TriStateMatrix expected, IEnumerable<bool> codewords)
 {
 	Test_One_Case(version, expected, codewords);
 }
Esempio n. 55
0
 public void Test_against_reference_implementation(int version, TriStateMatrix expected, IEnumerable<bool> codewords)
 {
 	Test_One_Case(version, expected, codewords);
 }
Esempio n. 56
0
 /// <summary>
 /// Applies the specified pattern.
 /// </summary>
 /// <param name="pattern">The pattern.</param>
 /// <param name="matrix">The matrix.</param>
 /// <param name="errorlevel">The errorlevel.</param>
 /// <returns></returns>
 /// <remarks></remarks>
 public static TriStateMatrix Apply(this Pattern pattern, TriStateMatrix matrix, ErrorCorrectionLevel errorlevel)
 {
     return matrix.Xor(pattern, errorlevel);
 }
Esempio n. 57
0
		private void Encode(string content, int option)
		{
			ErrorCorrectionLevel errorLevel = ErrorCorrectionLevel.H;
			
			EncodationStruct encodeStruct = DataEncode.Encode(content, errorLevel);
			
			BitList codewords = ECGenerator.FillECCodewords(encodeStruct.DataCodewords, encodeStruct.VersionDetail);
			
			if(option == 3)
				return;
			
			TriStateMatrix triMatrix = new TriStateMatrix(encodeStruct.VersionDetail.MatrixWidth);
			PositioninngPatternBuilder.EmbedBasicPatterns(encodeStruct.VersionDetail.Version, triMatrix);
			
			triMatrix.EmbedVersionInformation(encodeStruct.VersionDetail.Version);
			triMatrix.EmbedFormatInformation(errorLevel, new Pattern0());
			triMatrix.TryEmbedCodewords(codewords);
			
			triMatrix.GetLowestPenaltyMatrix(errorLevel);
		}
Esempio n. 58
0
 public abstract void ApplyTo(TriStateMatrix matrix);
Esempio n. 59
0
 /// <summary>
 /// Get Qr BitMatrix as two dimentional bool array.
 /// </summary>
 /// <returns>null if matrix is null, else full matrix</returns>
 public BitMatrix GetQrMatrix()
 {
     if (m_QrCode.Matrix == null)
         return null;
     else
     {
         BitMatrix matrix = m_QrCode.Matrix;
         TriStateMatrix clone = new TriStateMatrix(matrix.Width);
         for (int x = 0; x < matrix.Width; x++)
         {
             for (int y = 0; y < matrix.Width; y++)
             {
                 clone[x, y, MatrixStatus.NoMask] = matrix[x, y];
             }
         }
         return clone;
     }
 }
Esempio n. 60
0
 public IEnumerable<MatrixPoint> GetNonColidingCoordinatePairs(TriStateMatrix matrix)
 {
     return
         GetAllCoordinatePairs()
             .Where(point => matrix.MStatus(point.Offset(2, 2)) == MatrixStatus.None);
 }