For palette indexing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.5 KiB

using System.Collections.Generic;
namespace Palettizer
{
public class Palette
{
public Dictionary<byte, Color> GrayscaleToColorMap { get; }
public Color[] GrayscaleToColor { get; }
public Dictionary<Color, byte> ColorToGrayscaleMap { get; }
public int GrayscaleCount { get; set; }
public List<byte[]> AlternateColorRows { get; }
public int Width { get => 256; }
public int Height { get => AlternateColorRows.Count + 1; }
public Palette()
{
GrayscaleToColorMap = new Dictionary<byte, Color>();
ColorToGrayscaleMap = new Dictionary<Color, byte>();
GrayscaleCount = 0;
AlternateColorRows = new List<byte[]>();
}
public void AddColor(Color color)
{
var average = (color.R + color.G + color.B) / 3;
if (GrayscaleToColorMap.ContainsKey((byte)average))
{
var increment = 1;
for (var i = 0; i < 256; i += 1)
{
average = (average + increment) % 256;
if (!GrayscaleToColorMap.ContainsKey((byte)average))
{
break;
}
increment = ((increment > 0) ? (increment + 1) : (increment - 1)) * -1; // up by 1, down by 2, up by 3, etc
}
}
GrayscaleToColorMap.Add((byte)average, color);
ColorToGrayscaleMap.Add(color, (byte)average);
GrayscaleCount += 1;
}
public void AddAlternateColorRow(Color[] colors)
{
var byteArray = new byte[256 * 4];
for (var i = 0; i < 256; i += 1)
{
byteArray[i * 4] = colors[i].R;
byteArray[i * 4 + 1] = colors[i].G;
byteArray[i * 4 + 2] = colors[i].B;
byteArray[i * 4 + 3] = colors[i].A;
}
AlternateColorRows.Add(byteArray);
}
public void ClearAlternateColorRows()
{
AlternateColorRows.Clear();
}
public byte[] CreateIndexedPaletteBitmap()
{
var paletteBitmap = new byte[256 * (AlternateColorRows.Count + 1) * 4];
for (var i = 0; i < 256; i += 1)
{
var color = new Color();
color.A = 255;
if (GrayscaleToColorMap.ContainsKey((byte)i))
{
color = GrayscaleToColorMap[(byte)i];
}
paletteBitmap[i * 4] = color.R;
paletteBitmap[i * 4 + 1] = color.G;
paletteBitmap[i * 4 + 2] = color.B;
paletteBitmap[i * 4 + 3] = color.A;
}
for (var i = 0; i < 256; i += 1)
{
for (var j = 0; j < AlternateColorRows.Count; j += 1)
{
var alpha = AlternateColorRows[j][i * 4 + 3];
if (alpha == 0)
{
// grab from top row
paletteBitmap[(i + (256 * (j + 1))) * 4] = paletteBitmap[i * 4];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 1] = paletteBitmap[i * 4 + 1];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 2] = paletteBitmap[i * 4 + 2];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 3] = paletteBitmap[i * 4 + 3];
}
else
{
paletteBitmap[(i + (256 * (j + 1))) * 4] = AlternateColorRows[j][i * 4];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 1] = AlternateColorRows[j][i * 4 + 1];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 2] = AlternateColorRows[j][i * 4 + 2];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 3] = alpha;
}
}
}
return paletteBitmap;
}
}
}