Pixel operations of 24-Bit Bitmap in 32bpp window mode (1)

Source: Internet
Author: User

Just a few days after learning DirectDraw, the author's example in the book is mostly the full screen program of the 8-bit color palette, and it is to read the 8-Bit Bitmap, you can easily use setdisplaymode to change the display mode, write pixels by byte. In the window mode, the common 32 BPP and 24-Bit Bitmap are not so convenient, and there is no complete example in the book.

 

DirectDraw in window mode has the following problems:

1.24-Bit Bitmap pixel format to 32-bit pixel format conversion

2. You cannot use flip. You need to use BLT to copy from the off-screen surface to the main display surface.

3. determine the real user window.

4. cropping.

 

The last three questions are well illustrated in the book, but there is not much to say about pixel operations at. The author means not to immediately get stuck in technical details. However, when I want to write a DD program for a window independently today, I have encountered pixel operations problems.

 

0. I thought so.

24-Bit Bitmap data zone [RR] [Gg] [BB]……

When ing each pixel of a 32-bit bpp, you can fill in the first a-bit zero: [00] [RR] [Gg] [BB]

However, the color is incorrect.

 

How is the data area of a 1.24-Bit Bitmap stored?

The bitmap's 14 bytes + 40 bytes + 4 * nbytes three heads will not be mentioned. Read the corresponding Microsoft structure with _ lread or readfile. Because of the incorrect color, I used ultraedit to view a 24-bit full purple bitmap. The data area is as follows:

[Ff] [00] [80]……

I checked the color table and waited for the following:

[BB] [Gg] [RR].

We can see that the order is the opposite to that of RGB.

 

2. display the pixel format of the table

The program is modified and written in reverse order on the display surface:

[BB] [Gg] [RR] [AA]……

The test was successful.

 

3. Usage

Use macros to generate a DWORD:

 # DEFINE _ rgb32bit (A, R, G, B) (B) + (g) <8) + (r) <16) + () <24 ))

_ Rgb32bit (0,128, 0,255) is equivalent to 00 80 00 ff.

Because it is littleendian, the order of DWORD in memory is [ff] [00] [80] [00].

Use the following statement to color the 24-Bit Bitmap on (x, y:

 For (INT y = 0; y <screen_height; ++ y) <br/>{< br/> for (INT x = 0; x <screen_width; ++ X) <br/>{< br/> uchar Blue = (bitmap. buffer [y * screen_width * 3 + X * 3 + 0]), <br/> Green = (bitmap. buffer [y * screen_width * 3 + X * 3 + 1]), <br/> Red = (bitmap. buffer [y * screen_width * 3 + X * 3 + 2]); </P> <p> DWORD pixel = _ rgb32bit (0, red, green, blue ); </P> <p> * (DWORD *) (video_buffer + x * 4 + y * mempitch) = pixel; <br/>}< br/>} 

 

4. Supplement

The above Code only applies to the case where the bitmap pixel width is a multiple of 4, and when the number of Bitmap pixels is not a multiple of 4, the appropriate 0 will be added in each line, therefore, you need to modify the index calculation. The following code converts the buffer sorted by Reverse rows in BMP into a normal order, and calculates the zeros to read a bitmap24 example.

Typedef struct bitmap24_typ <br/>{< br/> bitmapfileheader fileheader; <br/> bitmapinfoheader infoheader; <br/> uchar * buffer; <br/>} bitmap24, * bitmap24_ptr; </P> <p> int loadbitmap24 (bitmap24_ptr bitmap, char * filename) <br/>{< br/> handle hfile; <br/> DWORD dwbytesread = 0; </P> <p> hfile = createfile (filename, <br/> generic_read, <br/> file_1__read, <br/> null, <br/> open_existing, <br/> file_attribute_normal, <br/> null); </P> <p> If (hfile = invalid_handle_value) <br/>{< br/> return 0; <br/>}</P> <p> If (false = readfile (hfile, & (Bitmap-> fileheader), sizeof (bitmapfileheader), & dwbytesread, null )) <br/>{< br/> closehandle (hfile); <br/> return 0; <br/>}</P> <p> If (false = readfile (hfile, & (Bitmap-> infoheader), sizeof (bitmapinfoheader), & dwbytesread, null )) <br/>{< br/> closehandle (hfile); <br/> return 0; <br/>}</P> <p> uchar * orgbuffer = (uchar *) malloc (Bitmap-> infoheader. bisizeimage); </P> <p> If (false = readfile (hfile, orgbuffer, bitmap-> infoheader. bisizeimage, & dwbytesread, null) <br/>{< br/> closehandle (hfile); <br/> return 0; <br/>}</P> <p> closehandle (hfile); </P> <p> long pixelwidth = Bitmap-> infoheader. biwidth; <br/> long pixelheight = Bitmap-> infoheader. biheight; <br/> long rowbytewidth; <br/> If (pixelwidth % 4 = 0) <br/>{< br/> rowbytewidth = pixelwidth * 3; <br/>}< br/> else <br/> {<br/> rowbytewidth = pixelwidth * 3 + (pixelwidth % 4 ); <br/>}</P> <p> Bitmap-> buffer = (uchar *) malloc (pixelwidth * pixelheight * 4 ); </P> <p> for (INT y = 0; y <pixelheight; ++ y) <br/> {<br/> for (INT x = 0; x <pixelwidth; ++ X) <br/>{< br/> uchar Blue = orgbuffer [y * rowbytewidth + x * 3 + 0]; <br/> uchar Green = orgbuffer [y * rowbytewidth + x * 3 + 1]; <br/> uchar Red = orgbuffer [y * rowbytewidth + x * 3 + 2]; </P> <p> DWORD pixel = _ rgb32bit (0, red, green, blue); </P> <p> * (DWORD *) (Bitmap-> buffer + x * 4 + (pixelheight-y-1) * pixelwidth * 4) = pixel; <br/>}</P> <p> free (orgbuffer); </P> <p> return 1; <br/>}</P> <p> void unloadbitmap24 (bitmap24_ptr Bitmap) <br/>{< br/> free (Bitmap-> buffer ); <br/> Bitmap-> buffer = NULL; <br/>}

 

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.