public static bool DDSCompress(Bitmap bitmap, DDSFourCC fourCC, out byte[] newData) { Bitmap temp = null; if (bitmap.PixelFormat == PixelFormats.Bgra32) { temp = bitmap; } else { temp = bitmap.ConvertTo(PixelFormats.Bgra32, null); } bool returned = DDSCompress(temp.Width, temp.Height, temp.InternalGetData(), fourCC, out byte[] returneddata); newData = returneddata; return(returned); }
public static bool DDSCompress(int width, int height, byte[] data, DDSFourCC fourCC, out byte[] newData) { newData = null; if (fourCC == DDSFourCC.NONE) { return(false); } int Width = (int)Math.Ceiling((float)width / 4); int Heigth = (int)Math.Ceiling((float)height / 4); int step = fourCC == DDSFourCC.DXT1 ? 8 : 16; byte[,,] pixels = new byte[height, width, 4]; Buffer.BlockCopy(data, 0, pixels, 0, height * width * 4); byte[] compressedData = new byte[Width * Heigth * step]; for (int y = 0, index = 0; y < height; y += 4) { for (int x = 0; x < width; x += 4, index += step) { if (fourCC == DDSFourCC.DXT1) { EncodeBC1(pixels, x, y, compressedData, index, false); } else if (fourCC == DDSFourCC.DXT3) { EncodeBC2(pixels, x, y, compressedData, index); } else if (fourCC == DDSFourCC.DXT5) { EncodeBC3(pixels, x, y, compressedData, index); } } } newData = compressedData; return(true); }
/// <summary> /// If returned true then newData is Bgra32. /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <param name="data"></param> /// <param name="fourCC"></param> /// <param name="newData"></param> /// <returns></returns> public static bool DDSDecompress(int width, int height, byte[] data, DDSFourCC fourCC, out byte[] newData) { newData = null; if (fourCC == DDSFourCC.NONE) { return(false); } int Width = (int)Math.Ceiling((float)width / 4); int Heigth = (int)Math.Ceiling((float)height / 4); int step = fourCC == DDSFourCC.DXT1 ? 8 : 16; byte[,,] pixel = new byte[height, width, 4]; byte[] uncompressed_data = new byte[width * height * 4]; for (int i = 0, index = 0; i < Heigth; i++) { for (int k = 0; k < Width; k++, index += step) { if (fourCC == DDSFourCC.DXT1) { DDS_DXT1_GetPixels(pixel, k * 4, i * 4, data, index); } else if (fourCC == DDSFourCC.DXT3) { DDS_DXT3_GetPixels(pixel, k * 4, i * 4, data, index); } else if (fourCC == DDSFourCC.DXT5) { DDS_DXT5_GetPixels(pixel, k * 4, i * 4, data, index); } } } Buffer.BlockCopy(pixel, 0, uncompressed_data, 0, uncompressed_data.Length); newData = uncompressed_data; return(true); }