I am trying to make my own classes to manipulate BMP images..
I started off with this simple code:
Everything has been done as given in this wikepedia link.
I am having two problems:
When I try to open the image it's giving error
Premature end of file detected
The actual file I created exceeds the file size thats supposed to be by two bytes.
Here's the code:
#include <fstream>
#include <iostream>
using namespace std;
struct BMP_Header
{
public :
char id[2];
unsigned int total_image_size;
short int app_data1,app_data2;
unsigned int offset;
};
struct DIB_Header
{
public :
unsigned int dib_header_size;
int image_width;
int image_height;
short int no_colour_planes;
short int colour_depth;
unsigned int compression_method;
unsigned int raw_image_size;
unsigned int horizontal_resolution;
unsigned int vertical_resolution;
unsigned int num_colours_palette;
unsigned int imp_colours_used;
};
int main ()
{
int index=0;
BMP_Header bmp_header;
DIB_Header dib_header;
char pixel_array[16];
bmp_header.total_image_size=70;
bmp_header.offset=54;
bmp_header.id[0]='B';
bmp_header.id[1]='M';
bmp_header.app_data1=0;
bmp_header.app_data2=0;
dib_header.dib_header_size=40;
dib_header.image_width=2;
dib_header.image_height=2;
dib_header.colour_depth=24;
dib_header.raw_image_size=16;
dib_header.horizontal_resolution=2835;
dib_header.vertical_resolution=2835;
dib_header.no_colour_planes=1;
dib_header.compression_method=0;
dib_header.num_colours_palette=0;
dib_header.imp_colours_used=0;
pixel_array[index++]=0;
pixel_array[index++]=0;
pixel_array[index++]=255;
pixel_array[index++]=255;
pixel_array[index++]=255;
pixel_array[index++]=255;
pixel_array[index++]=0;
pixel_array[index++]=0;
pixel_array[index++]=255;
pixel_array[index++]=0;
pixel_array[index++]=0;
pixel_array[index++]=0;
pixel_array[index++]=255;
pixel_array[index++]=0;
pixel_array[index++]=0;
pixel_array[index++]=0;
// Image Made
fstream file;
file.open ("abc.bmp",ios::out | ios::binary);
file.write ((char*)&bmp_header,sizeof (bmp_header));
file.write ((char*)&dib_header,sizeof (dib_header));
file.write ((char*)pixel_array,sizeof (pixel_array));
file.close ();
return 0;
}