Example #1
0
        public static Npck ChangeTextureImages(EmguImage[] images, int[] frames, Palette palOut, Npck original)
        {
            // Get an original texture file to get images and the texture to modify
            long start = original[0].Position;

            Btx0 oriTexture = new Btx0(original[0]);
            original[0].Position = start;

            Btx0 newTexture = new Btx0(original[0]);
            original[0].Position = start;

            // Create importer and remove all images
            TextureImporter importer = new TextureImporter(newTexture);
            importer.RemoveImages();

            // Add images from arguments or original texture if not presents.
            List<int> frameList = frames.ToList();
            for (int i = 0; i < oriTexture.NumTextures; i++) {
                int idx = frameList.IndexOf(i);

                // Set quantization to original palette or argument palette
                Color[] originalPalette = oriTexture.GetPalette(i).GetPalette(0);
                if (idx != -1 && palOut != null)
                    importer.Quantization = new FixedPaletteQuantization(palOut.GetPalette(idx));
                else
                    importer.Quantization = new FixedPaletteQuantization(originalPalette);

                // Keep original color format and name
                string name = oriTexture.GetTextureName(i);
                importer.Format = oriTexture.GetImage(i).Format;

                // Keep original unknown values
                int[] texUnks = oriTexture.GetTextureUnknowns(i);
                int[] palUnks = oriTexture.GetPaletteUnknowns(i);

                // Import :D
                if (idx != -1)
                    importer.AddImage(images[idx], name, texUnks, palUnks, originalPalette);
                else
                    importer.AddImage(oriTexture.CreateBitmap(i), name, texUnks, palUnks);
            }

            // Write the new texture file
            MemoryStream textureStream = new MemoryStream();
            newTexture.Write(textureStream);
            textureStream.Position = 0;

            // Create a copy of the NPCK from the original
            Npck npck = new Npck();
            npck.AddSubfile(textureStream);
            for (int i = 1; i < 6; i++)
                npck.AddSubfile(original[i]);

            return npck;
        }
Example #2
0
        public static Npck[] FromBackgroundImageShareImage(EmguImage[] images, BackgroundImporter importer)
        {
            Npck[] packs = new Npck[images.Length];

            EmguImage combinedImg = images[0].Clone();

            // Concatenate images
            for (int i = 1; i < images.Length; i++)
            {
                combinedImg = combinedImg.ConcateHorizontal(images[i]);
            }

            if (!(importer.Quantization is FixedPaletteQuantization))
            {
                // Get quantization to share palette
                NdsQuantization quantization = new NdsQuantization();
                quantization.Quantizate(combinedImg);
                importer.Quantization = new FixedPaletteQuantization(quantization.Palette);
            }

            // Get the palette and image file that it's shared
            MemoryStream nclrStr = new MemoryStream();
            MemoryStream ncgrStr = new MemoryStream();

            importer.ImportBackground(combinedImg, null, ncgrStr, nclrStr);
            nclrStr.Position = ncgrStr.Position = 0;

            // Get the array of pixel from the image file
            Ncgr ncgr = new Ncgr(ncgrStr);

            Pixel[] fullImage = ncgr.GetPixels();
            ncgrStr.Position = 0;

            // Create packs
            for (int i = 0; i < images.Length; i++)
            {
                MemoryStream nscrStr = new MemoryStream();
                importer.ImportBackgroundShareImage(images[i], fullImage, nscrStr);
                nscrStr.Position = 0;

                // Only first pack file has palette and image files
                if (i == 0)
                {
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr);
                }
                else
                {
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, null, null);
                }
            }

            combinedImg.Dispose();
            return(packs);
        }
Example #3
0
        public static Npck FromBackgroundImage(EmguImage image)
        {
            MemoryStream nclrStr = new MemoryStream();
            MemoryStream ncgrStr = new MemoryStream();
            MemoryStream nscrStr = new MemoryStream();

            BackgroundImporter importer = new BackgroundImporter();

            importer.ImportBackground(image, nscrStr, ncgrStr, nclrStr);

            nclrStr.Position = ncgrStr.Position = nscrStr.Position = 0;
            return(Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr));
        }
