Wednesday, 21 August 2013

Parallel Image Proccessing with C# 5.0

Parallel Image Proccessing with C# 5.0

. .
I'm trying to do some image proccessing with C# using that same old GDI
techniques, iterating throught every pixel with a nested for-loop, then
using the GetPixel and SetPixel methods on that (Bitmap) image.
I have already got the same results with the pointers approach (using
unsafe context) but I'm trying now to do the old-school Get/Set-Pixel
Methods to play with my Bitmaps . . .
Bitmap ToGrayscale(Bitmap source)
{
for (int y = 0; y < source.Height;y++ )
{
for (int x = 0; x < source.Width; x++)
{
Color current = source.GetPixel(x, y);
int avg = (current.R + current.B + current.G) / 3;
Color output = Color.FromArgb(avg, avg, avg);
source.SetPixel(x, y, output);
}
}
return source;
}
considering performance with the code above . . . it takes just tooooo
musch to finish while stressing the user out waiting for his 1800x1600
image to finish processing.
So i thought that i could use the technique that we use working with HLSL,
running a seperate function for each pixel (Pixel Shader engine (as i was
tought) copies the function returning the float4 (Color) thousands of
times on GPU to do the processing parallel).
So I tried to run a seperate Task (function) for each pixel, putting these
Task variables into a List and the 'await' for List.ToArray(). But I
failed doing that as every new Task 'awaits' to be finished before the
next one runs.
I wanted to call a new Task for each pixel to run this :
Color current = source.GetPixel(x, y);
int avg = (current.R + current.B + current.G) / 3;
Color output = Color.FromArgb(avg, avg, avg);
source.SetPixel(x, y, output);
At the end of the day I got my self an async non-blocking code but not
parallel . . . Any suggesions guys?
Thanks in advance, Zaid A.

No comments:

Post a Comment