BMP data

xiaoxiao2021-03-06  55

BMP data is translated by the VC5 online help information related to Bitmap

Ready about bitmap bitmap type bitmap, device description table, and drawing surface bitmap Rotating bitmap telescoped bitmap bitmap storage using bitmap capture image stretching or compressed an image Save an image Appendix : Functions and structures related to bitmaps

● Ready Back to Directory Bitmap is a very common image format in the Windows system platform. If it is not the best from the performance of the saved image, JPG, GIF format exceeds it, but because it is Microsoft Standard, and it is embedded in the operating system, so it is very important. The following content is that I will help translate according to the VC5.0 online, some places I have joined my own point of view (I have used parentheses to indicate), because I have a deep understanding of the knowledge of bitmaps, so there may be some The local translation is not too accurate, even a mistake, if a master is discovered, please let me know. At the same time, I also hope that this information can help those friends who want to know about the bitmap, that is the biggest wish that I compiled this information! ● About Bitmap Returns a directory bitmap is one of the seven targets that can be selected, and the other six goals are: pen (PEN), Brush, Font (Font) ), Region, logical palette, and path (PATH). In the Microsoft Win32 Application Interface (API), the control panel application is a program that uses a bitmap. When the user selects wallpaper for the desktop, it is the bitmap, and the Windows system will be based on this bitmap. To draw a desktop. From the user's point of view, the bitmap is just a rectangular image, and in the eyes of the developer, the bitmap is a structure of the aggregate. It may contain some of the following elements: * A structure that describes image information, such as the size of the image (wide, high), the size of the number of images, and the resolution of the device of this picture, and so on. * A logical palette * a bit array that defines the relationship between pixel values ​​and logical palettes (Translator Note: If the picture has a logical palette, if there is no palette, the pixel value in the bit array Will directly represent the color components of each pixel). The following legend can be used to illustrate the real bit array content of a bitmap: In the previous example, the image is created in a VGA display, which uses a 16-color palette, and a 16-color color toning The board is at least 4-bit index values ​​to represent (2 ^ 4), so in the bit array of bitmaps, each pixel represents the index value with a 4-bit (half byte). Note: In the above bitmap, the Windows system maps the index value to the pixel to start from the bottom scan, and the scan line is ended (the scan line is all the horizontal direction of the image). For example, the first line (ROW 0) in the bit array corresponds to the bottom of the image. This is because the above bitmap is a Dib (device-UP) bitmap, which is a very ordinary bitmap. For DIBs and DDB bitmaps top to the lower type, the Windows system maps the index value to the pixel, starting from the top scaich. ● BitMap Types Returns a total of two types, namely: Device-related bitmap (DDB), and devices have no bitmap (DIB). DDB bitmaps are very common in early Windows systems (previously before Windows 3.0), in fact it is also unique. However, with the progress of the display manufacturing technology, some inherent problems of the DDB bitmap have begun to emerge.

