c++ - Programatically making an bmp image -



c++ - Programatically making an bmp image -

i trying create own classes manipulate bmp images..

i started off simple code :

everything has been done given in wikepedia link.

i having 2 problems :

when seek open image giving error

premature end of file detected

the actual file created exceeds file size thats supposed two bytes.

here's 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 (); homecoming 0; }

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; } __attribute__ ((packed));

this should eliminate padding problem reporting, provided elements appropriate size header (i didn't cross check). there no problems handling bmp's in c/c++. ymmv depending on compiler each can handle packed attribute differently. should fins in case however.

also, aware of row padding bmp files expect in info elements. per format guidelines http://en.wikipedia.org/wiki/bmp_file_format:

the bits representing bitmap pixels packed in rows. size of each row rounded multiple of 4 bytes (a 32-bit dword) padding.

c++ image bmp

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -