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