|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Drawing; |
| 7 | +using System.Drawing.Drawing2D; |
| 8 | + |
| 9 | +namespace Pixelate_Core |
| 10 | +{ |
| 11 | + public class Pixelator |
| 12 | + { |
| 13 | + static List<Color> ColorPallete; |
| 14 | + /// <summary> |
| 15 | + /// Pixalates given Bitmap |
| 16 | + /// </summary> |
| 17 | + /// <param name="bitmap"></param> |
| 18 | + /// <param name="blockSize">Sample Factor, how many pixels colors transforms into one color</param> |
| 19 | + /// <param name="colors">Amount of color in pallete, -1 for using all colors in Bitmap</param> |
| 20 | + /// <param name="randomColors">Color selection mode, random or most used</param> |
| 21 | + /// <returns>Pixelated Bitmap</returns> |
| 22 | + public static Bitmap Pixelate(Bitmap bitmap, IEnumerable<Color> Pallete, int blockSize = 4, int colors = -1, bool randomColors = true) |
| 23 | + { |
| 24 | + if (Pallete != null) |
| 25 | + ColorPallete = new List<Color>(Pallete); |
| 26 | + else |
| 27 | + ColorPallete = new List<Color>(); |
| 28 | + |
| 29 | + if (colors != -1) |
| 30 | + GeneratePallete(new BitmapWrapper(bitmap), colors, randomColors); |
| 31 | + else |
| 32 | + ColorPallete = null; |
| 33 | + |
| 34 | + |
| 35 | + using (Graphics graphics = Graphics.FromImage(bitmap)) |
| 36 | + { |
| 37 | + for (int i = 0; i < bitmap.Width; i += blockSize) |
| 38 | + { |
| 39 | + for (int j = 0; j < bitmap.Height; j += blockSize) |
| 40 | + { |
| 41 | + Color avrg = AverageColor(bitmap, i, j, blockSize); |
| 42 | + graphics.FillRectangle(new SolidBrush(avrg), new Rectangle(i, j, blockSize, blockSize)); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + return bitmap; |
| 47 | + } |
| 48 | + |
| 49 | + public static Bitmap Pixelate(Bitmap bitmap, int blockSize = 4, int colors = -1, bool randomColors = true) |
| 50 | + { |
| 51 | + return Pixelate(bitmap, null, blockSize, colors); |
| 52 | + } |
| 53 | + static Color AverageColor(Bitmap bitmap, int x, int y, int size) |
| 54 | + { |
| 55 | + Bitmap bmp = new Bitmap(1, 1); |
| 56 | + using (Graphics g = Graphics.FromImage(bmp)) |
| 57 | + { |
| 58 | + g.InterpolationMode = InterpolationMode.NearestNeighbor; |
| 59 | + g.DrawImage(bitmap, new Rectangle(0, 0, 1, 1), new Rectangle(x, y, size, size), GraphicsUnit.Pixel); |
| 60 | + } |
| 61 | + Color pixel = bmp.GetPixel(0, 0); |
| 62 | + Pixelation_.ImageHelper.DeleteObject(bmp.GetHbitmap()); |
| 63 | + // if ColorPallete == null, then we use all color mode |
| 64 | + if (ColorPallete != null) |
| 65 | + pixel = FindNearest(pixel); |
| 66 | + return pixel; |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Generates color pallete |
| 71 | + /// </summary> |
| 72 | + /// <param name="bitmap"></param> |
| 73 | + /// <param name="size"></param> |
| 74 | + /// <param name="randomColors">Mode: random or most used</param> |
| 75 | + static void GeneratePallete(BitmapWrapper bitmap, int size, bool randomColors) |
| 76 | + { |
| 77 | + if (randomColors) |
| 78 | + { |
| 79 | + Random random = new Random((int)DateTime.Now.Ticks); |
| 80 | + ColorPallete.Add(Color.Black); |
| 81 | + ColorPallete.Add(Color.White); |
| 82 | + while(ColorPallete.Count < size + 2) |
| 83 | + { |
| 84 | + ColorPallete.Add |
| 85 | + ( |
| 86 | + bitmap.GetPixel(random.Next(0, bitmap.Bitmap.Width), random.Next(0, bitmap.Bitmap.Height)) |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + Dictionary<Color, int> colors = new Dictionary<Color, int>(); |
| 93 | + Color color; |
| 94 | + for (int i = 0; i < bitmap.Bitmap.Width; i++) |
| 95 | + { |
| 96 | + for (int j = 0; j < bitmap.Bitmap.Height; j++) |
| 97 | + { |
| 98 | + color = bitmap.GetPixel(i, j); |
| 99 | + if (colors.ContainsKey(color)) |
| 100 | + { |
| 101 | + colors[color]++; |
| 102 | + } |
| 103 | + else |
| 104 | + colors.Add(color, 1); |
| 105 | + } |
| 106 | + } |
| 107 | + List<Color> avrgColors = colors.OrderByDescending(item => item.Value).Select(item => item.Key).ToList(); |
| 108 | + ColorPallete.Add(avrgColors[0]); |
| 109 | + const int step = 30; |
| 110 | + for (int i = 1; i < colors.Count; i++) |
| 111 | + { |
| 112 | + if (Math.Abs(RGBSum(avrgColors[i]) - RGBSum(ColorPallete.Last())) > step) |
| 113 | + ColorPallete.Add(avrgColors[i]); |
| 114 | + } |
| 115 | + ColorPallete = ColorPallete.Take(size).ToList(); |
| 116 | + colors = null; |
| 117 | + |
| 118 | + } |
| 119 | + bitmap.Dispose(); |
| 120 | + } |
| 121 | + static Color FindNearest(Color color) |
| 122 | + { |
| 123 | + int difference = 1000; |
| 124 | + int rgb_color = color.R + color.G + color.B; |
| 125 | + |
| 126 | + Color nearest = color; |
| 127 | + |
| 128 | + int temp; |
| 129 | + foreach(var item in ColorPallete) |
| 130 | + { |
| 131 | + temp = item.R + item.G + item.B; |
| 132 | + if (Math.Abs(temp - rgb_color) < difference) |
| 133 | + { |
| 134 | + difference = Math.Abs(temp - rgb_color); |
| 135 | + nearest = item; |
| 136 | + } |
| 137 | + } |
| 138 | + return nearest; |
| 139 | + } |
| 140 | + public static int RGBSum(Color color) |
| 141 | + { |
| 142 | + return color.R + color.G + color.B; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments