예제 #1
0
        public void ReadBytesTest()
        {
            int bufferLength = 10;
            int chunkSize    = 2;

            byte[] input  = new byte[bufferLength];
            byte[] output = new byte[bufferLength];
            byte[] chunk  = new byte[chunkSize];

            for (int index = 0; index < bufferLength; index++)
            {
                input[index]  = (byte)(index + 10);
                output[index] = 0xff;
            }

            WangStream wangStream = new WangStream(input);
            int        read       = 0;

            while (wangStream.AvailableBytes() != 0)
            {
                Assert.IsFalse(wangStream.IsEnd());
                wangStream.ReadBytes(chunk, chunkSize);
                Array.Copy(chunk, 0, output, read, chunkSize);
                read += chunkSize;
            }

            Assert.IsTrue(wangStream.IsEnd());
            Assert.AreEqual(0, wangStream.AvailableBytes());
            Assert.AreEqual(bufferLength, output.Length);
            for (int index = 0; index < bufferLength; index++)
            {
                Assert.AreEqual(input[index], output[index]);
            }
        }
예제 #2
0
        public void ReadInt32Test()
        {
            byte[] input = new byte[4] {
                0xff, 0xFF, 0xff, 0xFF
            };

            WangStream wangStream = new WangStream(input);

            Assert.IsFalse(wangStream.IsEnd());
            Assert.AreEqual(-1, wangStream.ReadInt32());
            Assert.AreEqual(0, wangStream.AvailableBytes());
            Assert.IsTrue(wangStream.IsEnd());
            Assert.AreEqual(0, wangStream.AvailableBytes());
        }
예제 #3
0
        public void SkipBytesTest()
        {
            byte[] input = new byte[4] {
                1, 2, 3, 4
            };
            WangStream wangStream = new WangStream(input);

            Assert.IsFalse(wangStream.IsEnd());
            wangStream.SkipBytes(2);
            Assert.IsFalse(wangStream.IsEnd());
            Assert.AreEqual(2, wangStream.AvailableBytes());
            Assert.AreEqual(3, wangStream.ReadByte());
            Assert.IsFalse(wangStream.IsEnd());
            Assert.AreEqual(1, wangStream.AvailableBytes());
        }
예제 #4
0
        public void ReadRectangleStd()
        {
            // Well formed data
            byte[] streamData =
            {
                // Left
                100, 0, 0, 0,
                // Top
                1,   0, 0, 0,
                // Right
                200, 0, 0, 0,
                // Bottom
                55,  0, 0, 0,
            };

            WangStream stream = new WangStream(streamData);

            int[] coordinates = new int[4];
            Assert.IsTrue(WangAnnotationStructureReader.ReadRectangle(coordinates, stream));

            Assert.AreEqual(100, coordinates[WangAnnotationTranslation.LeftIndex]);
            Assert.AreEqual(1, coordinates[WangAnnotationTranslation.TopIndex]);
            Assert.AreEqual(200, coordinates[WangAnnotationTranslation.RightIndex]);
            Assert.AreEqual(55, coordinates[WangAnnotationTranslation.BottomIndex]);
            Assert.AreEqual(0, stream.AvailableBytes());
        }
        public void ReadBlockOiDib()
        {
            const int headerSize = 12;

            // Stream with a named block header and some bytes for the dib
            byte[] streamData =
            {
                // Named block header
                79, 105, 68, 73, 66, 0, 0, 0, 4, 0, 0, 0,
                // Dummy dib
                0,    1,  2,  3,
                // Extra bytes
                1,    2,  3, 4
            };

            WangAnnotationProperties properties = new WangAnnotationProperties();
            WangStream stream = new WangStream(streamData);

            Assert.IsTrue(WangAnnotationsReader.ReadBlock(properties, stream, headerSize));
            Assert.AreEqual(4, stream.AvailableBytes());
            Assert.IsTrue(properties.HasDib);
            Assert.AreEqual(4, properties.DibInfo.Length);
            for (int index = 0; index < 4; index++)
            {
                Assert.AreEqual((byte)index, properties.DibInfo[index]);
            }
        }
        public void ReadBlockOiFilNam()
        {
            const int headerSize = 12;

            // Stream with a named block header and a file name
            byte[] streamData =
            {
                // Named block header
                79,  105,  70, 105, 108,  78,  97, 109,
                61,    0,   0,   0,
                // Character string for filename
                67,   58,  92,  68, 111,  99, 117,
                109, 101, 110, 116, 115,  32,  97,
                110, 100,  32,  83, 101, 116, 116,
                105, 110, 103, 115,  92,  65, 100,
                109, 105, 110,  92,  68, 101, 115,
                107, 116, 111, 112,  92,  87,  65,
                78,   71,  92, 115, 111, 117, 114,
                99,  101,  92,  48,  48,  49,  46,
                98,  109, 112,   0,   0,
                // Extra bytes
                1,     2,   3, 4
            };

            WangAnnotationProperties properties = new WangAnnotationProperties();
            WangStream stream = new WangStream(streamData);

            Assert.IsTrue(WangAnnotationsReader.ReadBlock(properties, stream, headerSize));
            Assert.AreEqual(4, stream.AvailableBytes());
            Assert.AreEqual("C:\\Documents and Settings\\Admin\\Desktop\\WANG\\source\\001.bmp", properties.Filename);
        }
예제 #7
0
        public void ReadRotationStdExactDifferentResolution()
        {
            byte[] streamData =
            {
                1,   0, 0, 0,
                232, 3, 0, 0,
                200, 0, 0, 0,
                200, 0, 0, 0,
                200, 0, 0, 0,
                145, 0, 0, 0,
                0,   0, 0, 0,
                0,   0, 0, 0,
                1,   0, 0, 0,
                0,   0, 0, 0,
                0,   0, 0, 0,
                0,   0, 0, 0,
                0,   0, 0, 0,
                0,   0, 0, 0
            };

            WangStream   stream = new WangStream(streamData);
            WangRotation read   = WangAnnotationStructureReader.ReadRotation(stream, 56);

            Assert.IsTrue(read != null);

            Assert.AreEqual(200, read.OrigHRes);
            Assert.AreEqual(145, read.OrigVRes);
            Assert.AreEqual(1, read.Rotation);
            Assert.AreEqual(0, stream.AvailableBytes());
        }
        public void ReadBlockOiAnText()
        {
            const int headerSize = 12;

            // Stream with a named block header and a display text
            byte[] streamData =
            {
                // Named block header
                79,  105,  65, 110, 84, 101, 120, 116,
                24,    0,   0,   0,
                // Display text
                0,     0,   0,   0,
                242,   2,   0,   0,
                104,   1,   0,   0,
                5,     0,   0,   0,
                72,  101, 108, 108,
                111,   0,   0,   0,
                // Extra bytes
                1,     2,   3, 4
            };

            WangAnnotationProperties properties = new WangAnnotationProperties();
            WangStream stream = new WangStream(streamData);

            Assert.IsTrue(WangAnnotationsReader.ReadBlock(properties, stream, headerSize));
            Assert.AreEqual(4, stream.AvailableBytes());
            Assert.AreEqual((uint)360, properties.DisplayText.CreationScale);
            Assert.AreEqual(0, properties.DisplayText.Orientation);
            Assert.AreEqual("Hello", properties.DisplayText.Text);
        }
        public void ReadBlockOiHypLnk()
        {
            const int headerSize = 12;

            // Stream with a named block header and a display text
            byte[] streamData =
            {
                // Named block header
                79,  105,  72, 121, 112, 76, 110, 107, 35, 0, 0, 0,
                // Hyperlink
                1,     0,   0,   0,
                14,    0,   0,   0,
                115, 111, 117, 114,
                99,  101,  92,  48,
                48,   49,  46, 106,
                112, 103,   1,   0,
                0,     0,  49,   0,
                0,     0,   0,   0,
                0,     0,   0,
                // Extra bytes
                1,     2,   3, 4
            };

            WangAnnotationProperties properties = new WangAnnotationProperties();
            WangStream stream = new WangStream(streamData);

            Assert.IsTrue(WangAnnotationsReader.ReadBlock(properties, stream, headerSize));
            Assert.AreEqual(4, stream.AvailableBytes());
            Assert.AreEqual(false, properties.Hyperlink.CanRemoveHyperlink);
            Assert.AreEqual(false, properties.Hyperlink.InternalLink);
            Assert.AreEqual("source\\001.jpg", properties.Hyperlink.Link);
            Assert.AreEqual("1", properties.Hyperlink.Location);
            Assert.AreEqual("", properties.Hyperlink.WorkingDirectory);
        }
