Load BMP data (for heightmaps etc)

Overview
BMP is a simple image format developed by Microsoft. The colordepth wary between 2 (1-bit), 16 (4-bit), 256 (8-bit), 16-bit and 24-bit. The image is usually not compressed, but it might be compressed with the RLE algorithm. This tutorial will cover how to read pixels from a BMP image into an 2-dimensional array.

Reading the header
The first step to read our image is to read its header to find out its size,bits-per-pixel and encoding. We will begin by making a simple struct to hold our data and a couple of enumerations for eventual errors. Lets start with struct.

typedef struct Bitmap { int offset; int width; int height; int bpp; int encoding; int size; bool loaded; unsigned char* pData; };

For now this is all we need to store, but we will add more later. I don't think any of these need any further explanation.

enum BITMAP_RESULTS { BMP_OK, BMP_NO_FILE, BMP_INVALID_HEADER, BMP_OUT_OF_MEMORY, BMP_UNSUPPORTED, BMP_LOADED, };

The first enum simply indicates everythings good soo far. BMP_NO_FILE means the file could not be opened, most likely the file doesn't exist but it might be permission error. BMP_INVALID_HEADER tells us the file isn't a valid BMP file, either it doesn't start with BM or the length is wrong. BMP_OUT_OF_MEMORY is self explanary. BMP_UNSUPPORTED is going to be used when the bpp has weired modes and/or encodings. The last one, BMP_LOADED, is used when there is already a bitmap loaded.

In order to read the header we first need to load the file into memory:


 * 1) include

using namespace std;

int loadBMP(Bitmap* img,char* filename){ ifstream::pos_type size ifstream file(filename, ios::in|ios::binary|ios::ate); if(file.is_open){ size = file.tellg; memblock = new char [size]; file.seekg (0, ios::beg); file.read (memblock, size); file.close; } }

Loading 8-bit monocrome image
TBD

Loading 24-bit truecolor image
TBD

Summary
TBD