Quantified GDI +: Fast bitmap read/write pixels

Source: Internet
Author: User

Preface:

This article tests the bitmap operation (get/setpixel) in GDI +, rather than seeking the fastest Bitmap Processing Method. If you want to increase the speed, use technologies other than GDI +, such as parallel computing, MMX/SSE command calling, and Cuda.

This is an old technique:
Getpixel and setpixel are often used when using the bitmap class, but these two methods are relatively slow, so lockbits/unlockbits are usually used to lock the bitmap in the memory to speed up the operation.
The standard reference on msdn is as follows:

Private void lockunlockbitsexample (painteventargs e) {// create a new bitmap. create bitmap BMP = new Bitmap ("C: \ fakephoto.jpg"); // lock the bitmap's bits. lock bitmap rectangle rect = new rectangle (0, 0, BMP. width, BMP. height); system. drawing. imaging. bitmapdata bmp data = BMP. lockbits (rect, system. drawing. imaging. imagelockmode. readwrite, BMP. pixelformat); // get the address of the first line. get the first line address intptr = BMP data. scan0; // declare an array to hold the bytes of the bitmap. define an array to save bitmap int bytes = math. ABS (BMP data. stride) * BMP. height; byte [] rgbvalues = new byte [bytes]; // copy the RGB values into the array. copy the RGB value to the array system. runtime. interopservices. marshal. copy (PTR, rgbvalues, 0, bytes); // set every third value to 255. A 24bpp bitmap will look red. the bitmap with 3rd values per pixel set to BPP will become red for (INT counter = 2; counter <rgbvalues. length; counter + = 3) rgbvalues [Counter] = 255; // copy the RGB values back to the bitmap to copy the RGB value back to the bitmap system. runtime. interopservices. marshal. copy (rgbvalues, 0, PTR, bytes); // unlock the bits. unlock BMP. unlockbits (BMP data); // draw the modified image. draw the updated bitmap E. graphics. drawimage (BMP, 0,150 );}

Because I am relatively idle, I am wondering how fast is the acceleration?
To this end, I slightly adjusted the bitmapex class I used previously (remember whether it should be facial recognition or code), changed it to fastbitmap, and then created a test program, A series of test cases were collected. (Click the left-side frame to open the image file. No exception is reported)

Post a post and then steamed bread

--------

This is an old technique:
Getpixel and setpixel are often used when using the bitmap class, but these two methods are relatively slow, so lockbits/unlockbits are usually used to lock the bitmap in the memory to speed up the operation.
The standard reference on msdn is as follows:

C # code
Private void lockunlockbitsexample (painteventargs e) {// create a new bitmap. create bitmap BMP = new Bitmap ("C: \ fakephoto.jpg"); // lock the bitmap's bits. lock bitmap rectangle rect = new rectangle (0, 0, BMP. width, BMP. height); system. drawing. imaging. bitmapdata bmp data = BMP. lockbits (rect, system. drawing. imaging. imagelockmode. readwrite, BMP. pixelformat); // get the address of the first line. get the first line address intptr = BMP data. scan0; // declare an array to hold the bytes of the bitmap. define an array to save bitmap int bytes = math. ABS (BMP data. stride) * BMP. height; byte [] rgbvalues = new byte [bytes]; // copy the RGB values into the array. copy the RGB value to the array system. runtime. interopservices. marshal. copy (PTR, rgbvalues, 0, bytes); // set every third value to 255. A 24bpp bitmap will look red. the bitmap with 3rd values per pixel set to BPP will become red for (INT counter = 2; counter <rgbvalues. length; counter + = 3) rgbvalues [Counter] = 255; // copy the RGB values back to the bitmap to copy the RGB value back to the bitmap system. runtime. interopservices. marshal. copy (rgbvalues, 0, PTR, bytes); // unlock the bits. unlock BMP. unlockbits (BMP data); // draw the modified image. draw the updated bitmap E. graphics. drawimage (BMP, 0,150 );}

Because I am relatively idle, I am wondering how fast is the acceleration?
To this end, I slightly adjusted the bitmapex class I used previously (remember whether it should be facial recognition or code), changed it to fastbitmap, and then created a test program, A series of test cases were collected. (Click the left-side frame to open the image file. No exception is reported)

The test cases are as follows:

To ensure that the file format is not affected, use the BMP format of 24bpp. (Thanks to the development of science and technology, the memory cabbage price, otherwise a single file will be nearly MB, which will really bother me .)

The test is divided into getpixel and setpixel to separate read and write. The test code (take getpixel as an example) is very simple, as follows:

for (int y = 0; y < h; y++){    for (int x = 0; x < w; x++)    {        tmp = bmp.GetPixel(x, y);    }}

BMP is bitmap and fastbitmap, respectively.
In order to focus on the comparison results, although pixel-by-pixel image traversal is very time-consuming, parallel computing is not deliberately used and completed using a single CPU core. So if you want to use this program to test extremely large images (10000 × 10000 orders of magnitude or above), please be careful.

The test result is as follows:

From the test results, the so-called "fast" has two brushes, with an average efficiency of 90% ~ 95%, that is, the performance has been improved by 10 ~ 20 times.
Although this result is not very fast, I think it is basically the limit of GDI + (the rest is the improvement of machine performance, you can try technologies such as parallel computing, C ++ native, direct call of MMX/SSE commands, and Cuda.
I don't know how many times bitmap is used in the current technological development. I just think bitmap can be a good choice when we pursue a balance between development efficiency and performance.

Download the test program: Click to download

Postscript

Some friends pointed out:

The lockbits of GDI + temporarily read image data to the memory, which is not suitable for professional image processing software. If it is professional, the format of an image loaded in the memory should be fixed, in this way, the algorithm directly accesses the data in the memory. Functions such as getpixel do not exist for professional image processing, but are convenient to process small batches of data such as screen color or DC color.

To speed up, image processing algorithms are first written in common languages and optimized to the core of the algorithm. If the speed is not enough, consider further optimization using the Assembly. The simpler the algorithm, the higher the speed that can be improved by using assembly optimization, for example, the simplest color inversion algorithm. For images of 3000*4000*24, the processing time of a general language is about Ms, 20 ms is enough for assembly. However, for complex algorithms, it is not obvious that assembly can be upgraded.

Although this article does not aim at speed, it is just a "test" speed. I tried to optimize the code and used the "Reversed" operation as an example to test it.

Test Case selection #7 (4096x4096 @ 24bpp), 299 ms, as shown below:

Then the algorithm was improved again, and the speed was improved by 17% ms (about ).

This result is much faster than the general operation method, but it is a little more (Laugh) than the general language about Ms ). Compared with the compiled "20 ms" (without experimental data), the difference is far greater.

It seems interesting to optimize the code. I will continue to try to optimize it. By adjusting the call structure, improving the algorithm, and using multi-thread parallel computing, we finally entered 50 ms.

It is still based on lockbits/unlockbits of the bitmap class.

Language: C #, C # pointer

Testing Machine: I3 380 m @ 2.53 GHz, 2.92g DDR3-1333, Windows 7 32-bit

Speed: about 50 ms

Comparison of testing results by netizens

The following is the test result data provided by some enthusiastic netizens for their reference.

In the test project, bitmap images of the 4096x4096x24bpp type are reversed.

Test 1:

Imagewizard(Author: laviewpbt)

Implementation: Assembly + VB. NET

Configuration: I3 380 m @ 2.53 GHz, 2.92g DDR3-1333, Windows 7 32-bit

Time: 25 ms

Test 2:

Temporary Test(Author: Lan zhengpeng)

Implementation: VC ++. Net calls the SSE command

Configuration: i7 860@2.93GHz, 12g pc1333 memory, Windows 7 64-bit

Time: 12 ~ 19 ms

Test 3:

Gebimage(Author: xiaotie)

Implementation: C # rewrite all image libraries and unsafe pointers

Configuration: Better than Test 1

Time used: 33 Ms

Test 4:

This article(Author: byoy)

Implementation: GDI +, unsafe pointer

Configuration: Same as test 1

Time: 46 Ms

(End)

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.