Loading a BMP image into an array
The code below reads a 24-bit uncompressed BMP image file into a texture
image array (which must be declared in the calling program).
For other image formats, you can convert your image to 24-bit BMP via
Windows Paint, or write your own code.
#include <stdio.h>
#include <stdlib.h>
#pragma pack(2)
typedef struct {
unsigned char magic1;
unsigned char magic2;
unsigned int size;
unsigned short int reserved1, reserved2;
unsigned int pixelOffset;
} Header;
#pragma pack()
typedef struct {
unsigned int size;
int cols, rows;
unsigned short int planes;
unsigned short int bitsPerPixel;
unsigned int compression;
unsigned int cmpSize;
int xScale, yScale;
unsigned int numColors;
unsigned int importantColors;
} InfoHeader;
extern unsigned char *imageData;
extern int imageRows, imageCols;
void loadBMP(char *filename) {
Header header;
InfoHeader infoHeader;
FILE *fin;
fin = fopen(filename, "rb+");
if (fin == NULL) {
printf("Error opening image file: %s\n", filename);
exit(1);
}
fread(&header, sizeof(header), 1, fin);
if (!((header.magic1 == 'B') && (header.magic2 == 'M'))) {
printf("Not a valid BMP file: %s\n", filename);
exit(1);
}
fread(&infoHeader, sizeof(infoHeader), 1, fin);
if (!((infoHeader.bitsPerPixel == 24) && !infoHeader.compression)) {
printf("Cannot handle this type of BMP file\n");
exit(1);
}
imageRows = infoHeader.rows;
imageCols = infoHeader.cols;
imageData = (unsigned char*)malloc(3 * sizeof(unsigned char) * infoHeader.cols * infoHeader.rows);
int count = 0;
for (int row = 0; row < infoHeader.rows; row++) {
for (int col = 0; col < infoHeader.cols; col++) {
fread(imageData + count + 2, sizeof(unsigned char), 1, fin);
fread(imageData + count + 1, sizeof(unsigned char), 1, fin);
fread(imageData + count, sizeof(unsigned char), 1, fin);
count = count + 3;
}
}
}
REFERENCES & RESOURCES