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 GAMVAL 1.5 /* 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 */ |
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 |
|
char *progname; |
43 |
|
|
44 |
|
int xmax, ymax; |
45 |
|
|
46 |
+ |
extern char *malloc(); |
47 |
|
|
48 |
+ |
|
49 |
|
headline(s) /* check header line */ |
50 |
|
char *s; |
51 |
|
{ |
70 |
|
for (i = 1; i < argc; i++) |
71 |
|
if (argv[i][0] == '-') |
72 |
|
switch (argv[i][1]) { |
73 |
+ |
case 'c': /* produce color PostScript */ |
74 |
+ |
docolor++; |
75 |
+ |
break; |
76 |
|
case 'e': /* exposure adjustment */ |
77 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
78 |
|
goto userr; |
79 |
|
bradj = atoi(argv[++i]); |
80 |
|
break; |
81 |
+ |
case 'n': /* number of copies */ |
82 |
+ |
ncopies = atoi(argv[++i]); |
83 |
+ |
break; |
84 |
|
default: |
85 |
|
goto userr; |
86 |
|
} |
95 |
|
exit(1); |
96 |
|
} |
97 |
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
98 |
< |
fprintf(stderr, "can't open output \"%s\"\n", |
98 |
> |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
99 |
|
progname, argv[i+1]); |
100 |
|
exit(1); |
101 |
|
} |
102 |
+ |
#ifdef MSDOS |
103 |
+ |
setmode(fileno(stdin), O_BINARY); |
104 |
+ |
#endif |
105 |
|
/* get our header */ |
106 |
|
getheader(stdin, headline, NULL); |
107 |
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
108 |
|
quiterr("bad picture format"); |
109 |
+ |
/* gamma compression */ |
110 |
+ |
setcolrgam(GAMVAL); |
111 |
|
/* write header */ |
112 |
|
PSheader(i <= argc-1 ? argv[i] : "<stdin>"); |
113 |
|
/* convert file */ |
116 |
|
PStrailer(); |
117 |
|
exit(0); |
118 |
|
userr: |
119 |
< |
fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname); |
119 |
> |
fprintf(stderr, "Usage: %s [-c][-e +/-stops] [input [output]]\n", |
120 |
> |
progname); |
121 |
|
exit(1); |
122 |
|
} |
123 |
|
|
137 |
|
char *name; |
138 |
|
{ |
139 |
|
int landscape = 0; |
140 |
< |
double pwidth, pheight; |
141 |
< |
double iwidth, iheight; |
142 |
< |
|
143 |
< |
printf("%%!\n"); |
140 |
> |
double pwidth, pheight; |
141 |
> |
double iwidth, iheight; |
142 |
> |
/* EPS comments */ |
143 |
> |
puts("%!PS-Adobe-2.0 EPSF-2.0"); |
144 |
|
printf("%%%%Title: %s\n", name); |
145 |
< |
printf("%%%%Creator: %s\n", progname); |
146 |
< |
printf("%%%%Pages: 1\n"); |
145 |
> |
printf("%%%%Creator: %s = %s\n", progname, SCCSid); |
146 |
> |
printf("%%%%Pages: %d\n", ncopies); |
147 |
|
if (landscape = xmax > pixaspect*ymax) |
148 |
< |
printf("%%%%Landscape\n"); |
148 |
> |
puts("%%Orientation: Landscape"); |
149 |
|
else |
150 |
< |
printf("%%%%Portrait\n"); |
123 |
< |
printf("%%%%EndComments\n"); |
124 |
< |
printf("gsave\n"); |
125 |
< |
printf("64 dict begin\n"); |
126 |
< |
/* define image reader */ |
127 |
< |
PSprocdef("read6bit"); |
128 |
< |
/* set up transformation matrix */ |
129 |
< |
printf("%f %f translate\n", HMARGIN, VMARGIN); |
150 |
> |
puts("%%Orientation: Portrait"); |
151 |
|
if (PWIDTH > PHEIGHT ^ landscape) { |
131 |
– |
printf("0 %f translate\n", PHEIGHT); |
132 |
– |
printf("-90 rotate\n"); |
152 |
|
pwidth = PHEIGHT; |
153 |
|
pheight = PWIDTH; |
154 |
|
} else { |
162 |
|
iheight = pheight; |
163 |
|
iwidth = pheight*xmax/(pixaspect*ymax); |
164 |
|
} |
165 |
+ |
if (pwidth == PHEIGHT) |
166 |
+ |
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", |
167 |
+ |
HMARGIN+(pheight-iheight)*.5, |
168 |
+ |
VMARGIN+(pwidth-iwidth)*.5, |
169 |
+ |
HMARGIN+(pheight-iheight)*.5+iheight, |
170 |
+ |
VMARGIN+(pwidth-iwidth)*.5+iwidth); |
171 |
+ |
else |
172 |
+ |
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", |
173 |
+ |
HMARGIN+(pwidth-iwidth)*.5, |
174 |
+ |
VMARGIN+(pheight-iheight)*.5, |
175 |
+ |
HMARGIN+(pwidth-iwidth)*.5+iwidth, |
176 |
+ |
VMARGIN+(pheight-iheight)*.5+iheight); |
177 |
+ |
puts("%%EndComments"); |
178 |
+ |
puts("gsave save"); |
179 |
+ |
puts("64 dict begin"); |
180 |
+ |
/* define image reader */ |
181 |
+ |
if (docolor) { |
182 |
+ |
printf("/redline %d string def\n", xmax); |
183 |
+ |
printf("/grnline %d string def\n", xmax); |
184 |
+ |
printf("/bluline %d string def\n", xmax); |
185 |
+ |
printf("/rgbline %d string def\n", 3*xmax); |
186 |
+ |
PSprocdef("read6bitRLE", "interleave"); |
187 |
+ |
} else { |
188 |
+ |
printf("/greyline %d string def\n", xmax); |
189 |
+ |
PSprocdef("read6bitRLE", NULL); |
190 |
+ |
} |
191 |
+ |
/* set up transformation matrix */ |
192 |
+ |
printf("%f %f translate\n", HMARGIN, VMARGIN); |
193 |
+ |
if (pwidth == PHEIGHT) { |
194 |
+ |
printf("0 %f translate\n", PHEIGHT); |
195 |
+ |
puts("-90 rotate"); |
196 |
+ |
} |
197 |
|
printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5); |
198 |
|
printf("%f %f scale\n", iwidth, iheight); |
199 |
< |
printf("%%%%EndProlog\n"); |
199 |
> |
puts("%%EndProlog"); |
200 |
|
/* start image procedure */ |
201 |
< |
printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax, |
202 |
< |
xmax, -ymax, ymax); |
201 |
> |
printf("%d %d 8 [%d 0 0 %d 0 %d] ", xmax, ymax, xmax, -ymax, ymax); |
202 |
> |
if (docolor) { |
203 |
> |
puts("{redline read6bitRLE grnline read6bitRLE"); |
204 |
> |
puts("\tbluline read6bitRLE rgbline interleave} false 3 colorimage"); |
205 |
> |
} else |
206 |
> |
puts("{greyline read6bitRLE} image"); |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
|
PStrailer() /* print PostScript trailer */ |
211 |
|
{ |
212 |
|
puts("%%Trailer"); |
213 |
< |
puts("end"); |
213 |
> |
if (ncopies > 1) |
214 |
> |
printf("/#copies %d def\n", ncopies); |
215 |
|
puts("showpage"); |
216 |
< |
puts("grestore"); |
216 |
> |
puts("end"); |
217 |
> |
puts("restore grestore"); |
218 |
|
puts("%%EOF"); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
< |
PSprocdef(nam) /* define PS procedure to read image */ |
223 |
< |
char *nam; |
222 |
> |
PSprocdef(nam, inam) /* define PS procedure to read image */ |
223 |
> |
char *nam, *inam; |
224 |
|
{ |
225 |
|
short itab[128]; |
226 |
|
register int i; |
227 |
< |
|
228 |
< |
for (i = 0; i < 128; i++) |
227 |
> |
/* assign code values */ |
228 |
> |
for (i = 0; i < 128; i++) /* clear */ |
229 |
|
itab[i] = -1; |
230 |
< |
for (i = 0; i < 64; i++) |
231 |
< |
itab[code[i]] = i<<2 | 2; |
232 |
< |
printf("/decode ["); |
230 |
> |
for (i = 1; i < 63; i++) /* assign greys */ |
231 |
> |
itab[code[i]] = 256.0*pow(((i<<2)+2.5)/256.0, GAMVAL); |
232 |
> |
itab[code[0]] = 0; /* black is black */ |
233 |
> |
itab[code[63]] = 255; /* and white is white */ |
234 |
> |
printf("/codetab ["); |
235 |
|
for (i = 0; i < 128; i++) { |
236 |
|
if (!(i & 0xf)) |
237 |
|
putchar('\n'); |
238 |
|
printf(" %3d", itab[i]); |
239 |
|
} |
240 |
|
printf("\n] def\n"); |
241 |
< |
printf("/scanline %d string def\n", xmax); |
242 |
< |
printf("/%s {\n", nam); |
241 |
> |
printf("/nrept 0 def\n"); |
242 |
> |
printf("/readbyte { currentfile read not {stop} if } bind def\n"); |
243 |
> |
printf("/decode { codetab exch get } bind def\n"); |
244 |
> |
printf("/%s {\t%% scanbuffer\n", nam); |
245 |
> |
printf("\t/scanline exch def\n"); |
246 |
|
printf("\t{ 0 1 %d { scanline exch\n", xmax-1); |
247 |
< |
printf("\t\t{ decode currentfile read not {stop} if get\n"); |
248 |
< |
printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n"); |
249 |
< |
printf("} stopped {pop pop pop 0 string} {scanline} ifelse } def\n"); |
247 |
> |
printf("\t\tnrept 0 le\n"); |
248 |
> |
printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR); |
249 |
> |
printf("\t\t\t\t\t{ pop /nrept readbyte %d sub def\n", RSTRT-MINRUN+1); |
250 |
> |
printf("\t\t\t\t\t\t/reptv readbyte decode def\n"); |
251 |
> |
printf("\t\t\t\t\t\treptv exit }\n"); |
252 |
> |
printf("\t\t\t\t\t{ decode dup 0 lt {pop} {exit} ifelse }\n"); |
253 |
> |
printf("\t\t\t\tifelse } loop }\n"); |
254 |
> |
printf("\t\t\t{ /nrept nrept 1 sub def reptv }\n"); |
255 |
> |
printf("\t\tifelse put\n"); |
256 |
> |
printf("\t\t} for\n"); |
257 |
> |
printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n"); |
258 |
> |
printf("} bind def\n"); |
259 |
> |
if (inam == NULL) |
260 |
> |
return; |
261 |
> |
/* define interleaving procedure */ |
262 |
> |
printf("/%s {\t%% redscn grnscn bluscn rgbscn\n", inam); |
263 |
> |
printf("\t/rgbscan exch def /bscan exch def\n"); |
264 |
> |
printf("\t/gscan exch def /rscan exch def\n"); |
265 |
> |
printf("\trscan length %d eq gscan length %d eq and ", xmax, xmax); |
266 |
> |
printf("bscan length %d eq and \n", xmax); |
267 |
> |
printf("\t{ 0 1 %d { /ndx exch def\n", xmax-1); |
268 |
> |
printf("\t\trgbscan ndx 3 mul rscan ndx get put\n"); |
269 |
> |
printf("\t\trgbscan ndx 3 mul 1 add gscan ndx get put\n"); |
270 |
> |
printf("\t\trgbscan ndx 3 mul 2 add bscan ndx get put\n"); |
271 |
> |
printf("\t\t} for rgbscan }\n"); |
272 |
> |
printf("\t{0 string} ifelse\n"); |
273 |
> |
printf("} bind def\n"); |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
|
ra2ps() /* convert Radiance scanlines to 6-bit */ |
278 |
|
{ |
279 |
< |
COLR *scanin; |
194 |
< |
register int col = 0; |
195 |
< |
register int c; |
196 |
< |
register int x; |
279 |
> |
register COLR *scanin; |
280 |
|
int y; |
281 |
|
/* allocate scanline */ |
282 |
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
286 |
|
for (y = ymax-1; y >= 0; y--) { |
287 |
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
288 |
|
quiterr("error reading Radiance picture"); |
289 |
< |
normcolrs(scanin, xmax, bradj); /* normalize */ |
290 |
< |
for (x = 0; x < xmax; x++) { |
291 |
< |
if (!(col++ & 0x3f)) |
292 |
< |
putchar('\n'); |
293 |
< |
c = normbright(scanin[x]) + (random()&3); |
294 |
< |
if (c > 255) c = 255; |
295 |
< |
putchar(code[c>>2]); |
296 |
< |
} |
289 |
> |
if (bradj) /* adjust exposure */ |
290 |
> |
shiftcolrs(scanin, xmax, bradj); |
291 |
> |
colrs_gambs(scanin, xmax); /* gamma compression */ |
292 |
> |
if (docolor) { |
293 |
> |
putprim(scanin, RED); |
294 |
> |
putprim(scanin, GRN); |
295 |
> |
putprim(scanin, BLU); |
296 |
> |
} else |
297 |
> |
putprim(scanin, GRY); |
298 |
|
if (ferror(stdout)) |
299 |
|
quiterr("error writing PostScript file"); |
300 |
|
} |
301 |
|
putchar('\n'); |
302 |
|
/* free scanline */ |
303 |
|
free((char *)scanin); |
304 |
+ |
} |
305 |
+ |
|
306 |
+ |
|
307 |
+ |
putprim(scn, pri) /* put out one primary from scanline */ |
308 |
+ |
COLR *scn; |
309 |
+ |
int pri; |
310 |
+ |
{ |
311 |
+ |
register int c; |
312 |
+ |
register int x; |
313 |
+ |
int lastc, cnt; |
314 |
+ |
|
315 |
+ |
lastc = -1; cnt = 0; |
316 |
+ |
for (x = 0; x < xmax; x++) { |
317 |
+ |
if (pri == GRY) |
318 |
+ |
c = normbright(scn[x]) + 2; |
319 |
+ |
else |
320 |
+ |
c = scn[x][pri] + 2; |
321 |
+ |
if (c > 255) c = 255; |
322 |
+ |
c = code[c>>2]; |
323 |
+ |
if (c == lastc && cnt < MAXRUN) |
324 |
+ |
cnt++; |
325 |
+ |
else { |
326 |
+ |
putrle(cnt, lastc); |
327 |
+ |
lastc = c; |
328 |
+ |
cnt = 1; |
329 |
+ |
} |
330 |
+ |
} |
331 |
+ |
putrle(cnt, lastc); |
332 |
+ |
} |
333 |
+ |
|
334 |
+ |
|
335 |
+ |
putrle(cnt, cod) /* put out cnt of cod */ |
336 |
+ |
register int cnt, cod; |
337 |
+ |
{ |
338 |
+ |
static int col = 0; |
339 |
+ |
|
340 |
+ |
if (cnt >= MINRUN) { |
341 |
+ |
col += 3; |
342 |
+ |
putchar(RUNCHR); |
343 |
+ |
putchar(RSTRT-MINRUN+cnt); |
344 |
+ |
putchar(cod); |
345 |
+ |
} else { |
346 |
+ |
col += cnt; |
347 |
+ |
while (cnt-- > 0) |
348 |
+ |
putchar(cod); |
349 |
+ |
} |
350 |
+ |
if (col >= 72) { |
351 |
+ |
putchar('\n'); |
352 |
+ |
col = 0; |
353 |
+ |
} |
354 |
|
} |