/// <summary>
/// Reads a DDS header from a stream. On return, the stream will be positioned at the beginning of the texture data.
/// </summary>
/// <param name="stream">The stream to read from.</param>
/// <returns>The header that was read.</returns>
/// <exception cref="System.InvalidOperationException">Thrown if the DDS header is invalid.</exception>
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);
}