2016-11-25 06:05:29 +00:00
|
|
|
|
namespace SpriteCompiler
|
|
|
|
|
{
|
2016-12-05 05:14:51 +00:00
|
|
|
|
using Fclp;
|
|
|
|
|
using SpriteCompiler.Problem;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-12-06 03:55:04 +00:00
|
|
|
|
using System.Drawing;
|
2016-12-05 05:14:51 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
public static class ExtensionMethods
|
|
|
|
|
{
|
|
|
|
|
public static void Dump(this int[,] array)
|
|
|
|
|
{
|
|
|
|
|
var rows = array.GetLength(1);
|
|
|
|
|
var cols = array.GetLength(0);
|
|
|
|
|
|
|
|
|
|
for (int r = 0; r < rows; r++)
|
|
|
|
|
{
|
|
|
|
|
for (int c = 0; c < cols; c++)
|
|
|
|
|
{
|
|
|
|
|
Console.Write(array[c, r].ToString("X1"));
|
|
|
|
|
}
|
|
|
|
|
Console.Write(Environment.NewLine);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2016-12-05 05:14:51 +00:00
|
|
|
|
public class ApplicationArguments
|
|
|
|
|
{
|
|
|
|
|
public List<string> Data { get; set; }
|
|
|
|
|
public List<string> Mask { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-25 06:05:29 +00:00
|
|
|
|
public class Program
|
|
|
|
|
{
|
2016-12-05 05:14:51 +00:00
|
|
|
|
public static void WriteOutSolution(IEnumerable<SpriteGeneratorSearchNode> solution)
|
|
|
|
|
{
|
|
|
|
|
foreach (var step in solution.Skip(1))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(step.Action.Emit());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(string.Format("; Total Cost = {0} cycles", (int)solution.Last().PathCost));
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-25 06:05:29 +00:00
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2016-12-06 05:17:31 +00:00
|
|
|
|
var data = new List<byte>();
|
|
|
|
|
var mask = new List<byte>();
|
|
|
|
|
var filename = (string)null;
|
|
|
|
|
Color? maskColor = null;
|
|
|
|
|
var sprite = new List<SpriteByte>();
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
var p = new FluentCommandLineParser();
|
|
|
|
|
|
|
|
|
|
p.Setup<List<string>>('d', "data")
|
|
|
|
|
.Callback(_ => data = _.Select(s => Convert.ToByte(s, 16)).ToList());
|
|
|
|
|
|
|
|
|
|
p.Setup<List<string>>('m', "mask")
|
|
|
|
|
.Callback(_ => data = _.Select(s => Convert.ToByte(s, 16)).ToList());
|
|
|
|
|
|
|
|
|
|
p.Setup<string>('i', "image")
|
|
|
|
|
.Callback(_ => filename = _);
|
|
|
|
|
|
|
|
|
|
p.Setup<string>("bg-color")
|
|
|
|
|
.Callback(_ => maskColor = Color.FromArgb(0xFF, Color.FromArgb(Convert.ToInt32(_, 16))));
|
|
|
|
|
|
|
|
|
|
p.Parse(args);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Manual data has " + data.Count + " bytes");
|
|
|
|
|
Console.WriteLine("Manual mask has " + mask.Count + " bytes");
|
|
|
|
|
Console.WriteLine("Input filename is " + filename);
|
|
|
|
|
Console.WriteLine("Image mask color is " + (maskColor.HasValue ? maskColor.ToString() : "(none)"));
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(filename))
|
2016-12-06 03:55:04 +00:00
|
|
|
|
{
|
2016-12-06 05:17:31 +00:00
|
|
|
|
var palette = new Dictionary<Color, int>();
|
|
|
|
|
int nextIndex = 1;
|
|
|
|
|
|
|
|
|
|
// Convert the image / mask to a paletted image
|
|
|
|
|
var bitmap = new Bitmap(filename);
|
|
|
|
|
int[,] data_buffer = new int[bitmap.Width, bitmap.Height];
|
|
|
|
|
int[,] mask_buffer = new int[bitmap.Width, bitmap.Height];
|
2016-12-06 03:55:04 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
Console.WriteLine(String.Format(" Image is {0} x {1}", bitmap.Width, bitmap.Height));
|
|
|
|
|
|
|
|
|
|
if (maskColor.HasValue)
|
|
|
|
|
{
|
|
|
|
|
palette[maskColor.Value] = 0;
|
|
|
|
|
}
|
2016-12-06 03:55:04 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
for (int r = 0; r < bitmap.Height; r++)
|
|
|
|
|
{
|
|
|
|
|
for (int w = 0; w < bitmap.Width; w++)
|
2016-12-06 03:55:04 +00:00
|
|
|
|
{
|
2016-12-06 05:17:31 +00:00
|
|
|
|
var rgb = bitmap.GetPixel(w, r);
|
|
|
|
|
|
|
|
|
|
if (!palette.ContainsKey(rgb))
|
2016-12-06 03:55:04 +00:00
|
|
|
|
{
|
2016-12-06 05:17:31 +00:00
|
|
|
|
if (palette.Count >= 15)
|
2016-12-06 03:55:04 +00:00
|
|
|
|
{
|
2016-12-06 05:17:31 +00:00
|
|
|
|
throw new Exception("Image cannot have more than 15 unique colors");
|
2016-12-06 03:55:04 +00:00
|
|
|
|
}
|
2016-12-06 05:17:31 +00:00
|
|
|
|
palette[rgb] = nextIndex++;
|
2016-12-06 03:55:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
data_buffer[w, r] = palette[rgb];
|
2016-12-06 03:55:04 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
if (maskColor.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (rgb.Equals(maskColor.Value))
|
|
|
|
|
{
|
|
|
|
|
data_buffer[w, r] = 0x0;
|
|
|
|
|
mask_buffer[w, r] = 0xF;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
data_buffer[w, r] = palette[rgb];
|
|
|
|
|
mask_buffer[w, r] = 0x0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-12-06 03:55:04 +00:00
|
|
|
|
{
|
2016-12-06 05:17:31 +00:00
|
|
|
|
data_buffer[w, r] = palette[rgb];
|
2016-12-06 03:55:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
data_buffer.Dump();
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
mask_buffer.Dump();
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
// Pair up pixels to build bytes
|
|
|
|
|
for (int r = 0; r < bitmap.Height; r++)
|
|
|
|
|
{
|
|
|
|
|
for (int w = 0; w < bitmap.Width; w += 2)
|
|
|
|
|
{
|
|
|
|
|
var mask_byte = (byte)((mask_buffer[w, r] << 4) + mask_buffer[w + 1, r]);
|
|
|
|
|
var data_byte = (byte)((data_buffer[w, r] << 4) + data_buffer[w + 1, r]);
|
|
|
|
|
var offset = (ushort)(r * 160 + (w / 2));
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
// Skip fully transparent bytes
|
|
|
|
|
if (mask_byte == 0xFF)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
Console.WriteLine(String.Format("Adding ({0:X2}, {1:X2}, {2})", data_byte, mask_byte, offset));
|
|
|
|
|
|
|
|
|
|
sprite.Add(new SpriteByte(data_byte, mask_byte, offset));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-09 03:59:46 +00:00
|
|
|
|
// Set the global state
|
2016-12-06 05:17:31 +00:00
|
|
|
|
var problem = SpriteGeneratorSearchProblem.CreateSearchProblem();
|
|
|
|
|
var search = SpriteGeneratorSearchProblem.Create();
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-09 03:59:46 +00:00
|
|
|
|
var solution = search.Search(problem, SpriteGeneratorState.Init(data));
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
2016-12-06 05:17:31 +00:00
|
|
|
|
WriteOutSolution(solution);
|
2016-11-25 06:05:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|