| 1 |
greg |
2.1 |
/* Copyright (c) 1992 Regents of the University of California */ |
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* Radiance picture to PostScript file translator -- one way! |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
#include <stdio.h> |
| 12 |
greg |
2.6 |
#ifdef MSDOS |
| 13 |
|
|
#include <fcntl.h> |
| 14 |
|
|
#endif |
| 15 |
greg |
2.1 |
#include "color.h" |
| 16 |
|
|
|
| 17 |
greg |
2.11 |
#define GRY -1 /* artificial index for grey */ |
| 18 |
|
|
|
| 19 |
greg |
2.1 |
#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 |
greg |
2.9 |
#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 |
greg |
2.1 |
char code[] = /* 6-bit code lookup table */ |
| 30 |
|
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+"; |
| 31 |
|
|
|
| 32 |
|
|
int wrongformat = 0; /* input in wrong format? */ |
| 33 |
greg |
2.6 |
double pixaspect = 1.0; /* pixel aspect ratio */ |
| 34 |
greg |
2.1 |
|
| 35 |
greg |
2.11 |
int docolor = 0; /* produce color image? */ |
| 36 |
greg |
2.1 |
int bradj = 0; /* brightness adjustment */ |
| 37 |
greg |
2.4 |
int ncopies = 1; /* number of copies */ |
| 38 |
greg |
2.1 |
|
| 39 |
|
|
char *progname; |
| 40 |
|
|
|
| 41 |
|
|
int xmax, ymax; |
| 42 |
|
|
|
| 43 |
greg |
2.6 |
extern char *malloc(); |
| 44 |
greg |
2.1 |
|
| 45 |
greg |
2.6 |
|
| 46 |
greg |
2.1 |
headline(s) /* check header line */ |
| 47 |
|
|
char *s; |
| 48 |
|
|
{ |
| 49 |
|
|
char fmt[32]; |
| 50 |
|
|
|
| 51 |
|
|
if (isformat(s)) { |
| 52 |
|
|
formatval(fmt, s); |
| 53 |
|
|
wrongformat = strcmp(fmt, COLRFMT); |
| 54 |
|
|
} else if (isaspect(s)) |
| 55 |
|
|
pixaspect *= aspectval(s); |
| 56 |
|
|
} |
| 57 |
|
|
|
| 58 |
|
|
|
| 59 |
|
|
main(argc, argv) |
| 60 |
|
|
int argc; |
| 61 |
|
|
char *argv[]; |
| 62 |
|
|
{ |
| 63 |
|
|
int i; |
| 64 |
|
|
|
| 65 |
|
|
progname = argv[0]; |
| 66 |
|
|
|
| 67 |
|
|
for (i = 1; i < argc; i++) |
| 68 |
|
|
if (argv[i][0] == '-') |
| 69 |
|
|
switch (argv[i][1]) { |
| 70 |
greg |
2.11 |
case 'c': /* produce color PostScript */ |
| 71 |
|
|
docolor++; |
| 72 |
|
|
break; |
| 73 |
greg |
2.1 |
case 'e': /* exposure adjustment */ |
| 74 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
| 75 |
|
|
goto userr; |
| 76 |
|
|
bradj = atoi(argv[++i]); |
| 77 |
|
|
break; |
| 78 |
greg |
2.4 |
case 'n': /* number of copies */ |
| 79 |
|
|
ncopies = atoi(argv[++i]); |
| 80 |
|
|
break; |
| 81 |
greg |
2.1 |
default: |
| 82 |
|
|
goto userr; |
| 83 |
|
|
} |
| 84 |
|
|
else |
| 85 |
|
|
break; |
| 86 |
|
|
|
| 87 |
|
|
if (i < argc-2) |
| 88 |
|
|
goto userr; |
| 89 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
| 90 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n", |
| 91 |
|
|
progname, argv[i]); |
| 92 |
|
|
exit(1); |
| 93 |
|
|
} |
| 94 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
| 95 |
greg |
2.7 |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
| 96 |
greg |
2.1 |
progname, argv[i+1]); |
| 97 |
|
|
exit(1); |
| 98 |
|
|
} |
| 99 |
greg |
2.6 |
#ifdef MSDOS |
| 100 |
|
|
setmode(fileno(stdin), O_BINARY); |
| 101 |
|
|
#endif |
| 102 |
greg |
2.1 |
/* get our header */ |
| 103 |
|
|
getheader(stdin, headline, NULL); |
| 104 |
|
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
| 105 |
|
|
quiterr("bad picture format"); |
| 106 |
|
|
/* write header */ |
| 107 |
|
|
PSheader(i <= argc-1 ? argv[i] : "<stdin>"); |
| 108 |
|
|
/* convert file */ |
| 109 |
|
|
ra2ps(); |
| 110 |
|
|
/* write trailer */ |
| 111 |
|
|
PStrailer(); |
| 112 |
|
|
exit(0); |
| 113 |
|
|
userr: |
| 114 |
greg |
2.11 |
fprintf(stderr, "Usage: %s [-c][-e +/-stops] [input [output]]\n", |
| 115 |
|
|
progname); |
| 116 |
greg |
2.1 |
exit(1); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
|
| 120 |
|
|
quiterr(err) /* print message and exit */ |
| 121 |
|
|
char *err; |
| 122 |
|
|
{ |
| 123 |
|
|
if (err != NULL) { |
| 124 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
| 125 |
|
|
exit(1); |
| 126 |
|
|
} |
| 127 |
|
|
exit(0); |
| 128 |
|
|
} |
| 129 |
|
|
|
| 130 |
|
|
|
| 131 |
|
|
PSheader(name) /* print PostScript header */ |
| 132 |
|
|
char *name; |
| 133 |
|
|
{ |
| 134 |
|
|
int landscape = 0; |
| 135 |
greg |
2.6 |
double pwidth, pheight; |
| 136 |
|
|
double iwidth, iheight; |
| 137 |
greg |
2.10 |
/* EPS comments */ |
| 138 |
greg |
2.11 |
puts("%!PS-Adobe-2.0 EPSF-2.0"); |
| 139 |
greg |
2.1 |
printf("%%%%Title: %s\n", name); |
| 140 |
greg |
2.10 |
printf("%%%%Creator: %s = %s\n", progname, SCCSid); |
| 141 |
greg |
2.4 |
printf("%%%%Pages: %d\n", ncopies); |
| 142 |
greg |
2.1 |
if (landscape = xmax > pixaspect*ymax) |
| 143 |
greg |
2.10 |
puts("%%Orientation: Landscape"); |
| 144 |
greg |
2.1 |
else |
| 145 |
greg |
2.10 |
puts("%%Orientation: Portrait"); |
| 146 |
greg |
2.1 |
if (PWIDTH > PHEIGHT ^ landscape) { |
| 147 |
|
|
pwidth = PHEIGHT; |
| 148 |
|
|
pheight = PWIDTH; |
| 149 |
|
|
} else { |
| 150 |
|
|
pwidth = PWIDTH; |
| 151 |
|
|
pheight = PHEIGHT; |
| 152 |
|
|
} |
| 153 |
|
|
if (pheight/pwidth > pixaspect*ymax/xmax) { |
| 154 |
greg |
2.2 |
iwidth = pwidth; |
| 155 |
|
|
iheight = pwidth*pixaspect*ymax/xmax; |
| 156 |
greg |
2.1 |
} else { |
| 157 |
greg |
2.2 |
iheight = pheight; |
| 158 |
|
|
iwidth = pheight*xmax/(pixaspect*ymax); |
| 159 |
greg |
2.1 |
} |
| 160 |
greg |
2.10 |
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 |
greg |
2.11 |
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 |
greg |
2.10 |
/* 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 |
greg |
2.2 |
printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5); |
| 193 |
|
|
printf("%f %f scale\n", iwidth, iheight); |
| 194 |
greg |
2.10 |
puts("%%%%EndProlog"); |
| 195 |
greg |
2.3 |
/* start image procedure */ |
| 196 |
greg |
2.11 |
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 |
greg |
2.1 |
} |
| 203 |
|
|
|
| 204 |
|
|
|
| 205 |
|
|
PStrailer() /* print PostScript trailer */ |
| 206 |
|
|
{ |
| 207 |
|
|
puts("%%Trailer"); |
| 208 |
greg |
2.4 |
if (ncopies > 1) |
| 209 |
|
|
printf("/#copies %d def\n", ncopies); |
| 210 |
|
|
puts("showpage"); |
| 211 |
greg |
2.1 |
puts("end"); |
| 212 |
greg |
2.10 |
puts("restore"); |
| 213 |
greg |
2.1 |
puts("%%EOF"); |
| 214 |
|
|
} |
| 215 |
|
|
|
| 216 |
|
|
|
| 217 |
greg |
2.11 |
PSprocdef(nam, inam) /* define PS procedure to read image */ |
| 218 |
|
|
char *nam, *inam; |
| 219 |
greg |
2.1 |
{ |
| 220 |
|
|
short itab[128]; |
| 221 |
|
|
register int i; |
| 222 |
greg |
2.5 |
/* assign code values */ |
| 223 |
|
|
for (i = 0; i < 128; i++) /* clear */ |
| 224 |
greg |
2.1 |
itab[i] = -1; |
| 225 |
greg |
2.5 |
for (i = 1; i < 63; i++) /* assign greys */ |
| 226 |
greg |
2.1 |
itab[code[i]] = i<<2 | 2; |
| 227 |
greg |
2.5 |
itab[code[0]] = 0; /* black is black */ |
| 228 |
|
|
itab[code[63]] = 255; /* and white is white */ |
| 229 |
greg |
2.9 |
printf("/codetab ["); |
| 230 |
greg |
2.1 |
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 |
greg |
2.9 |
printf("/nrept 0 def\n"); |
| 237 |
greg |
2.11 |
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 |
greg |
2.1 |
printf("\t{ 0 1 %d { scanline exch\n", xmax-1); |
| 242 |
greg |
2.9 |
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 |
greg |
2.8 |
printf("} bind def\n"); |
| 254 |
greg |
2.11 |
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 |
greg |
2.1 |
} |
| 270 |
|
|
|
| 271 |
|
|
|
| 272 |
|
|
ra2ps() /* convert Radiance scanlines to 6-bit */ |
| 273 |
|
|
{ |
| 274 |
greg |
2.9 |
register COLR *scanin; |
| 275 |
greg |
2.1 |
int y; |
| 276 |
|
|
/* allocate scanline */ |
| 277 |
|
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
| 278 |
|
|
if (scanin == NULL) |
| 279 |
|
|
quiterr("out of memory in ra2ps"); |
| 280 |
|
|
/* convert image */ |
| 281 |
|
|
for (y = ymax-1; y >= 0; y--) { |
| 282 |
|
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
| 283 |
|
|
quiterr("error reading Radiance picture"); |
| 284 |
greg |
2.6 |
normcolrs(scanin, xmax, bradj); /* normalize */ |
| 285 |
greg |
2.11 |
if (docolor) { |
| 286 |
|
|
putprim(scanin, RED); |
| 287 |
|
|
putprim(scanin, GRN); |
| 288 |
|
|
putprim(scanin, BLU); |
| 289 |
|
|
} else |
| 290 |
|
|
putprim(scanin, GRY); |
| 291 |
greg |
2.1 |
if (ferror(stdout)) |
| 292 |
|
|
quiterr("error writing PostScript file"); |
| 293 |
|
|
} |
| 294 |
|
|
putchar('\n'); |
| 295 |
|
|
/* free scanline */ |
| 296 |
|
|
free((char *)scanin); |
| 297 |
greg |
2.11 |
} |
| 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 |
greg |
2.9 |
} |
| 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 |
greg |
2.1 |
} |