Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » writing 256 color bitmap to textfile

This thread is locked; no one can reply to it. rss feed Print
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?

====================================================================
I have a working version of this program that outputs to a textfile while running in 16-bit color depth. I'm guessing the missing step is to find where the coorisponding value of each pixel is located in the palete.
And here it is...

#include <fstream>
#include "allegro.h"
using namespace std;
int main(int argc, char *argv[])
{
ofstream outFile("map.txt");
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, 420,420,0,0);

BITMAP *my_pic;
my_pic = load_bitmap("picture1.bmp", NULL);

int c1,y,x;
for (y=0; y<my_pic->h; y++) {
unsigned short *bmpline = (unsigned short *)(my_pic->line[y]);
for (x=0; x<my_pic->w; x++) {
c1 = bmpline[x];
outFile<<c1<<" ";
}
outFile<<endl;
}
destroy_bitmap(my_pic);
return 0;
}
END_OF_MAIN()

Steve Terry
Member #1,989
March 2002
avatar

And you can't use save_bitmap why?

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

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
avatar

1. Load the bitmap without color conversion.
2. Use _getpixel or the line[] array to get at the data.
3. Save the returned values.
4. ???
5. Profit.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

idathunkit2
Member #8,120
December 2006

Thanks gnolam!
Here's what I did with your help. I'm posting it in case any other newbie like me wants to see how it works (specifically getpixel).

(where my_pic is a pointer to a BITMAP and outFile is an ofstream object
and y,x, and c1 are int).

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;
}

more getpixel help...
http://www.allegro.cc/manual/api/drawing-primitives/getpixel

LennyLen
Member #5,313
December 2004
avatar

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 the red "x" that closes a window, really isn't red, but white on red background.

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
avatar

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.
I haven't tested it at all, it might blow up your computer, or at least the compiler 8-)

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

Michael Jensen
Member #2,870
October 2002
avatar

wow! that's teh mega-cool, ixilom!

Is the "<int,char>" table a c++-ism?

GullRaDriel
Member #3,861
September 2003
avatar

Yeah.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Go to: