private static void SetupExtendedHeader(out ExtendedHeader header, TextureHeader textureHeader)
 {
     header.Format     = DDSHelpers.GetDXGIFormat(textureHeader.Format);
     header.Dimension  = 3;
     header.MiscFlags  = 0;
     header.ArraySize  = 1;
     header.MiscFlags2 = 0;
 }
        public static void WriteFile(TextureHeader textureHeader, byte[] data, Stream output)
        {
            const Endian endian = Endian.Little;

            output.WriteString("DDS ", Encoding.ASCII);

            switch (textureHeader.Format)
            {
            case TextureFormat.BC1_UNORM:
            case TextureFormat.BC1_SRGB:
            case TextureFormat.BC2_UNORM:
            case TextureFormat.BC2_SRGB:
            case TextureFormat.BC3_UNORM:
            case TextureFormat.BC3_SRGB:
            case TextureFormat.BC4_UNORM:
            case TextureFormat.BC5_UNORM:
            {
                Header header;
                SetupHeader(out header, textureHeader);
                header.PixelFormat.FourCC = DDSHelpers.GetFourCC(textureHeader.Format);
                header.PixelFormat.Flags  = (PixelFormatFlags)4;
                header.Write(output, endian);
                output.WriteBytes(data);
                break;
            }

            case TextureFormat.BC6U_FLOAT:
            case TextureFormat.BC7_UNORM:
            case TextureFormat.BC7_SRGB:
            case TextureFormat.R9G9B9E5_FLOAT:     // TODO(gibbed): probably not right
            {
                Header         header;
                ExtendedHeader extendedHeader;
                SetupHeader(out header, textureHeader);
                header.PixelFormat.FourCC = 0x30315844;
                header.PixelFormat.Flags  = (PixelFormatFlags)4;
                SetupExtendedHeader(out extendedHeader, textureHeader);
                header.Write(output, endian);
                extendedHeader.Write(output, endian);
                output.WriteBytes(data);
                break;
            }

            case TextureFormat.R8_UNORM:
            {
                Header header;
                SetupHeader(out header, textureHeader);
                header.PixelFormat.RedBitMask   = 0x000000FF;
                header.PixelFormat.GreenBitMask = 0x00000000;
                header.PixelFormat.BlueBitMask  = 0x00000000;
                header.PixelFormat.AlphaBitMask = 0x00000000;
                header.PixelFormat.RGBBitCount  = 8;
                header.PitchOrLinearSize        = textureHeader.Width;
                header.PixelFormat.Flags        = PixelFormatFlags.Luminance;
                header.Write(output, endian);
                output.WriteBytes(data);
                break;
            }

            case TextureFormat.R8G8B8A8_UNORM:
            case (TextureFormat)20:     // R8G8B8A8_SRGB
                //case GX2SurfaceFormat.TCS_R8_G8_B8_A8_SRGB:
            {
                Header header;
                SetupHeader(out header, textureHeader);
                header.PixelFormat.RedBitMask   = 0x000000FF;
                header.PixelFormat.GreenBitMask = 0x0000FF00;
                header.PixelFormat.BlueBitMask  = 0x00FF0000;
                header.PixelFormat.AlphaBitMask = 0xFF000000;
                header.PixelFormat.RGBBitCount  = 32;
                header.PitchOrLinearSize        = (uint)textureHeader.Width * 4;
                header.PixelFormat.Flags        = PixelFormatFlags.RGBA;
                header.Write(output, endian);
                output.WriteBytes(data);
                break;
            }

            case TextureFormat.R10G10B10A2_UNORM:
            {
                Header header;
                SetupHeader(out header, textureHeader);
                header.PixelFormat.RedBitMask   = 0x000003FF;
                header.PixelFormat.GreenBitMask = 0x000FFC00;
                header.PixelFormat.BlueBitMask  = 0x3FF00000;
                header.PixelFormat.AlphaBitMask = 0xC0000000;
                header.PixelFormat.RGBBitCount  = 32;
                header.PitchOrLinearSize        = (uint)textureHeader.Width * 4;
                header.PixelFormat.Flags        = PixelFormatFlags.RGBA;
                header.Write(output, endian);
                output.WriteBytes(data);
                break;
            }

            case TextureFormat.R32G32B32A32_FLOAT:
            {
                Header header;
                SetupHeader(out header, textureHeader);
                header.PixelFormat.FourCC = 0x00000074;
                header.PitchOrLinearSize  = (uint)textureHeader.Width * 16;
                header.PixelFormat.Flags  = (PixelFormatFlags)4;
                header.Write(output, endian);
                output.WriteBytes(data);
                break;
            }

            case TextureFormat.R16G16B16A16_FLOAT:
            {
                Header header;
                SetupHeader(out header, textureHeader);
                header.PixelFormat.FourCC = 0x00000071;
                header.PitchOrLinearSize  = (uint)textureHeader.Width * 8;
                header.PixelFormat.Flags  = (PixelFormatFlags)4;
                header.Write(output, endian);
                output.WriteBytes(data);
                break;
            }

            default:
            {
                throw new NotSupportedException();
            }
            }
        }