Example #4
0
 public static Npck FromBackgroundStreams(Stream nscrStr, Stream ncgrStr, Stream nclrStr)
 {
     Npck npck = new Npck();
     npck.AddSubfile(nclrStr);
     npck.AddSubfile(ncgrStr);
     npck.AddSubfile(null);		// NCBR (NCGR lineal)
     npck.AddSubfile(null);		// NCER
     npck.AddSubfile(null);		// Unknown
     npck.AddSubfile(null);		// NANR
     npck.AddSubfile(nscrStr);
     npck.AddSubfile(null);		// Unknown
     npck.AddSubfile(null);		// Unknown
     return npck;
 }
Example #5
0
        public static Npck[] FromBackgroundImageSharePaletteChangeDepth(string[] images,
                                                                        Npck original, bool partialImage = false)
        {
            EmguImage[] emguImgs = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++)
            {
                emguImgs[i] = new EmguImage(images[i]);
            }

            BackgroundImporter importer = new BackgroundImporter();

            importer.SetOriginalSettings(original[6], original[1], original[0]);
            importer.PartialImage = partialImage;

            // Create packs
            Npck[] packs = new Npck[images.Length];
            for (int i = 0; i < images.Length; i++)
            {
                MemoryStream nclrStr = new MemoryStream();
                MemoryStream ncgrStr = new MemoryStream();
                MemoryStream nscrStr = new MemoryStream();

                if (i > 0)
                {
                    importer.PaletteMode = PaletteMode.Palette16_16;
                    importer.Format      = ColorFormat.Indexed_4bpp;
                }

                importer.ImportBackground(emguImgs[i], nscrStr, ncgrStr, nclrStr);
                nclrStr.Position = ncgrStr.Position = nscrStr.Position = 0;

                // Only first pack file has palette file
                if (i == 0)
                {
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr);
                }
                else
                {
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, null);
                }
            }

            // Dispose images
            foreach (EmguImage emgu in emguImgs)
            {
                emgu.Dispose();
            }

            return(packs);
        }
Example #6
0
        public static Npck FromBackgroundStreams(Stream nscrStr, Stream ncgrStr, Stream nclrStr)
        {
            Npck npck = new Npck();

            npck.AddSubfile(nclrStr);
            npck.AddSubfile(ncgrStr);
            npck.AddSubfile(null);                      // NCBR (NCGR lineal)
            npck.AddSubfile(null);                      // NCER
            npck.AddSubfile(null);                      // Unknown
            npck.AddSubfile(null);                      // NANR
            npck.AddSubfile(nscrStr);
            npck.AddSubfile(null);                      // Unknown
            npck.AddSubfile(null);                      // Unknown
            return(npck);
        }
Example #7
0
        public static Npck FromSpriteStreams(Stream ncerStr, Stream ncgrLinealStr,
                                             Stream ncgrTiledStr, Stream nclrStr, Stream nanrStr)
        {
            Npck npck = new Npck();

            npck.AddSubfile(nclrStr);
            npck.AddSubfile(ncgrTiledStr);
            npck.AddSubfile(ncgrLinealStr);
            npck.AddSubfile(ncerStr);
            npck.AddSubfile(null);                      // Unknown
            npck.AddSubfile(nanrStr);
            npck.AddSubfile(null);                      // NSCR
            npck.AddSubfile(null);                      // Unknown
            npck.AddSubfile(null);                      // Unknown
            return(npck);
        }
Example #8
0
        public static Npck[] FromBackgroundImageSharePalette(EmguImage[] images, BackgroundImporter importer)
        {
            if (!(importer.Quantization is FixedPaletteQuantization))
            {
                // Concatenate images
                EmguImage combinedImg = images[0].Clone();
                for (int i = 1; i < images.Length; i++)
                {
                    combinedImg = combinedImg.ConcateHorizontal(images[i]);
                }

                NdsQuantization quantization = new NdsQuantization();
                quantization.Quantizate(combinedImg);
                importer.Quantization = new FixedPaletteQuantization(quantization.Palette);

                combinedImg.Dispose();
            }

            // Create packs
            Npck[] packs = new Npck[images.Length];
            for (int i = 0; i < images.Length; i++)
            {
                MemoryStream nclrStr = new MemoryStream();
                MemoryStream ncgrStr = new MemoryStream();
                MemoryStream nscrStr = new MemoryStream();

                importer.ImportBackground(images[i], nscrStr, ncgrStr, nclrStr);
                nclrStr.Position = ncgrStr.Position = nscrStr.Position = 0;

                // Only first pack file has palette file
                if (i == 0)
                {
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr);
                }
                else
                {
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, null);
                }
            }

            return(packs);
        }
