1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
9 |
|
*/ |
10 |
|
|
11 |
|
#include <stdio.h> |
12 |
+ |
#include <math.h> |
13 |
|
#ifdef MSDOS |
14 |
|
#include <fcntl.h> |
15 |
|
#endif |
16 |
|
#include "color.h" |
16 |
– |
#include "random.h" |
17 |
|
|
18 |
+ |
#define GAMVAL 1.47 /* 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 |
+ |
extern int Aputprim(), Bputprim(), Cputprim(); |
43 |
+ |
|
44 |
+ |
int (*putprim)() = Aputprim; /* function for writing scanline */ |
45 |
+ |
|
46 |
|
char *progname; |
47 |
|
|
48 |
|
int xmax, ymax; |
74 |
|
for (i = 1; i < argc; i++) |
75 |
|
if (argv[i][0] == '-') |
76 |
|
switch (argv[i][1]) { |
77 |
+ |
case 'b': /* produce b&w PostScript */ |
78 |
+ |
docolor = 0; |
79 |
+ |
break; |
80 |
+ |
case 'c': /* produce color PostScript */ |
81 |
+ |
docolor = 1; |
82 |
+ |
break; |
83 |
+ |
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 |
|
case 'e': /* exposure adjustment */ |
93 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
94 |
|
goto userr; |
122 |
|
getheader(stdin, headline, NULL); |
123 |
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
124 |
|
quiterr("bad picture format"); |
125 |
+ |
/* gamma compression */ |
126 |
+ |
if (putprim == Cputprim) |
127 |
+ |
setcolrgam(GAMVAL); |
128 |
|
/* write header */ |
129 |
|
PSheader(i <= argc-1 ? argv[i] : "<stdin>"); |
130 |
|
/* convert file */ |
133 |
|
PStrailer(); |
134 |
|
exit(0); |
135 |
|
userr: |
136 |
< |
fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname); |
136 |
> |
fprintf(stderr, "Usage: %s [-b|c][-A|B|C][-e +/-stops] [input [output]]\n", |
137 |
> |
progname); |
138 |
|
exit(1); |
139 |
|
} |
140 |
|
|
153 |
|
PSheader(name) /* print PostScript header */ |
154 |
|
char *name; |
155 |
|
{ |
156 |
+ |
char *rstr; |
157 |
|
int landscape = 0; |
158 |
|
double pwidth, pheight; |
159 |
|
double iwidth, iheight; |
160 |
< |
|
161 |
< |
printf("%%!\n"); |
160 |
> |
/* EPS comments */ |
161 |
> |
puts("%!PS-Adobe-2.0 EPSF-2.0"); |
162 |
|
printf("%%%%Title: %s\n", name); |
163 |
< |
printf("%%%%Creator: %s\n", progname); |
163 |
> |
printf("%%%%Creator: %s = %s\n", progname, SCCSid); |
164 |
|
printf("%%%%Pages: %d\n", ncopies); |
165 |
|
if (landscape = xmax > pixaspect*ymax) |
166 |
< |
printf("%%%%Landscape\n"); |
166 |
> |
puts("%%Orientation: Landscape"); |
167 |
|
else |
168 |
< |
printf("%%%%Portrait\n"); |
135 |
< |
printf("%%%%EndComments\n"); |
136 |
< |
printf("64 dict begin\n"); |
137 |
< |
/* define image reader */ |
138 |
< |
PSprocdef("read6bit"); |
139 |
< |
/* set up transformation matrix */ |
140 |
< |
printf("%f %f translate\n", HMARGIN, VMARGIN); |
168 |
> |
puts("%%Orientation: Portrait"); |
169 |
|
if (PWIDTH > PHEIGHT ^ landscape) { |
142 |
– |
printf("0 %f translate\n", PHEIGHT); |
143 |
– |
printf("-90 rotate\n"); |
170 |
|
pwidth = PHEIGHT; |
171 |
|
pheight = PWIDTH; |
172 |
|
} else { |
180 |
|
iheight = pheight; |
181 |
|
iwidth = pheight*xmax/(pixaspect*ymax); |
182 |
|
} |
183 |
+ |
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 |
+ |
puts("gsave save"); |
197 |
+ |
puts("17 dict begin"); |
198 |
+ |
/* define image reader */ |
199 |
+ |
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 |
+ |
} else |
204 |
+ |
printf("/gryline %d string def\n", xmax); |
205 |
+ |
/* use compressed encoding? */ |
206 |
+ |
if (putprim == Cputprim) |
207 |
+ |
PSprocdef("read6bitRLE"); |
208 |
+ |
/* set up transformation matrix */ |
209 |
+ |
printf("%f %f translate\n", HMARGIN, VMARGIN); |
210 |
+ |
if (pwidth == PHEIGHT) { |
211 |
+ |
printf("%f 0 translate\n", PWIDTH); |
212 |
+ |
puts("90 rotate"); |
213 |
+ |
} |
214 |
|
printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5); |
215 |
|
printf("%f %f scale\n", iwidth, iheight); |
216 |
< |
printf("%%%%EndProlog\n"); |
216 |
> |
puts("%%EndProlog"); |
217 |
|
/* start image procedure */ |
218 |
< |
printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax, |
219 |
< |
xmax, -ymax, ymax); |
218 |
> |
printf("%d %d 8 [%d 0 0 %d 0 %d]\n", xmax, ymax, xmax, -ymax, ymax); |
219 |
> |
if (putprim == Cputprim) { |
220 |
> |
if (docolor) { |
221 |
> |
puts("{redline read6bitRLE grnline read6bitRLE"); |
222 |
> |
puts("bluline read6bitRLE} true 3 colorimage"); |
223 |
> |
} else |
224 |
> |
puts("{gryline read6bitRLE} image"); |
225 |
> |
} else { |
226 |
> |
rstr = putprim==Aputprim ? "readhexstring" : "readstring"; |
227 |
> |
if (docolor) { |
228 |
> |
printf("{currentfile redline %s pop}\n", rstr); |
229 |
> |
printf("{currentfile grnline %s pop}\n", rstr); |
230 |
> |
printf("{currentfile bluline %s pop}\n", rstr); |
231 |
> |
puts("true 3 colorimage"); |
232 |
> |
} else |
233 |
> |
printf("{currentfile gryline %s pop} image\n", rstr); |
234 |
> |
} |
235 |
|
} |
236 |
|
|
237 |
|
|
242 |
|
printf("/#copies %d def\n", ncopies); |
243 |
|
puts("showpage"); |
244 |
|
puts("end"); |
245 |
+ |
puts("restore grestore"); |
246 |
|
puts("%%EOF"); |
247 |
|
} |
248 |
|
|
256 |
|
for (i = 0; i < 128; i++) /* clear */ |
257 |
|
itab[i] = -1; |
258 |
|
for (i = 1; i < 63; i++) /* assign greys */ |
259 |
< |
itab[code[i]] = i<<2 | 2; |
259 |
> |
itab[code[i]] = 256.0*pow((i+.5)/64.0, GAMVAL); |
260 |
|
itab[code[0]] = 0; /* black is black */ |
261 |
|
itab[code[63]] = 255; /* and white is white */ |
262 |
< |
printf("/decode ["); |
262 |
> |
printf("/codetab ["); |
263 |
|
for (i = 0; i < 128; i++) { |
264 |
|
if (!(i & 0xf)) |
265 |
|
putchar('\n'); |
266 |
|
printf(" %3d", itab[i]); |
267 |
|
} |
268 |
|
printf("\n] def\n"); |
269 |
< |
printf("/scanline %d string def\n", xmax); |
270 |
< |
printf("/%s {\n", nam); |
269 |
> |
printf("/nrept 0 def\n"); |
270 |
> |
printf("/readbyte { currentfile read not {stop} if } bind def\n"); |
271 |
> |
printf("/decode { codetab exch get } bind def\n"); |
272 |
> |
printf("/%s {\t%% scanbuffer\n", nam); |
273 |
> |
printf("\t/scanline exch def\n"); |
274 |
|
printf("\t{ 0 1 %d { scanline exch\n", xmax-1); |
275 |
< |
printf("\t\t{ decode currentfile read not {stop} if get\n"); |
276 |
< |
printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n"); |
277 |
< |
printf("} stopped {pop pop pop 0 string} {scanline} ifelse } def\n"); |
275 |
> |
printf("\t\tnrept 0 le\n"); |
276 |
> |
printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR); |
277 |
> |
printf("\t\t\t\t\t{ pop /nrept readbyte %d sub def\n", RSTRT-MINRUN+1); |
278 |
> |
printf("\t\t\t\t\t\t/reptv readbyte decode def\n"); |
279 |
> |
printf("\t\t\t\t\t\treptv exit }\n"); |
280 |
> |
printf("\t\t\t\t\t{ decode dup 0 lt {pop} {exit} ifelse }\n"); |
281 |
> |
printf("\t\t\t\tifelse } loop }\n"); |
282 |
> |
printf("\t\t\t{ /nrept nrept 1 sub def reptv }\n"); |
283 |
> |
printf("\t\tifelse put\n"); |
284 |
> |
printf("\t\t} for\n"); |
285 |
> |
printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n"); |
286 |
> |
printf("} bind def\n"); |
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
|
ra2ps() /* convert Radiance scanlines to 6-bit */ |
291 |
|
{ |
292 |
< |
COLR *scanin; |
208 |
< |
register int col = 0; |
209 |
< |
register int c; |
210 |
< |
register int x; |
292 |
> |
register COLR *scanin; |
293 |
|
int y; |
294 |
|
/* allocate scanline */ |
295 |
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
299 |
|
for (y = ymax-1; y >= 0; y--) { |
300 |
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
301 |
|
quiterr("error reading Radiance picture"); |
302 |
< |
normcolrs(scanin, xmax, bradj); /* normalize */ |
303 |
< |
for (x = 0; x < xmax; x++) { |
304 |
< |
if (!(col++ & 0x3f)) |
305 |
< |
putchar('\n'); |
306 |
< |
c = normbright(scanin[x]) + (random()&3); |
307 |
< |
if (c > 255) c = 255; |
308 |
< |
putchar(code[c>>2]); |
309 |
< |
} |
302 |
> |
if (putprim == Cputprim) { |
303 |
> |
if (bradj) /* adjust exposure */ |
304 |
> |
shiftcolrs(scanin, xmax, bradj); |
305 |
> |
colrs_gambs(scanin, xmax); /* gamma compression */ |
306 |
> |
} else |
307 |
> |
normcolrs(scanin, xmax, bradj); |
308 |
> |
if (docolor) { |
309 |
> |
(*putprim)(scanin, RED); |
310 |
> |
(*putprim)(scanin, GRN); |
311 |
> |
(*putprim)(scanin, BLU); |
312 |
> |
} else |
313 |
> |
(*putprim)(scanin, GRY); |
314 |
|
if (ferror(stdout)) |
315 |
|
quiterr("error writing PostScript file"); |
316 |
|
} |
317 |
|
putchar('\n'); |
318 |
|
/* free scanline */ |
319 |
|
free((char *)scanin); |
320 |
+ |
} |
321 |
+ |
|
322 |
+ |
|
323 |
+ |
int |
324 |
+ |
Aputprim(scn, pri) /* put out hex ASCII primary from scanline */ |
325 |
+ |
COLR *scn; |
326 |
+ |
int pri; |
327 |
+ |
{ |
328 |
+ |
static char hexdigit[] = "0123456789ABCDEF"; |
329 |
+ |
static int col = 0; |
330 |
+ |
register int x, c; |
331 |
+ |
|
332 |
+ |
for (x = 0; x < xmax; x++) { |
333 |
+ |
if (pri == GRY) |
334 |
+ |
c = normbright(scn[x]); |
335 |
+ |
else |
336 |
+ |
c = scn[x][pri]; |
337 |
+ |
if (c > 255) c = 255; |
338 |
+ |
putchar(hexdigit[c>>4]); |
339 |
+ |
putchar(hexdigit[c&0xf]); |
340 |
+ |
if ((col += 2) >= 72) { |
341 |
+ |
putchar('\n'); |
342 |
+ |
col = 0; |
343 |
+ |
} |
344 |
+ |
} |
345 |
+ |
} |
346 |
+ |
|
347 |
+ |
|
348 |
+ |
int |
349 |
+ |
Bputprim(scn, pri) /* put out binary primary from scanline */ |
350 |
+ |
COLR *scn; |
351 |
+ |
int pri; |
352 |
+ |
{ |
353 |
+ |
register int x, c; |
354 |
+ |
|
355 |
+ |
for (x = 0; x < xmax; x++) { |
356 |
+ |
if (pri == GRY) |
357 |
+ |
c = normbright(scn[x]); |
358 |
+ |
else |
359 |
+ |
c = scn[x][pri]; |
360 |
+ |
if (c > 255) c = 255; |
361 |
+ |
putchar(c); |
362 |
+ |
} |
363 |
+ |
} |
364 |
+ |
|
365 |
+ |
|
366 |
+ |
int |
367 |
+ |
Cputprim(scn, pri) /* put out compressed primary from scanline */ |
368 |
+ |
COLR *scn; |
369 |
+ |
int pri; |
370 |
+ |
{ |
371 |
+ |
register int c; |
372 |
+ |
register int x; |
373 |
+ |
int lastc, cnt; |
374 |
+ |
|
375 |
+ |
lastc = -1; cnt = 0; |
376 |
+ |
for (x = 0; x < xmax; x++) { |
377 |
+ |
if (pri == GRY) |
378 |
+ |
c = normbright(scn[x]) + 2; |
379 |
+ |
else |
380 |
+ |
c = scn[x][pri] + 2; |
381 |
+ |
if (c > 255) c = 255; |
382 |
+ |
c = code[c>>2]; |
383 |
+ |
if (c == lastc && cnt < MAXRUN) |
384 |
+ |
cnt++; |
385 |
+ |
else { |
386 |
+ |
putrle(cnt, lastc); |
387 |
+ |
lastc = c; |
388 |
+ |
cnt = 1; |
389 |
+ |
} |
390 |
+ |
} |
391 |
+ |
putrle(cnt, lastc); |
392 |
+ |
} |
393 |
+ |
|
394 |
+ |
|
395 |
+ |
putrle(cnt, cod) /* put out cnt of cod */ |
396 |
+ |
register int cnt, cod; |
397 |
+ |
{ |
398 |
+ |
static int col = 0; |
399 |
+ |
|
400 |
+ |
if (cnt >= MINRUN) { |
401 |
+ |
col += 3; |
402 |
+ |
putchar(RUNCHR); |
403 |
+ |
putchar(RSTRT-MINRUN+cnt); |
404 |
+ |
putchar(cod); |
405 |
+ |
} else { |
406 |
+ |
col += cnt; |
407 |
+ |
while (cnt-- > 0) |
408 |
+ |
putchar(cod); |
409 |
+ |
} |
410 |
+ |
if (col >= 72) { |
411 |
+ |
putchar('\n'); |
412 |
+ |
col = 0; |
413 |
+ |
} |
414 |
|
} |