For example, it is not able to store (or acquisition) to create the resolution of the original device of this picture, so that the application cannot quickly determine if the client's display device is suitable for displaying this picture. In order to solve this problem, Microsoft creates a DIB bitmap format. Device-independent bitmap DIB bitmap contains the following color and size information: * Color format of the original device (ie, a device created). * The resolution of the original device. * Original device's palette * A bit array, three values ​​from red, green, and blue (RGB) represent a pixel. * A array compression flag is used to indicate the compression scheme of the data (if needed). The above information is saved in the BitmapInfo structure, which is composed of BitmapInfoHeader structure and two or more RGBQUAD structures. The members of the BitMapInfoheader structure contains the size of the image, the color format of the original device, and information such as data compression scheme. The RGBQUAD structure identifies the color data used in pixels. There are also two forms of DIB bitmap, namely: bottom to upper DIB (Bottom-Up), and top to the lower DIB (Top-Down). The bottom to the origin of the upper DIB is in the lower left corner of the image, and the origin of the lower DIB is in the upper left corner of the image. If DIB's height value (marked by Biheight member in the BitmapInfoHeader structure) is a positive value, then this DIB is a bottom to the upper DIB, if the height value is a negative value, then it is a top to the lower DIB . Note: The DIB bitmap top to the lower type cannot be compressed. The color format of the bitmap is calculated by the color panel value (playes) and color bit values, the color panel value is always 1, and the color bit value can be 1, 4, 8, 16, 24, 32. one of them. If it is 1, the bitmap is a monochrome bitmap (the translator Note: It is usually a black and white bitmap, only black and white color, of course it can also be any two specified colors), if it It is 4, which means this is a VGA bitmap, if it is 8, 16, 24, or 32, indicating that the bitmap is a bitmap generated by other devices. If the application wants to get the color bit value (or the depth of the printer) (or the depth), the API function getDeviceCaps () can be called, and the second parameter is set to Bitspixel. The resolution of the display device is indicated by how many pixels per meter. The application can obtain the horizontal resolution of the display device or printer through the following three steps: 1. Call the getDeviceCaps () function, specify the second parameter for Horzres . 2. Call the getDeviceCaps () again, specify the second parameter to Horzsize. 3. Use the first return value to divide the second return value. That is: DETDEVICECAPS (HDC, Horzres) / GetDeviceCaps (HDC, Horzsize); applications can also use the same three steps to get the vertical resolution of the device, and the difference is just to replace Horzres to Vertres and replace Horzsize to Vertsize. That is. The palette is stored in an array of RGBQUAD structures that indicate the red, green, and blue components of each color. Each index in the bit array corresponds to a palette item (ie, a RGBQUAD structure), the application will convert the pixel index value into a pixel RGB value (real pixel color) based on this correspondence.

