BITMAP COLOR FORMATS (intermediate level)

© 2002 by Alyce Watson

Home

Use of Color in Graphics

Bitmap Graphics Tutorial

Bitmap Color Formats

Bmp Dimensions

SCAN vs WAIT

Text Input Boxes

An Easy Calendar Control

Shareware Marketing

Date/Time Picker

Text Line-Wrap

Combining Commands

Newsletter help

Index


All bitmaps are not created equal. One bitmap can have a very large file size for an image with certain dimensions, while another bitmap with the same dimensions can have a much smaller file size. Why is this? The answer lies in the color format of the bitmap.

You might have heard the term "24-bit bitmap" or "256-color bitmap" or "1-bit bitmap" or others. This description tells us how much storage space is needed to document each single pixel in the bitmap.

24-BIT BITMAPS

Let's first consider 24-bit bitmaps. They require 24-bits of data to document each single pixel in the bitmap. You can see how this would lead to large file sizes, particularly for bitmaps with large dimensions. What is stored in the 24-bits of data? The 24-bits are divided into three 8-bit pieces for each pixel. The first 8-bits holds the blue color value, the next 8 bits holds the green color value, and the final 8 bits holds the red color value. For more information on red-green-blue color specifications, see the color graphics tutorial above.

8-bits of data will hold the numbers 0 through 255, for a total of 256 possible values. Each color component then can have a value of 0-255, with 0 being none of that color, and 255 being total saturation of that color. Since there are three colors to consider for each pixel, each pixel then uses 24-bits of data to document its color. A 24-bit bitmap on disk consists of a header, which contains information about the size and format of the bitmap, followed by the pixel data - 24-bits of data for each pixel.

Structure of a 24-bit bitmap file:

bitmap header

pixel data requiring 24-bits for each pixel

8-BIT OR 256-COLOR BITMAPS

As mentioned above, 8-bits can hold 256 possible values, 0-255. An 8-bit bitmap requires only 8 bits of data storage space to document each pixel. This shorthand is accomplished by including a palette in the file, which is placed after the header information, and before the pixel data. A palette is also often called a Color Table.

The palette for a 256-color bitmap can hold up to 256 colors. There is a standard Windows palette of 256-colors, but bitmaps can also have custom palettes. It goes something like this: In the palette, color number 1 has a red-green-blue value. Each pixel in the bitmap that is to be displayed in color number 1 will have data equal to "1" in binary numbers. Color number 113 has a different red-green-blue value, so every pixel in the bitmap that is to be displayed in color number 113 has data equal to "113" in binary numbers. Windows then must look up the red-gree-blue value of color 113 to display a pixel in that color.

For a bitmap with large dimensions, this palette indexing method of documenting colors creates a huge savings in file size. In general, 256-color bitmaps have much smaller file sizes than a 24-bit bitmap with the same dimensions.

Structure of a 256-color (8-bit) bitmap:

bitmap header

indexed palette information

pixel data requiring 8-bits for each pixel

4-BIT OR 16-COLOR BITMAPS

The 16 named colors in Liberty BASIC represent the typical colors of a 4-bit bitmap. A 4-bit bitmap is constructed in a similar manner to an 8-bit bitmap. The difference is that instead of 256 possible palette colors, there are only 16 possible colors. In binary, the number 16 requires 4 bits of space.

The 16-color bitmap consists of a bitmap header and a palette of 16 colors. It is followed by the pixel data. Each pixel requires only 4-bits of data to indicate one of the 16 colors in the palette.

Structure of a 16-color (4-bit) bitmap:

bitmap header

indexed palette information

pixel data requiring 4-bits for each pixel

1-BIT OR 2-COLOR BITMAPS

In binary numbers, a bit is either set or not set. If it is set, there will be a "1" in that spot. If it is not set, there will be a zero there. A plain black and white bitmap requires only a single bit to document each pixel. If the bit is set (equal to 1), the pixel is white, if the bit is not set, (equal to 0), the pixel is black. This type of bitmap has a very small file size, and doesn't require a palette.

Structure of a 2-color (1-bit) bitmap:

bitmap header

pixel data requiring 1-bit for each pixel

The color formats mentioned are the formats of the bitmap as it is saved on disk. Please refer to Newsletter #62 for an explanation of reading a bitmap file header to determine the color format of a bitmap on disk. A small example is included here. The BmpSave command will save the bitmap to disk with its original color format. This means that a bitmap that has been loaded from disk with LOADBMP will be saved in the same color format as the original bitmap on disk. If a bitmap that is loaded with GETBMP is saved, it will be saved to disk with the color format that matches the current screen color resolution, because it has been loaded from the screen.

Here is a small demo that determines the color bits of a bitmap

on disk.

FileDialog "Open","*.bmp",bmp$
If bmp$="" Then End

'get bits per pixel of bitmap file on disk
open bmp$ for input as #pic
pic$=input$(#pic,29)
close #pic

BitsPixel=asc(right$(pic$,1))
print "Bitmap Color Format Bits: ";BitsPixel


Home

Use of Color in Graphics

Bitmap Graphics Tutorial

Bitmap Color Formats

Bmp Dimensions

SCAN vs WAIT

Text Input Boxes

An Easy Calendar Control

Shareware Marketing

Date/Time Picker

Text Line-Wrap

Combining Commands

Newsletter help

Index