예제 #10
0
        public void ReadRgbQuadStd()
        {
            // Well formed data
            byte[] streamData =
            {
                // Blue
                1,
                // Green
                2,
                // Red
                3,
                // Reserved
                0
            };

            WangStream stream = new WangStream(streamData);

            byte[] components = new byte[3];
            Assert.IsTrue(WangAnnotationStructureReader.ReadRgbQuad(components, stream));

            Assert.AreEqual(1, components[WangAnnotationTranslation.BlueIndex]);
            Assert.AreEqual(2, components[WangAnnotationTranslation.GreenIndex]);
            Assert.AreEqual(3, components[WangAnnotationTranslation.RedIndex]);
            Assert.AreEqual(0, stream.AvailableBytes());
        }
예제 #11
0
        public void ReadByteTest()
        {
            byte[] input = new byte[3] {
                1, 2, 3
            };

            WangStream wangStream = new WangStream(input);

            for (int index = 0; index < input.Length; index++)
            {
                Assert.IsFalse(wangStream.IsEnd());
                Assert.AreEqual(input.Length - index, wangStream.AvailableBytes());
                Assert.AreEqual(input[index], wangStream.ReadByte());
            }
            Assert.IsTrue(wangStream.IsEnd());
            Assert.AreEqual(0, wangStream.AvailableBytes());
        }
예제 #12
0
        public void ReadCharStringStdShort()
        {
            // The buffer contains the string "OiGroup" and trailing null bytes.
            byte[] streamData =
            {
                79, 105, 71, 114, 111, 117, 112, 0, 0, 0, 0, 0, 0
            };

            WangStream stream = new WangStream(streamData);
            string     read   = WangAnnotationStructureReader.ReadCharString(stream, streamData.Length);

            Assert.AreEqual("OiGroup", read);
            Assert.AreEqual(0, stream.AvailableBytes());
        }
예제 #13
0
        public void ReadCharStringReadOversize()
        {
            // The buffer contains the string "OiGroup" and no null bytes.
            byte[] streamData =
            {
                79, 105, 71, 114, 111, 117, 112, 112
            };

            WangStream stream = new WangStream(streamData);
            string     read   = WangAnnotationStructureReader.ReadCharString(stream, 7);

            Assert.AreEqual("OiGroup", read);
            Assert.AreEqual(1, stream.AvailableBytes());
        }
예제 #14
0
        public void ReadNamedBlockHeaderStd32()
        {
            // Well formed data with an extra last byte
            byte[] streamData =
            {
                79, 105, 71, 114, 111, 117, 112, 0, 11, 0, 0, 0, 91
            };

            WangStream           stream = new WangStream(streamData);
            WangNamedBlockHeader read   = WangAnnotationStructureReader.ReadNamedBlockHeader(stream, 12);

            Assert.IsTrue(read != null);

            Assert.AreEqual("OiGroup", read.Name);
            Assert.AreEqual(11, read.Size);
            Assert.AreEqual(1, stream.AvailableBytes());
        }
