Log in

View Full Version : Source code to read a greyscale BMP into an array


alexh110
15th March 2008, 23:00
I'm looking for some source code to input an uncompressed greyscale BMP into a C++ array.

The BMP was originally a 24bit RGB image that I greyscaled with Paintshop.

drmpeg
16th March 2008, 02:23
Something like this:

#include <stdio.h>
#include <stdlib.h>

FILE *fp;
char filename[] = {"test.bmp"};
unsigned char header[54];
unsigned int size;
unsigned char *rgb;

fp = fopen(&filename[0], "rb");
length = fread(&header[0], 1, 54, fp);
size = header[2];
size |= header[3] << 8;
size |= header[4] << 16;
size = size - 54;
rgb = malloc((unsigned char *)size);
length = fread(rgb, 1, size, fp);


.bmp format is here:

http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html

Ron

alexh110
17th March 2008, 03:36
Thanks!