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