c# - How to parallelize image pixelation? -


i've been playing around image pixelation algorithms , came across post.

private static bitmap pixelate(bitmap image, rectangle rectangle, int32 pixelatesize) {     bitmap pixelated = new system.drawing.bitmap(image.width, image.height);      // make exact copy of bitmap provided     using (graphics graphics = system.drawing.graphics.fromimage(pixelated))         graphics.drawimage(image, new system.drawing.rectangle(0, 0, image.width, image.height),             new rectangle(0, 0, image.width, image.height), graphicsunit.pixel);      // @ every pixel in rectangle while making sure we're within image bounds     (int32 xx = rectangle.x; xx < rectangle.x + rectangle.width && xx < image.width; xx += pixelatesize)     {         (int32 yy = rectangle.y; yy < rectangle.y + rectangle.height && yy < image.height; yy += pixelatesize)         {             int32 offsetx = pixelatesize / 2;             int32 offsety = pixelatesize / 2;              // make sure offset within boundry of image             while (xx + offsetx >= image.width) offsetx--;             while (yy + offsety >= image.height) offsety--;              // pixel color in center of pixelated area             color pixel = pixelated.getpixel(xx + offsetx, yy + offsety);              // each pixel in pixelate size, set center color             (int32 x = xx; x < xx + pixelatesize && x < image.width; x++)                 (int32 y = yy; y < yy + pixelatesize && y < image.height; y++)                     pixelated.setpixel(x, y, pixel);         }         }      return pixelated; } 

everything works fine, have hard time parallelizing (hope it's word) code. know you're supposed use lockbits , not, i'm having difficult time doing it. i'm sure there's way pixel block / core.

parallel code not strong point.

using lockbitmap class found here code becomes using lockbits:

private static bitmap pixelatelockbits(bitmap image, rectangle rectangle, int pixelatesize) {     using (lockbitmap lockbitmap = new lockbitmap(image))     {         var width = image.width;         var height = image.height;          (int32 xx = rectangle.x; xx < rectangle.x + rectangle.width && xx < image.width; xx += pixelatesize)         {             (int32 yy = rectangle.y; yy < rectangle.y + rectangle.height && yy < image.height; yy += pixelatesize)             {                 int32 offsetx = pixelatesize / 2;                 int32 offsety = pixelatesize / 2;                  // make sure offset within boundry of image                 while (xx + offsetx >= image.width) offsetx--;                 while (yy + offsety >= image.height) offsety--;                  // pixel color in center of pixelated area                 color pixel = lockbitmap.getpixel(xx + offsetx, yy + offsety);                  // each pixel in pixelate size, set center color                 (int32 x = xx; x < xx + pixelatesize && x < image.width; x++)                     (int32 y = yy; y < yy + pixelatesize && y < image.height; y++)                         lockbitmap.setpixel(x, y, pixel);             }         }     }     return image; } 

you can transform inner 2 fors parallel.for:

parallel.for(xx, xx + pixelatesize, x => {     if (x < width)     {         parallel.for(yy, yy + pixelatesize, y =>         {             if (y < height)             {                 lockbitmap.setpixel(x, y, pixel);             }         });     } }); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -