using System; using System.Collections.Generic; using System.IO; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using RecrownedAthenaeum.Pipeline.TextureAtlas; using RecrownedAthenaeum.Pipeline.NinePatch; using SixLabors.ImageSharp.Processing; using SixLabors.Primitives; using System.Linq; namespace RecrownedAthenaeum.Tools.TextureAtlas { public class TexturePacker : IDisposable { private enum SupportedExtensions { jpeg, jpg, png } int powLimit; Node masterNode; Dictionary imageHandlers; int textureLength; /// /// Machine to pack multiple textures into one large texture. /// /// Path to textures. /// Power of two limit for auto expanding texture. Default is 12 which is a 4096x4096 texture. /// What power to start at and build up from. Default is 8 which is a 256x256 texture. internal TexturePacker(string rootDirectoryPath, int powLimit = 12, int startingPower = 8) { this.powLimit = powLimit; string[] paths = Directory.GetFiles(rootDirectoryPath); textureLength = startingPower; List imageHandlers = new List(); for (int pathID = 0; pathID < paths.Length; pathID++) { SupportedExtensions extension; if (Enum.TryParse(Path.GetExtension(paths[pathID]), out extension)) { ImageHandler image = new ImageHandler(paths[pathID]); imageHandlers.Add(image); } } imageHandlers.Sort(); this.imageHandlers = new Dictionary(); foreach (ImageHandler imageHandler in imageHandlers) { this.imageHandlers.Add(imageHandler.Name, imageHandler); } } /// /// Builds a texture atlas. /// /// Whether or not to automatically upscale atlas' texture in the case it is too small. Goes up to 4096 by default. public void Build(bool AutoCorrectAtlasSize = true) { masterNode = new Node(); masterNode.region.Width = textureLength; masterNode.region.Height = textureLength; Queue imageHandlerQueue = new Queue(imageHandlers.Values); ImageHandler imageHandler; while (imageHandlerQueue.TryDequeue(out imageHandler)) { Node activeNode = null; do { activeNode = masterNode.InsertImageHandler(imageHandler); if (activeNode == null) { if (!AutoCorrectAtlasSize || (textureLength *= 2) > Math.Pow(2, powLimit)) { throw new InvalidOperationException("Dimensions of texture goes past limit amount of " + powLimit + " which is " + Math.Pow(2, powLimit) + ". New texture size would be " + textureLength + "x" + textureLength + "."); } } } while (activeNode == null); } } /// /// Renders the build into a PNG file and generates the respective meant for serialization and later to be loaded. /// /// public void Save(string output) { GraphicsOptions gOptions = new GraphicsOptions(); TextureAtlasData.TextureAtlasRegion[] regions = new TextureAtlasData.TextureAtlasRegion[imageHandlers.Count]; using (Image atlasTexture = new Image(textureLength, textureLength)) { ImageHandler[] imageHandlers = this.imageHandlers.Values.ToArray(); for (int i = 0; i < imageHandlers.Length; i++) { regions[i] = new TextureAtlasData.TextureAtlasRegion(); ImageHandler imageH = imageHandlers[i]; regions[i].SetBounds(imageH.x, imageH.y, imageH.Width, imageH.Height); regions[i].ninePatchData = imageH.ninePatchData; atlasTexture.Mutate(img => img.DrawImage(gOptions, imageH.image, new Point(imageH.x, imageH.y))); } using (FileStream stream = new FileStream(output, FileMode.Create)) { atlasTexture.SaveAsPng(stream); } } } public void SetNinePatch(string fileName, int a, int b, int c, int d) { ImageHandler imageHandler = imageHandlers[fileName]; NinePatchData ninePatchData = new NinePatchData(fileName, a, b, c, d); imageHandler.ninePatchData = ninePatchData; } public void RemoveNinePatch(string fileName) { imageHandlers[fileName].ninePatchData = null; } public void Dispose() { ImageHandler[] imageHandlers = this.imageHandlers.Values.ToArray(); this.imageHandlers.Clear(); foreach (ImageHandler imageHandler in imageHandlers) { imageHandler.Dispose(); } } private class Node { public Node parent; private Node a, b; public Node childA { get { if (a == null) a = new Node(this); return a; } set { value.parent = this; a = value; } } public Node childB { get { if (b == null) { b = new Node(this); } return b; } set { value.parent = this; b = value; } } public Rectangle region; public bool containsImage = false; public bool Filled { get { return containsImage || (a != null && b != null && a.Filled && b.Filled); } } public Node(Node parent = null) { this.parent = parent; region = parent.region; } /// /// Attempts to insert image within the node. This builds the node to have children if needed. /// /// the image to insert. /// The node the image is placed in. public Node InsertImageHandler(ImageHandler imageHandler) { if (imageHandler.Width != region.Width) { if (imageHandler.Width < region.Width) { if (a == null) { childA.region.Width = imageHandler.Width; childB.region.Width = region.Width - childA.region.Width; } if (!childA.Filled && imageHandler.Width <= childA.region.Width) { return childA.InsertImageHandler(imageHandler); } if (!childB.Filled) { return childB.InsertImageHandler(imageHandler); } } } else if (imageHandler.Height != region.Height) { if (imageHandler.Height < region.Height) { if (a == null) { childA.region.Height = imageHandler.Height; childB.region.Height = region.Height - childA.region.Height; } if (!childA.Filled && imageHandler.Width <= childA.region.Width) { return childA.InsertImageHandler(imageHandler); } if (!childB.Filled) { return childB.InsertImageHandler(imageHandler); } } } else { imageHandler.x = region.X; imageHandler.y = region.Y; containsImage = true; return this; } return null; } } private class ImageHandler : IComparable, IDisposable { public readonly string path; public readonly Image image; public int Area { get { return image.Width * image.Height; } } public string Name { get { return Path.GetFileName(path); } } public int Width { get { return image.Width; } } public int Height { get { return image.Height; } } public int x, y; public NinePatchData ninePatchData; internal ImageHandler(String path) { this.path = path; using (FileStream stream = new FileStream(path, FileMode.Open)) { image = Image.Load(stream); } } public int CompareTo(ImageHandler tImage) { return Area - tImage.Area; } public void Dispose() { image.Dispose(); } } } }