ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.1
Committed: Mon Jul 13 09:28:01 1992 UTC (31 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /* Copyright (c) 1992 Regents of the University of California */
8
9 #ifndef lint
10 static char SCCSid[] = "$SunId$ LBL";
11 #endif
12
13 /*
14 * Radiance picture to PostScript file translator -- one way!
15 */
16
17 #include <stdio.h>
18 #include "color.h"
19 #include "random.h"
20
21 #define HMARGIN (.5*72) /* horizontal margin */
22 #define VMARGIN (.5*72) /* vertical margin */
23 #define PWIDTH (8.5*72-2*HMARGIN) /* width of device */
24 #define PHEIGHT (11*72-2*VMARGIN) /* height of device */
25
26 char code[] = /* 6-bit code lookup table */
27 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+";
28
29 int wrongformat = 0; /* input in wrong format? */
30 double pixaspect = 1.0; /* pixel aspect ratio */
31
32 int bradj = 0; /* brightness adjustment */
33
34 char *progname;
35
36 int xmax, ymax;
37
38
39 headline(s) /* check header line */
40 char *s;
41 {
42 char fmt[32];
43
44 if (isformat(s)) {
45 formatval(fmt, s);
46 wrongformat = strcmp(fmt, COLRFMT);
47 } else if (isaspect(s))
48 pixaspect *= aspectval(s);
49 }
50
51
52 main(argc, argv)
53 int argc;
54 char *argv[];
55 {
56 int i;
57
58 progname = argv[0];
59
60 for (i = 1; i < argc; i++)
61 if (argv[i][0] == '-')
62 switch (argv[i][1]) {
63 case 'e': /* exposure adjustment */
64 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
65 goto userr;
66 bradj = atoi(argv[++i]);
67 break;
68 default:
69 goto userr;
70 }
71 else
72 break;
73
74 if (i < argc-2)
75 goto userr;
76 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
77 fprintf(stderr, "%s: can't open input \"%s\"\n",
78 progname, argv[i]);
79 exit(1);
80 }
81 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
82 fprintf(stderr, "can't open output \"%s\"\n",
83 progname, argv[i+1]);
84 exit(1);
85 }
86 /* get our header */
87 getheader(stdin, headline, NULL);
88 if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
89 quiterr("bad picture format");
90 /* write header */
91 PSheader(i <= argc-1 ? argv[i] : "<stdin>");
92 /* convert file */
93 ra2ps();
94 /* write trailer */
95 PStrailer();
96 exit(0);
97 userr:
98 fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname);
99 exit(1);
100 }
101
102
103 quiterr(err) /* print message and exit */
104 char *err;
105 {
106 if (err != NULL) {
107 fprintf(stderr, "%s: %s\n", progname, err);
108 exit(1);
109 }
110 exit(0);
111 }
112
113
114 PSheader(name) /* print PostScript header */
115 char *name;
116 {
117 int landscape = 0;
118 double pwidth, pheight;
119
120 printf("%%!\n");
121 printf("%%%%Title: %s\n", name);
122 printf("%%%%Creator: %s\n", progname);
123 printf("%%%%Pages: 1\n");
124 if (landscape = xmax > pixaspect*ymax)
125 printf("%%%%Landscape\n");
126 else
127 printf("%%%%Portrait\n");
128 printf("%%%%EndComments\n");
129 printf("gsave\n");
130 printf("64 dict begin\n");
131 /* set up transformation matrix */
132 printf("%f %f translate 1 -1 scale\n", HMARGIN, VMARGIN+PHEIGHT);
133 if (PWIDTH > PHEIGHT ^ landscape) {
134 printf("0 %f translate\n", PHEIGHT);
135 printf("90 rotate\n");
136 pwidth = PHEIGHT;
137 pheight = PWIDTH;
138 } else {
139 pwidth = PWIDTH;
140 pheight = PHEIGHT;
141 }
142 if (pheight/pwidth > pixaspect*ymax/xmax) {
143 printf("%f %f matrix scale\n", pwidth/xmax,
144 pixaspect*pwidth/xmax);
145 printf("0 %f matrix translate\n",
146 .5*(pheight-pwidth*pixaspect*ymax/xmax));
147 } else {
148 printf("%f %f matrix scale\n", pheight/(ymax*pixaspect),
149 pheight/ymax);
150 printf("%f 0 matrix translate\n",
151 .5*(pwidth-pheight*xmax/(ymax*pixaspect)));
152 }
153 printf("matrix concatmatrix /imat exch def\n");
154 PSprocdef("read6bit");
155 printf("%%%%EndProlog\n");
156 printf("%d %d 8 imat {read6bit} image", xmax, ymax);
157 }
158
159
160 PStrailer() /* print PostScript trailer */
161 {
162 puts("%%Trailer");
163 puts("end");
164 puts("showpage");
165 puts("grestore");
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 }