Пример #1
0
 public void testApplyMaskPenaltyRule1()
 {
     {
         ByteMatrix matrix = new ByteMatrix(4, 1);
         matrix.set(0, 0, 0);
         matrix.set(1, 0, 0);
         matrix.set(2, 0, 0);
         matrix.set(3, 0, 0);
         Assert.AreEqual(0, MaskUtil.applyMaskPenaltyRule1(matrix));
     }
     { // Horizontal.
         ByteMatrix matrix = new ByteMatrix(6, 1);
         matrix.set(0, 0, 0);
         matrix.set(1, 0, 0);
         matrix.set(2, 0, 0);
         matrix.set(3, 0, 0);
         matrix.set(4, 0, 0);
         matrix.set(5, 0, 1);
         Assert.AreEqual(3, MaskUtil.applyMaskPenaltyRule1(matrix));
         matrix.set(5, 0, 0);
         Assert.AreEqual(4, MaskUtil.applyMaskPenaltyRule1(matrix));
     }
     { // Vertical.
         ByteMatrix matrix = new ByteMatrix(1, 6);
         matrix.set(0, 0, 0);
         matrix.set(0, 1, 0);
         matrix.set(0, 2, 0);
         matrix.set(0, 3, 0);
         matrix.set(0, 4, 0);
         matrix.set(0, 5, 1);
         Assert.AreEqual(3, MaskUtil.applyMaskPenaltyRule1(matrix));
         matrix.set(0, 5, 0);
         Assert.AreEqual(4, MaskUtil.applyMaskPenaltyRule1(matrix));
     }
 }
Пример #2
0
 public void testApplyMaskPenaltyRule4()
 {
     {
         // Dark cell ratio = 0%
         ByteMatrix matrix = new ByteMatrix(1, 1);
         matrix.set(0, 0, 0);
         Assert.AreEqual(100, MaskUtil.applyMaskPenaltyRule4(matrix));
     }
     {
         // Dark cell ratio = 5%
         ByteMatrix matrix = new ByteMatrix(2, 1);
         matrix.set(0, 0, 0);
         matrix.set(0, 0, 1);
         Assert.AreEqual(0, MaskUtil.applyMaskPenaltyRule4(matrix));
     }
     {
         // Dark cell ratio = 66.67%
         ByteMatrix matrix = new ByteMatrix(6, 1);
         matrix.set(0, 0, 0);
         matrix.set(1, 0, 1);
         matrix.set(2, 0, 1);
         matrix.set(3, 0, 1);
         matrix.set(4, 0, 1);
         matrix.set(5, 0, 0);
         Assert.AreEqual(30, MaskUtil.applyMaskPenaltyRule4(matrix));
     }
 }
