BMP Image Reader in C

Overview

This project is a BMP image reader implemented in C. It supports reading BMP images with bit depths of 8, 24, and 32 (the 32-bit depth has not been fully verified). The tool parses BMP file headers, bitmap information headers, pixel data, and color index tables (for 8-bit images), and stores the image as a structured object in memory.

The source code is available at GitHub.

Architecture

Data Structures

BMP File Header

The BMP file header contains metadata about the file structure:

  • bfType: File type (must be 0x4D42 for BMP).
  • bfSize: Total file size in bytes.
  • bfReserves: Reserved field (typically zero).
  • bfOffBits: Offset to the pixel data.
typedef struct {
   int bfhSize;
   short bfType;
   int bfSize;
   int bfReserves;
   int bfOffBits;
} BMP_FILE_HEADER;

Bitmap Information Header

This structure contains details about the image dimensions and format:

  • biSize: Size of this header (usually 40 bytes).
  • biWidth, biHeight: Image dimensions.
  • biPlanes: Number of color planes (always 1).
  • biBitCount: Bit depth (8, 24, or 32).
  • biCompression: Compression method (0 for none).
  • biSizeImages: Size of the pixel data in bytes.
  • biXPelsPerMeter, biYPelsPerMeter: Resolution in pixels per meter.
  • biClrUsed, biClrImportant: Number of colors used and important colors.
typedef struct {
   int biSize;
   int biWidth;
   int biHeight;
   short biPlanes;
   short biBitCount;
   int biCompression;
   int biSizeImages;
   int biXPelsPerMeter;
   int biYPelsPerMeter;
   int biClrUsed;
   int biClrImportant;
} BITMAP_INFORMATION;

Pixel Color Structure

This structure represents a single pixel's color in BGR format:

  • Blue, Green, Red: Color components.
  • Alpha: Reserved or transparency value.
  • Average: Optional average of RGB values for grayscale.
typedef struct {
   unsigned char Blue;
   unsigned char Green;
   unsigned char Red;
   unsigned char Alpha;
   unsigned char Average;
} BMP_COLORS;

Image Strutcure

This structure encapsulates the entire image:

typedef struct {
   char* fileAddress;
   BMP_FILE_HEADER bfh;
   BITMAP_INFORMATION bi;
   BMP_COLORS** mat;
} BMP_IMAGE;

Core Functions

Reading BMP Files

The main function BMP_IMAGE BMP_Read(char* Address) reads a BMP file and returns an image structure. It performs the following steps:

  1. Reads and validates the file header.
  2. Reads the bitmap information header.
  3. Reads the color palette (if present).
  4. Allocates memory for the pixel matrix.
  5. Reads pixel data based on bit depth (8, 24, or 32).
  6. Handles padding bytes at the end of each row.
  7. Calculates grayscale average for each pixel.

Displaying Grayscale Output

The function void printBMPGray(BMP_IMAGE image) prints the image metadata and grayscale values of each pixel to the console. It outputs the average grayscale, as well as individual red, green, and blue components in hex format.

Memory Cleanup

The function void freeBMPMemory(BMP_IMAGE image) frees the dynamically allocated memory used for the pixel matrix to prevent memory leaks.

void freeBMPMemory(BMP_IMAGE image) {
   for (int i = 0; i < image.bi.biHeight; i++)
       free(image.mat[i]);
   free(image.mat);
   image.mat = NULL;
}

Tags: BMP image-processing C pixel-manipulation file-format

Posted on Fri, 02 Oct 2026 16:36:52 +0000 by adavis