Windows Dib file operation in detail -1.dib read, save, and display

Source: Internet
Author: User

A DIB (device-independent bitmap) is a bitmap file stored on disk that can be read from disk to memory or saved to disk, and its disk file structure is standardized and can be displayed on Linux, UNIX, and windows with the same effect. The bitmap is the closest hardware image format, and the core of Windows is the bitmap, and its SDK API specifically provides a set of functions for manipulating the Dib file. However, for one reason or another, efficient and reasonable use of these DIB APIs is necessary to understand a lot of history and use of the background, here I smoke cocoon stripping introduction and demonstration of the use of DIB, I believe that you better use the Dib file is helpful, because the DIB function is more, here are divided into three parts, The first is the read, save, and display of the DIB.


Composition of 1.DIB files

The composition of the Dib file is described in many places and is not detailed here. Mainly divided into the following parts:

    • File Table Header
    • Information table Header
    • RGB color table (not necessarily)
    • Bit image bit

which

    1. The file header contains the file type (BMP), the file size, and the offset value of the actual pixel data saved in the file.
    2. The information header contains information such as the size, number of bits, bit depth, compression, and mask of the bitmap.
    3. The RGB color table is also known as a palette, 8-bit bitmaps may contain color palette information, 16 bits and above generally no color palette
    4. Bit image bit is the actual bitmap data save area


Where the information header and RGB palette are called bitmap information.


2.Windows DIB Memory data structure

We want to read the DIB data into memory, then we must allocate the corresponding memory, read the data into the corresponding memory area, where the SDK provides data structure is a variety of structures, the structure of the various fields corresponding to the individual information values in the disk file. We use the most commonly used DIB structures here for the sake of logic.


, the data in the file is read into the corresponding memory structure, and the data in the memory structure is written to the corresponding file to complete the file's saving.

which

    1. File information header data read into the Bitmapfileheader structure
    2. Bitmap information headers are read into the bitmapinfoheader structure
    3. The bitmap palette is read into the RGBQUAD structure array
    4. The bitmap data is read to the data area of the corresponding size allocated according to the information provided by the bitmap information header


Bitmapinfoheader and Rgbquad[0] as members of the BITMAPINFO structure, on the one hand to the disk file corresponding to, on the other hand, to access the color palette data convenience.


Each segment of the disk file here must be contiguous on disk, but the corresponding in-memory file information header, bitmap information, and bitmap data are not necessarily contiguous. Therefore, in the read file you can either read the disk files in a single time into the contiguous memory, or read into three separate contiguous memory, the following will do the relevant demonstration.


The 3.DIB display reads the DIB into the corresponding memory structure, and now how do we show the pixel data in the corresponding memory structure? For example, the pixel value of the fixed point in memory is how to step into the specified position on the monitor, and there are some simplifications that do not consider the compression bitmap and display accuracy.
This diagram looks very complex, actually I do not want to tell it completely here, if you have a certain DIB experience, then this picture can help you better understand the entire display process, if there is no use of DIB experience, then do not look at this bitmap carefully. From this picture, we know that the data we need to display is:in-memory bitmap data, in-memory bitmap information, in-memory bitmaps specify areas to display, areas for display on the display。
After providing the information you need, Windows provides the SetDIBitsToDevice and StretchDIBits functions to help us complete this display process. The prototypes for the two functions are as follows
int SetDIBitsToDevice (    hdc hdc,                 //handle to DC  int xdest,               //X-coord of destination Upper-left Corner
   int ydest,               //Y-coord of Destination Upper-left corner   DWORD dwwidth,           //Source Rectangle width  DWORD D Wheight,          //source Rectangle height  int xsrc,                //X-coord of source lower-left corner  int ysrc,                /  /Y-coord of source Lower-left corner  uint Ustartscan,         //First scan line in array  UINT cscanlines,         // Number of scan lines  const VOID *lpvbits,     //array of DIB bits  CONST bitmapinfo *lpbmi,//Bitmap Informat Ion  UINT Fucoloruse          //RGB or palette indexes  );

int StretchDIBits (  hdc hdc,                      //handle to DC  int xdest,                    //X-coord of destination  upper-left Corner in T ydest,                    //Y-coord of destination upper-left corner  int ndestwidth,               //width of destination rectangle  in  T ndestheight,              //height of destination rectangle  int xsrc,                     //X-coord of source upper-left corner  int YSRC,                     //Y-coord of source upper-left corner  int nsrcwidth,                //width of source rectangle  int Nsrcheig  HT,               //height of source rectangle  Const VOID *lpbits,           //Bitmap bits  CONST bitmapinfo *lpbitsinfo,// Bitmap data  UINT iusage,                  //Usage options  DWORD dwrop                   //Raster operation code  );

You can see the top-to-bottom information for the two functions: the DC to display window, the area to display for display, the part to display in the bitmap, the bitmap data, the bitmap information, other options, see MSDN for each parameter.

4. Code demo here The demo program interface is as follows

You can choose to read in the read-only or fragment-read-in, the difference between the two ways is to read in memory after the individual segments of the bitmap are contiguous. The save also corresponds to two ways to read in. Display can choose to display the normal center or stretch to the entire window screen, the former corresponds to the SetDIBitsToDevice function, the latter corresponds to the StretchDIBits function. Only one-time read-in, save, and display code is available, and the complete code is attached.
BOOL dibtotalload (ptstr szbmpfile, Pbitmapfileheader *ppbmfh, Pbitmapinfo *ppbmi, Pbyte *ppbits, PLONG pBmpWidth, PL ONG pbmpheight) {handlehfile;dworddwfilesize, dwbytesread; boolbsuccess;//Open File hfile = CreateFile (Szbmpfile, Generic_read, File_share_read, NULL, open_existing, File_flag_ Sequential_scan, NULL); if (Invalid_handle_value = = hfile) {return FALSE;} Determine the BMP file size (all parts) dwfilesize = GetFileSize (hfile, NULL);//allocate corresponding size memory for saving disk BMP file Contents *PPBMFH = malloc (dwfilesize); *PPBMFH)) {CloseHandle (hfile); return FALSE;} Read in file contents bsuccess = ReadFile (hfile, *PPBMFH, dwFileSize, &dwbytesread, NULL); CloseHandle (hfile);//Verify that the read-in is correct and that the file is a BMP file if (!bsuccess | | (dwBytesRead! = dwfilesize) | | (*PPBMFH)->bftype! = * (WORD *) "BM") {free (*PPBMFH); return FALSE;} Calculates the remaining return parameter *ppbmi = (pbitmapinfo) (*ppbmfh+1); *ppbits = (pbyte) (*PPBMFH) + (*PPBMFH)->bfoffbits;*pbmpwidth = (*ppbmi )->bmiheader.biwidth;*pbmpheight = (*PPBMI)->bmiheader.biheight;return TRUE;} BOOL Dibtotalsave (ptstr szbmpfile, PbitmapfilehEader pbmfh) {boolbsuccess;dworddwbyteswrite; handlehfile;//Open the file to be written hfile = CreateFile (szbmpfile, generic_write, 0, NULL, create_always, file_attribute_normal, NULL if (Invalid_handle_value = = hfile) {return FALSE;} Write File bsuccess = WriteFile (hfile, PBMFH, Pbmfh->bfsize, &dwbyteswrite, NULL); CloseHandle (hfile); if (!bsuccess | | Dwbyteswrite! = pbmfh->bfsize) {DeleteFile (hfile); return FALSE;} return TRUE;}  void Showdib (hdc hdc, Pbitmapinfo Pbmi, Pbyte pbits, Long nbmpwidth, long nbmpheight, long cxclient, long cyclient, BOOL Bfull) {if (FALSE = = bfull)//center Display {SetDIBitsToDevice (hdc, (cxclient-nbmpwidth)/2, (cyclient-nbmpheight)/2,nbmpwidth, nbmpheight,0, 0,0, Nbmpheight, Pbits, pbmi,dib_rgb_colors);} else//tensile Display {Setstretchbltmode (hdc, Coloroncolor); StretchDIBits (hdc, 0, 0, cxclient, cyclient, 0, 0, nbmpwidth, Nbmpheight, Pbits, Pbmi, Dib_rgb_colors, srccopy) ;}}


what needs to be added here is that in order to ensure the logic is clear, this article does not consider the bitmap OS/2 compatibility format and top-down bitmap and other special cases, error handling may not be particularly perfect (without considering the case of oversized bitmaps), the demo code in most cases is applicable, just to play a role. A detailed description of the DIB when pushing Petzold's "device-independent bitmap" chapter of Windows Programming, this chapter describes it in great detail, but the individual places are somewhat obscure, combined with this article to deepen understanding.

Complete source 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.