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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines