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