There are two types of bitmaps: device-dependent bitmaps (DDBs) and device-independent bitmaps (DIBs). Device-dependent bitmaps were common in early (pre-3.0) versions of Windows, in fact they were the only bitmaps available to application developers. However, as display technology improved and as the variety of display types increased among the installed base of Windows users, certain inherent problems surfaced. For example, there was no method of storing (and likewise retrieving) the resolution of the display on which a bitmap was created. This meant that a drawing application had no means of quickly determining whether a bitmap stored in a file was suitable for the video display on which the user was running the app. To solve this problem, Microsoft created DIBs.
A Device Independent Bitmap (or DIB) contains the data that appears in the following list:
The color format of the device on which the rectangular image appears
The resolution of the device on which the rectangular image appears
The color palette for the device on which the image appears
An array of bits that maps RGB triplets to pixels in the rectangular image.
A data-compression identifier which indicates which (if any) data compression scheme was used to reduce the size of the array of bits.
This information is stored in a BITMAPINFO data structure which is actually a BITMAPINFOHEADER followed by two or more RGBQUAD data structures. The BITMAPINFOHEADER describes the dimensions of the pixel rectangle, the device's color technology, and identifies any compression schemes that were used to reduce the bitmap's size. The RGBQUAD data structures identify the colors that appeared in the pixel rectangle.
The color format is specified in terms of a count of color-planes and color-bits: the count of color planes will always be 1; the count of color-bits can be 1 for monochrome bitmaps, 4 for VGA bitmaps, and 8, 16, 24, or 32 for other color devices. You can retrieve the number of color-bits that a particular display (or printer) uses by calling the GetDeviceCaps function and specifying BITSPIXEL as the second argument.
The resolution is specified in pels per meter. You can retrieve the horizontal resolution for a display (or printer) by calling the GetDeviceCaps function and specifying the HORZRES as the second argument, calling GetDeviceCaps a second time and specifying HORZSIZE as the second argument, and then dividing the first return value by the second. Likewise, you can retrieve the vertical resolution by calling the GetDeviceCaps function and specifying VERTRES as the second argument, calling GetDeviceCaps a second time and specifying VERTSIZE as the second argument, and then dividing the first return value by the second.
The color-palette is represented by an array of RGBQUAD structures. The RGBQUAD data structures specify the red-, green-, and blue-intensity components for each color in a display's color palette. You can retrieve the size of the device's color palette by calling the GetDeviceCaps function and specifying the NUMCOLORS constant as the second argument.
The array of palette indices specify the color of each pixel in the rectangular region associated with the bitmap. The size of this array (in bits) is equivalent to the width of the rectangle (in pixels) multiplied by the height of the rectangle (in pixels) multiplied by the count of color-bits for the device.
Windows allows the compression of the array that maps color indices to pixels in DIBs that use either the 8-bit per pixel or the 4-bit per pixel format. These arrays can be compressed using the run-length encoding scheme. This scheme uses two-byte values: the first byte specifies the number of consecutive pixels that use a color index and the second byte specifies the index.
You create a DIB by initializing the required data structures and calling the CreateDIBitmap function. Once you create a DIB, you can initialize the array of color-indexes by calling the SetDIBits function. (You can determine whether a device supports this function by calling GetDeviceCaps and specifying RC_DI_BITMAP as the RASTERCAPS flag.)
You can use a DIB to set pixels on the display by calling the SetDIBitsToDevice or the BitBlt function. (You can determine whether a device supports the SetDIBitsToDevice function by calling GetDeviceCaps and specifying RC_DIBTODEV as the RASTERCAPS flag.) You should use the SetDIBitsToDevice function if your application simply needs to display a pre-existing DIB. For example, a spreadsheet application can open existing charts and display them in a window using the SetDIBitsToDevice function. You should use the BitBlt function if your application repeatedly redraws a bitmap in a window. For example, a multi-media application that combines animated graphics with sound would benefit from calling the BitBlt function since it executes faster than SetDIBitsToDevice.
Device-dependent bitmaps are supported only for backwards compatibility—that is, so that applications written for pre-3.0 versions of Windows can run in Win32. If you are writing a new application or, if you are porting an application written for a previous version of Windows to the Win32 platform, you should use device-independent bitmaps.
Device-dependent bitmaps are described using a single data structure— the BITMAP structure. The members of this structure specify the width and height of a rectangular region (in pixels), the width of the array that maps entries from the device palette to pixels, and the device's color format (in terms of color planes and bits per pixel). You can retrieve the color-format of a device by calling the GetDeviceCaps function and specifying the appropriate constants.
There are two types of device-dependent bitmaps: discardable and non-discardable. A discardable device-dependent bitmap is a bitmap that Windows discards if the bitmap is not selected into a DC and if system memory is low. You create discardable bitmaps by calling the CreateDiscardable bitmap function. You create non-discardable bitmaps by calling the CreateBitmap, CreateCompatibleBitmap, or CreateBitmapIndirect functions.