|
writing 256 color bitmap to textfile |
idathunkit2
Member #8,120
December 2006
|
Im trying to write a program that outputs to a textfile the content of a bitmap image (in 256 colors). I want to write a single byte (to the textfile) representing each pixel. (that is, if the first pixel in the bitmap is black and black is the first color in the color palete, then i want the first character in the textfile to be char(0), if the second pixel was palete color 2, then the second character in the textfile should be char(1), etc.. ) But, the 8-bit color depths and color palete stuff doens't seem to have clear documentation. Can someone help me? ==================================================================== #include <fstream> BITMAP *my_pic; int c1,y,x; |
Steve Terry
Member #1,989
March 2002
|
And you can't use save_bitmap why? ___________________________________ |
idathunkit2
Member #8,120
December 2006
|
I don't want to 'save' the bitmap. I want to convert it into a VERY simple format. The fact that it's a bitmap is not important to the final product. I'm just using the bitmap as an easy way to create a simple map, then I want to convert it into my own data structure. I plan to add other elements to the data structure later, but for now, I just want to converte the pixels into a 1-byte code. |
gnolam
Member #2,030
March 2002
|
1. Load the bitmap without color conversion. -- |
idathunkit2
Member #8,120
December 2006
|
Thanks gnolam! (where my_pic is a pointer to a BITMAP and outFile is an ofstream object for (y=0; y<my_pic->h; y++) more getpixel help... |
LennyLen
Member #5,313
December 2004
|
Psssst... If you wrap your code in code-tags, then Allegro functions become links to their page in the manual: for (y=0; y<my_pic->h; y++) { for (x=0; x<my_pic->w; x++) { c1 = getpixel(my_pic,x,y); outFile<<char(c1)<<" "; } outFile<<endl; }
|
Johan Halmén
Member #1,550
September 2001
|
Um... You can't output char(0) and char(1) to a text file. Or you can, but it won't be a text file after that. Characters 0 - 31 are not text file characters, except line feed and carriage return, char(13) and char(10), I think. You'd better treat the file as a binary file. In your code you output endl after each row. But endl outputs lf and cr, or either one, depending on system. But you might as well have other endl:s due to the char(c1), that might as well be cr or lf. Not to mention all other creepy characters in the range 0 - 31. Either treat the file as a binary file and skip the endl thing, or output the byte as a hex value instead. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
ixilom
Member #7,167
April 2006
|
If you for some reason need to have it in "readable" format, here's an idéa: map<int,char> lookup; lookup[makecol(255,0,0)] = 'W'; // red = wall lookup[makecol(0,255,0)] = 'D'; // green = door lookup[makecol(0,0,255)] = 'F'; // blue = floor for (y=0; y<my_pic->h; y++) { for (x=0; x<my_pic->w; x++) { int color = getpixel(my_pic,x,y); outFile << lookup[color]; } outFile<<endl; }
Now, paint a bitmap with the colors defined above for walls, doors floors etc. ___________________________________________ |
Michael Jensen
Member #2,870
October 2002
|
wow! that's teh mega-cool, ixilom! Is the "<int,char>" table a c++-ism?
|
GullRaDriel
Member #3,861
September 2003
|
Yeah. "Code is like shit - it only smells if it is not yours" |
|