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