RasterImage GetDXT1RasterImage() { //open stream and read file data MemoryStream mStream = new MemoryStream(FileData); mStream.ReadBytes(6); //skip byte compression = mStream.ReadByte(); //Compression Type if (compression == 4) //DXT1 { int mipLevels = mStream.ReadByte(); //number of Miplevels //get header values int imageWidth = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int imageHeight = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int offset = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int dataLenght = BitConverter.ToInt32(mStream.ReadBytes(4), 0); //Get pixeldata mStream.SetPosition(offset); RasterImage image = new RasterImage(imageWidth, imageHeight); int xOffset = 0; int yOffset = 0; for (int i = 0; i < dataLenght; i += 8) { //Get 4 colors UInt16 iC0 = BitConverter.ToUInt16(mStream.ReadBytes(2), 0); //16bit Color c0 5:6:5 RGB UInt16 iC1 = BitConverter.ToUInt16(mStream.ReadBytes(2), 0); //16bit Color c1 5:6:5 RGB Color[] colors = GetColorsFromDXTFormat(iC0, iC1, true); //Calculate the 4 colors //Get 4x4 Pixelblock and set Pixels in bitmap for (int y = 0; y < 4; y++) { List <byte> pointerBits = new List <byte>(ByteAnalyzer.GetBitsFromNumber((int)mStream.ReadByte()));//byte holding 4 2bit Color pointers for (int x = 0; x < 4; x++) { int colorPointer = ByteAnalyzer.GetIntFromBits(pointerBits.GetRange(x * 2, 2).ToArray());//Pointer for pixel image.SetPixel(xOffset + x, yOffset + y, colors[colorPointer]); } } // set xOffset and yOffset values if (xOffset + 4 < imageWidth) { xOffset += 4; } else { xOffset = 0; yOffset += 4; } } return(image); } else { return(null); } }
//Save as ddt file public void SaveDdt(string savePath) { //open stream and write file data Stream stream = new FileStream(savePath, FileMode.Create); BinaryWriter writer = new BinaryWriter(stream); writer.Write(Encoding.Default.GetBytes("RTS3")); //Magic word (constant) writer.Write((byte)0); //Usage unknown 0 or 1 writer.Write((byte)8); //Alpha size 0=no aplha 8=8bit aplha writer.Write((byte)1); //compression 1 for uncompressed writer.Write((byte)1); //num mipmaps writer.Write(BitConverter.GetBytes(Width)); //image width writer.Write(BitConverter.GetBytes(Height)); //image height int dataOffset = 24; for (int i = 0; i < 1; i++)//foreach miplevel { int mipMapWidth = (int)(Width / Math.Pow((double)2, (double)i)); int mipMapHeight = (int)(Height / Math.Pow((double)2, (double)i)); if (mipMapWidth < 1) { mipMapWidth = 1; } if (mipMapHeight < 1) { mipMapHeight = 1; } int dataLenght = mipMapWidth * mipMapHeight * 4; writer.Write(BitConverter.GetBytes(dataOffset)); //data offset per mipmap writer.Write(BitConverter.GetBytes(dataLenght)); //data lenght per mipmap RasterImage mipMapRasterImage = new RasterImage(this, mipMapWidth, mipMapHeight); byte[] pixelData = new byte[dataLenght]; for (int x = 0; x < mipMapRasterImage.Width; x++) { for (int y = 0; y < mipMapRasterImage.Height; y++) { pixelData[(x + y * mipMapRasterImage.Width) * 4] = mipMapRasterImage.GetPixel(x, y).B; pixelData[(x + y * mipMapRasterImage.Width) * 4 + 1] = mipMapRasterImage.GetPixel(x, y).G; pixelData[(x + y * mipMapRasterImage.Width) * 4 + 2] = mipMapRasterImage.GetPixel(x, y).R; pixelData[(x + y * mipMapRasterImage.Width) * 4 + 3] = mipMapRasterImage.GetPixel(x, y).A; } } writer.Write(pixelData);//pixeldata dataOffset += dataLenght; } stream.Close(); }
public RasterImage(RasterImage image, int width, int height) { Width = width; Height = height; PixelData = new byte[width * height * 4]; float scaleWidth = (float)image.Width / width; float scaleHeight = (float)image.Height / height; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color pixel = image.GetPixel((int)(x * scaleWidth), (int)(y * scaleHeight)); SetPixel(x, y, pixel); } } }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { pictureBox1.Visible = false; foreach (AoEFile file in barfile.Files) { if (file.fileName == listBox1.SelectedItem.ToString()) { string fileExtension = file.fileName.Substring(file.fileName.Length - 4, 4); if (fileExtension == ".xmb") { XmbFile xmbFile = new XmbFile(barfile.ReadFile(file)); richTextBox1.Text = xmbFile.Read(); } else if (fileExtension == ".ddt") { DdtImage ddt = new DdtImage(barfile.ReadFile(file)); RasterImage image = ddt.ToRasterImage(); if (image != null) { pictureBox1.Visible = true; pictureBox1.Image = image.ToBitmap(image.Width, image.Height); } else { richTextBox1.Text = "Deze \".ddt\" indeling kan niet worden gelezen."; } } else if (fileExtension == ".xml") { richTextBox1.Text = Encoding.UTF8.GetString(barfile.ReadFile(file)); } else { richTextBox1.Text = "Dubbel klik op het bestand om het uit te pakken."; } } } }
RasterImage GetUncompressedRasterImage() { //open stream and read file data MemoryStream mStream = new MemoryStream(FileData); mStream.ReadBytes(6); //skip byte compression = mStream.ReadByte(); //Compression Type if (compression == 1) //uncompressed { int mipLevels = mStream.ReadByte(); //number of Miplevels int imageWidth = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int imageHeight = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int offset = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int dataLenght = BitConverter.ToInt32(mStream.ReadBytes(4), 0); //Get pixeldata mStream.SetPosition(offset); RasterImage image = new RasterImage(imageWidth, imageHeight); for (int y = 0; y < imageHeight; y++) { for (int x = 0; x < imageWidth; x++) { int B = mStream.ReadByte(); int G = mStream.ReadByte(); int R = mStream.ReadByte(); int A = mStream.ReadByte(); image.SetPixel(x, y, Color.FromArgb(A, R, G, B)); } } return(image); } else { return(null); } }
private void listBox1_DoubleClick(object sender, EventArgs e) { foreach (AoEFile file in barfile.Files) { if (file.fileName == listBox1.SelectedItem.ToString()) { FileInfo fileInfo = new FileInfo(filePath); string savePath = fileInfo.DirectoryName + "\\" + file.fileName; FileInfo fileInfoNewFile = new FileInfo(savePath); if (!Directory.Exists(fileInfoNewFile.DirectoryName)) { Directory.CreateDirectory(fileInfoNewFile.DirectoryName); } if (fileInfoNewFile.Extension == ".xmb" && Form1.ConvertXMBOnExtract) { savePath = savePath.Substring(0, savePath.Length - fileInfoNewFile.Extension.Length); savePath += ".xml"; byte[] fileBytes = barfile.ReadFile(file); XmbFile xmbFile = new XmbFile(fileBytes); Stream writeStream = new FileStream(savePath, FileMode.Create); BinaryWriter writer = new BinaryWriter(writeStream); writer.Write(Encoding.UTF8.GetBytes(xmbFile.Read())); writeStream.Close(); } else if (fileInfoNewFile.Extension == ".ddt" && Form1.ConvertDDTOnExtract) { savePath = savePath.Substring(0, savePath.Length - fileInfoNewFile.Extension.Length); byte[] fileBytes = barfile.ReadFile(file); DdtImage ddt = new DdtImage(fileBytes); RasterImage image = ddt.ToRasterImage(); if (Form1.imageConvertingSetting == ImageConvertionType.Tga) { savePath += ".tga"; if (image != null) { image.SaveTga(savePath); } } else if (Form1.imageConvertingSetting == ImageConvertionType.AlphaMapPng) { if (image != null) { image.Save(savePath + "1.png", false); image.SaveAlphaMap(savePath + "A.png"); } } else if (Form1.imageConvertingSetting == ImageConvertionType.Png) { if (image != null) { image.Save(savePath + "0.png", false); } } } else { Stream stream = new FileStream(savePath, FileMode.Create); BinaryWriter writer = new BinaryWriter(stream); writer.Write(barfile.ReadFile(file)); stream.Close(); } } } }
RasterImage GetDXT3RasterImage() { //open stream and read file data MemoryStream mStream = new MemoryStream(FileData); mStream.ReadBytes(6); //skip byte compression = mStream.ReadByte(); //Compression Type if (compression == 8) //DXT3 { int mipLevels = mStream.ReadByte(); //number of Miplevels //get header values int imageWidth = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int imageHeight = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int offset = BitConverter.ToInt32(mStream.ReadBytes(4), 0); int dataLenght = BitConverter.ToInt32(mStream.ReadBytes(4), 0); //Get pixeldata mStream.SetPosition(offset); RasterImage image = new RasterImage(imageWidth, imageHeight); int xOffset = 0; int yOffset = 0; for (int i = 0; i < dataLenght; i += 16) { //Get Alpha values List <byte> alphaBytes = new List <byte>(mStream.ReadBytes(8)); //Get 4 colors UInt16 iC0 = BitConverter.ToUInt16(mStream.ReadBytes(2), 0); //16bit Color c0 5:6:5 RGB UInt16 iC1 = BitConverter.ToUInt16(mStream.ReadBytes(2), 0); //16bit Color c1 5:6:5 RGB Color[] colors = GetColorsFromDXTFormat(iC0, iC1, false); //Calculate the 4 colors //Get 4x4 Pixelblock and set Pixels in bitmap for (int y = 0; y < 4; y++) { List <byte> pointerBits = new List <byte>(ByteAnalyzer.GetBitsFromNumber((int)mStream.ReadByte())); //Convert byte holding 4 2bit Color pointers to bit list List <byte> alphaBits = new List <byte>(ByteAnalyzer.GetBitsFromNumber((alphaBytes[y * 2]))); //Convert bytes holding 2 4bit Alpha value each, to bit list alphaBits.AddRange(ByteAnalyzer.GetBitsFromNumber(alphaBytes[y * 2 + 1])); //Add secon Byte-bits for (int x = 0; x < 4; x++) { int colorPointer = ByteAnalyzer.GetIntFromBits(pointerBits.GetRange(x * 2, 2).ToArray()); //Pointer for pixel byte alpha = (byte)((float)ByteAnalyzer.GetIntFromBits(alphaBits.GetRange(x * 4, 4).ToArray()) / 15 * 255); //Calulate alpha value byte Color color = Color.FromArgb(alpha, colors[colorPointer].R, colors[colorPointer].G, colors[colorPointer].B); image.SetPixel(xOffset + x, yOffset + y, color); } } // set xOffset and yOffset values if (xOffset + 4 < imageWidth) { xOffset += 4; } else { xOffset = 0; yOffset += 4; } } return(image); } else { return(null); } }