Пример #1
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");
            }
        }
Пример #2
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);
            }
        }
Пример #3
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);
            }
        }
Пример #4
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);
            }
        }
Пример #5
0
        private static void ImportPack(string inputImage, string outputPack)
        {
            Npck npck = NpckFactory.FromBackgroundImage(inputImage);

            npck.Write(outputPack);
        }
Пример #6
0
        private static void ExtractPack(string packFile, string outputImage)
        {
            Npck npck = new Npck(packFile);

            npck.GetBackgroundImage().Save(outputImage);
        }
Пример #7
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);
        }
Пример #8
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");
            }
        }