Hello, im a noob with C++ at the moment and Im trying to create a BMP image from pixel data stored in a array. Everything seems to work except that when i look at the file generated in a hex editor i get an extra '00 00' in between the BM identifier and the bytes reserved for the file size. It is as though the member short BM within the bmp_header struct is taking 4 bytes where i think i it is suppose to take 2 bytes. Could someone please explain why this is happening. Thanks in advance. (there are probably much more efficient ways of doing this but this is the way i want 2 do it at the moment)
Template is from http://en.wikipedia.org/wiki/BMP_file_format (the 2 by 2 pixel example)
#include <fstream>
#include <iostream>
using namespace std;
#define w_in_pix 2
#define h_in_pix 2
short padding = 0x0000;
struct bmp_header
{
short BM;
long size_of_file;
long reserve;
long offset_of_pixle_data;
long size_of_header;
long width;
long hight;
short num_of_colour_plane;
short num_of_bit_per_pix;
long compression;
long size_of_pix_data;
long h_resolution;
long v_resolution;
long num_of_colour_in_palette;
long important_colours;
}
HEADER;
// pix array ///////////////////////////////////////////////////////////////////////////
// L to R //
char BGR [12] = {
0xFF,0x00,0x00, 0x00,0xFF,0x00,
0x00,0x00,0xFF, 0xFF,0xFF,0xFF};
////////////////////////////////////////////////////////////////////////////////////////
int main()
{
HEADER.BM = 0x4d42;
HEADER.size_of_file = sizeof(HEADER) + sizeof(BGR) + sizeof(padding) * h_in_pix;
HEADER.reserve = 0000;
HEADER.offset_of_pixle_data = 54;
HEADER.size_of_header = 40;
HEADER.width = w_in_pix;
HEADER.hight = h_in_pix;
HEADER.num_of_colour_plane = 1;
HEADER.num_of_bit_per_pix = 24;
HEADER.compression = 0;
HEADER.size_of_pix_data = sizeof(BGR) + sizeof(padding) * h_in_pix;
HEADER.h_resolution = 2835;
HEADER.v_resolution = 2835;
HEADER.num_of_colour_in_palette = 0;
HEADER.important_colours = 0;
// write BMP Header ////////////////////////////////////////////////////////////////
ofstream file;
file.open ("test.bmp", ios::out | ios::trunc | ios::binary);
file.write ((char*)(&HEADER), sizeof(HEADER));
////////////////////////////////////////////////////////////////////////////////////
// write BMP data //////////////////////////////////////////////////////////////////
//file.write ((char*)(&BGR[0]),1);
int position = w_in_pix * h_in_pix * 3;
while (position != 0)
{
for (int n = 0; n != w_in_pix * 3; n++)
{
file.write ((char*)(&BGR[position - w_in_pix * 3 + n]), 1);
}
file.write ((char*)(&padding), 2);
position = position - w_in_pix * 3;
}
}