/// <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); }
private void WriteDdsPixelFormat(BinaryWriter writer) { writer.Write(32); writer.Write((uint)CalculateFormatFlags()); writer.Write((D3D10Format == DxgiFormat.Unknown) ? FourCc : DdsFourCc.FromString("DX10")); writer.Write(BitsPerPixel); writer.Write(RBitMask); writer.Write(GBitMask); writer.Write(BBitMask); writer.Write(ABitMask); }
/// <summary> /// Writes the DDS header to a stream. On return, the stream will be positioned where the texture data should go. /// </summary> /// <param name="stream">The stream to write to.</param> /// <exception cref="System.InvalidOperationException">Thrown if an error occurs while saving.</exception> public void WriteTo(Stream stream) { var writer = new BinaryWriter(stream); writer.Write(DdsFourCc.FromString("DDS ")); WriteDdsHeader(writer); if (D3D10Format != DxgiFormat.Unknown) { WriteDdsD3D10Header(writer); } }