Example #9
0
        public static Npck ChangeTextureImages(string[] images, int[] frames, Palette palOut, Npck original)
        {
            // Creamos las imágenes
            EmguImage[] emguImages = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++) {
                try {
                    emguImages[i] = new EmguImage(images[i]);
                } catch (Exception ex) {
                    throw new FormatException("Unknown exception with: " + images[i], ex);
                }
            }

            Npck npck = ChangeTextureImages(emguImages, frames, palOut, original);

            // Liberamos recursos
            foreach (EmguImage img in emguImages)
                img.Dispose();

            return npck;
        }
Example #10
0
        public static Npck[] FromBackgroundImageShareImage(string[] images, Npck original)
        {
            EmguImage[] emguImgs = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++)
            {
                emguImgs[i] = new EmguImage(images[i]);
            }

            BackgroundImporter importer = new BackgroundImporter();

            importer.SetOriginalSettings(original[6], original[1], original[0]);
            Npck[] npcks = FromBackgroundImageShareImage(emguImgs, importer);

            // Dispose images
            foreach (EmguImage emgu in emguImgs)
            {
                emgu.Dispose();
            }

            return(npcks);
        }
Example #11
0
        public static Npck FromSpriteImage(string[] images, int[] frames, Npck original)
        {
            EmguImage[] emguImages = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++)
            {
                try {
                    emguImages[i] = new EmguImage(images[i]);
                } catch (Exception ex) {
                    throw new FormatException("Unknown exception with: " + images[i], ex);
                }
            }

            Npck npck = FromSpriteImage(emguImages, frames, original);

            foreach (EmguImage img in emguImages)
            {
                img.Dispose();
            }

            return(npck);
        }
Example #12
0
        public static Npck ChangeTextureImages(string[] images, int[] frames, Palette palOut, Npck original)
        {
            // Creamos las imágenes
            EmguImage[] emguImages = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++)
            {
                try {
                    emguImages[i] = new EmguImage(images[i]);
                } catch (Exception ex) {
                    throw new FormatException("Unknown exception with: " + images[i], ex);
                }
            }

            Npck npck = ChangeTextureImages(emguImages, frames, palOut, original);

            // Liberamos recursos
            foreach (EmguImage img in emguImages)
            {
                img.Dispose();
            }

            return(npck);
        }
Example #13
0
        public static Npck FromBackgroundImage(EmguImage image, Npck original,
                                               bool changePalette = false)
        {
            if (original[0] == null || original[1] == null)
            {
                throw new FormatException(
                          "Can not import image.\n" +
                          "There is not palette or image in the original pack."
                          );
            }

            MemoryStream nclrStr = new MemoryStream();
            MemoryStream ncgrStr = new MemoryStream();
            MemoryStream nscrStr = new MemoryStream();

            // Import image
            BackgroundImporter importer = new BackgroundImporter();

            importer.SetOriginalSettings(original[2], original[1], original[0], changePalette);
            importer.ImportBackground(image, nscrStr, ncgrStr, nclrStr);

            nclrStr.Position = ncgrStr.Position = nscrStr.Position = 0;
            return(Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr));
        }
Example #14
0
        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]));
        }
