Windows Dib file operation details -5.dib and palette

Source: Internet
Author: User

The Windows palette is the product of the 256-color graphics period, and now the graphics card is at least 16bit, so the palette is basically out of the reach.

However, there are several situations where you need to use and understand the palette:

1. Ensure the normal operation of the 256 color compatibility mode on the new graphics card

2. Run the program on an older machine with 256-color graphics or less than 256-color graphics or in some industrial control situations (256-color graphics or less than 256-color graphics cards may be used to save costs)

3. Manipulating the specified pixel point data of a DIB


1. What is a palette?

Also use the following image



For example, we can divide the palette into the following types:

Palettes in a 1.DIB file

2. The logical palette object created in memory

3. Hardware palette in the graphics card


Palette as the name implies is similar to the painter's palette, in 256-color graphics card, now has 256 small lattice, each grid has a color, each drawing you can use these 256 colors to draw, if you are not satisfied with the current 256 colors, you can change the color palette, But each drawing can only be made with a confirmed 256 color. Corresponding to the computer's palette, the graphics card supports only 256 colors, stored in the graphics hardware palette , you can set the 256 colors to be used, and then all the colors you want to display will be approximately one of the 256 colors in the display, This is the so-called palette look-up table to get the most recent color-like.

As you can see, the effect of a picture on a computer is inseparable from the colors in the current graphics hardware palette , so the best way to save a bitmap with a hardware palette is to save the bitmap data while saving the bitmap palette .

In order to change the colors in the hardware palette we need to first create the logical palette object and then map the logical palette to the hardware palette.


2. Use of the palette


For a DIB with a palette, the general steps for displaying a DIB are as follows:

1. The read-in Dib is the file Header, Info Header, Mask, Color Table, bits each area

2. Create a Logical Palette object (CreatePalette) using the DIB's Palette data (color table area), typically in wm_create

3. Before WM_PAINT displays the DIB, the logical palette object created based on the DIB is selected into the current Device description table (SelectPalette) and mapped to the Hardware palette (realizepalette).

4. If you just want to ensure that the specified window is the color of the active window, finish the three-foot walk. If you want to ensure that the specified window is not the color of the active window, then the Wm_querypalette and wm_palettechanged messages are also processed. These two messages are intended to help the Windows organization System palette, which is not detailed here and is used directly in fixed mode, to view MSDN.


3. Code Demo

The demo program is a typical Windows Ribbon palette for the display of Dib. The following is part of the code, the full source code will finally give the download link.

To create a logical palette from a DIB color table

Create Data Display palette Hpalette packeddibcreatepalette (bitmapinfo *ppackeddib, hwnd hwnd) {hpalettehpalete; Hdchdc;inti, inumcolors; Logpalette*plp= NULL; rgbquad*prgb= null;if (0 = = packeddibgetcolortablesize (ppackeddib) && packeddibgetbitcount (PPACKEDDIB) > 8) There is no palette area and the bit depth is greater than 8, it is not necessary to create a palette {return NULL;} else if (0 = = Packeddibgetcolortablesize (PPACKEDDIB))//No palette area and bit depth is less than or equal to 8, then a universal palette {HDC = GetDC (hwnd) is created; Hpalete = CreateHalftonePalette (HDC); ReleaseDC (hwnd, HDC); return hpalete;} else//using the bitmap's own palette area {inumcolors = Packeddibgetnumcolors (ppackeddib);p LP = malloc (sizeof (Logpalette) + (iNumColors-1) * sizeof (paletteentry)); if (null = = PLP) {return null;} plp->palversion= 0x0300;plp->palnumentries= inumcolors;for (i = 0; i < inumcolors; i++) {Prgb = PackedDibGetColor Tableentry (Ppackeddib, i);p lp->palpalentry[i].pered = Prgb->rgbred;plp->palpalentry[i].pegreen = Prgb ->rgbgreen;plp->palpalentry[i].peblue = Prgb->rgbblue;plp->palpalentry[i].peflags = 0;} Hpalete = CreaTepalette (PLP); if (Invalid_handle_value = = Hpalete) {return NULL;} return hpalete;}}

Processing of DIB and Wm_querypalette and wm_palettechanged messages displayed in WM_PAINT

    Case WM_PAINT:        hdc = BeginPaint (hwnd, &PS), if (Hpalette) {SelectPalette (hdc, Hpalette, FALSE); RealizePalette (hdc);} if (Ppackeddib) {setdibitstodevice (hdc, 0, 0, packeddibgetwidth (ppackeddib), Packeddibgetheight (Ppackeddib), 0, 0, 0, Packeddibgetheight (Ppackeddib), Packeddibgetbitsptr (Ppackeddib), ppackeddib,dib_rgb_colors);}        EndPaint (hwnd, &PS);        return (0); case wm_querynewpalette:if (!hpalette) {return FALSE;} HDC = GetDC (hwnd); SelectPalette (hdc, Hpalette, FALSE); RealizePalette (HDC); InvalidateRect (hwnd, NULL, FALSE); ReleaseDC (hwnd, HDC); return true;case wm_palettechanged:if (!hpalette | | (HWND) Wparam==hwnd) {break;} HDC = GetDC (hwnd); SelectPalette (hdc, Hpalette, FALSE); RealizePalette (HDC); Updatecolors (HDC); ReleaseDC (hwnd, HDC);

4.DIB Operation Library

So far, all the knowledge points about the DIB display are all finished. A complete DIB operations library is implemented in the given code, primarily for opening, displaying, and saving the DIB. Unlike the experimental DIB display function, a well-integrated DIB operation library must be compatible with various versions of DIB format, non-standard DIB bitmap .

Compatibility of the 1.DIB version mainly includes compatibility with OS/2 format bitmaps (bitmapcoreheader definition brings different differences, Rgbtriple brings differences).

2. The non-standard Dib bitmap mainly refers to the Dib bitmap is not populated with clrused, sizeimage items, some less than 8bit Dib bitmap does not have a palette need to use a universal palette, some 16bit bitmap does not provide a mask and so on.

In short, a complete Dib library needs to consider the various branches of the stub, if used in commercial programs, it is best to use the various implementations of open source. The DIB library provided by the blog post is modified on the implementation of Pelzold, but there are still deficiencies, such as no consideration of compressed DIB, for technical research purposes only.


Full Source code Download link

Original, reprint please indicate from http://blog.csdn.net/wenzhou1219

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.