public static Color[] createPaletteForImage(Bitmap b, int palLen) { List <Bitmap> bl = new List <Bitmap>(); bl.Add(b); ImageIndexer i = new ImageIndexer(bl, palLen, true, 0); return(i.palettes[0]); }
public bool import() { checkStuff(); OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = LanguageManager.Get("Filters", "png"); if (ofd.ShowDialog() == DialogResult.Cancel) { return(false); } calcSizes(); Bitmap b = new Bitmap(ofd.FileName); if (b.Width != tx || b.Height != ty) { throw new Exception("Wrong input image size"); } List <Bitmap> bl = new List <Bitmap>(); for (int i = 0; i < pals.Count; i++) { Bitmap bbb = new Bitmap(b.Width, tys); Graphics bbbgfx = Graphics.FromImage(bbb); bbbgfx.DrawImage(b, 0, -i * tys, b.Width, b.Height); bl.Add(bbb); } b.Dispose(); ImageIndexer ii = new ImageIndexer(bl, pals[0].pal.Length, true, 0); int x = 0; foreach (PixelPalettedImage i in imgs) { i.setPixelData(ii.imageData, x, 0); x += i.getWidth(); } for (int i = 0; i < pals.Count; i++) { pals[i].pal = ii.palettes[i]; } return(false); }
public int getClosestColor(Color c) { if (c.A == 0) { return(0); } int bestInd = 1; float bestDif = ImageIndexer.colorDifferenceWithoutAlpha(pal[1], c); for (int i = 1; i < pal.Length; i++) { float d = ImageIndexer.colorDifferenceWithoutAlpha(pal[i], c); if (d < bestDif) { bestDif = d; bestInd = i; } } return(bestInd); }
public override void replaceImgAndPal(Bitmap b, Palette p) { p.pal = ImageIndexer.createPaletteForImage(b, p.pal.Length); replaceWithPal(b, p); }
private void importPNGButton_Click(object sender, EventArgs e) { getFiles(); if (GFXFile == null) { return; } if (PalFile == null) { return; } if (LayoutFile == null) { return; } // Create a local copy because the global variables could be overwritten while the background is importing File myGFXFile = GFXFile; File myPalFile = PalFile; File myLayoutFile = LayoutFile; int offs = bg.topLayer ? 256 : 576; int palOffs = bg.topLayer ? 8 : 10; if (bg.mappedTileset) { offs = 192; palOffs = 2; } OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = LanguageManager.Get("Filters", "png"); ofd.CheckFileExists = true; if (ofd.ShowDialog() != DialogResult.OK) { return; } string filename = ofd.FileName; Bitmap b = new Bitmap(filename); if (b.Size != new Size(512, 512)) { throw new Exception("Wrong image size"); } BgPNGImportPrompt bgPrompt = new BgPNGImportPrompt(bg.topLayer, bg.mappedTileset); if (!bgPrompt.finished) { return; } ImageTiler ti; if (bg.topLayer) { bgPrompt.bgFirstTileOffset = 0; ti = new ImageTiler(b, new Size(256, bgPrompt.fgHeightInPixels), new Size(512, 512), 50); } else { ti = new ImageTiler(b, new Size(256, bgPrompt.bgHeightInPixels), new Size(512, 512), 50); } Color[] palette = ImageIndexer.createPaletteForImage(b); ByteArrayOutputStream oo = new ByteArrayOutputStream(); for (int i = 0; i < palette.Length; i++) { oo.writeUShort(NSMBTileset.toRGB15(palette[i])); } ByteArrayOutputStream offsetBitmapData = new ByteArrayOutputStream(); for (int i = 0; i < 256 * bgPrompt.bgFirstTileOffset; i++) { offsetBitmapData.writeByte(0); } //byte[] newBitmapData = ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette); byte[] newBitmapData = new byte[offsetBitmapData.getArray().Length + ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette).Length]; Buffer.BlockCopy(offsetBitmapData.getArray(), 0, newBitmapData, 0, offsetBitmapData.getArray().Length); Buffer.BlockCopy(ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette), 0, newBitmapData, offsetBitmapData.getArray().Length, ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette).Length); myPalFile.beginEdit(this); myPalFile.replace(ROM.LZ77_Compress(oo.getArray()), this); myPalFile.endEdit(this); myGFXFile.beginEdit(this); myGFXFile.replace(ROM.LZ77_Compress(newBitmapData), this); myGFXFile.endEdit(this); b.Dispose(); ByteArrayOutputStream layout = new ByteArrayOutputStream(); for (int y = 0; y < ti.heightTileCount; y++) { for (int x = 0; x < ti.widthTileCount; x++) { layout.writeUShort((ushort)((ti.tileMap[x, y] + offs + (bgPrompt.bgFirstTileOffset * 4)) | (palOffs << 12))); } } myLayoutFile.beginEdit(this); myLayoutFile.replace(ROM.LZ77_Compress(layout.getArray()), this); myLayoutFile.endEdit(this); }