|
|
|
ppm2ansi/ 40705 765 144 0 6206554126 11420 5 ustar raster users ppm2ansi/ppm2ansi.c 100605 765 145 5767 6206553371 13254 0 ustar raster priv #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
loadppm(char *name){
FILE *f;
int done,width,height,x,y;
int gray;
unsigned char r,g,b,s[256];
float rr,gg,bb;
float cls[1024][3];
int bgs[1024];
int fgs[1024];
int hls[1024];
int chs[1024];
int col_num=0;
int col,colsel;
int bg,fg,hl,ch;
float diff,maxdiff,dr,dg,db;
float ddr,ddg,ddb;
char chlist[]={" .:>ia0N"};
float chlevl[]={0,0.03,0.07,0.12,0.17,0.20,0.27,0.33};
float clr1[]={0,1,0,1,0,1,0,1};
float clg1[]={0,0,1,1,0,0,1,1};
float clb1[]={0,0,0,0,1,1,1,1};
float clr2[]={0,1,0,1,0,1,0,1};
float clg2[]={0,0,1,1,0,0,1,1};
float clb2[]={0,0,0,0,1,1,1,1};
for(fg=0;fg<8;fg++)
{
for(bg=0;bg<8;bg++)
{
for (hl=0;hl<1;hl++)
{
for (ch=0;ch<8;ch++)
{
bgs[col_num]=40+bg;
fgs[col_num]=30+fg;
hls[col_num]=hl;
chs[col_num]=chlist[ch];
if (hl==0)
{
cls[col_num][0]=
clr1[fg]*chlevl[ch]+
clr1[bg]*(1-chlevl[ch]);
cls[col_num][1]=
clg1[fg]*chlevl[ch]+
clg1[bg]*(1-chlevl[ch]);
cls[col_num][2]=
clb1[fg]*chlevl[ch]+
clb1[bg]*(1-chlevl[ch]);
}
else
{
cls[col_num][0]=
clr2[fg]*chlevl[ch]+
clr1[bg]*(1-chlevl[ch]);
cls[col_num][1]=
clg2[fg]*chlevl[ch]+
clg1[bg]*(1-chlevl[ch]);
cls[col_num][2]=
clb2[fg]*chlevl[ch]+
clb1[bg]*(1-chlevl[ch]);
}
col_num++;
}
}
}
}
f=fopen(name,"r");
fgets(s,256,f);
s[strlen(s)-1]=0;
if (!strcmp(s,"P6"))
{
done=1;
while (done)
{
fgets(s,256,f);
s[strlen(s)-1]=0;
if (s[0]!='#')
{
done=0;
sscanf(s,"%i %i",&width,&height);
fgets(s,256,f);
s[strlen(s)-1]=0;
hl=0;
fg=-1;
bg=-1;
for (y=0;y<height;y++)
{
for (x=0;x<width;x++)
{
fread(&r,1,1,f);
fread(&g,1,1,f);
fread(&b,1,1,f);
rr=((float)r)/255;
gg=((float)g)/255;
bb=((float)b)/255;
maxdiff=999999;
colsel=0;
for(col=0;col<col_num;col++)
{
dr=rr-cls[col][0];
dg=gg-cls[col][1];
db=bb-cls[col][2];
if (dr<0) dr=-dr;
if (dg<0) dg=-dg;
if (db<0) db=-db;
diff=dr+dg+db;
if (diff<maxdiff)
{
maxdiff=diff;
colsel=col;
}
}
if (hl!=hls[colsel])
{
printf("%c[%im",27,hls[colsel]);
if (hl==1)
{
fg=-1;
bg=-1;
}
}
if ((fg!=fgs[colsel])&&(bg!=bgs[colsel]))
{
printf("%c[%i;%im",27,fgs[colsel],bgs[colsel]);
}
else if (bg!=bgs[colsel])
{
printf("%c[%im",27,bgs[colsel]);
}
else if ((fg!=fgs[colsel])&&(chs[colsel]!=' '))
{
printf("%c[%im",27,fgs[colsel]);
}
printf("%c",chs[colsel]);
hl=hls[colsel];
fg=fgs[colsel];
bg=bgs[colsel];
}
printf("\n");
}
printf("%c[0m\n",27);
}
}
}
fclose(f);
}
main(int argc,char **argv)
{
loadppm(argv[1]);
}
ppm2ansi/README 100644 765 145 2462 6206554123 12223 0 ustar raster priv Now.. this code comes with NO warranties whatsoever... so don't blame me if
it somehow manages, by sheer stupidity to trash your system...
Anywa.. how do you compile it?
gcc -O2 ppm2ansi.c -o ppm2ansi
simple... and how do you run it?
ppm2ansi <filename_of_ppmfile>
this will spew out a colour ansi pic to stdout... redirect it to a file to
make it useful.. :-)
Enjoy :-)
___ ___ _________ ___ ___ __ __ ___ __ __
/ \/ \/ \ \ \/ \/ \ / \/ \ / \ / \
__ _____/ o \ o \ __/ __/___/ o \ \/ | o \ \ |______ __
/ // ___/ _ _/ \ \ | ___) _ _/ _ _ | \ \ |___ // //
/_//____/ \ \ /\ \_ \ | \ \ \| |\/| | /\ \ |____//_//
`-'`---/ / \ \/ \ / | \ / \ | | | \/ \\ |----``-'
\____/\__/ /\____/_/ |______//\__/ | | | /\____/|\ |
`---' `--' `--' `--'-' `--' `--'
The Rasterman ........ E-Mail: s2154962@cse.unw.edu.au
a.k.a. Carsten Haitzler Web: http://www.cse.unsw.edu.au/~s2154962/
16/67 St. Marks Rd. Ph: +61 2 399 8786
Randwick 2031 Fax: +61 2 399 8786
Sydney Australia
================================================================================
| Results 1 - 1 |