예제 #15
0
        public void ReadPointsStdShort()
        {
            // Well formed data with 4 extra last byte
            byte[] streamData =
            {
                // max is 2
                2,   0, 0, 0,
                // count is 2
                2,   0, 0, 0,
                // x0 = 0
                0,   0, 0, 0,
                // y0 = 0
                0,   0, 0, 0,
                // x1 = 729
                217, 2, 0, 0,
                // y1 = 446
                190, 1, 0, 0,
                // extra
                1,   2, 3, 4
            };

            int expectedPointsCount = 2;

            int[] expectedPoints =
            {
                // X0, Y0
                0,   0,
                // X1, Y1
                729, 446
            };

            WangStream stream = new WangStream(streamData);

            int[] read = WangAnnotationStructureReader.ReadPoints(stream, 24);
            Assert.IsTrue(read != null);

            Assert.AreEqual(2, WangAnnotationTranslation.PointsLength(read));
            Assert.AreEqual(4, stream.AvailableBytes());

            for (int index = 0; index < expectedPointsCount; index++)
            {
                Assert.AreEqual(expectedPoints[index * 2], WangAnnotationTranslation.PointX(index, read));
                Assert.AreEqual(expectedPoints[index * 2 + 1], WangAnnotationTranslation.PointY(index, read));
            }
        }
예제 #16
0
        public void ReadDibStd()
        {
            // Raw buffer not interpreted by the method.
            byte[] streamData =
            {
                1, 2, 3, 4, 5, 6
            };

            WangStream stream = new WangStream(streamData);

            byte[] read = WangAnnotationStructureReader.ReadDib(stream, streamData.Length);
            Assert.AreEqual(0, stream.AvailableBytes());
            Assert.AreEqual(streamData.Length, read.Length);
            for (int index = 0; index < streamData.Length; index++)
            {
                Assert.AreEqual(streamData[index], read[index]);
            }
        }
        public void ReadHeaderStd()
        {
            Random random = new Random(10);

            for (int dataSize = 8; dataSize < 16; dataSize++)
            {
                byte[] streamData = new byte[dataSize];
                for (int index = 0; index < dataSize; index++)
                {
                    streamData[index] = (byte)random.Next();
                }

                WangStream stream = new WangStream(streamData);
                Assert.IsTrue(WangAnnotationsReader.ReadHeader(stream));
                Assert.IsTrue(stream.AvailableBytes() == dataSize - 8);
            }
            ;
        }
예제 #18
0
        public void ReadPointsStdGreaterMax()
        {
            // Well formed data
            byte[] streamData =
            {
                // max is 2
                2,   0, 0, 0,
                // count is 1
                1,   0, 0, 0,
                // x0 = 729
                217, 2, 0, 0,
                // y0 = 446
                190, 1, 0, 0,
                // x1 = 0
                0,   0, 0, 0,
                // y1 = 0
                0,   0, 0, 0,
            };

            int expectedPointsCount = 1;

            int[] expectedPoints =
            {
                // X1, Y1
                729, 446
            };

            WangStream stream = new WangStream(streamData);

            int[] read = WangAnnotationStructureReader.ReadPoints(stream, 24);
            Assert.IsTrue(read != null);

            Assert.AreEqual(1, WangAnnotationTranslation.PointsLength(read));
            Assert.AreEqual(0, stream.AvailableBytes());

            for (int index = 0; index < expectedPointsCount; index++)
            {
                Assert.AreEqual(expectedPoints[index * 2], WangAnnotationTranslation.PointX(index, read));
                Assert.AreEqual(expectedPoints[index * 2 + 1], WangAnnotationTranslation.PointY(index, read));
            }
        }
