// BMP file header structure (14 bytes)
#pragma pack(push, 1)
typedef struct {
unsigned short type; // Magic identifier: 0x4D42 (BM)
unsigned int size; // File size in bytes
unsigned short reserved1; // Not used
unsigned short reserved2; // Not used
unsigned int offset; // Offset to image data in bytes
} BMPHeader;
// BMP info header structure (40 bytes)
typedef struct {
unsigned int size; // Header size in bytes
int width; // Image width in pixels
int height; // Image height in pixels
unsigned short planes; // Number of color planes (must be 1)
unsigned short bits; // Bits per pixel (24 for our case)
unsigned int compression; // Compression type (0 for BI_RGB)
unsigned int image_size; // Image size in bytes (can be 0 for BI_RGB)
int x_resolution; // Pixels per meter
int y_resolution; // Pixels per meter
unsigned int n_colors; // Number of colors (0 for 24-bit)
unsigned int n_important_colors; // All colors are important
} BMPInfoHeader;
#pragma pack(pop)
Image* read_bmp_to_grayscale(const char* filename) {
FILE* f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Error: Could not open file %s\n", filename);
return NULL;
}
code
BMPHeader header;
BMPInfoHeader info_header;
fread(&header, sizeof(BMPHeader), 1, f);
fread(&info_header, sizeof(BMPInfoHeader), 1, f);
if (header.type != 0x4D42) { // Check for 'BM'
fprintf(stderr, "Error: Not a valid BMP file.\n");
fclose(f);
return NULL;
}
if (info_header.bits != 24) {
fprintf(stderr, "Error: Only 24-bit BMP files are supported.\n");
fclose(f);
return NULL;
}
Image* img = (Image*)malloc(sizeof(Image));
img->width = info_header.width;
img->height = abs(info_header.height); // Height can be negative
img->data = (unsigned char*)malloc(img->width * img->height);
fseek(f, header.offset, SEEK_SET);
int row_padded = (img->width * 3 + 3) & (~3);
unsigned char* row = (unsigned char*)malloc(row_padded);
for (int i = 0; i < img->height; i++) {
fread(row, row_padded, 1, f);
for (int j = 0; j < img->width; j++) {
// Convert to grayscale using luminosity method (0.299*R + 0.587*G + 0.114*B)
float r = row[j * 3 + 2];
float g = row[j * 3 + 1];
float b = row[j * 3];
img->data[i * img->width + j] = (unsigned char)(0.299f * r + 0.587f * g + 0.114f * b);
}
}
free(row);
fclose(f);
return img;
}
int write_grayscale_to_bmp(const char* filename, const Image* image) {
FILE* f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "Error: Could not open file %s for writing.\n", filename);
return -1;
}
Your code is generally well-structured and looks fairly stable for reading a 24-bit BMP image, converting it to grayscale, and writing it back to disk. You’ve clearly considered padding, memory allocation, and BMP structure alignment.
However, let me apply a "Stability-Sniff-Test" (i.e., sniffing out possible crash sources, bugs, undefined behaviors, or maintainability issues). I'll give you:
✅ What's Good
⚠️ Possible Issues / Improvements
🧪 Edge Cases to Test
✅ What's Good
✔️ Correct use of #pragma pack(push, 1) to ensure no struct padding (important for reading binary files).
✔️ Proper file opening checks.
✔️ BMP magic number check (0x4D42).
✔️ Verifies 24-bit-only input BMPs.
✔️ Correct handling of row padding (BMP-specific).
✔️ Using luminosity formula for grayscale: ✅ perceptually accurate.
✔️ Avoids memory leaks, proper freeing.
⚠️ Potential Issues & Recommendations
1. ❗ No check on malloc() for NULL
In both read_bmp_to_grayscale and write_grayscale_to_bmp, you use:
c
img = (Image*)malloc(sizeof(Image)); // Same for row, img->data
None of these have a NULL check to handle memory allocation failures.
📌 Fix:
c
if (!img) {
fclose(f);
fprintf(stderr, "Error: Failed to allocate memory for image\n");
return NULL;
}
Repeat this for all major malloc() / calloc() calls.
2. ❗ BMP height can be negative (top-down bitmaps)
You do:
c
img->height = abs(info_header.height); // Height can be negative
While this avoids memory crash issues, you're ignoring top-down BMPs, and always treating them as bottom-up. This means vertical inversion in top-down files.