private static void ProccesImage(string fileName) { if (fileName.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)) { BMPImage bmp = new BMPImage(fileName.Remove(fileName.Length-4)); DDSImage dds = ImageCompression.ConvertBMPToDDS(bmp); Console.WriteLine("Saved"); } else if(fileName.EndsWith(".dds", StringComparison.OrdinalIgnoreCase)) { DDSImage dds = new DDSImage(fileName.Remove(fileName.Length-4)); BMPImage bmp = ImageCompression.ConvertDDSToBMP(dds); Console.WriteLine("Saved"); } else { Console.WriteLine(fileName+ " File not supported"); } }
public static BMPImage ConvertDDSToBMP(DDSImage dds) { //Creates new empty BMP file BMPImage bmp = new BMPImage((UInt32)dds.Width, (UInt32)dds.Height); //container for colors RGBColor[] colors = new RGBColor[4]; int texelsX = (int)dds.Width / 4; int x, y = 0; //iterates through all data in DDS file for (int i = 0; i < dds.ImageDataLength; i++) { DDSImage.TexelBlock t = dds.GetImageData(i); colors = ReadMainColors(t); RGBColor color = new RGBColor(0,0,0); byte[] texelCol = new byte[4]; for (int k=0;k<t.blocks.Length;k++) { texelCol[0] = (byte)(t.blocks[k] & 0x3); texelCol[1] = (byte)((t.blocks[k] & 0xC) >> 2); texelCol[2] = (byte)((t.blocks[k] & 0x30) >> 4); texelCol[3] = (byte)((t.blocks[k] & 0xC0) >> 6); for (int j = 0; j < 4; j++) { color = colors[texelCol[j]]; x = (i % texelsX) * 4 + j; y = ((int)dds.Height - 1) - ((i / texelsX) * 4) - k; bmp.AddImageData(x, y, color); } } } bmp.Save(dds.FileName); return bmp; }
public static DDSImage ConvertBMPToDDS(BMPImage bmp) { // In this algorithm (0,0) is the lower left corner because BMP data starts from there DDSImage dds = new DDSImage(bmp.Width,bmp.Height); for (int y = 0; y< bmp.Height; y += 4) { for (int x = 0; x < bmp.Width; x += 4) { RGBColor[] block = new RGBColor[DDSConst.DDS_BLOCK_SIZE]; for (int i = 0; i < block.Length; i++) { block[i] = bmp.GetImageData(x +(i%4) , (int)bmp.Height - 1 - (y + (i / 4))); } dds.AddImageData(CompressDataBlock(block)); } } dds.Save(bmp.FileName); return dds; }
private static RGBColor[] ReadMainColors(DDSImage.TexelBlock texel) { RGBColor[] colors = new RGBColor[4]; RGBColor rgbColor0 = ImageUtils.ConvertRGB565ToRGB888(texel.color0); RGBColor rgbColor1 = ImageUtils.ConvertRGB565ToRGB888(texel.color1); RGBColor rgbColor2 = new RGBColor( (int)((2.0f / 3.0f) * rgbColor0.R + (1.0f / 3.0f) * rgbColor1.R), (int)((2.0f / 3.0f) * rgbColor0.G + (1.0f / 3.0f) * rgbColor1.G), (int)((2.0f / 3.0f) * rgbColor0.B + (1.0f / 3.0f) * rgbColor1.B)); RGBColor rgbColor3 = new RGBColor( (int)((1.0f / 3.0f) * rgbColor0.R + (2.0f / 3.0f) * rgbColor1.R), (int)((1.0f / 3.0f) * rgbColor0.G + (2.0f / 3.0f) * rgbColor1.G), (int)((1.0f / 3.0f) * rgbColor0.B + (2.0f / 3.0f) * rgbColor1.B)); colors[0] = rgbColor0; colors[1] = rgbColor1; colors[2] = rgbColor2; colors[3] = rgbColor3; return colors; }