Last post May 09, 2007 01:48 AM by shrainpatel
Mar 27, 2007 02:38 AM|atsofttech|LINK
I am writing code to Gray Scale an Image. For that I am doing loop on each pixel as shown in code below. It is working but time required to do this is very long compare to other Graphics Processing Programs.
Is there other fast way to do this?
Mar 27, 2007 04:15 AM|Svante|LINK
I'm no grahics wiz, I'm sure there's a cool way to do this with GDI+, but at least you can start by getting rid of the floats, and move out the temp-allocations. I.e.:
for (int x = 0; x < m_Bitmap.Width; x++)
c = m_Bitmap.GetPixel(x, y);
luma = (c.R * 30 + c.G * 59 + c.B * 11)/100;
m_Bitmap.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
You might also gain a bit by pre-generating the Color-structures (since you're doing Color.FromArg(luma, luma, luma) there are only 256 possible values). Create an array with 256 entires, and place ready-mady Color-structures there. Something like this:
// Do this first - outside both loops
Color colors = new Color;
for (int i = 0; i < colors.Length; i++)
colors[i] = Color.FromArgb(i, i, i);
// then you can do...
m_Bitmap.SetPixel(x, y, colors[luma]);
Mar 27, 2007 04:47 AM|atsofttech|LINK
Thanks. This makes sense and helpful.
But I am still looking for fast way. Please let me know if anybody have idea.
Mar 28, 2007 12:57 AM|atsofttech|LINK
Solution is here
May 09, 2007 01:48 AM|shrainpatel|LINK
Its really great article, It helps me lot.