diff --git a/Models/CelModel.cs b/Models/CelModel.cs index 77644f1..d611a24 100644 --- a/Models/CelModel.cs +++ b/Models/CelModel.cs @@ -6,7 +6,7 @@ namespace fxl.codes.kisekae.Models { 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 ImageByPalette = new(); public CelModel(string line) @@ -14,32 +14,33 @@ namespace fxl.codes.kisekae.Models var matcher = new Regex(Regex); 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); - if (string.IsNullOrEmpty(value?.Value)) continue; + match.Groups.TryGetValue(groupName, out var group); + if (string.IsNullOrEmpty(group?.Value)) continue; + var value = group.Value.Trim(); - switch (group) + switch (groupName) { case "1": - Id = int.Parse(value.Value); + Id = int.Parse(value); break; case "2": - Fix = int.Parse(value.Value); + Fix = int.Parse(value); break; case "3": - FileName = value.Value; + FileName = value; break; case "4": - PaletteId = int.Parse(value.Value); + PaletteId = int.Parse(value); break; 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; case "6": - Comment = value.Value; + Comment = value; break; } } diff --git a/Models/PaletteModel.cs b/Models/PaletteModel.cs index 20576cd..26e9b4e 100644 --- a/Models/PaletteModel.cs +++ b/Models/PaletteModel.cs @@ -1,38 +1,20 @@ -using System.Drawing; -using System.Text.RegularExpressions; +using System; +using SixLabors.ImageSharp; namespace fxl.codes.kisekae.Models { public class PaletteModel { - private const string Regex = "%([a-zA-Z0-9\\-]*\\.[kKcCfF]+)[\\s]*;(.*)"; - public PaletteModel(string line) { - var matcher = new Regex(Regex); - var match = matcher.Match(line); + var parts = line.Split(';'); + FileName = parts[0].Replace("%", "").Trim(); - foreach (var group in matcher.GetGroupNames()) - { - 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; - } - } + if (parts.Length > 1) Comment = parts[1].Trim(); } public string FileName { get; } public string Comment { get; } - internal Color[] Colors { get; set; } + public Color[] Colors { get; set; } = Array.Empty(); } } \ No newline at end of file diff --git a/Services/ConfigurationReaderService.cs b/Services/ConfigurationReaderService.cs index 4ef599d..e52ca98 100644 --- a/Services/ConfigurationReaderService.cs +++ b/Services/ConfigurationReaderService.cs @@ -46,7 +46,9 @@ namespace fxl.codes.kisekae.Services model.Height = int.Parse(resolutionMatch.Groups[2].Value); break; 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; case '%': model.Palettes.Add(new PaletteModel(line)); @@ -70,7 +72,7 @@ namespace fxl.codes.kisekae.Services _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); } diff --git a/Services/FileParserService.cs b/Services/FileParserService.cs index 34370bf..a05adf7 100644 --- a/Services/FileParserService.cs +++ b/Services/FileParserService.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.IO; using System.IO.IsolatedStorage; @@ -9,7 +8,6 @@ using fxl.codes.kisekae.Models; using Microsoft.Extensions.Logging; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; -using Color = System.Drawing.Color; namespace fxl.codes.kisekae.Services { @@ -30,20 +28,22 @@ namespace fxl.codes.kisekae.Services { _logger.LogTrace($"Reading palette {palette.FileName} from {directory}"); var buffer = ReadToBuffer(directory, palette.FileName); + if (buffer.Length == 0) return; if (buffer[..KissHeader.Length].SequenceEqual(KissHeader)) { // 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 colorsPerGroup = BitConverter.ToInt16(buffer, 8); 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 { - 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}"); var buffer = ReadToBuffer(directory, cel.FileName); + if (buffer.Length == 0) return; if (buffer[..KissHeader.Length].SequenceEqual(KissHeader)) { // 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 width = BitConverter.ToInt16(buffer, 8); var height = BitConverter.ToInt16(buffer, 10); var xOffset = BitConverter.ToInt16(buffer, 12); 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 { var width = BitConverter.ToInt16(buffer, 0); 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; } - 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 colors = new Color[colorsPerGroup * groups]; - var multiplier = depth == 12 ? 16 : 1; - var data = new bool[bitArray.Length]; - bitArray.CopyTo(data, 0); + var chunkSize = depth == 12 ? 2 : 3; - var length = depth / 3; - for (var index = 0; index < colorsPerGroup * groups; index += depth) + for (var index = 0; index < bytes.Length; index += chunkSize) { - var greenStart = index + length; - 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); + colors[colorCounter] = GetColor(bytes[index..(index + chunkSize)], depth); colorCounter++; } return colors; } - private void GetCelImages(BitArray bitArray, IEnumerable palettes, int width, int height, int pixelBits = 4, int xOffset = 0, int yOffset = 0) + private static Color GetColor(IReadOnlyList bytes, int depth) { - _logger.LogTrace("Converting bit array to base64 encoded gifs per palette"); - var data = new bool[bitArray.Length]; - bitArray.CopyTo(data, 0); + if (depth != 12) return Color.FromRgb(bytes[0], bytes[1], bytes[2]); - 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 GetCelImages(IReadOnlyList bytes, + IReadOnlyList palettes, + int width, + int height, + int pixelBits = 4, + int xOffset = 0, + int yOffset = 0) + { + _logger.LogTrace("Converting byte array to base64 encoded gifs per palette"); + var dictionary = new Dictionary(); + var step = (int)Math.Ceiling((double)8 / pixelBits); + + for (var paletteIndex = 0; paletteIndex < palettes.Count; paletteIndex++) { - using var bitmap = new Image(width, height); - var pixelCounter = 0; - + var palette = palettes[paletteIndex]; + using var bitmap = new Image(width, height); + var index = 0; + for (var row = 0; row < height; row++) { var span = bitmap.GetPixelRowSpan(row); + for (var column = 0; column < width; column += step) + { + var pixelByte = bytes[index]; + var nib1 = (pixelByte >> 4) & 0x0F; + span[column] = nib1 == 0 ? Color.Black.WithAlpha(0) : palette.Colors[nib1]; + + 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())); } - } - private static int GetValue(bool[] data) - { - var bits = new BitArray(data); - var value = new int[1]; - bits.CopyTo(value, 0); - - return value[0]; + return dictionary; } } } \ No newline at end of file diff --git a/Utilities/Extensions.cs b/Utilities/Extensions.cs new file mode 100644 index 0000000..e602f6b --- /dev/null +++ b/Utilities/Extensions.cs @@ -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]; + } + } +} \ No newline at end of file diff --git a/Views/Home/Play.cshtml b/Views/Home/Play.cshtml index 3c19b7d..e1254e4 100644 --- a/Views/Home/Play.cshtml +++ b/Views/Home/Play.cshtml @@ -5,6 +5,25 @@ Layout = "_Layout"; } -
-@Json.Serialize(Model)
-
\ No newline at end of file +
+ @foreach (var palette in Model.Palettes) + { +

+ @palette.Comment
+ @foreach (var color in palette.Colors) + { + @color + } +

+ } +
+ +
+ @foreach (var cel in Model.Cels) + { + foreach (var (key, value) in cel.ImageByPalette) + { + @key + } + } +
\ No newline at end of file