Example #15
0
        private static void MultiImport(string baseDir, string outputDir, 
			List<ImageInfo> importedList, string xml, bool filterDate)
        {
            if (string.IsNullOrEmpty(xml))
                return;

            XDocument doc = XDocument.Load(xml);
            foreach (XElement entry in doc.Root.Elements("Pack")) {
                // Get mode
                string mode = entry.Element("Mode").Value;

                // Get paths
                bool existImages = true;
                List<string> images   = new List<string>();
                List<ImageInfo> infos = new List<ImageInfo>();
                foreach (XElement ximg in entry.Element("Images").Elements("Image")) {
                    string frame = (ximg.Attribute("frame") != null) ?
                        "_" + ximg.Attribute("frame").Value : "" ;

                    ImageInfo info = new ImageInfo();
                    info.AbsoluteImage = Path.Combine(baseDir, ximg.Value) + frame + M + ".png";
                    info.Filename      = Path.GetFileNameWithoutExtension(ximg.Value);
                    info.PackExtension = (mode != "TextureWithPalette") ? ".n2d" : ".n3d";
                    info.RelativePath  = Path.GetDirectoryName(ximg.Value) + Path.DirectorySeparatorChar;
                    infos.Add(info);

                    images.Add(info.AbsoluteImage);

                    if (!File.Exists(info.AbsoluteImage)) {
                        existImages = false;
                        break;
                    }
                }

                // Import
                Console.Write("|-Importing {0,-45} | ", infos[0].RelativeImage);
                for (int i = 1; i < infos.Count; i++) {
                    Console.WriteLine();
                    Console.Write("            {0,-45} | ", infos[i].RelativeImage);
                }

                // Images still not translated
                if (!existImages) {
                    Console.WriteLine("Skipped: Images not found");
                    continue;
                }

                // If don't match the filter, skip
                if (filterDate) {
                    DateTime referenceDate = File.GetLastWriteTime(outputDir + infos[0].RelativeNewPack);
                    if (!images.Any(f => File.GetLastWriteTime(f) > referenceDate)) {
                        Console.WriteLine("Skipped: date filter");
                        continue;
                    }
                }

                Npck originalPack = new Npck(outputDir + infos[0].RelativePack);
                Npck[] packs = null;
                if (mode == "SharePalette")
                    packs = NpckFactory.FromBackgroundImageSharePalette(images.ToArray(), originalPack);
                else if (mode == "ChangePalette")
                    packs = new Npck[1] { NpckFactory.FromBackgroundImage(images[0], originalPack, true) };
                else if (mode == "ShareImage")
                    packs = NpckFactory.FromBackgroundImageShareImage(images.ToArray(), originalPack);
                else if (mode == "SharePaletteChangeDepth")
                    packs = NpckFactory.FromBackgroundImageSharePaletteChangeDepth(images.ToArray(), originalPack, true);
                else if (mode == "TextureWithPalette") {
                    // Get frames
                    string frame = entry.Element("Images").Elements("Image").First().Attribute("frame").Value;
                    List<int> frames = new List<int>() { Convert.ToInt32(frame) };

                    // Create palette
                    XElement[] xcolors = entry.Element("Palette").Elements("Color").ToArray();
                    Color[] colors = new Color[xcolors.Length];
                    for (int i = 0; i < colors.Length; i++) {
                        colors[i] = new Color();
                        colors[i].Red   = Convert.ToInt32(xcolors[i].Attribute("red").Value);
                        colors[i].Green = Convert.ToInt32(xcolors[i].Attribute("green").Value);
                        colors[i].Blue  = Convert.ToInt32(xcolors[i].Attribute("blue").Value);
                    }
                    Palette palette = new Palette(colors);

                    // Generate pack file
                    packs = new Npck[1];
                    packs[0] = NpckFactory.ChangeTextureImages(images.ToArray(), frames.ToArray(), palette, originalPack);
                } else
                    throw new FormatException(string.Format("Unsopported mode \"{0}\"", mode));

                // Write output
                originalPack.CloseAll();
                for (int i = 0; i < infos.Count; i++) {
                    packs[i].Write(outputDir + infos[i].RelativeNewPack);
                    packs[i].CloseAll();
                }

                importedList.AddRange(infos);
                Console.WriteLine("Successfully");
            }
        }
Example #16
0
        public static Npck FromBackgroundImage(EmguImage image, Npck original, 
			bool changePalette = false)
        {
            if (original[0] == null || original[1] == null)
                throw new FormatException(
                    "Can not import image.\n" +
                    "There is not palette or image in the original pack."
                );

            MemoryStream nclrStr = new MemoryStream();
            MemoryStream ncgrStr = new MemoryStream();
            MemoryStream nscrStr = new MemoryStream();

            // Import image
            BackgroundImporter importer = new BackgroundImporter();
            importer.SetOriginalSettings(original[2], original[1], original[0], changePalette);
            importer.ImportBackground(image, nscrStr, ncgrStr, nclrStr);

            nclrStr.Position = ncgrStr.Position = nscrStr.Position = 0;
            return Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr);
        }
Example #17
0
        public static Npck FromBackgroundImage(string image, Npck original,
			bool changePalette = false)
        {
            using (EmguImage emgu = new EmguImage(image))
                return FromBackgroundImage(emgu, original, changePalette);
        }
