TagTool.Common.DdsHeader.ReadDdsPixelFormat C# (CSharp) Method

ReadDdsPixelFormat() private method

private ReadDdsPixelFormat ( BinaryReader reader ) : void
reader System.IO.BinaryReader
return void
        private void ReadDdsPixelFormat(BinaryReader reader)
        {
            // Verify header size
            if (reader.ReadUInt32() != 32)
                throw new InvalidOperationException("Invalid DDS file: invalid DDS_PIXELFORMAT size");

            // Read flags and set FormatType accordingly
            var flags = (DdsFormatFlags)reader.ReadUInt32();
            if ((flags & DdsFormatFlags.Alpha) != 0)
                FormatType = DdsFormatType.Alpha;
            else if ((flags & DdsFormatFlags.Rgb) != 0)
                FormatType = DdsFormatType.Rgb;
            else if ((flags & DdsFormatFlags.Yuv) != 0)
                FormatType = DdsFormatType.Yuv;
            else if ((flags & DdsFormatFlags.Luminance) != 0)
                FormatType = DdsFormatType.Luminance;
            else if ((flags & DdsFormatFlags.FourCc) != 0)
                FormatType = DdsFormatType.Other;
            else
                throw new InvalidOperationException("Invalid DDS file: invalid DDS_PIXELFORMAT flags");

            // Read FourCC code (optional)
            var fourCc = reader.ReadUInt32();
            if ((flags & DdsFormatFlags.FourCc) != 0)
            {
                FourCc = fourCc;
                reader.BaseStream.Position += 20; // Skip masks
            }
            else
            {
                // Read RGB masks
                BitsPerPixel = reader.ReadUInt32();
                RBitMask = reader.ReadUInt32();
                GBitMask = reader.ReadUInt32();
                BBitMask = reader.ReadUInt32();

                // Read alpha mask (optional)
                var alphaMask = reader.ReadUInt32();
                if ((flags & DdsFormatFlags.AlphaPixels) != 0)
                    ABitMask = alphaMask;
            }
        }