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