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 |
#ifdef MSDOS |
13 |
#include <fcntl.h> |
14 |
#endif |
15 |
#include "color.h" |
16 |
#include "resolu.h" |
17 |
|
18 |
extern char *malloc(); |
19 |
|
20 |
double gamma = 2.2; /* gamma correction */ |
21 |
|
22 |
int bradj = 0; /* brightness adjustment */ |
23 |
|
24 |
char *progname; |
25 |
|
26 |
int xmax, ymax; |
27 |
|
28 |
|
29 |
main(argc, argv) |
30 |
int argc; |
31 |
char *argv[]; |
32 |
{ |
33 |
extern long getint(); |
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': /* gamma correction */ |
43 |
gamma = atof(argv[++i]); |
44 |
break; |
45 |
case 'e': /* exposure adjustment */ |
46 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
47 |
goto userr; |
48 |
bradj = atoi(argv[++i]); |
49 |
break; |
50 |
case 'r': /* reverse conversion */ |
51 |
reverse = 1; |
52 |
break; |
53 |
default: |
54 |
goto userr; |
55 |
} |
56 |
else |
57 |
break; |
58 |
|
59 |
if (i < argc-2) |
60 |
goto userr; |
61 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
62 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
63 |
progname, argv[i]); |
64 |
exit(1); |
65 |
} |
66 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
67 |
fprintf(stderr, "can't open output \"%s\"\n", |
68 |
progname, argv[i+1]); |
69 |
exit(1); |
70 |
} |
71 |
#ifdef MSDOS |
72 |
setmode(fileno(stdin), O_BINARY); |
73 |
setmode(fileno(stdout), O_BINARY); |
74 |
#endif |
75 |
setcolrgam(gamma); /* set up gamma correction */ |
76 |
if (reverse) { |
77 |
/* get their image resolution */ |
78 |
xmax = getint(4, stdin); |
79 |
ymax = getint(4, stdin); |
80 |
/* put our header */ |
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 |
(void)getc(stdin); /* toss alpha */ |
130 |
scanout[x][RED] = getc(stdin); |
131 |
scanout[x][GRN] = getc(stdin); |
132 |
scanout[x][BLU] = getc(stdin); |
133 |
if (feof(stdin) || ferror(stdin)) |
134 |
quiterr("error reading AVS image"); |
135 |
/* undo gamma */ |
136 |
gambs_colrs(scanout, xmax); |
137 |
if (bradj) /* adjust exposure */ |
138 |
shiftcolrs(scanout, xmax, bradj); |
139 |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
140 |
quiterr("error writing Radiance picture"); |
141 |
} |
142 |
/* free scanline */ |
143 |
free((char *)scanout); |
144 |
} |
145 |
|
146 |
|
147 |
ra2avs() /* convert Radiance scanlines to 24-bit */ |
148 |
{ |
149 |
COLR *scanin; |
150 |
register int x; |
151 |
int y; |
152 |
/* allocate scanline */ |
153 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
154 |
if (scanin == NULL) |
155 |
quiterr("out of memory in ra2avs"); |
156 |
/* convert image */ |
157 |
for (y = ymax-1; y >= 0; y--) { |
158 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
159 |
quiterr("error reading Radiance picture"); |
160 |
if (bradj) /* adjust exposure */ |
161 |
shiftcolrs(scanin, xmax, bradj); |
162 |
colrs_gambs(scanin, xmax); /* gamma correction */ |
163 |
for (x = 0; x < xmax; x++) { |
164 |
putc(0, stdout); /* no alpha */ |
165 |
putc(scanin[x][RED], stdout); |
166 |
putc(scanin[x][GRN], stdout); |
167 |
putc(scanin[x][BLU], stdout); |
168 |
} |
169 |
if (ferror(stdout)) |
170 |
quiterr("error writing AVS file"); |
171 |
} |
172 |
/* free scanline */ |
173 |
free((char *)scanin); |
174 |
} |