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