postgif.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <signal.h>
  4. #include <ctype.h>
  5. #ifdef plan9
  6. #define isascii(c) ((unsigned char)(c)<=0177)
  7. #endif
  8. #include <sys/types.h>
  9. #include <fcntl.h>
  10. #include "comments.h"
  11. #include "gen.h"
  12. #include "path.h"
  13. #include "ext.h"
  14. #define dbprt if (debug) fprintf
  15. char *optnames = "a:c:fglm:n:o:p:x:y:C:E:DG:IL:P:";
  16. char *prologue = POSTGIF; /* default PostScript prologue */
  17. char *formfile = FORMFILE; /* stuff for multiple pages per sheet */
  18. int formsperpage = 1; /* page images on each piece of paper */
  19. int copies = 1; /* and this many copies of each sheet */
  20. int page = 0; /* last page we worked on */
  21. int printed = 0; /* and the number of pages printed */
  22. extern char *malloc();
  23. extern void free();
  24. extern double atof(), pow();
  25. unsigned char ibuf[BUFSIZ];
  26. unsigned char *cmap, *gcmap, *lcmap;
  27. unsigned char *gmap, *ggmap, *lgmap;
  28. unsigned char *pmap;
  29. double gamma;
  30. float cr = 0.3, cg = 0.59, cb = 0.11;
  31. int maplength, gmaplength, lmaplength;
  32. int scrwidth, scrheight;
  33. int gcolormap, lcolormap;
  34. int bitperpixel, background;
  35. int imageleft, imagetop;
  36. int imagewidth, imageheight;
  37. int interlaced, lbitperpixel;
  38. int gray = 0;
  39. int gammaflag = 0;
  40. int negative = 0;
  41. int terminate = 0;
  42. int codesize, clearcode, endcode, curstblsize, pmindex, byteinibuf, bitsleft;
  43. int prefix[4096], suffix[4096], cstbl[4096];
  44. int bburx = -32767, bbury = -32767;
  45. FILE *fp_in = NULL;
  46. FILE *fp_out = stdout;
  47. char *
  48. allocate(size)
  49. int size;
  50. {
  51. char *p;
  52. if ((p = malloc(size)) == NULL) error(FATAL, "not enough memory");
  53. return(p);
  54. }
  55. void
  56. puthex(c, fp)
  57. unsigned char c;
  58. FILE *fp;
  59. {
  60. static char hextbl[16] = {
  61. '0', '1', '2', '3', '4', '5', '6', '7',
  62. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
  63. };
  64. putc(hextbl[(c >> 4) & 017], fp);
  65. putc(hextbl[c & 017], fp);
  66. }
  67. void
  68. setcolormap(bp)
  69. int bp;
  70. {
  71. int i, entries = 1, scale = 1;
  72. unsigned char *p, *q;
  73. for (i = 0; i < bp; i++) entries *= 2;
  74. for (i = 0; i < 8 - bp; i++) scale *= 2;
  75. gcmap = (unsigned char *) allocate(entries*3);
  76. ggmap = (unsigned char *) allocate(entries);
  77. gmaplength = entries;
  78. for (i = 0, p = gcmap, q = ggmap; i < 256; i += scale, p += 3, q++) {
  79. if (negative) {
  80. *p = 255 - i; p[1] = *p; p[2] = *p;
  81. *q = *p;
  82. }
  83. else {
  84. *p = i; p[1] = i; p[2] = i;
  85. *q = i;
  86. }
  87. }
  88. if (gammaflag)
  89. for (i = 0, p = gcmap; i < 256; i += scale, p += 3) {
  90. *p = (unsigned char) (pow((double) *p/256.0, gamma)*256);
  91. p[1] = *p; p[2] = *p;
  92. }
  93. dbprt(stderr,"default color map:\n");
  94. for (i = 0; i < entries*3; i += 3)
  95. dbprt(stderr, "%d, %d, %d\n", gcmap[i], gcmap[i+1], gcmap[i+2]);
  96. }
  97. void
  98. readgcolormap(bp)
  99. int bp;
  100. {
  101. int i, entries = 1;
  102. unsigned char *p, *q;
  103. for (i = 0; i < bp; i++) entries *= 2;
  104. gcmap = (unsigned char *) allocate(entries*3);
  105. ggmap = (unsigned char *) allocate(entries);
  106. gmaplength = entries;
  107. fread(gcmap, sizeof(*gcmap), entries*3, fp_in);
  108. if (negative)
  109. for (i = 0, p = gcmap; i < entries*3; i++, p++) *p = 255 - *p;
  110. for (i = 0, p = gcmap, q = ggmap; i < entries; i++, p += 3, q++)
  111. *q = cr*(int)p[0] + cg*(int)p[1] + cb*(int)p[2] + 0.5;
  112. if (gammaflag)
  113. for (i = 0, p = gcmap; i < entries*3; i++, p++)
  114. *p = (unsigned char) (pow((double) *p/256.0, gamma)*256);
  115. dbprt(stderr,"global color map:\n");
  116. for (i = 0; i < entries*3; i += 3)
  117. dbprt(stderr, "%d, %d, %d\n", gcmap[i], gcmap[i+1], gcmap[i+2]);
  118. }
  119. void
  120. readlcolormap(bp)
  121. int bp;
  122. {
  123. int i, entries = 1;
  124. unsigned char *p, *q;
  125. for (i = 0; i < bp; i++) entries *= 2;
  126. lcmap = (unsigned char *) allocate(entries*3);
  127. lgmap = (unsigned char *) allocate(entries);
  128. lmaplength = entries;
  129. fread(lcmap, sizeof(*lcmap), entries*3, fp_in);
  130. if (negative)
  131. for (i = 0, p = lcmap; i < entries*3; i++, p++) *p = 255 - *p;
  132. for (i = 0, p = lcmap, q = lgmap; i < entries; i++, p += 3, q++)
  133. *q = cr*(int)p[0] + cg*(int)p[1] + cb*(int)p[2] + 0.5;
  134. if (gammaflag)
  135. for (i = 0, p = lcmap; i < entries*3; i++, p++)
  136. *p = (unsigned char) (pow((double) *p/256.0, gamma)*256);
  137. dbprt(stderr,"local color map:\n");
  138. for (i = 0; i < entries*3; i += 3)
  139. dbprt(stderr, "%d, %d, %d\n", lcmap[i], lcmap[i+1], lcmap[i+2]);
  140. }
  141. void
  142. initstbl()
  143. {
  144. int i, entries = 1, *p, *s;
  145. for (i = 0; i < codesize; i++) entries *= 2;
  146. clearcode = entries;
  147. endcode = clearcode + 1;
  148. for (i = 0, p = prefix, s = suffix; i <= endcode; i++, p++, s++) {
  149. *p = endcode;
  150. *s = i;
  151. }
  152. curstblsize = endcode + 1;
  153. pmindex = 0;
  154. byteinibuf = 0;
  155. bitsleft = 0;
  156. }
  157. int
  158. nextbyte()
  159. {
  160. static ibufindex;
  161. if (byteinibuf) {
  162. byteinibuf--;
  163. ibufindex++;
  164. }
  165. else {
  166. fread(ibuf, sizeof(*ibuf), 1, fp_in);
  167. byteinibuf = ibuf[0];
  168. dbprt(stderr, "byte count: %d\n", byteinibuf);
  169. if (byteinibuf) fread(ibuf, sizeof(*ibuf), byteinibuf, fp_in);
  170. else error(FATAL, "encounter zero byte count block before end code");
  171. ibufindex = 0;
  172. byteinibuf--;
  173. ibufindex++;
  174. }
  175. return(ibuf[ibufindex-1]);
  176. }
  177. int masktbl[25] = {
  178. 0, 01, 03, 07, 017, 037, 077, 0177, 0377, 0777, 01777, 03777, 07777,
  179. 017777, 037777, 077777, 0177777, 0377777, 0777777, 01777777, 03777777,
  180. 07777777, 017777777, 037777777, 077777777
  181. };
  182. int
  183. getcode()
  184. {
  185. int cs, c;
  186. static int oldc;
  187. if (curstblsize < 4096) cs = cstbl[curstblsize];
  188. else cs = 12;
  189. while (bitsleft < cs) {
  190. oldc = (oldc & masktbl[bitsleft]) | ((nextbyte() & 0377) << bitsleft);
  191. bitsleft += 8;
  192. }
  193. c = oldc & masktbl[cs];
  194. oldc = oldc >> cs;
  195. bitsleft -= cs;
  196. /* dbprt(stderr, "code: %d %d %d\n", curstblsize, cs, c); */
  197. return(c);
  198. }
  199. void
  200. putcode(c)
  201. int c;
  202. {
  203. if (prefix[c] != endcode) {
  204. putcode(prefix[c]);
  205. pmap[pmindex] = suffix[c];
  206. pmindex++;
  207. }
  208. else {
  209. pmap[pmindex] = suffix[c];
  210. pmindex++;
  211. }
  212. }
  213. int
  214. firstof(c)
  215. int c;
  216. {
  217. while (prefix[c] != endcode) c = prefix[c];
  218. return(suffix[c]);
  219. }
  220. void
  221. writeimage()
  222. {
  223. int i, j, k;
  224. dbprt(stderr, "pmindex: %d\n", pmindex);
  225. fputs("save\n", fp_out);
  226. fprintf(fp_out, "/codestr %d string def\n", imagewidth);
  227. if (!gray) {
  228. fprintf(fp_out, "/colortbl currentfile %d string readhexstring\n",
  229. maplength*3);
  230. for (i = 0; i < maplength; i++) puthex(cmap[i], fp_out);
  231. fputs("\n", fp_out);
  232. for (i = maplength ; i < maplength*2; i++) puthex(cmap[i], fp_out);
  233. fputs("\n", fp_out);
  234. for (i = maplength*2 ; i < maplength*3; i++) puthex(cmap[i], fp_out);
  235. fputs("\npop def\n", fp_out);
  236. fprintf(fp_out, "/graytbl currentfile %d string readhexstring\n",
  237. maplength);
  238. for (i = 0; i < maplength; i++) puthex(gmap[i], fp_out);
  239. fputs("\npop def\n", fp_out);
  240. }
  241. fprintf(fp_out, "%s %d %d %d %d gifimage\n",
  242. gray ? "true" : "false", imagewidth, imageheight,
  243. scrwidth - imageleft - imagewidth, scrheight - imagetop - imageheight);
  244. if (gray) {
  245. if (interlaced) {
  246. int *iltbl;
  247. iltbl = (int *) allocate(imageheight*sizeof(int));
  248. j = 0;
  249. for (i = 0; i < imageheight; i += 8) {
  250. iltbl[i] = j;
  251. j += imagewidth;
  252. }
  253. dbprt(stderr, "pass1: %d\n", j);
  254. for (i = 4; i < imageheight; i += 8) {
  255. iltbl[i] = j;
  256. j += imagewidth;
  257. }
  258. dbprt(stderr, "pass2: %d\n", j);
  259. for (i = 2; i < imageheight; i += 4) {
  260. iltbl[i] = j;
  261. j += imagewidth;
  262. }
  263. dbprt(stderr, "pass3: %d\n", j);
  264. for (i = 1; i < imageheight; i += 2) {
  265. iltbl[i] = j;
  266. j += imagewidth;
  267. }
  268. dbprt(stderr, "pass4: %d\n", j);
  269. for (i = 0; i < imageheight; i++) {
  270. k = iltbl[i];
  271. for (j = 0; j < imagewidth; j++, k++)
  272. puthex(gmap[pmap[k]], fp_out);
  273. fputs("\n", fp_out);
  274. }
  275. }
  276. else {
  277. for (i = 0, k = 0; i < imageheight; i++) {
  278. for (j = 0; j < imagewidth; j++, k++)
  279. puthex(gmap[pmap[k]], fp_out);
  280. fputs("\n", fp_out);
  281. }
  282. }
  283. }
  284. else {
  285. if (interlaced) {
  286. int *iltbl;
  287. iltbl = (int *) allocate(imageheight*sizeof(int));
  288. j = 0;
  289. for (i = 0; i < imageheight; i += 8) {
  290. iltbl[i] = j;
  291. j += imagewidth;
  292. }
  293. dbprt(stderr, "pass1: %d\n", j);
  294. for (i = 4; i < imageheight; i += 8) {
  295. iltbl[i] = j;
  296. j += imagewidth;
  297. }
  298. dbprt(stderr, "pass2: %d\n", j);
  299. for (i = 2; i < imageheight; i += 4) {
  300. iltbl[i] = j;
  301. j += imagewidth;
  302. }
  303. dbprt(stderr, "pass3: %d\n", j);
  304. for (i = 1; i < imageheight; i += 2) {
  305. iltbl[i] = j;
  306. j += imagewidth;
  307. }
  308. dbprt(stderr, "pass4: %d\n", j);
  309. for (i = 0; i < imageheight; i++) {
  310. k = iltbl[i];
  311. for (j = 0; j < imagewidth; j++, k++) puthex(pmap[k], fp_out);
  312. fputs("\n", fp_out);
  313. }
  314. }
  315. else {
  316. for (i = 0, k = 0; i < imageheight; i++) {
  317. for (j = 0; j < imagewidth; j++, k++) puthex(pmap[k], fp_out);
  318. fputs("\n", fp_out);
  319. }
  320. }
  321. }
  322. fputs("restore\n", fp_out);
  323. }
  324. void
  325. readimage()
  326. {
  327. int bytecount, zerobytecount = 0;
  328. int code, oldcode;
  329. fread(ibuf, sizeof(*ibuf), 9, fp_in);
  330. imageleft = ibuf[0] + 256*ibuf[1];
  331. imagetop = ibuf[2] + 256*ibuf[3];
  332. imagewidth = ibuf[4] + 256*ibuf[5];
  333. imageheight = ibuf[6] + 256*ibuf[7];
  334. lcolormap = ibuf[8] & 0200;
  335. interlaced = ibuf[8] & 0100;
  336. lbitperpixel = (ibuf[8] & 07) + 1;
  337. dbprt(stderr, "imageleft: %d\n", imageleft);
  338. dbprt(stderr, "imagetop: %d\n", imagetop);
  339. dbprt(stderr, "imagewidth: %d\n", imagewidth);
  340. dbprt(stderr, "imgaeheight: %d\n", imageheight);
  341. dbprt(stderr, "lcolormap: %d\n", lcolormap ? 1 : 0);
  342. dbprt(stderr, "interlaced: %d\n", interlaced ? 1 : 0);
  343. dbprt(stderr, "lbitperpixel: %d\n", lbitperpixel);
  344. if (lcolormap) {
  345. readlcolormap(lbitperpixel);
  346. cmap = lcmap;
  347. gmap = lgmap;
  348. maplength = lmaplength;
  349. }
  350. dbprt(stderr, "start reading raster data\n");
  351. fread(ibuf, sizeof(*ibuf), 1, fp_in);
  352. codesize = ibuf[0];
  353. dbprt(stderr, "codesize: %d\n", codesize);
  354. pmap = (unsigned char *) allocate(imagewidth*imageheight);
  355. initstbl();
  356. while ((code = getcode()) != endcode) {
  357. if (code == clearcode) {
  358. curstblsize = endcode + 1;
  359. code = getcode();
  360. putcode(code);
  361. oldcode = code;
  362. }
  363. else if (code < curstblsize) {
  364. putcode(code);
  365. prefix[curstblsize] = oldcode;
  366. suffix[curstblsize] = firstof(code);
  367. curstblsize++;
  368. oldcode = code;
  369. }
  370. else {
  371. if (code != curstblsize) error(FATAL, "code out of order");
  372. prefix[curstblsize] = oldcode;
  373. suffix[curstblsize] = firstof(oldcode);
  374. curstblsize++;
  375. putcode(curstblsize-1);
  376. oldcode = code;
  377. }
  378. }
  379. dbprt(stderr, "finish reading raster data\n");
  380. /* read the rest of the raster data */
  381. do {
  382. fread(ibuf, sizeof(*ibuf), 1, fp_in);
  383. bytecount = ibuf[0];
  384. dbprt(stderr, "byte count: %d\n", bytecount);
  385. if (bytecount) fread(ibuf, sizeof(*ibuf), bytecount, fp_in);
  386. else zerobytecount = 1;
  387. } while (!zerobytecount);
  388. writeimage();
  389. if (lcolormap) {
  390. cmap = gcmap;
  391. gmap = ggmap;
  392. maplength = gmaplength;
  393. free(lcmap);
  394. free(lgmap);
  395. }
  396. }
  397. void
  398. readextensionblock()
  399. {
  400. int functioncode, bytecount, zerobytecount = 0;
  401. fread(ibuf, sizeof(*ibuf), 1, fp_in);
  402. functioncode = ibuf[0];
  403. dbprt(stderr, "function code: %d\n", functioncode);
  404. do {
  405. fread(ibuf, sizeof(*ibuf), 1, fp_in);
  406. bytecount = ibuf[0];
  407. dbprt(stderr, "byte count: %d\n", bytecount);
  408. if (bytecount) fread(ibuf, sizeof(*ibuf), bytecount, fp_in);
  409. else zerobytecount = 1;
  410. } while (!zerobytecount);
  411. }
  412. void
  413. writebgscr()
  414. {
  415. fprintf(fp_out, "%s %d %d\n", PAGE, page, printed+1);
  416. fputs("/saveobj save def\n", fp_out);
  417. fprintf(fp_out, "%s: %d %d %d %d\n",
  418. "%%PageBoundingBox", 0, 0, scrwidth, scrheight);
  419. if (scrwidth > bburx) bburx = scrwidth;
  420. if (scrheight > bbury) bbury = scrheight;
  421. fprintf(fp_out, "%d %d gifscreen\n", scrwidth, scrheight);
  422. }
  423. void
  424. writeendscr()
  425. {
  426. if ( fp_out == stdout ) printed++;
  427. fputs("showpage\n", fp_out);
  428. fputs("saveobj restore\n", fp_out);
  429. fprintf(fp_out, "%s %d %d\n", ENDPAGE, page, printed);
  430. }
  431. void
  432. redirect(pg)
  433. int pg; /* next page we're printing */
  434. {
  435. static FILE *fp_null = NULL; /* if output is turned off */
  436. if ( pg >= 0 && in_olist(pg) == ON )
  437. fp_out = stdout;
  438. else if ( (fp_out = fp_null) == NULL )
  439. fp_out = fp_null = fopen("/dev/null", "w");
  440. }
  441. void
  442. readgif()
  443. {
  444. int i, j, k;
  445. for (i = 0, j = 1, k = 0; i < 13; i++) {
  446. for (; k < j; k++) cstbl[k] = i;
  447. j *= 2;
  448. }
  449. fread(ibuf, sizeof(*ibuf), 6, fp_in);
  450. dbprt(stderr, "%.6s\n", ibuf);
  451. if (strncmp((char *)ibuf, "GIF87a", 6) != 0) {
  452. fread(ibuf, sizeof(*ibuf), 122, fp_in);
  453. fread(ibuf, sizeof(*ibuf), 6, fp_in);
  454. dbprt(stderr, "%.6s\n", ibuf);
  455. if (strncmp((char *)ibuf, "GIF87a", 6) != 0)
  456. error(FATAL, "wrong GIF signature");
  457. }
  458. fread(ibuf, sizeof(*ibuf), 7, fp_in);
  459. scrwidth = ibuf[0] + 256*ibuf[1];
  460. scrheight = ibuf[2] + 256*ibuf[3];
  461. gcolormap = ibuf[4] & 0200;
  462. bitperpixel = (ibuf[4] & 07) + 1;
  463. background = ibuf[5];
  464. dbprt(stderr, "scrwidth: %d\n", scrwidth);
  465. dbprt(stderr, "scrheight: %d\n", scrheight);
  466. dbprt(stderr, "gcolormap: %d\n", gcolormap ? 1 : 0);
  467. dbprt(stderr, "bitperpixel: %d\n", bitperpixel);
  468. dbprt(stderr, "background: %d\n", background);
  469. if (ibuf[6] != 0) error(FATAL, "wrong screen descriptor");
  470. if (gcolormap) readgcolormap(bitperpixel);
  471. else setcolormap(bitperpixel);
  472. redirect(++page);
  473. writebgscr();
  474. cmap = gcmap;
  475. gmap = ggmap;
  476. maplength = gmaplength;
  477. do {
  478. fread(ibuf, sizeof(*ibuf), 1, fp_in);
  479. if (ibuf[0] == ',') readimage();
  480. else if (ibuf[0] == ';') terminate = 1;
  481. else if (ibuf[0] == '!') readextensionblock();
  482. else
  483. error(FATAL, "wrong image separator character or wrong GIF terminator");
  484. } while (!terminate);
  485. writeendscr();
  486. free(gcmap);
  487. free(ggmap);
  488. }
  489. void
  490. init_signals()
  491. {
  492. if ( signal(SIGINT, interrupt) == SIG_IGN ) {
  493. signal(SIGINT, SIG_IGN);
  494. signal(SIGQUIT, SIG_IGN);
  495. signal(SIGHUP, SIG_IGN);
  496. }
  497. else {
  498. signal(SIGHUP, interrupt);
  499. signal(SIGQUIT, interrupt);
  500. }
  501. signal(SIGTERM, interrupt);
  502. }
  503. void
  504. header()
  505. {
  506. int ch; /* return value from getopt() */
  507. int old_optind = optind; /* for restoring optind - should be 1 */
  508. while ( (ch = getopt(argc, argv, optnames)) != EOF )
  509. if ( ch == 'L' )
  510. prologue = optarg;
  511. else if ( ch == '?' )
  512. error(FATAL, "");
  513. optind = old_optind; /* get ready for option scanning */
  514. fprintf(stdout, "%s", CONFORMING);
  515. fprintf(stdout, "%s %s\n", VERSION, PROGRAMVERSION);
  516. fprintf(stdout, "%s %s\n", BOUNDINGBOX, ATEND);
  517. fprintf(stdout, "%s %s\n", PAGES, ATEND);
  518. fprintf(stdout, "%s", ENDCOMMENTS);
  519. if ( cat(prologue) == FALSE )
  520. error(FATAL, "can't read %s", prologue);
  521. fprintf(stdout, "%s", ENDPROLOG);
  522. fprintf(stdout, "%s", BEGINSETUP);
  523. fprintf(stdout, "mark\n");
  524. }
  525. void
  526. options()
  527. {
  528. int ch; /* return value from getopt() */
  529. while ( (ch = getopt(argc, argv, optnames)) != EOF ) {
  530. switch ( ch ) {
  531. case 'a': /* aspect ratio */
  532. fprintf(stdout, "/aspectratio %s def\n", optarg);
  533. break;
  534. case 'c': /* copies */
  535. copies = atoi(optarg);
  536. fprintf(stdout, "/#copies %s store\n", optarg);
  537. break;
  538. case 'f':
  539. negative = TRUE;
  540. break;
  541. case 'g':
  542. gray = TRUE;
  543. break;
  544. case 'l':
  545. fprintf(stdout, "/alignment true def\n");
  546. break;
  547. case 'm': /* magnification */
  548. fprintf(stdout, "/magnification %s def\n", optarg);
  549. break;
  550. case 'n': /* forms per page */
  551. formsperpage = atoi(optarg);
  552. fprintf(stdout, "%s %s\n", FORMSPERPAGE, optarg);
  553. fprintf(stdout, "/formsperpage %s def\n", optarg);
  554. break;
  555. case 'o': /* output page list */
  556. out_list(optarg);
  557. break;
  558. case 'p': /* landscape or portrait mode */
  559. if ( *optarg == 'l' )
  560. fprintf(stdout, "/landscape true def\n");
  561. else fprintf(stdout, "/landscape false def\n");
  562. break;
  563. case 'x': /* shift things horizontally */
  564. fprintf(stdout, "/xoffset %s def\n", optarg);
  565. break;
  566. case 'y': /* and vertically on the page */
  567. fprintf(stdout, "/yoffset %s def\n", optarg);
  568. break;
  569. case 'C': /* copy file straight to output */
  570. if ( cat(optarg) == FALSE )
  571. error(FATAL, "can't read %s", optarg);
  572. break;
  573. case 'E': /* text font encoding - unnecessary */
  574. fontencoding = optarg;
  575. break;
  576. case 'D': /* debug flag */
  577. debug = ON;
  578. break;
  579. case 'G':
  580. gammaflag = ON;
  581. gamma = atof(optarg);
  582. break;
  583. case 'I': /* ignore FATAL errors */
  584. ignore = ON;
  585. break;
  586. case 'L': /* PostScript prologue file */
  587. prologue = optarg;
  588. break;
  589. case 'P': /* PostScript pass through */
  590. fprintf(stdout, "%s\n", optarg);
  591. break;
  592. case '?': /* don't understand the option */
  593. error(FATAL, "");
  594. break;
  595. default: /* don't know what to do for ch */
  596. error(FATAL, "missing case for option %c\n", ch);
  597. break;
  598. }
  599. }
  600. argc -= optind; /* get ready for non-option args */
  601. argv += optind;
  602. }
  603. void
  604. setup()
  605. {
  606. /*setencoding(fontencoding);*/
  607. fprintf(stdout, "setup\n");
  608. if ( formsperpage > 1 ) { /* followed by stuff for multiple pages
  609. */
  610. if ( cat(formfile) == FALSE )
  611. error(FATAL, "can't read %s", formfile);
  612. fprintf(stdout, "%d setupforms\n", formsperpage);
  613. } /* End if */
  614. fprintf(stdout, "%s", ENDSETUP);
  615. }
  616. void
  617. arguments()
  618. {
  619. if ( argc < 1 ) {
  620. fp_in = stdin;
  621. readgif();
  622. }
  623. else { /* at least one argument is left */
  624. while ( argc > 0 ) {
  625. if ( strcmp(*argv, "-") == 0 )
  626. fp_in = stdin;
  627. else if ( (fp_in = fopen(*argv, "r")) == NULL )
  628. error(FATAL, "can't open %s", *argv);
  629. readgif();
  630. if ( fp_in != stdin )
  631. fclose(fp_in);
  632. argc--;
  633. argv++;
  634. }
  635. }
  636. }
  637. void
  638. done()
  639. {
  640. fprintf(stdout, "%s", TRAILER);
  641. fprintf(stdout, "done\n");
  642. fprintf(stdout, "%s 0 0 %d %d\n", BOUNDINGBOX, bburx, bbury);
  643. fprintf(stdout, "%s %d\n", PAGES, printed);
  644. }
  645. main(agc, agv)
  646. int agc;
  647. char *agv[];
  648. {
  649. argc = agc;
  650. argv = agv;
  651. prog_name = argv[0];
  652. init_signals();
  653. header();
  654. options();
  655. setup();
  656. arguments();
  657. done();
  658. exit(0);
  659. }