ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.34
Committed: Fri Jun 6 19:11:21 2025 UTC (2 weeks, 1 day ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.33: +2 -3 lines
Log Message:
refactor: Making use of printargs() more consistent with fixargv0()

File Contents

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