Пример #3
0
        public void testApplyMaskPenaltyRule2()
        {
            var matrix = new ByteMatrix(1, 1);

            matrix.set(0, 0, 0);
            Assert.AreEqual(0, MaskUtil.applyMaskPenaltyRule2(matrix));
            matrix = new ByteMatrix(2, 2);
            matrix.set(0, 0, 0);
            matrix.set(1, 0, 0);
            matrix.set(0, 1, 0);
            matrix.set(1, 1, 1);
            Assert.AreEqual(0, MaskUtil.applyMaskPenaltyRule2(matrix));
            matrix = new ByteMatrix(2, 2);
            matrix.set(0, 0, 0);
            matrix.set(1, 0, 0);
            matrix.set(0, 1, 0);
            matrix.set(1, 1, 0);
            Assert.AreEqual(3, MaskUtil.applyMaskPenaltyRule2(matrix));
            matrix = new ByteMatrix(3, 3);
            matrix.set(0, 0, 0);
            matrix.set(1, 0, 0);
            matrix.set(2, 0, 0);
            matrix.set(0, 1, 0);
            matrix.set(1, 1, 0);
            matrix.set(2, 1, 0);
            matrix.set(0, 2, 0);
            matrix.set(1, 2, 0);
            matrix.set(2, 2, 0);
            // Four instances of 2x2 blocks.
            Assert.AreEqual(3 * 4, MaskUtil.applyMaskPenaltyRule2(matrix));
        }
        // 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.
        public static void EmbedDataBits(BitVector dataBits, int maskPattern, ByteMatrix matrix)
        {
            var bitIndex  = 0;
            var direction = -1;
            // Start from the right bottom cell.
            var x = matrix.GetWidth() - 1;
            var y = matrix.GetHeight() - 1;

            while (x > 0)
            {
                // Skip the vertical timing pattern.
                if (x == 6)
                {
                    x -= 1;
                }
                while (y >= 0 && y < matrix.GetHeight())
                {
                    for (var i = 0; i < 2; ++i)
                    {
                        var xx = x - i;
                        // Skip the cell if it's not empty.
                        if (!IsEmpty(matrix.Get(xx, y)))
                        {
                            continue;
                        }
                        int bit;
                        if (bitIndex < dataBits.Size())
                        {
                            bit = dataBits.At(bitIndex);
                            ++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.Set(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());
            }
        }
Пример #5
0
        public void testApplyMaskPenaltyRule3()
        {
            // Horizontal 00001011101.
            var matrix = new ByteMatrix(11, 1);

            matrix.set(0, 0, 0);
            matrix.set(1, 0, 0);
            matrix.set(2, 0, 0);
            matrix.set(3, 0, 0);
            matrix.set(4, 0, 1);
            matrix.set(5, 0, 0);
            matrix.set(6, 0, 1);
            matrix.set(7, 0, 1);
            matrix.set(8, 0, 1);
            matrix.set(9, 0, 0);
            matrix.set(10, 0, 1);
            Assert.AreEqual(40, MaskUtil.applyMaskPenaltyRule3(matrix));
            // Horizontal 10111010000.
            matrix = new ByteMatrix(11, 1);
            matrix.set(0, 0, 1);
            matrix.set(1, 0, 0);
            matrix.set(2, 0, 1);
            matrix.set(3, 0, 1);
            matrix.set(4, 0, 1);
            matrix.set(5, 0, 0);
            matrix.set(6, 0, 1);
            matrix.set(7, 0, 0);
            matrix.set(8, 0, 0);
            matrix.set(9, 0, 0);
            matrix.set(10, 0, 0);
            Assert.AreEqual(40, MaskUtil.applyMaskPenaltyRule3(matrix));
            // Vertical 00001011101.
            matrix = new ByteMatrix(1, 11);
            matrix.set(0, 0, 0);
            matrix.set(0, 1, 0);
            matrix.set(0, 2, 0);
            matrix.set(0, 3, 0);
            matrix.set(0, 4, 1);
            matrix.set(0, 5, 0);
            matrix.set(0, 6, 1);
            matrix.set(0, 7, 1);
            matrix.set(0, 8, 1);
            matrix.set(0, 9, 0);
            matrix.set(0, 10, 1);
            Assert.AreEqual(40, MaskUtil.applyMaskPenaltyRule3(matrix));
            // Vertical 10111010000.
            matrix = new ByteMatrix(1, 11);
            matrix.set(0, 0, 1);
            matrix.set(0, 1, 0);
            matrix.set(0, 2, 1);
            matrix.set(0, 3, 1);
            matrix.set(0, 4, 1);
            matrix.set(0, 5, 0);
            matrix.set(0, 6, 1);
            matrix.set(0, 7, 0);
            matrix.set(0, 8, 0);
            matrix.set(0, 9, 0);
            matrix.set(0, 10, 0);
            Assert.AreEqual(40, MaskUtil.applyMaskPenaltyRule3(matrix));
        }
Пример #6
0
        public void PerformanceTest(int rules, ByteMatrix bMatrix, BitMatrix bitMatrix)
        {
            Stopwatch sw          = new Stopwatch();
            int       timesofTest = 1000;

            Penalty penalty = new PenaltyFactory().CreateByRule((PenaltyRules)rules);


            string[] timeElapsed = new string[2];

            sw.Start();

            for (int i = 0; i < timesofTest; i++)
            {
                penalty.PenaltyCalculate(bitMatrix);
            }

            sw.Stop();

            timeElapsed[0] = sw.ElapsedMilliseconds.ToString();

            sw.Reset();

            sw.Start();

            for (int i = 0; i < timesofTest; i++)
            {
                switch (rules)
                {
                case 1:
                    MaskUtil.applyMaskPenaltyRule1(bMatrix);
                    break;

                case 2:
                    MaskUtil.applyMaskPenaltyRule2(bMatrix);
                    break;

                case 3:
                    MaskUtil.applyMaskPenaltyRule3(bMatrix);
                    break;

                case 4:
                    MaskUtil.applyMaskPenaltyRule4(bMatrix);
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Unsupport Rules {0}", rules.ToString()));
                }
            }
            sw.Stop();

            timeElapsed[1] = sw.ElapsedMilliseconds.ToString();


            Assert.Pass("Terminator performance {0} Tests~ QrCode.Net: {1} ZXing: {2}", timesofTest, timeElapsed[0], timeElapsed[1]);
        }
        internal void ApplyPattern(ByteMatrix matrix, int pattern)
        {
            for (int i = 0; i < matrix.Width; i++)
            {
                for (int j = 0; j < matrix.Height; j++)
                {
                    int bit = matrix[i, j];
                    if (MaskUtil.getDataMaskBit(pattern, j, i))
                    {
                        bit ^= 0x1;
                    }

                    matrix[i, j] = (sbyte)bit;
                }
            }
        }
Пример #8
0
 private static bool TestGetDataMaskBitInternal(int maskPattern,
                                                int[][] expected)
 {
     for (int x = 0; x < 6; ++x)
     {
         for (int y = 0; y < 6; ++y)
         {
             if ((expected[y][x] == 1) !=
                 MaskUtil.getDataMaskBit(maskPattern, x, y))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Пример #9
0
        private void ApplyPattern(ByteMatrix matrix, int pattern)
        {
            for (int i = 0; i < matrix.Width; i++)
            {
                for (int j = 0; j < matrix.Height; j++)
                {
                    int bit = matrix[i, j];
                    if (MaskUtil.getDataMaskBit(pattern, j, i))
                    {
                        bit ^= 0x1;
                    }

                    matrix[i, j] = (sbyte)bit;
                }
            }
            MatrixUtil.embedTypeInfo(ErrorCorrectionLevelInternal.H, pattern, matrix);
        }
        protected virtual TestCaseData GenerateRandomTestCaseData(int matrixSize, Random randomizer, MaskPatternType pattern, PenaltyRules rules)
        {
            ByteMatrix matrix;

            BitMatrix bitmatrix = GetOriginal(matrixSize, randomizer, out matrix);

            ApplyPattern(matrix, (int)pattern);

            int expect;

            switch (rules)
            {
            case PenaltyRules.Rule01:
                expect = MaskUtil.applyMaskPenaltyRule1(matrix);
                break;

            case PenaltyRules.Rule02:
                expect = MaskUtil.applyMaskPenaltyRule2(matrix);
                break;

            case PenaltyRules.Rule03:
                expect = MaskUtil.applyMaskPenaltyRule3(matrix);
                break;

            case PenaltyRules.Rule04:
                expect = MaskUtil.applyMaskPenaltyRule4(matrix);
                break;

            default:
                throw new InvalidOperationException(string.Format("Unsupport Rules {0}", rules.ToString()));
            }


            BitMatrix input = matrix.ToBitMatrix();

            return(new TestCaseData(input, (int)rules, expect).SetName(string.Format(s_TestNameFormat, input.Width, rules.ToString(), expect)));
        }
Пример #11
0
    protected override void Execute(List <LogicEntity> entities)
    {
        foreach (LogicEntity e in entities)
        {
            if (e.isDead)
            {
                e.collider.value.mask = Mask.DEAD;

                e.collider.value.check =
                    e.collider.value.check.RemoveFlags(Mask.P1, Mask.P2, Mask.P3, Mask.P4, Mask.P5, Mask.P6, Mask.P7, Mask.P8);
            }
            else if (e.hasCollider)
            {
                if (e.hasPlayerID)
                {
                    e.collider.value.mask = MaskUtil.GetMaskForPlayerID(e.playerID.id);
                }
                else if (e.hasFollowPoint)
                {
                    e.collider.value.mask = MaskUtil.GetMaskForPlayerID(
                        Contexts.sharedInstance.logic.GetEntityWithId(e.followPoint.targetID).playerID.id
                        );
                }
                else
                {
                    e.collider.value.mask = Mask.DEFAULT;
                }

                e.collider.value.check =
                    e.collider.value.check.AddFlags(Mask.DEFAULT, Mask.P1, Mask.P2, Mask.P3, Mask.P4, Mask.P5, Mask.P6, Mask.P7, Mask.P8);

                e.collider.value.check =
                    e.collider.value.check.RemoveFlag(e.collider.value.mask);
            }
        }
    }
Пример #12
0
        public void WhenFormatMaskIsCEPThenToMaskedStringShouldReturnMaskedString(string source, string targetMaskedString)
        {
            string maskedString = MaskUtil.ToMaskedString(source, StringMask.CEP);

            Assert.AreEqual(targetMaskedString, maskedString);
        }
Пример #13
0
        public void WhenFormatMaskStringAndSourceStringAreValidThenToMaskedStringShouldReturnMaskedString(string source, string formatMask, string targetMaskedString)
        {
            string maskedString = MaskUtil.ToMaskedString(source, formatMask);

            Assert.AreEqual(targetMaskedString, maskedString);
        }
Пример #14
0
 public void WhenFormatMaskStringThenToMaskedStringShouldThrowArgumentNullException()
 {
     Assert.ThrowsException <ArgumentNullException>(() => MaskUtil.ToMaskedString("00000", null));
 }
Пример #15
0
 public void WhenFormatMaskStringAndSourceStringAreDifferentLengthThenToMaskedStringShouldThrowArgumentException()
 {
     Assert.ThrowsException <ArgumentException>(() => MaskUtil.ToMaskedString("00000", "#"));
 }
Пример #16
0
 public void WhenSourceStringIsNullThenToMaskedStringShouldThrowArgumentException()
 {
     Assert.ThrowsException <ArgumentNullException>(() => MaskUtil.ToMaskedString(null, StringMask.CNPJ));
 }