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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines