Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Gifscii (ascgif) new source. (pixellated stuff :-)

0 views
Skip to first unread message

pk6...@acad.drake.edu

unread,
Feb 16, 1994, 2:41:11 PM2/16/94
to
This is Gifscii, a gif-to-ascii converter. Gifscii is based on Ascgif 1.6,
but the conversion routines have been completely rewritten. Gifscii
automatically adapts its conversion to the color palettes in the gif image,
so it is able to handle many variations in base brightness and color
combination. Also, Gifscii uses a larger number of ascii characters and,
by taking their shape and within-cell position into account, can give
somewhat better results at lower resolution than ascgif.

Gifscii can be run with command line arguments:
'gifscii filename width height'
or without, in which case it will prompt you for them.

Gifscii will work best on images with lots of shading, worst on images
having a small number of colors of equal intensity - like cartoons. It
does recognize black-and-white images and converts accordingly.

Warning! Gifscii is not magic! The output generated will frequently
be cruddy. Before promulgating converted images over the network, show
them around, see if anyone likes them, or if they are recognizable. Also,
ask yourself whether people wouldn't rather have copies of the original
gif image, so why an ascii version?

That said, have fun and don't expect a lot of help from the author(s) :-)

/* Gifscii version 1.0 (c) Feb, 1994 Paul Kline pk6...@acad.drake.edu */
/* ascgif version 1.6 (c) May, 1992 John Ferrell jw...@juts.ccc.amdahl.com */
/* 5 changes for pc to mainframe portability. (search on 'mainframes') */

/* This program converts graphics stored in gif format (87a) to ascii text */
/* Syntax is 'gifscii filename columns lines' */
/* If run without command-line arguments, program will ask for them. */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define UBYTE unsigned char /* 8Bit */
#define UWORD unsigned short /* 16Bit */
#define ULONG unsigned long /* 32Bit int for mainframes, long for pc */
#define BOOL unsigned short

#define TRUE 1
#define FALSE 0

struct AsciiMetric {
UWORD am_ul;
UWORD am_ur;
UWORD am_ml;
UWORD am_mr;
UWORD am_bl;
UWORD am_br;
char am_char;
};

struct GIFdescriptor {
UWORD gd_Width;
UWORD gd_Height;
UBYTE gd_ColInfo;
UBYTE gd_BackGround;
UBYTE gd_PixelAspect;
};

struct ImageDesc {
UWORD id_Left;
UWORD id_Top;
UWORD id_Width;
UWORD id_Height;
UBYTE id_Info;
};

struct RGB {
UBYTE rgb_Red;
UBYTE rgb_Green;
UBYTE rgb_Blue;
};


#define GIF_IMAGE 0x2C
#define GIF_EXTENSION 0x21
#define GIF_TERMINATOR 0x3B
#define GIF_COMMENT_EXT 0xFE

void *screen; /* uncomment for pc, comment out for mainframes */

int gx=79;
int gy=23;

struct GIFdescriptor gdesc;

UBYTE **BitPlane;
struct RGB GlobalColourTable[256];

UBYTE Map[256];

ULONG ImageNumber;
ULONG ImageColours;

FILE *GIFfh = NULL;
FILE *CAPT = NULL;

void ERROR(str)
char *str;
{
printf(str);
exit(1);
}

void FlipWord(word)
UWORD *word;
{
UBYTE swap1;
UBYTE swap2;
swap1 = *word & 0xFF; /*comment out for pc, needed for mainframes */
swap2 = (*word & 0xFF00) >> 8; /* " " */
*word = swap1 << 8 | swap2; /* " " */
}

static struct ImageDesc idesc;
static struct RGB LocalColourTable[256];

static UWORD Xpos, Ypos;
static BOOL interleave;

static UBYTE LeaveStep[5] = {1, 8, 8, 4, 2};
static UBYTE LeaveFirst[5] = {0, 0, 4, 2, 1};

static int ReadError;
static UBYTE CodeSize;
static int EOFCode;
static UBYTE ReadMask;
static int CompDataPointer;
static int CompDataCount;
static UBYTE CompData[256];

