Represents the header of a DirectDraw Surface (DDS) file.
 private static DdsHeader CreateDdsHeader(BitmapTextureResourceDefinition definition)
 {
     var info = definition.Texture.Definition;
     var result = new DdsHeader
     {
         Width = (uint)info.Width,
         Height = (uint)info.Height,
         MipMapCount = (uint)info.Levels
     };
     BitmapDdsFormatDetection.SetUpHeaderForFormat(info.Format, result);
     switch (info.Type)
     {
         case BitmapType.CubeMap:
             result.SurfaceComplexityFlags = DdsSurfaceComplexityFlags.Complex;
             result.SurfaceInfoFlags = DdsSurfaceInfoFlags.CubeMap | DdsSurfaceInfoFlags.CubeMapNegativeX |
                                       DdsSurfaceInfoFlags.CubeMapNegativeY | DdsSurfaceInfoFlags.CubeMapNegativeZ |
                                       DdsSurfaceInfoFlags.CubeMapPositiveX | DdsSurfaceInfoFlags.CubeMapPositiveY |
                                       DdsSurfaceInfoFlags.CubeMapPositiveZ;
             break;
         case BitmapType.Texture3D:
             result.Depth = (uint)info.Depth;
             result.SurfaceInfoFlags = DdsSurfaceInfoFlags.Volume;
             break;
     }
     const string dew = "Doritos(TM) Dew(TM) it right!";
     Encoding.ASCII.GetBytes(dew, 0, dew.Length, result.Reserved, 0);
     return result;
 }
 public static BitmapType DetectType(DdsHeader dds)
 {
     if ((dds.SurfaceInfoFlags & DdsSurfaceInfoFlags.CubeMap) != 0)
         return BitmapType.CubeMap;
     if ((dds.SurfaceInfoFlags & DdsSurfaceInfoFlags.Volume) != 0)
         return BitmapType.Texture3D;
     return BitmapType.Texture2D;
 }
 public static void SetUpHeaderForFormat(BitmapFormat format, DdsHeader header)
 {
     BitmapFormatDefinition definition;
     if (!ExtractionDefinitions.TryGetValue(format, out definition))
         throw new InvalidOperationException("Invalid bitmap format: " + format);
     header.FormatType = definition.FormatType;
     header.BitsPerPixel = definition.BitsPerPixel;
     header.RBitMask = definition.RBitMask;
     header.GBitMask = definition.GBitMask;
     header.BBitMask = definition.BBitMask;
     header.ABitMask = definition.ABitMask;
     header.FourCc = definition.FourCc;
     header.D3D10Format = definition.D3D10Format;
 }
 public static BitmapFormat DetectFormat(DdsHeader header)
 {
     InitInjectionDefinitions();
     var definition = new BitmapFormatDefinition
     {
         FormatType = header.FormatType,
         BitsPerPixel = header.BitsPerPixel,
         RBitMask = header.RBitMask,
         GBitMask = header.GBitMask,
         BBitMask = header.BBitMask,
         ABitMask = header.ABitMask,
         FourCc = header.FourCc,
         D3D10Format = header.D3D10Format
     };
     BitmapFormat result;
     if (!_injectionDefinitions.TryGetValue(definition, out result))
         throw new InvalidOperationException("Unsupported pixel format");
     return result;
 }
Example #5
0
        /// <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;
        }