public void Apply(PixelFunction pixelFunc, Rectangle region) { Rectangle rect = ClampRectangle(region, Bounds); if (Rectangle.IsAreaNegativeOrEmpty(rect)) { return; } int sourceOffset = rect.Y * ScanlineSize + rect.X * _bytesPerPixel; for (int y = 0; y < rect.Height; y++) { int lineIndex = sourceOffset + y * ScanlineSize; for (int x = 0; x < rect.Width; x++) { int index = lineIndex + x * _bytesPerPixel; Color result = pixelFunc(new Color( _data[index + 0], _data[index + 1], _data[index + 2], _data[index + 3])); _data[index + 0] = result.R; _data[index + 1] = result.G; _data[index + 2] = result.B; _data[index + 3] = result.A; } } }
// Calculates new pixel values using the given pixel function. public static int[,] Compute(int[,] image, PixelFunction f) { int[,] result = new int[image.GetLength(0), image.GetLength(1)]; Parallel.For(0, image.GetLength(0) * image.GetLength(1), i => { int x = i % image.GetLength(0); int y = i / image.GetLength(0); result[x, y] = f(image[x, y]); }); return(result); }
public void Apply(PixelFunction pixelFunc) { Apply(pixelFunc, Bounds); }
public void Apply(PixelFunction pixelFunc, Rectangle region) { Rectangle rect = ClampRectangle(region, Bounds); if (Rectangle.IsAreaNegativeOrEmpty(rect)) return; int sourceOffset = rect.Y * ScanlineSize + rect.X * _bytesPerPixel; for (int y = 0; y < rect.Height; y++) { int lineIndex = sourceOffset + y * ScanlineSize; for (int x = 0; x < rect.Width; x++) { int index = lineIndex + x * _bytesPerPixel; Color result = pixelFunc(new Color( _data[index + 0], _data[index + 1], _data[index + 2], _data[index + 3])); _data[index + 0] = result.R; _data[index + 1] = result.G; _data[index + 2] = result.B; _data[index + 3] = result.A; } } }