static UWORD Prefix[4096];
static UBYTE Suffix[4096];
static UBYTE OutCode[1025];

static UBYTE ByteBuf;

void BuildSubsTable(asciisub)

/* Copyright Feb, 1994 Paul Kline. pk6...@acad.drake.edu */
/* All rights to these tables and gif-ascii conversion routines reserved. */
/* These tables and routines may be copied and distributed except that no */
/* computer program may be developed from them for commercial or for-profit */
/* purposes without the express written permission of the copyright holder */
/* This notice is not to be understood as granting such permission. */
/* All output of the program is the property of the owner of the original */
/* artwork. */

UBYTE asciisub [5][5][5][5][5][5];
{
FILE *fp_ascsub;
UBYTE sub_char;
UWORD i1,i2,i3,i4,i5,i6,a1;
UWORD d1,d2,d3,d4,d5,d6,subtot,curtot,curix;
struct AsciiMetric asciitab[73] = {
{0,0,0,0,0,0,' '},
{0,0,0,0,2,2,'.'},
{0,0,1,1,2,2,':'},
{0,0,2,2,0,0,'-'},
{0,0,2,2,3,3,'x'},
{0,0,2,2,4,4,'u'},
{0,0,2,3,4,3,'z'},
{0,0,3,2,3,0,'r'},
{0,0,3,2,4,3,'c'},
{0,0,3,3,0,0,'='},
{0,0,3,3,1,1,'+'},
{0,0,3,3,3,3,'n'},
{0,0,3,3,3,4,'s'},
{0,0,3,3,4,4,'o'},
{0,0,4,4,3,3,'m'},
{0,0,4,4,4,3,'e'},
{0,1,2,1,1,2,'<'},
{0,2,0,1,0,0,'\''},
{0,2,1,2,3,0,'/'},
{0,2,3,3,4,4,'d'},
{1,0,2,2,2,0,'>'},
{1,1,3,2,4,4,'i'},
{1,2,0,1,0,0,'`'},
{1,2,1,1,0,0,'~'},
{1,2,3,3,0,3,'4'},
{1,3,1,0,0,0,'^'},
{1,4,0,2,4,4,'J'},
{2,0,2,0,4,3,'L'},
{2,0,2,1,0,3,'\\'},
{2,0,3,2,4,3,'k'},
{2,0,3,3,3,3,'h'},
{2,0,3,3,4,4,'b'},
{2,2,2,2,1,1,'!'},
{2,2,2,2,3,3,'X'},
{2,2,2,2,4,4,'U'},
{2,2,3,3,2,4,'t'},
{2,2,3,3,3,3,'H'},
{2,2,3,3,4,4,'W'},
{2,2,4,1,4,3,'K'},
{2,3,2,0,3,2,'('},
{2,3,3,1,3,0,'f'},
{2,3,4,4,3,3,'@'},
{3,1,3,3,1,2,'%'},
{3,2,0,2,2,3,')'},
{3,2,3,2,4,4,'&'},
{3,2,4,4,3,4,'N'},
{3,3,1,1,0,0,'"'},
{3,3,1,2,2,2,'?'},
{3,3,1,2,4,3,'2'},
{3,3,1,3,3,4,'3'},
{3,3,2,0,4,3,'C'},
{3,3,2,1,4,4,'G'},
{3,3,2,2,3,4,'S'},
{3,3,2,2,4,4,'Q'},
{3,3,2,3,3,4,'9'},
{3,3,3,2,4,4,'6'},
{3,3,3,3,3,3,'M'},
{3,3,3,3,4,4,'8'},
{3,3,4,4,1,1,'*'},
{3,4,1,2,3,3,'7'},
{3,4,2,2,4,3,'Z'},
{4,2,2,2,4,4,'l'},
{4,3,2,2,4,4,'D'},
{4,3,3,1,3,0,'F'},
{4,3,3,1,4,3,'E'},
{4,3,3,2,3,4,'5'},
{4,3,3,3,3,0,'P'},
{4,3,3,3,3,3,'R'},
{4,3,3,3,4,4,'B'},
{4,4,2,2,3,3,'T'},
{4,4,2,2,4,4,'I'},
{4,4,3,3,1,1,'#'},
{4,4,4,4,4,4,'$'}
};
if ((fp_ascsub = fopen("gifscii1","r")) == NULL) {
printf("\nBuilding tables on file gifscii1");
printf("\n(this should only happen the first time)\n");
if ((fp_ascsub = fopen("gifscii1","w")) == NULL) ERROR("\nCan't create gifscii1 on disk\n");
for (i1 = 0; i1 <= 4; i1++) {
for (i2 = 0; i2 <= 4; i2++) {
for (i3 = 0; i3 <= 4; i3++) {
for (i4 = 0; i4 <= 4; i4++) {
for (i5 = 0; i5 <= 4; i5++) {
for (i6 = 0; i6 <= 4; i6++) {
curtot=100000; curix=5;
for (a1 = 0; a1 <= 72; a1++) {
d1 = asciitab[a1].am_ul - i1; d1 = d1 * d1;
d2 = asciitab[a1].am_ur - i2; d2 = d2 * d2;
d3 = asciitab[a1].am_ml - i3; d3 = d3 * d3;
d4 = asciitab[a1].am_mr - i4; d4 = d4 * d4;
d5 = asciitab[a1].am_bl - i5; d5 = d5 * d5;
d6 = asciitab[a1].am_br - i6; d6 = d6 * d6;
subtot = d1 + d2 + d3 + d4 + d5 + d6;
if (subtot < curtot) {
curtot = subtot;
curix = a1;
}
}
sub_char = asciitab[curix].am_char;
if ((fwrite(&sub_char,1,1,fp_ascsub))==0)
ERROR("\nError writing gifscii1 to disk\n");
}
}
}
}
}
}
fclose(fp_ascsub);
}
if ((fp_ascsub = fopen("gifscii1","r")) == NULL) ERROR("\nCan't find gifscii1 on disk\n");
for (i1 = 0; i1 <= 4; i1++) {
for (i2 = 0; i2 <= 4; i2++) {
for (i3 = 0; i3 <= 4; i3++) {
for (i4 = 0; i4 <= 4; i4++) {
for (i5 = 0; i5 <= 4; i5++) {
for (i6 = 0; i6 <= 4; i6++) {
if (fread(&sub_char,1,1,fp_ascsub)==0)
ERROR("\nError reading gifscii1 from disk\n");
asciisub[i1][i2][i3][i4][i5][i6] = sub_char;
}
}
}
}
}
}
fclose(fp_ascsub);
}

int ReadCode(fh)
FILE *fh;
{
register int temp;
register int DstMasked;
register int DstMask;
long size;
temp = 0;
DstMasked = 1L << CodeSize;
for (DstMask = 1; DstMask != DstMasked; DstMask <<= 1)
{
if (!ReadMask)
{
if (CompDataPointer == CompDataCount)
{
if ((size = fgetc(fh)) == -1)
{
printf("\nI/O Error during decompression.\n");
ReadError = 1;
return EOFCode;
}
if (fread((char *)CompData,1,size,fh) != size)
{
printf("\nI/O Error during decompression.\n");
ReadError = 1;
return EOFCode;
}
CompDataCount = size;
CompDataPointer = 0;
}
ReadMask = 1;
ByteBuf = CompData[CompDataPointer++];
}
if (ByteBuf & ReadMask) temp |= DstMask;
ReadMask <<= 1;
}
return temp;
}

void AddPixel(index)
UBYTE index;
{
register UWORD XStore;
register UWORD YStore;
XStore = Xpos + idesc.id_Left;
YStore = Ypos + idesc.id_Top;
BitPlane[YStore][XStore] = index;
if (++Xpos == idesc.id_Width)
{
Xpos = 0;
Ypos += LeaveStep[interleave];
if (Ypos >= idesc.id_Height) Ypos = LeaveFirst[++interleave];
}
}

