public DdsHeader(Stream s) { BinaryReader r = new BinaryReader(s); // -- DWORD dwMagic -- magic = (DdsMagic)r.ReadUInt32(); if (!Enum.IsDefined(typeof(DdsMagic), magic)) throw new InvalidDataException(String.Format( "DDS Magic Number invalid. Got 0x{0:X8}, expected 0x{1:X8} ('DDS '). At 0x{2:X8}." , (uint)magic , (uint)DdsMagic.dds_ , s.Position ) ); // -- DDS_HEADER header -- size = r.ReadUInt32(); if (size == 0) { Console.WriteLine(String.Format( "DDS Header Size is zero, expected 124. At 0x{0:X8}. Correcting..." , s.Position )); size = 124; } else if (size != 124) throw new InvalidDataException(String.Format( "DDS Header Size invalid. Got {0}, expected 124. At 0x{1:X8}." , size , s.Position )); flags = (DdsFlags)r.ReadUInt32(); if (((uint)(flags & (writeFlags | DdsFlags.pitch | DdsFlags.linearSize))).Bits() != ((uint)writeFlags).Bits() + 1) { Console.WriteLine(String.Format( "DDS Header Flags invalid. Got {0}, expected {1} and either _pitch or _linearSize. At 0x{2:X8}. Correcting..." , flags , writeFlags , s.Position )); flags |= writeFlags; // We can't know yet which is best to pick of _pitch and _linearSize } height = r.ReadUInt32(); if (height == 0) throw new InvalidDataException(String.Format( "DDS Height invalid. Got 0, expected non-zero value. At 0x{1:X8}" , s.Position ) ); width = r.ReadUInt32(); if (width == 0) throw new InvalidDataException(String.Format( "DDS Width invalid. Got 0, expected non-zero value. At 0x{1:X8}" , s.Position ) ); pitchOrLinearSize = r.ReadUInt32(); depth = r.ReadUInt32(); if ((flags & DdsFlags.depth) != 0 && depth != 0) { Console.WriteLine(String.Format( "Only simple DDS Textures are supported. This DDS has depth {0}. At 0x{1:X8}. Clearing flag and _depth to zero..." , depth , s.Position )); flags &= ~DdsFlags.depth; depth = 0; } if ((flags & DdsFlags.depth) != 0 || depth != 0) { Console.WriteLine(String.Format( "DDS Header invalid. Inconsistent depth flag ({0}) and _depth ({1}). At 0x{2:X8}. Clearing flag and _depth to zero..." , flags & DdsFlags.depth , depth , s.Position )); flags &= ~DdsFlags.depth; depth = 0; } numMipMaps = r.ReadUInt32(); if ((flags & DdsFlags.mipMapCount) != 0 && numMipMaps != 1) { Console.WriteLine(String.Format( "Only simple DDS Textures are supported. This DDS has {0} mip maps. At 0x{1:X8}. Clearing flag and setting count to one..." , numMipMaps , s.Position )); flags &= ~DdsFlags.mipMapCount; numMipMaps = 1; } if ((flags & DdsFlags.mipMapCount) != 0 || numMipMaps == 0) { Console.WriteLine(String.Format( "DDS Header invalid. Inconsistent mipMapCount flag ({0}) and _numMipMaps ({1}). At 0x{2:X8}. Clearing flag and setting count to one..." , flags & DdsFlags.mipMapCount , numMipMaps , s.Position )); flags &= ~DdsFlags.mipMapCount; numMipMaps = 1; } for (int i = 0; i < reserved1.Length; i++) reserved1[i] = r.ReadUInt32(); for (int i = 0; i < reserved1.Length; i++) reserved1[i] = 0; pixelFormat = new DdsPixelFormat(s); var mask = (pixelFormat.FourCC == 0) ? DdsFlags.pitch : DdsFlags.linearSize; var notMask = (pixelFormat.FourCC != 0) ? DdsFlags.pitch : DdsFlags.linearSize; if ((flags & mask) == 0) { Console.WriteLine(String.Format( "DDS Header Flags invalid. {0} not set. At 0x{1:X8}. Correcting..." , mask , s.Position )); flags &= ~notMask; flags |= mask; } surfaceCaps = (DdsSurfaceCaps)r.ReadUInt32(); if ((surfaceCaps & ~DdsSurfaceCaps.texture) != 0) { Console.WriteLine(String.Format( "Only simple DDS Textures are supported. This DDS has other surface capabilities: {0}. At 0x{1:X8}. Clearing unsupported capabilities..." , surfaceCaps , s.Position )); surfaceCaps &= DdsSurfaceCaps.texture; } if (surfaceCaps == 0) { Console.WriteLine(String.Format( "DDS Header Surface Caps invalid. Got zero, expected texture. At 0x{0:X8}. Correcting..." , s.Position )); surfaceCaps |= DdsSurfaceCaps.texture; } cubeMapCaps = (DdsCubeMapCaps)r.ReadUInt32(); if (cubeMapCaps != 0) { Console.WriteLine(String.Format( "Only simple DDS Textures are supported. This DDS has cube map capabilities: {0}. At 0x{1:X8}. Clearing unsupported capabilities..." , cubeMapCaps , s.Position )); cubeMapCaps = 0; } unusedCaps3 = r.ReadUInt32(); unusedCaps4 = r.ReadUInt32(); reserved2 = r.ReadUInt32(); unusedCaps3 = unusedCaps4 = reserved2 = 0; // -- DDS_HEADER_DX10 header10 -- Not Supported }
uint reserved2;//124 #endregion public DdsHeader(DdsFormat ddsFormat, int width, int height) { this.magic = DdsMagic.dds_; this.size = 124; //flags this.height = (uint)height; this.width = (uint)width; //pitchOrLinearSize this.depth = 0; this.numMipMaps = 0; for (int i = 0; i < reserved1.Length; i++) this.reserved1[i] = 0; this.pixelFormat = new DdsPixelFormat(ddsFormat); this.surfaceCaps = DdsSurfaceCaps.texture; this.cubeMapCaps = 0; this.unusedCaps3 = 0; this.unusedCaps4 = 0; this.reserved2 = 0; this.flags = writeFlags | (IsBlockCompressed ? DdsFlags.linearSize : DdsFlags.pitch); this.pitchOrLinearSize = IsBlockCompressed ? LinearSize : Pitch; }