📄 giftrans.c
字号:
/*
** GIFtrans v1.11.1
**
** Convert any GIF file into a GIF89a
** Allows for setting the transparent or background color, changing colors,
** adding or removing comments. Also code to analyze GIF contents.
**
** Copyright (c) 24.2.94 by Andreas Ley <ley@rz.uni-karlsruhe.de>
**
** Permission to use, copy, modify, and distribute this software for any
** purpose and without fee is hereby granted, provided that the above
** copyright notice appears in all copies. This software is provided "as is"
** and without any express or implied warranties.
**
** This program has been tested on a HP9000/720 with HP-UX A.08.07
** In this environment, neither lint -u nor gcc -Wall produce any messages.
** If you encounter any errors or need to make any changes to port it
** to another platform, please contact me.
**
** Known bugs:
** -B flag won't work if there's an Extension between the Global Color
** Table and the Image Descriptor (or Graphic Control Extension). If -V
** has been specified, a Warning Message will be displayed.
** Will be fixed in 2.0
** Always outputs GIF89a. Shouldn't do this if version is newer.
** -D option may output changed data instead of original data, use
** with caution, best only with then -L option.
**
** Version history
**
** Version 1.11.1 - 11.8.94
** Allows for use of the -g option without the -B option.
**
** Version 1.11 - 21.7.94
** Moved Plain Text Extension to the Extensions section where it belongs.
** Accept Unknown Extension Labels.
** Incorporated MS-DOS port by enzo@hk.net (Enzo Michelangeli).
** Added -o and -e options to redirect stdout and stderr.
** Added -D debug flag.
**
** Version 1.10.2 - 22.6.94
** Support for -DRGBTXT flag.
**
** Version 1.10.1 - 21.6.94
** Different rgb.txt file FreeBSD/386BSD.
**
** Version 1.10 - 19.6.94
** Added -g option to change a color in the global color table.
** Added -B option to change the color for the transparent color index.
**
** Version 1.9.1 - 7.6.94
** Different rgb.txt files for X11 and Open Windows.
**
** Version 1.9 - 1.6.94
** Fixed a bug which caused color names to be rejected.
**
** Version 1.8 - 30.5.94
** Accept #rrggbb style arguments.
** Do nothing if rgb-color not found in GIF.
**
** Version 1.7 - 16.5.94
** Added -l option to only list the color table.
** Added -L option for verbose output without creating a gif.
** Added -b option to change the background color index.
** Display all matching color names for color table entries.
** Fixed a bug which caused bad color names if rgb.txt starts with
** whitespace.
** Doesn't use strdup anymore.
** Fixed =& bug on dec machines.
**
** Version 1.6 - 5.4.94
** Added color names recognition.
**
** Version 1.5 - 15.3.94
** Added basic verbose output to analyze GIFs.
**
** Version 1.4 - 8.3.94
** Fixed off-by-one bug in Local Color table code.
** Added -c and -C options to add or remove a comment.
** Transparency is no longer the default.
**
** Thanx for bug reports, ideas and fixes to
** patricka@cs.kun.nl (Patrick Atoon)
** wes@msc.edu (Wes Barris)
** pmfitzge@ingr.com (Patrick M. Fitzgerald)
** hoesel@chem.rug.nl (Frans van Hoesel)
** boardman@jerry.sal.wisc.edu (Dan Boardman)
** krweiss@chip.ucdavis.edu (Ken Weiss)
** chuck.musciano@harris.com (Chuck Musciano)
** heycke@camis.stanford.edu (Torsten Heycke)
** claw@spacsun.rice.edu (Colin Law)
** jwalker@eos.ncsu.edu (Joseph C. Walker)
** Bjorn.Borud@alkymi.unit.no (Bjorn Borud)
** Christopher.Vance@adfa.oz.au (CJS Vance)
** pederl@norway.hp.com (Peder Langlo)
** I.Rutson@bradford.ac.uk (Ian Rutson)
** Nicolas.Pioch@enst.fr (Nicolas Pioch)
** john@charles.CS.UNLV.EDU (John Kilburg)
** enzo@hk.net (Enzo Michelangeli)
** twv@hpwtwe0.cup.hp.com (Terry von Gease)
**
** Original distribution site is
** ftp://ftp.rz.uni-karlsruhe.de/pub/net/www/tools/giftrans.c
** A man-page by knordlun@fltxa.helsinki.fi (Kai Nordlund) is at
** ftp://ftp.rz.uni-karlsruhe.de/pub/net/www/tools/giftrans.1
** To compile for MS-DOS, you need getopt:
** ftp://ftp.rz.uni-karlsruhe.de/pub/net/www/tools/getopt.c
** MS-DOS executable can be found at
** ftp://ftp.rz.uni-karlsruhe.de/pub/net/www/tools/giftrans.exe
** A template rgb.txt for use with the MS-DOS version can be found at
** ftp://ftp.rz.uni-karlsruhe.de/pub/net/www/tools/rgb.txt
** Additional info can be found on
** http://melmac.corp.harris.com/transparent_images.html
*/
#define X11 /* When using X Window System */
#undef OPENWIN /* When using Open Windows */
#undef X386 /* When using FreeBSD/386BSD */
#undef MSDOS /* When using Borland C (maybe MSC too) */
char header[]="GIFtrans v1.11.1\n(c) 1994 by Andreas Ley\n";
#ifndef RGBTXT
#ifdef X11
#define RGBTXT "/usr/lib/X11/rgb.txt"
#else /* X11 */
#ifdef OPENWIN
#define RGBTXT "/usr/openwin/lib/rgb.txt"
#else /* OPENWIN */
#ifdef X386
#define RGBTXT "/usr/X386/lib/X11/rgb.txt"
#else /* X386 */
#ifdef MSDOS
#define RGBTXT "rgb.txt"
#else /* MSDOS */
#define RGBTXT "";
#endif /* MSDOS */
#endif /* X386 */
#endif /* OPENWIN */
#endif /* X11 */
#endif /* RGBTXT */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifndef MSDOS
#include <unistd.h>
#include <sys/param.h>
#else
#include <fcntl.h>
#include "getopt.c"
#endif
#ifndef MAXPATHLEN
#define MAXPATHLEN 256
#endif /* MAXPATHLEN */
#define FALSE (0) /* This is the naked Truth */
#define TRUE (1) /* and this is the Light */
#define SUCCESS (0)
#define FAILURE (1)
struct entry {
struct entry *next;
char *name;
int red;
int green;
int blue;
} *root;
#define NONE (-1)
#define OTHER (-2)
#define RGB (-3)
struct color {
int index;
int red;
int green;
int blue;
} bc,tc,tn,go,gn;
static char *image,*comment;
static int skipcomment,list,verbose,output,debug;
static long int pos;
static char rgb[] = RGBTXT;
static char true[] = "True";
static char false[] = "False";
#define readword(buffer) ((buffer)[0]+256*(buffer)[1])
#define readflag(buffer) ((buffer)?true:false)
#define hex(c) ('a'<=(c)&&(c)<='z'?(c)-'a'+10:'A'<=(c)&&(c)<='Z'?(c)-'A'+10:(c)-'0')
void dump(adr,data,len)
long int adr;
unsigned char *data;
size_t len;
{
int i;
while (len>0) {
(void)fprintf(stderr,"%08lx:%*s",adr,(int)((adr%16)*3+(adr%16>8?1:0)),"");
for (i=adr%16;i<16&&len>0;i++,adr++,data++,len--)
(void)fprintf(stderr,"%s%02x",i==8?" ":" ",*data);
(void)fprintf(stderr,"\n");
}
}
void writedata(dest,data,len)
FILE *dest;
unsigned char *data;
size_t len;
{
unsigned char size;
while (len) {
size=len<256?len:255;
(void)fwrite((void *)&size,1,1,dest);
(void)fwrite((void *)data,(size_t)size,1,dest);
data+=size;
len-=size;
}
size=0;
(void)fwrite((void *)&size,1,1,dest);
}
void skipdata(src)
FILE *src;
{
unsigned char size,buffer[256];
do {
pos=ftell(src);
(void)fread((void *)&size,1,1,src);
if (debug)
dump(pos,&size,1);
if (debug) {
pos=ftell(src);
(void)fread((void *)buffer,(size_t)size,1,src);
dump(pos,buffer,(size_t)size);
}
else
(void)fseek(src,(long int)size,SEEK_CUR);
} while (!feof(src)&&size>0);
}
void transblock(src,dest)
FILE *src;
FILE *dest;
{
unsigned char size,buffer[256];
pos=ftell(src);
(void)fread((void *)&size,1,1,src);
if (debug)
dump(pos,&size,1);
if (output)
(void)fwrite((void *)&size,1,1,dest);
pos=ftell(src);
(void)fread((void *)buffer,(size_t)size,1,src);
if (debug)
dump(pos,buffer,(size_t)size);
if (output)
(void)fwrite((void *)buffer,(size_t)size,1,dest);
}
void transdata(src,dest)
FILE *src;
FILE *dest;
{
unsigned char size,buffer[256];
do {
pos=ftell(src);
(void)fread((void *)&size,1,1,src);
if (debug)
dump(pos,&size,1);
if (output)
(void)fwrite((void *)&size,1,1,dest);
pos=ftell(src);
(void)fread((void *)buffer,(size_t)size,1,src);
if (debug)
dump(pos,buffer,(size_t)size);
if (output)
(void)fwrite((void *)buffer,(size_t)size,1,dest);
} while (!feof(src)&&size>0);
}
int giftrans(src,dest)
FILE *src;
FILE *dest;
{
unsigned char buffer[3*256],lsd[7],gct[3*256],gce[5];
unsigned int cnt,cols,size,gct_size,gct_delay,gce_present;
struct entry *rgbptr;
/* Header */
pos=ftell(src);
(void)fread((void *)buffer,6,1,src);
if (strncmp((char *)buffer,"GIF",3)) {
(void)fprintf(stderr,"No GIF file!\n");
return(1);
}
if (verbose) {
buffer[6]='\0';
(void)fprintf(stderr,"Header: \"%s\"\n",buffer);
}
if (debug)
dump(pos,buffer,6);
if (output)
(void)fputs("GIF89a",dest);
/* Logical Screen Descriptor */
pos=ftell(src);
(void)fread((void *)lsd,7,1,src);
if (verbose) {
(void)fprintf(stderr,"Logical Screen Descriptor:\n");
(void)fprintf(stderr,"\tLogical Screen Width: %d pixels\n",readword(lsd));
(void)fprintf(stderr,"\tLogical Screen Height: %d pixels\n",readword(lsd+2));
(void)fprintf(stderr,"\tGlobal Color Table Flag: %s\n",readflag(lsd[4]&0x80));
(void)fprintf(stderr,"\tColor Resolution: %d bits\n",(lsd[4]&0x70>>4)+1);
if (lsd[4]&0x80) {
(void)fprintf(stderr,"\tSort Flag: %s\n",readflag(lsd[4]&0x8));
(void)fprintf(stderr,"\tSize of Global Color Table: %d colors\n",2<<(lsd[4]&0x7));
(void)fprintf(stderr,"\tBackground Color Index: %d\n",lsd[5]);
}
if (lsd[6])
(void)fprintf(stderr,"\tPixel Aspect Ratio: %d (Aspect Ratio %f)\n",lsd[6],((double)lsd[6]+15)/64);
}
if (debug)
dump(pos,lsd,7);
/* Global Color Table */
gct_delay=FALSE;
if (lsd[4]&0x80) {
gct_size=2<<(lsd[4]&0x7);
pos=ftell(src);
(void)fread((void *)gct,gct_size,3,src);
if (go.index==RGB)
for(cnt=0;cnt<gct_size&&go.index==RGB;cnt++)
if (gct[3*cnt]==go.red&&gct[3*cnt+1]==go.green&&gct[3*cnt+2]==go.blue)
go.index=cnt;
if (go.index>=0) {
if (gn.index>=0) {
gn.red=gct[3*gn.index];
gn.green=gct[3*gn.index+1];
gn.blue=gct[3*gn.index+2];
}
gct[3*go.index]=gn.red;
gct[3*go.index+1]=gn.green;
gct[3*go.index+2]=gn.blue;
}
if (bc.index==RGB)
for(cnt=0;cnt<gct_size&&bc.index==RGB;cnt++)
if (gct[3*cnt]==bc.red&&gct[3*cnt+1]==bc.green&&gct[3*cnt+2]==bc.blue)
bc.index=cnt;
if (bc.index>=0)
lsd[5]=bc.index;
if (tc.index==RGB)
for(cnt=0;cnt<gct_size&&tc.index==RGB;cnt++)
if (gct[3*cnt]==tc.red&&gct[3*cnt+1]==tc.green&&gct[3*cnt+2]==tc.blue)
tc.index=cnt;
if (tc.index==OTHER)
tc.index=lsd[5];
if (tn.index>=0) {
tn.red=gct[3*tn.index];
tn.green=gct[3*tn.index+1];
tn.blue=gct[3*tn.index+2];
}
if (tn.index!=NONE)
gct_delay=TRUE;
}
if (output)
(void)fwrite((void *)lsd,7,1,dest);
if (lsd[4]&0x80) {
if (list||verbose) {
(void)fprintf(stderr,"Global Color Table:\n");
for(cnt=0;cnt<gct_size;cnt++) {
(void)fprintf(stderr,"\tColor %d: Red %d, Green %d, Blue %d",cnt,gct[3*cnt],gct[3*cnt+1],gct[3*cnt+2]);
(void)fprintf(stderr,", #%02x%02x%02x",gct[3*cnt],gct[3*cnt+1],gct[3*cnt+2]);
for (rgbptr=root,cols=0;rgbptr;rgbptr=rgbptr->next)
if (rgbptr->red==gct[3*cnt]&&rgbptr->green==gct[3*cnt+1]&&rgbptr->blue==gct[3*cnt+2])
(void)fprintf(stderr,"%s%s",cols++?", ":" (",rgbptr->name);
(void)fprintf(stderr,"%s\n",cols?")":"");
}
}
if (debug)
dump(pos,gct,gct_size*3);
if (output&&(!gct_delay))
(void)fwrite((void *)gct,gct_size,3,dest);
}
gce_present=FALSE;
do {
pos=ftell(src);
(void)fread((void *)buffer,1,1,src);
switch (buffer[0]) {
case 0x2c: /* Image Descriptor */
if (verbose)
(void)fprintf(stderr,"Image Descriptor:\n");
(void)fread((void *)(buffer+1),9,1,src);
/* Write Graphic Control Extension */
if (tc.index>=0||gce_present) {
if (!gce_present) {
gce[0]=0;
gce[1]=0;
gce[2]=0;
}
if (tc.index>=0) {
gce[0]|=0x01; /* Set Transparent Color Flag */
gce[3]=tc.index; /* Set Transparent Color Index */
}
else if (gce[0]&0x01)
tc.index=gce[3]; /* Remember Transparent Color Index */
gce[4]=0;
if (tc.index>=0&&(!(buffer[8]&0x80))) { /* Transparent Color Flag set and no Local Color Table */
gct[3*tc.index]=tn.red;
gct[3*tc.index+1]=tn.green;
gct[3*tc.index+2]=tn.blue;
}
if (output&&gct_delay) {
(void)fwrite((void *)gct,gct_size,3,dest);
gct_delay=FALSE;
}
if (output) {
(void)fputs("\041\371\004",dest);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -