1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* program to convert between RADIANCE and Poskanzer Pixmaps |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
|
13 |
#include <ctype.h> |
14 |
|
15 |
#include "color.h" |
16 |
|
17 |
extern double atof(), pow(); |
18 |
|
19 |
int agryscan(), bgryscan(), aclrscan(), bclrscan(); |
20 |
|
21 |
int bradj = 0; /* brightness adjustment */ |
22 |
|
23 |
int maxval = 255; /* maximum primary value */ |
24 |
|
25 |
char *progname; |
26 |
|
27 |
int xmax, ymax; |
28 |
|
29 |
|
30 |
main(argc, argv) |
31 |
int argc; |
32 |
char *argv[]; |
33 |
{ |
34 |
double gamma = 2.2; |
35 |
int binflag = 1; |
36 |
int reverse = 0; |
37 |
int ptype; |
38 |
int i; |
39 |
|
40 |
progname = argv[0]; |
41 |
|
42 |
for (i = 1; i < argc; i++) |
43 |
if (argv[i][0] == '-') |
44 |
switch (argv[i][1]) { |
45 |
case 'g': |
46 |
gamma = atof(argv[++i]); |
47 |
break; |
48 |
case 'e': |
49 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
50 |
goto userr; |
51 |
bradj = atoi(argv[++i]); |
52 |
break; |
53 |
case 'a': |
54 |
binflag = 0; |
55 |
break; |
56 |
case 'r': |
57 |
reverse = !reverse; |
58 |
break; |
59 |
default: |
60 |
goto userr; |
61 |
} |
62 |
else |
63 |
break; |
64 |
|
65 |
if (i < argc-2) |
66 |
goto userr; |
67 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
68 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
69 |
progname, argv[i]); |
70 |
exit(1); |
71 |
} |
72 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
73 |
fprintf(stderr, "can't open output \"%s\"\n", |
74 |
progname, argv[i+1]); |
75 |
exit(1); |
76 |
} |
77 |
setcolrgam(gamma); |
78 |
if (reverse) { |
79 |
/* get header */ |
80 |
if (getc(stdin) != 'P') |
81 |
quiterr("input not a Poskanzer Pixmap"); |
82 |
ptype = getc(stdin); |
83 |
xmax = scanint(stdin); |
84 |
ymax = scanint(stdin); |
85 |
maxval = scanint(stdin); |
86 |
/* put header */ |
87 |
printargs(i, argv, stdout); |
88 |
fputformat(COLRFMT, stdout); |
89 |
putchar('\n'); |
90 |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
91 |
/* convert file */ |
92 |
switch (ptype) { |
93 |
case '2': |
94 |
ppm2ra(agryscan); |
95 |
break; |
96 |
case '5': |
97 |
ppm2ra(bgryscan); |
98 |
break; |
99 |
case '3': |
100 |
ppm2ra(aclrscan); |
101 |
break; |
102 |
case '6': |
103 |
ppm2ra(bclrscan); |
104 |
break; |
105 |
default: |
106 |
quiterr("unsupported Pixmap type"); |
107 |
} |
108 |
} else { |
109 |
/* get header info. */ |
110 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
111 |
fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR)) |
112 |
quiterr("bad picture format"); |
113 |
/* write PPM header */ |
114 |
printf("P%c\n%d %d\n%d\n", binflag ? '6' : '3', |
115 |
xmax, ymax, maxval); |
116 |
/* convert file */ |
117 |
ra2ppm(binflag); |
118 |
} |
119 |
exit(0); |
120 |
userr: |
121 |
fprintf(stderr, |
122 |
"Usage: %s [-r][-a][-g gamma][-e +/-stops] [input [output]]\n", |
123 |
progname); |
124 |
exit(1); |
125 |
} |
126 |
|
127 |
|
128 |
quiterr(err) /* print message and exit */ |
129 |
char *err; |
130 |
{ |
131 |
if (err != NULL) { |
132 |
fprintf(stderr, "%s: %s\n", progname, err); |
133 |
exit(1); |
134 |
} |
135 |
exit(0); |
136 |
} |
137 |
|
138 |
|
139 |
ppm2ra(getscan) /* convert color Pixmap to Radiance picture */ |
140 |
int (*getscan)(); |
141 |
{ |
142 |
COLR *scanout; |
143 |
register int x; |
144 |
int y; |
145 |
/* allocate scanline */ |
146 |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
147 |
if (scanout == NULL) |
148 |
quiterr("out of memory in ppm2ra"); |
149 |
/* convert image */ |
150 |
for (y = ymax-1; y >= 0; y--) { |
151 |
if ((*getscan)(scanout, xmax, stdin) < 0) |
152 |
quiterr("error reading Pixmap"); |
153 |
gambs_colrs(scanout, xmax); |
154 |
if (bradj) |
155 |
shiftcolrs(scanout, xmax, bradj); |
156 |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
157 |
quiterr("error writing Radiance picture"); |
158 |
} |
159 |
/* free scanline */ |
160 |
free((char *)scanout); |
161 |
} |
162 |
|
163 |
|
164 |
ra2ppm(binary) /* convert Radiance picture to Pixmap */ |
165 |
int binary; |
166 |
{ |
167 |
COLR *scanin; |
168 |
register int x; |
169 |
int y; |
170 |
/* allocate scanline */ |
171 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
172 |
if (scanin == NULL) |
173 |
quiterr("out of memory in ra2pr"); |
174 |
/* convert image */ |
175 |
for (y = ymax-1; y >= 0; y--) { |
176 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
177 |
quiterr("error reading Radiance picture"); |
178 |
if (bradj) |
179 |
shiftcolrs(scanin, xmax, bradj); |
180 |
colrs_gambs(scanin, xmax); |
181 |
if (binary) |
182 |
for (x = 0; x < xmax; x++) { |
183 |
putc(scanin[x][RED], stdout); |
184 |
putc(scanin[x][GRN], stdout); |
185 |
putc(scanin[x][BLU], stdout); |
186 |
} |
187 |
else |
188 |
for (x = 0; x < xmax; x++) |
189 |
printf("%d %d %d\n", scanin[x][RED], |
190 |
scanin[x][GRN], |
191 |
scanin[x][BLU]); |
192 |
if (ferror(stdout)) |
193 |
quiterr("error writing Pixmap"); |
194 |
} |
195 |
/* free scanline */ |
196 |
free((char *)scanin); |
197 |
} |
198 |
|
199 |
|
200 |
agryscan(scan, len, fp) /* get an ASCII greyscale scanline */ |
201 |
register COLR *scan; |
202 |
register int len; |
203 |
FILE *fp; |
204 |
{ |
205 |
while (len-- > 0) { |
206 |
scan[0][RED] = |
207 |
scan[0][GRN] = |
208 |
scan[0][BLU] = normval(scanint(fp)); |
209 |
scan++; |
210 |
} |
211 |
return(0); |
212 |
} |
213 |
|
214 |
|
215 |
bgryscan(scan, len, fp) /* get a binary greyscale scanline */ |
216 |
register COLR *scan; |
217 |
int len; |
218 |
register FILE *fp; |
219 |
{ |
220 |
register int c; |
221 |
|
222 |
while (len-- > 0) { |
223 |
if ((c = getc(fp)) == EOF) |
224 |
return(-1); |
225 |
if (maxval != 255) |
226 |
c = normval(c); |
227 |
scan[0][RED] = |
228 |
scan[0][GRN] = |
229 |
scan[0][BLU] = c; |
230 |
scan++; |
231 |
} |
232 |
return(0); |
233 |
} |
234 |
|
235 |
|
236 |
aclrscan(scan, len, fp) /* get an ASCII color scanline */ |
237 |
register COLR *scan; |
238 |
register int len; |
239 |
FILE *fp; |
240 |
{ |
241 |
while (len-- > 0) { |
242 |
scan[0][RED] = normval(scanint(fp)); |
243 |
scan[0][GRN] = normval(scanint(fp)); |
244 |
scan[0][BLU] = normval(scanint(fp)); |
245 |
scan++; |
246 |
} |
247 |
return(0); |
248 |
} |
249 |
|
250 |
|
251 |
bclrscan(scan, len, fp) /* get a binary color scanline */ |
252 |
register COLR *scan; |
253 |
int len; |
254 |
register FILE *fp; |
255 |
{ |
256 |
int r, g, b; |
257 |
|
258 |
while (len-- > 0) { |
259 |
r = getc(fp); |
260 |
g = getc(fp); |
261 |
if ((b = getc(fp)) == EOF) |
262 |
return(-1); |
263 |
if (maxval == 255) { |
264 |
scan[0][RED] = r; |
265 |
scan[0][GRN] = g; |
266 |
scan[0][BLU] = b; |
267 |
} else { |
268 |
scan[0][RED] = normval(r); |
269 |
scan[0][GRN] = normval(g); |
270 |
scan[0][BLU] = normval(b); |
271 |
} |
272 |
scan++; |
273 |
} |
274 |
return(0); |
275 |
} |
276 |
|
277 |
|
278 |
int |
279 |
scanint(fp) /* scan the next positive integer value */ |
280 |
register FILE *fp; |
281 |
{ |
282 |
register int i, c; |
283 |
tryagain: |
284 |
while (isspace(c = getc(fp))) |
285 |
; |
286 |
if (c == EOF) |
287 |
quiterr("unexpected end of file"); |
288 |
if (c == '#') { /* comment */ |
289 |
while ((c = getc(fp)) != EOF && c != '\n') |
290 |
; |
291 |
goto tryagain; |
292 |
} |
293 |
/* should be integer */ |
294 |
i = 0; |
295 |
do { |
296 |
if (!isdigit(c)) |
297 |
quiterr("error reading integer"); |
298 |
i = 10*i + c - '0'; |
299 |
c = getc(fp); |
300 |
} while (c != EOF && !isspace(c)); |
301 |
return(i); |
302 |
} |
303 |
|
304 |
|
305 |
int |
306 |
normval(v) /* normalize a value to [0,255] */ |
307 |
register int v; |
308 |
{ |
309 |
if (v >= maxval) |
310 |
return(255); |
311 |
if (maxval == 255) |
312 |
return(v); |
313 |
return(v*255L/maxval); |
314 |
} |