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 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

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