Ejemplo n.º 1
0
        public static int PackPalettes(Config config)
        {
            //original config holds the input paths at minimum
            //search those for an existing .config and replace if exists
            config = LoadConfig(config);

            // compile a list of images
            var images = new List <string>();

            int error = FindImages(config, images);

            if (error != 0)
            {
                return(error);
            }

            // make sure we found some images
            if (images.Count == 0)
            {
                System.Console.WriteLine("No images to pack.");
                return((int)FailCode.NoImages);
            }

            // generate our output
            var imagePacker = new ImagePacker();

            // pack the image, generating a map only if desired
            int result = imagePacker.PackPalette(images, config.PaletteWidth, config.MaxPaletteHeight, config.TopPadding, out Bitmap outputImage, out Dictionary <string, int> outputMap);

            if (result != 0)
            {
                System.Console.WriteLine("There was an error making the image sheet.");
                return(result);
            }

            // try to save using our exporters
            if (File.Exists(config.PaletteOutputFile))
            {
                File.Delete(config.PaletteOutputFile);
            }

            var imageExtension = Path.GetExtension(config.PaletteOutputFile).Substring(1).ToLower();

            switch (imageExtension)
            {
            case "png":
                outputImage.Save(config.PaletteOutputFile, ImageFormat.Png);
                break;

            case "jpg":
                outputImage.Save(config.PaletteOutputFile, ImageFormat.Jpeg);
                break;

            case "bmp":
                outputImage.Save(config.PaletteOutputFile, ImageFormat.Bmp);
                break;

            default:
                throw new Exception("Invalid image format for output image");
            }

            if (File.Exists(config.MapOutputFile))
            {
                File.Delete(config.MapOutputFile);
            }


            //PaletteMapExporter.Save(config.MapOutputFile, outputMap, config);

            return(0);
        }