public IndexedImage(ColorList colorList, Bitmap bitmap, ColorSpace colorSpace, Dither dither) { ColorList = colorList; Width = bitmap.Width; Height = bitmap.Height; Pixels = new int[Width * Height]; for (int y = 0; y < Height; y++) { if (y % 10 == 0) { Console.WriteLine("Indexing... {0}/{1}", y, Height); } for (int x = 0; x < Width; x++) { Color color = bitmap.GetPixel(x, y); SetIndex(x, y, ColorList.GetNearestColorIndexDither(color, x, y, colorSpace, dither)); //SetIndex( x, y, ColorList.GetNearestColorIndex(color) ); } } }
public int GetNearestColorIndexDither(Color colorTarget, int x, int y, ColorSpace colorSpace, Dither dither) { ColorDistance[] distances = new ColorDistance[Colors.Length]; for (int i = 0; i < distances.Length; i++) { distances[i] = new ColorDistance(i, Colors[i], colorSpace.Distance(Colors[i], colorTarget)); } for (int i = 0; i < 2; i++) { for (int j = 0; j < distances.Length - i - 1; j++) { if (distances[j].Distance < distances[j + 1].Distance) { ColorDistance swap = distances[j]; distances[j] = distances[j + 1]; distances[j + 1] = swap; } } } Color color1 = distances[Colors.Length - 1].Color; Color color2 = distances[Colors.Length - 2].Color; float f = distances[Colors.Length - 1].Distance + distances[13].Distance; float fLerp = 0.0f; if (f > 0) { fLerp = FMath.Powf(distances[Colors.Length - 1].Distance / f, 1.0f); } return(dither.SelectDither(x, y, fLerp) == 0 ? distances[Colors.Length - 1].Index : distances[Colors.Length - 2].Index); }