Project 06 · C++ · Earlier work

Image Flipper

Flips bitmap images upside down, reading the file format by hand.

A small C++ program from my second year at college, written to learn how an image file is laid out. It reads a .bmp byte by byte, keeps the headers, and writes the rows of pixels back in reverse order.

How it works ↓Code on GitHub ↗

flipImage(), one row at a time

read
written

The last row read is the first row written.

Reading the file

A bitmap starts with two headers, 54 bytes in all. The program reads both as raw bytes and looks at three things in them.

The first 54 bytes of a 400 × 295 bitmap, in hex

424d0667050000000000360000002800000090010000270100000100180000000000d0660500130b0000130b00000000000000000000

file header · 14 bytesinfo header · 40 bytes

  1. Signaturebytes 0–1

    “BM”

    fileHeader[0] != 'B' || fileHeader[1] != 'M'

    Anything else stops with “Not a bitmap file”.

  2. Widthbytes 18–21

    400

    infoHeader[4] + (infoHeader[5] << 8) + …

    Four bytes, least significant first: 0x0190.

  3. Heightbytes 22–25

    295

    infoHeader[8] + (infoHeader[9] << 8) + …

    Same again, four bytes later: 0x0127.

Rows and padding

Each pixel is three bytes, and each row is padded with zeros to a multiple of four bytes.

const int paddingAmount = (4 - (imgWidth * 3 % 4)) % 4;

  1. 1 px3 bytes + 1 padding
  2. 2 px6 bytes + 2 padding
  3. 3 px9 bytes + 3 padding
  4. 4 px12 bytes + 0 padding
  5. 5 px15 bytes + 1 padding

Reading skips the padding with f.ignore(); writing puts it back as zeros. At 400 pixels a row is 1,200 bytes, so it needs none.

The flip

With the pixels in memory, the flip is the order they are written back in.

image.cppcpp
f.write((char*)(fileHeader), sizeof(fileHeader));
f.write((char*)(infoHeader), sizeof(infoHeader));

for(int i = imgHeight-1; i >= 0; i--)
{
    for(int j = 0; j < imgWidth; j++)
    {
        unsigned char color[3];
        color[0] = (char)(imgColor[imgWidth * i + j].r * 255.0f);
        // ...
        f.write((char*)(color), sizeof(color));
    }
    f.write((char*)(padding), paddingAmount);
}

Trimmed: the green and blue lines are left out.

  • Headers copied as they are

    A flipped image is the same size, so the 54 bytes go back unchanged.

  • Rows in reverse

    A bitmap stores the bottom row of the picture first. The outer loop counts down from the last row read, so the top row goes back where the bottom one was.

  • r, g, b in name only

    Bitmaps store each pixel as blue, green, red. The code calls them r, g, b, but writes them back in the order it read them, so the colours come through.

  • Checked

    On a 400 × 295 bitmap the output matched ImageMagick’s flip, pixel for pixel.

It expects the classic 40-byte info header. Newer bitmaps with longer headers start their pixels further in, and those it reads from the wrong place.

Jump to

↑ ↓ move↵ openesc close

You found the 8.

mi8ess can read this board but cannot generate moves yet, so both sides are moved by hand.

Play e4 to start

Open mi8ess

Click anywhere to close