Example #18
0
        public static Npck[] FromBackgroundImageShareImage(EmguImage[] images, BackgroundImporter importer)
        {
            Npck[] packs = new Npck[images.Length];

            EmguImage combinedImg = images[0].Clone();
            // Concatenate images
            for (int i = 1; i < images.Length; i++)
                combinedImg = combinedImg.ConcateHorizontal(images[i]);

            if (!(importer.Quantization is FixedPaletteQuantization)) {
                // Get quantization to share palette
                NdsQuantization quantization = new NdsQuantization();
                quantization.Quantizate(combinedImg);
                importer.Quantization = new FixedPaletteQuantization(quantization.Palette);
            }

            // Get the palette and image file that it's shared
            MemoryStream nclrStr = new MemoryStream();
            MemoryStream ncgrStr = new MemoryStream();
            importer.ImportBackground(combinedImg, null, ncgrStr, nclrStr);
            nclrStr.Position = ncgrStr.Position = 0;

            // Get the array of pixel from the image file
            Ncgr ncgr = new Ncgr(ncgrStr);
            Pixel[] fullImage = ncgr.GetPixels();
            ncgrStr.Position = 0;

            // Create packs
            for (int i = 0; i < images.Length; i++) {
                MemoryStream nscrStr = new MemoryStream();
                importer.ImportBackgroundShareImage(images[i], fullImage, nscrStr);
                nscrStr.Position = 0;

                // Only first pack file has palette and image files
                if (i == 0)
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr);
                else
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, null, null);
            }

            combinedImg.Dispose();
            return packs;
        }
Example #19
0
        private static void SearchAndExportBg(string baseDir, string outputDir)
        {
            foreach (string file in Directory.GetFiles(outputDir, "*.n2d", SearchOption.AllDirectories)) {
                string relativePath = file.Replace(outputDir, "");
                string imageName = Path.GetFileNameWithoutExtension(file);
                string imagePath = Path.Combine(baseDir, relativePath, imageName + ".png");

                try {
                    Npck pack = new Npck(file);
                    pack.GetBackgroundImage().Save(imagePath);
                } catch (Exception ex) {
                    Console.WriteLine("Error trying to export: {0} to {1}", relativePath, imagePath);
                    Console.WriteLine("\t" + ex.ToString());
                    continue;
                }

                Console.WriteLine("Exported {0} -> {1}", relativePath, imageName);
            }
        }
Example #20
0
        public static Npck[] FromBackgroundImageSharePalette(EmguImage[] images, BackgroundImporter importer)
        {
            if (!(importer.Quantization is FixedPaletteQuantization)) {
                // Concatenate images
                EmguImage combinedImg = images[0].Clone();
                for (int i = 1; i < images.Length; i++)
                    combinedImg = combinedImg.ConcateHorizontal(images[i]);

                NdsQuantization quantization = new NdsQuantization();
                quantization.Quantizate(combinedImg);
                importer.Quantization = new FixedPaletteQuantization(quantization.Palette);

                combinedImg.Dispose();
            }

            // Create packs
            Npck[] packs = new Npck[images.Length];
            for (int i = 0; i < images.Length; i++) {
                MemoryStream nclrStr = new MemoryStream();
                MemoryStream ncgrStr = new MemoryStream();
                MemoryStream nscrStr = new MemoryStream();

                importer.ImportBackground(images[i], nscrStr, ncgrStr, nclrStr);
                nclrStr.Position = ncgrStr.Position = nscrStr.Position = 0;

                // Only first pack file has palette file
                if (i == 0)
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr);
                else
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, null);
            }

            return packs;
        }
Example #21
0
        public static Npck[] FromBackgroundImageSharePalette(string[] images, Npck original)
        {
            EmguImage[] emguImgs = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++)
                emguImgs[i] = new EmguImage(images[i]);

            BackgroundImporter importer = new BackgroundImporter();
            importer.SetOriginalSettings(original[6], original[1], original[0]);
            Npck[] npcks = FromBackgroundImageSharePalette(emguImgs, importer);

            // Dispose images
            foreach (EmguImage emgu in emguImgs)
                emgu.Dispose();

            return npcks;
        }
