ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.28
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad5R1, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1
Changes since 2.27: +71 -46 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

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