wrote:
> But when I write the bitmap headers as understood from the
> specifications I found when googling specifically the offset
> to the bitmap data goes in the wrong place.
>
> I used od -d to look at the header that I was producing and the header
> of a sample bitmap file. By doing this I saw what needed to be
> changed.
> typedef struct /**** BMP file info structure
> ****/
> {
> unsigned int biSize; /* Size of info header */
> int biWidth; /* Width of image */
> int biHeight; /* Height of image */
> unsigned short biPlanes; /* Number of color planes */
> unsigned short biBitCount; /* Number of bits per pixel */
> unsigned int biCompression; /* Type of compression to use */
> unsigned int biSizeImage; /* Size of image data */
> int biXPelsPerMeter; /* X pixels per meter */
> int biYPelsPerMeter; /* Y pixels per meter */
> unsigned int biClrUsed; /* Number of colors used */
> unsigned int biClrImportant; /* Number of important colors */
>
> } BITMAPINFOHEADER;
>
> <snip>
> bitfile = fopen("gasket.bmp","wb");
> rc = fwrite(&header,sizeof(BITMAPFILEHEADER)-2,1,bitfile);
> rc = fwrite(&info,sizeof(BITMAPINFOHEADER),1,bitfile);
Don't do that. You're expecting the layout of your struct and the
layout of bytes in the bitmap format to correspond exactly. This
will happen only by an accident of the implementation: it requires
(a) that the compiler use no padding and (b) that your machine's
endianness -- the order that bytes appear in words -- is the same
as that specified by the bitmap file, as indeed they are not.
Write the bytes out in the correct order, as specified by the map
format. (You could assemble the bytes in a buffer and write /that/
out, but even that assumes that a C char aka byte corresponds to a
bitmap's byte. One day that might not be true ...)
It might be something like [I've quietly ignored signedness issues]
write_short( bitfile, header.bfType );
write_int( bitfile, header.bfSize );
write_int( bitfile, header.bfReserved1 );
write_short( bitfile, header.bfreserved2 );
write_int( bitfile, header.bfOffBits );
...
/* assuming 4-byte ints written big-endian */
void write_int( FILE *sink, unsigned int x )
{
putc( (x >> 24) & 0xff, sink );
putc( (x >> 16) & 0xff, sink );
putc( (x >>

& 0xff, sink );
putc( x & 0xff, sink );
}
...
It's probably a FAQ but I forgot to look it up.
--
Jena user conference, September 2007:
http://hpl.hp.com/conferences/juc2007/
"The path to the web becomes deeper and wider." - October Project
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England