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 HMARGIN (.5*72) /* horizontal margin */ |
18 |
|
#define VMARGIN (.5*72) /* vertical margin */ |
19 |
|
#define PWIDTH (8.5*72-2*HMARGIN) /* width of device */ |
20 |
|
#define PHEIGHT (11*72-2*VMARGIN) /* height of device */ |
21 |
|
|
22 |
+ |
#define RUNCHR '*' /* character to start rle */ |
23 |
+ |
#define MINRUN 4 /* minimum run-length */ |
24 |
+ |
#define RSTRT '!' /* character for MINRUN */ |
25 |
+ |
#define MAXRUN (MINRUN+'~'-RSTRT) /* maximum run-length */ |
26 |
+ |
|
27 |
|
char code[] = /* 6-bit code lookup table */ |
28 |
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+"; |
29 |
|
|
30 |
|
int wrongformat = 0; /* input in wrong format? */ |
31 |
< |
double pixaspect = 1.0; /* pixel aspect ratio */ |
31 |
> |
double pixaspect = 1.0; /* pixel aspect ratio */ |
32 |
|
|
33 |
|
int bradj = 0; /* brightness adjustment */ |
34 |
|
int ncopies = 1; /* number of copies */ |
37 |
|
|
38 |
|
int xmax, ymax; |
39 |
|
|
40 |
+ |
extern char *malloc(); |
41 |
|
|
42 |
+ |
|
43 |
|
headline(s) /* check header line */ |
44 |
|
char *s; |
45 |
|
{ |
86 |
|
exit(1); |
87 |
|
} |
88 |
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
89 |
< |
fprintf(stderr, "can't open output \"%s\"\n", |
89 |
> |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
90 |
|
progname, argv[i+1]); |
91 |
|
exit(1); |
92 |
|
} |
93 |
+ |
#ifdef MSDOS |
94 |
+ |
setmode(fileno(stdin), O_BINARY); |
95 |
+ |
#endif |
96 |
|
/* get our header */ |
97 |
|
getheader(stdin, headline, NULL); |
98 |
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
125 |
|
char *name; |
126 |
|
{ |
127 |
|
int landscape = 0; |
128 |
< |
double pwidth, pheight; |
129 |
< |
double iwidth, iheight; |
128 |
> |
double pwidth, pheight; |
129 |
> |
double iwidth, iheight; |
130 |
|
|
131 |
|
printf("%%!\n"); |
132 |
|
printf("%%%%Title: %s\n", name); |
137 |
|
else |
138 |
|
printf("%%%%Portrait\n"); |
139 |
|
printf("%%%%EndComments\n"); |
128 |
– |
printf("gsave\n"); |
140 |
|
printf("64 dict begin\n"); |
141 |
|
/* define image reader */ |
142 |
< |
PSprocdef("read6bit"); |
142 |
> |
PSprocdef("read6bitRLE"); |
143 |
|
/* set up transformation matrix */ |
144 |
|
printf("%f %f translate\n", HMARGIN, VMARGIN); |
145 |
|
if (PWIDTH > PHEIGHT ^ landscape) { |
162 |
|
printf("%f %f scale\n", iwidth, iheight); |
163 |
|
printf("%%%%EndProlog\n"); |
164 |
|
/* start image procedure */ |
165 |
< |
printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax, |
165 |
> |
printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bitRLE} image\n", xmax, ymax, |
166 |
|
xmax, -ymax, ymax); |
167 |
|
} |
168 |
|
|
183 |
|
{ |
184 |
|
short itab[128]; |
185 |
|
register int i; |
186 |
< |
|
187 |
< |
for (i = 0; i < 128; i++) |
186 |
> |
/* assign code values */ |
187 |
> |
for (i = 0; i < 128; i++) /* clear */ |
188 |
|
itab[i] = -1; |
189 |
< |
for (i = 0; i < 64; i++) |
189 |
> |
for (i = 1; i < 63; i++) /* assign greys */ |
190 |
|
itab[code[i]] = i<<2 | 2; |
191 |
< |
printf("/decode ["); |
191 |
> |
itab[code[0]] = 0; /* black is black */ |
192 |
> |
itab[code[63]] = 255; /* and white is white */ |
193 |
> |
printf("/codetab ["); |
194 |
|
for (i = 0; i < 128; i++) { |
195 |
|
if (!(i & 0xf)) |
196 |
|
putchar('\n'); |
198 |
|
} |
199 |
|
printf("\n] def\n"); |
200 |
|
printf("/scanline %d string def\n", xmax); |
201 |
+ |
printf("/nrept 0 def\n"); |
202 |
+ |
printf("/readbyte { currentfile read not {stop} if } def\n"); |
203 |
+ |
printf("/decode { codetab exch get } def\n"); |
204 |
|
printf("/%s {\n", nam); |
205 |
|
printf("\t{ 0 1 %d { scanline exch\n", xmax-1); |
206 |
< |
printf("\t\t{ decode currentfile read not {stop} if get\n"); |
207 |
< |
printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n"); |
208 |
< |
printf("} stopped {pop pop pop 0 string} {scanline} ifelse } def\n"); |
206 |
> |
printf("\t\tnrept 0 le\n"); |
207 |
> |
printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR); |
208 |
> |
printf("\t\t\t\t\t{ pop /nrept readbyte %d sub def\n", RSTRT-MINRUN+1); |
209 |
> |
printf("\t\t\t\t\t\t/reptv readbyte decode def\n"); |
210 |
> |
printf("\t\t\t\t\t\treptv exit }\n"); |
211 |
> |
printf("\t\t\t\t\t{ decode dup 0 lt {pop} {exit} ifelse }\n"); |
212 |
> |
printf("\t\t\t\tifelse } loop }\n"); |
213 |
> |
printf("\t\t\t{ /nrept nrept 1 sub def reptv }\n"); |
214 |
> |
printf("\t\tifelse put\n"); |
215 |
> |
printf("\t\t} for\n"); |
216 |
> |
printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n"); |
217 |
> |
printf("} bind def\n"); |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
ra2ps() /* convert Radiance scanlines to 6-bit */ |
222 |
|
{ |
223 |
< |
COLR *scanin; |
199 |
< |
register int col = 0; |
223 |
> |
register COLR *scanin; |
224 |
|
register int c; |
225 |
+ |
int lastc, cnt; |
226 |
|
register int x; |
227 |
|
int y; |
228 |
|
/* allocate scanline */ |
233 |
|
for (y = ymax-1; y >= 0; y--) { |
234 |
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
235 |
|
quiterr("error reading Radiance picture"); |
236 |
< |
normcolrs(scanin, xmax, bradj); /* normalize */ |
236 |
> |
normcolrs(scanin, xmax, bradj); /* normalize */ |
237 |
> |
lastc = -1; cnt = 0; |
238 |
|
for (x = 0; x < xmax; x++) { |
239 |
< |
if (!(col++ & 0x3f)) |
214 |
< |
putchar('\n'); |
215 |
< |
c = normbright(scanin[x]) + (random()&3); |
239 |
> |
c = normbright(scanin[x]) + 2; |
240 |
|
if (c > 255) c = 255; |
241 |
< |
putchar(code[c>>2]); |
241 |
> |
c = code[c>>2]; |
242 |
> |
if (c == lastc && cnt < MAXRUN) |
243 |
> |
cnt++; |
244 |
> |
else { |
245 |
> |
putrle(cnt, lastc); |
246 |
> |
lastc = c; |
247 |
> |
cnt = 1; |
248 |
> |
} |
249 |
|
} |
250 |
+ |
putrle(cnt, lastc); |
251 |
|
if (ferror(stdout)) |
252 |
|
quiterr("error writing PostScript file"); |
253 |
|
} |
254 |
|
putchar('\n'); |
255 |
|
/* free scanline */ |
256 |
|
free((char *)scanin); |
257 |
+ |
} |
258 |
+ |
|
259 |
+ |
|
260 |
+ |
putrle(cnt, cod) /* put out cnt of cod */ |
261 |
+ |
register int cnt, cod; |
262 |
+ |
{ |
263 |
+ |
static int col = 0; |
264 |
+ |
|
265 |
+ |
if (cnt >= MINRUN) { |
266 |
+ |
col += 3; |
267 |
+ |
putchar(RUNCHR); |
268 |
+ |
putchar(RSTRT-MINRUN+cnt); |
269 |
+ |
putchar(cod); |
270 |
+ |
} else { |
271 |
+ |
col += cnt; |
272 |
+ |
while (cnt-- > 0) |
273 |
+ |
putchar(cod); |
274 |
+ |
} |
275 |
+ |
if (col >= 72) { |
276 |
+ |
putchar('\n'); |
277 |
+ |
col = 0; |
278 |
+ |
} |
279 |
|
} |