topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday March 29, 2024, 10:54 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Random bmp generator  (Read 6426 times)

Cyeb

  • Participant
  • Joined in 2009
  • *
  • Posts: 50
    • View Profile
    • Donate to Member
Random bmp generator
« on: January 16, 2010, 08:53 PM »
I'm creating a random bmp generator just for the sake of learning in C++.  As I've come to understand, bmp files have a header at the top that says that it's a bmp, and how large it is, correct?  And now when I want my C++ code to write the header for a 100x100 file to a new file created by C++, and then the following 30,000 characters to be random.

Problems:
1.  I can't write null characters to a file?
2.  My code for some reason writes the header at the beginning and the end.

Everything else:
I've probably got figured out! :)

The source can be released for any other beginners, but there's really no point for anybody else.

*Edit* that is, once it is finished, if it is finished.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Random bmp generator
« Reply #1 on: January 16, 2010, 09:29 PM »
If you haven't already, grab a .BMP format reference from Wotsit - the (Windows) BMP header is a bit more than resolution :)

For the sake of not going insane, define the header as a C struct - or if you feel like going all-in, a C++ class. You should end up with either a struct and a write_to_file() function that takes a file and a header struct as arguments, or a C++ class with a write_to_file member function (we'll save proper C++ iostreams to quite a bit later :)).

Now, for file I/O, you have a lot of options. You can go for low-level file routines, C-style FILE* streams, or C++ iostreams. Low-level and FILE* are pretty much the same: they let you write out X bytes at a time. C++ iostreams are focused with writing "objects" - but you can also use them to write out bytes.

Writing out the header - the safe and portable way is to write out each member of the header structure one by one (basically making sure you're only ever writing C primitive types). This works, but can be pretty slow for large structures - we're talking huge then, though. You'd probably be tempted to simply write out the header in one go, but this won't work because of the memory padding the compiler does for speed. You can tell it to not do this packing, but that is unportable - beware if you ever plan on supporting "queer" platforms. When dealing with binary file formats, you'll want to define types (or find a stdint.h, if it doesn't come with your compiler (*cough* Visual C++ *cough*)) that have the correct bitsize.

So... which file I/O routines do you currently use, and how have you defined the header? :)
- carpe noctem

Cyeb

  • Participant
  • Joined in 2009
  • *
  • Posts: 50
    • View Profile
    • Donate to Member
Re: Random bmp generator
« Reply #2 on: January 16, 2010, 09:53 PM »
Err...UMMMMM....Idon'tknow.  I have no idea what you're saying, I'm sorry. :stars:  How about showing you my extremely inefficient code?  Feel free to laugh.

Spoiler
#include <iostream>
#include <string>
#include <fstream>



using namespace std;

int main()
{
    int a,b,c,d,f,g,h,i,j,k;
    const char e = NULL;

    a=66;
    b=77;
    c=102;
    d=117;
    f=54;
    g=40;
    h=100;
    i=1;
    j=24;
    k=48;

    ofstream bmp;
    bmp.open ("random.bmp");

    char header[54];

    header[0]=a;
    header[1]=b;
    header[2]=c;
    header[3]=d;
    header[4]=e;
    header[5]=e;
    header[6]=e;
    header[7]=e;
    header[8]=e;
    header[9]=e;
    header[10]=f;
    header[11]=e;
    header[12]=e;
    header[13]=e;
    header[14]=g;
    header[15]=e;
    header[16]=e;
    header[17]=e;
    header[18]=h;
    header[19]=e;
    header[20]=e;
    header[21]=e;
    header[22]=h;
    header[23]=e;
    header[24]=e;
    header[25]=e;
    header[26]=i;
    header[27]=e;
    header[28]=j;
    header[29]=e;
    header[30]=e;
    header[31]=e;
    header[32]=e;
    header[33]=e;
    header[34]=k;
    header[35]=d;
    header[36]=e;
    header[37]=e;
    header[38]=e;
    header[39]=e;
    header[40]=e;
    header[41]=e;
    header[42]=e;
    header[43]=e;
    header[44]=e;
    header[45]=e;
    header[46]=e;
    header[47]=e;
    header[48]=e;
    header[49]=e;
    header[50]=e;
    header[51]=e;
    header[52]=e;
    header[53]=e;

    bmp << header;
    bmp.close();


    bmp.open("random.bmp", ios::app);
    char random[30000];
    int jay=55;
    char jj=jay;

    for (int ii=0;ii<30000;ii++)
    {
        random[ii]=jj;
    }
    bmp << random;

    bmp.close();







    system("pause");
    return 0;
}



Haven't gotten the random part finished yet.  That big long list of header[0-53] is the decimal value of the order of the header when the bmp was opened up in a hex editor.
« Last Edit: January 16, 2010, 09:59 PM by Cyeb »

Cyeb

  • Participant
  • Joined in 2009
  • *
  • Posts: 50
    • View Profile
    • Donate to Member
Re: Random bmp generator
« Reply #3 on: January 21, 2010, 07:24 PM »
Meh.  Ignore.  This thread is dead.  I killed the project.  There are just some things one must learn first before attempting to do other things.  I tried to learn, really!  But oh well.  Source is up there.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Random bmp generator
« Reply #4 on: January 21, 2010, 07:25 PM »
There are just some things one must learn first before attempting to do other things.

wise words.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Random bmp generator
« Reply #5 on: January 22, 2010, 07:45 AM »
There are just some things one must learn first before attempting to do other things.
wise words.
Indeed - just don't get discouraged because you can't do everything right from the beginning :)

Outputting random bitmaps isn't too hard, but you do need a fairly solid grasp of your basic language features to avoid messy code :). Also, the BMP file format is slightly messy - if you have a graphics editor that supports .raw files, you could start by generating that output format and work up your way from there.
- carpe noctem