1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Convert Radiance file to/from AVS file. |
6 |
*/ |
7 |
|
8 |
#include <stdio.h> |
9 |
#include <math.h> |
10 |
#ifdef MSDOS |
11 |
#include <fcntl.h> |
12 |
#endif |
13 |
#include <time.h> |
14 |
#include "color.h" |
15 |
#include "resolu.h" |
16 |
|
17 |
double gamcor = 2.2; /* gamma correction */ |
18 |
|
19 |
int bradj = 0; /* brightness adjustment */ |
20 |
|
21 |
char *progname; |
22 |
|
23 |
int xmax, ymax; |
24 |
|
25 |
|
26 |
main(argc, argv) |
27 |
int argc; |
28 |
char *argv[]; |
29 |
{ |
30 |
extern long getint(); |
31 |
int reverse = 0; |
32 |
int i; |
33 |
|
34 |
progname = argv[0]; |
35 |
|
36 |
for (i = 1; i < argc; i++) |
37 |
if (argv[i][0] == '-') |
38 |
switch (argv[i][1]) { |
39 |
case 'g': /* gamma correction */ |
40 |
gamcor = atof(argv[++i]); |
41 |
break; |
42 |
case 'e': /* exposure adjustment */ |
43 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
44 |
goto userr; |
45 |
bradj = atoi(argv[++i]); |
46 |
break; |
47 |
case 'r': /* reverse conversion */ |
48 |
reverse = 1; |
49 |
break; |
50 |
default: |
51 |
goto userr; |
52 |
} |
53 |
else |
54 |
break; |
55 |
|
56 |
if (i < argc-2) |
57 |
goto userr; |
58 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
59 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
60 |
progname, argv[i]); |
61 |
exit(1); |
62 |
} |
63 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
64 |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
65 |
progname, argv[i+1]); |
66 |
exit(1); |
67 |
} |
68 |
#ifdef MSDOS |
69 |
setmode(fileno(stdin), O_BINARY); |
70 |
setmode(fileno(stdout), O_BINARY); |
71 |
#endif |
72 |
setcolrgam(gamcor); /* set up gamma correction */ |
73 |
if (reverse) { |
74 |
/* get their image resolution */ |
75 |
xmax = getint(4, stdin); |
76 |
ymax = getint(4, stdin); |
77 |
if (feof(stdin)) |
78 |
quiterr("empty input file"); |
79 |
/* put our header */ |
80 |
newheader("RADIANCE", stdout); |
81 |
printargs(i, argv, stdout); |
82 |
fputformat(COLRFMT, stdout); |
83 |
putchar('\n'); |
84 |
fprtresolu(xmax, ymax, stdout); |
85 |
/* convert file */ |
86 |
avs2ra(); |
87 |
} else { |
88 |
/* get our header */ |
89 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
90 |
fgetresolu(&xmax, &ymax, stdin) < 0) |
91 |
quiterr("bad picture format"); |
92 |
/* write their header */ |
93 |
putint((long)xmax, 4, stdout); |
94 |
putint((long)ymax, 4, stdout); |
95 |
/* convert file */ |
96 |
ra2avs(); |
97 |
} |
98 |
exit(0); |
99 |
userr: |
100 |
fprintf(stderr, |
101 |
"Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
102 |
progname); |
103 |
exit(1); |
104 |
} |
105 |
|
106 |
|
107 |
quiterr(err) /* print message and exit */ |
108 |
char *err; |
109 |
{ |
110 |
if (err != NULL) { |
111 |
fprintf(stderr, "%s: %s\n", progname, err); |
112 |
exit(1); |
113 |
} |
114 |
exit(0); |
115 |
} |
116 |
|
117 |
|
118 |
avs2ra() /* convert 24-bit scanlines to Radiance picture */ |
119 |
{ |
120 |
COLR *scanout; |
121 |
register int x; |
122 |
int y; |
123 |
/* allocate scanline */ |
124 |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
125 |
if (scanout == NULL) |
126 |
quiterr("out of memory in avs2ra"); |
127 |
/* convert image */ |
128 |
for (y = ymax-1; y >= 0; y--) { |
129 |
for (x = 0; x < xmax; x++) { |
130 |
(void)getc(stdin); /* toss alpha */ |
131 |
scanout[x][RED] = getc(stdin); |
132 |
scanout[x][GRN] = getc(stdin); |
133 |
scanout[x][BLU] = getc(stdin); |
134 |
} |
135 |
if (feof(stdin) | ferror(stdin)) |
136 |
quiterr("error reading AVS image"); |
137 |
/* undo gamma */ |
138 |
gambs_colrs(scanout, xmax); |
139 |
if (bradj) /* adjust exposure */ |
140 |
shiftcolrs(scanout, xmax, bradj); |
141 |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
142 |
quiterr("error writing Radiance picture"); |
143 |
} |
144 |
/* free scanline */ |
145 |
free((void *)scanout); |
146 |
} |
147 |
|
148 |
|
149 |
ra2avs() /* convert Radiance scanlines to 24-bit */ |
150 |
{ |
151 |
COLR *scanin; |
152 |
register int x; |
153 |
int y; |
154 |
/* allocate scanline */ |
155 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
156 |
if (scanin == NULL) |
157 |
quiterr("out of memory in ra2avs"); |
158 |
/* convert image */ |
159 |
for (y = ymax-1; y >= 0; y--) { |
160 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
161 |
quiterr("error reading Radiance picture"); |
162 |
if (bradj) /* adjust exposure */ |
163 |
shiftcolrs(scanin, xmax, bradj); |
164 |
colrs_gambs(scanin, xmax); /* gamma correction */ |
165 |
for (x = 0; x < xmax; x++) { |
166 |
putc(0, stdout); /* no alpha */ |
167 |
putc(scanin[x][RED], stdout); |
168 |
putc(scanin[x][GRN], stdout); |
169 |
putc(scanin[x][BLU], stdout); |
170 |
} |
171 |
if (ferror(stdout)) |
172 |
quiterr("error writing AVS file"); |
173 |
} |
174 |
/* free scanline */ |
175 |
free((void *)scanin); |
176 |
} |