askvity

How is a bitmap image encoded?

Published in Image Encoding 3 mins read

A bitmap image is encoded by representing each pixel as a series of bits directly mapped to a corresponding location on the display.

Bitmap image encoding involves assigning a binary value to each pixel, which determines its color and brightness. The complexity and size of the encoding depend on the number of colors the image supports and whether it uses compression.

Here's a breakdown of the process:

  • Pixel Representation: The fundamental concept of bitmap encoding is representing each pixel as binary data. The number of bits per pixel (bit depth) determines the number of colors that can be displayed.

  • Bit Depth:

    • 1-bit (Monochrome): Each pixel is represented by a single bit. 0 typically represents black, and 1 represents white.
    • 8-bit (Grayscale/Indexed Color): Each pixel is represented by 8 bits (1 byte). In grayscale, this allows for 256 shades of gray (0-255). In indexed color, each byte represents an index into a color palette of 256 colors.
    • 24-bit (True Color): Each pixel is represented by 24 bits (3 bytes), typically divided into 8 bits for red, 8 bits for green, and 8 bits for blue (RGB). This allows for 16,777,216 (224) different colors.
    • 32-bit (True Color with Alpha): Similar to 24-bit color, but with an additional 8 bits for alpha (transparency).
  • Raster Data: The encoded pixels are arranged in a raster, which is a rectangular grid. The raster data is a sequential stream of pixel values, typically organized row by row (from top to bottom or bottom to top).

  • File Header: Bitmap image files (like BMP, TIFF, or uncompressed PNG) typically include a header that contains metadata such as:

    • Image width and height
    • Bit depth
    • Color palette (if applicable)
    • Compression method (if any)
    • Other image properties
  • Example (1-bit Monochrome):

    Suppose you have a small 4x4 monochrome image where '1' represents white and '0' represents black:

    1111
    1001
    1001
    1111

    This could be stored in memory as:

    1111100110011111

  • Example (8-bit Grayscale):

    Each pixel's intensity can be represented by a number from 0 (black) to 255 (white).

  • Compression: Many bitmap image formats support compression to reduce file size. Common compression methods include:

    • Run-Length Encoding (RLE): Replaces consecutive identical pixel values with a count and the value.
    • Lossless Compression (e.g., LZW, Deflate): Compresses the image data without losing any information. Used in TIFF and PNG formats.

In summary, bitmap image encoding fundamentally involves translating each pixel into binary data based on a chosen bit depth and storing this data, along with image metadata, in a specific file format.

Related Articles