Windows DIB file operations specifically explain -5.dib and palette

Source: Internet
Author: User

The Windows palette is the product of the 256-color graphics period, and today's graphics cards are 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 execution of the 256 color compatibility mode on the new graphics card

2. Execute programs on older machines 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 for cost savings)

3. Manipulating the specified pixel point data of a DIB


1. What is a palette?

The same is used for the next picture

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2vuemhvdteymtk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


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

The palette in the 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 paint, assuming you are not comfortable with the current 256 colors, you can change the color palette, But each picture can only be used to confirm the color of 256. corresponding to the computer palette, the graphics card supports only 256 colors, stored in the graphics hardware palette , you can set the 256 colors to use. Then all the colors you want to display will be shown in approximately one of the 256 colors, which is called a palette look-up to get a near-term color.

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 from a hardware palette is to save the bitmap data at the same time as 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 process of displaying a DIB such as the following:

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 (the color table area). Generally in the 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. Assuming that you want to ensure that the specified form is the color that is displayed when it is the active form, you are done with these three steps. If you want to ensure that the specified form is not the color of the active form, you also process the Wm_querypalette and wm_palettechanged messages.

These two messages are intended to help the Windows Organizational system palette. Not detailed here. Use directly in fixed mode to view MSDN in detail.


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 will finally give the download link.

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 version number DIB format, non-standard DIB bitmap .

The compatibility of the 1.DIB version number mainly includes the compatibility of OS/2 format bitmaps (the various differences caused by the bitmapcoreheader definition. Rgbtriple the difference).

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 has no palette to use the Universal palette, some 16bit bitmap does not provide a mask and so on.

In short, a complete Dib library needs to be considered for a variety of stub, assuming that it is used in commercial programs. It is best to use various implementations already open source. The DIB library provided by the blog post has been changed in the implementation of Pelzold, but there are still deficiencies, such as the compression of the DIB is not considered, for technical research purposes only.


Full source Download link

Original. Reprint please specify from http://blog.csdn.net/wenzhou1219

Windows DIB file operations specifically explain -5.dib and palette

Related Article

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.