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 |
|
|
#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 |
greg |
2.6 |
double pixaspect = 1.0; /* pixel aspect ratio */ |
28 |
greg |
2.1 |
|
29 |
|
|
int bradj = 0; /* brightness adjustment */ |
30 |
greg |
2.4 |
int ncopies = 1; /* number of copies */ |
31 |
greg |
2.1 |
|
32 |
|
|
char *progname; |
33 |
|
|
|
34 |
|
|
int xmax, ymax; |
35 |
|
|
|
36 |
greg |
2.6 |
extern char *malloc(); |
37 |
greg |
2.1 |
|
38 |
greg |
2.6 |
|
39 |
greg |
2.1 |
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 |
greg |
2.4 |
case 'n': /* number of copies */ |
69 |
|
|
ncopies = atoi(argv[++i]); |
70 |
|
|
break; |
71 |
greg |
2.1 |
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 |
greg |
2.7 |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
86 |
greg |
2.1 |
progname, argv[i+1]); |
87 |
|
|
exit(1); |
88 |
|
|
} |
89 |
greg |
2.6 |
#ifdef MSDOS |
90 |
|
|
setmode(fileno(stdin), O_BINARY); |
91 |
|
|
#endif |
92 |
greg |
2.1 |
/* 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 |
greg |
2.6 |
double pwidth, pheight; |
125 |
|
|
double iwidth, iheight; |
126 |
greg |
2.1 |
|
127 |
|
|
printf("%%!\n"); |
128 |
|
|
printf("%%%%Title: %s\n", name); |
129 |
|
|
printf("%%%%Creator: %s\n", progname); |
130 |
greg |
2.4 |
printf("%%%%Pages: %d\n", ncopies); |
131 |
greg |
2.1 |
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 |
greg |
2.3 |
/* define image reader */ |
138 |
|
|
PSprocdef("read6bit"); |
139 |
greg |
2.1 |
/* set up transformation matrix */ |
140 |
greg |
2.2 |
printf("%f %f translate\n", HMARGIN, VMARGIN); |
141 |
greg |
2.1 |
if (PWIDTH > PHEIGHT ^ landscape) { |
142 |
|
|
printf("0 %f translate\n", PHEIGHT); |
143 |
greg |
2.2 |
printf("-90 rotate\n"); |
144 |
greg |
2.1 |
pwidth = PHEIGHT; |
145 |
|
|
pheight = PWIDTH; |
146 |
|
|
} else { |
147 |
|
|
pwidth = PWIDTH; |
148 |
|
|
pheight = PHEIGHT; |
149 |
|
|
} |
150 |
|
|
if (pheight/pwidth > pixaspect*ymax/xmax) { |
151 |
greg |
2.2 |
iwidth = pwidth; |
152 |
|
|
iheight = pwidth*pixaspect*ymax/xmax; |
153 |
greg |
2.1 |
} else { |
154 |
greg |
2.2 |
iheight = pheight; |
155 |
|
|
iwidth = pheight*xmax/(pixaspect*ymax); |
156 |
greg |
2.1 |
} |
157 |
greg |
2.2 |
printf("%f %f translate\n", (pwidth-iwidth)*.5, (pheight-iheight)*.5); |
158 |
|
|
printf("%f %f scale\n", iwidth, iheight); |
159 |
greg |
2.1 |
printf("%%%%EndProlog\n"); |
160 |
greg |
2.3 |
/* start image procedure */ |
161 |
greg |
2.2 |
printf("%d %d 8 [%d 0 0 %d 0 %d] {read6bit} image", xmax, ymax, |
162 |
|
|
xmax, -ymax, ymax); |
163 |
greg |
2.1 |
} |
164 |
|
|
|
165 |
|
|
|
166 |
|
|
PStrailer() /* print PostScript trailer */ |
167 |
|
|
{ |
168 |
|
|
puts("%%Trailer"); |
169 |
greg |
2.4 |
if (ncopies > 1) |
170 |
|
|
printf("/#copies %d def\n", ncopies); |
171 |
|
|
puts("showpage"); |
172 |
greg |
2.1 |
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 |
greg |
2.5 |
/* assign code values */ |
183 |
|
|
for (i = 0; i < 128; i++) /* clear */ |
184 |
greg |
2.1 |
itab[i] = -1; |
185 |
greg |
2.5 |
for (i = 1; i < 63; i++) /* assign greys */ |
186 |
greg |
2.1 |
itab[code[i]] = i<<2 | 2; |
187 |
greg |
2.5 |
itab[code[0]] = 0; /* black is black */ |
188 |
|
|
itab[code[63]] = 255; /* and white is white */ |
189 |
greg |
2.1 |
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 |
greg |
2.8 |
printf("\t} stopped {pop pop pop 0 string} {scanline} ifelse\n"); |
202 |
|
|
printf("} bind def\n"); |
203 |
greg |
2.1 |
} |
204 |
|
|
|
205 |
|
|
|
206 |
|
|
ra2ps() /* convert Radiance scanlines to 6-bit */ |
207 |
|
|
{ |
208 |
|
|
COLR *scanin; |
209 |
|
|
register int col = 0; |
210 |
|
|
register int c; |
211 |
|
|
register int x; |
212 |
|
|
int y; |
213 |
|
|
/* allocate scanline */ |
214 |
|
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
215 |
|
|
if (scanin == NULL) |
216 |
|
|
quiterr("out of memory in ra2ps"); |
217 |
|
|
/* convert image */ |
218 |
|
|
for (y = ymax-1; y >= 0; y--) { |
219 |
|
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
220 |
|
|
quiterr("error reading Radiance picture"); |
221 |
greg |
2.6 |
normcolrs(scanin, xmax, bradj); /* normalize */ |
222 |
greg |
2.1 |
for (x = 0; x < xmax; x++) { |
223 |
|
|
if (!(col++ & 0x3f)) |
224 |
|
|
putchar('\n'); |
225 |
|
|
c = normbright(scanin[x]) + (random()&3); |
226 |
|
|
if (c > 255) c = 255; |
227 |
|
|
putchar(code[c>>2]); |
228 |
|
|
} |
229 |
|
|
if (ferror(stdout)) |
230 |
|
|
quiterr("error writing PostScript file"); |
231 |
|
|
} |
232 |
|
|
putchar('\n'); |
233 |
|
|
/* free scanline */ |
234 |
|
|
free((char *)scanin); |
235 |
|
|
} |