1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* ra_bn.c - program to convert between RADIANCE and barneyscan picture format. |
6 |
|
* |
8 |
|
*/ |
9 |
|
|
10 |
|
#include <stdio.h> |
11 |
+ |
#include <time.h> |
12 |
+ |
#include <math.h> |
13 |
|
|
14 |
+ |
#include "platform.h" |
15 |
|
#include "color.h" |
16 |
+ |
#include "resolu.h" |
17 |
|
|
18 |
< |
extern double atof(), pow(); |
18 |
> |
double gamcor = 2.0; /* gamma correction */ |
19 |
|
|
20 |
< |
double gamma = 2.0; /* gamma correction */ |
20 |
> |
int bradj = 0; /* brightness adjustment */ |
21 |
|
|
22 |
|
char *progname; |
23 |
|
|
34 |
|
{ |
35 |
|
int reverse = 0; |
36 |
|
int i; |
37 |
< |
|
37 |
> |
SET_DEFAULT_BINARY(); |
38 |
> |
SET_FILE_BINARY(stdin); |
39 |
> |
SET_FILE_BINARY(stdout); |
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]); |
46 |
> |
gamcor = atof(argv[++i]); |
47 |
|
break; |
48 |
|
case 'r': |
49 |
|
reverse = !reverse; |
50 |
|
break; |
51 |
+ |
case 'e': |
52 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
53 |
+ |
goto userr; |
54 |
+ |
bradj = atoi(argv[++i]); |
55 |
+ |
break; |
56 |
|
default: |
57 |
|
goto userr; |
58 |
|
} |
59 |
|
else |
60 |
|
break; |
61 |
+ |
/* set gamma correction */ |
62 |
+ |
setcolrgam(gamcor); |
63 |
|
|
64 |
|
if (reverse) { |
65 |
|
if (i > argc-1 || i < argc-2) |
76 |
|
quiterr(errmsg); |
77 |
|
} |
78 |
|
/* put header */ |
79 |
+ |
newheader("RADIANCE", stdout); |
80 |
|
printargs(i, argv, rafp); |
81 |
+ |
fputformat(COLRFMT, rafp); |
82 |
|
putc('\n', rafp); |
83 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, rafp); |
83 |
> |
fprtresolu(xmax, ymax, rafp); |
84 |
|
/* convert file */ |
85 |
|
bn2ra(); |
86 |
|
} else { |
94 |
|
quiterr(errmsg); |
95 |
|
} |
96 |
|
/* get header */ |
97 |
< |
getheader(rafp, NULL); |
98 |
< |
if (fgetresolu(&xmax, &ymax, rafp) != (YMAJOR|YDECR)) |
97 |
> |
if (checkheader(rafp, COLRFMT, NULL) < 0 || |
98 |
> |
fgetresolu(&xmax, &ymax, rafp) < 0) |
99 |
|
quiterr("bad RADIANCE format"); |
100 |
|
if (openbarney(argv[i+1], "w") < 0) { |
101 |
|
sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]); |
106 |
|
} |
107 |
|
quiterr(NULL); |
108 |
|
userr: |
109 |
< |
fprintf(stderr, "Usage: %s {input|-} output\n", progname); |
98 |
< |
fprintf(stderr, " or: %s -r [-g gamma] input [output|-]\n", |
109 |
> |
fprintf(stderr, "Usage: %s [-g gamma][-e +/-stops] {input|-} output\n", |
110 |
|
progname); |
111 |
+ |
fprintf(stderr, " or: %s -r [-g gamma][-e +/-stops] input [output|-]\n", |
112 |
+ |
progname); |
113 |
|
exit(1); |
114 |
|
} |
115 |
|
|
178 |
|
|
179 |
|
ra2bn() /* convert radiance to barneyscan */ |
180 |
|
{ |
181 |
< |
unsigned char gmap[1024]; |
182 |
< |
register int i,k,c; |
170 |
< |
register COLOR *inline; |
181 |
> |
register int i; |
182 |
> |
register COLR *inl; |
183 |
|
int j; |
184 |
|
|
185 |
< |
if ((inline = (COLOR *)malloc(xmax*sizeof(COLOR))) == NULL) |
185 |
> |
if ((inl = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
186 |
|
quiterr("out of memory"); |
175 |
– |
for (i = 0; i < 1024; i++) |
176 |
– |
gmap[i] = 256.*pow((i+.5)/1024., 1./gamma); |
187 |
|
for (j = 0; j < ymax; j++) { |
188 |
< |
if (freadscan(inline, xmax, rafp) < 0) |
188 |
> |
if (freadcolrs(inl, xmax, rafp) < 0) |
189 |
|
quiterr("error reading RADIANCE file"); |
190 |
< |
for (i = 0; i < xmax; i++) |
191 |
< |
for (k = 0; k < 3; k++) { |
192 |
< |
c = 1024.*colval(inline[i],k); |
193 |
< |
if (c >= 1024) |
194 |
< |
c = 1023; |
195 |
< |
putc(gmap[c], bnfp[k]); |
196 |
< |
} |
190 |
> |
if (bradj) |
191 |
> |
shiftcolrs(inl, xmax, bradj); |
192 |
> |
colrs_gambs(inl, xmax); |
193 |
> |
for (i = 0; i < xmax; i++) { |
194 |
> |
putc(inl[i][RED], bnfp[0]); |
195 |
> |
putc(inl[i][GRN], bnfp[1]); |
196 |
> |
putc(inl[i][BLU], bnfp[2]); |
197 |
> |
} |
198 |
> |
if (ferror(bnfp[0]) || ferror(bnfp[1]) || ferror(bnfp[2])) |
199 |
> |
quiterr("error writing Barney files"); |
200 |
|
} |
201 |
< |
free((char *)inline); |
201 |
> |
free((void *)inl); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
bn2ra() /* convert barneyscan to radiance */ |
206 |
|
{ |
207 |
< |
float gmap[256]; |
208 |
< |
register int i,k,c; |
196 |
< |
register COLOR *outline; |
207 |
> |
register int i; |
208 |
> |
register COLR *outline; |
209 |
|
int j; |
210 |
|
|
211 |
< |
if ((outline = (COLOR *)malloc(xmax*sizeof(COLOR))) == NULL) |
211 |
> |
if ((outline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
212 |
|
quiterr("out of memory"); |
201 |
– |
for (i = 0; i < 256; i++) |
202 |
– |
gmap[i] = pow((i+.5)/256., gamma); |
213 |
|
for (j = 0; j < ymax; j++) { |
214 |
< |
for (i = 0; i < xmax; i++) |
215 |
< |
for (k = 0; k < 3; k++) |
216 |
< |
if ((c = getc(bnfp[k])) == EOF) |
217 |
< |
quiterr("error reading barney file"); |
218 |
< |
else |
219 |
< |
colval(outline[i],k) = gmap[c]; |
220 |
< |
if (fwritescan(outline, xmax, rafp) < 0) |
214 |
> |
for (i = 0; i < xmax; i++) { |
215 |
> |
outline[i][RED] = getc(bnfp[0]); |
216 |
> |
outline[i][GRN] = getc(bnfp[1]); |
217 |
> |
outline[i][BLU] = getc(bnfp[2]); |
218 |
> |
} |
219 |
> |
if (feof(bnfp[0]) || feof(bnfp[1]) || feof(bnfp[2])) |
220 |
> |
quiterr("error reading barney file"); |
221 |
> |
gambs_colrs(outline, xmax); |
222 |
> |
if (bradj) |
223 |
> |
shiftcolrs(outline, xmax, bradj); |
224 |
> |
if (fwritecolrs(outline, xmax, rafp) < 0) |
225 |
|
quiterr("error writing RADIANCE file"); |
226 |
|
} |
227 |
< |
free((char *)outline); |
227 |
> |
free((void *)outline); |
228 |
|
} |