Bitmap to binary data

From Pixels to Binary

Converting a bitmap image into binary data is the reverse of decoding. Instead of reading a bit stream and drawing pixels, you read a pixel grid and write the corresponding bit values. A computer performs this process every time an image is saved to a file or transmitted across a network.

The Three-Step Process

  1. Identify the colour key and colour depth. Each colour is assigned a binary code, and the colour depth tells you how many bits each code uses.
  2. Read the pixel grid left to right, top to bottom. For each pixel in turn, look up its colour in the colour key and write down its binary code.
  3. Concatenate the codes into a single stream. Write the codes one after another with no separators. The result is the raw binary data for the image.

The order of reading is always the same: start at the top-left pixel, move right across the first row, then drop to the left of the next row, and continue until the bottom-right pixel. This convention matches how bitmaps are stored in memory and on disk.

Worked Examples

1-Bit Colour Depth

Image: 4 × 4 pixel checkerboard. Colour depth: 1 bit per pixel.
Colour key: white = 0, black = 1.

Read each pixel left to right, top to bottom, and write its bit:

Row 1: 1 0 1 0 Row 2: 0 1 0 1 Row 3: 1 0 1 0 Row 4: 0 1 0 1 Stream: 1010010110100101

2-Bit Colour Depth

Image: 4 × 3 pixel image. Colour depth: 2 bits per pixel.
Colour key: white = 00, light grey = 01, dark grey = 10, black = 11.

Read each pixel and write its 2-bit code:

Row 1: 00 10 10 00 Row 2: 01 11 11 01 Row 3: 00 10 10 00 Stream: 001010000111110100101000

 Key Takeaways

  • Converting a bitmap to binary is the reverse of decoding: for each pixel, look up its colour in the key and write the corresponding bit code.
  • Read the pixel grid left to right, top to bottom - the same order used when decoding.
  • Codes are written consecutively with no separators. The number of bits per pixel equals the colour depth.
  • A single misread pixel shifts the code of every pixel written after it, so systematic row-by-row reading is essential.