ReadByte() public méthode

public ReadByte ( ) : int
Résultat int
Exemple #1
0
        public void MustReadByteCorrectly()
        {
            var sut = new SubStream(stream.Object, 100, 200);

            sut.Position = -100;
            Assert.AreEqual(-1, sut.ReadByte());

            sut.Position = 200;
            Assert.AreEqual(-1, sut.ReadByte());

            sut.Position = 25;
            sut.ReadByte();

            stream.Verify(s => s.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()), Times.Once);
        }
Exemple #2
0
        public ImageInfo Load(Stream input)
        {
            using var br = new BinaryReaderX(input);

            // Header
            _header = br.ReadType <ImgcHeader>();
            //if (_header.imageFormat == 28 && _header.bitDepth == 8)
            //    _header.imageFormat = 29;

            // Get tile table
            var tileTableComp = new SubStream(input, _header.tableDataOffset, _header.tileTableSize);
            var tileTable     = new MemoryStream();

            Level5Compressor.Decompress(tileTableComp, tileTable);

            tileTableComp.Position = 0;
            _tileTableCompression  = (Level5CompressionMethod)(tileTableComp.ReadByte() & 0x7);

            // Get image data
            var imageDataComp = new SubStream(input, _header.tableDataOffset + _header.tileTableSizePadded,
                                              _header.imgDataSize);
            var imageData = new MemoryStream();

            Level5Compressor.Decompress(imageDataComp, imageData);

            imageDataComp.Position = 0;
            _imageDataCompression  = (Level5CompressionMethod)(imageDataComp.ReadByte() & 0x7);

            // Combine tiles to full image data
            tileTable.Position = imageData.Position = 0;
            var combinedImageStream = CombineTiles(tileTable, imageData, _header.bitDepth);

            // Split image data and mip map data
            var images = new byte[_header.imageCount][];

            var(width, height) = ((int)_header.width, (int)_header.height);
            for (var i = 0; i < _header.imageCount; i++)
            {
                images[i] = new byte[width * height * _header.bitDepth];
                combinedImageStream.Read(images[i], 0, images[i].Length);

                (width, height) = (width / 2, height / 2);
            }

            return(new ImageInfo
            {
                ImageData = images.FirstOrDefault(),
                ImageFormat = _header.imageFormat,
                ImageSize = new Size(_header.width, _header.height),
                MipMapData = images.Skip(1).ToArray(),
                Configuration = new ImageConfiguration()
                                .PadSizeWith(size => new Size((size.Width + 7) & ~7, (size.Height + 7) & ~7))
                                .RemapPixelsWith(size => new ImgcSwizzle(size.Width, size.Height))
            });
        }
Exemple #3
0
        public void Seek_FromCurrent()
        {
            // Verify that we can seek via the [Seek/current] method.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 0, 10))
                {
                    for (int i = 0; i < 5; i++)
                    {
                        substream.Position = 5;
                        Assert.Equal(i + 5, substream.Seek(i, SeekOrigin.Current));
                        Assert.Equal(i + 5, substream.ReadByte());
                    }
                }
            }

            // Verify that we can't seek before the beginning of the substream.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 5, 5))
                {
                    Assert.Throws <IOException>(
                        () =>
                    {
                        substream.Position = 0;
                        substream.Seek(-1, SeekOrigin.Current);
                    });
                }
            }

            // Verify that we can't seek past the end of the substream.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 5, 5))
                {
                    Assert.Throws <IOException>(
                        () =>
                    {
                        substream.Seek(6, SeekOrigin.Current);
                    });
                }
            }
        }
Exemple #4
0
        public void Seek_FromEnd()
        {
            // Verify that we can seek via the [Seek/end] method.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 0, 10))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Assert.Equal(9 - i, substream.Seek(-(i + 1), SeekOrigin.End));
                        Assert.Equal(9 - i, substream.ReadByte());
                    }
                }
            }

            // Verify that we can't seek before the beginning of the substream.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 5, 5))
                {
                    Assert.Throws <IOException>(
                        () =>
                    {
                        substream.Seek(-6, SeekOrigin.End);
                    });
                }
            }

            // Verify that we can't seek past the end of the substream.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 5, 5))
                {
                    Assert.Throws <IOException>(
                        () =>
                    {
                        substream.Seek(1, SeekOrigin.End);
                    });
                }
            }
        }
Exemple #5
0
        public void Seek_Position()
        {
            // Verify that we can seek via the [Position] property.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 5, 5))
                {
                    for (int i = 0; i < 5; i++)
                    {
                        substream.Position = i;
                        Assert.Equal(i + 5, substream.ReadByte());
                    }
                }
            }

            // Verify that we can't seek before the beginning of the substream.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 5, 5))
                {
                    Assert.Throws <IOException>(
                        () =>
                    {
                        substream.Position = -1;
                    });
                }
            }

            // Verify that we can't seek past the end of the substream.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 5, 5))
                {
                    Assert.Throws <IOException>(
                        () =>
                    {
                        substream.Position = 11;
                    });
                }
            }
        }
Exemple #6
0
        public void ReadByte()
        {
            // Verify that we can support an empty parent stream.

            using (var parent = new MemoryStream())
            {
                using (var substream = new SubStream(parent, 0, 0))
                {
                    Assert.Equal(0, substream.Position);
                    Assert.Equal(-1, substream.ReadByte());
                    Assert.Equal(0, substream.Position);
                }
            }

            // Verify that we can read all of the bytes from the parent when substream spans everything.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

                using (var substream = new SubStream(parent, 0, 10))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Assert.Equal(i, substream.Position);
                        Assert.Equal(i, substream.ReadByte());
                        Assert.Equal(i + 1, substream.Position);
                    }

                    // The next read should return -1.

                    Assert.Equal(10, substream.Position);
                    Assert.Equal(-1, substream.ReadByte());
                    Assert.Equal(10, substream.Position);
                }
            }
        }
Exemple #7
0
        public ImageInfo Load(Stream input)
        {
            using var br = new BinaryReaderX(input);

            // Header
            _header = br.ReadType <ImgxHeader>();

            // Get tile table
            var tileTableComp = new SubStream(input, _header.tableDataOffset, _header.tileTableSize);
            var tileTable     = new MemoryStream();

            Level5Compressor.Decompress(tileTableComp, tileTable);

            tileTableComp.Position = 0;
            _tileTableMethod       = (Level5CompressionMethod)(tileTableComp.ReadByte() & 0x7);

            // Get image data
            var imageDataComp = new SubStream(input, _header.tableDataOffset + _header.tileTableSizePadded, _header.imgDataSize);
            var imageData     = new MemoryStream();

            Level5Compressor.Decompress(imageDataComp, imageData);

            imageDataComp.Position = 0;
            _imgDataMethod         = (Level5CompressionMethod)(imageDataComp.ReadByte() & 0x7);

            // Combine tiles to full image data
            tileTable.Position = imageData.Position = 0;
            var combinedImageStream = CombineTiles(tileTable, imageData, _header.bitDepth);

            // Split image data and mip map data
            var images = new byte[_header.imageCount][];

            var(width, height) = ((_header.width + 7) & ~7, (_header.height + 7) & ~7);
            for (var i = 0; i < _header.imageCount; i++)
            {
                images[i] = new byte[width * height * _header.bitDepth / 8];
                combinedImageStream.Read(images[i], 0, images[i].Length);

                (width, height) = (width >> 1, height >> 1);
            }

            var imageInfo = new ImageInfo(images.FirstOrDefault(), images.Skip(1).ToArray(), _header.imageFormat, new Size(_header.width, _header.height));

            imageInfo.RemapPixels.With(context => new ImgxSwizzle(context, _header.magic));

            return(imageInfo);
        }
Exemple #8
0
        public void Dispose()
        {
            // Verify that substring dispose restores the parent stream position.

            using (var parent = new MemoryStream())
            {
                parent.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
                parent.Position = 5;

                using (var substream = new SubStream(parent, 2, 5))
                {
                    Assert.Equal(2, substream.ReadByte());
                }

                Assert.Equal(5, parent.Position);
            }
        }