Getting started with digital image processing-Chapter 1 Windows bitmap and palette

Source: Internet
Author: User
Tags color representation

1.1Concept of Bitmap and palette

1. True Color: the number of colors is as high as 256 × 256 × 256, that is, all colors in the R, G, and B Color Representation Methods We mentioned above. A true color chart can display all colors at most.

 

1.2BMPFile Format

(Word is a 16-bit unsigned integer, DWORD is a 32-bit unsigned integer, and long is a 32-bit long integer)

1. The BMP file is roughly divided into four parts:

1) bitmapfileheader, 14 bytes in total.

2) bitmapinfoheader, 40 bytes in total.

3) palette

4) Actual bitmap data imagedate

The color palette is actually an array with a total of biclrused elements (if the value is zero, there is an element ). The type of each element in the array is an rgbquad structure, which occupies 4 bytes. (The sorting of red, green, and blue is the opposite of the sorting of color data in general image files.~)

For bitmap that uses the color palette, the image data is the index value of the image pigment in the color palette. For a true color chart, the image data is the actual R, G, and B values.

 

2. Pay attention to the following two points:

1) the number of bytes in each row must be an integral multiple of 4. If not, you need to complete it.

2) In general,BMP file data from bottom to top, from left to right.

 

1.3Display a BMPFile C program

I. implementation process of the loadbmp file function:

1. open the file to the hfile structure variable.

2. Read bitmpfileheader and bitmapinfoheader from the hfile variable.

3. Calculate the number of bytes of the image row and the total number of bytes of the image.

4. Calculate the actual number of colors used and verify the result.

5. Fill in BF. bfsize;

6. Allocate the global memory space. If the size is (bitmapinfoheader + palette + actual image), a handle is returned.

7. lpbitmapinfoheader points to the allocated global memory space.

8. Locate the file pointer at the beginning of bitmapinfoheader and read the content into the lpbitmapinfoheader type pointer. Close the file. The difference between _ hread and _ lread.

9. If numcolors is not zero, the color palette is used. Allocate local memory for the logical palette. The size is the length of the logical palette structure plus numcolors palettentry.

10. The logpalette type pointer ppal points to the memory area and fill in the header of the logical color palette structure. Lprgb points to the start position of the palette. Fill in each item of ppal-> palpalentry.

11. Generate the global variable hpalette of the logical palette, unlock and release the local memory.

12. Get the device context handle.

13. If a logical palette is generated, select the new logical palette to DC and save the old logical palette handle to hprevpalette.

14. Generate a bitmap handle. After using the current palette, we need to restore the original palette to DC.

15. Release the device context and unlock the memory zone.

 

2. Respond to the wm_paint message:

1. Obtain the difference between the context beginpaint and getdc of the screen device.

2. Create a memory device context. When a palette is available, the palette is selected for the device context and memory context on the screen.

3. Select the memory device context, display the bitmap, release the memory device context, and release the screen device context.

Note: 1) the reason for two DC nodes is that the context content of the source (memory) device must be copied to the context of the target (screen) device when creating a bitmap.

 

There is a problem with the code running provided by the book. After modification, the error is lost, but the picture cannot be displayed! I still don't quite understand its Code. For example, how does the system send the message wm_loadbitmap?

 

Finally solved it !!

A: When I read the code provided, I didn't notice a resource file with a. RC suffix. It defines a menu, and the menu contains a sub-control open, which is associated with wm_loadbitmap. I never thought about manually editing resource files before. I didn't want to open this file if I tried to compile C ++ files in DOS yesterday. It seems that it is correct to put it on the shelf. After so long, it is finally liberated! I will write an article about the structure of the VC resource file later, which will be helpful for future study.

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.