ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
(Generate patch)

Comparing ray/src/px/ra_ps.c (file contents):
Revision 2.14 by greg, Wed Oct 4 16:01:32 1995 UTC vs.
Revision 2.28 by schorsch, Sun Mar 28 20:33:14 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  Radiance picture to PostScript file translator -- one way!
6   */
7  
8   #include  <stdio.h>
9 + #include  <string.h>
10   #include  <math.h>
11 < #ifdef MSDOS
12 < #include  <fcntl.h>
13 < #endif
11 > #include  <ctype.h>
12 >
13 > #include  "platform.h"
14   #include  "color.h"
15 + #include  "resolu.h"
16  
17 < #define GAMVAL          1.5                     /* gamma value for pixels */
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 HMARGIN         (.5*72)                 /* horizontal margin */
26 < #define VMARGIN         (.5*72)                 /* vertical margin */
27 < #define PWIDTH          (8.5*72-2*HMARGIN)      /* width of device */
28 < #define PHEIGHT         (11*72-2*VMARGIN)       /* height of device */
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 */
# Line 35 | Line 41 | char  code[] =                 /* 6-bit code lookup table */
41   int  wrongformat = 0;                   /* input in wrong format? */
42   double  pixaspect = 1.0;                /* pixel aspect ratio */
43  
44 < int  docolor = 0;                       /* produce color image? */
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 < int  xmax, ymax;
57 > typedef void putprimf_t(COLR *scn, int pri);
58  
59 < extern char  *malloc();
59 > static gethfunc headline;
60 > static putprimf_t Aputprim, Bputprim, Cputprim;
61  
62 + static double unit2inch(register 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 < headline(s)             /* check header line */
73 < char  *s;
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[32];
83  
# Line 56 | Line 86 | char  *s;
86                  wrongformat = strcmp(fmt, COLRFMT);
87          } else if (isaspect(s))
88                  pixaspect *= aspectval(s);
89 +        return(0);
90   }
91  
92 <
93 < main(argc, argv)
63 < int  argc;
64 < char  *argv[];
92 > int
93 > main(int  argc, char  *argv[])
94   {
95          int  i;
96 +        double  d;
97          
98          progname = argv[0];
99  
100          for (i = 1; i < argc; i++)
101                  if (argv[i][0] == '-')
102                          switch (argv[i][1]) {
103 +                        case 'b':               /* produce b&w PostScript */
104 +                                docolor = 0;
105 +                                break;
106                          case 'c':               /* produce color PostScript */
107 <                                docolor++;
107 >                                docolor = 1;
108                                  break;
109 +                        case 'A':               /* standard ASCII encoding */
110 +                                putprim = Aputprim;
111 +                                break;
112 +                        case 'B':               /* standard binary encoding */
113 +                                putprim = Bputprim;
114 +                                break;
115 +                        case 'C':               /* compressed ASCII encoding */
116 +                                putprim = Cputprim;
117 +                                break;
118 +                        case 'd':               /* print density */
119 +                                dpi = atof(argv[++i]);
120 +                                break;
121 +                        case 'g':               /* device gamma adjustment */
122 +                                devgam = atof(argv[++i]);
123 +                                break;
124 +                        case 'p':               /* paper size */
125 +                                parsepaper(argv[++i]);
126 +                                break;
127 +                        case 'm':               /* margin */
128 +                                d = atof(argv[i+1]);
129 +                                d *= unit2inch(argv[i+1]);
130 +                                switch (argv[i][2]) {
131 +                                case '\0':
132 +                                        hmarg = vmarg = d;
133 +                                        break;
134 +                                case 'h':
135 +                                        hmarg = d;
136 +                                        break;
137 +                                case 'v':
138 +                                        vmarg = d;
139 +                                        break;
140 +                                default:
141 +                                        goto userr;
142 +                                }
143 +                                i++;
144 +                                break;
145                          case 'e':               /* exposure adjustment */
146                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
147                                          goto userr;
# Line 99 | Line 168 | char  *argv[];
168                                  progname, argv[i+1]);
169                  exit(1);
170          }
171 < #ifdef MSDOS
103 <        setmode(fileno(stdin), O_BINARY);
104 < #endif
171 >        SET_FILE_BINARY(stdin);
172                                  /* get our header */
173          getheader(stdin, headline, NULL);
174          if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
175                  quiterr("bad picture format");
176                                  /* gamma compression */
177 <        setcolrgam(GAMVAL);
177 >        if (devgam <= 0.05)
178 >                devgam = docolor ? DEFCGAM : DEFGGAM;
179 >        if (putprim == Cputprim)
180 >                setcolrgam(CODE6GAM);
181 >        else if (devgam != 1.)
182 >                setcolrgam(devgam);
183                                  /* write header */
184 <        PSheader(i <= argc-1 ? argv[i] : "<stdin>");
184 >        PSheader(argc, argv);
185                                  /* convert file */
186          ra2ps();
187                                  /* write trailer */
188          PStrailer();
189          exit(0);
190   userr:
191 <        fprintf(stderr, "Usage: %s [-c][-e +/-stops] [input [output]]\n",
191 >        fprintf(stderr,
192 > "Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-d dpi][-g gamma] [input [output]]\n",
193                          progname);
194          exit(1);
195   }
196  
197  
198 < quiterr(err)            /* print message and exit */
199 < char  *err;
198 > static double
199 > unit2inch(              /* determine unit */
200 >        register char   *s
201 > )
202   {
203 +        static struct unit {char n; float f;} u[] = {
204 +                {'i', 1.},
205 +                {'m', 1./25.4},
206 +                {'c', 1./2.54},
207 +                {'\0',0} };
208 +        register struct unit    *up;
209 +
210 +        while (*s && !isalpha(*s))
211 +                s++;
212 +        for (up = u; up->n; up++)
213 +                if (up->n == *s)
214 +                        return(up->f);
215 +        return(1.);
216 + }
217 +
218 +
219 + static int
220 + matchid(        /* see if name matches id (case insensitive) */
221 +        char    *name,
222 +        register char   *id
223 + )
224 + {
225 +        register char   *s = name;
226 +
227 +        while (*s) {
228 +                if (isalpha(*s)) {
229 +                        if (!isalpha(*id) || UPPER(*s) != UPPER(*id))
230 +                                return(0);
231 +                } else if (*s != *id)
232 +                        return(0);
233 +                s++; id++;
234 +        }
235 +        return(!*id || s-name >= 3);    /* substrings >= 3 chars OK */
236 + }
237 +
238 +
239 + static void
240 + parsepaper(             /* determine paper size from name */
241 +        char    *ps
242 + )
243 + {
244 +        static struct psize {char n[12]; float w,h;} p[] = {
245 +                {"envelope", 4.12, 9.5},
246 +                {"executive", 7.25, 10.5},
247 +                {"letter", 8.5, 11.},
248 +                {"lettersmall", 7.68, 10.16},
249 +                {"legal", 8.5, 14.},
250 +                {"monarch", 3.87, 7.5},
251 +                {"statement", 5.5, 8.5},
252 +                {"tabloid", 11., 17.},
253 +                {"A3", 11.69, 16.54},
254 +                {"A4", 8.27, 11.69},
255 +                {"A4small", 7.47, 10.85},
256 +                {"A5", 6.00, 8.27},
257 +                {"A6", 4.13, 6.00},
258 +                {"B4", 10.12, 14.33},
259 +                {"B5", 7.17, 10.12},
260 +                {"C5", 6.38, 9.01},
261 +                {"C6", 4.49, 6.38},
262 +                {"DL", 4.33, 8.66},
263 +                {"hagaki", 3.94, 5.83},
264 +                {"",0.0,0.0} };
265 +        register struct psize   *pp;
266 +        register char   *s = ps;
267 +        double  d;
268 +
269 +        if (isdigit(*s)) {              /* check for WWxHH specification */
270 +                width = atof(s);
271 +                while (*s && !isalpha(*s))
272 +                        s++;
273 +                d = unit2inch(s);
274 +                height = atof(++s);
275 +                width *= d;
276 +                height *= d;
277 +                if ((width >= 1.) & (height >= 1.))
278 +                        return;
279 +        } else                          /* check for match to standard size */
280 +                for (pp = p; pp->n[0]; pp++)
281 +                        if (matchid(s, pp->n)) {
282 +                                width = pp->w;
283 +                                height = pp->h;
284 +                                return;
285 +                        }
286 +        fprintf(stderr, "%s: unknown paper size \"%s\" -- known sizes:\n",
287 +                        progname, ps);
288 +        fprintf(stderr, "_Name________Width_Height_(inches)\n");
289 +        for (pp = p; pp->n[0]; pp++)
290 +                fprintf(stderr, "%-11s  %5.2f  %5.2f\n", pp->n, pp->w, pp->h);
291 +        fprintf(stderr, "Or use WWxHH size specification\n");
292 +        exit(1);
293 + }
294 +
295 +
296 + static void
297 + quiterr(                /* print message and exit */
298 +        char  *err
299 + )
300 + {
301          if (err != NULL) {
302                  fprintf(stderr, "%s: %s\n", progname, err);
303                  exit(1);
# Line 133 | Line 306 | char  *err;
306   }
307  
308  
309 < PSheader(name)                  /* print PostScript header */
310 < char  *name;
309 > static void
310 > PSheader(               /* print PostScript header */
311 >        int  ac,
312 >        char  **av
313 > )
314   {
315 <        int  landscape = 0;
315 >        char  *rstr;
316 >        int  landscape, rotate, n;
317          double  pwidth, pheight;
318          double  iwidth, iheight;
319                                          /* EPS comments */
320          puts("%!PS-Adobe-2.0 EPSF-2.0");
321 <        printf("%%%%Title: %s\n", name);
322 <        printf("%%%%Creator: %s = %s\n", progname, SCCSid);
321 >        printf("%%%%Title: "); printargs(ac, av, stdout);
322 >        printf("%%%%Creator: %s\n", progname);
323          printf("%%%%Pages: %d\n", ncopies);
324 <        if (landscape = xmax > pixaspect*ymax)
324 >        if ( (landscape = xmax > pixaspect*ymax) )
325                  puts("%%Orientation: Landscape");
326          else
327                  puts("%%Orientation: Portrait");
328 <        if (PWIDTH > PHEIGHT ^ landscape) {
328 >        if ( (rotate = (PWIDTH > PHEIGHT) ^ landscape) ) {
329                  pwidth = PHEIGHT;
330                  pheight = PWIDTH;
331          } else {
332                  pwidth = PWIDTH;
333                  pheight = PHEIGHT;
334          }
335 <        if (pheight/pwidth > pixaspect*ymax/xmax) {
336 <                iwidth = pwidth;
337 <                iheight = pwidth*pixaspect*ymax/xmax;
338 <        } else {
339 <                iheight = pheight;
340 <                iwidth = pheight*xmax/(pixaspect*ymax);
341 <        }
342 <        if (pwidth == PHEIGHT)
335 >        if (dpi > 100 && (pixaspect >= 0.99) & (pixaspect <= 1.01))
336 >                if (pheight/pwidth > ymax/xmax) {
337 >                        n = pwidth*dpi/xmax;    /* floor */
338 >                        iwidth = n > 0 ? (double)(n*xmax)/dpi : pwidth;
339 >                        iheight = iwidth*ymax/xmax;
340 >                } else {
341 >                        n = pheight*dpi/ymax;   /* floor */
342 >                        iheight = n > 0 ? (double)(n*ymax)/dpi : pheight;
343 >                        iwidth = iheight*xmax/ymax;
344 >                }
345 >        else
346 >                if (pheight/pwidth > pixaspect*ymax/xmax) {
347 >                        iwidth = pwidth;
348 >                        iheight = iwidth*pixaspect*ymax/xmax;
349 >                } else {
350 >                        iheight = pheight;
351 >                        iwidth = iheight*xmax/(pixaspect*ymax);
352 >                }
353 >        if (rotate)
354                  printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
355                                  HMARGIN+(pheight-iheight)*.5,
356                                  VMARGIN+(pwidth-iwidth)*.5,
# Line 176 | Line 364 | char  *name;
364                                  VMARGIN+(pheight-iheight)*.5+iheight);
365          puts("%%EndComments");
366          puts("gsave save");
367 <        puts("64 dict begin");
367 >        puts("17 dict begin");
368                                          /* define image reader */
369          if (docolor) {
370                  printf("/redline %d string def\n", xmax);
371                  printf("/grnline %d string def\n", xmax);
372                  printf("/bluline %d string def\n", xmax);
373 <                printf("/rgbline %d string def\n", 3*xmax);
374 <                PSprocdef("read6bitRLE", "interleave");
375 <        } else {
376 <                printf("/greyline %d string def\n", xmax);
377 <                PSprocdef("read6bitRLE", NULL);
190 <        }
373 >        } else
374 >                printf("/gryline %d string def\n", xmax);
375 >                                        /* use compressed encoding? */
376 >        if (putprim == Cputprim)
377 >                PSprocdef("read6bitRLE");
378                                          /* set up transformation matrix */
379          printf("%f %f translate\n", HMARGIN, VMARGIN);
380 <        if (pwidth == PHEIGHT) {
380 >        if (rotate) {
381                  printf("%f 0 translate\n", PWIDTH);
382                  puts("90 rotate");
383          }
# Line 198 | Line 385 | char  *name;
385          printf("%f %f scale\n", iwidth, iheight);
386          puts("%%EndProlog");
387                                          /* start image procedure */
388 <        printf("%d %d 8 [%d 0 0 %d 0 %d] ", xmax, ymax, xmax, -ymax, ymax);
389 <        if (docolor) {
390 <                puts("{redline read6bitRLE grnline read6bitRLE");
391 <                puts("\tbluline read6bitRLE rgbline interleave} false 3 colorimage");
392 <        } else
393 <                puts("{greyline read6bitRLE} image");
388 >        printf("%d %d 8 [%d 0 0 %d 0 %d]\n", xmax, ymax, xmax, -ymax, ymax);
389 >        if (putprim == Cputprim) {
390 >                if (docolor) {
391 >                        puts("{redline read6bitRLE}");
392 >                        puts("{grnline read6bitRLE}");
393 >                        puts("{bluline read6bitRLE}");
394 >                        puts("true 3 colorimage");
395 >                } else
396 >                        puts("{gryline read6bitRLE} image");
397 >        } else {
398 >                rstr = putprim==Aputprim ? "readhexstring" : "readstring";
399 >                if (docolor) {
400 >                        printf("{currentfile redline %s pop}\n", rstr);
401 >                        printf("{currentfile grnline %s pop}\n", rstr);
402 >                        printf("{currentfile bluline %s pop}\n", rstr);
403 >                        puts("true 3 colorimage");
404 >                } else
405 >                        printf("{currentfile gryline %s pop} image\n", rstr);
406 >        }
407   }
408  
409  
410 < PStrailer()                     /* print PostScript trailer */
410 > static void
411 > PStrailer(void)                 /* print PostScript trailer */
412   {
413          puts("%%Trailer");
414          if (ncopies > 1)
# Line 219 | Line 420 | PStrailer()                    /* print PostScript trailer */
420   }
421  
422  
423 < PSprocdef(nam, inam)            /* define PS procedure to read image */
424 < char  *nam, *inam;
423 > static void
424 > PSprocdef(                      /* define PS procedure to read image */
425 >        char  *nam
426 > )
427   {
428          short  itab[128];
429          register int  i;
# Line 228 | Line 431 | char  *nam, *inam;
431          for (i = 0; i < 128; i++)       /* clear */
432                  itab[i] = -1;
433          for (i = 1; i < 63; i++)        /* assign greys */
434 <                itab[code[i]] = 256.0*pow(((i<<2)+2.5)/256.0, GAMVAL);
435 <        itab[code[0]] = 0;              /* black is black */
436 <        itab[code[63]] = 255;           /* and white is white */
434 >                itab[(int)code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam);
435 >        itab[(int)code[0]] = 0;         /* black is black */
436 >        itab[(int)code[63]] = 255;              /* and white is white */
437          printf("/codetab [");
438          for (i = 0; i < 128; i++) {
439                  if (!(i & 0xf))
# Line 256 | Line 459 | char  *nam, *inam;
459          printf("\t\t} for\n");
460          printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n");
461          printf("} bind def\n");
259        if (inam == NULL)
260                return;
261                                        /* define interleaving procedure */
262        printf("/%s {\t%% redscn grnscn bluscn rgbscn\n", inam);
263        printf("\t/rgbscan exch def /bscan exch def\n");
264        printf("\t/gscan exch def /rscan exch def\n");
265        printf("\trscan length %d eq gscan length %d eq and ", xmax, xmax);
266        printf("bscan length %d eq and \n", xmax);
267        printf("\t{ 0 1 %d { /ndx exch def\n", xmax-1);
268        printf("\t\trgbscan ndx 3 mul rscan ndx get put\n");
269        printf("\t\trgbscan ndx 3 mul 1 add gscan ndx get put\n");
270        printf("\t\trgbscan ndx 3 mul 2 add bscan ndx get put\n");
271        printf("\t\t} for rgbscan }\n");
272        printf("\t{0 string} ifelse\n");
273        printf("} bind def\n");
462   }
463  
464  
465 < ra2ps()                         /* convert Radiance scanlines to 6-bit */
465 > static void
466 > ra2ps(void)                             /* convert Radiance scanlines to 6-bit */
467   {
468          register COLR   *scanin;
469          int     y;
# Line 286 | Line 475 | ra2ps()                                /* convert Radiance scanlines to 6-bit */
475          for (y = ymax-1; y >= 0; y--) {
476                  if (freadcolrs(scanin, xmax, stdin) < 0)
477                          quiterr("error reading Radiance picture");
478 <                if (bradj)                      /* adjust exposure */
479 <                        shiftcolrs(scanin, xmax, bradj);
480 <                colrs_gambs(scanin, xmax);      /* gamma compression */
478 >                if (putprim == Cputprim || devgam != 1.) {
479 >                        if (bradj)                      /* adjust exposure */
480 >                                shiftcolrs(scanin, xmax, bradj);
481 >                        colrs_gambs(scanin, xmax);      /* gamma compression */
482 >                } else
483 >                        normcolrs(scanin, xmax, bradj);
484                  if (docolor) {
485 <                        putprim(scanin, RED);
486 <                        putprim(scanin, GRN);
487 <                        putprim(scanin, BLU);
485 >                        (*putprim)(scanin, RED);
486 >                        (*putprim)(scanin, GRN);
487 >                        (*putprim)(scanin, BLU);
488                  } else
489 <                        putprim(scanin, GRY);
489 >                        (*putprim)(scanin, GRY);
490                  if (ferror(stdout))
491                          quiterr("error writing PostScript file");
492          }
493          putchar('\n');
494                                                  /* free scanline */
495 <        free((char *)scanin);
495 >        free((void *)scanin);
496   }
497  
498  
499 < putprim(scn, pri)               /* put out one primary from scanline */
500 < COLR    *scn;
501 < int     pri;
499 > static void
500 > Aputprim(               /* put out hex ASCII primary from scanline */
501 >        COLR    *scn,
502 >        int     pri
503 > )
504   {
505 +        static char     hexdigit[] = "0123456789ABCDEF";
506 +        static int      col = 0;
507 +        register int    x, c;
508 +
509 +        for (x = 0; x < xmax; x++) {
510 +                if (pri == GRY)
511 +                        c = normbright(scn[x]);
512 +                else
513 +                        c = scn[x][pri];
514 +                if (c > 255) c = 255;
515 +                putchar(hexdigit[c>>4]);
516 +                putchar(hexdigit[c&0xf]);
517 +                if ((col += 2) >= 72) {
518 +                        putchar('\n');
519 +                        col = 0;
520 +                }
521 +        }
522 + }
523 +
524 +
525 + static void
526 + Bputprim(               /* put out binary primary from scanline */
527 +        COLR    *scn,
528 +        int     pri
529 + )
530 + {
531 +        register int    x, c;
532 +
533 +        for (x = 0; x < xmax; x++) {
534 +                if (pri == GRY)
535 +                        c = normbright(scn[x]);
536 +                else
537 +                        c = scn[x][pri];
538 +                if (c > 255) c = 255;
539 +                putchar(c);
540 +        }
541 + }
542 +
543 +
544 + static void
545 + Cputprim(               /* put out compressed primary from scanline */
546 +        COLR    *scn,
547 +        int     pri
548 + )
549 + {
550          register int    c;
551          register int    x;
552          int     lastc, cnt;
# Line 332 | Line 571 | int    pri;
571   }
572  
573  
574 < putrle(cnt, cod)                /* put out cnt of cod */
575 < register int    cnt, cod;
574 > static void
575 > putrle(         /* put out cnt of cod */
576 >        register int    cnt,
577 >        register int    cod
578 > )
579   {
580          static int      col = 0;
581  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines