private static Texture2D LoadTexture2D(DDSImageInfo image, BinaryReader input, SurfaceFormat format) { DDSHeader header = image.Header; int width = header.DwWidth; int height = header.DwHeight; int flags = header.ImageFormat.DwFlags; bool compressedFormat = IsSet(flags, (int)DDSPixelFlags.FourCC); int[] mipMapSizes = new int[header.DwMipMapCount]; Texture2D tex = new Texture2D(width, height, mipMapSizes.Length > 1, format); if (compressedFormat) { int num = 16; if (format == SurfaceFormat.DXT1) { num = 8; } int mipLevel = 0; while (mipLevel < header.DwMipMapCount) { byte[] data = input.ReadBytes(CalculateMipMapSize(true, width, height, num, mipLevel)); tex.SetData <byte>(data, mipLevel, null, 0, data.Length); width = System.Math.Max(width / 2, 1); height = System.Math.Max(height / 2, 1); mipLevel++; } } return(tex); }
private static SurfaceFormat DetermineFormat(DDSImageInfo image) { DDSHeader header = image.Header; DDSHeader10 header10 = image.Header10; int flags = header.ImageFormat.DwFlags; bool compressedFormat = IsSet(flags, (int)DDSPixelFlags.FourCC); bool rgb = IsSet(flags, (int)DDSPixelFlags.RGB); bool alphaPixels = IsSet(flags, (int)DDSPixelFlags.AlphaPixels); bool alpha = IsSet(flags, (int)DDSPixelFlags.Alpha); bool luminance = IsSet(flags, (int)DDSPixelFlags.Luminance); if (compressedFormat) { int fourCC = header.ImageFormat.DwFourCC; if (fourCC == GetInt("DXT1")) { return(SurfaceFormat.DXT1); } else if (fourCC == GetInt("DXT3")) { return(SurfaceFormat.DXT3); } else if (fourCC == GetInt("DXT5")) { return(SurfaceFormat.DXT5); } } else { if (rgb) { int bpp = header.ImageFormat.DwRGBBitCount; switch (bpp) { case 32: return(SurfaceFormat.Color); } } else if (alphaPixels && alpha && (header.ImageFormat.DwRGBBitCount == 8)) { return(SurfaceFormat.Alpha8); } } throw new InvalidDataException("Unsupported DDS format."); }
private static DDSHeader ReadHeader(BinaryReader input) { DDSHeader header = new DDSHeader(); header.DwSize = input.ReadInt32(); if (header.DwSize != 124) { throw new InvalidOperationException("Invalid dds header size."); } header.DwFlags = input.ReadInt32(); header.DwHeight = input.ReadInt32(); header.DwWidth = input.ReadInt32(); header.DwLinearSize = input.ReadInt32(); header.DwDepth = input.ReadInt32(); header.DwMipMapCount = input.ReadInt32(); header.DwAlphaBitDepth = input.ReadInt32(); header.DwReserved1 = new int[10]; for (int i = 0; i < 10; i++) { header.DwReserved1[i] = input.ReadInt32(); } header.ImageFormat = ReadImageFormat(input); header.DwCaps = input.ReadInt32(); header.DwCaps2 = input.ReadInt32(); header.DwCaps3 = input.ReadInt32(); header.DwCaps4 = input.ReadInt32(); header.DwTextureStage = input.ReadInt32(); int mipMaps = 1 + (int)System.Math.Ceiling(System.Math.Log(System.Math.Max(header.DwHeight, header.DwWidth)) / System.Math.Log(2)); DDSCaps cap = (DDSCaps)header.DwCaps; if ((cap & DDSCaps.MipMap) == DDSCaps.MipMap) { DDSFlags flag = (DDSFlags)header.DwFlags; if ((flag & DDSFlags.MipCount) != DDSFlags.MipCount) { header.DwMipMapCount = mipMaps; } } else { header.DwMipMapCount = 1; } return(header); }
private static TextureDimensions DetermineTextureDimensions(DDSImageInfo image) { DDSHeader header = image.Header; DDSHeader10 header10 = image.Header10; if (IsSet(header.DwCaps2, (int)DDSCaps2.CubeMap)) { return(TextureDimensions.Cube); } else if (IsSet(header.DwCaps2, (int)DDSCaps2.Volume)) { return(TextureDimensions.Three); } else { return(TextureDimensions.Two); } }