BOOL DoImage(fh)
FILE *fh;
{
register int index;
register int colours;
int Code;
printf("Image #%ld encountered.\n", ImageNumber++);
if (fread((char *)&idesc,1,9,fh) != 9)
ERROR("Error reading image descriptor.\n");
FlipWord(&idesc.id_Left);
FlipWord(&idesc.id_Top);
FlipWord(&idesc.id_Width);
FlipWord(&idesc.id_Height);
interleave = idesc.id_Info & 1L << 6;
if (interleave) interleave = 1;
printf("Xpos from %d to %d, Ypos from %d to %d, %sinterlaced.\n",
idesc.id_Left, idesc.id_Left + idesc.id_Width - 1,
idesc.id_Top, idesc.id_Top + idesc.id_Height - 1,
interleave ? "" : "not ");
if (idesc.id_Info & 1L << 7)
{
colours = 1L << ((idesc.id_Info & 7) + 1);
printf("Local colour map contains %d entries.\n", colours);
for (index = 0; index < colours; index++)
if (fread(&LocalColourTable[index],1,3,fh) != 3)
ERROR("......Error reading local colour\n");
}
else
{
colours = 1L << ((gdesc.gd_ColInfo & 7) + 1);
for (index=0; index<colours; index++)
LocalColourTable[index]=GlobalColourTable[index];
}
Xpos = Ypos = 0;

{
int MaxCode, ClearCode, CurCode,
OldCode, InCode, FreeCode;
int OutCount;
int FirstFree;
UBYTE InitCodeSize, FinChar, BitMask;
if ((CodeSize = fgetc(fh)) == -1)
ERROR("\n......I/O Error during decompression.\n");
ClearCode = 1L << CodeSize;
EOFCode = ClearCode + 1;
FreeCode = FirstFree = ClearCode + 2;
CodeSize++;
InitCodeSize = CodeSize;
MaxCode = 1L << CodeSize;
ReadError = ReadMask = OutCount = 0;
CompDataPointer = CompDataCount = 0;
BitMask = colours - 1;
Code = ReadCode(fh);
while (Code != EOFCode)
{
if (ReadError) return TRUE;
if (Code == ClearCode)
{
CodeSize = InitCodeSize;
MaxCode = 1L << CodeSize;
FreeCode = FirstFree;
FinChar = CurCode = OldCode = Code = ReadCode(fh);
AddPixel(FinChar);
}
else
{
CurCode = InCode = Code;
if (CurCode >= FreeCode)
{
CurCode = OldCode;
OutCode[OutCount++] = FinChar;
}
while (CurCode > BitMask)
{
if (OutCount > 1024)
{
printf("\nCorrupt GIF file (OutCount)\n");
return TRUE;
}
OutCode[OutCount++] = Suffix[CurCode];
CurCode = Prefix[CurCode];
}
FinChar = CurCode;
AddPixel(FinChar);
for (index = OutCount - 1; index >= 0; index--)
AddPixel(OutCode[index]);
OutCount = 0;
Prefix[FreeCode] = OldCode;
Suffix[FreeCode] = FinChar;
OldCode = InCode;
if (++FreeCode >= MaxCode)
{
if (CodeSize < 12)
{
CodeSize++;
MaxCode <<= 1;
}
}
}
Code = ReadCode(fh);
}
}
if ((Code = fgetc(fh)) == -1) return TRUE;
if (Code != 0) printf("Warning: Unaligned packet.\n");
return FALSE;
}

#define base 5

UBYTE AvgRed,AvgGreen,AvgBlue;

void contrast_colour(colour)
struct RGB colour;
{
/*
int w1,w2,w3;
w1 = (int) (((colour.rgb_Red - AvgRed) * 110)/100 + AvgRed);
if (w1<0) w1=0; if (w1>255) w1=255;
colour.rgb_Red = (UBYTE) w1;
w1 = (int) (((colour.rgb_Green - AvgGreen) * 110)/100 + AvgGreen);
if (w1<0) w1=0; if (w1>255) w1=255;
colour.rgb_Green = (UBYTE) w1;
w1 = (int) (((colour.rgb_Blue - AvgBlue) * 110)/100 + AvgBlue);
if (w1<0) w1=0; if (w1>255) w1=255;
colour.rgb_Blue = (UBYTE) w1;
*/
}

