BMP Tutor - Ref-02 Bitmap Types: DIB, DDB, and DIB Section

Category: BMP Tutor Series (API)

Date: 02-16-2022

Return to Index


 
'From a programmer's viewpoint, there are 3 different types of bitmaps,
'varying on how they are stored, what information is contained in each,
'and how they be accessed within an application.
 
'Note: all of the 'BMP Tutor' snippet series are written to apply to
'24bit and 32bit bitmaps only.  These resolutions are the most commonly
'used, in part because of their ability to display a large number of
'colors, but also because they are easier for the programmer to work with.
 
 
'1. DIB - Device Independent Bitmap
'   A DIB is the name given to the bitmap data structure that is found in a bitmap
'   file (*.bmp).  A bitmap file contains all information needed to recreate
'   the bitmap - all attributes (such as size, bits per pixel, ...) and
'   color information on each pixel.
 
'   A programmer can read the contents of a bitmap file, edit the attributes
'   and color data, and write the information back to a file.
 
'   A bitmap file contains these three sections:
'       File Header - data structure of type BITMAPFILEHEADER
'       Info Header - data structure of type BITMAPINFOHEADER
'       Color Data  - series of Byte values, corresponding to the RGB color values of each pixel
'   See the bottom of this discussion for a listing of the data structure members.
'   Also, note that in bitmaps with less than 24bit resolution, there would also
'   be a section for palette information, just ahead of the color data.
 
'   In general, a DIB is not compatible with the Windows GDI API.  These API
'   provide advanced methods of working with and manipulating the content of a
'   bitmap - but are not compatible with the format of a DIB bitmap. There
'   are, however, a few API which allow a DIB to be converted to a DDB or
'   DIB section bitmap.  The full range of GDI API can then be applied to the
'   results of the conversion.
 
 
'2. DDB - Device Dependent Bitmap
'   A DDB is the name given to the bitmap data structure that is used to contain
'   information about an image that is compatible with the GDI API. This
'   means that the data structure contains attributes of an image but not
'   the color information about each pixel.
 
'   Instead, the color information of a DDB is contained within the device
'   on which the image is to be displayed - usually a screen or printer.  Before
'   an image can be displayed (screen) or printed (printer), a device
'   context for the device must be created.  A device context represents
'   a drawing surface for the device and is the basis on which most GDI API
'   are written (they all interact with a device context). As part of creating
'   the device context, memory within the target device is set aside to contain
'   the color information about each pixel in the image.
 
'   The way in which the color information is stored/formatted can be unique
'   for each screen (actually, the graphics card) or printer.  This means
'   that none of the GDI API allow the programmer to have direct access to
'   the color information because the data storage format is usually not known.
 
'   However, there are a few GDI API which can extract the color information
'   by interacting with the device drivers.  The result of this interaction
'   is create a new bitmap of type DIB Section.
 
 
'3. DIB Section
'   A DIB Section is a bitmap whose data structure is similar to that of a DIB,
'   but which is also compatible with GDI API.  In particular, it contains the
'   same info header (but no file header) found in a DIB.  And unlike a DDB,
'   it contains an RGBQuad array that contains the RGB color values for each pixel.
 
'   The key features of a DIB Section is that it can be selected into device
'   context, meaning that all of the GDI API can applied to modify the bitmap.
 
'   Technically, a DIB Section contains a single data structure, BITMAPINFO. But that
'   structure contains the following two structures.
'       Info Header - data structure of type BITMAPINFOHEADER (same structure as a DIB)
'       Color Data  - series of Byte values, corresponding to the RGB color values of each pixel
 
'   Another convenient of the color data is that RGBQuad values always align on
'   a Long boundary (a Windows requirement for image data), so the data contains
'   no padding bytes. This is discussed in more detail in other snippets.
 
'Other 'BMP Tutor' snippets provide code samples for working with all three types of
'bitmaps.
 
 
'Common API For Creating Bitmaps
'Here are the primary API used to create bitmaps (DDB and DIB Section). There is no API
'that directly creates a DIB (*.bmp file).
 
LoadImage                hInst, lpszName, %Image_Bitmap, 0, 0, fuLoad
 
CreateDIBSection         hDC, pbmi [BitmapINFO], %DIB_RGB_Colors, ppvBits, %NULL, 0
 
CreateBitmap             w, h, cPlanes, cBitsPerPixel, lpvBits
CreateBitmapIndirect     lpbm [Bitmap structure]
CreateCompatibleBitmap   hDC, w, h
CreateDiscardableBitmap  hDC, w, h
 
'Notes:
'LoadImage  hInst   - GetModuleHandle("")
'           0,0     - load at normal size
'           fuLoad  - LR_DefaultColor or LR_CreateDIBSection|LR_LoadFromFile
 
'CreateDIBSection
'           pbmi    - pointer to BITMAP structure
'           ppvBits - pointer to variable that receives pointer to Location of DIB Bit Values
'           %NULL,0 - tells Windows to allocate the memory for the DIB
 
'CreateBitmap  cplanes       - 1 for bitmaps
'              cBitsPerPixel - 24/32 for purposes of the BMP Tutor snippets
'              lpvBits       - pointer to array of color data OR zero if content is undefined
 
'CreateBitmapIndirect
'               lpbm      - BITMAP:  0, w, h, wScanLine, cPlanes, bits-per-pixel, ptr to byte array
'               0         - required value (type of bitmap)
'               w,h       - width/height in pixels
'               wScanLine - scanline width in bytes, including padding
'               cPlanes   - 1 for 24bit/32bit bitmaps
 
 
 
'Commonly Used Bitmap Data Structures
'These type declarations are found in the PowerBASIC win32api.inc include file.
'Note that some member renaming may have occurred, as compared to the MSDN documentation.
'These are in no particular order.
 
 
Type Bitmap
  bmType As Long           'must be zero
  bmWidth As Long          'width (pixels)
  bmHeight As Long         'height (pixels)
  bmWidthBytes As Long     'bytes per scan line (must be divisible by 2)
  bmPlanes AS WORD         'count of color plans (1 for 24/32bit bitmaps)
  bmBitsPixel AS WORD      '24/32 bots per pixel
  bmBits AS BYTE PTR       'pointer to byte array (color data)
End Type
 
Type BITMAPINFO
   bmiHeader AS BITMAPINFOHEADER     'BITMAPINFOHEADER data structure
   bmiColors As Long                 'array of RGBQuad data structures
End Type
 
 
Type BITMAPFILEHEADER
  bfType AS WORD             'must be 'BM'
  bfSize AS DWord            'size (bytes) of the *.bmp file (headers and color data)
  bfReserved1 AS WORD        'not used
  bfReserved2 AS WORD        'not used
  bfOffBits AS DWord         'offset (color data starts at .bfOffBits + 1 within the file
End Type
 
 
Type BITMAPINFOHEADER
   biSize As DWord           'size of this structure (40 bytes)
   biWidth As Long           'image width - pixel width of image (does not include padding bytes)
   biHeight As Long          'image height - pixel height of image
   biPlanes As Word          'bit planes - always 1
   biBitCount As Word        'resolution (24 or 32 bits per pixel for this tutorial)
   biCompression As DWord    'compression method (%BI_RGB for uncompressed)
   biSizeImage AS DWord      '0 for %BI_RGB compression
   biXPelsPerMeter As Long   'pixels per meter X of target device (typically not used)
   biYPelsPerMeter As Long   'pixels per meter Y of target device (typically not used)
   biClrUsed AS DWord        'no palette with 24/32bit bitmaps, so set to zero
   biClrImportant AS DWord   'no palette with 24/32bit bitmaps, so set to zero
End Type
 
 
Type RGBTRIPLE BYTE            Type RGBQUAD
   rgbtBlue As Byte               rgbBlue As Byte
   rgbtGreen As Byte              rgbGreen As Byte
   rgbtRed As Byte                rgbRed As Byte
End Type                          rgbReserved As Byte
                                  End Type
 
Type DIBSECTION
  dsBm AS Bitmap
  dsBmih AS BITMAPINFOHEADER
  dsBitfields(0 TO 2) AS DWord
  dshSection AS DWord
  dsOffset AS DWord
End Type
 
'gbs_00445
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm