Пример #1
0
        void PackOnce()
        {
            if (mPackTasks.Count == 0)
            {
                return;
            }
            // generate our output
            ImagePacker imagePacker = new ImagePacker();
            Bitmap      outputImage;
            Dictionary <string, System.Drawing.Rectangle> outputMap;
            PackTask pt = mPackTasks.Dequeue();

            Console.WriteLine("Packing {0} ({1} left to pack)", pt.outputFile, mPackTasks.Count);

            // pack the image, generating a map only if desired
            int result = imagePacker.PackImage(pt.inputFiles, REQUIRE_POW2, REQUIRE_SQUARE, 4096, 4096, PADDING, true, out outputImage, out outputMap);

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

            // try to save using our exporters
            try
            {
                if (File.Exists(pt.outputFile))
                {
                    File.Delete(pt.outputFile);
                }
                mImageExporter.Save(pt.outputFile, outputImage);
                Console.WriteLine("Saved atlas {0}.", pt.outputFile);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error saving file: " + e.Message);
                return;
            }

            if (mMapExporter != null)
            {
                try
                {
                    if (File.Exists(pt.outputMap))
                    {
                        File.Delete(pt.outputMap);
                    }
                    mMapExporter.Save(pt.outputMap, outputMap);
                    Console.WriteLine("Saved atlas map {0}.", pt.outputMap);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error saving file: " + e.Message);
                    return;
                }
            }
            foreach (string filename in pt.inputFiles)
            {
                if (File.Exists(filename))
                {
                    try
                    {
                        File.Delete(filename);
                    }
                    catch (IOException)
                    {
                        // Welp
                    }
                }
            }
        }
Пример #2
0
        public static int Launch(string[] args)
        {
            ProgramArguments arguments = ProgramArguments.Parse(args);

            if (arguments == null)
            {
                return((int)FailCode.FailedParsingArguments);
            }
            else
            {
                // make sure we have our list of exporters
                Exporters.Load();

                // try to find matching exporters
                IImageExporter imageExporter = null;
                IMapExporter   mapExporter   = null;

                string imageExtension = Path.GetExtension(arguments.image).Substring(1).ToLower();
                foreach (var exporter in Exporters.ImageExporters)
                {
                    if (exporter.ImageExtension.ToLower() == imageExtension)
                    {
                        imageExporter = exporter;
                        break;
                    }
                }

                if (imageExporter == null)
                {
                    Console.WriteLine("Failed to find exporters for specified image type.");
                    return((int)FailCode.ImageExporter);
                }

                if (!string.IsNullOrEmpty(arguments.map))
                {
                    string mapExtension = Path.GetExtension(arguments.map).Substring(1).ToLower();
                    foreach (var exporter in Exporters.MapExporters)
                    {
                        if (exporter.MapExtension.ToLower() == mapExtension)
                        {
                            mapExporter = exporter;
                            break;
                        }
                    }

                    if (mapExporter == null)
                    {
                        Console.WriteLine("Failed to find exporters for specified map type.");
                        return((int)FailCode.MapExporter);
                    }
                }

                // compile a list of images
                List <string> images = new List <string>();
                FindImages(arguments, images);

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

                // make sure no images have the same name if we're building a map
                if (mapExporter != null)
                {
                    for (int i = 0; i < images.Count; i++)
                    {
                        string str1 = Path.GetFileNameWithoutExtension(images[i]);

                        for (int j = i + 1; j < images.Count; j++)
                        {
                            string str2 = Path.GetFileNameWithoutExtension(images[j]);

                            if (str1 == str2)
                            {
                                Console.WriteLine("Two images have the same name: {0} = {1}", images[i], images[j]);
                                return((int)FailCode.ImageNameCollision);
                            }
                        }
                    }
                }

                // generate our output
                ImagePacker imagePacker = new ImagePacker();
                Bitmap      outputImage;
                Dictionary <string, Rectangle> outputMap;

                // pack the image, generating a map only if desired
                int result = imagePacker.PackImage(images, arguments.pow2, arguments.sqr, arguments.mw, arguments.mh, arguments.pad, mapExporter != null, out outputImage, out outputMap);
                if (result != 0)
                {
                    Console.WriteLine("There was an error making the image sheet.");
                    return(result);
                }

                // try to save using our exporters
                try
                {
                    if (File.Exists(arguments.image))
                    {
                        File.Delete(arguments.image);
                    }
                    imageExporter.Save(arguments.image, outputImage);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error saving file: " + e.Message);
                    return((int)FailCode.FailedToSaveImage);
                }

                if (mapExporter != null)
                {
                    try
                    {
                        if (File.Exists(arguments.map))
                        {
                            File.Delete(arguments.map);
                        }
                        mapExporter.Save(arguments.map, outputMap);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error saving file: " + e.Message);
                        return((int)FailCode.FailedToSaveMap);
                    }
                }
            }

            return(0);
        }
