public void SetPixel(int x, int y, ColorBgra color) { Pointer[x + y * Width] = color; }
public void SetPixel(int i, ColorBgra color) { Pointer[i] = 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 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); }