1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_skel.c,v 2.11 2003/02/22 02:07:28 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Skeletal 24-bit image conversion program. Replace "skel" |
6 |
* in this file with a more appropriate image type identifier. |
7 |
* |
8 |
* The Rmakefile entry should look something like this: |
9 |
* ra_skel: ra_skel.o |
10 |
* cc $(CFLAGS) -o ra_skel ra_skel.o -lrt -lm |
11 |
* ra_skel.o: ../common/color.h ../common/resolu.h |
12 |
* |
13 |
* If you like to do things the hard way, you can link directly |
14 |
* to the object files "color.o colrops.o resolu.o header.o" in |
15 |
* the common subdirectory instead of using the -lrt library. |
16 |
*/ |
17 |
|
18 |
#include <stdio.h> |
19 |
#include <math.h> |
20 |
#include <time.h> |
21 |
|
22 |
#include "platform.h" |
23 |
#include "color.h" |
24 |
#include "resolu.h" |
25 |
|
26 |
double gamcor = 2.2; /* gamma correction */ |
27 |
|
28 |
int bradj = 0; /* brightness adjustment */ |
29 |
|
30 |
char *progname; |
31 |
|
32 |
int xmax, ymax; |
33 |
|
34 |
|
35 |
main(argc, argv) |
36 |
int argc; |
37 |
char *argv[]; |
38 |
{ |
39 |
int reverse = 0; |
40 |
int i; |
41 |
|
42 |
progname = argv[0]; |
43 |
|
44 |
for (i = 1; i < argc; i++) |
45 |
if (argv[i][0] == '-') |
46 |
switch (argv[i][1]) { |
47 |
case 'g': /* gamma correction */ |
48 |
gamcor = atof(argv[++i]); |
49 |
break; |
50 |
case 'e': /* exposure adjustment */ |
51 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
52 |
goto userr; |
53 |
bradj = atoi(argv[++i]); |
54 |
break; |
55 |
case 'r': /* reverse conversion */ |
56 |
reverse = 1; |
57 |
break; |
58 |
default: |
59 |
goto userr; |
60 |
} |
61 |
else |
62 |
break; |
63 |
|
64 |
if (i < argc-2) |
65 |
goto userr; |
66 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
67 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
68 |
progname, argv[i]); |
69 |
exit(1); |
70 |
} |
71 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
72 |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
73 |
progname, argv[i+1]); |
74 |
exit(1); |
75 |
} |
76 |
SET_FILE_BINARY(stdin); |
77 |
SET_FILE_BINARY(stdout); |
78 |
setcolrgam(gamcor); /* set up gamma correction */ |
79 |
if (reverse) { |
80 |
/* get their image resolution */ |
81 |
read_skel_head(&xmax, &ymax); |
82 |
/* put our header */ |
83 |
newheader("RADIANCE", stdout); |
84 |
printargs(i, argv, stdout); |
85 |
fputformat(COLRFMT, stdout); |
86 |
putchar('\n'); |
87 |
fprtresolu(xmax, ymax, stdout); |
88 |
/* convert file */ |
89 |
skel2ra(); |
90 |
} else { |
91 |
/* get our header */ |
92 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
93 |
fgetresolu(&xmax, &ymax, stdin) < 0) |
94 |
quiterr("bad picture format"); |
95 |
/* write their header */ |
96 |
write_skel_head(xmax, ymax); |
97 |
/* convert file */ |
98 |
ra2skel(); |
99 |
} |
100 |
exit(0); |
101 |
userr: |
102 |
fprintf(stderr, |
103 |
"Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
104 |
progname); |
105 |
exit(1); |
106 |
} |
107 |
|
108 |
|
109 |
quiterr(err) /* print message and exit */ |
110 |
char *err; |
111 |
{ |
112 |
if (err != NULL) { |
113 |
fprintf(stderr, "%s: %s\n", progname, err); |
114 |
exit(1); |
115 |
} |
116 |
exit(0); |
117 |
} |
118 |
|
119 |
|
120 |
skel2ra() /* convert 24-bit scanlines to Radiance picture */ |
121 |
{ |
122 |
COLR *scanout; |
123 |
register int x; |
124 |
int y; |
125 |
/* allocate scanline */ |
126 |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
127 |
if (scanout == NULL) |
128 |
quiterr("out of memory in skel2ra"); |
129 |
/* convert image */ |
130 |
for (y = ymax-1; y >= 0; y--) { |
131 |
for (x = 0; x < xmax; x++) { |
132 |
scanout[x][RED] = getc(stdin); |
133 |
scanout[x][GRN] = getc(stdin); |
134 |
scanout[x][BLU] = getc(stdin); |
135 |
} |
136 |
if (feof(stdin) | ferror(stdin)) |
137 |
quiterr("error reading skel image"); |
138 |
/* undo gamma */ |
139 |
gambs_colrs(scanout, xmax); |
140 |
if (bradj) /* adjust exposure */ |
141 |
shiftcolrs(scanout, xmax, bradj); |
142 |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
143 |
quiterr("error writing Radiance picture"); |
144 |
} |
145 |
/* free scanline */ |
146 |
free((void *)scanout); |
147 |
} |
148 |
|
149 |
|
150 |
ra2skel() /* convert Radiance scanlines to 24-bit */ |
151 |
{ |
152 |
COLR *scanin; |
153 |
register int x; |
154 |
int y; |
155 |
/* allocate scanline */ |
156 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
157 |
if (scanin == NULL) |
158 |
quiterr("out of memory in ra2skel"); |
159 |
/* convert image */ |
160 |
for (y = ymax-1; y >= 0; y--) { |
161 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
162 |
quiterr("error reading Radiance picture"); |
163 |
if (bradj) /* adjust exposure */ |
164 |
shiftcolrs(scanin, xmax, bradj); |
165 |
colrs_gambs(scanin, xmax); /* gamma correction */ |
166 |
for (x = 0; x < xmax; x++) { |
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 skel file"); |
173 |
} |
174 |
/* free scanline */ |
175 |
free((void *)scanin); |
176 |
} |