A C++ program that generates a BMP image file from scratch — no external libraries required. It manually constructs BMP file headers and pixel data to render a red circle on a white background.
The program builds a valid 24-bit BMP file by:
- Constructing BMP headers — Writes the 14-byte file header and 40-byte info header using packed structs to ensure correct binary layout.
- Generating pixel data — Iterates over a 100×100 pixel grid, using the circle equation
(x - cx)² + (y - cy)² ≤ r²to determine whether each pixel falls inside the circle (red) or outside (white). - Handling row padding — Each row is padded to a 4-byte boundary, as required by the BMP specification.
- Writing to file — Outputs the raw binary data to
circle.bmp.
A 100×100 pixel BMP image with a red circle (radius 30) centered at (50, 50) on a white background.
g++ -o circle "circlcle bmp.cpp"
./circleThis creates circle.bmp in the current directory.
- Low-level binary file I/O in C++
- BMP file format specification (headers, pixel layout, row padding)
- Memory layout control with
#pragma pack - Pointer arithmetic and
reinterpret_cast