bValues[(y * image.Width + x) * 3 + 1] = cIn.G;
rgbValues[(y * image.Width + x) * 3 + 2] = cIn.R;
}
public static implicit operator Image(FastBitmap bmp)
{
return bmp.image;
}
public static implicit operator Bitmap(FastBitmap bmp)
{
return bmp.image;
}
public void LockPixels()
{
LockPixels(new Rectangle(0, 0, image.Width, image.Height));
}
private void LockPixels(Rectangle area)
{
if (locked)
return;
locked = true;
bitmapData = image.LockBits(area, ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
IntPtr ptr = bitmapData.Scan0;
int stride = bitmapData.Stride;
int numBytes = image.Width * image.Height * 3;
rgbValues = new byte[numBytes];
Marshal.Copy(ptr, rgbValues, 0, numBytes);
}
public void UnlockPixels()
{
if (!locked)
return;
locked = false;
Marshal.Copy(rgbValues, 0, bitmapData.Scan0, image.Width * image.Height * 3);
image.UnlockBits(bitmapData);
}
}
With the class completed we can move on to the inversion algorithm. This part is actually quite simple.
public new static void DoFilter(FastBitmap image)
{
image.LockPixels();
byte[] pixels = image.GetAllPixels();
for (int i = 0; i < pixels.Length; i++)
{
pixels[i] = (byte)(255 - pixels[i]);
}
image.SetAllPixels(pixels);
image.UnlockPixels();
}
Now we have an algorithm that in union with our FastBitmap class can invert a 320x240 image nearly instantly on a mobile device.
Thanks for reading. Let me know what you thought about this by leaving a comment!
[Author:Roshan Khan]
http://blogs.msdn.com/windowsmobile/archive/2008/04/15/faster-c.aspx
上一页 [1] [2]