1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* ra_bn.c - program to convert between RADIANCE and barneyscan picture format. |
9 |
* |
10 |
* 10/12/88 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
|
15 |
#include "color.h" |
16 |
|
17 |
extern double atof(), pow(); |
18 |
|
19 |
double gamma = 2.0; /* gamma correction */ |
20 |
|
21 |
char *progname; |
22 |
|
23 |
char errmsg[128]; |
24 |
|
25 |
FILE *rafp, *bnfp[3]; |
26 |
|
27 |
int xmax, ymax; |
28 |
|
29 |
|
30 |
main(argc, argv) |
31 |
int argc; |
32 |
char *argv[]; |
33 |
{ |
34 |
int reverse = 0; |
35 |
int i; |
36 |
|
37 |
progname = argv[0]; |
38 |
|
39 |
for (i = 1; i < argc; i++) |
40 |
if (argv[i][0] == '-') |
41 |
switch (argv[i][1]) { |
42 |
case 'g': |
43 |
gamma = atof(argv[++i]); |
44 |
break; |
45 |
case 'r': |
46 |
reverse = !reverse; |
47 |
break; |
48 |
default: |
49 |
goto userr; |
50 |
} |
51 |
else |
52 |
break; |
53 |
/* set gamma correction */ |
54 |
setcolrgam(gamma); |
55 |
|
56 |
if (reverse) { |
57 |
if (i > argc-1 || i < argc-2) |
58 |
goto userr; |
59 |
if (openbarney(argv[i], "r") < 0) { |
60 |
sprintf(errmsg, "cannot open input \"%s\"", argv[i]); |
61 |
quiterr(errmsg); |
62 |
} |
63 |
if (i == argc-1 || !strcmp(argv[i+1], "-")) |
64 |
rafp = stdout; |
65 |
else if ((rafp = fopen(argv[i+1], "w")) == NULL) { |
66 |
sprintf(errmsg, "cannot open output \"%s\"", |
67 |
argv[i+1]); |
68 |
quiterr(errmsg); |
69 |
} |
70 |
/* put header */ |
71 |
printargs(i, argv, rafp); |
72 |
putc('\n', rafp); |
73 |
fputresolu(YMAJOR|YDECR, xmax, ymax, rafp); |
74 |
/* convert file */ |
75 |
bn2ra(); |
76 |
} else { |
77 |
if (i != argc-2) |
78 |
goto userr; |
79 |
if (!strcmp(argv[i], "-")) |
80 |
rafp = stdin; |
81 |
else if ((rafp = fopen(argv[i], "r")) == NULL) { |
82 |
sprintf(errmsg, "cannot open input \"%s\"", |
83 |
argv[i]); |
84 |
quiterr(errmsg); |
85 |
} |
86 |
/* get header */ |
87 |
getheader(rafp, NULL); |
88 |
if (fgetresolu(&xmax, &ymax, rafp) != (YMAJOR|YDECR)) |
89 |
quiterr("bad RADIANCE format"); |
90 |
if (openbarney(argv[i+1], "w") < 0) { |
91 |
sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]); |
92 |
quiterr(errmsg); |
93 |
} |
94 |
/* convert file */ |
95 |
ra2bn(); |
96 |
} |
97 |
quiterr(NULL); |
98 |
userr: |
99 |
fprintf(stderr, "Usage: %s [-g gamma] {input|-} output\n", progname); |
100 |
fprintf(stderr, " or: %s -r [-g gamma] input [output|-]\n", |
101 |
progname); |
102 |
exit(1); |
103 |
} |
104 |
|
105 |
|
106 |
quiterr(err) /* print message and exit */ |
107 |
char *err; |
108 |
{ |
109 |
if (err != NULL) { |
110 |
fprintf(stderr, "%s: %s\n", progname, err); |
111 |
exit(1); |
112 |
} |
113 |
exit(0); |
114 |
} |
115 |
|
116 |
|
117 |
openbarney(fname, mode) /* open barneyscan files */ |
118 |
char *fname; |
119 |
char *mode; |
120 |
{ |
121 |
static char suffix[3][4] = {"red", "grn", "blu"}; |
122 |
int i; |
123 |
char file[128]; |
124 |
|
125 |
for (i = 0; i < 3; i++) { |
126 |
sprintf(file, "%s.%s", fname, suffix[i]); |
127 |
if ((bnfp[i] = fopen(file, mode)) == NULL) |
128 |
return(-1); |
129 |
} |
130 |
if (mode[0] == 'r') { /* get header */ |
131 |
xmax = getint(bnfp[0]); |
132 |
ymax = getint(bnfp[0]); |
133 |
for (i = 1; i < 3; i++) |
134 |
if (getint(bnfp[i]) != xmax || getint(bnfp[i]) != ymax) |
135 |
return(-1); |
136 |
} else { /* put header */ |
137 |
for (i = 0; i < 3; i++) { |
138 |
putint(xmax, bnfp[i]); |
139 |
putint(ymax, bnfp[i]); |
140 |
} |
141 |
} |
142 |
return(0); |
143 |
} |
144 |
|
145 |
|
146 |
int |
147 |
getint(fp) /* get short int from barneyscan file */ |
148 |
register FILE *fp; |
149 |
{ |
150 |
register short val; |
151 |
|
152 |
val = getc(fp); |
153 |
val |= getc(fp) << 8; |
154 |
|
155 |
return(val); |
156 |
} |
157 |
|
158 |
|
159 |
putint(val, fp) /* put short int to barneyscan file */ |
160 |
register int val; |
161 |
register FILE *fp; |
162 |
{ |
163 |
putc(val&0xff, fp); |
164 |
putc((val >> 8)&0xff, fp); |
165 |
} |
166 |
|
167 |
|
168 |
ra2bn() /* convert radiance to barneyscan */ |
169 |
{ |
170 |
register int i; |
171 |
register COLR *inl; |
172 |
int j; |
173 |
|
174 |
if ((inl = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
175 |
quiterr("out of memory"); |
176 |
for (j = 0; j < ymax; j++) { |
177 |
if (freadcolrs(inl, xmax, rafp) < 0) |
178 |
quiterr("error reading RADIANCE file"); |
179 |
colrs_gambs(inl, xmax); |
180 |
for (i = 0; i < xmax; i++) { |
181 |
putc(inl[i][RED], bnfp[0]); |
182 |
putc(inl[i][GRN], bnfp[1]); |
183 |
putc(inl[i][BLU], bnfp[2]); |
184 |
} |
185 |
if (ferror(bnfp[0]) || ferror(bnfp[1]) || ferror(bnfp[2])) |
186 |
quiterr("error writing Barney files"); |
187 |
} |
188 |
free((char *)inl); |
189 |
} |
190 |
|
191 |
|
192 |
bn2ra() /* convert barneyscan to radiance */ |
193 |
{ |
194 |
register int i; |
195 |
register COLR *outline; |
196 |
int j; |
197 |
|
198 |
if ((outline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
199 |
quiterr("out of memory"); |
200 |
for (j = 0; j < ymax; j++) { |
201 |
for (i = 0; i < xmax; i++) { |
202 |
outline[i][RED] = getc(bnfp[0]); |
203 |
outline[i][GRN] = getc(bnfp[1]); |
204 |
outline[i][BLU] = getc(bnfp[2]); |
205 |
} |
206 |
if (feof(bnfp[0]) || feof(bnfp[1]) || feof(bnfp[2])) |
207 |
quiterr("error reading barney file"); |
208 |
gambs_colrs(outline, xmax); |
209 |
if (fwritecolrs(outline, xmax, rafp) < 0) |
210 |
quiterr("error writing RADIANCE file"); |
211 |
} |
212 |
free((char *)outline); |
213 |
} |