예제 #19
0
        public void ReadDisplayTextStdExact()
        {
            // Well formed stream data
            byte[] streamData =
            {
                0,     0,   0,   0,
                123,   1,   0,   0,
                104,   1,   0,   0,
                5,     0,   0,   0,
                72,  101, 108, 108,111, 0, 0, 0
            };

            WangStream      stream = new WangStream(streamData);
            WangDisplayText read   = WangAnnotationStructureReader.ReadDisplayText(stream, 24);

            Assert.IsTrue(read != null);

            Assert.AreEqual(0, read.Orientation);
            Assert.AreEqual("Hello", read.Text);
            Assert.AreEqual(360, (int)read.CreationScale);
            Assert.AreEqual(0, stream.AvailableBytes());
        }
        public void ReadBlockOiAnTextReadFailure()
        {
            const int headerSize = 12;

            // Stream with a named block header and a display text too short
            byte[] streamData =
            {
                // Named block header
                79,  105, 65, 110, 84, 101, 120, 116,
                12,    0,  0,   0,
                // Too short display text
                0,     0,  0,   0,
                242,   2,  0,   0,
                104,   1,  0, 0
            };

            WangAnnotationProperties properties = new WangAnnotationProperties();
            WangStream stream = new WangStream(streamData);

            Assert.IsFalse(WangAnnotationsReader.ReadBlock(properties, stream, headerSize));
            // Only the header should have been read as the rest is not there
            Assert.AreEqual(streamData.Length - 12, stream.AvailableBytes());
        }
        public void ReadDataTypeInvalid()
        {
            int[] invalidDataTypes =
            {
                0, 1, 3, 4, 7, 8, 9, 10
            };

            foreach (var expectedDataType in invalidDataTypes)
            {
                // Valid stream data but invalid data type
                byte[] streamData = new byte[8];
                Array.Copy(BitConverter.GetBytes((int)expectedDataType), streamData, 4);
                Array.Copy(BitConverter.GetBytes((int)8), 0, streamData, 4, 4);

                WangStream stream = new WangStream(streamData);
                WangAnnotationsReader.WangDataType dataType;
                int dataSize;
                Assert.IsTrue(WangAnnotationsReader.ReadDataType(out dataType, out dataSize, stream));
                Assert.AreEqual(WangAnnotationsReader.WangDataType.Invalid, dataType);
                Assert.AreEqual(8, dataSize);
                Assert.AreEqual(0, stream.AvailableBytes());
            }
        }
        public void ReadDataTypeStd()
        {
            // The int values for the data types which will be tested.
            int[] intTypes =
            {
                2, 5, 6
            };
            // The data types which will be tested, aligned with the corresponding entry
            // in the intTypes array.
            WangAnnotationsReader.WangDataType[] dataTypes =
            {
                WangAnnotationsReader.WangDataType.DefaultNamedBlock, WangAnnotationsReader.WangDataType.Attributes,
                WangAnnotationsReader.WangDataType.NamedBlock
            };

            // The test runs with different data size. At least 8 bytes
            // are required for the stream to be valid.
            for (int expectedDataSize = 8; expectedDataSize < 40; expectedDataSize++)
            {
                for (int dataTypeIndex = 0; dataTypeIndex < intTypes.Length; dataTypeIndex++)
                {
                    // Valid stream data
                    byte[] streamData = new byte[expectedDataSize];
                    Array.Copy(BitConverter.GetBytes(intTypes[dataTypeIndex]), streamData, 4);
                    Array.Copy(BitConverter.GetBytes((int)expectedDataSize), 0, streamData, 4, 4);

                    WangStream stream = new WangStream(streamData);
                    WangAnnotationsReader.WangDataType dataType;
                    int dataSize;
                    Assert.IsTrue(WangAnnotationsReader.ReadDataType(out dataType, out dataSize, stream));
                    Assert.AreEqual(expectedDataSize, dataSize);
                    Assert.AreEqual(dataTypes[dataTypeIndex], dataType);
                    Assert.AreEqual(expectedDataSize - 8, stream.AvailableBytes());
                }
            }
        }
        public void ReadBlockOiIndex()
        {
            const int headerSize = 12;

            // Stream with a named block header and a string for the index
            byte[] streamData =
            {
                // Named block header
                79,  105,  73, 110, 100, 101, 120, 0,
                11,    0,   0,   0,
                // Character string for filename
                67,   58,  92,  68, 111,  99, 117,
                109, 101, 110,   0,
                // Extra bytes
                1,     2,   3, 4
            };

            WangAnnotationProperties properties = new WangAnnotationProperties();
            WangStream stream = new WangStream(streamData);

            Assert.IsTrue(WangAnnotationsReader.ReadBlock(properties, stream, headerSize));
            Assert.AreEqual(4, stream.AvailableBytes());
            Assert.AreEqual("C:\\Documen", properties.OiIndex);
        }