Writing BMPs

N

Nhwk

I have an unsigned char banner[96], which contains data for a 12x8
bitmap file.
I am attempting to construct and write to disk a .bmp file with this
data.


So far, I started by constructing the required .bmp header, by doing
the following..

char b[57];
b[0] = 0x42;
b[1] = 0x4D;
b[2] = 0x98;
b[3] = 0x04;

etc, and have verified I have the correct header and palette info by
using a hex editor to compare my output file to a valid .bmp file.


Now here's my problem: I'm not quite sure how to append the unsigned
chars (p->banner) to the file after i write b.

std::eek:fstream bDump;
bDump.open("bannerdump.bmp", std::ios::eek:ut | std::ios::binary);
bDump.write((char*)&b, 58);
bDump.write((char*)&p->banner, 96);
bDump.close();

However this doesn't work, as it doesn't seem to add enough data to
the file as compared to the valid file when I view them in a hext
editor. Casting the unsigned char to a (char*) seems kind of wrong.


Can anyone give me any pointers?
 
J

John Harrison

Nhwk said:
I have an unsigned char banner[96], which contains data for a 12x8
bitmap file.
I am attempting to construct and write to disk a .bmp file with this
data.


So far, I started by constructing the required .bmp header, by doing
the following..

char b[57];

57 bytes
b[0] = 0x42;
b[1] = 0x4D;
b[2] = 0x98;
b[3] = 0x04;

etc, and have verified I have the correct header and palette info by
using a hex editor to compare my output file to a valid .bmp file.


Now here's my problem: I'm not quite sure how to append the unsigned
chars (p->banner) to the file after i write b.

std::eek:fstream bDump;
bDump.open("bannerdump.bmp", std::ios::eek:ut | std::ios::binary);
bDump.write((char*)&b, 58);

but you write 58!!
bDump.write((char*)&p->banner, 96);
bDump.close();

However this doesn't work, as it doesn't seem to add enough data to
the file as compared to the valid file when I view them in a hext
editor. Casting the unsigned char to a (char*) seems kind of wrong.


Can anyone give me any pointers?

You seem confused about arrays and pointers. What you've written isn't
*necessarily* wrong, but without seeing all the definitions you have used
its hard to be sure. Nevertheless it is not necessary to take the address of
an array to convert it to a pointer. That conversion happens automatically.

bDump.write(b, 58);
bDump.write((char*)p->banner, 96);

I'm not saying this will fix your problem, but make this change, fix the 57
vs. 58 problem, and if it still doesn't work post again but this time
remember to include all the variable declarations you use (p in particular).

john
 
A

Allan Bruce

John Harrison said:
Nhwk said:
I have an unsigned char banner[96], which contains data for a 12x8
bitmap file.
I am attempting to construct and write to disk a .bmp file with this
data.


So far, I started by constructing the required .bmp header, by doing
the following..

char b[57];

57 bytes
b[0] = 0x42;
b[1] = 0x4D;
b[2] = 0x98;
b[3] = 0x04;

etc, and have verified I have the correct header and palette info by
using a hex editor to compare my output file to a valid .bmp file.


Now here's my problem: I'm not quite sure how to append the unsigned
chars (p->banner) to the file after i write b.

std::eek:fstream bDump;
bDump.open("bannerdump.bmp", std::ios::eek:ut | std::ios::binary);
bDump.write((char*)&b, 58);

but you write 58!!
bDump.write((char*)&p->banner, 96);
bDump.close();

However this doesn't work, as it doesn't seem to add enough data to
the file as compared to the valid file when I view them in a hext
editor. Casting the unsigned char to a (char*) seems kind of wrong.


Can anyone give me any pointers?

You seem confused about arrays and pointers. What you've written isn't
*necessarily* wrong, but without seeing all the definitions you have used
its hard to be sure. Nevertheless it is not necessary to take the address of
an array to convert it to a pointer. That conversion happens automatically.

bDump.write(b, 58);
bDump.write((char*)p->banner, 96);

I'm not saying this will fix your problem, but make this change, fix the 57
vs. 58 problem, and if it still doesn't work post again but this time
remember to include all the variable declarations you use (p in particular).

john

A quick note on the bitmap file format. It doesnt dump all the bytes in one
like you are doing, instead it dumps them in scanlines, and sometimes there
are padding bytes after each. I cannot remeber how many padding bytes there
are, but a quick search in Google should serve you well.
Allan
 
S

Stewart Gordon

Nhwk said:
I have an unsigned char banner[96], which contains data for a 12x8
bitmap file.
I am attempting to construct and write to disk a .bmp file with this
data.


So far, I started by constructing the required .bmp header, by doing
the following..

char b[57];
b[0] = 0x42;
b[1] = 0x4D;
b[2] = 0x98;
b[3] = 0x04;

etc, and have verified I have the correct header and palette info by
using a hex editor to compare my output file to a valid .bmp file.

Why are you using a separate assignment statement for each byte?

Easier way: use a string or array initialisation for b.

Much easier way: do away with b and actually define the struct.
http://astronomy.swin.edu.au/~pbourke/dataformats/bmp/
Now here's my problem: I'm not quite sure how to append the unsigned
chars (p->banner) to the file after i write b.

std::eek:fstream bDump;
bDump.open("bannerdump.bmp", std::ios::eek:ut | std::ios::binary);
bDump.write((char*)&b, 58);
bDump.write((char*)&p->banner, 96);
bDump.close();

I presume that the offset variable in the header correctly matches the
point in the file where you start writing out the data? And that you've
got your dimensions and colour depth correctly matched?
However this doesn't work, as it doesn't seem to add enough data to
the file as compared to the valid file when I view them in a hext
editor. Casting the unsigned char to a (char*) seems kind of wrong.
<snip>

Good job there isn't a cast of unsigned char to char* in the code you've
provided then.

Stewart.
 
P

Peter van Merkerk

A quick note on the bitmap file format. It doesnt dump all the bytes
in one
like you are doing, instead it dumps them in scanlines, and sometimes there
are padding bytes after each. I cannot remeber how many padding bytes there
are, but a quick search in Google should serve you well.

IIRC every scan-line should be a multiple of 4 bytes, but I'm sure the
people at comp.os.ms-windows.programmer.win32 know the exact details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top