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 (3 weeks, 5 days 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_ps.c,v 2.33 2024/10/04 01:53:45 greg Exp $";
3 #endif
4 /*
5 * Radiance picture to PostScript file translator -- one way!
6 */
7
8 #include <math.h>
9 #include <ctype.h>
10
11 #include "platform.h"
12 #include "paths.h"
13 #include "rtio.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 int xmax, ymax; /* input image dimensions */
55
56 typedef void putprimf_t(COLR *scn, int pri);
57
58 static gethfunc headline;
59 static putprimf_t Aputprim, Bputprim, Cputprim;
60
61 static double unit2inch(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
71
72 putprimf_t *putprim = Aputprim; /* function for writing scanline */
73
74
75 static int
76 headline( /* check header line */
77 char *s,
78 void *p
79 )
80 {
81 char fmt[MAXFMTLEN];
82
83 if (formatval(fmt, s))
84 wrongformat = strcmp(fmt, COLRFMT) && strcmp(fmt, SPECFMT);
85 else if (isaspect(s))
86 pixaspect *= aspectval(s);
87 else if (isncomp(s))
88 NCSAMP = ncompval(s);
89 else if (iswlsplit(s))
90 wlsplitval(WLPART, s);
91 return(0);
92 }
93
94 int
95 main(int argc, char *argv[])
96 {
97 int i;
98 double d;
99
100 fixargv0(argv[0]); /* assigns progname */
101
102 for (i = 1; i < argc; i++)
103 if (argv[i][0] == '-')
104 switch (argv[i][1]) {
105 case 'b': /* produce b&w PostScript */
106 docolor = 0;
107 break;
108 case 'c': /* produce color PostScript */
109 docolor = 1;
110 break;
111 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 case 'd': /* print density */
121 dpi = atof(argv[++i]);
122 break;
123 case 'g': /* device gamma adjustment */
124 devgam = atof(argv[++i]);
125 break;
126 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 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 case 'n': /* number of copies */
153 ncopies = atoi(argv[++i]);
154 break;
155 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 fprintf(stderr, "%s: can't open output \"%s\"\n",
170 progname, argv[i+1]);
171 exit(1);
172 }
173 SET_FILE_BINARY(stdin);
174 /* get our header */
175 getheader(stdin, headline, NULL);
176 if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
177 quiterr("bad picture format");
178 /* gamma compression */
179 if (devgam <= 0.05)
180 devgam = docolor ? DEFCGAM : DEFGGAM;
181 if (putprim == Cputprim)
182 setcolrgam(CODE6GAM);
183 else if (devgam != 1.)
184 setcolrgam(devgam);
185 /* write header */
186 PSheader(argc, argv);
187 /* convert file */
188 ra2ps();
189 /* write trailer */
190 PStrailer();
191 exit(0);
192 userr:
193 fprintf(stderr,
194 "Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-d dpi][-g gamma] [input [output]]\n",
195 progname);
196 exit(1);
197 }
198
199
200 static double
201 unit2inch( /* determine unit */
202 char *s
203 )
204 {
205 static struct unit {char n; float f;} u[] = {
206 {'i', 1.},
207 {'m', 1./25.4},
208 {'c', 1./2.54},
209 {'\0',0} };
210 struct unit *up;
211
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 static int
222 matchid( /* see if name matches id (case insensitive) */
223 char *name,
224 char *id
225 )
226 {
227 char *s = name;
228
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 static void
242 parsepaper( /* determine paper size from name */
243 char *ps
244 )
245 {
246 static struct psize {char n[12]; float w,h;} p[] = {
247 {"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 struct psize *pp;
268 char *s = ps;
269 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 if ((width >= 1.) & (height >= 1.))
280 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 fprintf(stderr, "Or use WWxHH size specification\n");
294 exit(1);
295 }
296
297
298 static void
299 quiterr( /* print message and exit */
300 char *err
301 )
302 {
303 if (err != NULL) {
304 fprintf(stderr, "%s: %s\n", progname, err);
305 exit(1);
306 }
307 exit(0);
308 }
309
310
311 static void
312 PSheader( /* print PostScript header */
313 int ac,
314 char **av
315 )
316 {
317 char *rstr;
318 int landscape, rotate, n;
319 double pwidth, pheight;
320 double iwidth, iheight;
321 /* EPS comments */
322 puts("%!PS-Adobe-2.0 EPSF-2.0");
323 printf("%%%%Title: "); printargs(ac, av, stdout);
324 printf("%%%%Creator: %s\n", progname);
325 printf("%%%%Pages: %d\n", ncopies);
326 if ( (landscape = xmax > pixaspect*ymax) )
327 puts("%%Orientation: Landscape");
328 else
329 puts("%%Orientation: Portrait");
330 if ( (rotate = (PWIDTH > PHEIGHT) ^ landscape) ) {
331 pwidth = PHEIGHT;
332 pheight = PWIDTH;
333 } else {
334 pwidth = PWIDTH;
335 pheight = PHEIGHT;
336 }
337 if (dpi > 100 && (pixaspect >= 0.99) & (pixaspect <= 1.01))
338 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 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 puts("gsave save");
369 puts("17 dict begin");
370 /* define image reader */
371 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 } else
376 printf("/gryline %d string def\n", xmax);
377 /* use compressed encoding? */
378 if (putprim == Cputprim)
379 PSprocdef("read6bitRLE");
380 /* set up transformation matrix */
381 printf("%f %f translate\n", HMARGIN, VMARGIN);
382 if (rotate) {
383 printf("%f 0 translate\n", PWIDTH);
384 puts("90 rotate");
385 }
386 printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5);
387 printf("%f %f scale\n", iwidth, iheight);
388 puts("%%EndProlog");
389 /* start image procedure */
390 printf("%d %d 8 [%d 0 0 %d 0 %d]\n", xmax, ymax, xmax, -ymax, ymax);
391 if (putprim == Cputprim) {
392 if (docolor) {
393 puts("{redline read6bitRLE}");
394 puts("{grnline read6bitRLE}");
395 puts("{bluline read6bitRLE}");
396 puts("true 3 colorimage");
397 } 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 }
410
411
412 static void
413 PStrailer(void) /* print PostScript trailer */
414 {
415 puts("%%Trailer");
416 if (ncopies > 1)
417 printf("/#copies %d def\n", ncopies);
418 puts("showpage");
419 puts("end");
420 puts("restore grestore");
421 puts("%%EOF");
422 }
423
424
425 static void
426 PSprocdef( /* define PS procedure to read image */
427 char *nam
428 )
429 {
430 short itab[128];
431 int i;
432 /* assign code values */
433 for (i = 0; i < 128; i++) /* clear */
434 itab[i] = -1;
435 for (i = 1; i < 63; i++) /* assign greys */
436 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 printf("/codetab [");
440 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 printf("/nrept 0 def\n");
447 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 printf("\t{ 0 1 %d { scanline exch\n", xmax-1);
452 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 printf("} bind def\n");
464 }
465
466
467 static void
468 ra2ps(void) /* convert Radiance scanlines to 6-bit */
469 {
470 COLR *scanin;
471 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 if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0)
479 quiterr("error reading Radiance picture");
480 if ((putprim == Cputprim) | (devgam != 1.)) {
481 if (bradj) /* adjust exposure */
482 shiftcolrs(scanin, xmax, bradj);
483 colrs_gambs(scanin, xmax); /* gamma compression */
484 } else
485 normcolrs(scanin, xmax, bradj);
486 if (docolor) {
487 (*putprim)(scanin, RED);
488 (*putprim)(scanin, GRN);
489 (*putprim)(scanin, BLU);
490 } else
491 (*putprim)(scanin, GRY);
492 if (ferror(stdout))
493 quiterr("error writing PostScript file");
494 }
495 putchar('\n');
496 /* free scanline */
497 free((void *)scanin);
498 }
499
500
501 static void
502 Aputprim( /* put out hex ASCII primary from scanline */
503 COLR *scn,
504 int pri
505 )
506 {
507 static char hexdigit[] = "0123456789ABCDEF";
508 static int col = 0;
509 int x, c;
510
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 static void
528 Bputprim( /* put out binary primary from scanline */
529 COLR *scn,
530 int pri
531 )
532 {
533 int x, c;
534
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 static void
547 Cputprim( /* put out compressed primary from scanline */
548 COLR *scn,
549 int pri
550 )
551 {
552 int c;
553 int x;
554 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 }
574
575
576 static void
577 putrle( /* put out cnt of cod */
578 int cnt,
579 int cod
580 )
581 {
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 }