public void Write(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true); writer.Write(MagicNumber); Header.Write(outputStream); if (Header.IsDx10()) { HeaderDx10.Write(outputStream); } writer.Write(Data); }
/// <summary> /// Decode DDS Header. /// </summary> /// <param name="reader">Image stream.</param> /// <param name="description">Image description.</param> public void DecodeHeader(BinaryReader reader, out ImageDescription description) { Header header = ImageHelpers.ReadStruct <Header>(reader); DxgiFormat dxgiFormat; D3DFormat d3dFormat = D3DFormat.Unknown; HeaderDx10 headerDX10 = default; if (header.PixelFormat.FourCC != Helper.DX10) { Helper.DetermineFormat(ref header.PixelFormat, out dxgiFormat, out d3dFormat); } else { byte[] headerDx10Bytes = reader.ReadBytes(Marshal.SizeOf(typeof(HeaderDx10))); GCHandle headerDx10Handle = GCHandle.Alloc(headerDx10Bytes, GCHandleType.Pinned); headerDX10 = (HeaderDx10)Marshal.PtrToStructure(headerDx10Handle.AddrOfPinnedObject(), typeof(HeaderDx10)); headerDx10Handle.Free(); dxgiFormat = headerDX10.Format; } uint faces = 1; if (headerDX10.MiscFlags.HasFlag(ResourceMiscFlags.TextureCube) || header.Caps2.HasFlag(Caps2.CubeMap)) { faces = 6; } description = new ImageDescription() { imageFormat = ImageFormat.DDS, Width = header.Width, Height = header.Height, Depth = header.Flags.HasFlag(HeaderFlags.Depth) ? header.Depth : 1, MipLevels = header.MipMapCount == 0 ? 1 : header.MipMapCount, ArraySize = headerDX10.ArraySize == 0 ? 1 : headerDX10.ArraySize, Faces = faces, pixelFormat = (WaveEngine.Common.Graphics.PixelFormat)dxgiFormat, }; }