Bitmap To Ascii Generation

2 colour bitmap to ASCII converter by Na Than.
Needs allegro library.

#include
#include
#include

// This uses a conversion for bitmap only images.
// the conversion is based on Rob Harley's (robert@vlsi.cs.caltech.edu)
// code mapping.

char mapping[]={
' ',',','.','_','-','i','v','g','-','c','i','s','=','e','z','m',

'\'','!','/','2','!',']','/','d','/','(','/','K','Y','4','Z','W',

'`','\\','|','L','\\','\\',')','G','!','t','[','b','+','N','D','W',

'~','T','7','X','V','Y','Z','8','f','5','P','K','*','M','A','@'} ;

// ordered by six-bit numbers in 2x3 cells.

int main(int argc, char *argv[])
{
BITMAP *bp;
FILE *pf;
PALETTE dummy;
int x,y,xx,yy;
int i;
char c;
char ct;
char invert=0;

allegro_init(); set_color_depth(8);

puts("2 colour bitmap to ASCII converter by Na Than");
puts("Based on Rob Harley's mapping for 2x3 bitmap cells.");
puts("Contact me at na_th_an@hotmail.com");
puts(" ");
if (argc<3) {puts("ERROR 1 :: Arguments required.");
puts("Usage: ASC_B&W INPUT.{BMP|PCX|LBM|TGA} OUTPUT.EXT [invert]");
return 1;}

if (!strcmp(argv[3],"invert"))
invert=1;

// Convert

if((bp = load_bitmap(argv[1],dummy))==NULL)
{
puts("ERROR 2 :: unable to open input file."); return 2;
}

if((pf = fopen(argv[2],"wb"))==NULL)
{
puts("ERROR 3 :: unable to open output file.");return 3;
}

puts("Now converting. If you see a wrong picture, maybe the BITMAP is not");
puts("maped to have 0 for black, 1 for white.");
if (invert) puts("Doing it inverting colours");

for(y=0;yh;y+=3)
{
for(x=0;xw;x+=2)
{
// build binary
c=0;ct=0;
for (yy=2;yy>=0;yy--)
for (xx=1;xx>=0;xx--)
{
if (getpixel(bp,x+xx,y+yy) == 0)
c += (1<
ct++;
}
printf("Done :: %i%\r",1+ (100 * (x+y*bp->w))/(bp->h*bp->w));
// output character
fputc(mapping[c],pf);
}
fputc(13,pf);fputc(10,pf);
}

fclose(pf);
printf("\n");
puts("Done :)");

destroy_bitmap(bp);
allegro_exit();
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.