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.17 by gregl, Thu Sep 18 15:16:23 1997 UTC vs.
Revision 2.21 by gwlarson, Fri Aug 28 10:03:46 1998 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
10  
11   #include  <stdio.h>
12   #include  <math.h>
13 + #include  <ctype.h>
14   #ifdef MSDOS
15   #include  <fcntl.h>
16   #endif
17   #include  "color.h"
18  
19 + #define UPPER(c)        ((c)&~0x20)             /* ASCII trick */
20 +
21   #define CODE6GAM        1.47                    /* gamma for 6-bit codes */
22 < #define DEFDGAM         1.0                     /* default device gamma */
22 > #define DEFGGAM         1.0                     /* greyscale device gamma */
23 > #define DEFCGAM         1.8                     /* color device gamma */
24  
25   #define GRY             -1                      /* artificial index for grey */
26  
27 < #define HMARGIN         (.5*72)                 /* horizontal margin */
28 < #define VMARGIN         (.5*72)                 /* vertical margin */
29 < #define PWIDTH          (8.5*72-2*HMARGIN)      /* width of device */
30 < #define PHEIGHT         (11*72-2*VMARGIN)       /* height of device */
27 > #define DEFMARG         0.5                     /* default margin (inches) */
28 > #define DEFWIDTH        8.5                     /* default page width */
29 > #define DEFHEIGHT       11                      /* default page height */
30 > #define HMARGIN         (hmarg*72)              /* horizontal margin */
31 > #define VMARGIN         (vmarg*72)              /* vertical margin */
32 > #define PWIDTH          (width*72-2*HMARGIN)    /* width of device */
33 > #define PHEIGHT         (height*72-2*VMARGIN)   /* height of device */
34  
35   #define RUNCHR          '*'                     /* character to start rle */
36   #define MINRUN          4                       /* minimum run-length */
# Line 36 | Line 43 | char  code[] =                 /* 6-bit code lookup table */
43   int  wrongformat = 0;                   /* input in wrong format? */
44   double  pixaspect = 1.0;                /* pixel aspect ratio */
45  
46 < double  devgam = DEFDGAM;               /* device gamma response */
47 < int  docolor = 0;                       /* produce color image? */
46 > double  devgam = 0.;                    /* device gamma response */
47 > double  hmarg = DEFMARG,
48 >        vmarg = DEFMARG;                /* horizontal and vertical margins */
49 > double  width = DEFWIDTH,
50 >        height = DEFHEIGHT;             /* default paper width and height */
51 > double  dpi = 0;                        /* print density (0 if unknown) */
52 > int  docolor = 1;                       /* produce color image? */
53   int  bradj = 0;                         /* brightness adjustment */
54   int  ncopies = 1;                       /* number of copies */
55  
# Line 47 | Line 59 | int  (*putprim)() = Aputprim;          /* function for writing
59  
60   char  *progname;
61  
62 < int  xmax, ymax;
62 > int  xmax, ymax;                        /* input image dimensions */
63  
64   extern char  *malloc();
65 + extern double   unit2inch();
66  
67  
68   headline(s)             /* check header line */
# Line 70 | Line 83 | int  argc;
83   char  *argv[];
84   {
85          int  i;
86 +        double  d;
87          
88          progname = argv[0];
89  
# Line 91 | Line 105 | char  *argv[];
105                          case 'C':               /* compressed ASCII encoding */
106                                  putprim = Cputprim;
107                                  break;
108 <                        case 'g':
108 >                        case 'd':               /* print density */
109 >                                dpi = atof(argv[++i]);
110 >                                break;
111 >                        case 'g':               /* device gamma adjustment */
112                                  devgam = atof(argv[++i]);
113                                  break;
114 +                        case 'p':               /* paper size */
115 +                                parsepaper(argv[++i]);
116 +                                break;
117 +                        case 'm':               /* margin */
118 +                                d = atof(argv[i+1]);
119 +                                d *= unit2inch(argv[i+1]);
120 +                                switch (argv[i][2]) {
121 +                                case '\0':
122 +                                        hmarg = vmarg = d;
123 +                                        break;
124 +                                case 'h':
125 +                                        hmarg = d;
126 +                                        break;
127 +                                case 'v':
128 +                                        vmarg = d;
129 +                                        break;
130 +                                default:
131 +                                        goto userr;
132 +                                }
133 +                                i++;
134 +                                break;
135                          case 'e':               /* exposure adjustment */
136                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
137                                          goto userr;
# Line 128 | Line 166 | char  *argv[];
166          if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
167                  quiterr("bad picture format");
168                                  /* gamma compression */
169 +        if (devgam <= 0.05)
170 +                devgam = docolor ? DEFCGAM : DEFGGAM;
171          if (putprim == Cputprim)
172 <                setcolrgam(CODE6GAM/devgam);
172 >                setcolrgam(CODE6GAM);
173          else if (devgam != 1.)
174 <                setcolrgam(1./devgam);
174 >                setcolrgam(devgam);
175                                  /* write header */
176 <        PSheader(i <= argc-1 ? argv[i] : "<stdin>");
176 >        PSheader(argc, argv);
177                                  /* convert file */
178          ra2ps();
179                                  /* write trailer */
180          PStrailer();
181          exit(0);
182   userr:
183 <        fprintf(stderr, "Usage: %s [-b|c][-A|B|C][-e +/-stops][-g gamma] [input [output]]\n",
183 >        fprintf(stderr,
184 > "Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-d dpi][-g gamma] [input [output]]\n",
185                          progname);
186          exit(1);
187   }
188  
189  
190 + double
191 + unit2inch(s)            /* determine unit */
192 + register char   *s;
193 + {
194 +        static struct unit {char n; float f} u[] = {
195 +                'i', 1.,
196 +                'm', 1./25.4,
197 +                'c', 1./2.54,
198 +                '\0' };
199 +        register struct unit    *up;
200 +
201 +        while (*s && !isalpha(*s))
202 +                s++;
203 +        for (up = u; up->n; up++)
204 +                if (up->n == *s)
205 +                        return(up->f);
206 +        return(1.);
207 + }
208 +
209 +
210 + int
211 + matchid(name, id)       /* see if name matches id (case insensitive) */
212 + char    *name;
213 + register char   *id;
214 + {
215 +        register char   *s = name;
216 +
217 +        while (*s) {
218 +                if (isalpha(*s)) {
219 +                        if (!isalpha(*id) || UPPER(*s) != UPPER(*id))
220 +                                return(0);
221 +                } else if (*s != *id)
222 +                        return(0);
223 +                s++; id++;
224 +        }
225 +        return(!*id || s-name >= 3);    /* substrings >= 3 chars OK */
226 + }
227 +
228 +
229 + parsepaper(ps)          /* determine paper size from name */
230 + char    *ps;
231 + {
232 +        static struct psize {char n[12]; float w,h;} p[] = {
233 +                "envelope", 4.12, 9.5,
234 +                "executive", 7.25, 10.5,
235 +                "letter", 8.5, 11.,
236 +                "lettersmall", 7.68, 10.16,
237 +                "legal", 8.5, 14.,
238 +                "monarch", 3.87, 7.5,
239 +                "statement", 5.5, 8.5,
240 +                "tabloid", 11., 17.,
241 +                "A3", 11.69, 16.54,
242 +                "A4", 8.27, 11.69,
243 +                "A4small", 7.47, 10.85,
244 +                "A5", 6.00, 8.27,
245 +                "A6", 4.13, 6.00,
246 +                "B4", 10.12, 14.33,
247 +                "B5", 7.17, 10.12,
248 +                "C5", 6.38, 9.01,
249 +                "C6", 4.49, 6.38,
250 +                "DL", 4.33, 8.66,
251 +                "hagaki", 3.94, 5.83,
252 +                "" };
253 +        register struct psize   *pp;
254 +        register char   *s = ps;
255 +        double  d;
256 +
257 +        if (isdigit(*s)) {              /* check for WWxHH specification */
258 +                width = atof(s);
259 +                while (*s && !isalpha(*s))
260 +                        s++;
261 +                d = unit2inch(s);
262 +                height = atof(++s);
263 +                width *= d;
264 +                height *= d;
265 +                if (width >= 1. & height >= 1.)
266 +                        return;
267 +        } else                          /* check for match to standard size */
268 +                for (pp = p; pp->n[0]; pp++)
269 +                        if (matchid(s, pp->n)) {
270 +                                width = pp->w;
271 +                                height = pp->h;
272 +                                return;
273 +                        }
274 +        fprintf(stderr, "%s: unknown paper size \"%s\" -- known sizes:\n",
275 +                        progname, ps);
276 +        fprintf(stderr, "_Name________Width_Height_(inches)\n");
277 +        for (pp = p; pp->n[0]; pp++)
278 +                fprintf(stderr, "%-11s  %5.2f  %5.2f\n", pp->n, pp->w, pp->h);
279 +        fprintf(stderr, "Or use WWxHH size specification\n");
280 +        exit(1);
281 + }
282 +
283 +
284   quiterr(err)            /* print message and exit */
285   char  *err;
286   {
# Line 157 | Line 292 | char  *err;
292   }
293  
294  
295 < PSheader(name)                  /* print PostScript header */
296 < char  *name;
295 > PSheader(ac, av)                /* print PostScript header */
296 > int  ac;
297 > char  **av;
298   {
299          char  *rstr;
300 <        int  landscape = 0;
300 >        int  landscape, rotate, n;
301          double  pwidth, pheight;
302          double  iwidth, iheight;
303                                          /* EPS comments */
304          puts("%!PS-Adobe-2.0 EPSF-2.0");
305 <        printf("%%%%Title: %s\n", name);
306 <        printf("%%%%Creator: %s = %s\n", progname, SCCSid);
305 >        printf("%%%%Title: "); printargs(ac, av, stdout);
306 >        printf("%%%%Creator: %s\n", SCCSid);
307          printf("%%%%Pages: %d\n", ncopies);
308          if (landscape = xmax > pixaspect*ymax)
309                  puts("%%Orientation: Landscape");
310          else
311                  puts("%%Orientation: Portrait");
312 <        if (PWIDTH > PHEIGHT ^ landscape) {
312 >        if (rotate = PWIDTH > PHEIGHT ^ landscape) {
313                  pwidth = PHEIGHT;
314                  pheight = PWIDTH;
315          } else {
316                  pwidth = PWIDTH;
317                  pheight = PHEIGHT;
318          }
319 <        if (pheight/pwidth > pixaspect*ymax/xmax) {
320 <                iwidth = pwidth;
321 <                iheight = pwidth*pixaspect*ymax/xmax;
322 <        } else {
323 <                iheight = pheight;
324 <                iwidth = pheight*xmax/(pixaspect*ymax);
325 <        }
326 <        if (pwidth == PHEIGHT)
319 >        if (dpi > 100 && pixaspect >= 0.99 & pixaspect <= 1.01)
320 >                if (pheight/pwidth > ymax/xmax) {
321 >                        n = pwidth*dpi/xmax;    /* floor */
322 >                        iwidth = n > 0 ? (double)(n*xmax)/dpi : pwidth;
323 >                        iheight = iwidth*ymax/xmax;
324 >                } else {
325 >                        n = pheight*dpi/ymax;   /* floor */
326 >                        iheight = n > 0 ? (double)(n*ymax)/dpi : pheight;
327 >                        iwidth = iheight*xmax/ymax;
328 >                }
329 >        else
330 >                if (pheight/pwidth > pixaspect*ymax/xmax) {
331 >                        iwidth = pwidth;
332 >                        iheight = iwidth*pixaspect*ymax/xmax;
333 >                } else {
334 >                        iheight = pheight;
335 >                        iwidth = iheight*xmax/(pixaspect*ymax);
336 >                }
337 >        if (rotate)
338                  printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
339                                  HMARGIN+(pheight-iheight)*.5,
340                                  VMARGIN+(pwidth-iwidth)*.5,
# Line 214 | Line 361 | char  *name;
361                  PSprocdef("read6bitRLE");
362                                          /* set up transformation matrix */
363          printf("%f %f translate\n", HMARGIN, VMARGIN);
364 <        if (pwidth == PHEIGHT) {
364 >        if (rotate) {
365                  printf("%f 0 translate\n", PWIDTH);
366                  puts("90 rotate");
367          }
# Line 265 | Line 412 | char  *nam;
412          for (i = 0; i < 128; i++)       /* clear */
413                  itab[i] = -1;
414          for (i = 1; i < 63; i++)        /* assign greys */
415 <                itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM);
415 >                itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam);
416          itab[code[0]] = 0;              /* black is black */
417          itab[code[63]] = 255;           /* and white is white */
418          printf("/codetab [");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines