Previously, getpixel and setpixel were used to process pixels point-by-point. We compared Dib used in the qianxun series to know that the speed is really not very slow. The former can see the scanning line, processing a 1024*800 graph requires 1 ~ 2 s, but the latter processes images of the same size almost instantly. There are many concepts about Dib (device-independent Bitmap) and DDB (device-related Bitmap), which often make me confused. In fact, DDB is actually a dc-related bitmap, in different cases, DDB is created using createbmp (), createcompatiblebmp (), loadbmp (), and LoadImage. DiB is a piece of memory, which stores bitmap headers and tails, leaving only RGB (32-bit true color, 16-bit true color), or pixel + color palette (8-bit) information. It is also dizzy to create dib. In the qianxun series, functions such as reading BMP, PNG, and reading pixel are all self-written. However, there are many useful libraries for reading and writing images. So, my method is, use the cimage class to read images-> create dual cache-> read images to the off-screen DC-> Read Memory BMP to Dib-> getdibits-> process pixels-> setdibits to the screen DC. // 1. Create dual cache and DiB // HDC, memdc, and hbitmap membmp are member variables. HDC =: getdc (m_hwnd ); Memdc = createcompatibledc (HDC ); Membmp = createcompatiblebitmap (HDC, getsystemmetrics (sm_cxscreen), getsystemmetrics (sm_cyscreen )); SelectObject (memdc, membmp ); // Cimage * IMG is the member variable # include <atlimage. h> IMG = new cimage (); IMG-> load (_ T ("3.bmp ")); IMG-> draw (memdc, 0, 0, getsystemmetrics (sm_cxscreen), getsystemmetrics (sm_cyscreen), 0, IMG-> getwidth (), IMG-> getheight ()); // Obtain bitmap from hbitmap Cbitmap CBMP; CBMP. Attach (membmp ); CBMP. getbitmap (& BM ); // Create DIB in two steps // 1. A new bitmap space is generated in the memory. New bytes, new char, and globalalloc are all used. // 2. Enter the bitmapinfo Structure DWORD size = BM. bmwidthbytes * BM. bmheight; // The pixel position in each row * The Image Height Pbuf = new byte [size];
Zeromemory (& binfo, sizeof (bitmapinfo )); Binfo. bmiheader. bibitcount = BM. bmbitspixel; // The number of bits per pixel. You can also directly write 24 (RGB) or 32 (rgba) Binfo. bmiheader. bicompression = 0; Binfo. bmiheader. biheight = BM. bmheight; Binfo. bmiheader. biplanes = 1; Binfo. bmiheader. bisizeimage = 0; Binfo. bmiheader. bisize = sizeof (bitmapinfoheader ); Binfo. bmiheader. biwidth = BM. bmwidth; // Here we can process it point by point. // Obtain the bitmap to the memory Dib Getdibits (memdc, membmp, 0, binfo. bmiheader. biheight, pbuf, (bitmapinfo *) & binfo, dib_rgb_colors ); // Point-by-point processing, which is used for fading out // Here is an 8-bit color component, not a pixel For (INT I = 0; I <binfo. bmiheader. bisizeimage; I ++) { Pbuf [I] = pbuf [I] * level/256;
} // After the full graph processing is complete, read the data to the DC display on the screen. Setdibits (HDC, membmp, 0, binfo. bmiheader. biheight, pbuf, (bitmapinfo *) & binfo, dib_rgb_colors ); It turns out that DiB is so simple. Setdibits FunctionThis function uses the color data found in the specified DIB bitmap to set the pixels in the bitmap. Function prototypeInt setdibits (HDC, hbitmap hbmp, uint ustartscan, uint cscanlines, const void * lpvbits, const bitmapinfo * lpbmi, uint fucoloruse ); ParametersHDC: The handle pointing to the device environment. Hbmp: The handle pointing to the bitmap. The function changes the Bitmap Using the color data in the specified DiB. Ustartscan: Specifies the start scanning line for color data that is not related to the device in the array to which the parameter lpvbits points. Cscanlines: specify the number of scanned lines for the array containing color data unrelated to the device. Lpvbits: pointer to Dib color data, which is stored in byte arrays. The bitmap value format depends on the member bibitcount in the bitmapinfo structure pointed by the lpbmi parameter. Lpbmi: pointer to the bitmapinfo data structure, which contains information about DiB. Fucoloruse: Specifies whether the bmicolors member in the bitmapinfo structure is provided. If yes, whether the bmicolors contains clear RGB values or palette indexes. The fucoloruse parameter must have the following values, which indicate: Dib_pal_colors: The color table consists of 16-bit index values. These values can be used to index the logical color palette in the device environment identified by the HDC parameter. Dib_rgb_colors: provides a color table that contains the original RGB values. Return ValueIf the function succeeds, the returned value is the number of scanned lines copied. If the function fails, the returned value is 0. Windows NT: To obtain more error information, call the getlasterror function. RemarksWhen the bitmap bit is indexed to the system palette, you can obtain the best bitmap rendering speed. The application may call the getsystempaletteentries function to retrieve the color and index of the system palette. After the color and index value are retrieved, the application can create dib. For more information, see system paletle ). The HDC parameter is used only when the fucoloruse parameter is set to the dib_pal_colors constant. Otherwise, the value in the HDC parameter is ignored. When an application calls this function, the bitmap identified by the hbmp parameter must be selected into the device environment. The starting point of the bottom-up DIB bitmap is in the lower left corner of the bitmap. The source point of the top-down DIB bitmap is in the upper left corner of the bitmap. ICM: Color Management continues. If the specified bitmapinfo structure is not bitmapv4header or bitmapv5header, the color configuration (profile) of the current device environment is used as the source color configuration. If the bitmapinfo structure is not bitmapv4header or bitmapv5header, use the RGB color. If the specified bitmapinfo structure is bitmapv4header or bitmapv5header, the color configuration (profile) related to the bitmap is used as the source color. Quick query: Windows NT: 3.1 and later; windows: 95 and later; Windows CE: not supported; header file: wingdi. h: library file: gdi32.lib. |