Bitmaps

From QB64 Phoenix Edition Wiki
Revision as of 17:01, 22 January 2023 by RhoSigma (talk | contribs)
Jump to navigation Jump to search
Bitmaps are image files with the .BMP file name extension.
  • Bitmaps can use 1, 4, 8 or 24/32 bits per pixel(BPP) color palettes.
  • Unlike QBasic, QB64 is capable of working with 24 bit per pixel color(16 million) bitmaps and can create 32 bit screens to use them with the _NEWIMAGE function.
  • Text SCREEN mode 0 cannot be screen saved in QBasic or QB64.
  • The structure of the Bitmap header can be placed in a TYPE definition as below. This information can be used to find out the bitmap's Width and Height dimensions, Bits Per Pixel used and the offset of the actual image pixel data.
  • It should be noted that QB64's _LOADIMAGE function can load bitmaps and other type of images directly into a program and be placed simply by using _PUTIMAGE. _NEWIMAGE can create 256 or 32 bit SCREEN modes to display those images.

Bitmap Header

'Bitmap.BI can be included at start of program

TYPE BMPEntry              ' Description                          Bytes    QB64 Function
   ID AS STRING * 2        ' File ID("BM" text or 19778 AS Integer) 2      CVI("BM")
   Size AS LONG            ' Total Size of the file                 4      LOF
   Res1 AS INTEGER         ' Reserved 1 always 0                    2
   Res2 AS INTEGER         ' Reserved 2 always 0                    2
   Offset AS LONG          ' Start offset of image pixel data       4      (add one for GET)
END TYPE                   '                                 Total 14

TYPE BMPHeader          'BMP header also used in Icon and Cursor files(.ICO and .CUR)
   Hsize AS LONG           ' Info header size (always 40)           4
   PWidth AS LONG          ' Image width                            4      _WIDTH(handle&)
   PDepth AS LONG          ' Image height (doubled in icons)        4      _HEIGHT(handle&)
   Planes AS INTEGER       ' Number of planes (normally 1)          2
   BPP AS INTEGER          ' Bits per pixel(palette 1, 4, 8, 24)    2      _PIXELSIZE(handle&)
   Compression AS LONG     ' Compression type(normally 0)           4
   ImageBytes AS LONG      ' (Width + padder) * Height              4
   Xres AS LONG            ' Width in PELS per metre(normally 0)    4
   Yres AS LONG            ' Depth in PELS per metre(normally 0)    4
   NumColors AS LONG       ' Number of Colors(normally 0)           4       2 ^ BPP
   SigColors AS LONG       ' Significant Colors(normally 0)         4
END TYPE                   '                 Total Header bytes =  40
'$INCLUDE: 'Bitmap.BI'  'use only when including a BI file

DIM SHARED ENT AS BMPEntry
DIM SHARED BMP AS BMPHeader
LINE INPUT "Enter a bitmap file name: ", file$ '<<<< enter a bitmap file name

OPEN file$ FOR BINARY AS #1
GET #1, 1, ENT   'get entry header(1 is first file byte in QB64 and Qbasic)
GET #1, , BMP    'get bitmap header information

PRINT "Size:"; ENT.Size; "bytes, Offset:"; ENT.Offset
PRINT BMP.PWidth; "X"; BMP.PDepth
PRINT "BPP ="; BMP.BPP
CLOSE #1
Explanation: Use two GETs to read all of the header information from the start of the bitmap file opened FOR BINARY. It reads all 54 bytes as STRING, INTEGER and LONG type DOT variable values. TYPE DOT variables do not require type suffixes!


Snippet: Use the DOT variable name values like this GET (graphics statement) after you load the bitmap image to the screen:

GET (0, 0)-(BMP.PWidth - 1, BMP.PDepth - 1), Image(48) 'index after 16 * 3 RGB palette colors(0 to 47)

The bitmap image is now stored in an Template:KW to Template:KW to a file. The RGB color information follows the file header as ASCII character values read using Template:KW. The color values could be indexed at the start of the Array with the image being offset to: index = NumberOfColors * 3. As determined by the Template:KW mode used. In SCREEN 13(256 colors) the index would be 768.


Template:WhiteStart BITMAP COMPRESSION METHODS

Value Identified by Compression method Comments

 0	BI_RGB	            none	       Most common
 1	BI_RLE8	        * RLE 8-bit/pixel      Used only with 8-bit/pixel bitmaps
 2	BI_RLE4	        * RLE 4-bit/pixel      Used only with 4-bit/pixel bitmaps
 3	BI_BITFIELDS	  Bit field            Used only with 16 and 32-bit/pixel bitmaps.
 4	BI_JPEG	          JPEG	               Bitmap contains a JPEG image
 5	BI_PNG	          PNG	               Bitmap contains a PNG image
     * RLE stands for Run Length Encoding which counts the number of consecutive pixels
       that are of the same color instead of assigning each pixel color separately.

Template:WhiteEnd

(Return to Table of Contents)

Image Data

Template:WhiteStart Windows/OS2 Bitmaps

                                        ┌─────────┐
                                        │BMP Entry│
                                        │ 14 Byte │
                                        │─────────│
                                        │ Bitmap  │
                                        │ Header  │
                                        │ 40 Byte │
                                        └────┬────┘
                              ┌─────────┬────┴────┬─────────┐
                              │     ┌───┴───┐ ┌───┴───┐     │
                              │     │ 4 BPP │ │ 8 BPP │     │
                              │     │Palette│ │Palette│     │
                              │     │64 Byte│ │1024 B │     │
                          ┌───┴───┐ └───┬───┘ └───┬───┘ ┌───┴───┐
                          │ 1 BPP │ ┌───┴───┐ ┌───┴───┐ │24 BPP │
                          │ IMAGE │ │ IMAGE │ │ IMAGE │ │ IMAGE │
                          │ DATA  │ │ DATA  │ │ DATA  │ │ DATA  │
                          │(W*H)\8│ │(W*H)\2│ │(W*H)*1│ │(W*H)*3│
                          │ bytes │ │ bytes │ │ bytes │ │ bytes │
                          └───────┘ └───────┘ └───────┘ └───────┘

Template:WhiteEnd

Bits Per Pixel (BPP)

BPP returns 1 bit(Black and white), 4 bit(16 colors), 8 bit(256 colors) or 24 bit(16 million colors) for each pixel. In QBasic 24 bit can only be in greyscale, but QB64 can display them as True Color. 24 bit is also often referred to as 32 bit, but each pixel uses three bytes of information for the Red, Green and Blue color intensity settings. Intensity settings are read as ASCII characters using ASC.


Palette Data (4 and 8 Bit Only)
  • Attribute color intensities for 4 and 8 BPP are set by the bitmap itself using the Palette data immediately following the bitmap header. The data is read as Blue, Green and Red color intensities with a one byte padder following each BGR setting. This is true for ALL Windows/OS2 bitmap color intensities including 24 bit, which reads the intensities directly from the image pixel data!
The Four Bit Palette is 64 bytes and the Eight Bit is 1024 bytes. One Bit and 24/32 Bit have no palette data!
Note: _COPYPALETTE may be required to adapt the SCREEN palette to the custom colors of the bitmap.


Why BGR instead of RGB? Because the LONG _RGBA32 value with 0 _ALPHA is written to a file as 4 MKL$ ASCII characters.
SCREEN 13 '8 bit, 256 color screen mode
Q$ = CHR$(34)
INPUT "Enter a color number 1 to 255: ", colour
PRINT
OUT &H3C7, colour
red = INP(&H3C9) * 4
green = INP(&H3C9) * 4
blue = INP(&H3C9) * 4
alpha = 0 'alpha values > 127 in _RGBA or _RGBA32 should use _UNSIGNED LONG

COLOR _RGB(red, green, blue) 'returns closest attribute in 4 or 8 bit screen modes
rgba~& = _RGBA32(red, green, blue, alpha) 'alpha is actually highest byte
PRINT "RGBA ="; red; green; blue; alpha
PRINT "_RGBA32 ="; rgba~&; " &H"; HEX$(rgba~&)

_PRINTSTRING (40, 40), "BGR0 = " + Q$ + MKL$(rgba~&) + Q$ 'rightmost always CHR$(0) spacer

END
Note: 16 colors at 4 bytes each = 64 bytes. 256 colors at 4 bytes each = 1024 bytes in the palette data with CHR$(0) spacers.
Warning! Use _UNSIGNED LONG when comparing _RGB or _RGB32 full _ALPHA values with POINT values!


Image Data
  • Image data starts immediately after the bitmap header with One Bit and 24 Bit colors. Immediately after the palette data with 4 Bit and 8 Bit colors. Image pixel data is read starting with the data from the BOTTOM row of the image. This is another idiosyncrasy of the Windows/OS2 bitmap. The pixel columns thankfully are read left to right. You may notice the image being drawn from the bottom up in QBasic. The size of the data in a 24 Bit bitmap is almost triple the size of an 8 Bit one!
NOTE: The header Offset sets the position as the byte preceding the image data!


Image Data Padding Prevents Image Skewing
  • Image data is byte padded for odd bitmap widths and a minimum pixel byte width as set below:
  • 1 BPP: minimum pixel widths of multiples of 32 (32 bits = 4 bytes) per row. Use: Padder bytes = 32 - (width MOD 32)
  • 4 BPP: minimum pixel widths of multiples of 8 (32 bits = 4 bytes) per row. Padder bytes = (8 - (width MOD 8)) \ 2
  • 8 BPP: minimum pixel widths of multiples of 4 (4 bytes) per row. Padder bytes = 4 - (width MOD 4)
  • 24 BPP: minimum pixel widths of multiples of 4 (3 bytes/pixel = 12 bytes) per row. Padder bytes = 4 - ((width * 3) MOD 4)


(Return to Table of Contents)

One Bit:

Since the pixel value is either on(white) or off(black), eight pixels can be stored in one byte of information. The total byte value determines which pixels are on or off. The MSB(highest)value is to the left and each pixel's on value decreases by an exponent of two down to a value of 1 for the LSB. However a minimum of 4 bytes of data must be used for each row of data, so a padder is used for other widths. The padder can be determined before the data is read using the following routine:

SUB OneBit          'Any Screen as Black and White
BitsOver = BMP.PWidth MOD 32  'check bitmap width for 4 byte or odd width
IF BitsOver THEN ZeroPAD$ = SPACE$((32 - BitsOver) \ 8) '16 and 48 wide have 2 byte padder
y = BMPHead.PDepth - 1: o$ = " "
GET #1, BMP.Offset, o$  ' offset is last byte of BMP header data (NO Palette)
a$ = " "       'define a one byte string to read ASCII characters
DO
x = 0
    DO
      GET #1, , a$
      CharVAL = ASC(a$)        'ASCII value cannot use _BYTE
      Bit = 128                   'start at MSB
      FOR BitCOUNT = 1 TO 8
        IF CharVAL AND Bit THEN
          PSET (x, y), _RGB(255, 255, 255) '_RGB works in 1, 4, 8 or 32 bit screen mode
        ELSE PSET (x, y), _RGB(0, 0, 0) 'set pixels on as white
        END IF
        Bit = Bit / 2            'decrease exponent of 2 bit value
        x = x + 1                'move one pixel to the right
      NEXT BitCOUNT
    LOOP WHILE x < BMP.PWidth
    GET #1, , ZeroPAD$             'skip the padder bytes if any
    y = y - 1                      'move up one row from bottom
LOOP UNTIL y = -1
END SUB  
Code by Bob Seguin
One bit pixels are also used to create AND masks that can blend with a background for icons or cursors which are another form of bitmap. In fact, icons and cursors use a partial (40 byte) bitmap header! They just don't have the first 14 bytes of information. PSET can also use the B&W color values _RGB(255, 255, 255) and _RGB(0, 0, 0) when working in 4, 8 or 32 bit screen modes.

(Return to Table of Contents)

Four Bit:

Pixels can use 16 colors in QBasic legacy SCREEN modes 7, 8, 9, 12 and 13. After the bitmap header, the color palette is read to set the color intensities as explained above. Then the individual pixel attributes are read from the image data. Each pixel uses half a byte of color attribute information. To determine the pixel's attribute, each "nibble" is read by dividing the byte's ASCII value by 16 for the first pixel's value while the second pixel's value is found using AND 15 as shown below:

SUB FourBIT  ' 4 bit(16 color) Screens 7, 8, 9, 12 or 13
IF BMP.PWidth MOD 8 THEN ZeroPAD$ = SPACE$((8 - BMP.PWidth MOD 8) \ 2)
a$ = " "
OUT &H3C8, 0                         'start at attribute 0
FOR Colr = 0 TO 15        'read palette data for intensities
    GET #1, , a$: Blu = ASC(a$) \ 4  'intensity is divided by 4 to use OUT
    GET #1, , a$: Grn = ASC(a$) \ 4
    GET #1, , a$: Red = ASC(a$) \ 4
    OUT &H3C9, Red          'NOTE: RGB settings could also be sent directly to an
    OUT &H3C9, Grn          'array when the data is to be stored by a file using
    OUT &H3C9, Blu          'BSAVE or with one PUT # to a BINARY file in QB64
    GET #1, , a$