| 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 |
|
|
#include "color.h" |
| 13 |
|
|
#include "random.h" |
| 14 |
|
|
|
| 15 |
|
|
#define HMARGIN (.5*72) /* horizontal margin */ |
| 16 |
|
|
#define VMARGIN (.5*72) /* vertical margin */ |
| 17 |
|
|
#define PWIDTH (8.5*72-2*HMARGIN) /* width of device */ |
| 18 |
|
|
#define PHEIGHT (11*72-2*VMARGIN) /* height of device */ |
| 19 |
|
|
|
| 20 |
|
|
char code[] = /* 6-bit code lookup table */ |
| 21 |
|
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@+"; |
| 22 |
|
|
|
| 23 |
|
|
int wrongformat = 0; /* input in wrong format? */ |
| 24 |
|
|
double pixaspect = 1.0; /* pixel aspect ratio */ |
| 25 |
|
|
|
| 26 |
|
|
int bradj = 0; /* brightness adjustment */ |
| 27 |
|
|
|
| 28 |
|
|
char *progname; |
| 29 |
|
|
|
| 30 |
|
|
int xmax, ymax; |
| 31 |
|
|
|
| 32 |
|
|
|
| 33 |
|
|
headline(s) /* check header line */ |
| 34 |
|
|
char *s; |
| 35 |
|
|
{ |
| 36 |
|
|
char fmt[32]; |
| 37 |
|
|
|
| 38 |
|
|
if (isformat(s)) { |
| 39 |
|
|
formatval(fmt, s); |
| 40 |
|
|
wrongformat = strcmp(fmt, COLRFMT); |
| 41 |
|
|
} else if (isaspect(s)) |
| 42 |
|
|
pixaspect *= aspectval(s); |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
|
|
| 46 |
|
|
main(argc, argv) |
| 47 |
|
|
int argc; |
| 48 |
|
|
char *argv[]; |
| 49 |
|
|
{ |
| 50 |
|
|
int i; |
| 51 |
|
|
|
| 52 |
|
|
progname = argv[0]; |
| 53 |
|
|
|
| 54 |
|
|
for (i = 1; i < argc; i++) |
| 55 |
|
|
if (argv[i][0] == '-') |
| 56 |
|
|
switch (argv[i][1]) { |
| 57 |
|
|
case 'e': /* exposure adjustment */ |
| 58 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
| 59 |
|
|
goto userr; |
| 60 |
|
|
bradj = atoi(argv[++i]); |
| 61 |
|
|
break; |
| 62 |
|
|
default: |
| 63 |
|
|
goto userr; |
| 64 |
|
|
} |
| 65 |
|
|
else |
| 66 |
|
|
break; |
| 67 |
|
|
|
| 68 |
|
|
if (i < argc-2) |
| 69 |
|
|
goto userr; |
| 70 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
| 71 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n", |
| 72 |
|
|
progname, argv[i]); |
| 73 |
|
|
exit(1); |
| 74 |
|
|
} |
| 75 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
| 76 |
|
|
fprintf(stderr, "can't open output \"%s\"\n", |
| 77 |
|
|
progname, argv[i+1]); |
| 78 |
|
|
exit(1); |
| 79 |
|
|
} |
| 80 |
|
|
/* get our header */ |
| 81 |
|
|
getheader(stdin, headline, NULL); |
| 82 |
|
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
| 83 |
|
|
quiterr("bad picture format"); |
| 84 |
|
|
/* write header */ |
| 85 |
|
|
PSheader(i <= argc-1 ? argv[i] : "<stdin>"); |
| 86 |
|
|
/* convert file */ |
| 87 |
|
|
ra2ps(); |
| 88 |
|
|
/* write trailer */ |
| 89 |
|
|
PStrailer(); |
| 90 |
|
|
exit(0); |
| 91 |
|
|
userr: |
| 92 |
|
|
fprintf(stderr, "Usage: %s [-e +/-stops] [input [output]]\n", progname); |
| 93 |
|
|
exit(1); |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
|
| 97 |
|
|
quiterr(err) /* print message and exit */ |
| 98 |
|
|
char *err; |
| 99 |
|
|
{ |
| 100 |
|
|
if (err != NULL) { |
| 101 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
| 102 |
|
|
exit(1); |
| 103 |
|
|
} |
| 104 |
|
|
exit(0); |
| 105 |
|
|
} |
| 106 |
|
|
|
| 107 |
|
|
|
| 108 |
|
|
PSheader(name) /* print PostScript header */ |
| 109 |
|
|
char *name; |
| 110 |
|
|
{ |
| 111 |
|
|
int landscape = 0; |
| 112 |
|
|
double pwidth, pheight; |
| 113 |
greg |
2.2 |
double iwidth, iheight; |
| 114 |
greg |
2.1 |
|
| 115 |
|
|
printf("%%!\n"); |
| 116 |
|
|
printf("%%%%Title: %s\n", name); |
| 117 |
|
|
printf("%%%%Creator: %s\n", progname); |
| 118 |
|
|
printf("%%%%Pages: 1\n"); |
| 119 |
|
|
if (landscape = xmax > pixaspect*ymax) |
| 120 |
|
|
printf("%%%%Landscape\n"); |
| 121 |
|
|
else |
| 122 |
|
|
printf("%%%%Portrait\n"); |
| 123 |
|
|
printf("%%%%EndComments\n"); |
| 124 |
|
|
printf("gsave\n"); |
| 125 |
|
|
printf("64 dict begin\n"); |
| 126 |
greg |
2.3 |
/* define image reader */ |
| 127 |
|
|
PSprocdef("read6bit"); |
| 128 |
greg |
2.1 |
/* set up transformation matrix */ |
| 129 |
greg |
2.2 |
printf("%f %f translate\n", HMARGIN, VMARGIN); |
| 130 |
greg |
2.1 |
if (PWIDTH > PHEIGHT ^ landscape) { |
| 131 |
|
|
printf("0 %f translate\n", PHEIGHT); |
| 132 |
greg |
2.2 |
printf("-90 rotate\n"); |
| 133 |
greg |
2.1 |
pwidth = PHEIGHT; |
| 134 |
|
|
pheight = PWIDTH; |
| 135 |
|
|
} else { |
| 136 |
|
|
pwidth = PWIDTH; |
| 137 |
|
|
pheight = PHEIGHT; |
| 138 |
|
|
} |
| 139 |
|
|
if (pheight/pwidth > pixaspect*ymax/xmax) { |
| 140 |
greg |
2.2 |
iwidth = pwidth; |
| 141 |
|
|
iheight = pwidth*pixaspect*ymax/xmax; |
| 142 |
greg |
2.1 |
} else { |
| 143 |
greg |
2.2 |
iheight = pheight; |
| 144 |
|
|
iwidth = pheight*xmax/(pixaspect*ymax); |
| 145 |
greg |
2.1 |
} |
| 146 |
greg |
2.2 |
printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5); |
| 147 |
|
|
printf("%f %f scale\n", iwidth, iheight); |
| 148 |
greg |
2.1 |
printf("%%%%EndProlog\n"); |
| 149 |
greg |
2.3 |
/* start image procedure */ |
| 150 |
greg |
2.2 |
printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax, |
| 151 |
|
|
xmax, -ymax, ymax); |
| 152 |
greg |
2.1 |
} |
| 153 |
|
|
|
| 154 |
|
|
|
| 155 |
|
|
PStrailer() /* print PostScript trailer */ |
| 156 |
|
|
{ |
| 157 |
|
|
puts("%%Trailer"); |
| 158 |
|
|
puts("end"); |
| 159 |
|
|
puts("showpage"); |
| 160 |
|
|
puts("grestore"); |
| 161 |
|
|
puts("%%EOF"); |
| 162 |
|
|
} |
| 163 |
|
|
|
| 164 |
|
|
|
| 165 |
|
|
PSprocdef(nam) /* define PS procedure to read image */ |
| 166 |
|
|
char *nam; |
| 167 |
|
|
{ |
| 168 |
|
|
short itab[128]; |
| 169 |
|
|
register int i; |
| 170 |
|
|
|
| 171 |
|
|
for (i = 0; i < 128; i++) |
| 172 |
|
|
itab[i] = -1; |
| 173 |
|
|
for (i = 0; i < 64; i++) |
| 174 |
|
|
itab[code[i]] = i<<2 | 2; |
| 175 |
|
|
printf("/decode ["); |
| 176 |
|
|
for (i = 0; i < 128; i++) { |
| 177 |
|
|
if (!(i & 0xf)) |
| 178 |
|
|
putchar('\n'); |
| 179 |
|
|
printf(" %3d", itab[i]); |
| 180 |
|
|
} |
| 181 |
|
|
printf("\n] def\n"); |
| 182 |
|
|
printf("/scanline %d string def\n", xmax); |
| 183 |
|
|
printf("/%s {\n", nam); |
| 184 |
|
|
printf("\t{ 0 1 %d { scanline exch\n", xmax-1); |
| 185 |
|
|
printf("\t\t{ decode currentfile read not {stop} if get\n"); |
| 186 |
|
|
printf("\tdup 0 lt {pop} {exit} ifelse } loop put } for\n"); |
| 187 |
|
|
printf("} stopped {pop pop pop 0 string} {scanline} ifelse } def\n"); |
| 188 |
|
|
} |
| 189 |
|
|
|
| 190 |
|
|
|
| 191 |
|
|
ra2ps() /* convert Radiance scanlines to 6-bit */ |
| 192 |
|
|
{ |
| 193 |
|
|
COLR *scanin; |
| 194 |
|
|
register int col = 0; |
| 195 |
|
|
register int c; |
| 196 |
|
|
register int x; |
| 197 |
|
|
int y; |
| 198 |
|
|
/* allocate scanline */ |
| 199 |
|
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
| 200 |
|
|
if (scanin == NULL) |
| 201 |
|
|
quiterr("out of memory in ra2ps"); |
| 202 |
|
|
/* convert image */ |
| 203 |
|
|
for (y = ymax-1; y >= 0; y--) { |
| 204 |
|
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
| 205 |
|
|
quiterr("error reading Radiance picture"); |
| 206 |
|
|
normcolrs(scanin, xmax, bradj); /* normalize */ |
| 207 |
|
|
for (x = 0; x < xmax; x++) { |
| 208 |
|
|
if (!(col++ & 0x3f)) |
| 209 |
|
|
putchar('\n'); |
| 210 |
|
|
c = normbright(scanin[x]) + (random()&3); |
| 211 |
|
|
if (c > 255) c = 255; |
| 212 |
|
|
putchar(code[c>>2]); |
| 213 |
|
|
} |
| 214 |
|
|
if (ferror(stdout)) |
| 215 |
|
|
quiterr("error writing PostScript file"); |
| 216 |
|
|
} |
| 217 |
|
|
putchar('\n'); |
| 218 |
|
|
/* free scanline */ |
| 219 |
|
|
free((char *)scanin); |
| 220 |
|
|
} |