Windows Dib file Operation specific explanation-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 is another problem. If I wanted to manipulate the data of the DIB, it was obvious that DDB could not be used: first, the color conversion occurred when the DIB turned to DDB. And then DDB can't directly extract the data from the specified pixel point. So what do we do? Windows uses a compromise approach to this goal (which improves display efficiency and performance, and the ability to manipulate pixels directly).


1.DIB section Storage and display

Windows uses a DIB block (Dib section) to store the DIB data. Its memory structure such as the following

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


In fact, it feels the same as we read the DIB data to each of our own assigned data areas, just that each data area of these Dib is now maintained by Windows itself . It is worth noting that the DIB data areas maintained by Windows are organized by HBITMAP handles, which are not the same as the hbitmap handles of the bitmap. Hbitmap handle. As for why the same handle can represent different objects, you can see the author's blog post "Dig into what windows handles are."

Keep talking about today's problem, where the data for the DIB is stored. DIB->DDB conversions occur only when using BitBlt and StretchBlt display. Obviously here BitBlt and StretchBlt will make an inference to the handle property to confirm the different actions 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 itself maintains the DIB data areas. has been optimized so that the display efficiency and performance of each data area of the DIB are higher than our own allocation and storage.


Related functions for 2.DIB section use

After defining the storage principles and conversion rules for DIB sections in 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,//includes 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 for example the following:

1. The latter two parameters are generally not considered


2. The incoming bitmapinfo* pbmi pointer includes the DIB bitmap information header (info header), the compression mask (mask), and the Palette information (color Table, primarily for bitmap depth <=8). These three must be stored continuously. The dib_rgb_colors is generally specified so that Windows will voluntarily assign the corresponding DIB section 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.

That is, the call procedure is often used such as the following:

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. The HDC parameter is only required when using dib_pal_colors parameters. Only the color palette with the DIB uses the index storage method to use these two parameters. In fact, the HDC and dib_pal_colors here are actually called by SetDIBitsToDevice and StretchDIBits functions to see their two parameters.

The memory that 4.pBits points to is hosted by the Windows operating system, but users can manipulate pbits data. Pbits memory area will also be released at the same time when deleting hbitmap.


2.GetDIBColorTable and setdibcolortable two function definitions such as the following:
UINT Getdibcolortable (  hdc hdc,          //device descriptive narrative 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 descriptive narrative 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, for example, 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);

the use of the DIB section to get and set the palette is able to mask the differences caused by OS/2 compatible bitmaps (Bitmapcoreinfo palette entries are made using Rgbtriple structures rather than bitmapinfoheader rgbquad).
3. Get dibsectiondibsection definitions such as the following:
typedef struct TAGDIBSECTION {     BITMAP              dsBm;     Bitmapinfoheader    Dsbmih;     DWORD               dsbitfields[3];     HANDLE              dshsection;     DWORD               

Use
GetObject (hbitmap, sizeof (dibsection), &dibsection);
different from bitmap. The DIB section uses GetObject to get the DIB section and 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) does not occur incorrectly.


Bitmap information is obtained through the DIB section, regardless of the differences in the different DIB bitmap formats. Bitmap information headers are used Bitmapinfoheader. The compression mask is specified separately using a DWORD, regardless of the differences that Bitmapcoreheader, Bitmapv4header, Bitmapv5hweader bring.


3. Code Demo
In the demo program. We read in 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;} Assume that you have a palette to 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];}



Maybe I'll explain. Use the DIB section to complete the read and write of the specified pixel points in the image, in fact. Once you can operate the pbits. It is not difficult to read and write at the specified pixel point.
Full Demo code Download link original, reprint please specify from http://blog.csdn.net/wenzhou1219

Windows Dib file Operation specific explanation-4. Using the DIB section

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.