Windows Dib file operation in detail-4. Using the DIB section

Source: Internet
Author: User
Tags readfile

In order to improve the display performance and efficiency of the DIB, we converted the DIB to DDB, but there was a problem, and if I wanted to manipulate the data of the DIB, it was obvious that DDB could not be used: first, because of the color conversion when the DIB turned to DDB, and then the DDB was unable to extract data from the specified pixel directly. So what do we do? Windows uses a compromise approach to this goal (which improves display efficiency and performance, and can manipulate pixels directly).


1.DIB section Storage and display

Windows uses a DIB block (Dib section) to store the DIB data with the following memory structure



In fact, it feels the same as we read the DIB data to each of our own assigned data areas, but now each data area of these Dib is maintained by Windows itself . It is worth noting that these windows themselves maintain the DIB data area is organized by the HBITMAP handle, the hbitmap handle and the bitmap hbitmap handle is not the same, as to why the same handle can represent different objects can see the author's blog " Learn more about what the Windows handle really is.

Continue to say now the problem, here is stored in the DIB data, only when using BitBlt and StretchBlt display, only when the DIB->DDB conversion occurs, Obviously here BitBlt and StretchBlt will make a judgment on the handle property to confirm the different operations of hbitmap pointing to bitmap and hbitmap to the DIB section. Of course, the DIB section here is not necessarily contiguous , which is important to note.

Here, Windows maintains its own data area of the DIB and does a certain amount of optimization, so it is more efficient and performance than our own allocation and storage of each data area of the DIB.


Related functions for 2.DIB section use

Understand the storage principle and conversion rules for the DIB section of Windows, let's talk about the use of related functions of the DIB section.


1.CreateDIBSection