void show(name)
char *name;
{
UBYTE asciisub [5][5][5][5][5][5];
UWORD c1,c2,c3,c4,c5,c6;
UWORD x1,x2,y1,y2,y3,y4,y5;
double SumsqRed,SumsqGreen,SumsqBlue,SumRed,SumGreen,SumBlue,SumCt;
double VarRed,VarGreen,VarBlue,VarTotal;
UWORD Bright1,Bright2,Bright3,Bright4,TotBright,AvgBright,StdBright;
UBYTE b_w=0;
UWORD gri,rdi,bli;
register int i,j,x,y;
register int ix,iy,ii,jj;
struct RGB colour;
char Cbuf [1];
int flag, dx, dy, z, dcnt, g;
x=y=ix=iy=dx=dy=i=j=z=dcnt=g=0;
gri=59; rdi=30; bli=11;
dx=((idesc.id_Width<<base)/gx);
dy=((idesc.id_Height<<base)/gy);
x1=1;x2=2;y1=1;y2=2;y3=3;y4=4;y5=5;
BuildSubsTable(asciisub);
do
{
printf("\n(v)iew (z Z)oom (c)opy (l,r,u,d)=Pan (i)nvert (h)elp: ");
while ((flag=getchar()) == (char)10);
if (flag == 'v' || flag == 'c' || flag == 'i' || flag == 'h' ||
flag == 'z' || flag == 'Z' ||
flag == 'l' || flag == 'r' || flag == 'u' || flag == 'd'); else return;
if (flag=='c'){
printf("\nAppending copy of screen to file gifsciis\n");
if (!(CAPT = fopen("gifsciis","a"))) ERROR("Open error\n");
Cbuf[0] = 10;
if(fwrite(Cbuf,1,1,CAPT) != 1)
ERROR("Unable to write to gifsciis.\n");
if(fwrite(name,1,strlen(name),CAPT) != strlen(name))
ERROR("Unable to write to gifsciis.\n");
}
if (flag=='l'){if(ix>=dx*gx/4) ix=ix-dx*gx/4;};
if (flag=='r'){if(ix+dx*gx+2*dx < ((idesc.id_Width)<<base)) ix=ix+dx*gx/4;};
if (flag=='u'){if(iy>=dy*gy/4) iy=iy-dy*gy/4;};
if (flag=='d'){if(iy+dy*gy+2*dy < ((idesc.id_Height)<<base)) iy=iy+dy*gy/4;};
if (flag=='i') b_w = b_w ^ 1;
if (flag=='z'){
dx>>=1;
dy>>=1;
ix=ix+dx*gx/2;
iy=iy+dy*gy/2;
};
if (flag=='Z'){
dx=((idesc.id_Width<<base)/gx);
dy=((idesc.id_Height<<base)/gy);
ix=0;
iy=0;
};
if (flag == 'h'){
printf("\n ");
printf("v = View The Gif\n");
printf("z = Zoom In On The Center\n");
printf("Z = Zoom Out To Normal Size\n");
printf("c = Capture to disk\n");
printf("l = Pan Left\n");
printf("r = Pan Right\n");
printf("u = Pan Up\n");
printf("d = Pan Down\n");
printf("i = Invert Black/White\n");
printf("Any other character will quit\n");
}
else {

/* Copyright Feb, 1994 Paul Kline. pk6...@acad.drake.edu */
/* All rights to these tables and gif-ascii conversion routines reserved. */
/* These tables and routines may be copied and distributed except that no */
/* computer program may be developed from them for commercial or for-profit */
/* purposes without the express written permission of the copyright holder */
/* This notice is not to be understood as granting such permission. */
/* All output of the program is the property of the owner of the original */
/* artwork. */

SumCt=1;
SumsqRed=SumsqGreen=SumsqBlue=0;
SumRed=SumGreen=SumBlue=0;
y=iy;
for (j=1; j<gy; j++)
{
x=ix;
for (i=1; i<gx; i++)
{
colour=LocalColourTable[BitPlane[(y>>base)+idesc.id_Top]
[(x>>base)+idesc.id_Left]];
AvgBright = colour.rgb_Red+colour.rgb_Green+colour.rgb_Blue;
if ((AvgBright < 730) && (AvgBright > 35)) {
SumCt++;
SumRed += (double) colour.rgb_Red;
SumsqRed += ((double)colour.rgb_Red * (double)colour.rgb_Red);
SumGreen += (double) colour.rgb_Green;
SumsqGreen += ((double)colour.rgb_Green * (double)colour.rgb_Green);
SumBlue += (double) colour.rgb_Blue;
SumsqBlue += ((double)colour.rgb_Blue * (double)colour.rgb_Blue);
}
x += dx;
}
y += dy;
}
VarRed = SumsqRed - (SumRed*SumRed / SumCt);
VarGreen = SumsqGreen - (SumGreen*SumGreen / SumCt);
VarBlue = SumsqBlue - (SumBlue*SumBlue / SumCt);
VarTotal = VarRed + VarGreen + VarBlue;
rdi = (UWORD) (VarRed * 100 / VarTotal);
gri = (UWORD) (VarGreen * 100 / VarTotal);
bli = (UWORD) (100-rdi-gri);
VarRed = sqrt(VarRed);
VarGreen = sqrt(VarGreen);
VarBlue = sqrt(VarBlue);
AvgRed= SumRed/SumCt; AvgGreen=SumGreen/SumCt; AvgBlue=SumBlue/SumCt;
if (AvgRed>=AvgGreen && AvgRed>=AvgBlue)
{AvgBright=AvgRed;
StdBright=VarRed;
}
else {if (AvgGreen > AvgRed && AvgGreen >=AvgBlue)
{AvgBright=AvgGreen;
StdBright=VarGreen;
}
else
{AvgBright=AvgBlue;
StdBright=VarBlue;
}
}
if (ImageColours==2) {
rdi=gri=bli=33;
AvgBright=128;
Bright1=51; Bright2=2*Bright1; Bright3=3*Bright1; Bright4=4*Bright1;
}
StdBright=StdBright*2/5;
if ((AvgBright+StdBright) < 255) Bright3=AvgBright+StdBright; else Bright3=AvgBright+(255-AvgBright)/5;
Bright4=Bright3+(255-Bright3)/2;
if ((AvgBright-StdBright) > 0) Bright2=AvgBright-StdBright; else Bright2=AvgBright*4/5;
Bright1=Bright2/2;
/*
printf("\n %f %f %f %f %f %f",SumsqRed,SumsqGreen,SumsqBlue,SumRed,SumGreen,SumBlue);
printf("\n %f %f %f %u %u %u",VarRed,VarGreen,VarBlue,rdi,gri,bli);
printf("\n %u %u %u %u %u %u ",AvgBright,StdBright,Bright1,Bright2,Bright3,Bright4);
*/
y=iy;
for (j=0; j<gy; j++)
{
if (flag=='c'){
Cbuf[0] = 10;
if(fwrite(Cbuf,1,1,CAPT) != 1)
ERROR("Unable to write to gifsciis.\n");
}
printf("\n");
x=ix;
for (i=0; i<gx; i++)
{
c1=c2=c3=c4=c5=c6=0;
colour=LocalColourTable[BitPlane[((y>>base))+idesc.id_Top][((x>>base))+idesc.id_Left]];
contrast_colour(colour);
c1=c1+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base))+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c1=c1+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y1)+idesc.id_Top][((x>>base))+idesc.id_Left]];
contrast_colour(colour);
c1=c1+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y1)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c1=c1+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base))+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c2=c2+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base))+idesc.id_Top][((x>>base)+x2)+idesc.id_Left]];
contrast_colour(colour);
c2=c2+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y1)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c2=c2+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y1)+idesc.id_Top][((x>>base)+x2)+idesc.id_Left]];
contrast_colour(colour);
c2=c2+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y2)+idesc.id_Top][((x>>base))+idesc.id_Left]];
contrast_colour(colour);
c3=c3+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y2)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c3=c3+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y3)+idesc.id_Top][((x>>base))+idesc.id_Left]];
contrast_colour(colour);
c3=c3+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y3)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c3=c3+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y2)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c4=c4+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y2)+idesc.id_Top][((x>>base)+x2)+idesc.id_Left]];
contrast_colour(colour);
c4=c4+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y3)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c4=c4+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y3)+idesc.id_Top][((x>>base)+x2)+idesc.id_Left]];
contrast_colour(colour);
c4=c4+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y4)+idesc.id_Top][((x>>base))+idesc.id_Left]];
contrast_colour(colour);
c5=c5+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y4)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c5=c5+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y5)+idesc.id_Top][((x>>base))+idesc.id_Left]];
contrast_colour(colour);
c5=c5+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y5)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c5=c5+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y4)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c6=c6+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y4)+idesc.id_Top][((x>>base)+x2)+idesc.id_Left]];
contrast_colour(colour);
c6=c6+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y5)+idesc.id_Top][((x>>base)+x1)+idesc.id_Left]];
contrast_colour(colour);
c6=c6+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
colour=LocalColourTable[BitPlane[((y>>base)+y5)+idesc.id_Top][((x>>base)+x2)+idesc.id_Left]];
contrast_colour(colour);
c6=c6+(UWORD)(colour.rgb_Red*rdi+colour.rgb_Green*gri+colour.rgb_Blue*bli)/100;
c1=c1>>2;
c2=c2>>2;
c3=c3>>2;
c4=c4>>2;
c5=c5>>2;
c6=c6>>2;
if (c1<Bright1) c1=0;
if (c1>=Bright4) c1=4;
if (c1>=Bright3) c1=3;
if (c1>=Bright2) c1=2;
if (c1>=Bright1) c1=1;
if (c2<Bright1) c2=0;
if (c2>=Bright4) c2=4;
if (c2>=Bright3) c2=3;
if (c2>=Bright2) c2=2;
if (c2>=Bright1) c2=1;
if (c3<Bright1) c3=0;
if (c3>=Bright4) c3=4;
if (c3>=Bright3) c3=3;
if (c3>=Bright2) c3=2;
if (c3>=Bright1) c3=1;
if (c4<Bright1) c4=0;
if (c4>=Bright4) c4=4;
if (c4>=Bright3) c4=3;
if (c4>=Bright2) c4=2;
if (c4>=Bright1) c4=1;
if (c5<Bright1) c5=0;
if (c5>=Bright4) c5=4;
if (c5>=Bright3) c5=3;
if (c5>=Bright2) c5=2;
if (c5>=Bright1) c5=1;
if (c6<Bright1) c6=0;
if (c6>=Bright4) c6=4;
if (c6>=Bright3) c6=3;
if (c6>=Bright2) c6=2;
if (c6>=Bright1) c6=1;
if (b_w){
c1=4-c1;
c2=4-c2;
c3=4-c3;
c4=4-c4;
c5=4-c5;
c6=4-c6;
}
/*
printf("\n%d %d %d %d %d %d %d %d",c1,c2,c3,c4,c5,c6,x>>base,y>>base);
*/
if (flag=='c'){
Cbuf[0] = (char) asciisub [c1][c2][c3][c4][c5][c6];
if(fwrite(Cbuf,1,1,CAPT) != 1)
ERROR("Unable to write to gifsciis.\n");
}
printf("%c",asciisub [c1][c2][c3][c4][c5][c6]);
x+=dx;
if (x<0)return;
}
y+=dy;
}
}
if (flag== 'c') fclose(CAPT);
} while (TRUE);
}

