writing 256 color bitmap to textfile
idathunkit2

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

And you can't use save_bitmap why?

idathunkit2

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

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.

idathunkit2

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

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

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.

ixilom

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-)

Michael Jensen

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

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

GullRaDriel

Yeah.

Thread #589149. Printed from Allegro.cc