public static void FromBitmap(this DesignPattern pattern, TextureBitmap bitmap) { Dictionary <TextureBitmap.Color, byte> colorMap = new Dictionary <TextureBitmap.Color, byte>(); for (int i = 0; i < 15; i++) { pattern.Palette[i].R = 0; pattern.Palette[i].G = 0; pattern.Palette[i].B = 0; } int width = pattern.Width; int height = pattern.Height; unsafe { var colors = bitmap.GetColors(); pattern.Image = new byte[width * height]; int bitmapHeight = bitmap.Height; int bitmapWidth = bitmap.Width; for (var y = 0; y < width; y++) { for (var x = 0; x < height; x++) { var pixelColor = new TextureBitmap.Color(0, 0, 0, 0); if (x < bitmapWidth && y < bitmapHeight) { pixelColor = colors[x + (bitmapHeight - 1 - y) * bitmapHeight]; } byte index = 0xF; if (pixelColor.A == 255) { if (colorMap.ContainsKey(pixelColor)) { index = colorMap[pixelColor]; } else { index = (byte)colorMap.Count; pattern.Palette[index].R = pixelColor.R; pattern.Palette[index].G = pixelColor.G; pattern.Palette[index].B = pixelColor.B; colorMap.Add(pixelColor, index); } } pattern.SetPixel(x, y, index); } } } }
public static void FromBitmap(this DesignPattern pattern, Bitmap bitmap) { Dictionary <System.Drawing.Color, byte> colorMap = new Dictionary <System.Drawing.Color, byte>(); for (int i = 0; i < 15; i++) { pattern.Palette[i].R = 0; pattern.Palette[i].G = 0; pattern.Palette[i].B = 0; } int width = pattern.Width; int height = pattern.Height; pattern.Pixels = new byte[width * height]; for (var y = 0; y < width; y++) { for (var x = 0; x < height; x++) { var pixelColor = System.Drawing.Color.FromArgb(0, 0, 0, 0); if (x < bitmap.Width && y < bitmap.Height) { pixelColor = bitmap.GetPixel(x, y); } byte index = 0xF; if (pixelColor.A == 255) { if (colorMap.ContainsKey(pixelColor)) { index = colorMap[pixelColor]; } else { index = (byte)colorMap.Count; pattern.Palette[index].R = pixelColor.R; pattern.Palette[index].G = pixelColor.G; pattern.Palette[index].B = pixelColor.B; colorMap.Add(pixelColor, index); } } pattern.SetPixel(x, y, index); } } }
public static void FromTexture(this DesignPattern pattern, Texture2D texture) { var colors = texture.GetPixels(); Dictionary <UnityEngine.Color, byte> colorMap = new Dictionary <UnityEngine.Color, byte>(); for (int i = 0; i < 15; i++) { pattern.Palette[i].R = 0; pattern.Palette[i].G = 0; pattern.Palette[i].B = 0; } int width = pattern.Width; int height = pattern.Height; pattern.Image = new byte[width * height]; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var pixelColor = colors[x + (height - 1 - y) * width]; byte index = 0xF; if (pixelColor.a == 1f) { if (colorMap.ContainsKey(pixelColor)) { index = colorMap[pixelColor]; } else { index = (byte)colorMap.Count; pattern.Palette[index].R = (byte)(pixelColor.r * 255f); pattern.Palette[index].G = (byte)(pixelColor.g * 255f); pattern.Palette[index].B = (byte)(pixelColor.b * 255f); colorMap.Add(pixelColor, index); } } pattern.SetPixel(x, y, index); } } }