TagTool.Common.DdsHeader.ReadDdsHeader C# (CSharp) Méthode

ReadDdsHeader() private méthode

private ReadDdsHeader ( BinaryReader reader ) : void
reader System.IO.BinaryReader
Résultat void
        private void ReadDdsHeader(BinaryReader reader)
        {
            // Verify header size
            if (reader.ReadUInt32() != 124)
                throw new InvalidOperationException("Invalid DDS file: invalid DDS_HEADER size");

            // Read and verify flags
            var flags = (DdsFlags)reader.ReadUInt32();
            if ((flags & DdsFlags.HeaderFlags) != DdsFlags.HeaderFlags)
                throw new InvalidOperationException("Invalid DDS file: missing one or more required DDS_HEADER flags");

            // Read dimensions
            Height = reader.ReadUInt32();
            Width = reader.ReadUInt32();

            // Read pitch/linear size (both are stored in the same location)
            var pitchOrLinearSize = reader.ReadUInt32();
            if ((flags & DdsFlags.Pitch) != 0)
                Pitch = pitchOrLinearSize;
            else if ((flags & DdsFlags.LinearSize) != 0)
                LinearSize = pitchOrLinearSize;

            // Read depth (optional)
            var depth = reader.ReadUInt32();
            if ((flags & DdsFlags.Depth) != 0)
                Depth = depth;

            // Read mipmap count (optional)
            var mipMapCount = reader.ReadUInt32();
            if ((flags & DdsFlags.MipMapCount) != 0)
                MipMapCount = mipMapCount;

            // Read reserved/unused data
            Reserved = reader.ReadBytes(44);

            // Read DDS_PIXELFORMAT
            ReadDdsPixelFormat(reader);

            // Read capabilities and unused data
            SurfaceComplexityFlags = (DdsSurfaceComplexityFlags)reader.ReadUInt32();
            SurfaceInfoFlags = (DdsSurfaceInfoFlags)reader.ReadUInt32();
            UnusedCaps3 = reader.ReadUInt32();
            UnusedCaps4 = reader.ReadUInt32();
            Reserved2 = reader.ReadUInt32();
        }

Usage Example

Exemple #1
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);
        }
All Usage Examples Of TagTool.Common.DdsHeader::ReadDdsHeader