Queste sono le strutture header del BMP:
struct _bmp_header {
uint32_t file_size;
uint16_t reserved1;
uint16_t reserved2;
uint32_t bmp_offset;
};
struct _DIB_header{
uint32_t header_size;
int32_t width;
int32_t height;
uint16_t nplanes;
uint16_t bitspp;
uint32_t compress_type;
uint32_t bmp_bytesz;
int32_t hres;
int32_t vres;
uint32_t ncolors;
uint32_t nimpcolors;
};
un esempio sull' header lo trovi qui:
questa parte che ho scritto è la parte di creazione del BMP. Nota che anziche usare il 'bitsxpixel=24' dell'esempio wiki, ho preferito usare un valore di 32 mettendo a zero il canale alpha.
int save_bmp (FILE * fp,const uchar *mem, int width, int height)
{
struct _bmp_header bmp_header;
struct _DIB_header DIB_header;
/** init */
uint32_t mem_size=width*height;
uint16_t BitsPerPixel=32;
uint32_t RowSize=(BitsPerPixel*width/32)*4;
uint32_t ArrayByteSize=RowSize*height;
/** BMP HEADER */
memset (&bmp_header,0,sizeof(bmp_header));
bmp_header.file_size=ArrayByteSize+sizeof(bmp_header)+sizeof(DIB_header)+2;
bmp_header.reserved1=0;
bmp_header.reserved2=0;
bmp_header.bmp_offset=sizeof(bmp_header)+sizeof(DIB_header)+2;
/** DIB HEADER */
DIB_header.header_size=sizeof(DIB_header);
DIB_header.width=width;
DIB_header.height=height;
DIB_header.nplanes=1;
DIB_header.bitspp=BitsPerPixel;
DIB_header.compress_type=0;
DIB_header.bmp_bytesz=ArrayByteSize;
DIB_header.hres=2835;
DIB_header.vres=2835;
DIB_header.ncolors=0;
DIB_header.nimpcolors=0;
/** write headers */
fwrite ("BM",2,1,fp); /** magic */
fwrite (&bmp_header,sizeof(bmp_header),1,fp);
fwrite (&DIB_header,sizeof(DIB_header),1,fp);
/** write image */
for (;mem_size;mem_size--)
{
fwrite (&color[*mem++],sizeof(struct _color),1,fp);
fwrite ("\0",1,1,fp); /** ALPHA channel */
}
return 0;
}