1 |
greg |
2.3 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
|
3 |
greg |
2.1 |
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
greg |
2.4 |
/* |
8 |
|
|
* Skeletal 24-bit image conversion program. Replace "skel" |
9 |
greg |
2.1 |
* in this file with a more appropriate image type identifier. |
10 |
|
|
* |
11 |
|
|
* The Makefile 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 |
greg |
2.5 |
#include <math.h> |
23 |
greg |
2.3 |
#ifdef MSDOS |
24 |
|
|
#include <fcntl.h> |
25 |
|
|
#endif |
26 |
greg |
2.1 |
#include "color.h" |
27 |
|
|
#include "resolu.h" |
28 |
|
|
|
29 |
greg |
2.3 |
extern char *malloc(); |
30 |
greg |
2.1 |
|
31 |
greg |
2.6 |
double gamcor = 2.2; /* gamma correction */ |
32 |
greg |
2.1 |
|
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 |
greg |
2.6 |
gamcor = atof(argv[++i]); |
54 |
greg |
2.1 |
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, "can't open output \"%s\"\n", |
78 |
|
|
progname, argv[i+1]); |
79 |
|
|
exit(1); |
80 |
|
|
} |
81 |
greg |
2.3 |
#ifdef MSDOS |
82 |
|
|
setmode(fileno(stdin), O_BINARY); |
83 |
|
|
setmode(fileno(stdout), O_BINARY); |
84 |
|
|
#endif |
85 |
greg |
2.6 |
setcolrgam(gamcor); /* set up gamma correction */ |
86 |
greg |
2.1 |
if (reverse) { |
87 |
|
|
/* get their image resolution */ |
88 |
|
|
read_skel_head(&xmax, &ymax); |
89 |
|
|
/* put our header */ |
90 |
|
|
printargs(i, argv, stdout); |
91 |
|
|
fputformat(COLRFMT, stdout); |
92 |
|
|
putchar('\n'); |
93 |
|
|
fprtresolu(xmax, ymax, stdout); |
94 |
|
|
/* convert file */ |
95 |
|
|
skel2ra(); |
96 |
|
|
} else { |
97 |
|
|
/* get our header */ |
98 |
|
|
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
99 |
|
|
fgetresolu(&xmax, &ymax, stdin) < 0) |
100 |
|
|
quiterr("bad picture format"); |
101 |
|
|
/* write their header */ |
102 |
|
|
write_skel_head(xmax, ymax); |
103 |
|
|
/* convert file */ |
104 |
|
|
ra2skel(); |
105 |
|
|
} |
106 |
|
|
exit(0); |
107 |
|
|
userr: |
108 |
|
|
fprintf(stderr, |
109 |
|
|
"Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
110 |
|
|
progname); |
111 |
|
|
exit(1); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
|
115 |
|
|
quiterr(err) /* print message and exit */ |
116 |
|
|
char *err; |
117 |
|
|
{ |
118 |
|
|
if (err != NULL) { |
119 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
120 |
|
|
exit(1); |
121 |
|
|
} |
122 |
|
|
exit(0); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
skel2ra() /* convert 24-bit scanlines to Radiance picture */ |
127 |
|
|
{ |
128 |
|
|
COLR *scanout; |
129 |
|
|
register int x; |
130 |
|
|
int y; |
131 |
|
|
/* allocate scanline */ |
132 |
|
|
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
133 |
|
|
if (scanout == NULL) |
134 |
|
|
quiterr("out of memory in skel2ra"); |
135 |
|
|
/* convert image */ |
136 |
|
|
for (y = ymax-1; y >= 0; y--) { |
137 |
|
|
scanout[x][RED] = getc(stdin); |
138 |
|
|
scanout[x][GRN] = getc(stdin); |
139 |
|
|
scanout[x][BLU] = getc(stdin); |
140 |
|
|
if (feof(stdin) || ferror(stdin)) |
141 |
|
|
quiterr("error reading skel image"); |
142 |
|
|
/* undo gamma */ |
143 |
|
|
gambs_colrs(scanout, xmax); |
144 |
|
|
if (bradj) /* adjust exposure */ |
145 |
|
|
shiftcolrs(scanout, xmax, bradj); |
146 |
|
|
if (fwritecolrs(scanout, xmax, stdout) < 0) |
147 |
|
|
quiterr("error writing Radiance picture"); |
148 |
|
|
} |
149 |
|
|
/* free scanline */ |
150 |
|
|
free((char *)scanout); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
|
154 |
|
|
ra2skel() /* convert Radiance scanlines to 24-bit */ |
155 |
|
|
{ |
156 |
|
|
COLR *scanin; |
157 |
|
|
register int x; |
158 |
|
|
int y; |
159 |
|
|
/* allocate scanline */ |
160 |
|
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
161 |
|
|
if (scanin == NULL) |
162 |
|
|
quiterr("out of memory in ra2skel"); |
163 |
|
|
/* convert image */ |
164 |
|
|
for (y = ymax-1; y >= 0; y--) { |
165 |
|
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
166 |
|
|
quiterr("error reading Radiance picture"); |
167 |
|
|
if (bradj) /* adjust exposure */ |
168 |
|
|
shiftcolrs(scanin, xmax, bradj); |
169 |
|
|
colrs_gambs(scanin, xmax); /* gamma correction */ |
170 |
|
|
for (x = 0; x < xmax; x++) { |
171 |
|
|
putc(scanin[x][RED], stdout); |
172 |
|
|
putc(scanin[x][GRN], stdout); |
173 |
|
|
putc(scanin[x][BLU], stdout); |
174 |
|
|
} |
175 |
|
|
if (ferror(stdout)) |
176 |
|
|
quiterr("error writing skel file"); |
177 |
|
|
} |
178 |
|
|
/* free scanline */ |
179 |
|
|
free((char *)scanin); |
180 |
|
|
} |