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.12 by greg, Thu Sep 28 16:34:30 1995 UTC vs.
Revision 2.33 by greg, Fri Oct 4 01:53:45 2024 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 < #ifdef MSDOS
10 < #include  <fcntl.h>
11 < #endif
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 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 32 | 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(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];
82 >        char  fmt[MAXFMTLEN];
83  
84 <        if (isformat(s)) {
85 <                formatval(fmt, s);
86 <                wrongformat = strcmp(fmt, COLRFMT);
54 <        } else if (isaspect(s))
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 <
96 < main(argc, argv)
60 < int  argc;
61 < char  *argv[];
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++;
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;
# Line 96 | Line 171 | char  *argv[];
171                                  progname, argv[i+1]);
172                  exit(1);
173          }
174 < #ifdef MSDOS
100 <        setmode(fileno(stdin), O_BINARY);
101 < #endif
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(i <= argc-1 ? argv[i] : "<stdin>");
187 >        PSheader(argc, argv);
188                                  /* convert file */
189          ra2ps();
190                                  /* write trailer */
191          PStrailer();
192          exit(0);
193   userr:
194 <        fprintf(stderr, "Usage: %s [-c][-e +/-stops] [input [output]]\n",
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 < quiterr(err)            /* print message and exit */
202 < char  *err;
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);
# Line 128 | Line 309 | char  *err;
309   }
310  
311  
312 < PSheader(name)                  /* print PostScript header */
313 < char  *name;
312 > static void
313 > PSheader(               /* print PostScript header */
314 >        int  ac,
315 >        char  **av
316 > )
317   {
318 <        int  landscape = 0;
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: %s\n", name);
325 <        printf("%%%%Creator: %s = %s\n", progname, SCCSid);
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)
327 >        if ( (landscape = xmax > pixaspect*ymax) )
328                  puts("%%Orientation: Landscape");
329          else
330                  puts("%%Orientation: Portrait");
331 <        if (PWIDTH > PHEIGHT ^ landscape) {
331 >        if ( (rotate = (PWIDTH > PHEIGHT) ^ landscape) ) {
332                  pwidth = PHEIGHT;
333                  pheight = PWIDTH;
334          } else {
335                  pwidth = PWIDTH;
336                  pheight = PHEIGHT;
337          }
338 <        if (pheight/pwidth > pixaspect*ymax/xmax) {
339 <                iwidth = pwidth;
340 <                iheight = pwidth*pixaspect*ymax/xmax;
341 <        } else {
342 <                iheight = pheight;
343 <                iwidth = pheight*xmax/(pixaspect*ymax);
344 <        }
345 <        if (pwidth == PHEIGHT)
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,
# Line 170 | Line 366 | char  *name;
366                                  HMARGIN+(pwidth-iwidth)*.5+iwidth,
367                                  VMARGIN+(pheight-iheight)*.5+iheight);
368          puts("%%EndComments");
369 <        puts("save");
370 <        puts("64 dict begin");
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 <                printf("/rgbline %d string def\n", 3*xmax);
377 <                PSprocdef("read6bitRLE", "interleave");
378 <        } else {
379 <                printf("/greyline %d string def\n", xmax);
380 <                PSprocdef("read6bitRLE", NULL);
185 <        }
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 (pwidth == PHEIGHT) {
384 <                printf("0 %f translate\n", PHEIGHT);
385 <                puts("-90 rotate");
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] ", xmax, ymax, xmax, -ymax, ymax);
392 <        if (docolor) {
393 <                puts("{redline read6bitRLE grnline read6bitRLE");
394 <                puts("\tbluline read6bitRLE rgbline interleave} false 3 colorimage");
395 <        } else
396 <                puts("{greyline read6bitRLE} image");
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 < PStrailer()                     /* print PostScript trailer */
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");
421 >        puts("restore grestore");
422          puts("%%EOF");
423   }
424  
425  
426 < PSprocdef(nam, inam)            /* define PS procedure to read image */
427 < char  *nam, *inam;
426 > static void
427 > PSprocdef(                      /* define PS procedure to read image */
428 >        char  *nam
429 > )
430   {
431          short  itab[128];
432 <        register int  i;
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[code[i]] = i<<2 | 2;
438 <        itab[code[0]] = 0;              /* black is black */
439 <        itab[code[63]] = 255;           /* and white is white */
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))
# Line 251 | Line 462 | char  *nam, *inam;
462          printf("\t\t} for\n");
463          printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n");
464          printf("} bind def\n");
254        if (inam == NULL)
255                return;
256                                        /* define interleaving procedure */
257        printf("/%s {\t%% redscn grnscn bluscn rgbscn\n", inam);
258        printf("\t/rgbscan exch def /bscan exch def\n");
259        printf("\t/gscan exch def /rscan exch def\n");
260        printf("\trscan length %d eq gscan length %d eq and ", xmax, xmax);
261        printf("bscan length %d eq and \n", xmax);
262        printf("\t{ 0 1 %d { /ndx exch def\n", xmax-1);
263        printf("\t\trgbscan ndx 3 mul rscan ndx get put\n");
264        printf("\t\trgbscan ndx 3 mul 1 add gscan ndx get put\n");
265        printf("\t\trgbscan ndx 3 mul 2 add bscan ndx get put\n");
266        printf("\t\t} for rgbscan }\n");
267        printf("\t{0 string} ifelse\n");
268        printf("} bind def\n");
465   }
466  
467  
468 < ra2ps()                         /* convert Radiance scanlines to 6-bit */
468 > static void
469 > ra2ps(void)                             /* convert Radiance scanlines to 6-bit */
470   {
471 <        register COLR   *scanin;
471 >        COLR    *scanin;
472          int     y;
473                                                  /* allocate scanline */
474          scanin = (COLR *)malloc(xmax*sizeof(COLR));
# Line 279 | Line 476 | ra2ps()                                /* convert Radiance scanlines to 6-bit */
476                  quiterr("out of memory in ra2ps");
477                                                  /* convert image */
478          for (y = ymax-1; y >= 0; y--) {
479 <                if (freadcolrs(scanin, xmax, stdin) < 0)
479 >                if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0)
480                          quiterr("error reading Radiance picture");
481 <                normcolrs(scanin, xmax, bradj); /* normalize */
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);
488 >                        (*putprim)(scanin, RED);
489 >                        (*putprim)(scanin, GRN);
490 >                        (*putprim)(scanin, BLU);
491                  } else
492 <                        putprim(scanin, GRY);
492 >                        (*putprim)(scanin, GRY);
493                  if (ferror(stdout))
494                          quiterr("error writing PostScript file");
495          }
496          putchar('\n');
497                                                  /* free scanline */
498 <        free((char *)scanin);
498 >        free((void *)scanin);
499   }
500  
501  
502 < putprim(scn, pri)               /* put out one primary from scanline */
503 < COLR    *scn;
504 < int     pri;
502 > static void
503 > Aputprim(               /* put out hex ASCII primary from scanline */
504 >        COLR    *scn,
505 >        int     pri
506 > )
507   {
508 <        register int    c;
509 <        register int    x;
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;
# Line 325 | Line 574 | int    pri;
574   }
575  
576  
577 < putrle(cnt, cod)                /* put out cnt of cod */
578 < register int    cnt, cod;
577 > static void
578 > putrle(         /* put out cnt of cod */
579 >        int     cnt,
580 >        int     cod
581 > )
582   {
583          static int      col = 0;
584  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines