コード例 #1
0
        private static bool TryReadChunkHeader(Stream stream, out ChunkHeader chunkHeader)
        {
            chunkHeader = default;

            var position = stream.Position;

            if (!StreamHelper.TryReadHeaderBytes(stream, out var headerBytes))
            {
                return(false);
            }

            var length = StreamHelper.ReadBigEndianInt32(headerBytes, 0);

            var name = Encoding.ASCII.GetString(headerBytes, 4, 4);

            chunkHeader = new ChunkHeader(position, length, name);

            return(true);
        }
コード例 #2
0
        private static ImageHeader ReadImageHeader(Stream stream, byte[] crc)
        {
            if (!TryReadChunkHeader(stream, out var header))
            {
                throw new ArgumentException("The provided stream did not contain a single chunk.");
            }

            if (header.Name != "IHDR")
            {
                throw new ArgumentException($"The first chunk was not the IHDR chunk: {header}.");
            }

            if (header.Length != 13)
            {
                throw new ArgumentException($"The first chunk did not have a length of 13 bytes: {header}.");
            }

            var ihdrBytes = new byte[13];
            var read      = stream.Read(ihdrBytes, 0, ihdrBytes.Length);

            if (read != 13)
            {
                throw new InvalidOperationException($"Did not read 13 bytes for the IHDR, only found: {read}.");
            }

            read = stream.Read(crc, 0, crc.Length);
            if (read != 4)
            {
                throw new InvalidOperationException($"Did not read 4 bytes for the CRC, only found: {read}.");
            }

            var width             = StreamHelper.ReadBigEndianInt32(ihdrBytes, 0);
            var height            = StreamHelper.ReadBigEndianInt32(ihdrBytes, 4);
            var bitDepth          = ihdrBytes[8];
            var colorType         = ihdrBytes[9];
            var compressionMethod = ihdrBytes[10];
            var filterMethod      = ihdrBytes[11];
            var interlaceMethod   = ihdrBytes[12];

            return(new ImageHeader(width, height, bitDepth, (ColorType)colorType, (CompressionMethod)compressionMethod, (FilterMethod)filterMethod,
                                   (InterlaceMethod)interlaceMethod));
        }