Hi Fuchsteufel,
I had a project quite a while ago where I created a bitmap image (which just so happened to be greyscale) with a C program. Due to it being at least 2 years ago, I can't quite remember everything about it but I have dragged it up to help you.
The bitmap header contains a field for compression which can be set to 0, which I think makes it a lot easier.
There 2 headers, the main header of the file and then the info header. These can be created using structs (I'm going to give you the code in C which you can easily adapt to C++ if needed) I'm also going to give you the values I used for each field.
Main header
- Code: Select all
typedef struct
{
unsigned short int type; //set to 4D42 hex (19778)
unsigned int size; //size of the BMP file including headers in bytes
unsigned short int reserved1,reserved2; //both set to 0
unsigned int offset; //offset to start of image, set to 54
}Header;
Info Header
- Code: Select all
typedef struct
{
unsigned int size; //size of infoheader, set to 40
int width, height; //dimensions of image in pixels
unsigned short int planes; //set to 1
unsigned short int bits; //number of bits per pixel (I used 24)
unsigned int compression; //set to 0 for no compression
unsigned int imagesize; //same as the size field in the main header
int xresolution, yresolution; //resolution in pixels per meter (I used 3780)
unsigned int ncolors; //you can specify your own colour scheme, but can set it to 0 for default
unsigned int importantcolors;//likewise, I set to 0
}Infoheader
Finally, you can define a pixel struct:
- Code: Select all
typedef struct
{
unsigned char B, G, R;
} Pixel;
Keep in mind this will only be valid if using 24 bit pixels.
Another thing is that you must use #pragma pack(2) with your list of includes before the Header struct declaration and change it back with #pragma pack() before the Info header struct declaration.
After having all of this set up, you can simply create a nester loop for the width and height of the image and assign values for each colour of each pixel. For greyscale, simply set each of the values the same.
Also, keep in mind that the bitmap file is written right to left from bottom to top
Each row must also be a multiple of 4 bytes, so calculate how many 'spacer' bytes you need to write at the end of each row
Apologies if any of this doesn't make sense, and it has dragged on a lot longer than I intended, but hopefully that gives you all the information you need to be able to do it yourself
Good luck!
Cberg