Example #22
0
        private static void SingleImport(string imgDir, string outputDir,
			List<ImageInfo> importedList, bool filterDate)
        {
            int count = 0;
            Dictionary<string, int> errors = new Dictionary<string, int>();
            errors.Add("errorPack",  0); 	errors.Add("errorImgs",  0);
            errors.Add("noN2DPack",  0);	errors.Add("noN2DImgs",  0);
            errors.Add("noModPack",  0);	errors.Add("noModImgs",  0);
            errors.Add("inListPack", 0);	errors.Add("inListImgs", 0);
            errors.Add("noSuffPack", 0);    errors.Add("noSuffImgs", 0);

            // Search image: Group of images with same prefix ordered by frame index.
            Dictionary<string, SortedList<int, ImageInfo>> imageGroups =
                SearchImages(imgDir, outputDir);

            // Import!
            Console.WriteLine("Starting importing...");
            foreach (string relative in imageGroups.Keys) {
                // Get paths
                IList<ImageInfo> infos = imageGroups[relative].Values;
                string outFile = outputDir + infos[0].RelativeNewPack;
                string oriFile = outputDir + infos[0].RelativePack;
                string[] imgs  = infos.Select(i => i.AbsoluteImage).ToArray();
                int[] frameIdx = infos.Select(i => i.FrameIndex).ToArray();
                //string[] internalNames = infos.Select(i => i.InternalName).ToArray();

                // If don't match the filter, skip
                if (filterDate) {
                    DateTime referenceDate = File.GetLastWriteTime(outFile);
                    if (!imgs.Any(f => File.GetLastWriteTime(f) > referenceDate)) {
                        Console.WriteLine("|+ Skipped (date filter) {0}", relative);
                        errors["noModPack"]++;
                        errors["noModImgs"] += imgs.Length;
                        continue;
                    }
                }

                // Check if it has been already imported
                if (importedList.Any(i => i.RelativeImage == relative)) {
                    Console.WriteLine("|+ Skipped (already imported) {0}", relative);
                    errors["inListPack"]++;
                    errors["inListImgs"] += imgs.Length;
                    continue;
                }

                // If original file does not exist, skip
                // Odd way to import manually images and skip them here
                if (!File.Exists(oriFile) && File.Exists(outFile)) {
                    Console.WriteLine("|+ Skipped (manual mode) {0}", relative);
                    errors["noN2DPack"]++;
                    errors["noN2DImgs"] += imgs.Length;
                    continue;
                }

                // If the original file does not exists AND there isn't any manual import
                if (!File.Exists(oriFile)) {
                    Console.WriteLine("|+ Skipped (invalid suffix) {0}", relative);
                    errors["noSuffPack"]++;
                    errors["noSuffImgs"] += imgs.Length;
                    continue;
                }

                // Try to import
                Console.Write("|-Importing {0,-45} {1,2} | ", relative, imgs.Length);
                try {
                    Npck ori  = new Npck(oriFile);
                    Npck npck;
                    if (ori.IsSprite)
                        npck = NpckFactory.FromSpriteImage(imgs, frameIdx, ori);
                    else if (ori.IsBackground && imgs.Length == 1)
                        npck = NpckFactory.FromBackgroundImage(imgs[0], ori);
                    else if (ori.IsTexture)
                        npck = NpckFactory.ChangeTextureImages(imgs, frameIdx, ori);
                    else
                        throw new FormatException("Image format not supported");

                    npck.Write(outFile);
                    npck.CloseAll();
                    ori.CloseAll();
                } catch (Exception ex) {
                    Console.WriteLine("Error: {0}", ex.Message);
                    #if DEBUG
                    Console.WriteLine(ex.ToString());
                    #endif
                    errors["errorPack"]++;
                    errors["errorImgs"] += imgs.Length;
                    continue;
                }

                count += imgs.Length;
                importedList.AddRange(infos);
                Console.WriteLine("Successfully");
            }

            Console.WriteLine();
            Console.WriteLine("# Statistics #");
            Console.WriteLine("\tErrors in {0} packages ({1} images)",
                errors["errorPack"], errors["errorImgs"]);
            Console.WriteLine("\tNo N2D file found for {0} packages ({1} images)",
                errors["noN2DPack"], errors["noN2DImgs"]);
            Console.WriteLine("\tInvalid file suffix for {0} packages ({1} images)",
                errors["noSuffPack"], errors["noSuffImgs"]);
            Console.WriteLine("\tFilter skipped {0} packages ({1} images)",
                errors["noModPack"], errors["noModImgs"]);
            Console.WriteLine("\tAlready imported {0} packages ({1} images)",
                errors["inListPack"], errors["inListImgs"]);
            Console.WriteLine("\tImported {0} images successfully!", count);
        }
Example #23
0
        public static Npck[] FromBackgroundImageSharePaletteChangeDepth(string[] images,
			Npck original, bool partialImage = false)
        {
            EmguImage[] emguImgs = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++)
                emguImgs[i] = new EmguImage(images[i]);

            BackgroundImporter importer = new BackgroundImporter();
            importer.SetOriginalSettings(original[6], original[1], original[0]);
            importer.PartialImage = partialImage;

            // Create packs
            Npck[] packs = new Npck[images.Length];
            for (int i = 0; i < images.Length; i++) {
                MemoryStream nclrStr = new MemoryStream();
                MemoryStream ncgrStr = new MemoryStream();
                MemoryStream nscrStr = new MemoryStream();

                if (i > 0) {
                    importer.PaletteMode = PaletteMode.Palette16_16;
                    importer.Format = ColorFormat.Indexed_4bpp;
                }

                importer.ImportBackground(emguImgs[i], nscrStr, ncgrStr, nclrStr);
                nclrStr.Position = ncgrStr.Position = nscrStr.Position = 0;

                // Only first pack file has palette file
                if (i == 0)
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, nclrStr);
                else
                    packs[i] = Npck.FromBackgroundStreams(nscrStr, ncgrStr, null);
            }

            // Dispose images
            foreach (EmguImage emgu in emguImgs)
                emgu.Dispose();

            return packs;
        }
Example #24
0
        public static Npck ChangeTextureImages(EmguImage[] images, int[] frames, Palette palOut, Npck original)
        {
            // Get an original texture file to get images and the texture to modify
            long start = original[0].Position;

            Btx0 oriTexture = new Btx0(original[0]);

            original[0].Position = start;

            Btx0 newTexture = new Btx0(original[0]);

            original[0].Position = start;

            // Create importer and remove all images
            TextureImporter importer = new TextureImporter(newTexture);

            importer.RemoveImages();

            // Add images from arguments or original texture if not presents.
            List <int> frameList = frames.ToList();

            for (int i = 0; i < oriTexture.NumTextures; i++)
            {
                int idx = frameList.IndexOf(i);

                // Set quantization to original palette or argument palette
                Color[] originalPalette = oriTexture.GetPalette(i).GetPalette(0);
                if (idx != -1 && palOut != null)
                {
                    importer.Quantization = new FixedPaletteQuantization(palOut.GetPalette(idx));
                }
                else
                {
                    importer.Quantization = new FixedPaletteQuantization(originalPalette);
                }

                // Keep original color format and name
                string name = oriTexture.GetTextureName(i);
                importer.Format = oriTexture.GetImage(i).Format;

                // Keep original unknown values
                int[] texUnks = oriTexture.GetTextureUnknowns(i);
                int[] palUnks = oriTexture.GetPaletteUnknowns(i);

                // Import :D
                if (idx != -1)
                {
                    importer.AddImage(images[idx], name, texUnks, palUnks, originalPalette);
                }
                else
                {
                    importer.AddImage(oriTexture.CreateBitmap(i), name, texUnks, palUnks);
                }
            }

            // Write the new texture file
            MemoryStream textureStream = new MemoryStream();

            newTexture.Write(textureStream);
            textureStream.Position = 0;

            // Create a copy of the NPCK from the original
            Npck npck = new Npck();

            npck.AddSubfile(textureStream);
            for (int i = 1; i < 6; i++)
            {
                npck.AddSubfile(original[i]);
            }

            return(npck);
        }
Example #25
0
 public static Npck ChangeTextureImages(EmguImage[] images, int[] frames, Npck original)
 {
     return(ChangeTextureImages(images, frames, null, original));
 }
Example #26
0
        public static Npck FromSpriteImage(string[] images, int[] frames, Npck original)
        {
            EmguImage[] emguImages = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++) {
                try {
                    emguImages[i] = new EmguImage(images[i]);
                } catch (Exception ex) {
                    throw new FormatException("Unknown exception with: " + images[i], ex);
                }
            }

            Npck npck = FromSpriteImage(emguImages, frames, original);

            foreach (EmguImage img in emguImages)
                img.Dispose();

            return npck;
        }
Example #27
0
 public static Npck ChangeTextureImages(EmguImage[] images, int[] frames, Npck original)
 {
     return ChangeTextureImages(images, frames, null, original);
 }
Example #28
0
        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]);
        }
