1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
12 |
|
|
13 |
|
#include <stdio.h> |
14 |
|
|
15 |
+ |
#ifdef MSDOS |
16 |
+ |
#include <fcntl.h> |
17 |
+ |
#endif |
18 |
+ |
|
19 |
+ |
#include <math.h> |
20 |
+ |
|
21 |
|
#include "color.h" |
22 |
|
|
23 |
< |
extern double atof(), pow(); |
23 |
> |
#include "resolu.h" |
24 |
|
|
25 |
< |
double gamma = 2.0; /* gamma correction */ |
25 |
> |
extern char *malloc(); |
26 |
|
|
27 |
+ |
double gamcor = 2.0; /* gamma correction */ |
28 |
+ |
|
29 |
+ |
int bradj = 0; /* brightness adjustment */ |
30 |
+ |
|
31 |
|
char *progname; |
32 |
|
|
33 |
|
char errmsg[128]; |
43 |
|
{ |
44 |
|
int reverse = 0; |
45 |
|
int i; |
46 |
< |
|
46 |
> |
#ifdef MSDOS |
47 |
> |
extern int _fmode; |
48 |
> |
_fmode = O_BINARY; |
49 |
> |
setmode(fileno(stdin), O_BINARY); |
50 |
> |
setmode(fileno(stdout), O_BINARY); |
51 |
> |
#endif |
52 |
|
progname = argv[0]; |
53 |
|
|
54 |
|
for (i = 1; i < argc; i++) |
55 |
|
if (argv[i][0] == '-') |
56 |
|
switch (argv[i][1]) { |
57 |
|
case 'g': |
58 |
< |
gamma = atof(argv[++i]); |
58 |
> |
gamcor = atof(argv[++i]); |
59 |
|
break; |
60 |
|
case 'r': |
61 |
|
reverse = !reverse; |
62 |
|
break; |
63 |
+ |
case 'e': |
64 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
65 |
+ |
goto userr; |
66 |
+ |
bradj = atoi(argv[++i]); |
67 |
+ |
break; |
68 |
|
default: |
69 |
|
goto userr; |
70 |
|
} |
71 |
|
else |
72 |
|
break; |
73 |
+ |
/* set gamma correction */ |
74 |
+ |
setcolrgam(gamcor); |
75 |
|
|
76 |
|
if (reverse) { |
77 |
|
if (i > argc-1 || i < argc-2) |
88 |
|
quiterr(errmsg); |
89 |
|
} |
90 |
|
/* put header */ |
91 |
< |
printargs(argc, argv, rafp); |
91 |
> |
printargs(i, argv, rafp); |
92 |
> |
fputformat(COLRFMT, rafp); |
93 |
|
putc('\n', rafp); |
94 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, rafp); |
94 |
> |
fprtresolu(xmax, ymax, rafp); |
95 |
|
/* convert file */ |
96 |
|
bn2ra(); |
97 |
|
} else { |
105 |
|
quiterr(errmsg); |
106 |
|
} |
107 |
|
/* get header */ |
108 |
< |
getheader(rafp, NULL); |
109 |
< |
if (fgetresolu(&xmax, &ymax, rafp) != (YMAJOR|YDECR)) |
108 |
> |
if (checkheader(rafp, COLRFMT, NULL) < 0 || |
109 |
> |
fgetresolu(&xmax, &ymax, rafp) < 0) |
110 |
|
quiterr("bad RADIANCE format"); |
111 |
|
if (openbarney(argv[i+1], "w") < 0) { |
112 |
|
sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]); |
117 |
|
} |
118 |
|
quiterr(NULL); |
119 |
|
userr: |
120 |
< |
fprintf(stderr, "Usage: %s {input|-} output\n", progname); |
98 |
< |
fprintf(stderr, " or: %s -r [-g gamma] input [output|-]\n", |
120 |
> |
fprintf(stderr, "Usage: %s [-g gamma][-e +/-stops] {input|-} output\n", |
121 |
|
progname); |
122 |
+ |
fprintf(stderr, " or: %s -r [-g gamma][-e +/-stops] input [output|-]\n", |
123 |
+ |
progname); |
124 |
|
exit(1); |
125 |
|
} |
126 |
|
|
189 |
|
|
190 |
|
ra2bn() /* convert radiance to barneyscan */ |
191 |
|
{ |
192 |
< |
unsigned char gmap[1024]; |
193 |
< |
register int i,k,c; |
170 |
< |
register COLOR *inline; |
192 |
> |
register int i; |
193 |
> |
register COLR *inl; |
194 |
|
int j; |
195 |
|
|
196 |
< |
if ((inline = (COLOR *)malloc(xmax*sizeof(COLOR))) == NULL) |
196 |
> |
if ((inl = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
197 |
|
quiterr("out of memory"); |
175 |
– |
for (i = 0; i < 1024; i++) |
176 |
– |
gmap[i] = 256.*pow((i+.5)/1024., 1./gamma); |
198 |
|
for (j = 0; j < ymax; j++) { |
199 |
< |
if (freadscan(inline, xmax, rafp) < 0) |
199 |
> |
if (freadcolrs(inl, xmax, rafp) < 0) |
200 |
|
quiterr("error reading RADIANCE file"); |
201 |
< |
for (i = 0; i < xmax; i++) |
202 |
< |
for (k = 0; k < 3; k++) { |
203 |
< |
c = 1024.*colval(inline[i],k); |
204 |
< |
if (c >= 1024) |
205 |
< |
c = 1023; |
206 |
< |
putc(gmap[c], bnfp[k]); |
207 |
< |
} |
201 |
> |
if (bradj) |
202 |
> |
shiftcolrs(inl, xmax, bradj); |
203 |
> |
colrs_gambs(inl, xmax); |
204 |
> |
for (i = 0; i < xmax; i++) { |
205 |
> |
putc(inl[i][RED], bnfp[0]); |
206 |
> |
putc(inl[i][GRN], bnfp[1]); |
207 |
> |
putc(inl[i][BLU], bnfp[2]); |
208 |
> |
} |
209 |
> |
if (ferror(bnfp[0]) || ferror(bnfp[1]) || ferror(bnfp[2])) |
210 |
> |
quiterr("error writing Barney files"); |
211 |
|
} |
212 |
< |
free((char *)inline); |
212 |
> |
free((char *)inl); |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
bn2ra() /* convert barneyscan to radiance */ |
217 |
|
{ |
218 |
< |
float gmap[256]; |
219 |
< |
register int i,k,c; |
196 |
< |
register COLOR *outline; |
218 |
> |
register int i; |
219 |
> |
register COLR *outline; |
220 |
|
int j; |
221 |
|
|
222 |
< |
if ((outline = (COLOR *)malloc(xmax*sizeof(COLOR))) == NULL) |
222 |
> |
if ((outline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
223 |
|
quiterr("out of memory"); |
201 |
– |
for (i = 0; i < 256; i++) |
202 |
– |
gmap[i] = pow((i+.5)/256., gamma); |
224 |
|
for (j = 0; j < ymax; j++) { |
225 |
< |
for (i = 0; i < xmax; i++) |
226 |
< |
for (k = 0; k < 3; k++) |
227 |
< |
if ((c = getc(bnfp[k])) == EOF) |
228 |
< |
quiterr("error reading barney file"); |
229 |
< |
else |
230 |
< |
colval(outline[i],k) = gmap[c]; |
231 |
< |
if (fwritescan(outline, xmax, rafp) < 0) |
225 |
> |
for (i = 0; i < xmax; i++) { |
226 |
> |
outline[i][RED] = getc(bnfp[0]); |
227 |
> |
outline[i][GRN] = getc(bnfp[1]); |
228 |
> |
outline[i][BLU] = getc(bnfp[2]); |
229 |
> |
} |
230 |
> |
if (feof(bnfp[0]) || feof(bnfp[1]) || feof(bnfp[2])) |
231 |
> |
quiterr("error reading barney file"); |
232 |
> |
gambs_colrs(outline, xmax); |
233 |
> |
if (bradj) |
234 |
> |
shiftcolrs(outline, xmax, bradj); |
235 |
> |
if (fwritecolrs(outline, xmax, rafp) < 0) |
236 |
|
quiterr("error writing RADIANCE file"); |
237 |
|
} |
238 |
|
free((char *)outline); |