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.18 by gregl, Thu Sep 18 18:01:24 1997 UTC vs.
Revision 2.31 by greg, Sat Dec 28 18:05:14 2019 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  Radiance picture to PostScript file translator -- one way!
6   */
7  
11 #include  <stdio.h>
8   #include  <math.h>
9   #include  <ctype.h>
10 < #ifdef MSDOS
11 < #include  <fcntl.h>
12 < #endif
10 >
11 > #include  "platform.h"
12 > #include  "rtio.h"
13   #include  "color.h"
14 + #include  "resolu.h"
15  
16   #define UPPER(c)        ((c)&~0x20)             /* ASCII trick */
17  
18   #define CODE6GAM        1.47                    /* gamma for 6-bit codes */
19 < #define DEFDGAM         1.0                     /* default device gamma */
19 > #define DEFGGAM         1.0                     /* greyscale device gamma */
20 > #define DEFCGAM         1.8                     /* color device gamma */
21  
22   #define GRY             -1                      /* artificial index for grey */
23  
# Line 42 | Line 40 | char  code[] =                 /* 6-bit code lookup table */
40   int  wrongformat = 0;                   /* input in wrong format? */
41   double  pixaspect = 1.0;                /* pixel aspect ratio */
42  
43 < double  devgam = DEFDGAM;               /* device gamma response */
43 > double  devgam = 0.;                    /* device gamma response */
44   double  hmarg = DEFMARG,
45          vmarg = DEFMARG;                /* horizontal and vertical margins */
46   double  width = DEFWIDTH,
47          height = DEFHEIGHT;             /* default paper width and height */
48 < int  docolor = 0;                       /* produce color image? */
48 > double  dpi = 0;                        /* print density (0 if unknown) */
49 > int  docolor = 1;                       /* produce color image? */
50   int  bradj = 0;                         /* brightness adjustment */
51   int  ncopies = 1;                       /* number of copies */
52  
53 < extern int      Aputprim(), Bputprim(), Cputprim();
53 > char  *progname;
54 > int  xmax, ymax;                        /* input image dimensions */
55  
56 < int  (*putprim)() = Aputprim;           /* function for writing scanline */
56 > typedef void putprimf_t(COLR *scn, int pri);
57  
58 < char  *progname;
58 > static gethfunc headline;
59 > static putprimf_t Aputprim, Bputprim, Cputprim;
60  
61 < int  xmax, ymax;
61 > static double unit2inch(register 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  
62 extern char  *malloc();
63 extern double   unit2inch();
71  
72 + putprimf_t *putprim = Aputprim;         /* function for writing scanline */
73  
74 < headline(s)             /* check header line */
75 < char  *s;
74 >
75 > static int
76 > headline(               /* check header line */
77 >        char    *s,
78 >        void    *p
79 > )
80   {
81 <        char  fmt[32];
81 >        char  fmt[MAXFMTLEN];
82  
83          if (isformat(s)) {
84                  formatval(fmt, s);
85                  wrongformat = strcmp(fmt, COLRFMT);
86          } else if (isaspect(s))
87                  pixaspect *= aspectval(s);
88 +        return(0);
89   }
90  
91 <
92 < main(argc, argv)
80 < int  argc;
81 < char  *argv[];
91 > int
92 > main(int  argc, char  *argv[])
93   {
94          int  i;
95          double  d;
# Line 103 | Line 114 | char  *argv[];
114                          case 'C':               /* compressed ASCII encoding */
115                                  putprim = Cputprim;
116                                  break;
117 +                        case 'd':               /* print density */
118 +                                dpi = atof(argv[++i]);
119 +                                break;
120                          case 'g':               /* device gamma adjustment */
121                                  devgam = atof(argv[++i]);
122                                  break;
# Line 153 | Line 167 | char  *argv[];
167                                  progname, argv[i+1]);
168                  exit(1);
169          }
170 < #ifdef MSDOS
157 <        setmode(fileno(stdin), O_BINARY);
158 < #endif
170 >        SET_FILE_BINARY(stdin);
171                                  /* get our header */
172          getheader(stdin, headline, NULL);
173          if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
174                  quiterr("bad picture format");
175                                  /* gamma compression */
176 +        if (devgam <= 0.05)
177 +                devgam = docolor ? DEFCGAM : DEFGGAM;
178          if (putprim == Cputprim)
179 <                setcolrgam(CODE6GAM/devgam);
179 >                setcolrgam(CODE6GAM);
180          else if (devgam != 1.)
181 <                setcolrgam(1./devgam);
181 >                setcolrgam(devgam);
182                                  /* write header */
183 <        PSheader(i <= argc-1 ? argv[i] : "<stdin>");
183 >        PSheader(argc, argv);
184                                  /* convert file */
185          ra2ps();
186                                  /* write trailer */
# Line 174 | Line 188 | char  *argv[];
188          exit(0);
189   userr:
190          fprintf(stderr,
191 < "Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-g gamma] [input [output]]\n",
191 > "Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-d dpi][-g gamma] [input [output]]\n",
192                          progname);
193          exit(1);
194   }
195  
196  
197 < double
198 < unit2inch(s)            /* determine unit */
199 < register char   *s;
197 > static double
198 > unit2inch(              /* determine unit */
199 >        register char   *s
200 > )
201   {
202 <        static struct unit {char n; float f} u[] = {
203 <                'i', 1.,
204 <                'm', 1./25.4,
205 <                'c', 1./2.54,
206 <                '\0' };
202 >        static struct unit {char n; float f;} u[] = {
203 >                {'i', 1.},
204 >                {'m', 1./25.4},
205 >                {'c', 1./2.54},
206 >                {'\0',0} };
207          register struct unit    *up;
208  
209          while (*s && !isalpha(*s))
# Line 200 | Line 215 | register char  *s;
215   }
216  
217  
218 < int
219 < matchid(name, id)       /* see if name matches id (case insensitive) */
220 < char    *name;
221 < register char   *id;
218 > static int
219 > matchid(        /* see if name matches id (case insensitive) */
220 >        char    *name,
221 >        register char   *id
222 > )
223   {
224          register char   *s = name;
225  
# Line 219 | Line 235 | register char  *id;
235   }
236  
237  
238 < parsepaper(ps)          /* determine paper size from name */
239 < char    *ps;
238 > static void
239 > parsepaper(             /* determine paper size from name */
240 >        char    *ps
241 > )
242   {
243          static struct psize {char n[12]; float w,h;} p[] = {
244 <                "letter", 8.5, 11.,
245 <                "legal", 8.5, 14.,
246 <                "executive", 7.25, 10.5,
247 <                "envelope", 4.12, 9.5,
248 <                "monarch", 3.87, 7.5,
249 <                "tabloid", 11., 17.,
250 <                "A3", 11.69, 16.54,
251 <                "A4", 8.27, 11.69,
252 <                "A5", 6.00, 8.27,
253 <                "A6", 4.13, 6.00,
254 <                "B4", 10.12, 14.33,
255 <                "B5", 7.17, 10.12,
256 <                "DL", 4.33, 8.66,
257 <                "C5", 6.38, 9.01,
258 <                "C6", 4.49, 6.38,
259 <                "hagaki", 3.94, 5.83,
260 <                "" };
244 >                {"envelope", 4.12, 9.5},
245 >                {"executive", 7.25, 10.5},
246 >                {"letter", 8.5, 11.},
247 >                {"lettersmall", 7.68, 10.16},
248 >                {"legal", 8.5, 14.},
249 >                {"monarch", 3.87, 7.5},
250 >                {"statement", 5.5, 8.5},
251 >                {"tabloid", 11., 17.},
252 >                {"A3", 11.69, 16.54},
253 >                {"A4", 8.27, 11.69},
254 >                {"A4small", 7.47, 10.85},
255 >                {"A5", 6.00, 8.27},
256 >                {"A6", 4.13, 6.00},
257 >                {"B4", 10.12, 14.33},
258 >                {"B5", 7.17, 10.12},
259 >                {"C5", 6.38, 9.01},
260 >                {"C6", 4.49, 6.38},
261 >                {"DL", 4.33, 8.66},
262 >                {"hagaki", 3.94, 5.83},
263 >                {"",0.0,0.0} };
264          register struct psize   *pp;
265          register char   *s = ps;
266          double  d;
# Line 252 | Line 273 | char   *ps;
273                  height = atof(++s);
274                  width *= d;
275                  height *= d;
276 <                if (width >= 1. & height >= 1.)
276 >                if ((width >= 1.) & (height >= 1.))
277                          return;
278          } else                          /* check for match to standard size */
279                  for (pp = p; pp->n[0]; pp++)
# Line 266 | Line 287 | char   *ps;
287          fprintf(stderr, "_Name________Width_Height_(inches)\n");
288          for (pp = p; pp->n[0]; pp++)
289                  fprintf(stderr, "%-11s  %5.2f  %5.2f\n", pp->n, pp->w, pp->h);
290 +        fprintf(stderr, "Or use WWxHH size specification\n");
291          exit(1);
292   }
293  
294  
295 < quiterr(err)            /* print message and exit */
296 < char  *err;
295 > static void
296 > quiterr(                /* print message and exit */
297 >        char  *err
298 > )
299   {
300          if (err != NULL) {
301                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 281 | Line 305 | char  *err;
305   }
306  
307  
308 < PSheader(name)                  /* print PostScript header */
309 < char  *name;
308 > static void
309 > PSheader(               /* print PostScript header */
310 >        int  ac,
311 >        char  **av
312 > )
313   {
314          char  *rstr;
315 <        int  landscape = 0;
315 >        int  landscape, rotate, n;
316          double  pwidth, pheight;
317          double  iwidth, iheight;
318                                          /* EPS comments */
319          puts("%!PS-Adobe-2.0 EPSF-2.0");
320 <        printf("%%%%Title: %s\n", name);
321 <        printf("%%%%Creator: %s = %s\n", progname, SCCSid);
320 >        printf("%%%%Title: "); printargs(ac, av, stdout);
321 >        printf("%%%%Creator: %s\n", progname);
322          printf("%%%%Pages: %d\n", ncopies);
323 <        if (landscape = xmax > pixaspect*ymax)
323 >        if ( (landscape = xmax > pixaspect*ymax) )
324                  puts("%%Orientation: Landscape");
325          else
326                  puts("%%Orientation: Portrait");
327 <        if (PWIDTH > PHEIGHT ^ landscape) {
327 >        if ( (rotate = (PWIDTH > PHEIGHT) ^ landscape) ) {
328                  pwidth = PHEIGHT;
329                  pheight = PWIDTH;
330          } else {
331                  pwidth = PWIDTH;
332                  pheight = PHEIGHT;
333          }
334 <        if (pheight/pwidth > pixaspect*ymax/xmax) {
335 <                iwidth = pwidth;
336 <                iheight = pwidth*pixaspect*ymax/xmax;
337 <        } else {
338 <                iheight = pheight;
339 <                iwidth = pheight*xmax/(pixaspect*ymax);
340 <        }
341 <        if (pwidth == PHEIGHT)
334 >        if (dpi > 100 && (pixaspect >= 0.99) & (pixaspect <= 1.01))
335 >                if (pheight/pwidth > ymax/xmax) {
336 >                        n = pwidth*dpi/xmax;    /* floor */
337 >                        iwidth = n > 0 ? (double)(n*xmax)/dpi : pwidth;
338 >                        iheight = iwidth*ymax/xmax;
339 >                } else {
340 >                        n = pheight*dpi/ymax;   /* floor */
341 >                        iheight = n > 0 ? (double)(n*ymax)/dpi : pheight;
342 >                        iwidth = iheight*xmax/ymax;
343 >                }
344 >        else
345 >                if (pheight/pwidth > pixaspect*ymax/xmax) {
346 >                        iwidth = pwidth;
347 >                        iheight = iwidth*pixaspect*ymax/xmax;
348 >                } else {
349 >                        iheight = pheight;
350 >                        iwidth = iheight*xmax/(pixaspect*ymax);
351 >                }
352 >        if (rotate)
353                  printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
354                                  HMARGIN+(pheight-iheight)*.5,
355                                  VMARGIN+(pwidth-iwidth)*.5,
# Line 338 | Line 376 | char  *name;
376                  PSprocdef("read6bitRLE");
377                                          /* set up transformation matrix */
378          printf("%f %f translate\n", HMARGIN, VMARGIN);
379 <        if (pwidth == PHEIGHT) {
379 >        if (rotate) {
380                  printf("%f 0 translate\n", PWIDTH);
381                  puts("90 rotate");
382          }
# Line 368 | Line 406 | char  *name;
406   }
407  
408  
409 < PStrailer()                     /* print PostScript trailer */
409 > static void
410 > PStrailer(void)                 /* print PostScript trailer */
411   {
412          puts("%%Trailer");
413          if (ncopies > 1)
# Line 380 | Line 419 | PStrailer()                    /* print PostScript trailer */
419   }
420  
421  
422 < PSprocdef(nam)                  /* define PS procedure to read image */
423 < char  *nam;
422 > static void
423 > PSprocdef(                      /* define PS procedure to read image */
424 >        char  *nam
425 > )
426   {
427          short  itab[128];
428          register int  i;
# Line 389 | Line 430 | char  *nam;
430          for (i = 0; i < 128; i++)       /* clear */
431                  itab[i] = -1;
432          for (i = 1; i < 63; i++)        /* assign greys */
433 <                itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM);
434 <        itab[code[0]] = 0;              /* black is black */
435 <        itab[code[63]] = 255;           /* and white is white */
433 >                itab[(int)code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam);
434 >        itab[(int)code[0]] = 0;         /* black is black */
435 >        itab[(int)code[63]] = 255;              /* and white is white */
436          printf("/codetab [");
437          for (i = 0; i < 128; i++) {
438                  if (!(i & 0xf))
# Line 420 | Line 461 | char  *nam;
461   }
462  
463  
464 < ra2ps()                         /* convert Radiance scanlines to 6-bit */
464 > static void
465 > ra2ps(void)                             /* convert Radiance scanlines to 6-bit */
466   {
467          register COLR   *scanin;
468          int     y;
# Line 449 | Line 491 | ra2ps()                                /* convert Radiance scanlines to 6-bit */
491          }
492          putchar('\n');
493                                                  /* free scanline */
494 <        free((char *)scanin);
494 >        free((void *)scanin);
495   }
496  
497  
498 < int
499 < Aputprim(scn, pri)              /* put out hex ASCII primary from scanline */
500 < COLR    *scn;
501 < int     pri;
498 > static void
499 > Aputprim(               /* put out hex ASCII primary from scanline */
500 >        COLR    *scn,
501 >        int     pri
502 > )
503   {
504          static char     hexdigit[] = "0123456789ABCDEF";
505          static int      col = 0;
# Line 478 | Line 521 | int    pri;
521   }
522  
523  
524 < int
525 < Bputprim(scn, pri)              /* put out binary primary from scanline */
526 < COLR    *scn;
527 < int     pri;
524 > static void
525 > Bputprim(               /* put out binary primary from scanline */
526 >        COLR    *scn,
527 >        int     pri
528 > )
529   {
530          register int    x, c;
531  
# Line 496 | Line 540 | int    pri;
540   }
541  
542  
543 < int
544 < Cputprim(scn, pri)              /* put out compressed primary from scanline */
545 < COLR    *scn;
546 < int     pri;
543 > static void
544 > Cputprim(               /* put out compressed primary from scanline */
545 >        COLR    *scn,
546 >        int     pri
547 > )
548   {
549          register int    c;
550          register int    x;
# Line 525 | Line 570 | int    pri;
570   }
571  
572  
573 < putrle(cnt, cod)                /* put out cnt of cod */
574 < register int    cnt, cod;
573 > static void
574 > putrle(         /* put out cnt of cod */
575 >        register int    cnt,
576 >        register int    cod
577 > )
578   {
579          static int      col = 0;
580  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines