ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.33
Committed: Fri Oct 4 01:53:45 2024 UTC (7 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.32: +11 -8 lines
Log Message:
feat(ra_ps): Added conversion of hyperspectral pictures on input

File Contents

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