1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
#endif |
6 |
|
|
7 |
|
/* |
9 |
|
*/ |
10 |
|
|
11 |
|
#include <stdio.h> |
12 |
+ |
#include <math.h> |
13 |
+ |
#ifdef MSDOS |
14 |
+ |
#include <fcntl.h> |
15 |
+ |
#endif |
16 |
|
#include "color.h" |
13 |
– |
#include "random.h" |
17 |
|
|
18 |
+ |
#define CODE6GAM 1.47 /* gamma for 6-bit codes */ |
19 |
+ |
#define DEFDGAM 1.0 /* default device gamma */ |
20 |
+ |
|
21 |
+ |
#define GRY -1 /* artificial index for grey */ |
22 |
+ |
|
23 |
|
#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 |
+ |
#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 |
|
char code[] = /* 6-bit code lookup table */ |
34 |
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+"; |
35 |
|
|
36 |
|
int wrongformat = 0; /* input in wrong format? */ |
37 |
< |
double pixaspect = 1.0; /* pixel aspect ratio */ |
37 |
> |
double pixaspect = 1.0; /* pixel aspect ratio */ |
38 |
|
|
39 |
+ |
double devgam = DEFDGAM; /* device gamma response */ |
40 |
+ |
int docolor = 0; /* produce color image? */ |
41 |
|
int bradj = 0; /* brightness adjustment */ |
42 |
|
int ncopies = 1; /* number of copies */ |
43 |
|
|
44 |
+ |
extern int Aputprim(), Bputprim(), Cputprim(); |
45 |
+ |
|
46 |
+ |
int (*putprim)() = Aputprim; /* function for writing scanline */ |
47 |
+ |
|
48 |
|
char *progname; |
49 |
|
|
50 |
|
int xmax, ymax; |
51 |
|
|
52 |
+ |
extern char *malloc(); |
53 |
|
|
54 |
+ |
|
55 |
|
headline(s) /* check header line */ |
56 |
|
char *s; |
57 |
|
{ |
76 |
|
for (i = 1; i < argc; i++) |
77 |
|
if (argv[i][0] == '-') |
78 |
|
switch (argv[i][1]) { |
79 |
+ |
case 'b': /* produce b&w PostScript */ |
80 |
+ |
docolor = 0; |
81 |
+ |
break; |
82 |
+ |
case 'c': /* produce color PostScript */ |
83 |
+ |
docolor = 1; |
84 |
+ |
break; |
85 |
+ |
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 |
+ |
case 'g': |
95 |
+ |
devgam = atof(argv[++i]); |
96 |
+ |
break; |
97 |
|
case 'e': /* exposure adjustment */ |
98 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
99 |
|
goto userr; |
116 |
|
exit(1); |
117 |
|
} |
118 |
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
119 |
< |
fprintf(stderr, "can't open output \"%s\"\n", |
119 |
> |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
120 |
|
progname, argv[i+1]); |
121 |
|
exit(1); |
122 |
|
} |
123 |
+ |
#ifdef MSDOS |
124 |
+ |
setmode(fileno(stdin), O_BINARY); |
125 |
+ |
#endif |
126 |
|
/* get our header */ |
127 |
|
getheader(stdin, headline, NULL); |
128 |
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
129 |
|
quiterr("bad picture format"); |
130 |
+ |
/* gamma compression */ |
131 |
+ |
if (putprim == Cputprim) |
132 |
+ |
setcolrgam(CODE6GAM/devgam); |
133 |
+ |
else if (devgam != 1.) |
134 |
+ |
setcolrgam(1./devgam); |
135 |
|
/* write header */ |
136 |
|
PSheader(i <= argc-1 ? argv[i] : "<stdin>"); |
137 |
|
/* convert file */ |
140 |
|
PStrailer(); |
141 |
|
exit(0); |
142 |
|
userr: |
143 |
< |
fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname); |
143 |
> |
fprintf(stderr, "Usage: %s [-b|c][-A|B|C][-e +/-stops][-g gamma] [input [output]]\n", |
144 |
> |
progname); |
145 |
|
exit(1); |
146 |
|
} |
147 |
|
|
160 |
|
PSheader(name) /* print PostScript header */ |
161 |
|
char *name; |
162 |
|
{ |
163 |
+ |
char *rstr; |
164 |
|
int landscape = 0; |
165 |
< |
double pwidth, pheight; |
166 |
< |
double iwidth, iheight; |
167 |
< |
|
168 |
< |
printf("%%!\n"); |
165 |
> |
double pwidth, pheight; |
166 |
> |
double iwidth, iheight; |
167 |
> |
/* EPS comments */ |
168 |
> |
puts("%!PS-Adobe-2.0 EPSF-2.0"); |
169 |
|
printf("%%%%Title: %s\n", name); |
170 |
< |
printf("%%%%Creator: %s\n", progname); |
170 |
> |
printf("%%%%Creator: %s = %s\n", progname, SCCSid); |
171 |
|
printf("%%%%Pages: %d\n", ncopies); |
172 |
|
if (landscape = xmax > pixaspect*ymax) |
173 |
< |
printf("%%%%Landscape\n"); |
173 |
> |
puts("%%Orientation: Landscape"); |
174 |
|
else |
175 |
< |
printf("%%%%Portrait\n"); |
127 |
< |
printf("%%%%EndComments\n"); |
128 |
< |
printf("64 dict begin\n"); |
129 |
< |
/* define image reader */ |
130 |
< |
PSprocdef("read6bit"); |
131 |
< |
/* set up transformation matrix */ |
132 |
< |
printf("%f %f translate\n", HMARGIN, VMARGIN); |
175 |
> |
puts("%%Orientation: Portrait"); |
176 |
|
if (PWIDTH > PHEIGHT ^ landscape) { |
134 |
– |
printf("0 %f translate\n", PHEIGHT); |
135 |
– |
printf("-90 rotate\n"); |
177 |
|
pwidth = PHEIGHT; |
178 |
|
pheight = PWIDTH; |
179 |
|
} else { |
187 |
|
iheight = pheight; |
188 |
|
iwidth = pheight*xmax/(pixaspect*ymax); |
189 |
|
} |
190 |
+ |
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 |
+ |
puts("gsave save"); |
204 |
+ |
puts("17 dict begin"); |
205 |
+ |
/* define image reader */ |
206 |
+ |
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 |
+ |
} else |
211 |
+ |
printf("/gryline %d string def\n", xmax); |
212 |
+ |
/* use compressed encoding? */ |
213 |
+ |
if (putprim == Cputprim) |
214 |
+ |
PSprocdef("read6bitRLE"); |
215 |
+ |
/* set up transformation matrix */ |
216 |
+ |
printf("%f %f translate\n", HMARGIN, VMARGIN); |
217 |
+ |
if (pwidth == PHEIGHT) { |
218 |
+ |
printf("%f 0 translate\n", PWIDTH); |
219 |
+ |
puts("90 rotate"); |
220 |
+ |
} |
221 |
|
printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5); |
222 |
|
printf("%f %f scale\n", iwidth, iheight); |
223 |
< |
printf("%%%%EndProlog\n"); |
223 |
> |
puts("%%EndProlog"); |
224 |
|
/* start image procedure */ |
225 |
< |
printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax, |
226 |
< |
xmax, -ymax, ymax); |
225 |
> |
printf("%d %d 8 [%d 0 0 %d 0 %d]\n", xmax, ymax, xmax, -ymax, ymax); |
226 |
> |
if (putprim == Cputprim) { |
227 |
> |
if (docolor) { |
228 |
> |
puts("{redline read6bitRLE}"); |
229 |
> |
puts("{grnline read6bitRLE}"); |
230 |
> |
puts("{bluline read6bitRLE}"); |
231 |
> |
puts("true 3 colorimage"); |
232 |
> |
} 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 |
|
} |
245 |
|
|
246 |
|
|
251 |
|
printf("/#copies %d def\n", ncopies); |
252 |
|
puts("showpage"); |
253 |
|
puts("end"); |
254 |
+ |
puts("restore grestore"); |
255 |
|
puts("%%EOF"); |
256 |
|
} |
257 |
|
|
265 |
|
for (i = 0; i < 128; i++) /* clear */ |
266 |
|
itab[i] = -1; |
267 |
|
for (i = 1; i < 63; i++) /* assign greys */ |
268 |
< |
itab[code[i]] = i<<2 | 2; |
268 |
> |
itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM); |
269 |
|
itab[code[0]] = 0; /* black is black */ |
270 |
|
itab[code[63]] = 255; /* and white is white */ |
271 |
< |
printf("/decode ["); |
271 |
> |
printf("/codetab ["); |
272 |
|
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 |
< |
printf("/scanline %d string def\n", xmax); |
279 |
< |
printf("/%s {\n", nam); |
278 |
> |
printf("/nrept 0 def\n"); |
279 |
> |
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 |
|
printf("\t{ 0 1 %d { scanline exch\n", xmax-1); |
284 |
< |
printf("\t\t{ decode currentfile read not {stop} if get\n"); |
285 |
< |
printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n"); |
286 |
< |
printf("} stopped {pop pop pop 0 string} {scanline} ifelse } def\n"); |
284 |
> |
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 |
> |
printf("} bind def\n"); |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
ra2ps() /* convert Radiance scanlines to 6-bit */ |
300 |
|
{ |
301 |
< |
COLR *scanin; |
200 |
< |
register int col = 0; |
201 |
< |
register int c; |
202 |
< |
register int x; |
301 |
> |
register COLR *scanin; |
302 |
|
int y; |
303 |
|
/* allocate scanline */ |
304 |
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
308 |
|
for (y = ymax-1; y >= 0; y--) { |
309 |
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
310 |
|
quiterr("error reading Radiance picture"); |
311 |
< |
normcolrs(scanin, xmax, bradj); /* normalize */ |
312 |
< |
for (x = 0; x < xmax; x++) { |
313 |
< |
if (!(col++ & 0x3f)) |
314 |
< |
putchar('\n'); |
315 |
< |
c = normbright(scanin[x]) + (random()&3); |
316 |
< |
if (c > 255) c = 255; |
317 |
< |
putchar(code[c>>2]); |
318 |
< |
} |
311 |
> |
if (putprim == Cputprim || devgam != 1.) { |
312 |
> |
if (bradj) /* adjust exposure */ |
313 |
> |
shiftcolrs(scanin, xmax, bradj); |
314 |
> |
colrs_gambs(scanin, xmax); /* gamma compression */ |
315 |
> |
} else |
316 |
> |
normcolrs(scanin, xmax, bradj); |
317 |
> |
if (docolor) { |
318 |
> |
(*putprim)(scanin, RED); |
319 |
> |
(*putprim)(scanin, GRN); |
320 |
> |
(*putprim)(scanin, BLU); |
321 |
> |
} else |
322 |
> |
(*putprim)(scanin, GRY); |
323 |
|
if (ferror(stdout)) |
324 |
|
quiterr("error writing PostScript file"); |
325 |
|
} |
326 |
|
putchar('\n'); |
327 |
|
/* free scanline */ |
328 |
|
free((char *)scanin); |
329 |
+ |
} |
330 |
+ |
|
331 |
+ |
|
332 |
+ |
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 |
+ |
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 |
+ |
} |
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 |
|
} |