main(argc,argv)
int argc;
char *argv[];
{
register int index;
char sig[7];
int size;
int error;
int colours;
long cmdcode;
char gif_filename[250],gif_width[5]="00000",gif_height[5]="00000";
if (argc >= 2) {strcpy(argv[1],gif_filename);}
else {
printf("\nWhat is the name of the gif file?\n");
gets(gif_filename);
}
if (argc >= 3) {strcpy(argv[2],gif_width);}
else {
printf("\nOutput will be sized to the number of lines and columns you specify");
printf("\n");
printf("\nLines Columns Lines Columns");
printf("\nSpec. Spec. Output Output");
printf("\n----- ----- ----- -----");
printf("\n 0 0 GH/10 GW/6");
printf("\n 0 C C*GH/GW*.6 C");
printf("\n L 0 L L*GW/GH/.6");
printf("\n L C L C");
printf("\n");
printf("\nGW = GifWidth, GH = GifHeight");
printf("\n");
printf("\nHow many columns to create? ");
gets(gif_width);
}
if (argc >= 4) {strcpy(argv[3],gif_width);}
else {
printf("\nHow many lines to create? ");
gets(gif_height);
}
gx=atoi(gif_width);
gy=atoi(gif_height);
if (!(GIFfh = fopen(gif_filename,"rb"))) ERROR("Open error\n"); /*"rb" for pc, "r" for SOME mainframes */
sig[6] = '\0';
if (fread(sig,1,6,GIFfh) != 6 || strcmp("GIF87a", sig))
ERROR("Not an 87a GIF file...\n");
if (fread((char *)&gdesc,1,7,GIFfh) != 7)
ERROR("Error reading screen descriptor\n");
FlipWord(&gdesc.gd_Width);
FlipWord(&gdesc.gd_Height);
printf("Gifscii\n");
if (gx==0 && gy==0) {
gx=gdesc.gd_Width/6;
gy=gdesc.gd_Height/10;
}
if (gx==0 && gy>0) gx = gy * gdesc.gd_Width / gdesc.gd_Height * 10/6;
if (gy==0 && gx>0) gy = gx * gdesc.gd_Height / gdesc.gd_Width * 6/10;
printf("Signature = \"%s\", Width = %u, Height = %u, Columns = %u, Lines = %u,\n",
sig, gdesc.gd_Width, gdesc.gd_Height,gx,gy);
colours = 1L << ((gdesc.gd_ColInfo & 7) + 1);
ImageColours=(ULONG) colours;
if (!(gdesc.gd_ColInfo & 1L << 7))
{
printf("No global colour map supplied, using internal.\n");
for (index = 0; index < colours; index++)
{
GlobalColourTable[index].rgb_Red =
GlobalColourTable[index].rgb_Green =
GlobalColourTable[index].rgb_Blue = index;
}
}
else
{
printf("Global colour map contains %d entries.\n", colours);
for (index = 0; index < colours; index++)
if (fread(&GlobalColourTable[index],1,3,GIFfh) != 3)
ERROR("Error reading global colour\n");
}
size = ((gdesc.gd_Width + 7) / 8) + 1;
size += (size + 127) >> 7;
if (!(BitPlane = (UBYTE **)malloc(gdesc.gd_Height * sizeof(UBYTE *))))
ERROR("Not enough memory\n");
size = (gdesc.gd_Width + 1) * sizeof(UBYTE);
for (index = 0; index < gdesc.gd_Height; index++)
if (!(BitPlane[index] = (UBYTE *)malloc(size)))
ERROR("Not enough memory\n");
ImageNumber = 1;
for (error = FALSE; error == FALSE;)
{
if ((cmdcode = fgetc(GIFfh)) == -1) break;
if (cmdcode ==
GIF_IMAGE){
error = DoImage(GIFfh);
} else
if (cmdcode ==
GIF_EXTENSION){
error = TRUE;
} else
if (cmdcode ==
GIF_TERMINATOR){
show(gif_filename);
break;
}
else {
printf("Unknown directive encountered.\n");
error = TRUE;
}
}
printf("End of GIF session\n");
exit(0);
}


0 new messages