public void SetPixel(int x, int y, ColorBgra color) { Pointer[x + y * Width] = color; }
public static Image Apply(this ConvolutionMatrix matrix, Image img) { int factor = Math.Max(matrix.Factor, 1); Bitmap result = (Bitmap)img.Clone(); using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly)) { int height = source.Height - 2; int width = source.Width - 2; ColorBgra[,] pixelColor = new ColorBgra[3, 3]; int pixel; ColorBgra color = new ColorBgra(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pixelColor[0, 0] = source.GetPixel(x, y); pixelColor[0, 1] = source.GetPixel(x, y + 1); pixelColor[0, 2] = source.GetPixel(x, y + 2); pixelColor[1, 0] = source.GetPixel(x + 1, y); pixelColor[1, 1] = source.GetPixel(x + 1, y + 1); pixelColor[1, 2] = source.GetPixel(x + 1, y + 2); pixelColor[2, 0] = source.GetPixel(x + 2, y); pixelColor[2, 1] = source.GetPixel(x + 2, y + 1); pixelColor[2, 2] = source.GetPixel(x + 2, y + 2); pixel = (((pixelColor[0, 0].Blue * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Blue * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Blue * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Blue * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Blue * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Blue * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Blue * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Blue * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Blue * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) pixel = 0; else if (pixel > 255) pixel = 255; color.Blue = (byte)pixel; pixel = (((pixelColor[0, 0].Green * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Green * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Green * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Green * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Green * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Green * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Green * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Green * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Green * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) pixel = 0; else if (pixel > 255) pixel = 255; color.Green = (byte)pixel; pixel = (((pixelColor[0, 0].Red * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Red * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Red * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Red * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Red * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Red * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Red * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Red * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Red * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) pixel = 0; else if (pixel > 255) pixel = 255; color.Red = (byte)pixel; color.Alpha = pixelColor[1, 1].Alpha; dest.SetPixel(x + 1, y + 1, color); } } } return result; }
public static void Pixelate(Bitmap bmp, int pixelSize) { if (pixelSize > 1) { using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true)) { for (int y = 0; y < unsafeBitmap.Height; y += pixelSize) { for (int x = 0; x < unsafeBitmap.Width; x += pixelSize) { int xLimit = Math.Min(x + pixelSize, unsafeBitmap.Width); int yLimit = Math.Min(y + pixelSize, unsafeBitmap.Height); int pixelCount = (xLimit - x) * (yLimit - y); int r = 0, g = 0, b = 0, a = 0; for (int y2 = y; y2 < yLimit; y2++) { for (int x2 = x; x2 < xLimit; x2++) { ColorBgra color = unsafeBitmap.GetPixel(x2, y2); r += color.Red; g += color.Green; b += color.Blue; a += color.Alpha; } } ColorBgra averageColor = new ColorBgra((byte)(b / pixelCount), (byte)(g / pixelCount), (byte)(r / pixelCount), (byte)(a / pixelCount)); for (int y2 = y; y2 < yLimit; y2++) { for (int x2 = x; x2 < xLimit; x2++) { unsafeBitmap.SetPixel(x2, y2, averageColor); } } } } } } }
public void SetPixel(int i, ColorBgra color) { Pointer[i] = color; }
// http://incubator.quasimondo.com/processing/superfast_blur.php public static void FastBoxBlur(Bitmap bmp, int radius) { if (radius < 1) return; using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true)) { int w = unsafeBitmap.Width; int h = unsafeBitmap.Height; int wm = w - 1; int hm = h - 1; int wh = w * h; int div = radius + radius + 1; byte[] r = new byte[wh]; byte[] g = new byte[wh]; byte[] b = new byte[wh]; byte[] a = new byte[wh]; int rsum, gsum, bsum, asum, x, y, i, p, p1, p2, yp, yi, yw; int[] vmin = new int[Math.Max(w, h)]; int[] vmax = new int[Math.Max(w, h)]; byte[] dv = new byte[256 * div]; for (i = 0; i < 256 * div; i++) { dv[i] = (byte)(i / div); } yw = yi = 0; for (y = 0; y < h; y++) { rsum = gsum = bsum = asum = 0; for (i = -radius; i <= radius; i++) { p = (yi + Math.Min(wm, Math.Max(i, 0))); ColorBgra color = unsafeBitmap.GetPixel(p); rsum += color.Red; gsum += color.Green; bsum += color.Blue; asum += color.Alpha; } for (x = 0; x < w; x++) { r[yi] = dv[rsum]; g[yi] = dv[gsum]; b[yi] = dv[bsum]; a[yi] = dv[asum]; if (y == 0) { vmin[x] = Math.Min(x + radius + 1, wm); vmax[x] = Math.Max(x - radius, 0); } p1 = (yw + vmin[x]); p2 = (yw + vmax[x]); ColorBgra color1 = unsafeBitmap.GetPixel(p1); ColorBgra color2 = unsafeBitmap.GetPixel(p2); rsum += color1.Red - color2.Red; gsum += color1.Green - color2.Green; bsum += color1.Blue - color2.Blue; asum += color1.Alpha - color2.Alpha; yi++; } yw += w; } for (x = 0; x < w; x++) { rsum = gsum = bsum = asum = 0; yp = -radius * w; for (i = -radius; i <= radius; i++) { yi = Math.Max(0, yp) + x; rsum += r[yi]; gsum += g[yi]; bsum += b[yi]; asum += a[yi]; yp += w; } yi = x; for (y = 0; y < h; y++) { ColorBgra color = new ColorBgra(dv[bsum], dv[gsum], dv[rsum], dv[asum]); unsafeBitmap.SetPixel(yi, color); if (x == 0) { vmin[y] = Math.Min(y + radius + 1, hm) * w; vmax[y] = Math.Max(y - radius, 0) * w; } p1 = x + vmin[y]; p2 = x + vmax[y]; rsum += r[p1] - r[p2]; gsum += g[p1] - g[p2]; bsum += b[p1] - b[p2]; asum += a[p1] - a[p2]; yi += w; } } } }
private static void BoxBlurVertical(UnsafeBitmap unsafeBitmap, int range) { int w = unsafeBitmap.Width; int h = unsafeBitmap.Height; int halfRange = range / 2; ColorBgra[] newColors = new ColorBgra[w]; for (int x = 0; x < w; x++) { int hits = 0; int r = 0; int g = 0; int b = 0; int a = 0; for (int y = -halfRange; y < h; y++) { int oldPixel = y - halfRange - 1; if (oldPixel >= 0) { ColorBgra color = unsafeBitmap.GetPixel(x, oldPixel); if (color.Bgra != 0) { r -= color.Red; g -= color.Green; b -= color.Blue; a -= color.Alpha; } hits--; } int newPixel = y + halfRange; if (newPixel < h) { ColorBgra color = unsafeBitmap.GetPixel(x, newPixel); if (color.Bgra != 0) { r += color.Red; g += color.Green; b += color.Blue; a += color.Alpha; } hits++; } if (y >= 0) { newColors[y] = new ColorBgra((byte)(b / hits), (byte)(g / hits), (byte)(r / hits), (byte)(a / hits)); } } for (int y = 0; y < h; y++) { unsafeBitmap.SetPixel(x, y, newColors[y]); } } }
public static Image Apply(this ConvolutionMatrix matrix, Image img) { int factor = Math.Max(matrix.Factor, 1); Bitmap result = (Bitmap)img.Clone(); using (UnsafeBitmap source = new UnsafeBitmap((Bitmap)img, true, ImageLockMode.ReadOnly)) using (UnsafeBitmap dest = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly)) { int height = source.Height - 2; int width = source.Width - 2; ColorBgra[,] pixelColor = new ColorBgra[3, 3]; int pixel; ColorBgra color = new ColorBgra(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pixelColor[0, 0] = source.GetPixel(x, y); pixelColor[0, 1] = source.GetPixel(x, y + 1); pixelColor[0, 2] = source.GetPixel(x, y + 2); pixelColor[1, 0] = source.GetPixel(x + 1, y); pixelColor[1, 1] = source.GetPixel(x + 1, y + 1); pixelColor[1, 2] = source.GetPixel(x + 1, y + 2); pixelColor[2, 0] = source.GetPixel(x + 2, y); pixelColor[2, 1] = source.GetPixel(x + 2, y + 1); pixelColor[2, 2] = source.GetPixel(x + 2, y + 2); pixel = (((pixelColor[0, 0].Blue * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Blue * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Blue * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Blue * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Blue * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Blue * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Blue * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Blue * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Blue * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) { pixel = 0; } else if (pixel > 255) { pixel = 255; } color.Blue = (byte)pixel; pixel = (((pixelColor[0, 0].Green * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Green * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Green * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Green * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Green * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Green * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Green * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Green * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Green * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) { pixel = 0; } else if (pixel > 255) { pixel = 255; } color.Green = (byte)pixel; pixel = (((pixelColor[0, 0].Red * matrix.Matrix[0, 0]) + (pixelColor[1, 0].Red * matrix.Matrix[1, 0]) + (pixelColor[2, 0].Red * matrix.Matrix[2, 0]) + (pixelColor[0, 1].Red * matrix.Matrix[0, 1]) + (pixelColor[1, 1].Red * matrix.Matrix[1, 1]) + (pixelColor[2, 1].Red * matrix.Matrix[2, 1]) + (pixelColor[0, 2].Red * matrix.Matrix[0, 2]) + (pixelColor[1, 2].Red * matrix.Matrix[1, 2]) + (pixelColor[2, 2].Red * matrix.Matrix[2, 2])) / factor) + matrix.Offset; if (pixel < 0) { pixel = 0; } else if (pixel > 255) { pixel = 255; } color.Red = (byte)pixel; color.Alpha = pixelColor[1, 1].Alpha; dest.SetPixel(x + 1, y + 1, color); } } } return(result); }
public void SetPixel(int x, int y, ColorBgra color) { Pointer[x + (y * Width)] = color; }