Browse Source

Improving convolution filter speed with Parallel.For

TODO: Check this again and expand to other methods

Former-commit-id: d1e608bee96e75c26c0665518374f4db5af8e899
Former-commit-id: a43b63910def35dc41b4b5a494d00f7c9089e398
pull/17/head
James South 12 years ago
parent
commit
e44e2fd517
  1. 4
      src/ImageProcessor.Playground/Program.cs
  2. 10
      src/ImageProcessor/Imaging/Filters/EdgeDetection/ConvolutionFilter.cs

4
src/ImageProcessor.Playground/Program.cs

@ -77,9 +77,9 @@ namespace ImageProcessor.PlayGround
//.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80) //.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80)
//.Resize(layer) //.Resize(layer)
//.DetectEdges(new KirschEdgeFilter()) //.DetectEdges(new KirschEdgeFilter())
//.DetectEdges(new LaplacianOfGaussianFilter()) .DetectEdges(new LaplacianOfGaussianEdgeFilter())
//.EntropyCrop() //.EntropyCrop()
.Filter(MatrixFilters.Comic) //.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.Comic) //.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.HiSatch) //.Filter(MatrixFilters.HiSatch)
//.Pixelate(8) //.Pixelate(8)

10
src/ImageProcessor/Imaging/Filters/EdgeDetection/ConvolutionFilter.cs

@ -13,6 +13,7 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
using System; using System;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Threading.Tasks;
using ImageProcessor.Common.Extensions; using ImageProcessor.Common.Extensions;
using ImageProcessor.Imaging.Filters.Photo; using ImageProcessor.Imaging.Filters.Photo;
@ -92,7 +93,10 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
using (FastBitmap destinationBitmap = new FastBitmap(destination)) using (FastBitmap destinationBitmap = new FastBitmap(destination))
{ {
// Loop through the pixels. // Loop through the pixels.
for (int y = 0; y < height; y++) Parallel.For(
0,
height,
y =>
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
@ -131,6 +135,7 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
if (offsetX < width) if (offsetX < width)
{ {
// ReSharper disable once AccessToDisposedClosure
Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY); Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY);
double r = currentColor.R; double r = currentColor.R;
double g = currentColor.G; double g = currentColor.G;
@ -151,9 +156,10 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
byte blue = bX.ToByte(); byte blue = bX.ToByte();
Color newColor = Color.FromArgb(red, green, blue); Color newColor = Color.FromArgb(red, green, blue);
// ReSharper disable once AccessToDisposedClosure
destinationBitmap.SetPixel(x, y, newColor); destinationBitmap.SetPixel(x, y, newColor);
} }
} });
} }
} }
} }

Loading…
Cancel
Save