Efficiency Comparison of the three types of image processing, with the fastest pointer Method

Source: Internet
Author: User

// Pixel method, about 400 milliseconds
Private void pixel_click (Object sender, eventargs E)
{
If (curbitmap! = NULL)
{
Mytimer. cleartimer ();
Mytimer. Start ();
Color curcolor;
Int ret;
For (INT I = 0; I <curbitmap. width; I ++)
{
For (Int J = 0; j <curbitmap. height; j ++)
{
Curcolor = curbitmap. getpixel (I, j );
Ret = (INT) (curcolor. R * 0.299 + curcolor. g * 0.587 + curcolor. B * 0.114 );
Curbitmap. setpixel (I, j, color. fromargb (Ret, RET, RET ));
}
}
Mytimer. Stop ();
Timebox. Text = mytimer. Duration. tostring ("#######") + "millisecond ";
Invalidate ();
}
}

// Memory method, which is about 2.5 ms.
Private void memory_click (Object sender, eventargs E)
{
If (curbitmap! = NULL)
{
Mytimer. cleartimer ();
Mytimer. Start ();
Rectangle rect = new rectangle (0, 0, curbitmap. Width, curbitmap. Height );
System. Drawing. imaging. bitmapdata bmp data = curbitmap. lockbits (rect, system. Drawing. imaging. imagelockmode. readwrite, curbitmap. pixelformat );
Intptr = BMP data. scan0;
Int bytes = curbitmap. Width * curbitmap. Height * 3;
Byte [] rgbvalues = new byte [bytes];
System. runtime. interopservices. Marshal. Copy (PTR, rgbvalues, 0, bytes );
Double colortemp = 0;
For (INT I = 0; I <rgbvalues. length; I + = 3)
{
Colortemp = rgbvalues [I + 2] * 0.299 + rgbvalues [I + 1] * 0.587 + rgbvalues [I] * 0.114;
Rgbvalues [I] = rgbvalues [I + 1] = rgbvalues [I + 2] = (byte) colortemp;
}
System. runtime. interopservices. Marshal. Copy (rgbvalues, 0, PTR, bytes );
Curbitmap. unlockbits (BMP data );
Mytimer. Stop ();
Timebox. Text = mytimer. Duration. tostring ("#######") + "millisecond ";
Invalidate ();
}
}

// This method is the fastest, about 1.7 milliseconds.

Private void pointer_click (Object sender, eventargs E)
{
If (curbitmap! = NULL)
{
Mytimer. cleartimer ();
Mytimer. Start ();
Rectangle rect = new rectangle (0, 0, curbitmap. Width, curbitmap. Height );
System. Drawing. imaging. bitmapdata bmp data = curbitmap. lockbits (rect, system. Drawing. imaging. imagelockmode. readwrite, curbitmap. pixelformat );
Byte temp = 0;
Unsafe
{
Byte * PTR = (byte *) (BMP data. scan0 );
For (INT I = 0; I <BMP data. height; I ++)
{
For (Int J = 0; j <BMP data. width; j ++)
{
Temp = (byte) (0.299 * PTR [2] + 0.587 * PTR [1] + 0.114 * PTR [0]);
PTR [0] = PTR [1] = PTR [2] = temp;
PTR + = 3;
}
PTR + = BMP data. stride-BMP data. Width * 3;
}
}
Curbitmap. unlockbits (BMP data );
Mytimer. Stop ();
Timebox. Text = mytimer. Duration. tostring ("#######") + "millisecond ";
Invalidate ();
}
}

Http://hi.baidu.com/goga

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.