public EmguImage[] GetSpriteImage() { if (this.NumSubfiles != 9 || this[0] == null || this[1] == null || this[3] == null) { throw new FormatException("The pack does not contain a sprite image."); } this[0].Position = this[1].Position = this[3].Position = 0; Nclr nclr = new Nclr(this[0]); Ncgr ncgr = new Ncgr(this[1]); Ncer ncer = new Ncer(this[3]); this[0].Position = this[1].Position = this[3].Position = 0; EmguImage[] images = new EmguImage[ncer.NumFrames]; for (int i = 0; i < ncer.NumFrames; i++) { images[i] = ncer.CreateBitmap(i, ncgr, nclr); } return(images); }
private static void SpriteInfo(string spriteFile, string imgFile, string palFile, string outputDir) { Console.WriteLine("Reading {0} as NCLR palette...", palFile); Nclr palette = new Nclr(palFile); Console.WriteLine("Reading {0} as NCGR image...", imgFile); Ncgr image = new Ncgr(imgFile); Console.WriteLine("Reading {0} as NCER sprite...", spriteFile); Ncer sprite = new Ncer(spriteFile); if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir); for (int i = 0; i < sprite.NumFrames; i++) { if (!string.IsNullOrEmpty(outputDir)) { string outputFile = Path.Combine(outputDir, "Sprite" + i.ToString() + ".png"); if (File.Exists(outputFile)) File.Delete(outputFile); sprite.CreateBitmap(i, image, palette).Save(outputFile); } } }
private static void ImportTestSprite(string sprFile, string imgFile, string palFile) { FileStream oldPalStr = new FileStream(palFile, FileMode.Open); FileStream oldImgStr = new FileStream(imgFile, FileMode.Open); FileStream oldSprStr = new FileStream(sprFile, FileMode.Open); MemoryStream newPalStr = new MemoryStream(); MemoryStream newImgLinealStr = new MemoryStream(); MemoryStream newImgTiledStr = new MemoryStream(); MemoryStream newSprStr = new MemoryStream(); Nclr nclr = new Nclr(oldPalStr); Ncgr ncgr = new Ncgr(oldImgStr); Ncer ncer = new Ncer(oldSprStr); SpriteImporter importer = new SpriteImporter(); importer.Format = ColorFormat.Indexed_4bpp; importer.ObjectMode = ObjMode.Normal; importer.PaletteMode = PaletteMode.Palette16_16; importer.TileSize = new System.Drawing.Size(64, 64); importer.Quantization = new NdsQuantization() { BackdropColor = new Color(248, 0, 248, 255), Format = ColorFormat.Indexed_4bpp }; importer.Reducer = new SimilarDistanceReducer(); importer.Splitter = new NdsSplitter(1); for (int i = 0; i < ncer.NumFrames; i++) { EmguImage bmp = ncer.CreateBitmap(i, ncgr, nclr); bmp.Save(sprFile + i.ToString() + ".png"); importer.AddFrame(bmp); } importer.Generate(newPalStr, newImgLinealStr, newImgTiledStr, newSprStr); /* if (!Compare(oldPalStr, newPalStr)) { string newPalFile = palFile + ".new"; WriteStream(newPalFile, newPalStr); Console.WriteLine("Palette different... Written to {0}", newPalFile); } if (!Compare(oldImgStr, newImgStr)) { string newImgFile = imgFile + ".new"; WriteStream(newImgFile, newImgStr); Console.WriteLine("Image different... Written to {0}", newImgFile); } if (!Compare(oldSprStr, newSprStr)) { string newSprFile = sprFile + ".new"; WriteStream(newSprFile, newSprStr); Console.WriteLine("Sprite different... Written to {0}", newSprFile); } */ newPalStr.Position = newImgLinealStr.Position = newImgTiledStr.Position = newSprStr.Position = 0; nclr = new Nclr(newPalStr); ncgr = new Ncgr(newImgTiledStr); ncer = new Ncer(newSprStr); for (int i = 0; i < ncer.NumFrames; i++) ncer.CreateBitmap(i, ncgr, nclr).Save(sprFile + i.ToString() + "m.png"); oldPalStr.Close(); oldImgStr.Close(); oldSprStr.Close(); newPalStr.Close(); newImgTiledStr.Close(); newImgLinealStr.Close(); newSprStr.Close(); }
public static Npck FromSpriteImage(EmguImage[] images, int[] frames, Npck original) { SpriteImporter importer = new SpriteImporter(); MemoryStream nclrStr = new MemoryStream(); MemoryStream ncgrLinealStr = new MemoryStream(); MemoryStream ncgrTiledStr = new MemoryStream(); MemoryStream ncerStr = new MemoryStream(); // Create sprites images to import // those sprite that have not been exported (they didn't have text) if (original[0] != null) { Nclr nclr = new Nclr(original[0]); Ncgr ncgr = new Ncgr(original[1] == null ? original[2] : original[1]); Ncer ncer = new Ncer(original[3]); // Set old settings importer.DispCnt = ncgr.RegDispcnt; importer.Quantization = new ManyFixedPaletteQuantization(nclr.GetPalettes()); importer.OriginalPalettes = nclr.GetPalettes(); importer.Format = nclr.Format; if (nclr.Format == ColorFormat.Indexed_8bpp) { importer.PaletteMode = PaletteMode.Palette256_1; } else { importer.PaletteMode = PaletteMode.Palette16_16; } int idx = 0; for (int i = 0; i < ncer.NumFrames; i++) { if (frames.Contains(i)) { importer.AddFrame(images[idx++]); } else if (ncer != null) { importer.AddFrame(ncer.CreateBitmap(i, ncgr, nclr), ncer.GetFrame(i)); } } } else { foreach (EmguImage img in images) { importer.AddFrame(img); } } // TEMP: Check if the files were present if (original[0] == null) { Console.Write("(Warning: No palette) "); } if (original[1] == null) { //Console.Write("(Warning: No HImage) "); ncgrTiledStr = null; } if (original[2] == null) { //Console.Write("(Warning: No LImage) "); ncgrLinealStr = null; } if (original[3] == null) { Console.Write("(Warning: No sprite) "); } if (original[5] == null) { Console.Write("(Warning: No animation) "); } importer.Generate(nclrStr, ncgrLinealStr, ncgrTiledStr, ncerStr); nclrStr.Position = 0; ncerStr.Position = 0; if (ncgrTiledStr != null) { ncgrTiledStr.Position = 0; } if (ncgrLinealStr != null) { ncgrLinealStr.Position = 0; } return(Npck.FromSpriteStreams(ncerStr, ncgrLinealStr, ncgrTiledStr, nclrStr, original[5])); }
public EmguImage[] GetSpriteImage() { if (this.NumSubfiles != 9 || this[0] == null || this[1] == null || this[3] == null) throw new FormatException("The pack does not contain a sprite image."); this[0].Position = this[1].Position = this[3].Position = 0; Nclr nclr = new Nclr(this[0]); Ncgr ncgr = new Ncgr(this[1]); Ncer ncer = new Ncer(this[3]); this[0].Position = this[1].Position = this[3].Position = 0; EmguImage[] images = new EmguImage[ncer.NumFrames]; for (int i = 0; i < ncer.NumFrames; i++) images[i] = ncer.CreateBitmap(i, ncgr, nclr); return images; }
public static Npck FromSpriteImage(EmguImage[] images, int[] frames, Npck original) { SpriteImporter importer = new SpriteImporter(); MemoryStream nclrStr = new MemoryStream(); MemoryStream ncgrLinealStr = new MemoryStream(); MemoryStream ncgrTiledStr = new MemoryStream(); MemoryStream ncerStr = new MemoryStream(); // Create sprites images to import // those sprite that have not been exported (they didn't have text) if (original[0] != null) { Nclr nclr = new Nclr(original[0]); Ncgr ncgr = new Ncgr(original[1] == null ? original[2] : original[1]); Ncer ncer = new Ncer(original[3]); // Set old settings importer.DispCnt = ncgr.RegDispcnt; importer.Quantization = new ManyFixedPaletteQuantization(nclr.GetPalettes()); importer.OriginalPalettes = nclr.GetPalettes(); importer.Format = nclr.Format; if (nclr.Format == ColorFormat.Indexed_8bpp) importer.PaletteMode = PaletteMode.Palette256_1; else importer.PaletteMode = PaletteMode.Palette16_16; int idx = 0; for (int i = 0; i < ncer.NumFrames; i++) { if (frames.Contains(i)) importer.AddFrame(images[idx++]); else if (ncer != null) importer.AddFrame(ncer.CreateBitmap(i, ncgr, nclr), ncer.GetFrame(i)); } } else { foreach (EmguImage img in images) importer.AddFrame(img); } // TEMP: Check if the files were present if (original[0] == null) Console.Write("(Warning: No palette) "); if (original[1] == null) { //Console.Write("(Warning: No HImage) "); ncgrTiledStr = null; } if (original[2] == null) { //Console.Write("(Warning: No LImage) "); ncgrLinealStr = null; } if (original[3] == null) Console.Write("(Warning: No sprite) "); if (original[5] == null) Console.Write("(Warning: No animation) "); importer.Generate(nclrStr, ncgrLinealStr, ncgrTiledStr, ncerStr); nclrStr.Position = 0; ncerStr.Position = 0; if (ncgrTiledStr != null) ncgrTiledStr.Position = 0; if (ncgrLinealStr != null) ncgrLinealStr.Position = 0; return Npck.FromSpriteStreams(ncerStr, ncgrLinealStr, ncgrTiledStr, nclrStr, original[5]); }
public void Generate(Stream paletteStr, Stream imgLinealStr, Stream imgTiledStr, Stream spriteStr) { Pixel[] pixelsLin; Pixel[] pixelsHori; Color[][] palettes; this.CreateData(out pixelsLin, out pixelsHori, out palettes); // Get frame list Frame[] frames = new Frame[this.frameData.Count]; for (int i = 0; i < this.frameData.Count; i++) frames[i] = this.frameData[i].Item1; // Create palette format Nclr nclr = new Nclr() { Extended = false, Format = this.Format }; nclr.SetPalette(palettes); // Create image format Ncgr ncgrLineal = new Ncgr() { RegDispcnt = this.DispCnt, Unknown = this.UnknownChar, InvalidSize = true }; ncgrLineal.Width = (pixelsLin.Length > 256) ? 256 : pixelsLin.Length; ncgrLineal.Height = (int)Math.Ceiling(pixelsLin.Length / (double)ncgrLineal.Width); ncgrLineal.SetData(pixelsLin, PixelEncoding.Lineal, this.Format, this.TileSize); Ncgr ncgrTiled = new Ncgr() { RegDispcnt = this.DispCnt, Unknown = this.UnknownChar, InvalidSize = true }; ncgrTiled.Width = ncgrLineal.Width; ncgrTiled.Height = ncgrLineal.Height; if (ncgrTiled.Height % this.TileSize.Height != 0) ncgrTiled.Height += this.TileSize.Height - (ncgrTiled.Height % this.TileSize.Height); ncgrTiled.SetData(pixelsHori, PixelEncoding.HorizontalTiles, this.Format, this.TileSize); // Create sprite format Ncer ncer = new Ncer() { TileSize = 128, IsRectangularArea = this.UseRectangularArea }; ncer.SetFrames(frames); // Write data if (paletteStr != null) nclr.Write(paletteStr); if (imgLinealStr != null) ncgrLineal.Write(imgLinealStr); if (imgTiledStr != null) ncgrTiled.Write(imgTiledStr); if (spriteStr != null) ncer.Write(spriteStr); }