public Bitmap GetTextureBitmap(int startPixel, System.Drawing.Color[] palette, uint width, uint height) { Bitmap Texture = new Bitmap((int)width, (int)height); LockBitmap lockBitmap = new LockBitmap(Texture); lockBitmap.LockBits(); ushort textW2 = (ushort)(Texture.Width * 2); ushort textW4 = (ushort)(Texture.Width * 4); for (ushort x = 0; x < width; x++) { byte xMOD16 = (byte)(x & 15); byte xMOD8 = (byte)(x & 7); byte xOVER16 = (byte)(x >> 4); for (ushort y = 0; y < height; y++) { int offset = 32 * (xOVER16) + (4 * (xMOD16)) - (30 * ((xMOD16) >> 3)) + ((System.Byte)(y & 1)) * textW2 + ((System.Byte)(y >> 2)) * textW4 + ((((System.Byte)(y & 3)) >> 1) + (((xMOD8) >> 2) * -1 + (1 - ((xMOD8) >> 2))) * 16 * (((System.Byte)(y & 3)) >> 1) + (((xMOD8) >> 2) * -1 + (1 - ((xMOD8) >> 2))) * 16 * (((System.Byte)(y & 7)) >> 2)) * (1 - (((System.Byte)(y & 7)) >> 2) * (((System.Byte)(y & 3)) >> 1)) + (((System.Byte)(y & 7)) >> 2) * (((System.Byte)(y & 3)) >> 1); if (startPixel + offset < timBinary.Buffer.Length) { lockBitmap.SetPixel(x, y, palette[timBinary.Buffer[startPixel + offset]]); } } } lockBitmap.UnlockBits(); return(Texture); }
public Bitmap GetPatchBitmap(int startPixel, System.Drawing.Color[] palette, uint width, uint height) { Bitmap Texture = new Bitmap((int)width, (int)height); LockBitmap lockBitmap = new LockBitmap(Texture); lockBitmap.LockBits(); for (ushort x = 0; x < width; x++) { for (ushort y = 0; y < height; y++) { lockBitmap.SetPixel(x, y, palette[timBinary.Buffer[startPixel + (y * width) + x]]); } } lockBitmap.UnlockBits(); return(Texture); }
public void UpdateBitmaps(string name) { if (Directory.Exists(tempPath + "Okiaros")) { foreach (string currFile in Directory.GetFiles(tempPath + "Okiaros")) { File.Delete(currFile); } } else { Directory.CreateDirectory(tempPath + "Okiaros"); } string[] pngs = new string[Bitmaps.Count]; for (int i = 0; i < pngs.Length; i++) { pngs[i] = name + @"\texture-" + i.ToString("d3") + ".png"; } for (int i = 0; i < Bitmaps.Count; i++) { FileStream cFS = new FileStream(pngs[i], System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); Bitmaps[i] = new Bitmap(cFS); cFS.Close(); } var palettes = new List <string>(0); var toQuant = new List <Bitmap>(0); var filenames = new List <string>(0); for (int i = 0; i < paletteAddresses.Count; i++) { if (!palettes.Contains(paletteAddresses[i].ToString("X"))) { toQuant.Add(Bitmaps[i]); palettes.Add(paletteAddresses[i].ToString("X")); filenames.Add(paletteAddresses[i].ToString() + "@" + textureAddresses[i].ToString() + ",0,0," + Bitmaps[i].Width.ToString() + "," + Bitmaps[i].Height.ToString()); } else { for (int k = 0; k < paletteAddresses.Count; k++) { if (paletteAddresses[i].ToString() == paletteAddresses[k].ToString()) { int wi = toQuant[k].Width; int he = toQuant[k].Height; if (Bitmaps[i].Height > he) { he = Bitmaps[i].Height; } wi = wi + Bitmaps[i].Width; var merge = new Bitmap(wi, he); var mergeGr = Graphics.FromImage(merge); mergeGr.DrawImage(toQuant[k], new Rectangle(0, 0, toQuant[k].Width, toQuant[k].Height)); mergeGr.DrawImage(Bitmaps[i], new Rectangle(toQuant[k].Width, 0, Bitmaps[i].Width, Bitmaps[i].Height)); filenames[k] = filenames[k] + "@" + textureAddresses[i].ToString() + "," + toQuant[k].Width.ToString() + ",0," + Bitmaps[i].Width.ToString() + "," + Bitmaps[i].Height.ToString(); toQuant[k] = merge; k = paletteAddresses.Count; } } } } pngs = Directory.GetFiles(tempPath + "Okiaros", "*.png"); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "pngquant.exe"; startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.Arguments = "pngquant.exe "; string[] toQuantFilenames = new string[toQuant.Count]; for (int i = 0; i < toQuant.Count; i++) { toQuantFilenames[i] = tempPath + @"Okiaros\" + filenames[i]; startInfo.Arguments += "\"" + toQuantFilenames[i] + ".png" + "\" "; toQuant[i].Save(toQuantFilenames[i] + ".png"); } Process.Start(startInfo); while (Process.GetProcessesByName("pngquant").Length > 0) { } for (int i = 0; i < toQuantFilenames.Length; i++) { File.Delete(toQuantFilenames[i] + ".png"); toQuantFilenames[i] += "-fs8.png"; } for (int g = 0; g < toQuantFilenames.Length; g++) { string curr = toQuantFilenames[g]; string filename_ = Path.GetFileName(curr).Split('-')[0]; string[] bounds = filename_.Split('@'); int newPaletteAddress = (int)(int.Parse(bounds[0])); var stream = new System.IO.FileStream(curr, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); var bitm2Palette = new Bitmap(stream); var decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); var bitmapSource = decoder.Frames[0]; var palette = new List <System.Windows.Media.Color>(bitmapSource.Palette.Colors); while (palette.Count < 256) { palette.Add(System.Windows.Media.Colors.Black); } /*if (checkBox1.Checked) * palette.Sort((System.Windows.Media.Color left, System.Windows.Media.Color right) => (BrightNess(left)).CompareTo(BrightNess(right)));*/ for (int c = 0; c < 256; c++) { int colorSlot = ((64 * ((c / 8) % 2) + ((c / 32) * 128) + (8 * ((c / 16) % 2)) + (c % 8)) * 4); if (colorSlot < timBinary.Buffer.Length - 3) { timBinary.Buffer[newPaletteAddress + colorSlot] = (byte)palette[c].R; timBinary.Buffer[newPaletteAddress + colorSlot + 1] = (byte)palette[c].G; timBinary.Buffer[newPaletteAddress + colorSlot + 2] = (byte)palette[c].B; timBinary.Buffer[newPaletteAddress + colorSlot + 3] = (byte)((palette[c].A + 1) / 2); } } for (int i = 1; i < bounds.Length; i++) { string[] splited = bounds[i].Split(','); int pixAddress = (int)(int.Parse(splited[0])); Bitmap CropBitmap = bitm2Palette.Clone(new Rectangle(int.Parse(splited[1]), int.Parse(splited[2]), int.Parse(splited[3]), int.Parse(splited[4])), System.Drawing.Imaging.PixelFormat.Format32bppArgb); LockBitmap CropBitmapLK = new LockBitmap(CropBitmap); CropBitmapLK.LockBits(); if (dmyCount > 0 && pixAddress >= DMY) { for (ushort x = 0; x < CropBitmap.Width; x++) { for (ushort y = 0; y < CropBitmap.Height; y++) { System.Windows.Media.Color currCol = CropBitmapLK.GetPixel(x, y); int pos = pixAddress + (y * CropBitmap.Width) + x; if (pos > timBinary.Buffer.Length - 1) { pos = timBinary.Buffer.Length - 1; } timBinary.Buffer[pos] = (byte)palette.IndexOf(currCol); //433 } } } else { int textW2 = (CropBitmap.Width * 2); int textW4 = (CropBitmap.Width * 4); for (int x = 0; x < CropBitmap.Width; x++) { ushort xMOD16 = (ushort)(x % 16); ushort xMOD8 = (ushort)(x % 8); ushort xOVER16 = (ushort)(x / 16); for (int y = 0; y < CropBitmap.Height; y++) { System.Windows.Media.Color currCol = CropBitmapLK.GetPixel(x, y); ushort yOVER4 = (ushort)(y / 4); ushort yMOD2 = (ushort)(y % 2); ushort yMOD4 = (ushort)(y % 4); ushort yMOD8 = (ushort)(y % 8); int offset = 32 * (xOVER16) + (4 * (xMOD16)) - (30 * ((xMOD16) >> 3)) + ((System.Byte)(y & 1)) * textW2 + ((System.Byte)(y >> 2)) * textW4 + ((((System.Byte)(y & 3)) >> 1) + (((xMOD8) >> 2) * -1 + (1 - ((xMOD8) >> 2))) * 16 * (((System.Byte)(y & 3)) >> 1) + (((xMOD8) >> 2) * -1 + (1 - ((xMOD8) >> 2))) * 16 * (((System.Byte)(y & 7)) >> 2)) * (1 - (((System.Byte)(y & 7)) >> 2) * (((System.Byte)(y & 3)) >> 1)) + (((System.Byte)(y & 7)) >> 2) * (((System.Byte)(y & 3)) >> 1); if (pixAddress + offset < timBinary.Buffer.Length) { timBinary.Buffer[pixAddress + offset] = (byte)palette.IndexOf(currCol); } } } } } stream.Close(); stream = null; decoder = null; bitmapSource = null; palette = null; toQuant = null; filenames = null; } }