Пример #1
0
        public void ValidThreeTimesTwoLotsOfWhitespace()
        {
            const string bodyString = "P2 \n   3      2  1 \n 0   1  1  \t  1    0  0";
            var          bodyBytes  = UsAsciiEncoding.GetBytes(bodyString);

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(3, image.Header.Width);
            Assert.Equal(2, image.Header.Height);
            Assert.Equal(1, image.Header.HighestComponentValue);
            Assert.Equal(1, image.Header.Components.Count);
            Assert.Equal(Component.White, image.Header.Components[0]);
            Assert.Equal(0, image.GetNativePixel(0, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(1, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(2, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(0, 1)[0]);
            Assert.Equal(0, image.GetNativePixel(1, 1)[0]);
            Assert.Equal(0, image.GetNativePixel(2, 1)[0]);
        }
Пример #2
0
        public void ValidSixTimesSixGradient()
        {
            var          referenceRow = new byte[] { 255, 204, 153, 102, 51, 0 };
            const string bodyString   = "P2\n6 6 255\n255 204 153 102 51 0\n255 204 153 102 51 0\n255 204 153 102 51 0\n255 204 153 102 51 0\n255 204 153 102 51 0\n255 204 153 102 51 0";
            var          bodyBytes    = UsAsciiEncoding.GetBytes(bodyString);

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(6, image.Header.Width);
            Assert.Equal(6, image.Header.Height);
            Assert.Equal(255, image.Header.HighestComponentValue);
            Assert.Equal(1, image.Header.Components.Count);
            Assert.Equal(Component.White, image.Header.Components[0]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[0]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[1]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[2]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[3]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[4]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[5]);
        }
Пример #3
0
        public void SixTimesSixGradientValueOutOfRange()
        {
            var bodyRawBytes = new object[]
            {
                'P', '6',
                '\n',
                '6', ' ', '6', ' ', '2', '5', '4', '\n',
                255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0, 0, 0,
                0, 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0, 0,
                0, 0, 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0,
                255, 255, 0, 204, 204, 0, 153, 153, 0, 102, 102, 0, 51, 51, 0, 0, 0, 0,
                255, 0, 255, 204, 0, 204, 153, 0, 153, 102, 0, 102, 51, 0, 51, 0, 0, 0,
                0, 255, 255, 0, 204, 204, 0, 153, 153, 0, 102, 102, 0, 51, 51, 0, 0, 0
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var factory = new ImageFactories.Image8Factory();
            var reader  = new NetpbmReader();

            Assert.Throws <InvalidDataException>(() =>
            {
                using (var bodyStream = new MemoryStream(bodyBytes, false))
                {
                    var image = reader.ReadImage(bodyStream, factory);
                    image.LoadData();
                }
            });
        }
Пример #4
0
        public void ValidThreeTimesTwoLotsOfWhitespace()
        {
            var bodyRawBytes = new object[]
            {
                'P', '4',
                ' ', '\n', ' ', ' ', ' ',
                '3', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ',
                0x60,
                0x80
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(3, image.Header.Width);
            Assert.Equal(2, image.Header.Height);
            Assert.Equal(1, image.Header.HighestComponentValue);
            Assert.Equal(1, image.Header.Components.Count);
            Assert.Equal(Component.Black, image.Header.Components[0]);
            Assert.Equal(0, image.GetNativePixel(0, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(1, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(2, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(0, 1)[0]);
            Assert.Equal(0, image.GetNativePixel(1, 1)[0]);
            Assert.Equal(0, image.GetNativePixel(2, 1)[0]);
        }
Пример #5
0
        public void ValidTwoTimesTwoWithCommentGauntlet()
        {
            var bodyRawBytes = new object[]
            {
                'P', '5',
                '\n',
                '0', '#', 'o', 'm', 'g', '\n', '2', ' ', '2', ' ', '#', 'l', 'o', 'l', '\n', '#', ' ', 'r', 'o', 'f', 'l', '\n', '1', '\n',
                0x00, 0x01,
                0x01, 0x00
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(2, image.Header.Width);
            Assert.Equal(2, image.Header.Height);
            Assert.Equal(1, image.Header.HighestComponentValue);
            Assert.Equal(1, image.Header.Components.Count);
            Assert.Equal(Component.White, image.Header.Components[0]);
            Assert.Equal(0, image.GetNativePixel(0, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(1, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(0, 1)[0]);
            Assert.Equal(0, image.GetNativePixel(1, 1)[0]);
        }
Пример #6
0
        public void TwoImagesTwoTimesTwo()
        {
            var bodyRawBytes = new object[]
            {
                'P', '4',
                '\n',
                '2', ' ', '2', '\n',
                0x40,
                0x80,
                'P', '4',
                '\n',
                '2', ' ', '2', '\n',
                0xC0,
                0x00
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 firstImage;
            NetpbmImage8 secondImage;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                firstImage = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                firstImage.LoadData();
                secondImage = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                secondImage.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(2, firstImage.Header.Width);
            Assert.Equal(2, firstImage.Header.Height);
            Assert.Equal(1, firstImage.Header.HighestComponentValue);
            Assert.Equal(1, firstImage.Header.Components.Count);
            Assert.Equal(Component.Black, firstImage.Header.Components[0]);
            Assert.Equal(0, firstImage.GetNativePixel(0, 0)[0]);
            Assert.Equal(1, firstImage.GetNativePixel(1, 0)[0]);
            Assert.Equal(1, firstImage.GetNativePixel(0, 1)[0]);
            Assert.Equal(0, firstImage.GetNativePixel(1, 1)[0]);

            Assert.Equal(2, secondImage.Header.Width);
            Assert.Equal(2, secondImage.Header.Height);
            Assert.Equal(1, secondImage.Header.HighestComponentValue);
            Assert.Equal(1, secondImage.Header.Components.Count);
            Assert.Equal(Component.Black, secondImage.Header.Components[0]);
            Assert.Equal(1, secondImage.GetNativePixel(0, 0)[0]);
            Assert.Equal(1, secondImage.GetNativePixel(1, 0)[0]);
            Assert.Equal(0, secondImage.GetNativePixel(0, 1)[0]);
            Assert.Equal(0, secondImage.GetNativePixel(1, 1)[0]);
        }
Пример #7
0
        public void ValidSixTimesSixGradient()
        {
            var referenceRows = new[]
            {
                new byte[] { 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0, 0, 0 },
                new byte[] { 0, 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0, 0 },
                new byte[] { 0, 0, 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0 },
                new byte[] { 255, 255, 0, 204, 204, 0, 153, 153, 0, 102, 102, 0, 51, 51, 0, 0, 0, 0 },
                new byte[] { 255, 0, 255, 204, 0, 204, 153, 0, 153, 102, 0, 102, 51, 0, 51, 0, 0, 0 },
                new byte[] { 0, 255, 255, 0, 204, 204, 0, 153, 153, 0, 102, 102, 0, 51, 51, 0, 0, 0 }
            };
            var bodyRawBytes = new object[]
            {
                'P', '6',
                '\n',
                '6', ' ', '6', ' ', '2', '5', '5', '\n',
                255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0, 0, 0,
                0, 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0, 0,
                0, 0, 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0,
                255, 255, 0, 204, 204, 0, 153, 153, 0, 102, 102, 0, 51, 51, 0, 0, 0, 0,
                255, 0, 255, 204, 0, 204, 153, 0, 153, 102, 0, 102, 51, 0, 51, 0, 0, 0,
                0, 255, 255, 0, 204, 204, 0, 153, 153, 0, 102, 102, 0, 51, 51, 0, 0, 0
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(6, image.Header.Width);
            Assert.Equal(6, image.Header.Height);
            Assert.Equal(255, image.Header.HighestComponentValue);
            Assert.Equal(3, image.Header.Components.Count);
            Assert.Equal(Component.Red, image.Header.Components[0]);
            Assert.Equal(Component.Green, image.Header.Components[1]);
            Assert.Equal(Component.Blue, image.Header.Components[2]);
            Assert.Equal((IEnumerable <byte>)referenceRows[0], (IEnumerable <byte>)image.LoadedNativeRows[0]);
            Assert.Equal((IEnumerable <byte>)referenceRows[1], (IEnumerable <byte>)image.LoadedNativeRows[1]);
            Assert.Equal((IEnumerable <byte>)referenceRows[2], (IEnumerable <byte>)image.LoadedNativeRows[2]);
            Assert.Equal((IEnumerable <byte>)referenceRows[3], (IEnumerable <byte>)image.LoadedNativeRows[3]);
            Assert.Equal((IEnumerable <byte>)referenceRows[4], (IEnumerable <byte>)image.LoadedNativeRows[4]);
            Assert.Equal((IEnumerable <byte>)referenceRows[5], (IEnumerable <byte>)image.LoadedNativeRows[5]);
        }
Пример #8
0
        public void SixTimesSixGradientValueOutOfRange()
        {
            const string bodyString = "P2\n6 6 254\n255 204 153 102 51 0\n255 204 153 102 51 0\n255 204 153 102 51 0\n255 204 153 102 51 0\n255 204 153 102 51 0\n255 204 153 102 51 0";
            var          bodyBytes  = UsAsciiEncoding.GetBytes(bodyString);

            var factory = new ImageFactories.Image8Factory();
            var reader  = new NetpbmReader();

            Assert.Throws <InvalidDataException>(() =>
            {
                using (var bodyStream = new MemoryStream(bodyBytes, false))
                {
                    var image = reader.ReadImage(bodyStream, factory);
                    image.LoadData();
                }
            });
        }
Пример #9
0
        public void TwoTimesTwoTooFewPixels()
        {
            const string bodyString = "P2\n2 2 1\n0 0 1";
            var          bodyBytes  = UsAsciiEncoding.GetBytes(bodyString);

            var factory = new ImageFactories.Image8Factory();
            var reader  = new NetpbmReader();

            Assert.Throws <EndOfStreamException>(() =>
            {
                using (var bodyStream = new MemoryStream(bodyBytes, false))
                {
                    var image = reader.ReadImage(bodyStream, factory);
                    image.LoadData();
                }
            });
        }
Пример #10
0
        public void TwoTimesTwoValueBlatantlyOutOfRange()
        {
            const string bodyString = "P2\n2 2 1\n0 36893488147419103232 1 0";
            var          bodyBytes  = UsAsciiEncoding.GetBytes(bodyString);

            var factory = new ImageFactories.Image8Factory();
            var reader  = new NetpbmReader();

            Assert.Throws <InvalidDataException>(() =>
            {
                using (var bodyStream = new MemoryStream(bodyBytes, false))
                {
                    var image = reader.ReadImage(bodyStream, factory);
                    image.LoadData();
                }
            });
        }
Пример #11
0
        public void ValidSixTimesSixGrayscale()
        {
            var referenceRow = new byte[] { 255, 204, 153, 102, 51, 0 };
            var bodyRawBytes = new object[]
            {
                'P', '7',
                '\n',
                'W', 'I', 'D', 'T', 'H', ' ', '6', '\n',
                'H', 'E', 'I', 'G', 'H', 'T', ' ', '6', '\n',
                'D', 'E', 'P', 'T', 'H', ' ', '1', '\n',
                'M', 'A', 'X', 'V', 'A', 'L', ' ', '2', '5', '5', '\n',
                'T', 'U', 'P', 'L', 'T', 'Y', 'P', 'E', ' ', 'G', 'R', 'A', 'Y', 'S', 'C', 'A', 'L', 'E', '\n',
                'E', 'N', 'D', 'H', 'D', 'R', '\n',
                255, 204, 153, 102, 51, 0,
                255, 204, 153, 102, 51, 0,
                255, 204, 153, 102, 51, 0,
                255, 204, 153, 102, 51, 0,
                255, 204, 153, 102, 51, 0,
                255, 204, 153, 102, 51, 0
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(6, image.Header.Width);
            Assert.Equal(6, image.Header.Height);
            Assert.Equal(255, image.Header.HighestComponentValue);
            Assert.Equal(1, image.Header.Components.Count);
            Assert.Equal(Component.White, image.Header.Components[0]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[0]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[1]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[2]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[3]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[4]);
            Assert.Equal((IEnumerable <byte>)referenceRow, (IEnumerable <byte>)image.LoadedNativeRows[5]);
        }
Пример #12
0
        public void ValidThreeTimesThreeLotsOfWhitespace()
        {
            var redRow       = new byte[] { 1, 0, 0 };
            var greenRow     = new byte[] { 0, 1, 0 };
            var blueRow      = new byte[] { 0, 0, 1 };
            var bodyRawBytes = new object[]
            {
                'P', '6',
                ' ', '\n', ' ', ' ', ' ',
                '3', ' ', ' ', ' ', ' ', ' ', ' ', '3', ' ', ' ', '1', ' ',
                0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
                0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,
                0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(3, image.Header.Width);
            Assert.Equal(3, image.Header.Height);
            Assert.Equal(1, image.Header.HighestComponentValue);
            Assert.Equal(3, image.Header.Components.Count);
            Assert.Equal(Component.Red, image.Header.Components[0]);
            Assert.Equal(Component.Green, image.Header.Components[1]);
            Assert.Equal(Component.Blue, image.Header.Components[2]);
            Assert.Equal((IEnumerable <byte>)redRow, (IEnumerable <byte>)image.GetNativePixel(0, 0));
            Assert.Equal((IEnumerable <byte>)redRow, (IEnumerable <byte>)image.GetNativePixel(1, 0));
            Assert.Equal((IEnumerable <byte>)redRow, (IEnumerable <byte>)image.GetNativePixel(2, 0));
            Assert.Equal((IEnumerable <byte>)greenRow, (IEnumerable <byte>)image.GetNativePixel(0, 1));
            Assert.Equal((IEnumerable <byte>)greenRow, (IEnumerable <byte>)image.GetNativePixel(1, 1));
            Assert.Equal((IEnumerable <byte>)greenRow, (IEnumerable <byte>)image.GetNativePixel(2, 1));
            Assert.Equal((IEnumerable <byte>)blueRow, (IEnumerable <byte>)image.GetNativePixel(0, 2));
            Assert.Equal((IEnumerable <byte>)blueRow, (IEnumerable <byte>)image.GetNativePixel(1, 2));
            Assert.Equal((IEnumerable <byte>)blueRow, (IEnumerable <byte>)image.GetNativePixel(2, 2));
        }
Пример #13
0
        public void ValidTwoTimesTwoBitmapWithComments()
        {
            var bodyRawBytes = new object[]
            {
                'P', '7',
                '\n',
                '#', ' ', 't', 'w', 'o', ' ', 't', 'i', 'm', 'e', 's', ' ', 't', 'w', 'o', '\n',
                'W', 'I', 'D', 'T', 'H', ' ', '2', '\n',
                'H', 'E', 'I', 'G', 'H', 'T', ' ', '2', '\n',
                '#', ' ', 's', 'i', 'n', 'g', 'l', 'e', ' ', 'p', 'l', 'a', 'n', 'e', '\n',
                'D', 'E', 'P', 'T', 'H', ' ', '1', '\n',
                '#', ' ', 'b', 'i', 't', 'm', 'a', 'p', '\n',
                'M', 'A', 'X', 'V', 'A', 'L', ' ', '1', '\n',
                'T', 'U', 'P', 'L', 'T', 'Y', 'P', 'E', ' ', 'B', 'L', 'A', 'C', 'K', 'A', 'N', 'D', 'W', 'H', 'I', 'T', 'E', '\n',
                '#', ' ', 'd', 'o', 'n', 'e', '\n',
                'E', 'N', 'D', 'H', 'D', 'R', '\n',
                0x00, 0x01,
                0x01, 0x00
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(2, image.Header.Width);
            Assert.Equal(2, image.Header.Height);
            Assert.Equal(1, image.Header.HighestComponentValue);
            Assert.Equal(1, image.Header.Components.Count);
            Assert.Equal(Component.White, image.Header.Components[0]);
            Assert.Equal(0, image.GetNativePixel(0, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(1, 0)[0]);
            Assert.Equal(1, image.GetNativePixel(0, 1)[0]);
            Assert.Equal(0, image.GetNativePixel(1, 1)[0]);
        }
Пример #14
0
        public void ValidSixTimesSixGradient()
        {
            var referenceRows = new[]
            {
                new byte[] { 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0, 0, 0 },
                new byte[] { 0, 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0, 0 },
                new byte[] { 0, 0, 255, 0, 0, 204, 0, 0, 153, 0, 0, 102, 0, 0, 51, 0, 0, 0 },
                new byte[] { 255, 255, 0, 204, 204, 0, 153, 153, 0, 102, 102, 0, 51, 51, 0, 0, 0, 0 },
                new byte[] { 255, 0, 255, 204, 0, 204, 153, 0, 153, 102, 0, 102, 51, 0, 51, 0, 0, 0 },
                new byte[] { 0, 255, 255, 0, 204, 204, 0, 153, 153, 0, 102, 102, 0, 51, 51, 0, 0, 0 }
            };
            const string bodyString = "P3\n6 6 255\n255 0 0 204 0 0 153 0 0 102 0 0 51 0 0 0 0 0\n0 255 0 0 204 0 0 153 0 0 102 0 0 51 0 0 0 0\n0 0 255 0 0 204 0 0 153 0 0 102 0 0 51 0 0 0\n255 255 0 204 204 0 153 153 0 102 102 0 51 51 0 0 0 0\n255 0 255 204 0 204 153 0 153 102 0 102 51 0 51 0 0 0\n0 255 255 0 204 204 0 153 153 0 102 102 0 51 51 0 0 0";
            var          bodyBytes  = UsAsciiEncoding.GetBytes(bodyString);

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(6, image.Header.Width);
            Assert.Equal(6, image.Header.Height);
            Assert.Equal(255, image.Header.HighestComponentValue);
            Assert.Equal(3, image.Header.Components.Count);
            Assert.Equal(Component.Red, image.Header.Components[0]);
            Assert.Equal(Component.Green, image.Header.Components[1]);
            Assert.Equal(Component.Blue, image.Header.Components[2]);
            Assert.Equal((IEnumerable <byte>)referenceRows[0], (IEnumerable <byte>)image.LoadedNativeRows[0]);
            Assert.Equal((IEnumerable <byte>)referenceRows[1], (IEnumerable <byte>)image.LoadedNativeRows[1]);
            Assert.Equal((IEnumerable <byte>)referenceRows[2], (IEnumerable <byte>)image.LoadedNativeRows[2]);
            Assert.Equal((IEnumerable <byte>)referenceRows[3], (IEnumerable <byte>)image.LoadedNativeRows[3]);
            Assert.Equal((IEnumerable <byte>)referenceRows[4], (IEnumerable <byte>)image.LoadedNativeRows[4]);
            Assert.Equal((IEnumerable <byte>)referenceRows[5], (IEnumerable <byte>)image.LoadedNativeRows[5]);
        }
Пример #15
0
        public void TwoTimesTwoTooFewPixels()
        {
            var bodyRawBytes = new object[]
            {
                'P', '4',
                '9', ' ', '1', ' ',
                0x01
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var factory = new ImageFactories.Image8Factory();
            var reader  = new NetpbmReader();

            Assert.Throws <EndOfStreamException>(() =>
            {
                using (var bodyStream = new MemoryStream(bodyBytes, false))
                {
                    var image = reader.ReadImage(bodyStream, factory);
                    image.LoadData();
                }
            });
        }
Пример #16
0
        static int Main(string[] args)
        {
            ParserResult <Options> genericResult = Parser.Default.ParseArguments <Options>(args);
            var result = genericResult as Parsed <Options>;

            if (result == null)
            {
                return(1);
            }

            NetpbmImage8 image8;
            var          reader  = new NetpbmReader();
            var          writer  = new NetpbmWriter();
            var          factory = new ImageFactories.Image8Factory();

            using (var inputStream = new FileStream(result.Value.InputFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                image8 = (NetpbmImage8)reader.ReadImage(inputStream, factory);
            }

            var supportedTypes = writer.SupportedTypesForImage(image8);

            if (!supportedTypes.Contains(result.Value.ToType))
            {
                Console.Error.WriteLine("The chosen image cannot be converted to the chosen type. Supported types for the image are:");
                foreach (var supportedType in new SortedSet <ImageType>(supportedTypes))
                {
                    Console.Error.WriteLine(supportedType);
                }
                Environment.Exit(1);
            }

            using (var outputStream = new FileStream(result.Value.OutputFilename, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                writer.WriteImage(image8, outputStream, result.Value.ToType);
            }

            return(0);
        }
Пример #17
0
        public void TwoTimesTwoValueOutOfRange()
        {
            var bodyRawBytes = new object[]
            {
                'P', '6',
                '\n',
                '2', ' ', '2', ' ', '1', '\n',
                0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            var bodyBytes = bodyRawBytes.Select(Convert.ToByte).ToArray();

            var factory = new ImageFactories.Image8Factory();
            var reader  = new NetpbmReader();

            Assert.Throws <InvalidDataException>(() =>
            {
                using (var bodyStream = new MemoryStream(bodyBytes, false))
                {
                    var image = reader.ReadImage(bodyStream, factory);
                    image.LoadData();
                }
            });
        }
Пример #18
0
        public void ValidThreeTimesThreeLotsOfWhitespace()
        {
            var          redRow     = new byte[] { 1, 0, 0 };
            var          greenRow   = new byte[] { 0, 1, 0 };
            var          blueRow    = new byte[] { 0, 0, 1 };
            const string bodyString = "P3 \n   3      3  1 \n 1  0      0   1  0      0   1    0       0  0        1  0     0      1   0     0  1     0  0  0    1  0  0     1  0  0       1";
            var          bodyBytes  = UsAsciiEncoding.GetBytes(bodyString);

            var          factory = new ImageFactories.Image8Factory();
            var          reader  = new NetpbmReader();
            NetpbmImage8 image;

            using (var bodyStream = new MemoryStream(bodyBytes, false))
            {
                image = (NetpbmImage8)reader.ReadImage(bodyStream, factory);
                image.LoadData();
                Assert.Equal(-1, bodyStream.ReadByte());
            }

            Assert.Equal(3, image.Header.Width);
            Assert.Equal(3, image.Header.Height);
            Assert.Equal(1, image.Header.HighestComponentValue);
            Assert.Equal(3, image.Header.Components.Count);
            Assert.Equal(Component.Red, image.Header.Components[0]);
            Assert.Equal(Component.Green, image.Header.Components[1]);
            Assert.Equal(Component.Blue, image.Header.Components[2]);
            Assert.Equal((IEnumerable <byte>)redRow, (IEnumerable <byte>)image.GetNativePixel(0, 0));
            Assert.Equal((IEnumerable <byte>)redRow, (IEnumerable <byte>)image.GetNativePixel(1, 0));
            Assert.Equal((IEnumerable <byte>)redRow, (IEnumerable <byte>)image.GetNativePixel(2, 0));
            Assert.Equal((IEnumerable <byte>)greenRow, (IEnumerable <byte>)image.GetNativePixel(0, 1));
            Assert.Equal((IEnumerable <byte>)greenRow, (IEnumerable <byte>)image.GetNativePixel(1, 1));
            Assert.Equal((IEnumerable <byte>)greenRow, (IEnumerable <byte>)image.GetNativePixel(2, 1));
            Assert.Equal((IEnumerable <byte>)blueRow, (IEnumerable <byte>)image.GetNativePixel(0, 2));
            Assert.Equal((IEnumerable <byte>)blueRow, (IEnumerable <byte>)image.GetNativePixel(1, 2));
            Assert.Equal((IEnumerable <byte>)blueRow, (IEnumerable <byte>)image.GetNativePixel(2, 2));
        }