| 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 |
|
|
#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 |
greg |
2.9 |
#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 |
greg |
2.1 |
char code[] = /* 6-bit code lookup table */
|
| 28 |
|
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+";
|
| 29 |
|
|
|
| 30 |
|
|
int wrongformat = 0; /* input in wrong format? */
|
| 31 |
greg |
2.6 |
double pixaspect = 1.0; /* pixel aspect ratio */
|
| 32 |
greg |
2.1 |
|
| 33 |
|
|
int bradj = 0; /* brightness adjustment */
|
| 34 |
greg |
2.4 |
int ncopies = 1; /* number of copies */
|
| 35 |
greg |
2.1 |
|
| 36 |
|
|
char *progname;
|
| 37 |
|
|
|
| 38 |
|
|
int xmax, ymax;
|
| 39 |
|
|
|
| 40 |
greg |
2.6 |
extern char *malloc();
|
| 41 |
greg |
2.1 |
|
| 42 |
greg |
2.6 |
|
| 43 |
greg |
2.1 |
headline(s) /* check header line */
|
| 44 |
|
|
char *s;
|
| 45 |
|
|
{
|
| 46 |
|
|
char fmt[32];
|
| 47 |
|
|
|
| 48 |
|
|
if (isformat(s)) {
|
| 49 |
|
|
formatval(fmt, s);
|
| 50 |
|
|
wrongformat = strcmp(fmt, COLRFMT);
|
| 51 |
|
|
} else if (isaspect(s))
|
| 52 |
|
|
pixaspect *= aspectval(s);
|
| 53 |
|
|
}
|
| 54 |
|
|
|
| 55 |
|
|
|
| 56 |
|
|
main(argc, argv)
|
| 57 |
|
|
int argc;
|
| 58 |
|
|
char *argv[];
|
| 59 |
|
|
{
|
| 60 |
|
|
int i;
|
| 61 |
|
|
|
| 62 |
|
|
progname = argv[0];
|
| 63 |
|
|
|
| 64 |
|
|
for (i = 1; i < argc; i++)
|
| 65 |
|
|
if (argv[i][0] == '-')
|
| 66 |
|
|
switch (argv[i][1]) {
|
| 67 |
|
|
case 'e': /* exposure adjustment */
|
| 68 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
|
| 69 |
|
|
goto userr;
|
| 70 |
|
|
bradj = atoi(argv[++i]);
|
| 71 |
|
|
break;
|
| 72 |
greg |
2.4 |
case 'n': /* number of copies */
|
| 73 |
|
|
ncopies = atoi(argv[++i]);
|
| 74 |
|
|
break;
|
| 75 |
greg |
2.1 |
default:
|
| 76 |
|
|
goto userr;
|
| 77 |
|
|
}
|
| 78 |
|
|
else
|
| 79 |
|
|
break;
|
| 80 |
|
|
|
| 81 |
|
|
if (i < argc-2)
|
| 82 |
|
|
goto userr;
|
| 83 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
|
| 84 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n",
|
| 85 |
|
|
progname, argv[i]);
|
| 86 |
|
|
exit(1);
|
| 87 |
|
|
}
|
| 88 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
|
| 89 |
greg |
2.7 |
fprintf(stderr, "%s: can't open output \"%s\"\n",
|
| 90 |
greg |
2.1 |
progname, argv[i+1]);
|
| 91 |
|
|
exit(1);
|
| 92 |
|
|
}
|
| 93 |
greg |
2.6 |
#ifdef MSDOS
|
| 94 |
|
|
setmode(fileno(stdin), O_BINARY);
|
| 95 |
|
|
#endif
|
| 96 |
greg |
2.1 |
/* get our header */
|
| 97 |
|
|
getheader(stdin, headline, NULL);
|
| 98 |
|
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0)
|
| 99 |
|
|
quiterr("bad picture format");
|
| 100 |
|
|
/* write header */
|
| 101 |
|
|
PSheader(i <= argc-1 ? argv[i] : "<stdin>");
|
| 102 |
|
|
/* convert file */
|
| 103 |
|
|
ra2ps();
|
| 104 |
|
|
/* write trailer */
|
| 105 |
|
|
PStrailer();
|
| 106 |
|
|
exit(0);
|
| 107 |
|
|
userr:
|
| 108 |
|
|
fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname);
|
| 109 |
|
|
exit(1);
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
|
| 113 |
|
|
quiterr(err) /* print message and exit */
|
| 114 |
|
|
char *err;
|
| 115 |
|
|
{
|
| 116 |
|
|
if (err != NULL) {
|
| 117 |
|
|
fprintf(stderr, "%s: %s\n", progname, err);
|
| 118 |
|
|
exit(1);
|
| 119 |
|
|
}
|
| 120 |
|
|
exit(0);
|
| 121 |
|
|
}
|
| 122 |
|
|
|
| 123 |
|
|
|
| 124 |
|
|
PSheader(name) /* print PostScript header */
|
| 125 |
|
|
char *name;
|
| 126 |
|
|
{
|
| 127 |
|
|
int landscape = 0;
|
| 128 |
greg |
2.6 |
double pwidth, pheight;
|
| 129 |
|
|
double iwidth, iheight;
|
| 130 |
greg |
2.10 |
/* EPS comments */
|
| 131 |
|
|
puts("%%!PS-Adobe-2.0 EPSF-2.0");
|
| 132 |
greg |
2.1 |
printf("%%%%Title: %s\n", name);
|
| 133 |
greg |
2.10 |
printf("%%%%Creator: %s = %s\n", progname, SCCSid);
|
| 134 |
greg |
2.4 |
printf("%%%%Pages: %d\n", ncopies);
|
| 135 |
greg |
2.1 |
if (landscape = xmax > pixaspect*ymax)
|
| 136 |
greg |
2.10 |
puts("%%Orientation: Landscape");
|
| 137 |
greg |
2.1 |
else
|
| 138 |
greg |
2.10 |
puts("%%Orientation: Portrait");
|
| 139 |
greg |
2.1 |
if (PWIDTH > PHEIGHT ^ landscape) {
|
| 140 |
|
|
pwidth = PHEIGHT;
|
| 141 |
|
|
pheight = PWIDTH;
|
| 142 |
|
|
} else {
|
| 143 |
|
|
pwidth = PWIDTH;
|
| 144 |
|
|
pheight = PHEIGHT;
|
| 145 |
|
|
}
|
| 146 |
|
|
if (pheight/pwidth > pixaspect*ymax/xmax) {
|
| 147 |
greg |
2.2 |
iwidth = pwidth;
|
| 148 |
|
|
iheight = pwidth*pixaspect*ymax/xmax;
|
| 149 |
greg |
2.1 |
} else {
|
| 150 |
greg |
2.2 |
iheight = pheight;
|
| 151 |
|
|
iwidth = pheight*xmax/(pixaspect*ymax);
|
| 152 |
greg |
2.1 |
}
|
| 153 |
greg |
2.10 |
if (pwidth == PHEIGHT)
|
| 154 |
|
|
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
|
| 155 |
|
|
HMARGIN+(pheight-iheight)*.5,
|
| 156 |
|
|
VMARGIN+(pwidth-iwidth)*.5,
|
| 157 |
|
|
HMARGIN+(pheight-iheight)*.5+iheight,
|
| 158 |
|
|
VMARGIN+(pwidth-iwidth)*.5+iwidth);
|
| 159 |
|
|
else
|
| 160 |
|
|
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
|
| 161 |
|
|
HMARGIN+(pwidth-iwidth)*.5,
|
| 162 |
|
|
VMARGIN+(pheight-iheight)*.5,
|
| 163 |
|
|
HMARGIN+(pwidth-iwidth)*.5+iwidth,
|
| 164 |
|
|
VMARGIN+(pheight-iheight)*.5+iheight);
|
| 165 |
|
|
puts("%%EndComments");
|
| 166 |
|
|
puts("save");
|
| 167 |
|
|
puts("64 dict begin");
|
| 168 |
|
|
/* define image reader */
|
| 169 |
|
|
PSprocdef("read6bitRLE");
|
| 170 |
|
|
/* set up transformation matrix */
|
| 171 |
|
|
printf("%f %f translate\n", HMARGIN, VMARGIN);
|
| 172 |
|
|
if (pwidth == PHEIGHT) {
|
| 173 |
|
|
printf("0 %f translate\n", PHEIGHT);
|
| 174 |
|
|
puts("-90 rotate");
|
| 175 |
|
|
}
|
| 176 |
greg |
2.2 |
printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5);
|
| 177 |
|
|
printf("%f %f scale\n", iwidth, iheight);
|
| 178 |
greg |
2.10 |
puts("%%%%EndProlog");
|
| 179 |
greg |
2.3 |
/* start image procedure */
|
| 180 |
greg |
2.9 |
printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bitRLE} image\n", xmax, ymax,
|
| 181 |
greg |
2.2 |
xmax, -ymax, ymax);
|
| 182 |
greg |
2.1 |
}
|
| 183 |
|
|
|
| 184 |
|
|
|
| 185 |
|
|
PStrailer() /* print PostScript trailer */
|
| 186 |
|
|
{
|
| 187 |
|
|
puts("%%Trailer");
|
| 188 |
greg |
2.4 |
if (ncopies > 1)
|
| 189 |
|
|
printf("/#copies %d def\n", ncopies);
|
| 190 |
|
|
puts("showpage");
|
| 191 |
greg |
2.1 |
puts("end");
|
| 192 |
greg |
2.10 |
puts("restore");
|
| 193 |
greg |
2.1 |
puts("%%EOF");
|
| 194 |
|
|
}
|
| 195 |
|
|
|
| 196 |
|
|
|
| 197 |
|
|
PSprocdef(nam) /* define PS procedure to read image */
|
| 198 |
|
|
char *nam;
|
| 199 |
|
|
{
|
| 200 |
|
|
short itab[128];
|
| 201 |
|
|
register int i;
|
| 202 |
greg |
2.5 |
/* assign code values */
|
| 203 |
|
|
for (i = 0; i < 128; i++) /* clear */
|
| 204 |
greg |
2.1 |
itab[i] = -1;
|
| 205 |
greg |
2.5 |
for (i = 1; i < 63; i++) /* assign greys */
|
| 206 |
greg |
2.1 |
itab[code[i]] = i<<2 | 2;
|
| 207 |
greg |
2.5 |
itab[code[0]] = 0; /* black is black */
|
| 208 |
|
|
itab[code[63]] = 255; /* and white is white */
|
| 209 |
greg |
2.9 |
printf("/codetab [");
|
| 210 |
greg |
2.1 |
for (i = 0; i < 128; i++) {
|
| 211 |
|
|
if (!(i & 0xf))
|
| 212 |
|
|
putchar('\n');
|
| 213 |
|
|
printf(" %3d", itab[i]);
|
| 214 |
|
|
}
|
| 215 |
|
|
printf("\n] def\n");
|
| 216 |
|
|
printf("/scanline %d string def\n", xmax);
|
| 217 |
greg |
2.9 |
printf("/nrept 0 def\n");
|
| 218 |
|
|
printf("/readbyte { currentfile read not {stop} if } def\n");
|
| 219 |
|
|
printf("/decode { codetab exch get } def\n");
|
| 220 |
greg |
2.1 |
printf("/%s {\n", nam);
|
| 221 |
|
|
printf("\t{ 0 1 %d { scanline exch\n", xmax-1);
|
| 222 |
greg |
2.9 |
printf("\t\tnrept 0 le\n");
|
| 223 |
|
|
printf("\t\t\t{ { readbyte dup %d eq\n", RUNCHR);
|
| 224 |
|
|
printf("\t\t\t\t\t{ pop /nrept readbyte %d sub def\n", RSTRT-MINRUN+1);
|
| 225 |
|
|
printf("\t\t\t\t\t\t/reptv readbyte decode def\n");
|
| 226 |
|
|
printf("\t\t\t\t\t\treptv exit }\n");
|
| 227 |
|
|
printf("\t\t\t\t\t{ decode dup 0 lt {pop} {exit} ifelse }\n");
|
| 228 |
|
|
printf("\t\t\t\tifelse } loop }\n");
|
| 229 |
|
|
printf("\t\t\t{ /nrept nrept 1 sub def reptv }\n");
|
| 230 |
|
|
printf("\t\tifelse put\n");
|
| 231 |
|
|
printf("\t\t} for\n");
|
| 232 |
|
|
printf("\t} stopped {pop pop 0 string} {scanline} ifelse\n");
|
| 233 |
greg |
2.8 |
printf("} bind def\n");
|
| 234 |
greg |
2.1 |
}
|
| 235 |
|
|
|
| 236 |
|
|
|
| 237 |
|
|
ra2ps() /* convert Radiance scanlines to 6-bit */
|
| 238 |
|
|
{
|
| 239 |
greg |
2.9 |
register COLR *scanin;
|
| 240 |
greg |
2.1 |
register int c;
|
| 241 |
greg |
2.9 |
int lastc, cnt;
|
| 242 |
greg |
2.1 |
register int x;
|
| 243 |
|
|
int y;
|
| 244 |
|
|
/* allocate scanline */
|
| 245 |
|
|
scanin = (COLR *)malloc(xmax*sizeof(COLR));
|
| 246 |
|
|
if (scanin == NULL)
|
| 247 |
|
|
quiterr("out of memory in ra2ps");
|
| 248 |
|
|
/* convert image */
|
| 249 |
|
|
for (y = ymax-1; y >= 0; y--) {
|
| 250 |
|
|
if (freadcolrs(scanin, xmax, stdin) < 0)
|
| 251 |
|
|
quiterr("error reading Radiance picture");
|
| 252 |
greg |
2.6 |
normcolrs(scanin, xmax, bradj); /* normalize */
|
| 253 |
greg |
2.9 |
lastc = -1; cnt = 0;
|
| 254 |
greg |
2.1 |
for (x = 0; x < xmax; x++) {
|
| 255 |
greg |
2.9 |
c = normbright(scanin[x]) + 2;
|
| 256 |
greg |
2.1 |
if (c > 255) c = 255;
|
| 257 |
greg |
2.9 |
c = code[c>>2];
|
| 258 |
|
|
if (c == lastc && cnt < MAXRUN)
|
| 259 |
|
|
cnt++;
|
| 260 |
|
|
else {
|
| 261 |
|
|
putrle(cnt, lastc);
|
| 262 |
|
|
lastc = c;
|
| 263 |
|
|
cnt = 1;
|
| 264 |
|
|
}
|
| 265 |
greg |
2.1 |
}
|
| 266 |
greg |
2.9 |
putrle(cnt, lastc);
|
| 267 |
greg |
2.1 |
if (ferror(stdout))
|
| 268 |
|
|
quiterr("error writing PostScript file");
|
| 269 |
|
|
}
|
| 270 |
|
|
putchar('\n');
|
| 271 |
|
|
/* free scanline */
|
| 272 |
|
|
free((char *)scanin);
|
| 273 |
greg |
2.9 |
}
|
| 274 |
|
|
|
| 275 |
|
|
|
| 276 |
|
|
putrle(cnt, cod) /* put out cnt of cod */
|
| 277 |
|
|
register int cnt, cod;
|
| 278 |
|
|
{
|
| 279 |
|
|
static int col = 0;
|
| 280 |
|
|
|
| 281 |
|
|
if (cnt >= MINRUN) {
|
| 282 |
|
|
col += 3;
|
| 283 |
|
|
putchar(RUNCHR);
|
| 284 |
|
|
putchar(RSTRT-MINRUN+cnt);
|
| 285 |
|
|
putchar(cod);
|
| 286 |
|
|
} else {
|
| 287 |
|
|
col += cnt;
|
| 288 |
|
|
while (cnt-- > 0)
|
| 289 |
|
|
putchar(cod);
|
| 290 |
|
|
}
|
| 291 |
|
|
if (col >= 72) {
|
| 292 |
|
|
putchar('\n');
|
| 293 |
|
|
col = 0;
|
| 294 |
|
|
}
|
| 295 |
greg |
2.1 |
}
|