recrownedgtk/RecrownedAthenaeum.ConsoleTools/TextureAtlasTools/TexturePacker.cs

81 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace RecrownedAthenaeum.ConsoleTools.TextureAtlasTools
{
internal class TexturePacker
{
private enum SupportedExtensions
{
jpeg, jpg, png
}
Rectangle theoreticalSpace;
Node masterNode;
internal TexturePacker(string rootDirectoryPath)
{
string[] paths = Directory.GetFiles(rootDirectoryPath);
int minArea = 0;
int currentPoTArea = 2 * 2;
List<TheoreticalImage> theoreticalImages = new List<TheoreticalImage>();
for (int pathID = 0; pathID < paths.Length; pathID++)
{
SupportedExtensions extension;
if (Enum.TryParse(Path.GetExtension(paths[pathID]), out extension))
{
using (FileStream stream = new FileStream(paths[pathID], FileMode.Open))
{
IImageInfo info = Image.Identify(stream);
TheoreticalImage ti = new TheoreticalImage(info.Width, info.Height, paths[pathID]);
theoreticalImages.Add(ti);
minArea += ti.Area;
while (currentPoTArea < minArea)
{
currentPoTArea *= 2 * 2;
}
}
}
}
}
private class Node
{
public Node parent;
public Node[] branches = new Node[2];
public readonly TheoreticalImage theoreticalImage;
public Node(TheoreticalImage theoreticalImage)
{
this.theoreticalImage = theoreticalImage;
}
}
private struct TheoreticalImage
{
public Rectangle bounds;
public readonly string path;
public int Area { get { return bounds.Width * bounds.Height; } }
public TheoreticalImage(int width, int height, string path)
{
bounds = new Rectangle();
bounds.Width = width;
bounds.Height = height;
this.path = path;
}
}
}
}