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