Upgrade libraries
This commit is contained in:
@@ -14,30 +14,23 @@ using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace fxl.codes.kisekae.Services;
|
||||
|
||||
public class FileParserService
|
||||
public class FileParserService(ILogger<FileParserService> logger)
|
||||
{
|
||||
private static readonly byte[] KissHeader = "KiSS"u8.ToArray();
|
||||
private static readonly Color Transparent = Color.Black.WithAlpha(0);
|
||||
|
||||
private readonly ILogger<FileParserService> _logger;
|
||||
private readonly IsolatedStorageFile _storage;
|
||||
|
||||
public FileParserService(ILogger<FileParserService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_storage = IsolatedStorageFile.GetUserStoreForApplication();
|
||||
}
|
||||
private readonly IsolatedStorageFile _storage = IsolatedStorageFile.GetUserStoreForApplication();
|
||||
|
||||
public async Task UnzipLzh(IFormFile file, MemoryStream memoryStream = null)
|
||||
{
|
||||
if (!_storage.FileExists(file.FileName))
|
||||
{
|
||||
using var writer = _storage.CreateFile(file.FileName);
|
||||
await using var writer = _storage.CreateFile(file.FileName);
|
||||
|
||||
if (memoryStream != null)
|
||||
memoryStream.CopyTo(writer);
|
||||
await memoryStream.CopyToAsync(writer);
|
||||
else
|
||||
file.CopyTo(writer);
|
||||
await file.CopyToAsync(writer);
|
||||
|
||||
writer.Flush();
|
||||
writer.Close();
|
||||
@@ -58,7 +51,7 @@ public class FileParserService
|
||||
await process.WaitForExitAsync();
|
||||
}
|
||||
|
||||
public Color[] ParsePalette(Palette palette, out int groups, out int colorsPerGroup)
|
||||
public Rgba32[] ParsePalette(Palette palette, out int groups, out int colorsPerGroup)
|
||||
{
|
||||
groups = 0;
|
||||
colorsPerGroup = 0;
|
||||
@@ -67,7 +60,7 @@ public class FileParserService
|
||||
if (!palette.Data[..KissHeader.Length].SequenceEqual(KissHeader)) return GetColors(palette.Data);
|
||||
|
||||
// Verify palette mark?
|
||||
if (palette.Data[4] != 16) _logger.LogError($"{palette.FileName} is not a valid palette file");
|
||||
if (palette.Data[4] != 16) logger.LogError("{PaletteFileName} is not a valid palette file", palette.FileName);
|
||||
|
||||
var colorDepth = Convert.ToInt32(palette.Data[5]);
|
||||
colorsPerGroup = BitConverter.ToInt16(palette.Data, 8);
|
||||
@@ -78,14 +71,14 @@ public class FileParserService
|
||||
|
||||
public void RenderCel(CelConfig celConfig)
|
||||
{
|
||||
_logger.LogTrace($"Reading cel {celConfig.Cel.FileName}");
|
||||
var buffer = celConfig.Cel?.Data?.ToArray();
|
||||
if (buffer == null || buffer.Length == 0) return;
|
||||
logger.LogTrace("Reading cel {CelFileName}", celConfig.Cel.FileName);
|
||||
var buffer = celConfig.Cel.Data.ToArray();
|
||||
if (buffer.Length == 0) return;
|
||||
|
||||
if (buffer[..KissHeader.Length].SequenceEqual(KissHeader))
|
||||
{
|
||||
// Verify cel mark?
|
||||
if (buffer[4] != 32) _logger.LogError($"{celConfig.Cel.FileName} is not a valid cel file");
|
||||
if (buffer[4] != 32) logger.LogError("{CelFileName} is not a valid cel file", celConfig.Cel.FileName);
|
||||
|
||||
var pixelBits = Convert.ToInt32(buffer[5]);
|
||||
var width = BitConverter.ToInt16(buffer, 8);
|
||||
@@ -111,11 +104,11 @@ public class FileParserService
|
||||
}
|
||||
}
|
||||
|
||||
private Color[] GetColors(IReadOnlyCollection<byte> bytes, int depth = 12, int colorsPerGroup = 16, int groups = 10)
|
||||
private Rgba32[] GetColors(IReadOnlyCollection<byte> bytes, int depth = 12, int colorsPerGroup = 16, int groups = 10)
|
||||
{
|
||||
_logger.LogTrace("Converting byte array to colors");
|
||||
logger.LogTrace("Converting byte array to colors");
|
||||
var colorValues = GetValues(bytes, depth / 3);
|
||||
var colors = new Color[colorsPerGroup * groups];
|
||||
var colors = new Rgba32[colorsPerGroup * groups];
|
||||
var length = depth == 12 ? 4 : 3;
|
||||
var multiplier = depth == 12 ? 16 : 1;
|
||||
// Nibbles order for 12 bits: R, B, 0, G
|
||||
@@ -127,7 +120,7 @@ public class FileParserService
|
||||
var red = colorValues[start] * multiplier;
|
||||
var green = colorValues[start + (depth == 12 ? 3 : 1)] * multiplier;
|
||||
var blue = colorValues[start + (depth == 12 ? 1 : 2)] * multiplier;
|
||||
colors[groupIndex + colorIndex] = Color.FromRgb(Convert.ToByte(red), Convert.ToByte(green), Convert.ToByte(blue));
|
||||
colors[groupIndex + colorIndex] = new Rgba32(Convert.ToByte(red), Convert.ToByte(green), Convert.ToByte(blue));
|
||||
}
|
||||
|
||||
return colors;
|
||||
@@ -139,7 +132,7 @@ public class FileParserService
|
||||
int height,
|
||||
int pixelBits = 4)
|
||||
{
|
||||
_logger.LogTrace("Converting byte array to base64 encoded gifs per palette");
|
||||
logger.LogTrace("Converting byte array to base64 encoded gifs per palette");
|
||||
var values = GetValues(bytes, pixelBits);
|
||||
|
||||
using var bitmap = new Image<Rgba32>(width, height);
|
||||
@@ -153,7 +146,7 @@ public class FileParserService
|
||||
{
|
||||
var value = values[index];
|
||||
var isTransparent = value == 0 || value >= palette.Colors.Count;
|
||||
row[x] = isTransparent ? Transparent : Color.ParseHex(palette.Colors[value].Hex);
|
||||
row[x] = (isTransparent ? Transparent : Color.ParseHex(palette.Colors[value].Hex)).ToPixel<Rgba32>();
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user