ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Random bmp generator

(1/2) > >>

Cyeb:
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:
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? :)

Cyeb:
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.

Cyeb:
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:
There are just some things one must learn first before attempting to do other things.
--- End quote ---

wise words.

Navigation

[0] Message Index

[#] Next page

Go to full version