Writing BMPs

Discussion in 'C++' started by Nhwk, Aug 26, 2003.

  1. Nhwk

    Nhwk Guest

    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?
    Nhwk, Aug 26, 2003
    #1
    1. Advertising

  2. "Nhwk" <> wrote in message
    news:...
    > 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
    John Harrison, Aug 26, 2003
    #2
    1. Advertising

  3. Nhwk

    Allan Bruce Guest

    "John Harrison" <> wrote in message
    news:bif1i2$8lcim$-berlin.de...
    >
    > "Nhwk" <> wrote in message
    > news:...
    > > 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
    Allan Bruce, Aug 26, 2003
    #3
  4. Nhwk wrote:
    > 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.

    --
    My e-mail is valid but not my primary mailbox. Please keep replies on
    on the 'group where everyone may benefit.
    Stewart Gordon, Aug 26, 2003
    #4
  5. > 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.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Aug 26, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    1
    Views:
    2,440
  2. Amit
    Replies:
    8
    Views:
    3,761
    Alan Pretre
    Aug 4, 2003
  3. Kelly G.

    Writing XML file

    Kelly G., Jan 7, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,822
    Kelly G.
    Jan 8, 2004
  4. HNguyen
    Replies:
    4
    Views:
    2,382
    HNguyen
    Dec 21, 2004
  5. Craig

    Opening colour BMPs with PIL

    Craig, Dec 4, 2006, in forum: Python
    Replies:
    2
    Views:
    499
    Craig
    Dec 6, 2006
Loading...

Share This Page