Example #29
0
        private static void ExportTextureWithColors(string n3dPath, string outPath, string colors)
        {
            string name = Path.GetFileNameWithoutExtension(n3dPath);
            string path = Path.Combine(outPath, name);

            // Get texture file
            Npck pack = new Npck(n3dPath);
            Btx0 texture = new Btx0(pack[0]);

            // Parse colors
            string[] strColors = colors.Split(' ');
            Color[] newColors = new Color[strColors.Length];
            for (int i = 0; i < strColors.Length; i++) {
                int hexColor = Convert.ToInt32(strColors[i], 16);
                newColors[i] = new Color();
                newColors[i].Alpha = 255;
                newColors[i].Red   = (hexColor >> 00) & 0xFF;
                newColors[i].Green = (hexColor >> 08) & 0xFF;
                newColors[i].Blue  = (hexColor >> 16) & 0xFF;
            }

            // Create and export palette
            Palette palette = new Palette(newColors);
            palette.ToWinPaletteFormat(path + "_palGimp.pal", 0, true);
            palette.ToWinPaletteFormat(path + "_pal.pal", 0, false);
            palette.CreateBitmap(0).Save(path + "_pal.png");

            // For each image, set new palette and export it
            for (int i = 0; i < texture.NumTextures; i++)
                texture.CreateBitmap(i, palette).Save(path + "_" + i.ToString() + ".png");
        }
Example #30
0
 public static Npck FromSpriteStreams(Stream ncerStr, Stream ncgrLinealStr,
     Stream ncgrTiledStr, Stream nclrStr, Stream nanrStr)
 {
     Npck npck = new Npck();
     npck.AddSubfile(nclrStr);
     npck.AddSubfile(ncgrTiledStr);
     npck.AddSubfile(ncgrLinealStr);
     npck.AddSubfile(ncerStr);
     npck.AddSubfile(null);		// Unknown
     npck.AddSubfile(nanrStr);
     npck.AddSubfile(null);		// NSCR
     npck.AddSubfile(null);		// Unknown
     npck.AddSubfile(null);		// Unknown
     return npck;
 }
Example #31
0
 public static Npck FromBackgroundImage(string image, Npck original,
                                        bool changePalette = false)
 {
     using (EmguImage emgu = new EmguImage(image))
         return(FromBackgroundImage(emgu, original, changePalette));
 }
Example #32
0
        private static void SearchPalette(string packPath, string imgPath, int idx)
        {
            // Get new image
            EmguImage newImg = new EmguImage(imgPath);

            // Get original image
            Npck pack = new Npck(packPath);
            Btx0 texture = new Btx0(pack[0]);
            Image originalImg = texture.GetImage(idx);
            Pixel[] pixels = originalImg.GetPixels();

            // For each pixel, set palette color in the position given by original image
            Color[] palette = new Color[originalImg.Format.MaxColors()];
            for (int y = 0; y < newImg.Height; y++) {
                for (int x = 0; x < newImg.Width; x++) {
                    // Get target color
                    Color px = newImg[y, x];

                    // Get palette color index
                    uint index = pixels[y * newImg.Width + x].Info;

                    // If we have already set this color, and it does not match with
                    // this pixel... Error!
                    if (palette[index].Alpha != 0 && !palette[index].Equals(px)) {
                        Console.WriteLine("Can not find a valid color combination");
                        return;
                    }

                    // If the color has not been set, set it!
                    if (palette[index].Alpha == 0)
                        palette[index] = px;
                }
            }

            // Print palette
            Console.WriteLine("Palette found");
            string xmlColor = "          <Color red=\"{0}\" green=\"{1}\" blue=\"{2}\" />";
            foreach (Color c in palette)
                Console.WriteLine(xmlColor, c.Red, c.Green, c.Blue);
        }
Example #33
0
 private static void ExtractPack(string packFile, string outputImage)
 {
     Npck npck = new Npck(packFile);
     npck.GetBackgroundImage().Save(outputImage);
 }
Example #34
0
        private static void ExportTexture(string n3dPath, string outPath)
        {
            string filename = Path.GetFileNameWithoutExtension(n3dPath);

            // Get texture file
            Npck pack = new Npck(n3dPath);
            Btx0 texture = new Btx0(pack[0]);

            // Export images and palettes
            for (int i = 0; i < texture.NumTextures; i++) {
                string name = filename + "_" + i.ToString();
                string path = Path.Combine(outPath, name);
                if (File.Exists(path + ".png"))
                    path += Path.GetRandomFileName();

                texture.CreateBitmap(i).Save(path + ".png");
                texture.GetPalette(i).ToWinPaletteFormat(path + "_gimp.pal", 0, true);
                texture.GetPalette(i).ToWinPaletteFormat(path + ".pal", 0, false);
                texture.GetPalette(i).ToAcoFormat(path + ".aco", 0);
            }
        }