ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.31
Committed: Sat Dec 28 18:05:14 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +1 -3 lines
Log Message:
Removed redundant include files

File Contents

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