Writing BMPs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nhwk

    Writing BMPs

    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::ofstream bDump;
    bDump.open("ban nerdump.bmp", std::ios::out | std::ios::binar y);
    bDump.write((ch ar*)&b, 58);
    bDump.write((ch ar*)&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?
  • John Harrison

    #2
    Re: Writing BMPs


    "Nhwk" <n_hawk_@hotmai l.com> wrote in message
    news:c16f4bb8.0 308252300.6a88d ae8@posting.goo gle.com...[color=blue]
    > 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];[/color]

    57 bytes
    [color=blue]
    > 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::ofstream bDump;
    > bDump.open("ban nerdump.bmp", std::ios::out | std::ios::binar y);
    > bDump.write((ch ar*)&b, 58);[/color]

    but you write 58!!
    [color=blue]
    > bDump.write((ch ar*)&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?[/color]

    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((ch ar*)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


    Comment

    • Allan Bruce

      #3
      Re: Writing BMPs


      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
      news:bif1i2$8lc im$1@ID-196037.news.uni-berlin.de...[color=blue]
      >
      > "Nhwk" <n_hawk_@hotmai l.com> wrote in message
      > news:c16f4bb8.0 308252300.6a88d ae8@posting.goo gle.com...[color=green]
      > > 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];[/color]
      >
      > 57 bytes
      >[color=green]
      > > 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::ofstream bDump;
      > > bDump.open("ban nerdump.bmp", std::ios::out | std::ios::binar y);
      > > bDump.write((ch ar*)&b, 58);[/color]
      >
      > but you write 58!!
      >[color=green]
      > > bDump.write((ch ar*)&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?[/color]
      >
      > 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[/color]
      of[color=blue]
      > an array to convert it to a pointer. That conversion happens[/color]
      automatically.[color=blue]
      >
      > bDump.write(b, 58);
      > bDump.write((ch ar*)p->banner, 96);
      >
      > I'm not saying this will fix your problem, but make this change, fix the[/color]
      57[color=blue]
      > 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[/color]
      particular).[color=blue]
      >
      > john
      >
      >[/color]

      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


      Comment

      • Stewart Gordon

        #4
        Re: Writing BMPs

        Nhwk wrote:[color=blue]
        > 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.[/color]

        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.

        [color=blue]
        > 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::ofstream bDump;
        > bDump.open("ban nerdump.bmp", std::ios::out | std::ios::binar y);
        > bDump.write((ch ar*)&b, 58);
        > bDump.write((ch ar*)&p->banner, 96);
        > bDump.close();[/color]

        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?
        [color=blue]
        > 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.[/color]
        <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.

        Comment

        • Peter van Merkerk

          #5
          Re: Writing BMPs

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

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

          --
          Peter van Merkerk
          peter.van.merke rk(at)dse.nl


          Comment

          Working...