Пример #3
0
        public static void Launch(string dir)
        {
            // make sure we have our list of exporters
            Exporters.Exporters.Load();

            // try to find matching exporters
            IImageExporter imageExporter = null;
            IMapExporter   mapExporter   = null;

            string imageExtension = "png";

            foreach (var exporter in Exporters.Exporters.ImageExporters)
            {
                if (exporter.ImageExtension.ToLower() == imageExtension)
                {
                    imageExporter = exporter;
                    break;
                }
            }

            string mapExtension = "txt";

            foreach (var exporter in Exporters.Exporters.MapExporters)
            {
                if (exporter.MapExtension.ToLower() == mapExtension)
                {
                    mapExporter = exporter;
                    break;
                }
            }

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

            foreach (string str in Directory.GetFiles(dir, "*.png"))
            {
                if (str.EndsWith(".small.png"))
                {
                    images_lowres.Add(str.Substring(str.LastIndexOf(@"\") + 1).Replace(".small.png", ""));
                }
                else
                {
                    images.Add(str);
                }
            }

            if (Directory.Exists(dir + "_huge"))
            {
                foreach (string str in Directory.GetFiles(dir + "_huge", "*.png"))
                {
                    images_highres.Add(str.Substring(str.LastIndexOf(@"\") + 1).Replace(".png", ""));
                }
            }

            // generate our output
            ImagePacker imagePacker = new ImagePacker();
            Bitmap      outputImage;
            Dictionary <string, Rectangle> outputMap;

            // pack the image, generating a map only if desired
            int result = imagePacker.PackImage(images, true, false, 2048, 2048, 2, true, out outputImage, out outputMap);

            string sheetName = dir.Substring(dir.LastIndexOf(@"/") + 1);

            Bitmap   bmpLowres = new Bitmap(outputImage, new Size(outputImage.Width / 2, outputImage.Height / 2));
            Graphics gfxLowres = Graphics.FromImage(bmpLowres);

            gfxLowres.CompositingMode = CompositingMode.SourceCopy;

            Bitmap   bmpHighres = new Bitmap(outputImage, new Size(outputImage.Width * 2, outputImage.Height * 2));
            Graphics gfxHighres = Graphics.FromImage(bmpHighres);

            gfxHighres.CompositingMode = CompositingMode.SourceCopy;

            foreach (var m in outputMap)
            {
                if (m.Value.Width % 2 != 0)
                {
                    Console.WriteLine("FATAL: width of " + m.Key + " is not div 2");
                }
                if (m.Value.Height % 2 != 0)
                {
                    Console.WriteLine("FATAL: width of " + m.Key + " is not div 2");
                }

                string spriteName = m.Key.Substring(m.Key.LastIndexOf(@"\") + 1).Replace(".png", "");

                if (images_lowres.Contains(spriteName))
                {
                    Bitmap spriteLowres = Bitmap.FromFile(m.Key.Replace(".png", ".small.png")) as Bitmap;
                    gfxLowres.DrawImageUnscaledAndClipped(spriteLowres, new Rectangle(m.Value.X / 2, m.Value.Y / 2, m.Value.Width / 2, m.Value.Height / 2));
                }

                Brush transparentBrush = new SolidBrush(Color.Transparent);

                if (images_highres.Contains(spriteName))
                {
                    Bitmap spriteHighres = Bitmap.FromFile(dir + "_huge\\" + spriteName + ".png") as Bitmap;

                    if (spriteHighres.Width != m.Value.Width * 2 || spriteHighres.Height != m.Value.Height * 2)
                    {
                        Console.WriteLine("FATAL: dimensions of high res sprite " + m.Key + " do not match!");
                    }

                    // wipe away any sprite gunk that bled into the padding region from the upscale.
                    gfxHighres.FillRectangle(transparentBrush, new Rectangle(m.Value.X * 2 - 4, m.Value.Y * 2 - 4, m.Value.Width * 2 + 8, m.Value.Height * 2 + 8));
                    gfxHighres.DrawImageUnscaledAndClipped(spriteHighres, new Rectangle(m.Value.X * 2, m.Value.Y * 2, m.Value.Width * 2, m.Value.Height * 2));
                }

                sb.AppendFormat("            textureLocations.Add(OsuTexture.{0}, new SpriteSheetTexture(\"{1}\", {2}, {3}, {4}, {5}));\r\n",
                                spriteName, sheetName, m.Value.Left, m.Value.Top, m.Value.Width, m.Value.Height);
            }

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

            if (File.Exists(dir))
            {
                File.Delete(dir);
            }

            imageExporter.Save(dir + "_960.png", outputImage);
            bmpLowres.Save(dir + "_480.png", ImageFormat.Png);
            bmpHighres.Save(dir + "_1920.png", ImageFormat.Png);
        }