Hbitmap createdibsection (  hdc hdc,          //Device description table handle  CONST bitmapinfo *pbmi,//contains info Header, Mask, Color Table Data bitmapinfo pointer  UINT iusage,      //Dib_pal_colors or dib_rgb_colors  VOID *ppvbits,    // Pointer to the address where the bitmap data is stored  HANDLE hsection,    DWORD dwOffset    );


Use the following:

1. The latter two parameters are generally not considered


2. Pass in the bitmapinfo* pbmi pointer that contains the DIB bitmap information header (info header), the compression mask (mask), and the Palette information (color Table, primarily for bitmap depth <=8), which must be stored continuously, generally indicating dib_ The rgb_colors parameter so that Windows automatically assigns the corresponding DIB section according to the information provided by PBMI, including the info Header, Mask, Color table, and bits four extents, The address of the allocated bits area is written to the pointer that ppvbits points to.

The common invocation steps are as follows:

A. Bitmap information header (info header) read into the DIB, compression mask (mask), and color palette information (color Table) to Pbmi Point in memory

B.hbitmap = CreateDIBSection (null, PBMI, dib_rgb_colors, &pbits, NULL, 0);

C. Bits data read into the DIB into the memory pointed to by pbits

D.bitblt or StretchBlt display hbitmap


3. HDC parameters are required only when using the Dib_pal_colors parameter. These two parameters are required only if the color palette of the DIB uses the index storage method. In fact, the HDC and dib_pal_colors here actually end up being called by SetDIBitsToDevice and StretchDIBits functions to see both of their arguments.

The memory that the 4.pBits points to is hosted by the Windows operating system, but the user can manipulate the pbits data, and the Pbits memory area is also freed when Hbitmap is removed.


The two functions of 2.GetDIBColorTable and setdibcolortable are defined as follows:
UINT Getdibcolortable (  hdc hdc,          //Device description table handle  UINT Ustartindex,//palette start index  UINT centries,    // The number of palette entries to get  rgbquad *pcolors  //Store the address of the palette entry); UINT Setdibcolortable (  hdc hdc,          //Device description table handle  UINT Ustartindex,//palette start index  UINT centries,    // The number of palette entries to set  rgbquad *pcolors  //Store the address of the palette entry);

Used to get and set the specified palette items, which are generally used as follows.
Hdcmem = CreateCompatibleDC (NULL); SelectObject (Hdcmem, HBITMAP); Getdibcolortable (Hdcmem, Ufirstindex, Unumentries, &prgb);D Eletedc (HDCMEM);

Hdcmem = CreateCompatibleDC (NULL); SelectObject (Hdcmem, HBITMAP); Setdibcolortable (Hdcmem, Ufirstindex, Unumentries, &prgb);D Eletedc (HDCMEM);

using the DIB section to get and set the color palette, you can mask the differences caused by OS/2-compatible bitmaps (Bitmapcoreinfo palette entries take the rgbtriple structure instead of the rgbquad used by Bitmapinfoheader).
3. Get the Dibsectiondibsection definition as follows:
typedef struct TAGDIBSECTION {     BITMAP              dsBm;     Bitmapinfoheader    Dsbmih;     DWORD               dsbitfields[3];     HANDLE              dshsection;     DWORD               

Use
GetObject (hbitmap, sizeof (dibsection), &dibsection);
Unlike Bitmap,dib section, which uses GetObject to obtain a DIB section, you can see that dibsection sets bitmap as the first property, which is to ensure compatibility with bitmap, In case you don't know that the hbitmap attribute is pointing to the DIB section, then GetObject (Hbitmap, sizeof (BITMAP), &bitmap) will not be wrong.
The bitmap information can be obtained through the DIB section, regardless of the differences in the different DIB bitmap formats, the bitmap information headers are used Bitmapinfoheader, and the compression mask is specified separately using a DWORD, regardless of the Bitmapcoreheader , Bitmapv4header, Bitmapv5hweader the difference.


3. Code Demo
In the demo program, we read a picture (8bit, 16bit, 24bit) created DIB section form display, view palette and compression mask
Read in the Dib file and convert it to DIB sectionhbitmap Createdibsectionfromdibfile (ptstr szfilename) {bitmapfileheaderbmfh; BITMAPINFO*PBMI; Byte*pbits; Boolbsuccess;dworddwinfosize, dwBytesRead; Handlehfile; hbitmaphbitmap;//Open File hfile = CreateFile (szFileName, Generic_read, File_share_read, NULL, open_existing, File_flag_ Sequential_scan, NULL); if (Invalid_handle_value = = hfile) {return NULL;} Read into the Dib file header bsuccess = ReadFile (hfile, &AMP;BMFH, sizeof (Bitmapfileheader), &dwbytesread, NULL); if (!bsuccess | | (dwBytesRead! = sizeof (bitmapfileheader)) | | (Bmfh.bftype! = * (WORD *) "BM")) {CloseHandle (hfile); return NULL;} Allocates memory for the DIB Bitmapinfo and reads in the DIB data Dwinfosize = bmfh.bfoffbits-sizeof (bitmapfileheader);p BMI = malloc (dwinfosize); NULL = = Pbmi) {CloseHandle (hfile); return null;} bsuccess = ReadFile (hfile, Pbmi, Dwinfosize, &dwbytesread, NULL); if (!bsuccess | | (dwBytesRead! = dwinfosize)) {free (PBMI); CloseHandle (hfile); return NULL;} Create DIB sectionhbitmap = CreateDIBSection (null, PBMI, dib_rgb_colors, &pbits, NULL, 0); fRee (PBMI); if (null = = Hbitmap) {CloseHandle (hfile); return NULL;} Reads the bitmap data into the bits area of the Assigned DIB section (pbits points) bsuccess = ReadFile (hfile, Pbits, Bmfh.bfsize-bmfh.bfoffbits, &dwbytesread , NULL); CloseHandle (hfile); if (!bsuccess | | (dwBytesRead! = (bmfh.bfsize-bmfh.bfoffbits))) {return NULL;} return hbitmap;} If there is a palette then get the first palette entry bool Getfirstcolortableitem (Hbitmap hbitmap, Rgbquad *prgb) {HDC Hdcmem;intinum;hdcmem = CreateCompatibleDC (NULL);  SelectObject (Hdcmem, hbitmap); iNum = getdibcolortable (hdcmem, 0, 1, prgb);D Eletedc (HDCMEM); return 0==inum? False:true;} Get the first Palette entry DWORD Getfirstmaskitem (Hbitmap hbitmap) {dibsection ds; GetObject (hbitmap, sizeof (dibsection), &ds); return ds.dsbitfields[0];}


In the follow-up I will explain the use of the DIB section to complete the reading and writing of the image specified pixel, in fact, once you can manipulate the pbits, it is not difficult to complete the reading and writing of the specified pixel point.
Full Demo code Download link original, reprint please specify from http://blog.csdn.net/wenzhou1219

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.