The application can also get the color palette size of the current display device by calling the getDeviceCaps () function (set the second parameter of the function to Numcolors). Win32 API supports bit data compression (only 8-bit and 4-bit bottom to the upper DIB bitmap). The compression method is to use a run length coding scheme (RLE), and RLE uses two bytes to describe a syntax, the first byte represents the number of repetitive pixels, and the second byte indicates the index value of the duplicate pixels. For more information on the compression bitmap, see the interpretation of the BitMapInfoHeader structure. The application can create a DIB bitmap from a DDB bitmap. The step is to initialize some necessary structures and then call the getDibits () function. However, some display devices may not support this function, you can determine by calling the getDeviceCaps () function (the getDeviceCaps () function specifies RC_DI_bitmap as the RasterCaps flag when calling. The application can use DIB to set the pixels on the display device (the translator's note: also display DIB), the method is to call the setDibitStodevice () function or call the StretchDibits () function. Similarly, some display devices may not support these two functions, then you can specify RC_DIBTODEV as the RasterCaps flag, then call the getDeviceCaps () function to determine if the device supports the setDibitStodevice () function. You can also specify RC_STRETCHDIB as the RasterCaps flag to call the getDeviceCaps () function to determine if the device supports the StretchDibits () function. If the application just wants to display an existing DIB bitmap, it can only be called as long as it comes to the setDibitStodevice () function. For example, an spreadsheet software that opens a chart file and simply calls the setDibitStodevice () function in the window to display the graphic in the window. However, if the application is repeated to draw bitmaps, you should use the bitblt () function because the BitBLT () function performs a lot faster than the setDibitStodevice () function. Device-Dependent Bitmaps Device Related Bitmap (DDB) is now supported by system, just for compatible old Windows 3.0 software, if programmers now develop a program related to bitmaps, should Try to use or generate bitmaps in DIB format. The DDB bitmap is described by a single structure Bitmap. The member of this structure indicates information such as the width, height, the color format of the bitmap. There are two types of DDB bitmaps, namely: discardable DDB and non-discosable DDB. The discardable DDB bitmap is a lack of system memory, and the bitmap is not selected to be selected, the DDB bitmap is cleared from memory (i.e., discard). Uncomarable DDB is the DDB that is not cleared by the system regardless of the system memory. The API function createDiscardableBitmap () function can be used to create a discarded bitmap. Function createBitmap (), createCompatibleBitmap (), and CreateBitMapIndirect () can be used to create uncomarable bitmaps. The application can create a DDB bitmap through a DIB bitmap, just initialize some necessary structures, and then call the createDibitmap () function.

If the CBM_INIT flag is specified when the function is called, then this time call is equivalent to creating CreateCompaPaTMap () to create the DDB bitmap in the current device format, and then call the setDibits () function to convert DIB format to DDB format. (Possible devices do not support setDibits () functions, you can specify rc_di_bitmap as the RasterCaps flag, then call the getDeviceCaps () function to determine it). ● Bitmap, device description table, and draw surfaces (Bitmaps, Device Contexts, and Drawing Surfaces) Returns Directory Device Description Table (DC) is a data structure that defines graphical targets, properties, mapping modes, and more. The application can call the createdc () function to create a DC, which can also call the getDc () function to get a window DC. The window will first select a "drawing surface" to DC before the application gets a DC handle. If the application is a CREATEDC () function called on a VGA display device, the DC drawing surface size it obtains is 640 × 480 pixels. If the application is a calling getDC () function, the size of the drawing surface is equal to the size of the window client area. When the application uses the DC handle returned by the CREATEDC () function or the getDc () function (ie, calling the drawing function, if the DC is called, if the LineTo () function), the requested operation will appear in the selected The DC drawing surface. Compatible Device Contexts first painted graphics in memory, and then output to real display devices to output graphics to the display device in the real display device, and the program is easier to design. To this end, Microsoft specializes in a special device descriptor, called compatible device context (Translator Note: also called a memory device description table). The Windows system treats the compatible DC as a virtual device existing in memory. Substantially compatible DC is an array in memory, the application can save the color data of the bitmap (ie, pixels) in the compatible DC. The application can call the CreateComparableDC () function to create a compatible device descriptor table that will return a DC handle, you can use this compatible DC like using the real device DC. When the Windows system creates this DC, additional one bitmap (ie, monochrome bitmap) is also created, and the bitmap is selected in a compatible DC before returning. So the application should replace this 1 bitmap before using this compatible DC. The method is to create createBitMap (), createBitMapIndirect (), or createCompatibleBitMap () function to create a bitmap you need (the size and bit depth of you specify the bitmap) and get a bitmap section, and then call SelectObject. () The function selects this bitmap handle to a compatible DC. After the bitmap handle is selected to be compatible with DC, the Windows system will replace the original 1-bit image of the original 1 bitmap (Translator Note: Please pay attention to the 1 bit of bitmap, in the drawing Select back after the end of the operation). At this point, you can use this compatible DC to draw a drawing (such as: straight line, curve, text, region, etc.), the resulting result will be saved in a bit array in a compatible DC.

But this time, these results can't be seen immediately, you should call the bitblt () function will be saved on the drawing surface of the DC bit array (please note that when the bitblt () function is called, To use the compatible DC as a source DC, the real device DC is called as the target DC). When you display a DIB or DDB with a palette (the original device has a palette), you can speed up the display speed of the image by appropriate arrangement of the system palette. First, use the NumReServed as a parameter to call the getDeviceCaps () function to get the number of colors that remain in the system. Then call the getSystemPaletteEntries () function, and fill the NumReServed / 2 Toner board items starting with the logical palette with the corresponding system color. For example, NumReServed return value is 20, then you must use the system color to fill the number of palette data at the beginning and end of the logical palette. Then fill the remaining palette items with the palette of the bitmap (this palette item must set up the PC_NOCOLLLAPSE flag). For information on palettes and colors, please refer to the COLORS section in the VC online help. ● Bitmap rotation returns to the directory Windows provides a function that copies the rectangular bitmap into a parallelogram, this function is PLGBLT (). At the time of conversion, the rectangular bitmap DC is to be used as the source DC, and the parallelogram DC is used as the target DC. For information on rotation and World Units (Translator Note: This word is not translated), please refer to the contents of Coordinate Spaces and Transformations in the VC Online Help. ● Bitmap Scaling Returns the Directory Win32 API also provides a function of a proportional zoom bitmap, which is stretchblt (). It can transmit rectangular bitmaps in the source device description table (DC) to the target device description table. But unlike the bitblt () function, stretchblt () allows the application to specify the source, the dimensions of the target bitmap. If the application specified by the application is small in size than source bitmap size, the system will compress the source bitmap to meet the target bitmap size. At the same time, you can also specify the compression mode of the function, use the setstretchbitMode () function. When the dimension of the target bitmap is greater than the source bitmap, the system will the tensile source bitmap (ie the color range of the pixels) meets the requirements. ● Bitmaps as brushes returned to the directory WIN32 API, there are several functions in the Win32 API, which uses the paintings in the DC to perform bitmap operation, such as: Patblt () function can be in a rectangle of the window. The brush is copied in the area, and the floodfill () function can copy the brush in a non-rectangular area of ​​the window (this non-rectangular area can be set to the specified color). The name of the PATBLT () function is actually an abbreviation. The full name should be "pattern block transfer" (after abbreviation is Patblt), from this name, it seems to be just a simple copy brush (or called the pattern), It is over until the specified rectangle is filled, in fact, this function can be not as simple, it will be based on the Pixel Operation code (Raster Operation, abbreviation is ROP) before copying the painting brush and the target DC The pixels in the same position form the final image. Guangshan Operation Code (ROP) has played a very important role in the combination, in essence, is a bit operator, a total of 256, the PATBLT () function can accept ROP that requires the sample and the target bitmap.

The following table lists the five ROP: ROP descriptions of this function to be used in the target bitmap

Patinvert Use the pattern of pixels or (ie operation OR) target bitmap

Dstinvert will deflect the pixel value of the target bitmap (ie the pixel value of the non-target map)

Blackness sets all the outputs to binary 0

Whiteness sets all the outputs into binary 1

Unlike the PATBLT () function, the floodfill () function is just a simple reproduction of the paint brush selected to DC to a irregular area that is set by the user (filled in full). It does not accept the ROP command. ● Bitmap Storage (BitMap Storage) Returns Directory If the user wants to save the bitmap to a file, then the application must take an extension of .bmp as a suffix, then turn the bitmap with Windows bitmap file. Save in the format to this file. Windows bitmap file format consists of several parts, the following table Description: The first structure BitmapFileHeader includes some substantially explanations of bitmap files, such as the flag "BM", bitmap file size, bit array Offset and other information. The second structure BitmapInfoHeader indicates the width, height, color format (bit depth of the number, bit depth of the bit, the number, the bit depth of the bit, the number, the bit depth) of the bitmap image. Next, the RGBQUAD structure array is placed, and it places the palette data of the bitmap, each structure describes a palette item (including red, green, and blue component value). (Translator Note: Some bitmaps do not have this RGBQUAD array, such as 16-bit, 24-bit, 32-bit bitmap. And some bitmap files are shifted in the array to use, such as 16-bit, 32-bit map The BI_BITFIELD format will use the space as the storage of the three-color mask, and the program should be taken to the difference).

The following table shows the actual content of a BMP file:

0000 42 4d 76 02 00 00 00 00 00 00 76 00 00 00 28 00 0010 00 00 20 00 00 00 20 00 00 00 01 00 04 00 00 00 0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0030 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 80 0040 00 00 00 80 80 00 80 00 00 00 80 00 80 00 80 80 0050 00 00 80 80 80 00 c0 c0 c0 00 00 00 ff 00 00 ff 0060 00 00 00 ff ff 00 ff 00 00 00 ff 00 ff 00 ff ff 0070 00 00 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0090 00 00 00 11 11 01 19 11 01 10 09 09 00a0 01 09 11 11 01 90 11 01 19 09 09 91 11 10 09 11 00B0 09 11 19 10 90 11 19 01 19 19 10 10 11 10 09 01 00c0 91 10 91 09 10 10 90 99 11 11 11 19 00 09 01 00D0 91 01 01 19 00 99 11 10 11 91 99 11 09 90 09 91 00 01 11 11 11 91 10 09 19 01 00 11 90 91 10 09 01 11 11 11 11 11 19 10 11 99 10 09 10 11 09 09 10 19 10 10 10 09 01 0110 11 19 00 01 10 19 10 11 11 01 99 01 11 90 09 19 0120 11 91 11 91 01 11 19 10 99 00 01 19 09 10 09 19 0130 10 91 11 01 11 11 00 99 90 09 01 0140 01 99 191 09 11 99 11 10 09 91 0150 11 10 11 91 99 10 90 11 01 11 11 19 11 90 09 11 0160 0,019,101,101,119,999 9,999,999,999,990,999 0,170,999,999,999,999 0,000,000,000,000,000 0,000,018,000,000,000 0,000,900,000,000,000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0019911 90 91 00 91 19 19 09 01 10 09 01 11 11 11 01B0 91 11 11 11 10 00 91 11 01 19 10 11 10 01 01 11 11 91 00 99 09 19 10 11 90 09 90 91 01 01D0 19 09 91 11 01 00 90 10 19 11 00 11 11 00 10 11 01E0 01 10 11 19 11 00 90 19 10 91 01 90 19 99 00 11 01F0 91 01 11 01 91 00 99 09 09 01 10 11 91 01 10 91 0200 99 11 10 90 91 00 91 11 00 10 11 01 10 19 19 09

0210 10 00 99 01 01 00 91 01 19 91 191 11 09 10 11 0220 00 91 00 10 90 00 99 01 11 10 09 10 10 19 09 01 0230 91 90 11 09 11 00 90 99 11 11 11 90 19 01 19 01 0240 91 01 01 19 09 00 91 10 11 91 0250 01 19 11 11 91 00 91 19 01 00 11 00 91 10 11 01 0260 11 11 11 10 01 11 00 99 99 99 99 99 99 99 99 99 99 99 99 99 90 The following table Describes the location of each structure in the bitmap above, which can be viewed: Structure corresponding bytes BitmapfileHeader 0x00 - 0x0d

BitmapInfoHeader 0x0E - 0x35

RGBQUAD ARRAY 0x36 - 0x75

Color-index array 0x76 - 0x275

(Translator Note: Color-index array is actually a bitmap of bitmaps)

● Use the bitmap to return to the directory ● Capturing an image returns to the directory You can use the bitmap to capture the image, and save the captured image to the file, or display it at the window of the window. image. In some applications, sometimes you also have a temporary save screen, such as in a plot program, if the user selects an enlargement command, then you must save the image on the current screen, then zoom in When the user returns a normal image, you can restore it to the screen with the originally saved image. To save the image on the screen, your application must first call the createCompaTibleDC () function to create a DC compatible with the current display device, then call the createCompaBitMap () function to create a bitmap for proper size, and put it The bitmap is selected in the DC above (using the selectObject () function). When the compatible device description table is created, and the bitmap of the appropriate size is also selected after the DC, you can capture the image. The Win32 API provides a bitblt () function to capture images, which acts on a bit block transmission - is to copy the bit data of the source bit map to the target bitmap. Because the function is transmitted between two bitmaps, you may think that its entry parameter is the handle of two bitmaps, but the actual situation is not like this, you can't find a bitmap in the entrance parameters of the Bitblt () function. The parameters of the handle are actually operated by the device description table (DC), and the source bitmap and the handle of the target bitmap must be selected in the source DC and the target DC. After the bitblt () call ends, the data of the source graph is copied to the target bitmap. If you want to display this image, you can call the bitblt () function again, and use the original target DC as the source DC, put one Window DC (or printer DC) is a target DC. The following sample code demonstrates how to create a compatible DC, how to create a bitmap, how to select the bitmap to capture the image (Note: This section is captured). / *

* CREATE A NORMAL DC AND A MEMORY DC for the entire screen. The

* Normal DC Provides a "snapshot" of the screen contents. The screen

* Memory DC Keeps a Copy of this "snapshot" in the associated * bitmap.

* /

HDCScreen = Createdc ("Display", NULL, NULL, NULL

HDCCompatible = CreateCompatibleDC (HDCScreen);

/ * CREATE a Compatible Bitmap for hdcscreen. * /

HBMscreen = CreateCompatibleBitmap (HDCScreen,

GetDeviceCaps (HDCScreen, Horzres),

GetDeviceCaps (HDCScreen, Vertres);

IF (hbmscreen == 0)

Errhandler ("HBMscreen", HWND);

/ * SELECT The bitmaps INTO The Compatible DC. * /

IF (! SelectObject (HDCCompatible, HBMscreen)

Errhandler ("Compatible Bitmap Selection", HWND);

/ * Hide the application window. * /

Showwindow (hwnd, sw_hide);

/ *

* Copy Color Data for the Entire Display Into A

* Bitmap That Is SELECTED INTO A Compatible DC.

* /

IF (! Bitblt (HDCCompatible,

0, 0,

Bmp.bmwidth, bmp.bmheight,

HDCScreen,

0, 0,

SRCCopy)))

Errhandler ("Screen To Compat Blt Failed", HWND

/ * Redraw the application window. * /

ShowWindow (hwnd, sw_show);

● Scaling an image Returns Some applications (such as drawing software) need to be enlarged or reduced to images, at this time, the application can achieve the purpose by calling the stretchblt () function. The STRETCHBLT () function is also copied in the target DC as the bitblt () function. However, different from the bitblt () function is that in the entry parameters of the Stretchblt () function, the application can specify the size of the source bitmap and the target bitmap. If the specified source bitmap size is greater than the specified target bitmap size, the bitmap will be compressed, in turn, the bitmap will be amplified. If the specified target bitmap size is smaller than the source bitmap size, the stretchBLT () function will delete some pixels according to the current telescopic mode to make the image smaller. The following table is all telescopic mode code and their integration: scaling mode code integration

BlackonWhite logic and (AND) operations with the pixels to be deleted with the reserved pixels

WhiteonBlack logic or (OR) is logically or (OR) to the pixels to be deleted with the reserved pixels.

ColoronColor completely deletes pixels to be deleted

Halftone is approximated by target color data

If you want to set a telescopic mode, you can call the setstretchbltmode () function. The following sample code is from one of the programs that can be intercepted twice the source image (the application uses the default telescopic mode, so the setstretchbltMode () function is not used). HDCscaled = CreateCompATIBLEDC (HDCScreen);

HBMscaled = CreateCompATIBLEBITMAP (HDCScreen, GetDeviceCaps (HDCScreen, Horzres) * 2,

GetDeviceCaps (HDCScreen, Vertres) * 2);

IF (hbmscaled == 0)

Errhandler ("HBMscaled", HWND);

/ * SELECT The bitmaps INTO The Compatible DC. * /

IF (! selectObject (hdcscaled, hbmscaled))

Errhandler ("Scaled Bitmap Selection", HWND);

Case WM_COMMAND: / * Message: Command from Application Menu * /

Switch (wparam) {

Case IDM_SCALEX1:

IF (fblt) {

Fscaled = false;

HDCWIN = GETDC (HWND);

Bitblt (HDCWIN,

0, 0,

Bmp.bmwidth, bmp.bmheight,

HDCCompatible,

0, 0,

Srccopy);

ReleaseDC (hwnd, hdcwin);

}

Break;

Case IDM_SCALEX2:

IF (fblt) {

Fscaled = true;

Stretchblt (HDCScaled,

0, 0,

bmp.bmwidth * 2, bmp.bmheight * 2,

HDCCompatible,

0, 0,

Bmp.bmwidth, bmp.bmheight,

Srccopy);

HDCWIN = GETDC (HWND);

Bitblt (HDCWIN,

0, 0,

Bmp.bmwidth, bmp.bmheight,

HDCSCALED,

0, 0,

Srccopy);

ReleaseDC (hwnd, hdcwin);

}

Break;

● Storage An Image Back to Directory Many applications need to save images to files, such as drawing programs to save the image drawn by the user, the spreadsheet program to save the user's chart, CAD software To save the graphics, and many more. If your application wants to save an image in a bitmaphe, you can save it with a bitmap format of the Windows operating system. The step is to initialize the BitMapInfo structure (composed of the BitmapInfoHeader structure and RGBQUAD structure), then fill in the appropriate data to illustrate the various parameters to be saved. Finally, the BitmapFileHeader structure and the BitmapInfo structure and the bits are written into the file. The following sample code demonstrates how to initialize and fill in the BitMapInfoHeader structure: PbitmapInfo CreateBitmapInfostruct (hwnd hwnd, hbitmap HBMP)

{

Bitmap BMP;

Pbitmapinfo PBMI;

Word cclrbits;

/ * Retrieve The Bitmap's Color Format, Width, And Height. * /

IF (! getObject (HBMP, SIZEOF (Bitmap), (LPSTR) & bmp))

Errhandler ("getObject", hwnd;

/ * Convert The Color Format to a count of bits. * /

cclrbits = (word) (bmp.bmplanes * bmp.bmbitspixel);

IF (cclrbits == 1)

CClrbits = 1; Else IF (CClrbits <= 4)

Ccl RBITS = 4;

ELSE IF (CClrbits <= 8)

Ccl RBITS = 8;

ELSE IF (CClrbits <= 16)

Ccl RBITS = 16;

Else IF (cclrbits <= 24)

CclRbits = 24;

Else

CclRbits = 32;

/ *

* Allocate Memory for The BitmapInfo Structure. (This Structure

* Contains a BitmapInfoheader structure and an array of rgbquad data

* structures.)

* /

IF (cclrbits! = 24)

PBMI = (PbitmapInfo) Localalloc (LPTR,

Sizeof (BitmapInfoHead)

SizeOf (RGBQUAD) * (2 ^ cclrbits));

/ * There is no rgbquad array for the 24-bit-per-pixel format. * /

Else

PBMI = (PbitmapInfo) Localalloc (LPTR,

SizeOf (BitmapInfoHead);

/ * Initialize the fields in the bitmapinfo structure. * /

PBMI-> bmiheader.bisize = sizeof (bitmapinfoheader);

PBMI-> bmiheader.biWidth = bmp.bmwidth;

PBMI-> bmiheader.biheight = bmp.bmheight;

PBMI-> bmiheader.biplanes = bmp.bmplanes;

PBMI-> bmiheader.bibitcount = bmp.bmbitspixel;

IF (CCLRBITS <24)

PBMI-> bmiheader.berlrused = 2 ^ cclrbits;

/ * If the bitmap is not compressed, set the bi_rgb flag. * /

PBMI-> bmiheader.Bicompression = bi_rgb;

/ *

* Compute the number of bytes in the array of color

* Indices and store the result in bisizeimage.

* /

PBMI-> bmiheader.bisizeImage = (pbmi-> bmiheader.biwidth 7) / 8

* pbmi-> bmiheader.biheight

* cclrbits;

/ *

* Set biclrimportant to 0, Indicating That All of the

* Device Colors Are Important.

* /

PBMI-> bmiheader.biClrimportant = 0;

Return PBMI;

}

The following example will demonstrate how to open a file, copy array, get a palette index, initialize the reserved structure, close files, etc. Operation: void createBMPFile (HWND HWND, LPTSTSTSTSZFILE, PbitmapInfo PBI,

HBitmap HBMP, HDC HDC)

{

Handle HF; / * file handle * / bitmapfileHeader HDR; / * Bitmap file-header * /

PbitmapInfoHeader PBIH; / * Bitmap Info-Header * /

LPBYTE LPBITS; / * MEMORY POINTER * /

DWORD DWTOTAL; / * TOTAL Count of Bytes * /

DWORD CB; / * Incremental Count of bytes * /

Byte * hp; / * byte Pointer * /

DWORD DWTMP;

PBIH = (PbitmapInfoHeader) PBI;

LPBITS = (lpbyte) Globalalloc (GMEM_FIXED, PBIH-> BisizeImage); if (! lpbits)

Errhandler ("GlobalAlloc", HWND);

/ *

* Retrieve The Color Table (Rgbquad Array) and the bits

* (Array of Palette INDices) from the dib.

* /

IF (! GetDibits (HDC, HBMP, 0, (Word) PBIH-> Biheight,

LPBITS, PBI, DIB_RGB_COLORS))

Errhandler ("getDibits", hwnd;

/ * CREATE The .bmp file. * /

HF = CREATEFILE (pszfile,

Generic_read | generic_write,

(DWORD) 0,

(Lpsecurity_attributes) NULL,

Create_ALWAYS,

FILE_ATTRIBUTE_NORMAL,

(HANDLE) NULL);

IF (HF == Invalid_Handle_Value)

Errhandler ("CreateFile", HWND;

Hdr.bfType = 0x4d42; / * 0x42 = "b" 0x4d = "m" * /

/ * Compute the size of the entire file. * /

Hdr.bfsize = (dword) (Sizeof (BitmapfileHeader)

PBIH-> Bisize PBIH-> BICLRUSED

* sizeof (rgbquad) pbih-> bisizeImage;

Hdr.bfreserved1 = 0;

Hdr.bfreserved2 = 0;

/ * Compute the offset to the array of color indices. * /

HDr.bfoffbits = (dword) Sizeof (BitmapfileHeader)

PBIH-> Bisize PBIH-> BICLRUSED

* Sizeof (RGBQUAD);

/ * Copy the bitmapfileHeader INTO the .bmp file. * /

IF (! Writefile (HF, (LPVOID) & HDR, SIZEOF (BitmapfileHead),

(LPDWORD) & dWTMP, (LPOVERLAPPED) NULL))

Errhandler ("Writefile", HWnd; / * Copy The BitmapInfoHeader and Rgbquad Array Into The File. * /

IF (! Writefile (lpvoid) PBIH, SIZEOF (BitmapInfoHeader)

PBIH-> BICLRUSED * SIZEOF (RGBQUAD),

(LPDWORD) & dWTMP, (LPOVERLAPPED) NULL))

Errhandler ("Writefile", HWND;

/ * COPY The Array of Color INDES INTO The .BMP file. * /

DWTOTAL = CB = PBIH-> BisizeImage;

HP = lpbits;

While (CB> MaxWrite) {

IF (! Writefile (HF, (LPSTR) HP, (int) maxwrite,

(LPDWORD) & dWTMP, (LPOVERLAPPED) NULL))

Errhandler ("Writefile", HWND;

CB- = MaxWrite;

HP = MaxWrite;

}

IF (! Writefile (HF, (LPSTR) HP, (int) CB,

(LPDWORD) & dWTMP, (LPOVERLAPPED) NULL))

Errhandler ("Writefile", HWND;

/ * Close the .bmp file. * /

IF (! CLOSEHANDLE (HF))

Errhandler ("CloseHandle", HWND);

/ * Free memory. * / Globalfree ((hglobal) lpbits;

}

● Bitmap Functions Back to Directory The following is a variety of functions of the operational bitmap, interested friends can view the Win32 API manual. Bitblt ()

CreateBitmap ()

CreateBitMapIndirect ()

CreateCompatibleBitmap ()

CreatedIBITMAP ()

Createdibsection ()

CreatedIndiscardableBitmap ()

EXTFLOODFILL ()

Floodfill ()

GetBitmapbits ()

GetBitmapdimensionex ()

GetDibcolortable ()

GetDibits ()

Getpixel ()

Getstretchbltmode ()

Loadbitmap ()

Maskblt ()

PATBLT ()

PLGBLT ()

Setbitmapbits ()

Setbitmapdimensionex ()

SetDibcolortable ()

SetDibits ()

SetDibitStodevice ()

Setpixel ()

SetPixelv ()

Setstretchbltmode ()

Stretchblt ()

Stretchdibits ()

Bitmap structures Bitmap related to bitmap

Bitmapcoreheader

BitmapcoreInfo

BitmapfileHeader

BitmapInfo

BitmapInfoHeader

Coloradjustment

Dibsection

RGBQUAD

RGBTrIPLE

Size

转载请注明原文地址:https://www.9cbs.com/read-114948.html

New Post(0)