//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); } } }