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