public EmguImage GetBackgroundImage() { if (this.NumSubfiles != 9 || this[0] == null || this[1] == null || this[6] == null) { throw new FormatException("The pack does not contain a background image."); } this[0].Position = this[1].Position = this[6].Position = 0; Nclr nclr = new Nclr(this[0]); Ncgr ncgr = new Ncgr(this[1]); Nscr nscr = new Nscr(this[6]); this[0].Position = this[1].Position = this[6].Position = 0; return(nscr.CreateBitmap(ncgr, nclr)); }
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 PaletteInfo(string file, string outputDir) { Console.WriteLine("Reading {0} as NCLR palette...", file); Nclr palette = new Nclr(file); Console.WriteLine("\t* Version: {0}", palette.NitroData.VersionS); Console.WriteLine("\t* Contains PCMP section: {0}", palette.NitroData.Blocks.ContainsType("PCMP")); Console.WriteLine("\t* Number of palettes: {0}", palette.NumPalettes); if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir); for (int i = 0; i < palette.NumPalettes; i++) { Console.WriteLine("\t+ Palette {0}: {1} colors", i, palette.GetPalette(i).Length); if (!string.IsNullOrEmpty(outputDir)) { string outputFile = Path.Combine(outputDir, "Palette" + i.ToString() + ".png"); if (File.Exists(outputFile)) File.Delete(outputFile); palette.CreateBitmap(i).Save(outputFile); } } }
private static void MapInfo(string mapFile, string imgFile, string palFile, string outputFile) { 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 NSCR map...", mapFile); Nscr map = new Nscr(mapFile); Console.WriteLine("\t* Version: {0}", map.NitroData.VersionS); Console.WriteLine("\t* Height: {0}", map.Height); Console.WriteLine("\t* Width: {0}", map.Width); Console.WriteLine("\t* BG Mode: {0}", map.BgMode); Console.WriteLine("\t* Palette Mode: {0}", map.PaletteMode); map.CreateBitmap(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(); }
private static void ImportTestBackground(string mapFile, string imgFile, string palFile) { FileStream oldPalStr = new FileStream(palFile, FileMode.Open); FileStream oldImgStr = new FileStream(imgFile, FileMode.Open); FileStream oldMapStr = new FileStream(mapFile, FileMode.Open); MemoryStream newPalStr = new MemoryStream(); MemoryStream newImgStr = new MemoryStream(); MemoryStream newMapStr = new MemoryStream(); Nclr nclr = new Nclr(oldPalStr); Ncgr ncgr = new Ncgr(oldImgStr); Nscr nscr = new Nscr(oldMapStr); EmguImage bmp = nscr.CreateBitmap(ncgr, nclr); bmp.Save(mapFile + ".png"); BackgroundImporter importer = new BackgroundImporter(); importer.ImportBackground(bmp, newMapStr, newImgStr, newPalStr); 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(oldMapStr, newMapStr)) { string newMapFile = mapFile + ".new"; WriteStream(newMapFile, newMapStr); Console.WriteLine("Map different... Written to {0}", newMapFile); } newPalStr.Position = newImgStr.Position = newMapStr.Position = 0; nclr = new Nclr(newPalStr); ncgr = new Ncgr(newImgStr); nscr = new Nscr(newMapStr); nscr.CreateBitmap(ncgr, nclr).Save(mapFile + "m.png"); oldPalStr.Close(); oldImgStr.Close(); oldMapStr.Close(); newPalStr.Close(); newImgStr.Close(); newMapStr.Close(); }
private static void ImageInfo(string imgFile, string palFile, string outputFile) { 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("\t* Version: {0}", image.NitroData.VersionS); Console.WriteLine("\t* Contains CPOS section: {0}", image.NitroData.Blocks.ContainsType("CPOS")); Console.WriteLine("\t* Height: {0}", image.Height); Console.WriteLine("\t* Width: {0}", image.Width); Console.WriteLine("\t* Format: {0}", image.Format); Console.WriteLine("\t* Pixel encoding: {0}", image.PixelEncoding); image.CreateBitmap(palette, 0).Save(outputFile); }
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 EmguImage GetBackgroundImage() { if (this.NumSubfiles != 9 || this[0] == null || this[1] == null || this[6] == null) throw new FormatException("The pack does not contain a background image."); this[0].Position = this[1].Position = this[6].Position = 0; Nclr nclr = new Nclr(this[0]); Ncgr ncgr = new Ncgr(this[1]); Nscr nscr = new Nscr(this[6]); this[0].Position = this[1].Position = this[6].Position = 0; return nscr.CreateBitmap(ncgr, nclr); }
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 SetOriginalSettings(Stream mapStr, Stream imgStr, Stream palStr, bool changePalette = false) { // Set original palette settings if (palStr != null) { Nclr nclr = new Nclr(palStr); this.ExtendedPalette = nclr.Extended; if (!changePalette) this.Quantization = new FixedPaletteQuantization(nclr.GetPalette(0)); } // Set original image settings if the file is not compressed if (imgStr != null && imgStr.ReadByte() == 0x52) { imgStr.Position -= 1; Ncgr ncgr = new Ncgr(imgStr); this.DispCnt = ncgr.RegDispcnt; this.UnknownChar = ncgr.Unknown; this.Format = ncgr.Format; this.IncludeCpos = ncgr.HasCpos; this.PixelEncoding = ncgr.PixelEncoding; } // Set original map settings if (mapStr != null && mapStr.ReadByte() == 0x52) { mapStr.Position -= 1; Nscr nscr = new Nscr(mapStr); this.BgMode = nscr.BgMode; this.PaletteMode = nscr.PaletteMode; this.TileSize = nscr.TileSize; } }
/// <summary> /// Import a background image creating and writing a NSCR, NCGR and NCLR files to the streams passed. /// </summary> /// <param name="imgPath">Image path.</param> /// <param name="mapStr">Map stream output.</param> /// <param name="imgStr">Image stream output.</param> /// <param name="palStr">Pal strream output.</param> public void ImportBackground(EmguImage newImg, Stream mapStr, Stream imgStr, Stream palStr) { if (newImg == null) throw new ArgumentNullException(); int width = newImg.Width; int height = newImg.Height; int maxColors = this.Format.MaxColors(); Pixel[] pixels; Color[] palette; List<int> mapPalette = new List<int>(); bool is16ColFixed = (Format == ColorFormat.Indexed_4bpp) && (PaletteMode == PaletteMode.Palette16_16) && (Quantization is FixedPaletteQuantization); if (!is16ColFixed) { // Quantizate image -> get pixels and palette this.Quantization.Quantizate(newImg); pixels = this.Quantization.GetPixels(this.PixelEncoding); palette = this.Quantization.Palette; if (palette.Length > maxColors) throw new FormatException(string.Format("The image has more than {0} colors", maxColors)); } else { Console.Write("(16 Color fixed!) "); palette = this.Quantization.Palette; ManyFixedPaletteQuantization quant = new ManyFixedPaletteQuantization( this.Quantization.Palette.Split(16).ToArray()); List<Pixel> pixelList = new List<Pixel>(); for (int y = 0; y < newImg.Height; y += this.TileSize.Height) { for (int x = 0; x < newImg.Width; x += this.TileSize.Width) { Rectangle subArea = new Rectangle(x, y, this.TileSize.Width, this.TileSize.Height); EmguImage subImage = newImg.Copy(subArea); quant.Quantizate(subImage); mapPalette.Add(quant.SelectedPalette); pixelList.AddRange(quant.GetPixels(PixelEncoding.Lineal)); } } pixels = pixelList.ToArray(); } // Create palette format Nclr nclr = new Nclr() { Extended = this.ExtendedPalette }; nclr.SetData(palette, this.Format); // Create map from pixels Nscr nscr = new Nscr() { TileSize = this.TileSize, Width = width, Height = height, BgMode = this.BgMode, PaletteMode = this.PaletteMode }; if (!is16ColFixed) { nscr.PaletteMode = (this.Format == ColorFormat.Indexed_4bpp) ? PaletteMode.Palette16_16 : PaletteMode.Palette256_1; pixels = nscr.CreateMap(pixels); } else { nscr.PaletteMode = PaletteMode.Palette16_16; pixels = nscr.CreateMap(pixels, mapPalette.ToArray()); } if (this.PartialImage) { // As the image won't expand to all the screen, // The first tile must be transparent int tilesizeLength = this.TileSize.Width * this.TileSize.Height; Pixel[] newPixels = new Pixel[pixels.Length + tilesizeLength]; // New transparent pixels for (int i = 0; i < tilesizeLength; i++) newPixels[i] = new Pixel(0, 255, true); // Image pixels Array.Copy(pixels, 0, newPixels, tilesizeLength, pixels.Length); pixels = newPixels; // Update map info MapInfo[] mapInfo = nscr.GetMapInfo(); for (int i = 0; i < mapInfo.Length; i++) { mapInfo[i] = new MapInfo( mapInfo[i].TileIndex + 1, mapInfo[i].PaletteIndex, mapInfo[i].FlipX, mapInfo[i].FlipY); } nscr.SetMapInfo(mapInfo); } // Create image format Ncgr ncgr = new Ncgr() { RegDispcnt = this.DispCnt, Unknown = this.UnknownChar }; ncgr.Width = (pixels.Length > 256) ? 256 : pixels.Length; ncgr.Height = (int)Math.Ceiling(pixels.Length / (double)ncgr.Width); if (ncgr.Height % this.TileSize.Height != 0) ncgr.Height += this.TileSize.Height - (ncgr.Height % this.TileSize.Height); ncgr.SetData(pixels, this.PixelEncoding, this.Format, this.TileSize); // Write data if (palStr != null) nclr.Write(palStr); if (imgStr != null) ncgr.Write(imgStr); if (mapStr != null) nscr.Write(mapStr); }
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); }