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 Graphic2Image(IndexedImage indexedImage, ColorSpace colorSpace) { m_colorList = indexedImage.ColorList; Width = indexedImage.Width; Height = indexedImage.Height; Pixels = new int[Width * Height]; ForegroundColors = new int[Width * Height]; BackgroundColors = new int[Width * Height]; for (int y = 0; y < Height; y++) { if (y % 10 == 0) { Console.WriteLine("converting to graphic2 ... {0}/{1}", y, Height); } for (int x = 0; x < Width; x += 8) { //. ユニークカラーの取得. List <int> listIndices = new List <int>(); for (int i = 0; i < 8; i++) { if (x + i >= Width) { continue; } int index = indexedImage.GetIndex(x + i, y); if (listIndices.Contains(index) == false) { listIndices.Add(index); } } if (listIndices.Count <= 1) { for (int i = 0; i < 8; i++) { if (x + i >= Width) { continue; } SetIndex(x + i, y, listIndices[0], listIndices[0], 0); } } else { List <ColorDistance> listColorDistance = new List <ColorDistance>(); ColorSpaceRgb colorSpaceRgb = new ColorSpaceRgb(); Color color0, color1; for (int i = 0; i < listIndices.Count - 1; i++) { for (int j = i + 1; j < listIndices.Count; j++) { color0 = indexedImage.ColorList.Colors[listIndices[i]]; color1 = indexedImage.ColorList.Colors[listIndices[j]]; listColorDistance.Add(new ColorDistance(listIndices[i], listIndices[j], colorSpaceRgb.Distance(color0, color1))); } } ColorDistance[] arrayColorDistance = listColorDistance.ToArray(); Array.Sort(arrayColorDistance); int index0 = arrayColorDistance[0].Index0; int index1 = arrayColorDistance[0].Index1; color0 = indexedImage.ColorList.Colors[index0]; color1 = indexedImage.ColorList.Colors[index1]; for (int i = 0; i < 8; i++) { if (x + i >= Width) { continue; } Color color = indexedImage.GetPixel(x + i, y); float d0 = colorSpaceRgb.Distance(color0, color); float d1 = colorSpaceRgb.Distance(color1, color); SetIndex(x + i, y, d0 < d1 ? index0 : index1, index0, index1); } } /* * //. 多い色2つ取ってくる. * Dictionary<int, int> dicColors = new Dictionary<int, int>(); * * for (int i = 0; i < 8; i++) * { * if ((x + i) >= Width) * { * continue; * } * * int color = indexedImage.GetIndex(x + i, y); * if (dicColors.ContainsKey(color)) * { * dicColors[color] += 1; * } * else * { * dicColors.Add(color, 1); * } * } * * if (dicColors.Keys.Count == 1) * { * for (int i = 0; i < 8; i++) * { * if (x + i >= Width) * { * continue; * } * * SetIndex( x+ i , y, dicColors.Keys.ElementAt(0), dicColors.Keys.ElementAt(0), 0 ); * } * } * else * { * ColorCount[] colors = new ColorCount[dicColors.Keys.Count]; * for (int i = 0; i < colors.Length; i++) * { * colors[i] = new ColorCount(dicColors.Keys.ElementAt(i), dicColors.Values.ElementAt(i)); * } * * Array.Sort(colors); * * for (int i = 0; i < 8; i++) * { * if (x + i >= Width) * { * continue; * } * * Color color = indexedImage.GetPixel(x + i, y); * float d0 = colorSpace.Distance( m_colorList.GetColor(colors[0].Color), color); * float d1 = colorSpace.Distance( m_colorList.GetColor(colors[1].Color), color); * * SetIndex(x + i, y, d0 < d1 ? colors[0].Color : colors[1].Color, colors[0].Color, colors[1].Color); * } * } */ } } }