1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_skel.c,v 2.14 2025/06/06 19:11:21 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 -lrtrad -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 -lrtrad library. |
16 |
*/ |
17 |
|
18 |
#include <stdio.h> |
19 |
#include <math.h> |
20 |
#include <time.h> |
21 |
#include "platform.h" |
22 |
#include "color.h" |
23 |
#include "resolu.h" |
24 |
|
25 |
double gamcor = 2.2; /* gamma correction */ |
26 |
|
27 |
int bradj = 0; /* brightness adjustment */ |
28 |
|
29 |
int xmax, ymax; |
30 |
|
31 |
|
32 |
main(argc, argv) |
33 |
int argc; |
34 |
char *argv[]; |
35 |
{ |
36 |
int reverse = 0; |
37 |
int i; |
38 |
|
39 |
fixargv0(argv[0]); |
40 |
|
41 |
for (i = 1; i < argc; i++) |
42 |
if (argv[i][0] == '-') |
43 |
switch (argv[i][1]) { |
44 |
case 'g': /* gamma correction */ |
45 |
gamcor = atof(argv[++i]); |
46 |
break; |
47 |
case 'e': /* exposure adjustment */ |
48 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
49 |
goto userr; |
50 |
bradj = atoi(argv[++i]); |
51 |
break; |
52 |
case 'r': /* reverse conversion */ |
53 |
reverse = 1; |
54 |
break; |
55 |
default: |
56 |
goto userr; |
57 |
} |
58 |
else |
59 |
break; |
60 |
|
61 |
if (i < argc-2) |
62 |
goto userr; |
63 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
64 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
65 |
progname, argv[i]); |
66 |
exit(1); |
67 |
} |
68 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
69 |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
70 |
progname, argv[i+1]); |
71 |
exit(1); |
72 |
} |
73 |
SET_FILE_BINARY(stdin); |
74 |
SET_FILE_BINARY(stdout); |
75 |
setcolrgam(gamcor); /* set up gamma correction */ |
76 |
if (reverse) { |
77 |
/* get their image resolution */ |
78 |
read_skel_head(&xmax, &ymax); |
79 |
/* put our header */ |
80 |
newheader("RADIANCE", stdout); |
81 |
printargs(i, argv, stdout); |
82 |
fputformat(COLRFMT, stdout); |
83 |
putchar('\n'); |
84 |
fprtresolu(xmax, ymax, stdout); |
85 |
/* convert file */ |
86 |
skel2ra(); |
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 |
write_skel_head(xmax, ymax); |
94 |
/* convert file */ |
95 |
ra2skel(); |
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 |
skel2ra() /* 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 skel2ra"); |
126 |
/* convert image */ |
127 |
for (y = ymax-1; y >= 0; y--) { |
128 |
for (x = 0; x < xmax; x++) { |
129 |
scanout[x][RED] = getc(stdin); |
130 |
scanout[x][GRN] = getc(stdin); |
131 |
scanout[x][BLU] = getc(stdin); |
132 |
} |
133 |
if (feof(stdin) | ferror(stdin)) |
134 |
quiterr("error reading skel 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((void *)scanout); |
144 |
} |
145 |
|
146 |
|
147 |
ra2skel() /* 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 ra2skel"); |
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(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 skel file"); |
170 |
} |
171 |
/* free scanline */ |
172 |
free((void *)scanin); |
173 |
} |