ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_ps.c
Revision: 2.11
Committed: Thu Sep 28 11:23:42 1995 UTC (28 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +80 -28 lines
Log Message:
added -c option for color output

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 /*
8 * Radiance picture to PostScript file translator -- one way!
9 */
10
11 #include <stdio.h>
12 #ifdef MSDOS
13 #include <fcntl.h>
14 #endif
15 #include "color.h"
16
17 #define GRY -1 /* artificial index for grey */
18
19 #define HMARGIN (.5*72) /* horizontal margin */
20 #define VMARGIN (.5*72) /* vertical margin */
21 #define PWIDTH (8.5*72-2*HMARGIN) /* width of device */
22 #define PHEIGHT (11*72-2*VMARGIN) /* height of device */
23
24 #define RUNCHR '*' /* character to start rle */
25 #define MINRUN 4 /* minimum run-length */
26 #define RSTRT '!' /* character for MINRUN */
27 #define MAXRUN (MINRUN+'~'-RSTRT) /* maximum run-length */
28
29 char code[] = /* 6-bit code lookup table */
30 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+";
31
32 int wrongformat = 0; /* input in wrong format? */
33 double pixaspect = 1.0; /* pixel aspect ratio */
34
35 int docolor = 0; /* produce color image? */
36 int bradj = 0; /* brightness adjustment */
37 int ncopies = 1; /* number of copies */
38
39 char *progname;
40
41 int xmax, ymax;
42
43 extern char *malloc();
44
45
46 headline(s) /* check header line */
47 char *s;
48 {
49 char fmt[32];
50
51 if (isformat(s)) {
52 formatval(fmt, s);
53 wrongformat = strcmp(fmt, COLRFMT);
54 } else if (isaspect(s))
55 pixaspect *= aspectval(s);
56 }
57
58
59 main(argc, argv)
60 int argc;
61 char *argv[];
62 {
63 int i;
64
65 progname = argv[0];
66
67 for (i = 1; i < argc; i++)
68 if (argv[i][0] == '-')
69 switch (argv[i][1]) {
70 case 'c': /* produce color PostScript */
71 docolor++;
72 break;
73 case 'e': /* exposure adjustment */
74 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
75 goto userr;
76 bradj = atoi(argv[++i]);
77 break;
78 case 'n': /* number of copies */
79 ncopies = atoi(argv[++i]);
80 break;
81 default:
82 goto userr;
83 }
84 else
85 break;
86
87 if (i < argc-2)
88 goto userr;
89 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
90 fprintf(stderr, "%s: can't open input \"%s\"\n",
91 progname, argv[i]);
92 exit(1);
93 }
94 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
95 fprintf(stderr, "%s: can't open output \"%s\"\n",
96 progname, argv[i+1]);
97 exit(1);
98 }
99 #ifdef MSDOS
100 setmode(fileno(stdin), O_BINARY);
101 #endif
102 /* get our header */
103 getheader(stdin, headline, NULL);
104 if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
105 quiterr("bad picture format");
106 /* write header */
107 PSheader(i <= argc-1 ? argv[i] : "<stdin>");
108 /* convert file */
109 ra2ps();
110 /* write trailer */
111 PStrailer();
112 exit(0);
113 userr:
114 fprintf(stderr, "Usage: %s [-c][-e +/-stops] [input [output]]\n",
115 progname);
116 exit(1);
117 }
118
119
120 quiterr(err) /* print message and exit */
121 char *err;
122 {
123 if (err != NULL) {
124 fprintf(stderr, "%s: %s\n", progname, err);
125 exit(1);
126 }
127 exit(0);
128 }
129
130
131 PSheader(name) /* print PostScript header */
132 char *name;
133 {
134 int landscape = 0;
135 double pwidth, pheight;
136 double iwidth, iheight;
137 /* EPS comments */
138 puts("%!PS-Adobe-2.0 EPSF-2.0");
139 printf("%%%%Title: %s\n", name);
140 printf("%%%%Creator: %s = %s\n", progname, SCCSid);
141 printf("%%%%Pages: %d\n", ncopies);
142 if (landscape = xmax > pixaspect*ymax)
143 puts("%%Orientation: Landscape");
144 else
145 puts("%%Orientation: Portrait");
146 if (PWIDTH > PHEIGHT ^ landscape) {
147 pwidth = PHEIGHT;
148 pheight = PWIDTH;
149 } else {
150 pwidth = PWIDTH;
151 pheight = PHEIGHT;
152 }
153 if (pheight/pwidth > pixaspect*ymax/xmax) {
154 iwidth = pwidth;
155 iheight = pwidth*pixaspect*ymax/xmax;
156 } else {
157 iheight = pheight;
158 iwidth = pheight*xmax/(pixaspect*ymax);
159 }
160 if (pwidth == PHEIGHT)
161 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
162 HMARGIN+(pheight-iheight)*.5,
163 VMARGIN+(pwidth-iwidth)*.5,
164 HMARGIN+(pheight-iheight)*.5+iheight,
165 VMARGIN+(pwidth-iwidth)*.5+iwidth);
166 else
167 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
168 HMARGIN+(pwidth-iwidth)*.5,
169 VMARGIN+(pheight-iheight)*.5,
170 HMARGIN+(pwidth-iwidth)*.5+iwidth,
171 VMARGIN+(pheight-iheight)*.5+iheight);
172 puts("%%EndComments");
173 puts("save");
174 puts("64 dict begin");
175 /* define image reader */
176 if (docolor) {
177 printf("/redline %d string def\n", xmax);
178 printf("/grnline %d string def\n", xmax);
179 printf("/bluline %d string def\n", xmax);
180 printf("/rgbline %d string def\n", 3*xmax);
181 PSprocdef("read6bitRLE", "interleave");
182 } else {
183 printf("/greyline %d string def\n", xmax);
184 PSprocdef("read6bitRLE", NULL);
185 }
186 /* set up transformation matrix */
187 printf("%f %f translate\n", HMARGIN, VMARGIN);
188 if (pwidth == PHEIGHT) {
189 printf("0 %f translate\n", PHEIGHT);
190 puts("-90 rotate");
191 }
192 printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5);
193 printf("%f %f scale\n", iwidth, iheight);
194 puts("%%%%EndProlog");
195 /* start image procedure */
196 printf("%d %d 8 [%d 0 0 %d 0 %d] ", xmax, ymax, xmax, -ymax, ymax);
197 if (docolor) {
198 puts("{redline read6bitRLE grnline read6bitRLE");
199 puts("\tbluline read6bitRLE rgbline interleave} false 3 colorimage");
200 } else
201 puts("{greyline read6bitRLE} image");
202 }
203
204
205 PStrailer() /* print PostScript trailer */
206 {
207 puts("%%Trailer");
208 if (ncopies > 1)
209 printf("/#copies %d def\n", ncopies);
210 puts("showpage");
211 puts("end");
212 puts("restore");
213 puts("%%EOF");
214 }
215
216
217 PSprocdef(nam, inam) /* define PS procedure to read image */
218 char *nam, *inam;
219 {
220 short itab[128];
221 register int i;
222 /* assign code values */
223 for (i = 0; i < 128; i++) /* clear */
224 itab[i] = -1;
225 for (i = 1; i < 63; i++) /* assign greys */
226 itab[code[i]] = i<<2 | 2;
227 itab[code[0]] = 0; /* black is black */
228 itab[code[63]] = 255; /* and white is white */
229 printf("/codetab [");
230 for (i = 0; i < 128; i++) {
231 if (!(i & 0xf))
232 putchar('\n');
233 printf(" %3d", itab[i]);
234 }
235 printf("\n] def\n");
236 printf("/nrept 0 def\n");
237 printf("/readbyte { currentfile read not {stop} if } bind def\n");
238 printf("/decode { codetab exch get } bind def\n");
239 printf("/%s {\t%% scanbuffer\n", nam);
240 printf("\t/scanline exch def\n");
241 printf("\t{ 0 1 %d { scanline exch\n", xmax-1);
242 printf("\t\tnrept 0 le\n");
243 printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR);
244 printf("\t\t\t\t\t{ pop /nrept readbyte %d sub def\n", RSTRT-MINRUN+1);
245 printf("\t\t\t\t\t\t/reptv readbyte decode def\n");
246 printf("\t\t\t\t\t\treptv exit }\n");
247 printf("\t\t\t\t\t{ decode dup 0 lt {pop} {exit} ifelse }\n");
248 printf("\t\t\t\tifelse } loop }\n");
249 printf("\t\t\t{ /nrept nrept 1 sub def reptv }\n");
250 printf("\t\tifelse put\n");
251 printf("\t\t} for\n");
252 printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n");
253 printf("} bind def\n");
254 if (inam == NULL)
255 return;
256 /* define interleaving procedure */
257 printf("/%s {\t%% redscn grnscn bluscn rgbscn\n", inam);
258 printf("\t/rgbscan exch def /bscan exch def\n");
259 printf("\t/gscan exch def /rscan exch def\n");
260 printf("\trscan length %d eq gscan length %d eq and ", xmax, xmax);
261 printf("bscan length %d eq and \n", xmax);
262 printf("\t{ 0 1 %d { /ndx exch def\n", xmax-1);
263 printf("\t\trgbscan ndx 3 mul rscan ndx get put\n");
264 printf("\t\trgbscan ndx 3 mul 1 add gscan ndx get put\n");
265 printf("\t\trgbscan ndx 3 mul 2 add bscan ndx get put\n");
266 printf("\t\t} for rgbscan }\n");
267 printf("\t{0 string} ifelse\n");
268 printf("} bind def\n");
269 }
270
271
272 ra2ps() /* convert Radiance scanlines to 6-bit */
273 {
274 register COLR *scanin;
275 int y;
276 /* allocate scanline */
277 scanin = (COLR *)malloc(xmax*sizeof(COLR));
278 if (scanin == NULL)
279 quiterr("out of memory in ra2ps");
280 /* convert image */
281 for (y = ymax-1; y >= 0; y--) {
282 if (freadcolrs(scanin, xmax, stdin) < 0)
283 quiterr("error reading Radiance picture");
284 normcolrs(scanin, xmax, bradj); /* normalize */
285 if (docolor) {
286 putprim(scanin, RED);
287 putprim(scanin, GRN);
288 putprim(scanin, BLU);
289 } else
290 putprim(scanin, GRY);
291 if (ferror(stdout))
292 quiterr("error writing PostScript file");
293 }
294 putchar('\n');
295 /* free scanline */
296 free((char *)scanin);
297 }
298
299
300 putprim(scn, pri) /* put out one primary from scanline */
301 COLR *scn;
302 int pri;
303 {
304 register int c;
305 register int x;
306 int lastc, cnt;
307
308 lastc = -1; cnt = 0;
309 for (x = 0; x < xmax; x++) {
310 if (pri == GRY)
311 c = normbright(scn[x]) + 2;
312 else
313 c = scn[x][pri] + 2;
314 if (c > 255) c = 255;
315 c = code[c>>2];
316 if (c == lastc && cnt < MAXRUN)
317 cnt++;
318 else {
319 putrle(cnt, lastc);
320 lastc = c;
321 cnt = 1;
322 }
323 }
324 putrle(cnt, lastc);
325 }
326
327
328 putrle(cnt, cod) /* put out cnt of cod */
329 register int cnt, cod;
330 {
331 static int col = 0;
332
333 if (cnt >= MINRUN) {
334 col += 3;
335 putchar(RUNCHR);
336 putchar(RSTRT-MINRUN+cnt);
337 putchar(cod);
338 } else {
339 col += cnt;
340 while (cnt-- > 0)
341 putchar(cod);
342 }
343 if (col >= 72) {
344 putchar('\n');
345 col = 0;
346 }
347 }