TagTool.Common.DdsHeader.Read C# (CSharp) Method

Read() public static method

Reads a DDS header from a stream. On return, the stream will be positioned at the beginning of the texture data.
Thrown if the DDS header is invalid.
public static Read ( Stream stream ) : DdsHeader
stream Stream The stream to read from.
return DdsHeader
        public static DdsHeader Read(Stream stream)
        {
            var reader = new BinaryReader(stream);
            if (stream.Length - stream.Position < 128)
                throw new InvalidOperationException("Invalid DDS file: header is too small");
            var result = new DdsHeader();

            // Read and verify magic
            if (reader.ReadUInt32() != DdsFourCc.FromString("DDS "))
                throw new InvalidOperationException("Invalid DDS file: invalid header magic");

            // Read the DDS header
            result.ReadDdsHeader(reader);

            // If the format FourCC is 'DX10', read the extended header
            if (result.FourCc == DdsFourCc.FromString("DX10"))
                result.ReadDdsD3D10Header(reader);

            return result;
        }