ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.4
Committed: Fri Aug 14 13:27:52 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +8 -3 lines
Log Message:
added -n option for number of copies to print

File Contents

# User Rev Content
1 greg 2.1 /* Copyright (c) 1992 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Radiance picture to PostScript file translator -- one way!
9     */
10    
11     #include <stdio.h>
12     #include "color.h"
13     #include "random.h"
14    
15     #define HMARGIN (.5*72) /* horizontal margin */
16     #define VMARGIN (.5*72) /* vertical margin */
17     #define PWIDTH (8.5*72-2*HMARGIN) /* width of device */
18     #define PHEIGHT (11*72-2*VMARGIN) /* height of device */
19    
20     char code[] = /* 6-bit code lookup table */
21     "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+";
22    
23     int wrongformat = 0; /* input in wrong format? */
24     double pixaspect = 1.0; /* pixel aspect ratio */
25    
26     int bradj = 0; /* brightness adjustment */
27 greg 2.4 int ncopies = 1; /* number of copies */
28 greg 2.1
29     char *progname;
30    
31     int xmax, ymax;
32    
33    
34     headline(s) /* check header line */
35     char *s;
36     {
37     char fmt[32];
38    
39     if (isformat(s)) {
40     formatval(fmt, s);
41     wrongformat = strcmp(fmt, COLRFMT);
42     } else if (isaspect(s))
43     pixaspect *= aspectval(s);
44     }
45    
46    
47     main(argc, argv)
48     int argc;
49     char *argv[];
50     {
51     int i;
52    
53     progname = argv[0];
54    
55     for (i = 1; i < argc; i++)
56     if (argv[i][0] == '-')
57     switch (argv[i][1]) {
58     case 'e': /* exposure adjustment */
59     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
60     goto userr;
61     bradj = atoi(argv[++i]);
62     break;
63 greg 2.4 case 'n': /* number of copies */
64     ncopies = atoi(argv[++i]);
65     break;
66 greg 2.1 default:
67     goto userr;
68     }
69     else
70     break;
71    
72     if (i < argc-2)
73     goto userr;
74     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
75     fprintf(stderr, "%s: can't open input \"%s\"\n",
76     progname, argv[i]);
77     exit(1);
78     }
79     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
80     fprintf(stderr, "can't open output \"%s\"\n",
81     progname, argv[i+1]);
82     exit(1);
83     }
84     /* get our header */
85     getheader(stdin, headline, NULL);
86     if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
87     quiterr("bad picture format");
88     /* write header */
89     PSheader(i <= argc-1 ? argv[i] : "<stdin>");
90     /* convert file */
91     ra2ps();
92     /* write trailer */
93     PStrailer();
94     exit(0);
95     userr:
96     fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname);
97     exit(1);
98     }
99    
100    
101     quiterr(err) /* print message and exit */
102     char *err;
103     {
104     if (err != NULL) {
105     fprintf(stderr, "%s: %s\n", progname, err);
106     exit(1);
107     }
108     exit(0);
109     }
110    
111    
112     PSheader(name) /* print PostScript header */
113     char *name;
114     {
115     int landscape = 0;
116     double pwidth, pheight;
117 greg 2.2 double iwidth, iheight;
118 greg 2.1
119     printf("%%!\n");
120     printf("%%%%Title: %s\n", name);
121     printf("%%%%Creator: %s\n", progname);
122 greg 2.4 printf("%%%%Pages: %d\n", ncopies);
123 greg 2.1 if (landscape = xmax > pixaspect*ymax)
124     printf("%%%%Landscape\n");
125     else
126     printf("%%%%Portrait\n");
127     printf("%%%%EndComments\n");
128     printf("gsave\n");
129     printf("64 dict begin\n");
130 greg 2.3 /* define image reader */
131     PSprocdef("read6bit");
132 greg 2.1 /* set up transformation matrix */
133 greg 2.2 printf("%f %f translate\n", HMARGIN, VMARGIN);
134 greg 2.1 if (PWIDTH > PHEIGHT ^ landscape) {
135     printf("0 %f translate\n", PHEIGHT);
136 greg 2.2 printf("-90 rotate\n");
137 greg 2.1 pwidth = PHEIGHT;
138     pheight = PWIDTH;
139     } else {
140     pwidth = PWIDTH;
141     pheight = PHEIGHT;
142     }
143     if (pheight/pwidth > pixaspect*ymax/xmax) {
144 greg 2.2 iwidth = pwidth;
145     iheight = pwidth*pixaspect*ymax/xmax;
146 greg 2.1 } else {
147 greg 2.2 iheight = pheight;
148     iwidth = pheight*xmax/(pixaspect*ymax);
149 greg 2.1 }
150 greg 2.2 printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5);
151     printf("%f %f scale\n", iwidth, iheight);
152 greg 2.1 printf("%%%%EndProlog\n");
153 greg 2.3 /* start image procedure */
154 greg 2.2 printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax,
155     xmax, -ymax, ymax);
156 greg 2.1 }
157    
158    
159     PStrailer() /* print PostScript trailer */
160     {
161     puts("%%Trailer");
162 greg 2.4 if (ncopies > 1)
163     printf("/#copies %d def\n", ncopies);
164     puts("showpage");
165 greg 2.1 puts("end");
166     puts("%%EOF");
167     }
168    
169    
170     PSprocdef(nam) /* define PS procedure to read image */
171     char *nam;
172     {
173     short itab[128];
174     register int i;
175    
176     for (i = 0; i < 128; i++)
177     itab[i] = -1;
178     for (i = 0; i < 64; i++)
179     itab[code[i]] = i<<2 | 2;
180     printf("/decode [");
181     for (i = 0; i < 128; i++) {
182     if (!(i & 0xf))
183     putchar('\n');
184     printf(" %3d", itab[i]);
185     }
186     printf("\n] def\n");
187     printf("/scanline %d string def\n", xmax);
188     printf("/%s {\n", nam);
189     printf("\t{ 0 1 %d { scanline exch\n", xmax-1);
190     printf("\t\t{ decode currentfile read not {stop} if get\n");
191     printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n");
192     printf("} stopped {pop pop pop 0 string} {scanline} ifelse } def\n");
193     }
194    
195    
196     ra2ps() /* convert Radiance scanlines to 6-bit */
197     {
198     COLR *scanin;
199     register int col = 0;
200     register int c;
201     register int x;
202     int y;
203     /* allocate scanline */
204     scanin = (COLR *)malloc(xmax*sizeof(COLR));
205     if (scanin == NULL)
206     quiterr("out of memory in ra2ps");
207     /* convert image */
208     for (y = ymax-1; y >= 0; y--) {
209     if (freadcolrs(scanin, xmax, stdin) < 0)
210     quiterr("error reading Radiance picture");
211     normcolrs(scanin, xmax, bradj); /* normalize */
212     for (x = 0; x < xmax; x++) {
213     if (!(col++ & 0x3f))
214     putchar('\n');
215     c = normbright(scanin[x]) + (random()&3);
216     if (c > 255) c = 255;
217     putchar(code[c>>2]);
218     }
219     if (ferror(stdout))
220     quiterr("error writing PostScript file");
221     }
222     putchar('\n');
223     /* free scanline */
224     free((char *)scanin);
225     }