static Dictionary <int, int> FindColors(Bitmap bitmap, int x, int y, ColorList colorList, ColorSpace colorSpace) { Dictionary <int, int> dicColors = new Dictionary <int, int>(); for (int i = 0; i < 16; i++) { Color color = bitmap.GetPixel(x + i, y); int index = colorList.GetNearestColor(color, colorSpace); if (index <= 0) { continue; } if (dicColors.ContainsKey(index)) { dicColors[index] += 1; } else { dicColors.Add(index, 1); } } Dictionary <int, int> result = new Dictionary <int, int>(); if (dicColors.Count <= 0) { //return null; } else if (dicColors.Count <= 1) { //. 単色. result.Add(dicColors.Keys.ElementAt(0), 1); return(result); } else if (dicColors.Count <= 2) { //. 2 色. result.Add(dicColors.Keys.ElementAt(0), 1); result.Add(dicColors.Keys.ElementAt(1), 2); return(result); } else if (dicColors.Count <= 3) { int i0 = dicColors.Keys.ElementAt(0); int i1 = dicColors.Keys.ElementAt(1); int i2 = dicColors.Keys.ElementAt(2); if (i0 == (i1 | i2)) { result.Add(i1, 1); result.Add(i2, 2); result.Add(i0, 3); } else if (i1 == (i2 | i0)) { result.Add(i2, 1); result.Add(i0, 2); result.Add(i1, 3); } else if (i2 == (i0 | i1)) { result.Add(i0, 1); result.Add(i1, 2); result.Add(i2, 3); } } return(result); }
static void CreateSpriteMode2(string strOutputName, Bitmap bitmap, Rectangle rectPatterns, ColorList colorList, ColorSpace colorSpace) { System.IO.StreamWriter writer = new System.IO.StreamWriter(strOutputName); for (int y = 0; y < rectPatterns.Height; y += 16) { int yy = rectPatterns.Y + y; for (int x = 0; x < rectPatterns.Width; x += 16) { int xx = rectPatterns.X + x; SpritePattern pattern0 = new SpritePattern(); SpritePattern pattern1 = new SpritePattern(); for (int i = 0; i < 16; i++) { //. 3 色探す. Dictionary <int, int> dicIndexToBits = FindColors(bitmap, xx, yy + i, colorList, colorSpace); pattern1.Colors[i] = 0x60; if (dicIndexToBits.Count <= 0) { continue; } if (dicIndexToBits.Count <= 3) { pattern0.Colors[i] = dicIndexToBits.Keys.ElementAt(0); } if (dicIndexToBits.Count >= 2) { pattern1.Colors[i] |= dicIndexToBits.Keys.ElementAt(1); } for (int j = 0; j < 8; j++) { int leftIndex = colorList.GetNearestColor(bitmap.GetPixel(xx + j, yy + i), colorSpace); if (dicIndexToBits.ContainsKey(leftIndex)) { int leftBits = dicIndexToBits[leftIndex]; if ((leftBits & 0x1) == 0x1) { pattern0.Patterns[i] |= 0x80 >> j; } if ((leftBits & 0x2) == 0x2) { pattern1.Patterns[i] |= 0x80 >> j; } } int rightIndex = colorList.GetNearestColor(bitmap.GetPixel(xx + 8 + j, yy + i), colorSpace); if (dicIndexToBits.ContainsKey(rightIndex)) { int rightBits = dicIndexToBits[rightIndex]; if ((rightBits & 0x1) == 0x1) { pattern0.Patterns[16 + i] |= 0x80 >> j; } if ((rightBits & 0x2) == 0x2) { pattern1.Patterns[16 + i] |= 0x80 >> j; } } } } if (!pattern0.IsNull() && !pattern1.IsNull()) { writer.WriteLine("\t//. 2."); pattern0.WritePatterns(writer); pattern1.WritePatterns(writer); pattern0.WriteColors(writer); pattern1.WriteColors(writer); } else if (!pattern0.IsNull()) { writer.WriteLine("\t//. 1."); pattern0.WritePatterns(writer); pattern0.WriteColors(writer); } } } writer.Close(); }