private void NewPalette_Click(object sender, EventArgs e) { string filter = ConfigurationManager.AppSettings["PaletteFileSpec"]; string path = FileUploadPrompt(filter); if (String.IsNullOrEmpty(path)) { MessageBox.Show("A complete path might be nice"); hasPalette = false; return; } hasPalette = true; palette = PaletteProcessor.CreatePalette(path); if (hasImage) { RefreshDisplay(); } else if (!first) { MessageBox.Show("Get thee an image"); } }
static void Init() { // Get existing open window or if none, make a new one: PaletteProcessor window = (PaletteProcessor)EditorWindow.GetWindow(typeof(PaletteProcessor)); window.Show(); }
/// <summary> /// Creates new <see cref="PAL"/> instance from a supported image file. /// </summary> /// <param name="buffer">Memory buffer that contains a supported image file.</param> /// <param name="sortColors">Sort colors of the imported palette. By default is <see langword="false"/>.</param> /// <returns>Returns a new <see cref="PAL"/> instance.</returns> /// <remarks>Supported image formats are JPEG, PNG, BMP, GIF and TGA. /// Also supported 256 color PCX images, <see cref="MAP"/> and <see cref="FPG"/> files.</remarks> public static PAL FromImage(byte[] buffer, bool sortColors = false) { PAL pal = PaletteProcessor.ProcessPalette(buffer); if (sortColors) { pal.Sort(); } return(pal); }
public unsafe override void Load(string path, int offset) { Bitmap bp = new Bitmap(path); Int32[,] Bits = PaletteProcessor.BitmapToIntArray(bp); PaletteProcessor.RoundColor5BitsPerChannel(Bits); ColorReductor.ReduceColorsFromBitmap <UnityColorPalette, UnityColorPaletteIndex>(15, Bits); var ts = TileProcessor.GetUniqueTilesPositions(Bits, tileSize.Width, tileSize.Height); UnityColorPalette[] pals = PaletteProcessor.ExtractPalettesFromBitmap <UnityColorPalette, UnityColorPaletteIndex>(Bits, ts.Item1, tileSize.Width, tileSize.Height, 15); var tiles = TileProcessor.GetTiles <UnityColorPalette, IndexedBitmapBufferDisguise>(pals, ts.Item1, ts.Item2, Bits, tileSize.Width, tileSize.Height); int w = bp.Width; int h = bp.Height; foreach (var kvp in tiles) { BitmapBuffer bf = BitmapBuffer.CreateInstance(w, h, pals[0].RealObject.BytesPerColor); Int32[] retbp = new Int32[w * h]; GCHandle BitsHandle = GCHandle.Alloc(retbp, GCHandleType.Pinned); Bitmap finishedBP = new Bitmap(w, h, w * 4, PixelFormat.Format32bppArgb, BitsHandle.AddrOfPinnedObject()); byte[] ret = new byte[retbp.Length * 4]; foreach (var t in kvp.Value) { BitmapBuffer bpb = t.Item4.RealObject.CreateBitmapBuffer(Flip.GetFlip(t.Item2, t.Item3), pals[kvp.Key].RealObject); bf.DrawBitmapBuffer(bpb, t.Item1.X * tileSize.Width, t.Item1.Y * tileSize.Height); } unsafe { fixed(byte *bs = ret) { bf.CopyTo(bs, 0, w - 1, 0, h - 1, 0, 0, 0, 0); } } Buffer.BlockCopy(ret, 0, retbp, 0, ret.Length); finishedBP.Save(Path.GetFileNameWithoutExtension(path) + kvp.Key.Value + ".png", ImageFormat.Png); } }
//TODO : Add commandline args handling static void Main(string[] args) { /* * 1. Open the palette and create a dictionary of name, color * 2. Open image and iterate over all pixels, across columns, then rows * 3. For each pixel, resolve into nearest color from palette and add to the order sheet * NOTE : Transparent pixels don't enter into the count */ string srcImage = "test.png"; string srcPalette = "colours.csv"; string outImage = "paletteAdjusted.png"; Dictionary <string, Color> palette = PaletteProcessor.CreatePalette(srcPalette); Dictionary <string, int> orderSheet = new Dictionary <string, int>(); using (Bitmap src = new Bitmap(srcImage)) { using (Bitmap tar = new Bitmap(src.Width, src.Height)) { for (int x = 0; x < src.Width; x++) { for (int y = 0; y < src.Height; y++) { Color color = src.GetPixel(x, y); // Skip transparent if (color.A == 0) { tar.SetPixel(x, y, color); } else { ColorTuple tup = color.ClosestColor(palette); if (!orderSheet.ContainsKey(tup.Name)) { orderSheet.Add(tup.Name, 1); } else { orderSheet[tup.Name]++; } tar.SetPixel(x, y, tup.ColorValue); } } } //Dump the ordersheet StringBuilder sb = new StringBuilder(); foreach (string name in orderSheet.Keys) { sb.AppendFormat("{0}: {1}\r\n", name, orderSheet[name]); } File.WriteAllText("OrderSheet.txt", sb.ToString()); tar.Save(outImage, src.RawFormat); } } }