ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.27
Committed: Fri Jan 2 12:47:01 2004 UTC (20 years, 4 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.26: +34 -28 lines
Log Message:
Fixed typing/prototype of getheader() and its callback.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_ps.c,v 2.26 2003/07/27 22:12:03 schorsch Exp $";
3 #endif
4 /*
5 * Radiance picture to PostScript file translator -- one way!
6 */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <math.h>
11 #include <ctype.h>
12
13 #include "platform.h"
14 #include "color.h"
15 #include "resolu.h"
16
17 #define UPPER(c) ((c)&~0x20) /* ASCII trick */
18
19 #define CODE6GAM 1.47 /* gamma for 6-bit codes */
20 #define DEFGGAM 1.0 /* greyscale device gamma */
21 #define DEFCGAM 1.8 /* color device gamma */
22
23 #define GRY -1 /* artificial index for grey */
24
25 #define DEFMARG 0.5 /* default margin (inches) */
26 #define DEFWIDTH 8.5 /* default page width */
27 #define DEFHEIGHT 11 /* default page height */
28 #define HMARGIN (hmarg*72) /* horizontal margin */
29 #define VMARGIN (vmarg*72) /* vertical margin */
30 #define PWIDTH (width*72-2*HMARGIN) /* width of device */
31 #define PHEIGHT (height*72-2*VMARGIN) /* height of device */
32
33 #define RUNCHR '*' /* character to start rle */
34 #define MINRUN 4 /* minimum run-length */
35 #define RSTRT '!' /* character for MINRUN */
36 #define MAXRUN (MINRUN+'~'-RSTRT) /* maximum run-length */
37
38 char code[] = /* 6-bit code lookup table */
39 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+";
40
41 int wrongformat = 0; /* input in wrong format? */
42 double pixaspect = 1.0; /* pixel aspect ratio */
43
44 double devgam = 0.; /* device gamma response */
45 double hmarg = DEFMARG,
46 vmarg = DEFMARG; /* horizontal and vertical margins */
47 double width = DEFWIDTH,
48 height = DEFHEIGHT; /* default paper width and height */
49 double dpi = 0; /* print density (0 if unknown) */
50 int docolor = 1; /* produce color image? */
51 int bradj = 0; /* brightness adjustment */
52 int ncopies = 1; /* number of copies */
53
54 extern int Aputprim(), Bputprim(), Cputprim();
55
56 int (*putprim)() = Aputprim; /* function for writing scanline */
57
58 char *progname;
59
60 int xmax, ymax; /* input image dimensions */
61
62 extern double unit2inch();
63
64 static gethfunc headline;
65
66
67 static int
68 headline( /* check header line */
69 char *s,
70 void *p
71 )
72 {
73 char fmt[32];
74
75 if (isformat(s)) {
76 formatval(fmt, s);
77 wrongformat = strcmp(fmt, COLRFMT);
78 } else if (isaspect(s))
79 pixaspect *= aspectval(s);
80 return(0);
81 }
82
83
84 main(argc, argv)
85 int argc;
86 char *argv[];
87 {
88 int i;
89 double d;
90
91 progname = argv[0];
92
93 for (i = 1; i < argc; i++)
94 if (argv[i][0] == '-')
95 switch (argv[i][1]) {
96 case 'b': /* produce b&w PostScript */
97 docolor = 0;
98 break;
99 case 'c': /* produce color PostScript */
100 docolor = 1;
101 break;
102 case 'A': /* standard ASCII encoding */
103 putprim = Aputprim;
104 break;
105 case 'B': /* standard binary encoding */
106 putprim = Bputprim;
107 break;
108 case 'C': /* compressed ASCII encoding */
109 putprim = Cputprim;
110 break;
111 case 'd': /* print density */
112 dpi = atof(argv[++i]);
113 break;
114 case 'g': /* device gamma adjustment */
115 devgam = atof(argv[++i]);
116 break;
117 case 'p': /* paper size */
118 parsepaper(argv[++i]);
119 break;
120 case 'm': /* margin */
121 d = atof(argv[i+1]);
122 d *= unit2inch(argv[i+1]);
123 switch (argv[i][2]) {
124 case '\0':
125 hmarg = vmarg = d;
126 break;
127 case 'h':
128 hmarg = d;
129 break;
130 case 'v':
131 vmarg = d;
132 break;
133 default:
134 goto userr;
135 }
136 i++;
137 break;
138 case 'e': /* exposure adjustment */
139 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
140 goto userr;
141 bradj = atoi(argv[++i]);
142 break;
143 case 'n': /* number of copies */
144 ncopies = atoi(argv[++i]);
145 break;
146 default:
147 goto userr;
148 }
149 else
150 break;
151
152 if (i < argc-2)
153 goto userr;
154 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
155 fprintf(stderr, "%s: can't open input \"%s\"\n",
156 progname, argv[i]);
157 exit(1);
158 }
159 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
160 fprintf(stderr, "%s: can't open output \"%s\"\n",
161 progname, argv[i+1]);
162 exit(1);
163 }
164 SET_FILE_BINARY(stdin);
165 /* get our header */
166 getheader(stdin, headline, NULL);
167 if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
168 quiterr("bad picture format");
169 /* gamma compression */
170 if (devgam <= 0.05)
171 devgam = docolor ? DEFCGAM : DEFGGAM;
172 if (putprim == Cputprim)
173 setcolrgam(CODE6GAM);
174 else if (devgam != 1.)
175 setcolrgam(devgam);
176 /* write header */
177 PSheader(argc, argv);
178 /* convert file */
179 ra2ps();
180 /* write trailer */
181 PStrailer();
182 exit(0);
183 userr:
184 fprintf(stderr,
185 "Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-d dpi][-g gamma] [input [output]]\n",
186 progname);
187 exit(1);
188 }
189
190
191 double
192 unit2inch(s) /* determine unit */
193 register char *s;
194 {
195 static struct unit {char n; float f;} u[] = {
196 {'i', 1.},
197 {'m', 1./25.4},
198 {'c', 1./2.54},
199 {'\0',0} };
200 register struct unit *up;
201
202 while (*s && !isalpha(*s))
203 s++;
204 for (up = u; up->n; up++)
205 if (up->n == *s)
206 return(up->f);
207 return(1.);
208 }
209
210
211 int
212 matchid(name, id) /* see if name matches id (case insensitive) */
213 char *name;
214 register char *id;
215 {
216 register char *s = name;
217
218 while (*s) {
219 if (isalpha(*s)) {
220 if (!isalpha(*id) || UPPER(*s) != UPPER(*id))
221 return(0);
222 } else if (*s != *id)
223 return(0);
224 s++; id++;
225 }
226 return(!*id || s-name >= 3); /* substrings >= 3 chars OK */
227 }
228
229
230 parsepaper(ps) /* determine paper size from name */
231 char *ps;
232 {
233 static struct psize {char n[12]; float w,h;} p[] = {
234 {"envelope", 4.12, 9.5},
235 {"executive", 7.25, 10.5},
236 {"letter", 8.5, 11.},
237 {"lettersmall", 7.68, 10.16},
238 {"legal", 8.5, 14.},
239 {"monarch", 3.87, 7.5},
240 {"statement", 5.5, 8.5},
241 {"tabloid", 11., 17.},
242 {"A3", 11.69, 16.54},
243 {"A4", 8.27, 11.69},
244 {"A4small", 7.47, 10.85},
245 {"A5", 6.00, 8.27},
246 {"A6", 4.13, 6.00},
247 {"B4", 10.12, 14.33},
248 {"B5", 7.17, 10.12},
249 {"C5", 6.38, 9.01},
250 {"C6", 4.49, 6.38},
251 {"DL", 4.33, 8.66},
252 {"hagaki", 3.94, 5.83},
253 {"",0.0,0.0} };
254 register struct psize *pp;
255 register char *s = ps;
256 double d;
257
258 if (isdigit(*s)) { /* check for WWxHH specification */
259 width = atof(s);
260 while (*s && !isalpha(*s))
261 s++;
262 d = unit2inch(s);
263 height = atof(++s);
264 width *= d;
265 height *= d;
266 if ((width >= 1.) & (height >= 1.))
267 return;
268 } else /* check for match to standard size */
269 for (pp = p; pp->n[0]; pp++)
270 if (matchid(s, pp->n)) {
271 width = pp->w;
272 height = pp->h;
273 return;
274 }
275 fprintf(stderr, "%s: unknown paper size \"%s\" -- known sizes:\n",
276 progname, ps);
277 fprintf(stderr, "_Name________Width_Height_(inches)\n");
278 for (pp = p; pp->n[0]; pp++)
279 fprintf(stderr, "%-11s %5.2f %5.2f\n", pp->n, pp->w, pp->h);
280 fprintf(stderr, "Or use WWxHH size specification\n");
281 exit(1);
282 }
283
284
285 quiterr(err) /* print message and exit */
286 char *err;
287 {
288 if (err != NULL) {
289 fprintf(stderr, "%s: %s\n", progname, err);
290 exit(1);
291 }
292 exit(0);
293 }
294
295
296 PSheader(ac, av) /* print PostScript header */
297 int ac;
298 char **av;
299 {
300 char *rstr;
301 int landscape, rotate, n;
302 double pwidth, pheight;
303 double iwidth, iheight;
304 /* EPS comments */
305 puts("%!PS-Adobe-2.0 EPSF-2.0");
306 printf("%%%%Title: "); printargs(ac, av, stdout);
307 printf("%%%%Creator: %s\n", progname);
308 printf("%%%%Pages: %d\n", ncopies);
309 if ( (landscape = xmax > pixaspect*ymax) )
310 puts("%%Orientation: Landscape");
311 else
312 puts("%%Orientation: Portrait");
313 if ( (rotate = (PWIDTH > PHEIGHT) ^ landscape) ) {
314 pwidth = PHEIGHT;
315 pheight = PWIDTH;
316 } else {
317 pwidth = PWIDTH;
318 pheight = PHEIGHT;
319 }
320 if (dpi > 100 && (pixaspect >= 0.99) & (pixaspect <= 1.01))
321 if (pheight/pwidth > ymax/xmax) {
322 n = pwidth*dpi/xmax; /* floor */
323 iwidth = n > 0 ? (double)(n*xmax)/dpi : pwidth;
324 iheight = iwidth*ymax/xmax;
325 } else {
326 n = pheight*dpi/ymax; /* floor */
327 iheight = n > 0 ? (double)(n*ymax)/dpi : pheight;
328 iwidth = iheight*xmax/ymax;
329 }
330 else
331 if (pheight/pwidth > pixaspect*ymax/xmax) {
332 iwidth = pwidth;
333 iheight = iwidth*pixaspect*ymax/xmax;
334 } else {
335 iheight = pheight;
336 iwidth = iheight*xmax/(pixaspect*ymax);
337 }
338 if (rotate)
339 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
340 HMARGIN+(pheight-iheight)*.5,
341 VMARGIN+(pwidth-iwidth)*.5,
342 HMARGIN+(pheight-iheight)*.5+iheight,
343 VMARGIN+(pwidth-iwidth)*.5+iwidth);
344 else
345 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
346 HMARGIN+(pwidth-iwidth)*.5,
347 VMARGIN+(pheight-iheight)*.5,
348 HMARGIN+(pwidth-iwidth)*.5+iwidth,
349 VMARGIN+(pheight-iheight)*.5+iheight);
350 puts("%%EndComments");
351 puts("gsave save");
352 puts("17 dict begin");
353 /* define image reader */
354 if (docolor) {
355 printf("/redline %d string def\n", xmax);
356 printf("/grnline %d string def\n", xmax);
357 printf("/bluline %d string def\n", xmax);
358 } else
359 printf("/gryline %d string def\n", xmax);
360 /* use compressed encoding? */
361 if (putprim == Cputprim)
362 PSprocdef("read6bitRLE");
363 /* set up transformation matrix */
364 printf("%f %f translate\n", HMARGIN, VMARGIN);
365 if (rotate) {
366 printf("%f 0 translate\n", PWIDTH);
367 puts("90 rotate");
368 }
369 printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5);
370 printf("%f %f scale\n", iwidth, iheight);
371 puts("%%EndProlog");
372 /* start image procedure */
373 printf("%d %d 8 [%d 0 0 %d 0 %d]\n", xmax, ymax, xmax, -ymax, ymax);
374 if (putprim == Cputprim) {
375 if (docolor) {
376 puts("{redline read6bitRLE}");
377 puts("{grnline read6bitRLE}");
378 puts("{bluline read6bitRLE}");
379 puts("true 3 colorimage");
380 } else
381 puts("{gryline read6bitRLE} image");
382 } else {
383 rstr = putprim==Aputprim ? "readhexstring" : "readstring";
384 if (docolor) {
385 printf("{currentfile redline %s pop}\n", rstr);
386 printf("{currentfile grnline %s pop}\n", rstr);
387 printf("{currentfile bluline %s pop}\n", rstr);
388 puts("true 3 colorimage");
389 } else
390 printf("{currentfile gryline %s pop} image\n", rstr);
391 }
392 }
393
394
395 PStrailer() /* print PostScript trailer */
396 {
397 puts("%%Trailer");
398 if (ncopies > 1)
399 printf("/#copies %d def\n", ncopies);
400 puts("showpage");
401 puts("end");
402 puts("restore grestore");
403 puts("%%EOF");
404 }
405
406
407 PSprocdef(nam) /* define PS procedure to read image */
408 char *nam;
409 {
410 short itab[128];
411 register int i;
412 /* assign code values */
413 for (i = 0; i < 128; i++) /* clear */
414 itab[i] = -1;
415 for (i = 1; i < 63; i++) /* assign greys */
416 itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam);
417 itab[code[0]] = 0; /* black is black */
418 itab[code[63]] = 255; /* and white is white */
419 printf("/codetab [");
420 for (i = 0; i < 128; i++) {
421 if (!(i & 0xf))
422 putchar('\n');
423 printf(" %3d", itab[i]);
424 }
425 printf("\n] def\n");
426 printf("/nrept 0 def\n");
427 printf("/readbyte { currentfile read not {stop} if } bind def\n");
428 printf("/decode { codetab exch get } bind def\n");
429 printf("/%s {\t%% scanbuffer\n", nam);
430 printf("\t/scanline exch def\n");
431 printf("\t{ 0 1 %d { scanline exch\n", xmax-1);
432 printf("\t\tnrept 0 le\n");
433 printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR);
434 printf("\t\t\t\t\t{ pop /nrept readbyte %d sub def\n", RSTRT-MINRUN+1);
435 printf("\t\t\t\t\t\t/reptv readbyte decode def\n");
436 printf("\t\t\t\t\t\treptv exit }\n");
437 printf("\t\t\t\t\t{ decode dup 0 lt {pop} {exit} ifelse }\n");
438 printf("\t\t\t\tifelse } loop }\n");
439 printf("\t\t\t{ /nrept nrept 1 sub def reptv }\n");
440 printf("\t\tifelse put\n");
441 printf("\t\t} for\n");
442 printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n");
443 printf("} bind def\n");
444 }
445
446
447 ra2ps() /* convert Radiance scanlines to 6-bit */
448 {
449 register COLR *scanin;
450 int y;
451 /* allocate scanline */
452 scanin = (COLR *)malloc(xmax*sizeof(COLR));
453 if (scanin == NULL)
454 quiterr("out of memory in ra2ps");
455 /* convert image */
456 for (y = ymax-1; y >= 0; y--) {
457 if (freadcolrs(scanin, xmax, stdin) < 0)
458 quiterr("error reading Radiance picture");
459 if (putprim == Cputprim || devgam != 1.) {
460 if (bradj) /* adjust exposure */
461 shiftcolrs(scanin, xmax, bradj);
462 colrs_gambs(scanin, xmax); /* gamma compression */
463 } else
464 normcolrs(scanin, xmax, bradj);
465 if (docolor) {
466 (*putprim)(scanin, RED);
467 (*putprim)(scanin, GRN);
468 (*putprim)(scanin, BLU);
469 } else
470 (*putprim)(scanin, GRY);
471 if (ferror(stdout))
472 quiterr("error writing PostScript file");
473 }
474 putchar('\n');
475 /* free scanline */
476 free((void *)scanin);
477 }
478
479
480 int
481 Aputprim(scn, pri) /* put out hex ASCII primary from scanline */
482 COLR *scn;
483 int pri;
484 {
485 static char hexdigit[] = "0123456789ABCDEF";
486 static int col = 0;
487 register int x, c;
488
489 for (x = 0; x < xmax; x++) {
490 if (pri == GRY)
491 c = normbright(scn[x]);
492 else
493 c = scn[x][pri];
494 if (c > 255) c = 255;
495 putchar(hexdigit[c>>4]);
496 putchar(hexdigit[c&0xf]);
497 if ((col += 2) >= 72) {
498 putchar('\n');
499 col = 0;
500 }
501 }
502 }
503
504
505 int
506 Bputprim(scn, pri) /* put out binary primary from scanline */
507 COLR *scn;
508 int pri;
509 {
510 register int x, c;
511
512 for (x = 0; x < xmax; x++) {
513 if (pri == GRY)
514 c = normbright(scn[x]);
515 else
516 c = scn[x][pri];
517 if (c > 255) c = 255;
518 putchar(c);
519 }
520 }
521
522
523 int
524 Cputprim(scn, pri) /* put out compressed primary from scanline */
525 COLR *scn;
526 int pri;
527 {
528 register int c;
529 register int x;
530 int lastc, cnt;
531
532 lastc = -1; cnt = 0;
533 for (x = 0; x < xmax; x++) {
534 if (pri == GRY)
535 c = normbright(scn[x]) + 2;
536 else
537 c = scn[x][pri] + 2;
538 if (c > 255) c = 255;
539 c = code[c>>2];
540 if (c == lastc && cnt < MAXRUN)
541 cnt++;
542 else {
543 putrle(cnt, lastc);
544 lastc = c;
545 cnt = 1;
546 }
547 }
548 putrle(cnt, lastc);
549 }
550
551
552 putrle(cnt, cod) /* put out cnt of cod */
553 register int cnt, cod;
554 {
555 static int col = 0;
556
557 if (cnt >= MINRUN) {
558 col += 3;
559 putchar(RUNCHR);
560 putchar(RSTRT-MINRUN+cnt);
561 putchar(cod);
562 } else {
563 col += cnt;
564 while (cnt-- > 0)
565 putchar(cod);
566 }
567 if (col >= 72) {
568 putchar('\n');
569 col = 0;
570 }
571 }