public static void Compress8BitBlock(byte[] source, int sourcePosition, int sourceLineLength, byte[] destination, int destPosition, int channel, bool isSigned)
{
// KFreon: Get min and max
byte min = byte.MaxValue;
byte max = byte.MinValue;
int count = sourcePosition + channel;
for (int i = 1; i <= 4; i++)
{
for (int j= 0; j < 4; j++)
{
byte colour = source[count];
if (colour > max)
max = colour;
else if (colour < min)
min = colour;
count += 4; // skip to next entry in channel
}
count = sourcePosition + channel + sourceLineLength * i;
}
// Build Palette
byte[] Colours = Build8BitPalette(min, max, isSigned);
// Compress Pixels
ulong line = 0;
count = sourcePosition + channel;
List<int> indicies = new List<int>();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int ind = (i << 2) + j;
byte colour = source[count];
int index = GetClosestValue(Colours, colour);
indicies.Add(index);
line |= (ulong)index << (ind * 3);
count += 4; // Only need 1 channel
}
count = sourcePosition + channel + sourceLineLength * (i + 1);
}
byte[] compressed = BitConverter.GetBytes(line);
destination[destPosition] = min;
destination[destPosition + 1] = max;
for (int i = 2; i < 8; i++)
destination[destPosition + i] = compressed[i - 2];
}