Nibble math works better than parsing things manually
This commit is contained in:
+13
-12
@@ -6,7 +6,7 @@ namespace fxl.codes.kisekae.Models
|
|||||||
{
|
{
|
||||||
public class CelModel
|
public class CelModel
|
||||||
{
|
{
|
||||||
public const string Regex = "#([0-9]*)[\\.]?([0-9]*?)\\s([a-zA-Z\\-0-9]*\\.[cCeElL]+)\\s[\\*]?([0-9]*)?\\s?:([0-9\\s]*);(.*)";
|
public const string Regex = @"#(\d*)\.?(\d*)\s*([\w\d\-]*\.[cCeElL]*)\s*\*?(\d*)?\s*\:?([\d\s]*)?;?([\w\d\-\s\%]*)";
|
||||||
public Dictionary<int, string> ImageByPalette = new();
|
public Dictionary<int, string> ImageByPalette = new();
|
||||||
|
|
||||||
public CelModel(string line)
|
public CelModel(string line)
|
||||||
@@ -14,32 +14,33 @@ namespace fxl.codes.kisekae.Models
|
|||||||
var matcher = new Regex(Regex);
|
var matcher = new Regex(Regex);
|
||||||
var match = matcher.Match(line);
|
var match = matcher.Match(line);
|
||||||
|
|
||||||
foreach (var group in matcher.GetGroupNames())
|
foreach (var groupName in matcher.GetGroupNames())
|
||||||
{
|
{
|
||||||
if (group == "0") continue;
|
if (groupName == "0") continue;
|
||||||
|
|
||||||
match.Groups.TryGetValue(group, out var value);
|
match.Groups.TryGetValue(groupName, out var group);
|
||||||
if (string.IsNullOrEmpty(value?.Value)) continue;
|
if (string.IsNullOrEmpty(group?.Value)) continue;
|
||||||
|
var value = group.Value.Trim();
|
||||||
|
|
||||||
switch (group)
|
switch (groupName)
|
||||||
{
|
{
|
||||||
case "1":
|
case "1":
|
||||||
Id = int.Parse(value.Value);
|
Id = int.Parse(value);
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
Fix = int.Parse(value.Value);
|
Fix = int.Parse(value);
|
||||||
break;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
FileName = value.Value;
|
FileName = value;
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "4":
|
||||||
PaletteId = int.Parse(value.Value);
|
PaletteId = int.Parse(value);
|
||||||
break;
|
break;
|
||||||
case "5":
|
case "5":
|
||||||
foreach (var id in value.Value.Split(' ', StringSplitOptions.RemoveEmptyEntries)) Sets[int.Parse(id)] = true;
|
foreach (var id in value.Split(' ', StringSplitOptions.RemoveEmptyEntries)) Sets[int.Parse(id)] = true;
|
||||||
break;
|
break;
|
||||||
case "6":
|
case "6":
|
||||||
Comment = value.Value;
|
Comment = value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-24
@@ -1,38 +1,20 @@
|
|||||||
using System.Drawing;
|
using System;
|
||||||
using System.Text.RegularExpressions;
|
using SixLabors.ImageSharp;
|
||||||
|
|
||||||
namespace fxl.codes.kisekae.Models
|
namespace fxl.codes.kisekae.Models
|
||||||
{
|
{
|
||||||
public class PaletteModel
|
public class PaletteModel
|
||||||
{
|
{
|
||||||
private const string Regex = "%([a-zA-Z0-9\\-]*\\.[kKcCfF]+)[\\s]*;(.*)";
|
|
||||||
|
|
||||||
public PaletteModel(string line)
|
public PaletteModel(string line)
|
||||||
{
|
{
|
||||||
var matcher = new Regex(Regex);
|
var parts = line.Split(';');
|
||||||
var match = matcher.Match(line);
|
FileName = parts[0].Replace("%", "").Trim();
|
||||||
|
|
||||||
foreach (var group in matcher.GetGroupNames())
|
if (parts.Length > 1) Comment = parts[1].Trim();
|
||||||
{
|
|
||||||
if (group == "0") continue;
|
|
||||||
|
|
||||||
match.Groups.TryGetValue(group, out var value);
|
|
||||||
if (string.IsNullOrEmpty(value?.Value)) continue;
|
|
||||||
|
|
||||||
switch (group)
|
|
||||||
{
|
|
||||||
case "1":
|
|
||||||
FileName = value.Value;
|
|
||||||
break;
|
|
||||||
case "2":
|
|
||||||
Comment = value.Value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string FileName { get; }
|
public string FileName { get; }
|
||||||
public string Comment { get; }
|
public string Comment { get; }
|
||||||
internal Color[] Colors { get; set; }
|
public Color[] Colors { get; set; } = Array.Empty<Color>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,9 @@ namespace fxl.codes.kisekae.Services
|
|||||||
model.Height = int.Parse(resolutionMatch.Groups[2].Value);
|
model.Height = int.Parse(resolutionMatch.Groups[2].Value);
|
||||||
break;
|
break;
|
||||||
case '[':
|
case '[':
|
||||||
model.BorderColorIndex = int.Parse(line.Replace("[", ""));
|
var borderValue = line.Replace("[", "");
|
||||||
|
if (borderValue.Contains(';')) borderValue = borderValue.Split(';')[0].Trim();
|
||||||
|
model.BorderColorIndex = int.Parse(borderValue);
|
||||||
break;
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
model.Palettes.Add(new PaletteModel(line));
|
model.Palettes.Add(new PaletteModel(line));
|
||||||
@@ -70,7 +72,7 @@ namespace fxl.codes.kisekae.Services
|
|||||||
_fileParser.ParsePalette(directory, palette);
|
_fileParser.ParsePalette(directory, palette);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var cel in model.Cels)
|
foreach (var cel in model.Cels.Where(x => !string.IsNullOrEmpty(x.FileName)))
|
||||||
{
|
{
|
||||||
_fileParser.ParseCel(directory, cel, model.Palettes);
|
_fileParser.ParseCel(directory, cel, model.Palettes);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.IsolatedStorage;
|
using System.IO.IsolatedStorage;
|
||||||
@@ -9,7 +8,6 @@ using fxl.codes.kisekae.Models;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.PixelFormats;
|
using SixLabors.ImageSharp.PixelFormats;
|
||||||
using Color = System.Drawing.Color;
|
|
||||||
|
|
||||||
namespace fxl.codes.kisekae.Services
|
namespace fxl.codes.kisekae.Services
|
||||||
{
|
{
|
||||||
@@ -30,20 +28,22 @@ namespace fxl.codes.kisekae.Services
|
|||||||
{
|
{
|
||||||
_logger.LogTrace($"Reading palette {palette.FileName} from {directory}");
|
_logger.LogTrace($"Reading palette {palette.FileName} from {directory}");
|
||||||
var buffer = ReadToBuffer(directory, palette.FileName);
|
var buffer = ReadToBuffer(directory, palette.FileName);
|
||||||
|
if (buffer.Length == 0) return;
|
||||||
|
|
||||||
if (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
|
if (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
|
||||||
{
|
{
|
||||||
// Verify palette mark?
|
// Verify palette mark?
|
||||||
if (buffer[4] != 16) throw new InvalidDataException("File provided is not a valid palette file");
|
if (buffer[4] != 16) _logger.LogError($"{palette.FileName} is not a valid palette file");
|
||||||
|
|
||||||
var colorDepth = Convert.ToInt32(buffer[5]);
|
var colorDepth = Convert.ToInt32(buffer[5]);
|
||||||
var colorsPerGroup = BitConverter.ToInt16(buffer, 8);
|
var colorsPerGroup = BitConverter.ToInt16(buffer, 8);
|
||||||
var groups = BitConverter.ToInt16(buffer, 10);
|
var groups = BitConverter.ToInt16(buffer, 10);
|
||||||
|
|
||||||
palette.Colors = GetColors(new BitArray(buffer[32..]), colorDepth, colorsPerGroup, groups);
|
palette.Colors = GetColors(buffer[32..], colorDepth, colorsPerGroup, groups);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
palette.Colors = GetColors(new BitArray(buffer));
|
palette.Colors = GetColors(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,25 +51,27 @@ namespace fxl.codes.kisekae.Services
|
|||||||
{
|
{
|
||||||
_logger.LogTrace($"Reading cel {cel.FileName} from {directory}");
|
_logger.LogTrace($"Reading cel {cel.FileName} from {directory}");
|
||||||
var buffer = ReadToBuffer(directory, cel.FileName);
|
var buffer = ReadToBuffer(directory, cel.FileName);
|
||||||
|
if (buffer.Length == 0) return;
|
||||||
|
|
||||||
if (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
|
if (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
|
||||||
{
|
{
|
||||||
// Verify cel mark?
|
// Verify cel mark?
|
||||||
if (buffer[4] != 16) throw new InvalidDataException("File provided is not a valid palette file");
|
if (buffer[4] != 32) _logger.LogError($"{cel.FileName} is not a valid cel file");
|
||||||
|
|
||||||
var pixelBits = Convert.ToInt32(buffer[5]);
|
var pixelBits = Convert.ToInt32(buffer[5]);
|
||||||
var width = BitConverter.ToInt16(buffer, 8);
|
var width = BitConverter.ToInt16(buffer, 8);
|
||||||
var height = BitConverter.ToInt16(buffer, 10);
|
var height = BitConverter.ToInt16(buffer, 10);
|
||||||
var xOffset = BitConverter.ToInt16(buffer, 12);
|
var xOffset = BitConverter.ToInt16(buffer, 12);
|
||||||
var yOffset = BitConverter.ToInt16(buffer, 14);
|
var yOffset = BitConverter.ToInt16(buffer, 14);
|
||||||
|
|
||||||
GetCelImages(new BitArray(buffer[32..]), palettes, width, height, pixelBits, xOffset, yOffset);
|
cel.ImageByPalette = GetCelImages(buffer[32..], palettes.ToArray(), width, height, pixelBits, xOffset, yOffset);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var width = BitConverter.ToInt16(buffer, 0);
|
var width = BitConverter.ToInt16(buffer, 0);
|
||||||
var height = BitConverter.ToInt16(buffer, 2);
|
var height = BitConverter.ToInt16(buffer, 2);
|
||||||
|
|
||||||
GetCelImages(new BitArray(buffer[4..]), palettes, width, height);
|
cel.ImageByPalette = GetCelImages(buffer[4..], palettes.ToArray(), width, height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,56 +86,83 @@ namespace fxl.codes.kisekae.Services
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Color[] GetColors(BitArray bitArray, int depth = 12, int colorsPerGroup = 16, int groups = 10)
|
private Color[] GetColors(byte[] bytes, int depth = 12, int colorsPerGroup = 16, int groups = 10)
|
||||||
{
|
{
|
||||||
_logger.LogTrace("Converting bit array to colors");
|
_logger.LogTrace("Converting byte array to colors");
|
||||||
var colorCounter = 0;
|
var colorCounter = 0;
|
||||||
var colors = new Color[colorsPerGroup * groups];
|
var colors = new Color[colorsPerGroup * groups];
|
||||||
var multiplier = depth == 12 ? 16 : 1;
|
var chunkSize = depth == 12 ? 2 : 3;
|
||||||
var data = new bool[bitArray.Length];
|
|
||||||
bitArray.CopyTo(data, 0);
|
|
||||||
|
|
||||||
var length = depth / 3;
|
for (var index = 0; index < bytes.Length; index += chunkSize)
|
||||||
for (var index = 0; index < colorsPerGroup * groups; index += depth)
|
|
||||||
{
|
{
|
||||||
var greenStart = index + length;
|
colors[colorCounter] = GetColor(bytes[index..(index + chunkSize)], depth);
|
||||||
var blueStart = index + length * 2;
|
|
||||||
|
|
||||||
var red = GetValue(data[index..greenStart]) * multiplier;
|
|
||||||
var green = GetValue(data[greenStart..blueStart]) * multiplier;
|
|
||||||
var blue = GetValue(data[blueStart..(index + depth)]) * multiplier;
|
|
||||||
colors[colorCounter] = Color.FromArgb(255, red, green, blue);
|
|
||||||
colorCounter++;
|
colorCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return colors;
|
return colors;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GetCelImages(BitArray bitArray, IEnumerable<PaletteModel> palettes, int width, int height, int pixelBits = 4, int xOffset = 0, int yOffset = 0)
|
private static Color GetColor(IReadOnlyList<byte> bytes, int depth)
|
||||||
{
|
{
|
||||||
_logger.LogTrace("Converting bit array to base64 encoded gifs per palette");
|
if (depth != 12) return Color.FromRgb(bytes[0], bytes[1], bytes[2]);
|
||||||
var data = new bool[bitArray.Length];
|
|
||||||
bitArray.CopyTo(data, 0);
|
|
||||||
|
|
||||||
foreach (var palette in palettes)
|
var redBlue = bytes[0];
|
||||||
|
var red = (redBlue >> 4) & 0x0F;
|
||||||
|
red += red * 16;
|
||||||
|
|
||||||
|
var blue = redBlue & 0x0F;
|
||||||
|
blue += blue * 16;
|
||||||
|
|
||||||
|
var zeroGreen = bytes[1];
|
||||||
|
var green = zeroGreen & 0x0F;
|
||||||
|
green += green * 16;
|
||||||
|
|
||||||
|
return Color.FromRgb(Convert.ToByte(red), Convert.ToByte(green), Convert.ToByte(blue));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<int, string> GetCelImages(IReadOnlyList<byte> bytes,
|
||||||
|
IReadOnlyList<PaletteModel> palettes,
|
||||||
|
int width,
|
||||||
|
int height,
|
||||||
|
int pixelBits = 4,
|
||||||
|
int xOffset = 0,
|
||||||
|
int yOffset = 0)
|
||||||
{
|
{
|
||||||
using var bitmap = new Image<Bgr24>(width, height);
|
_logger.LogTrace("Converting byte array to base64 encoded gifs per palette");
|
||||||
var pixelCounter = 0;
|
var dictionary = new Dictionary<int, string>();
|
||||||
|
var step = (int)Math.Ceiling((double)8 / pixelBits);
|
||||||
|
|
||||||
|
for (var paletteIndex = 0; paletteIndex < palettes.Count; paletteIndex++)
|
||||||
|
{
|
||||||
|
var palette = palettes[paletteIndex];
|
||||||
|
using var bitmap = new Image<Rgba32>(width, height);
|
||||||
|
var index = 0;
|
||||||
|
|
||||||
for (var row = 0; row < height; row++)
|
for (var row = 0; row < height; row++)
|
||||||
{
|
{
|
||||||
var span = bitmap.GetPixelRowSpan(row);
|
var span = bitmap.GetPixelRowSpan(row);
|
||||||
}
|
for (var column = 0; column < width; column += step)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int GetValue(bool[] data)
|
|
||||||
{
|
{
|
||||||
var bits = new BitArray(data);
|
var pixelByte = bytes[index];
|
||||||
var value = new int[1];
|
var nib1 = (pixelByte >> 4) & 0x0F;
|
||||||
bits.CopyTo(value, 0);
|
span[column] = nib1 == 0 ? Color.Black.WithAlpha(0) : palette.Colors[nib1];
|
||||||
|
|
||||||
return value[0];
|
if (column + 1 < width)
|
||||||
|
{
|
||||||
|
var nib2 = pixelByte & 0x0F;
|
||||||
|
span[column + 1] = nib2 == 0 ? Color.Black.WithAlpha(0) : palette.Colors[nib2];
|
||||||
|
}
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
using var memory = new MemoryStream();
|
||||||
|
bitmap.SaveAsGif(memory);
|
||||||
|
dictionary.Add(paletteIndex, Convert.ToBase64String(memory.ToArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return dictionary;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace fxl.codes.kisekae.Utilities
|
||||||
|
{
|
||||||
|
public static class Extensions
|
||||||
|
{
|
||||||
|
public static bool[] ToBoolArray(this BitArray bitArray)
|
||||||
|
{
|
||||||
|
var data = new bool[bitArray.Length];
|
||||||
|
bitArray.CopyTo(data, 0);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int IntValue(this bool[] data)
|
||||||
|
{
|
||||||
|
var bits = new BitArray(data);
|
||||||
|
var value = new int[1];
|
||||||
|
bits.CopyTo(value, 0);
|
||||||
|
|
||||||
|
return value[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+22
-3
@@ -5,6 +5,25 @@
|
|||||||
Layout = "_Layout";
|
Layout = "_Layout";
|
||||||
}
|
}
|
||||||
|
|
||||||
<pre>
|
<div>
|
||||||
@Json.Serialize(Model)
|
@foreach (var palette in Model.Palettes)
|
||||||
</pre>
|
{
|
||||||
|
<p>
|
||||||
|
@palette.Comment<br>
|
||||||
|
@foreach (var color in palette.Colors)
|
||||||
|
{
|
||||||
|
<span style="background-color: #@(color.ToHex())">@color</span>
|
||||||
|
}
|
||||||
|
</p>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
@foreach (var cel in Model.Cels)
|
||||||
|
{
|
||||||
|
foreach (var (key, value) in cel.ImageByPalette)
|
||||||
|
{
|
||||||
|
<img src="data:image/gif;base64,@value" alt="@key">
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</div>
|
||||||
Reference in New Issue
Block a user