I think I got the palettes read

This commit is contained in:
Lani Aung
2021-10-03 21:23:49 -06:00
parent 4b4fffdbf1
commit 7dfa28674e
3 changed files with 56 additions and 12 deletions
+45 -1
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
@@ -31,8 +32,51 @@ namespace fxl.codes.kisekae.Services
var header = Encoding.ASCII.GetBytes("KiSS");
if (buffer[..header.Length].SequenceEqual(header))
{
// Matching header I guess?
// Verify palette mark?
if (buffer[4] != 16) throw new InvalidDataException("File provided 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);
}
else
{
palette.Colors = GetColors(new BitArray(buffer));
}
}
private Color[] GetColors(BitArray bitArray, int depth = 12, int colorsPerGroup = 16, int groups = 10)
{
_logger.LogTrace($"Converting bit array to colors");
var colorCounter = 0;
var colors = new Color[colorsPerGroup * groups];
var data = new bool[bitArray.Length];
bitArray.CopyTo(data, 0);
var length = depth / 3;
for (var index = 0; index < colorsPerGroup * groups; index += depth)
{
var greenStart = index + length;
var blueStart = index + length * 2;
var red = GetValue(data[index..greenStart]);
var green = GetValue(data[greenStart..blueStart]);
var blue = GetValue(data[blueStart..(index + depth)]);
colors[colorCounter] = new Color(red, green, blue, colorCounter / colorsPerGroup, depth);
colorCounter++;
}
return colors;
}
private static int GetValue(bool[] data)
{
var bits = new BitArray(data);
var value = new int[1];
bits.CopyTo(value, 0);
return value[0];
}
}
}