📄 postgif.c
字号:
#include <stdio.h>#include <string.h>#include <signal.h>#include <ctype.h>#ifdef plan9#define isascii(c) ((unsigned char)(c)<=0177)#endif#include <sys/types.h>#include <fcntl.h>#include "comments.h"#include "gen.h"#include "path.h"#include "ext.h"#define dbprt if (debug) fprintfchar *optnames = "a:c:fglm:n:o:p:x:y:C:E:DG:IL:P:";char *prologue = POSTGIF; /* default PostScript prologue */char *formfile = FORMFILE; /* stuff for multiple pages per sheet */int formsperpage = 1; /* page images on each piece of paper */int copies = 1; /* and this many copies of each sheet */int page = 0; /* last page we worked on */int printed = 0; /* and the number of pages printed */extern char *malloc();extern void free();extern double atof(), pow();unsigned char ibuf[BUFSIZ];unsigned char *cmap, *gcmap, *lcmap;unsigned char *gmap, *ggmap, *lgmap;unsigned char *pmap;double gamma;float cr = 0.3, cg = 0.59, cb = 0.11;int maplength, gmaplength, lmaplength;int scrwidth, scrheight;int gcolormap, lcolormap;int bitperpixel, background;int imageleft, imagetop;int imagewidth, imageheight;int interlaced, lbitperpixel;int gray = 0;int gammaflag = 0;int negative = 0;int terminate = 0;int codesize, clearcode, endcode, curstblsize, pmindex, byteinibuf, bitsleft;int prefix[4096], suffix[4096], cstbl[4096];int bburx = -32767, bbury = -32767;FILE *fp_in = NULL;FILE *fp_out = stdout;char *allocate(size) int size;{ char *p; if ((p = malloc(size)) == NULL) error(FATAL, "not enough memory"); return(p);}voidputhex(c, fp) unsigned char c; FILE *fp;{ static char hextbl[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; putc(hextbl[(c >> 4) & 017], fp); putc(hextbl[c & 017], fp);}voidsetcolormap(bp) int bp;{ int i, entries = 1, scale = 1; unsigned char *p, *q; for (i = 0; i < bp; i++) entries *= 2; for (i = 0; i < 8 - bp; i++) scale *= 2; gcmap = (unsigned char *) allocate(entries*3); ggmap = (unsigned char *) allocate(entries); gmaplength = entries; for (i = 0, p = gcmap, q = ggmap; i < 256; i += scale, p += 3, q++) { if (negative) { *p = 255 - i; p[1] = *p; p[2] = *p; *q = *p; } else { *p = i; p[1] = i; p[2] = i; *q = i; } } if (gammaflag) for (i = 0, p = gcmap; i < 256; i += scale, p += 3) { *p = (unsigned char) (pow((double) *p/256.0, gamma)*256); p[1] = *p; p[2] = *p; }dbprt(stderr,"default color map:\n");for (i = 0; i < entries*3; i += 3)dbprt(stderr, "%d, %d, %d\n", gcmap[i], gcmap[i+1], gcmap[i+2]);}voidreadgcolormap(bp) int bp;{ int i, entries = 1; unsigned char *p, *q; for (i = 0; i < bp; i++) entries *= 2; gcmap = (unsigned char *) allocate(entries*3); ggmap = (unsigned char *) allocate(entries); gmaplength = entries; fread(gcmap, sizeof(*gcmap), entries*3, fp_in); if (negative) for (i = 0, p = gcmap; i < entries*3; i++, p++) *p = 255 - *p; for (i = 0, p = gcmap, q = ggmap; i < entries; i++, p += 3, q++) *q = cr*(int)p[0] + cg*(int)p[1] + cb*(int)p[2] + 0.5; if (gammaflag) for (i = 0, p = gcmap; i < entries*3; i++, p++) *p = (unsigned char) (pow((double) *p/256.0, gamma)*256);dbprt(stderr,"global color map:\n");for (i = 0; i < entries*3; i += 3)dbprt(stderr, "%d, %d, %d\n", gcmap[i], gcmap[i+1], gcmap[i+2]);}voidreadlcolormap(bp) int bp;{ int i, entries = 1; unsigned char *p, *q; for (i = 0; i < bp; i++) entries *= 2; lcmap = (unsigned char *) allocate(entries*3); lgmap = (unsigned char *) allocate(entries); lmaplength = entries; fread(lcmap, sizeof(*lcmap), entries*3, fp_in); if (negative) for (i = 0, p = lcmap; i < entries*3; i++, p++) *p = 255 - *p; for (i = 0, p = lcmap, q = lgmap; i < entries; i++, p += 3, q++) *q = cr*(int)p[0] + cg*(int)p[1] + cb*(int)p[2] + 0.5; if (gammaflag) for (i = 0, p = lcmap; i < entries*3; i++, p++) *p = (unsigned char) (pow((double) *p/256.0, gamma)*256);dbprt(stderr,"local color map:\n");for (i = 0; i < entries*3; i += 3)dbprt(stderr, "%d, %d, %d\n", lcmap[i], lcmap[i+1], lcmap[i+2]);}voidinitstbl(){ int i, entries = 1, *p, *s; for (i = 0; i < codesize; i++) entries *= 2; clearcode = entries; endcode = clearcode + 1; for (i = 0, p = prefix, s = suffix; i <= endcode; i++, p++, s++) { *p = endcode; *s = i; } curstblsize = endcode + 1; pmindex = 0; byteinibuf = 0; bitsleft = 0;}intnextbyte(){ static ibufindex; if (byteinibuf) { byteinibuf--; ibufindex++; } else { fread(ibuf, sizeof(*ibuf), 1, fp_in); byteinibuf = ibuf[0];dbprt(stderr, "byte count: %d\n", byteinibuf); if (byteinibuf) fread(ibuf, sizeof(*ibuf), byteinibuf, fp_in); else error(FATAL, "encounter zero byte count block before end code"); ibufindex = 0; byteinibuf--; ibufindex++; } return(ibuf[ibufindex-1]);}int masktbl[25] = { 0, 01, 03, 07, 017, 037, 077, 0177, 0377, 0777, 01777, 03777, 07777, 017777, 037777, 077777, 0177777, 0377777, 0777777, 01777777, 03777777, 07777777, 017777777, 037777777, 077777777};intgetcode(){ int cs, c; static int oldc; if (curstblsize < 4096) cs = cstbl[curstblsize]; else cs = 12; while (bitsleft < cs) { oldc = (oldc & masktbl[bitsleft]) | ((nextbyte() & 0377) << bitsleft); bitsleft += 8; } c = oldc & masktbl[cs]; oldc = oldc >> cs; bitsleft -= cs;/* dbprt(stderr, "code: %d %d %d\n", curstblsize, cs, c); */ return(c);}voidputcode(c) int c;{ if (prefix[c] != endcode) { putcode(prefix[c]); pmap[pmindex] = suffix[c]; pmindex++; } else { pmap[pmindex] = suffix[c]; pmindex++; }}intfirstof(c) int c;{ while (prefix[c] != endcode) c = prefix[c]; return(suffix[c]);}voidwriteimage(){ int i, j, k;dbprt(stderr, "pmindex: %d\n", pmindex); fputs("save\n", fp_out); fprintf(fp_out, "/codestr %d string def\n", imagewidth); if (!gray) { fprintf(fp_out, "/colortbl currentfile %d string readhexstring\n", maplength*3); for (i = 0; i < maplength; i++) puthex(cmap[i], fp_out); fputs("\n", fp_out); for (i = maplength ; i < maplength*2; i++) puthex(cmap[i], fp_out); fputs("\n", fp_out); for (i = maplength*2 ; i < maplength*3; i++) puthex(cmap[i], fp_out); fputs("\npop def\n", fp_out); fprintf(fp_out, "/graytbl currentfile %d string readhexstring\n", maplength); for (i = 0; i < maplength; i++) puthex(gmap[i], fp_out); fputs("\npop def\n", fp_out); } fprintf(fp_out, "%s %d %d %d %d gifimage\n", gray ? "true" : "false", imagewidth, imageheight, scrwidth - imageleft - imagewidth, scrheight - imagetop - imageheight); if (gray) { if (interlaced) { int *iltbl; iltbl = (int *) allocate(imageheight*sizeof(int)); j = 0; for (i = 0; i < imageheight; i += 8) { iltbl[i] = j; j += imagewidth; }dbprt(stderr, "pass1: %d\n", j); for (i = 4; i < imageheight; i += 8) { iltbl[i] = j; j += imagewidth; }dbprt(stderr, "pass2: %d\n", j); for (i = 2; i < imageheight; i += 4) { iltbl[i] = j; j += imagewidth; }dbprt(stderr, "pass3: %d\n", j); for (i = 1; i < imageheight; i += 2) { iltbl[i] = j; j += imagewidth; }dbprt(stderr, "pass4: %d\n", j); for (i = 0; i < imageheight; i++) { k = iltbl[i]; for (j = 0; j < imagewidth; j++, k++) puthex(gmap[pmap[k]], fp_out); fputs("\n", fp_out); } } else { for (i = 0, k = 0; i < imageheight; i++) { for (j = 0; j < imagewidth; j++, k++) puthex(gmap[pmap[k]], fp_out); fputs("\n", fp_out); } } } else { if (interlaced) { int *iltbl; iltbl = (int *) allocate(imageheight*sizeof(int)); j = 0; for (i = 0; i < imageheight; i += 8) { iltbl[i] = j; j += imagewidth; }dbprt(stderr, "pass1: %d\n", j); for (i = 4; i < imageheight; i += 8) { iltbl[i] = j; j += imagewidth; }dbprt(stderr, "pass2: %d\n", j); for (i = 2; i < imageheight; i += 4) { iltbl[i] = j; j += imagewidth; }dbprt(stderr, "pass3: %d\n", j); for (i = 1; i < imageheight; i += 2) { iltbl[i] = j; j += imagewidth; }dbprt(stderr, "pass4: %d\n", j); for (i = 0; i < imageheight; i++) { k = iltbl[i]; for (j = 0; j < imagewidth; j++, k++) puthex(pmap[k], fp_out); fputs("\n", fp_out); } } else { for (i = 0, k = 0; i < imageheight; i++) { for (j = 0; j < imagewidth; j++, k++) puthex(pmap[k], fp_out); fputs("\n", fp_out); } } } fputs("restore\n", fp_out);}voidreadimage(){ int bytecount, zerobytecount = 0; int code, oldcode; fread(ibuf, sizeof(*ibuf), 9, fp_in); imageleft = ibuf[0] + 256*ibuf[1]; imagetop = ibuf[2] + 256*ibuf[3]; imagewidth = ibuf[4] + 256*ibuf[5]; imageheight = ibuf[6] + 256*ibuf[7]; lcolormap = ibuf[8] & 0200; interlaced = ibuf[8] & 0100; lbitperpixel = (ibuf[8] & 07) + 1;dbprt(stderr, "imageleft: %d\n", imageleft);dbprt(stderr, "imagetop: %d\n", imagetop);dbprt(stderr, "imagewidth: %d\n", imagewidth);dbprt(stderr, "imgaeheight: %d\n", imageheight);dbprt(stderr, "lcolormap: %d\n", lcolormap ? 1 : 0);dbprt(stderr, "interlaced: %d\n", interlaced ? 1 : 0);dbprt(stderr, "lbitperpixel: %d\n", lbitperpixel); if (lcolormap) { readlcolormap(lbitperpixel);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -