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