Using system agnostic image processing

This commit is contained in:
Lani Aung
2021-10-04 19:50:22 -06:00
parent 7dfa28674e
commit 3fc504a4f6
6 changed files with 83 additions and 50 deletions
+4 -8
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace fxl.codes.kisekae.Models namespace fxl.codes.kisekae.Models
@@ -6,6 +7,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 = "#([0-9]*)[\\.]?([0-9]*?)\\s([a-zA-Z\\-0-9]*\\.[cCeElL]+)\\s[\\*]?([0-9]*)?\\s?:([0-9\\s]*);(.*)";
public Dictionary<int, string> ImageByPalette = new();
public CelModel(string line) public CelModel(string line)
{ {
@@ -34,10 +36,7 @@ namespace fxl.codes.kisekae.Models
PaletteId = int.Parse(value.Value); PaletteId = int.Parse(value.Value);
break; break;
case "5": case "5":
foreach (var id in value.Value.Split(' ', StringSplitOptions.RemoveEmptyEntries)) foreach (var id in value.Value.Split(' ', StringSplitOptions.RemoveEmptyEntries)) Sets[int.Parse(id)] = true;
{
Sets[int.Parse(id)] = true;
}
break; break;
case "6": case "6":
Comment = value.Value; Comment = value.Value;
@@ -52,9 +51,6 @@ namespace fxl.codes.kisekae.Models
public int PaletteId { get; } public int PaletteId { get; }
public bool[] Sets { get; } = new bool[10]; public bool[] Sets { get; } = new bool[10];
public string Comment { get; } public string Comment { get; }
public Point[] InitialPositions { get; } = new Point[10];
public int CurrentPaletteId { get; set; }
public Point[] InitialPositions { get; set; } = new Point[10];
public Point[] CurrentPositions { get; set; } = new Point[10];
} }
} }
+2 -18
View File
@@ -1,3 +1,4 @@
using System.Drawing;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace fxl.codes.kisekae.Models namespace fxl.codes.kisekae.Models
@@ -32,23 +33,6 @@ namespace fxl.codes.kisekae.Models
public string FileName { get; } public string FileName { get; }
public string Comment { get; } public string Comment { get; }
public Color[] Colors { get; set; } internal Color[] Colors { get; set; }
}
public class Color
{
public Color(int red, int green, int blue, int group, int depth)
{
var multiplier = depth == 12 ? 16 : 1;
Red = red * multiplier;
Green = green * multiplier;
Blue = blue * multiplier;
Group = group;
}
public int Red { get; }
public int Green { get; }
public int Blue { get; }
public int Group { get; }
} }
} }
-9
View File
@@ -11,14 +11,5 @@ namespace fxl.codes.kisekae.Models
public List<PaletteModel> Palettes { get; } = new(); public List<PaletteModel> Palettes { get; } = new();
public List<CelModel> Cels { get; } = new(); public List<CelModel> Cels { get; } = new();
public int[] CurrentPalettes = new int[10]; public int[] CurrentPalettes = new int[10];
public void Reset()
{
foreach (var cel in Cels)
{
cel.CurrentPositions = cel.InitialPositions;
cel.CurrentPaletteId = cel.PaletteId;
}
}
} }
} }
+5 -1
View File
@@ -62,7 +62,6 @@ namespace fxl.codes.kisekae.Services
} }
SetInitialPositions(model, initialPositions.ToString()); SetInitialPositions(model, initialPositions.ToString());
model.Reset();
if (string.IsNullOrEmpty(directory)) return model; if (string.IsNullOrEmpty(directory)) return model;
@@ -71,6 +70,11 @@ namespace fxl.codes.kisekae.Services
_fileParser.ParsePalette(directory, palette); _fileParser.ParsePalette(directory, palette);
} }
foreach (var cel in model.Cels)
{
_fileParser.ParseCel(directory, cel, model.Palettes);
}
return model; return model;
} }
+69 -12
View File
@@ -1,16 +1,22 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.IsolatedStorage; using System.IO.IsolatedStorage;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using fxl.codes.kisekae.Models; using fxl.codes.kisekae.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Color = System.Drawing.Color;
namespace fxl.codes.kisekae.Services namespace fxl.codes.kisekae.Services
{ {
public class FileParserService public class FileParserService
{ {
private static readonly byte[] KissHeader = Encoding.ASCII.GetBytes("KiSS");
private readonly ILogger<FileParserService> _logger; private readonly ILogger<FileParserService> _logger;
private readonly IsolatedStorageFile _storage; private readonly IsolatedStorageFile _storage;
@@ -23,14 +29,9 @@ namespace fxl.codes.kisekae.Services
public void ParsePalette(string directory, PaletteModel palette) public void ParsePalette(string directory, PaletteModel palette)
{ {
_logger.LogTrace($"Reading palette {palette.FileName} from {directory}"); _logger.LogTrace($"Reading palette {palette.FileName} from {directory}");
using var stream = _storage.OpenFile(Path.Combine(directory, palette.FileName), FileMode.Open); var buffer = ReadToBuffer(directory, palette.FileName);
if (!stream.CanRead) throw new InvalidDataException();
var buffer = new byte[stream.Length]; if (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
stream.Read(buffer, 0, buffer.Length);
var header = Encoding.ASCII.GetBytes("KiSS");
if (buffer[..header.Length].SequenceEqual(header))
{ {
// 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) throw new InvalidDataException("File provided is not a valid palette file");
@@ -46,11 +47,49 @@ namespace fxl.codes.kisekae.Services
} }
} }
public void ParseCel(string directory, CelModel cel, IEnumerable<PaletteModel> palettes)
{
_logger.LogTrace($"Reading cel {cel.FileName} from {directory}");
var buffer = ReadToBuffer(directory, cel.FileName);
if (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
{
// Verify cel mark?
if (buffer[4] != 16) throw new InvalidDataException("File provided is not a valid palette 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);
}
else
{
var width = BitConverter.ToInt16(buffer, 0);
var height = BitConverter.ToInt16(buffer, 2);
GetCelImages(new BitArray(buffer[4..]), palettes, width, height);
}
}
private byte[] ReadToBuffer(string directory, string filename)
{
using var stream = _storage.OpenFile(Path.Combine(directory, filename), FileMode.Open);
if (!stream.CanRead) throw new InvalidDataException();
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
return buffer;
}
private Color[] GetColors(BitArray bitArray, int depth = 12, int colorsPerGroup = 16, int groups = 10) private Color[] GetColors(BitArray bitArray, int depth = 12, int colorsPerGroup = 16, int groups = 10)
{ {
_logger.LogTrace($"Converting bit array to colors"); _logger.LogTrace("Converting bit 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 data = new bool[bitArray.Length]; var data = new bool[bitArray.Length];
bitArray.CopyTo(data, 0); bitArray.CopyTo(data, 0);
@@ -60,16 +99,34 @@ namespace fxl.codes.kisekae.Services
var greenStart = index + length; var greenStart = index + length;
var blueStart = index + length * 2; var blueStart = index + length * 2;
var red = GetValue(data[index..greenStart]); var red = GetValue(data[index..greenStart]) * multiplier;
var green = GetValue(data[greenStart..blueStart]); var green = GetValue(data[greenStart..blueStart]) * multiplier;
var blue = GetValue(data[blueStart..(index + depth)]); var blue = GetValue(data[blueStart..(index + depth)]) * multiplier;
colors[colorCounter] = new Color(red, green, blue, colorCounter / colorsPerGroup, depth); 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)
{
_logger.LogTrace("Converting bit array to base64 encoded gifs per palette");
var data = new bool[bitArray.Length];
bitArray.CopyTo(data, 0);
foreach (var palette in palettes)
{
using var bitmap = new Image<Bgr24>(width, height);
var pixelCounter = 0;
for (var row = 0; row < height; row++)
{
var span = bitmap.GetPixelRowSpan(row);
}
}
}
private static int GetValue(bool[] data) private static int GetValue(bool[] data)
{ {
var bits = new BitArray(data); var bits = new BitArray(data);
+1
View File
@@ -11,5 +11,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.10" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.10" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.4" />
</ItemGroup> </ItemGroup>
</Project> </Project>