📄 giftrans.c
字号:
(void)fwrite((void *)gce,5,1,dest);
}
}
if (output&&gct_delay) {
if (verbose)
(void)fprintf(stderr,"Warning: Global Color Table has not been modified as no Transparent Color Index has been set\n");
(void)fwrite((void *)gct,gct_size,3,dest);
gct_delay=FALSE;
}
/* Write Image Descriptor */
if (verbose) {
(void)fprintf(stderr,"\tImage Left Position: %d pixels\n",readword(buffer+1));
(void)fprintf(stderr,"\tImage Top Position: %d pixels\n",readword(buffer+3));
(void)fprintf(stderr,"\tImage Width: %d pixels\n",readword(buffer+5));
(void)fprintf(stderr,"\tImage Height: %d pixels\n",readword(buffer+7));
(void)fprintf(stderr,"\tLocal Color Table Flag: %s\n",readflag(buffer[9]&0x80));
(void)fprintf(stderr,"\tInterlace Flag: %s\n",readflag(buffer[9]&0x40));
if (buffer[9]&0x80) {
(void)fprintf(stderr,"\tSort Flag: %s\n",readflag(buffer[9]&0x20));
(void)fprintf(stderr,"\tSize of Global Color Table: %d colors\n",2<<(buffer[9]&0x7));
}
}
if (debug)
dump(pos,buffer,10);
if (output)
(void)fwrite((void *)buffer,10,1,dest);
/* Local Color Table */
if (buffer[8]&0x80) {
size=2<<(buffer[8]&0x7);
pos=ftell(src);
(void)fread((void *)buffer,size,3,src);
if (verbose) {
(void)fprintf(stderr,"Local Color Table:\n");
for(cnt=0;cnt<size;cnt++)
(void)fprintf(stderr,"\tColor %d: Red %d, Green %d, Blue %d\n",cnt,buffer[3*cnt],buffer[3*cnt+1],buffer[3*cnt+2]);
}
if (tc.index>=0) { /* Transparent Color Flag set */
buffer[3*tc.index]=tn.red;
buffer[3*tc.index+1]=tn.green;
buffer[3*tc.index+2]=tn.blue;
}
if (debug)
dump(pos,buffer,size*3);
if (output)
(void)fwrite((void *)buffer,size,3,dest);
}
/* Table Based Image Data */
pos=ftell(src);
(void)fread((void *)buffer,1,1,src);
if (verbose) {
(void)fprintf(stderr,"Table Based Image Data:\n");
(void)fprintf(stderr,"\tLZW Minimum Code Size: 0x%02x\n",buffer[0]);
}
if (debug)
dump(pos,buffer,1);
if (output)
(void)fwrite((void *)buffer,1,1,dest);
transdata(src,dest);
gce_present=FALSE;
break;
case 0x3b: /* Trailer */
if (verbose)
(void)fprintf(stderr,"Trailer\n");
if (debug)
dump(pos,buffer,1);
if (comment&&*comment&&output) {
(void)fputs("\041\376",dest);
writedata(dest,(unsigned char *)comment,strlen(comment));
}
if (output)
(void)fwrite((void *)buffer,1,1,dest);
break;
case 0x21: /* Extension */
(void)fread((void *)(buffer+1),1,1,src);
switch (buffer[1]) {
case 0x01: /* Plain Text Extension */
if (output&&gct_delay) {
if (verbose)
(void)fprintf(stderr,"Warning: Global Color Table has not been modified due to a Plain Text Extension\n");
(void)fwrite((void *)gct,gct_size,3,dest);
gct_delay=FALSE;
}
if (verbose)
(void)fprintf(stderr,"Plain Text Extension\n");
if (debug)
dump(pos,buffer,2);
if (output)
(void)fwrite((void *)buffer,2,1,dest);
transblock(src,dest);
transdata(src,dest);
break;
case 0xf9: /* Graphic Control Extension */
if (verbose)
(void)fprintf(stderr,"Graphic Control Extension:\n");
(void)fread((void *)(buffer+2),1,1,src);
size=buffer[2];
(void)fread((void *)gce,size,1,src);
if (verbose) {
(void)fprintf(stderr,"\tDisposal Method: %d ",gce[0]&0x1c>>2);
switch (gce[0]&0x1c>>2) {
case 0:
(void)fprintf(stderr,"(no disposal specified)\n");
break;
case 1:
(void)fprintf(stderr,"(do not dispose)\n");
break;
case 2:
(void)fprintf(stderr,"(restore to background color)\n");
break;
case 3:
(void)fprintf(stderr,"(restore to previous)\n");
break;
default:
(void)fprintf(stderr,"(to be defined)\n");
}
(void)fprintf(stderr,"\tUser Input Flag: %s\n",readflag(gce[0]&0x2));
(void)fprintf(stderr,"\tTransparent Color Flag: %s\n",readflag(gce[0]&0x1));
(void)fprintf(stderr,"\tDelay Time: %d\n",readword(gce+1));
if (gce[0]&0x1)
(void)fprintf(stderr,"\tTransparent Color Index: %d\n",gce[3]);
}
if (debug) {
dump(pos,buffer,3);
dump(pos+3,gce,size);
}
pos=ftell(src);
(void)fread((void *)buffer,1,1,src);
if (debug)
dump(pos,buffer,1);
gce_present=TRUE;
break;
case 0xfe: /* Comment Extension */
if (verbose)
(void)fprintf(stderr,"Comment Extension\n");
if (debug)
dump(pos,buffer,2);
if (skipcomment)
skipdata(src);
else {
if (output&&gct_delay) {
if (verbose)
(void)fprintf(stderr,"Warning: Global Color Table has not been modified due to a Comment Extension\n");
(void)fwrite((void *)gct,gct_size,3,dest);
gct_delay=FALSE;
}
if (output)
(void)fwrite((void *)buffer,2,1,dest);
transdata(src,dest);
}
break;
case 0xff: /* Application Extension */
if (output&&gct_delay) {
if (verbose)
(void)fprintf(stderr,"Warning: Global Color Table has not been modified due to a Application Extension\n");
(void)fwrite((void *)gct,gct_size,3,dest);
gct_delay=FALSE;
}
if (verbose)
(void)fprintf(stderr,"Application Extension\n");
if (debug)
dump(pos,buffer,2);
if (output)
(void)fwrite((void *)buffer,2,1,dest);
transblock(src,dest);
transdata(src,dest);
break;
default:
if (output&&gct_delay) {
if (verbose)
(void)fprintf(stderr,"Warning: Global Color Table has not been modified due to an unknown Extension\n");
(void)fwrite((void *)gct,gct_size,3,dest);
gct_delay=FALSE;
}
if (verbose)
(void)fprintf(stderr,"Unknown label: 0x%02x\n",buffer[1]);
if (debug)
dump(pos,buffer,2);
if (output)
(void)fwrite((void *)buffer,2,1,dest);
transblock(src,dest);
transdata(src,dest);
break;
}
break;
default:
(void)fprintf(stderr,"0x%08lx: Unknown extension 0x%02x!\n",ftell(src)-1,buffer[0]);
if (debug)
dump(pos,buffer,1);
return(1);
}
} while (buffer[0]!=0x3b&&!feof(src));
return(buffer[0]==0x3b?SUCCESS:FAILURE);
}
int getindex(c,arg)
struct color *c;
char *arg;
{
struct entry *ptr;
if ('0'<=*arg&&*arg<='9')
c->index=atoi(arg);
else if (*arg=='#') {
if (strlen(arg)==4) {
c->index=RGB;
c->red=hex(arg[1])<<4;
c->green=hex(arg[2])<<4;
c->blue=hex(arg[3])<<4;
}
else if (strlen(arg)==7) {
c->index=RGB;
c->red=(hex(arg[1])<<4)+hex(arg[2]);
c->green=(hex(arg[3])<<4)+hex(arg[4]);
c->blue=(hex(arg[5])<<4)+hex(arg[6]);
}
else {
(void)fprintf(stderr,"%s: illegal color specification: %s\n",image,arg);
return(FAILURE);
}
}
else {
for (ptr=root;ptr&&c->index!=RGB;ptr=ptr->next)
if (!strcmp(ptr->name,arg)) {
c->index=RGB;
c->red=ptr->red;
c->green=ptr->green;
c->blue=ptr->blue;
}
if (c->index!=RGB) {
(void)fprintf(stderr,"%s: no such color: %s\n",image,arg);
return(FAILURE);
}
}
return(SUCCESS);
}
void usage()
{
(void)fprintf(stderr,"Usage: %s [-t color|-T] [-B color] [-b color] [-g oldcolor=newcolor] [-c comment|-C] [-l|-L|-V] [-o filename] [-e filename] [filename]\n",image);
(void)fprintf(stderr,"Convert any GIF file into a GIF89a, with the folloing changes possible:\n");
(void)fprintf(stderr,"-t Specify the transparent color\n");
(void)fprintf(stderr,"-T Index of the transparent color is the background color index\n");
(void)fprintf(stderr,"-B Specify the transparent color's new value\n");
(void)fprintf(stderr,"-b Specify the background color\n");
(void)fprintf(stderr,"-g Change a color in the global color table\n");
(void)fprintf(stderr,"-c Add a comment\n");
(void)fprintf(stderr,"-C Remove old comment\n");
(void)fprintf(stderr,"-l Only list the color table\n");
(void)fprintf(stderr,"-L Verbose output of GIFs contents\n");
(void)fprintf(stderr,"-V Verbose output while converting\n");
(void)fprintf(stderr,"-o Redirect stdout to a file\n");
(void)fprintf(stderr,"-e Redirect stderr to a file\n");
if (*rgb)
(void)fprintf(stderr,"Colors may be specified as index, as rgb.txt entry or in the #rrggbb form.\n");
else
(void)fprintf(stderr,"Colors may be specified as index or in the #rrggbb form.\n");
exit(1);
}
int main(argc,argv)
int argc;
char *argv[];
{
int c;
extern char *optarg;
extern int optind;
char error[2*MAXPATHLEN+14],line[BUFSIZ],*ptr,*nptr,*oname,*ename;
struct entry **next;
FILE *src;
int stat;
image=argv[0];
root=NULL;
if (*rgb)
if ((src=fopen(rgb,"r"))!=NULL) {
next= &root;
while (fgets(line,sizeof(line),src)) {
*next=(struct entry *)malloc(sizeof(struct entry));
for (ptr=line;strchr(" \t",*ptr);ptr++);
for (nptr=ptr;!strchr(" \t",*ptr);ptr++);
*ptr++='\0';
(*next)->red=atoi(nptr);
for (;strchr(" \t",*ptr);ptr++);
for (nptr=ptr;!strchr(" \t",*ptr);ptr++);
*ptr++='\0';
(*next)->green=atoi(nptr);
for (;strchr(" \t",*ptr);ptr++);
for (nptr=ptr;!strchr(" \t",*ptr);ptr++);
*ptr++='\0';
(*next)->blue=atoi(nptr);
for (;strchr(" \t",*ptr);ptr++);
for (nptr=ptr;!strchr(" \t\r\n",*ptr);ptr++);
*ptr='\0';
(void)strcpy((*next)->name=(char *)malloc(strlen(nptr)+1),nptr);
(*next)->next=NULL;
next= &(*next)->next;
}
(void)fclose(src);
}
else {
#ifndef MSDOS
(void)sprintf(error,"%s: cannot open %s",image,rgb);
perror(error);
return(FAILURE);
#else /* MSDOS */
*rgb='\0';
#endif
}
bc.index=NONE;
tc.index=NONE;
tn.index=NONE;
go.index=NONE;
gn.index=NONE;
comment=NULL;
skipcomment=FALSE;
verbose=FALSE;
output=TRUE;
debug=FALSE;
oname=NULL;
ename=NULL;
while ((c=getopt(argc,argv,"t:TB:b:g:c:ClLVDo:e:vh?")) != EOF)
switch ((char)c) {
case 'b':
if (getindex(&bc,optarg))
return(FAILURE);
break;
case 't':
if (getindex(&tc,optarg))
return(FAILURE);
break;
case 'T':
tc.index=OTHER;
break;
case 'B':
if (getindex(&tn,optarg))
return(FAILURE);
break;
case 'g':
if ((ptr=strchr(optarg,'='))!=NULL) {
*ptr++='\0';
if (getindex(&go,optarg))
return(FAILURE);
if (getindex(&gn,ptr))
return(FAILURE);
}
else
usage();
break;
case 'c':
comment=optarg;
break;
case 'C':
skipcomment=TRUE;
break;
case 'l':
list=TRUE;
output=FALSE;
break;
case 'L':
verbose=TRUE;
output=FALSE;
break;
case 'V':
verbose=TRUE;
break;
case 'D':
debug=TRUE;
break;
case 'o':
oname=optarg;
break;
case 'e':
ename=optarg;
break;
case 'v':
(void)fprintf(stderr,header);
return(0);
case 'h':
(void)fprintf(stderr,header);
case '?':
usage();
}
if (optind+1<argc||(bc.index==NONE&&tc.index==NONE&&tn.index==NONE&&gn.index==NONE&&comment==NULL&&!skipcomment&&!list&&!verbose))
usage();
if (oname&&freopen(oname,"wb",stdout)==NULL) {
(void)sprintf(error,"%s: cannot open %s",image,oname);
perror(error);
return(FAILURE);
}
if (ename&&freopen(ename,"wb",stderr)==NULL) {
(void)sprintf(error,"%s: cannot open %s",image,ename);
perror(error);
return(FAILURE);
}
#ifdef MSDOS
if(oname==NULL&&(stdout->flags&_F_TERM)==0&&setmode(fileno(stdout),O_BINARY)!=0) {
(void)fprintf(stderr,"%s: can't set stdout's mode to binary\n",image);
exit(2);
}
if(optind==argc&&(stdin->flags&_F_TERM)==0&&setmode(fileno(stdin),O_BINARY)) {
(void)fprintf(stderr,"%s: can't set stdin's mode to binary\n",image);
exit(2);
}
#endif /* MSDOS */
if (optind<argc)
if (strcmp(argv[optind],"-"))
if ((src=fopen(argv[optind],"rb"))!=NULL) {
stat=giftrans(src,stdout);
(void)fclose(src);
}
else {
(void)sprintf(error,"%s: cannot open %s",image,argv[optind]);
perror(error);
return(FAILURE);
}
else
stat=giftrans(stdin,stdout);
else
stat=giftrans(stdin,stdout);
(void)fclose(stdout);
(void)fclose(stderr);
return(stat);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -