1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* program to convert between RADIANCE and 24-bit TIFF files. |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
|
13 |
#include "tiffio.h" |
14 |
|
15 |
#include "color.h" |
16 |
|
17 |
#include "resolu.h" |
18 |
|
19 |
extern double atof(); |
20 |
|
21 |
extern char *malloc(), *realloc(); |
22 |
|
23 |
int lzcomp = 0; /* use Lempel-Ziv compression? */ |
24 |
|
25 |
int greyscale = 0; /* produce greyscale image? */ |
26 |
|
27 |
double gamma = 2.2; /* gamma correction */ |
28 |
|
29 |
int bradj = 0; /* brightness adjustment */ |
30 |
|
31 |
char *progname; |
32 |
|
33 |
|
34 |
main(argc, argv) |
35 |
int argc; |
36 |
char *argv[]; |
37 |
{ |
38 |
int reverse = 0; |
39 |
int i; |
40 |
|
41 |
progname = argv[0]; |
42 |
|
43 |
for (i = 1; i < argc; i++) |
44 |
if (argv[i][0] == '-') |
45 |
switch (argv[i][1]) { |
46 |
case 'g': |
47 |
gamma = atof(argv[++i]); |
48 |
break; |
49 |
case 'z': |
50 |
lzcomp = !lzcomp; |
51 |
break; |
52 |
case 'b': |
53 |
greyscale = !greyscale; |
54 |
break; |
55 |
case 'e': |
56 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
57 |
goto userr; |
58 |
bradj = atoi(argv[++i]); |
59 |
break; |
60 |
case 'r': |
61 |
reverse = !reverse; |
62 |
break; |
63 |
case '\0': |
64 |
goto doneopts; |
65 |
default: |
66 |
goto userr; |
67 |
} |
68 |
else |
69 |
break; |
70 |
doneopts: |
71 |
setcolrgam(gamma); |
72 |
|
73 |
if (reverse) |
74 |
if (i != argc-2 && i != argc-1) |
75 |
goto userr; |
76 |
else |
77 |
tiff2ra(argv[i], argv[i+1]); |
78 |
else |
79 |
if (i != argc-2) |
80 |
goto userr; |
81 |
else |
82 |
ra2tiff(argv[i], argv[i+1]); |
83 |
|
84 |
exit(0); |
85 |
userr: |
86 |
fprintf(stderr, |
87 |
"Usage: %s [-r][-b][-z][-e +/-stops][-g gamma] input output\n", |
88 |
progname); |
89 |
exit(1); |
90 |
} |
91 |
|
92 |
|
93 |
quiterr(err) /* print message and exit */ |
94 |
char *err; |
95 |
{ |
96 |
if (err != NULL) { |
97 |
fprintf(stderr, "%s: %s\n", progname, err); |
98 |
exit(1); |
99 |
} |
100 |
exit(0); |
101 |
} |
102 |
|
103 |
|
104 |
tiff2ra(inpf, outf) /* convert TIFF file to Radiance picture */ |
105 |
char *inpf, *outf; |
106 |
{ |
107 |
unsigned long xmax, ymax; |
108 |
TIFF *tif; |
109 |
unsigned short pconfig, nsamps; |
110 |
unsigned short hi; |
111 |
register BYTE *scanin; |
112 |
register COLR *scanout; |
113 |
register int x; |
114 |
int y; |
115 |
/* open/check TIFF file */ |
116 |
if ((tif = TIFFOpen(inpf, "r")) == NULL) |
117 |
quiterr("cannot open TIFF input"); |
118 |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamps) || |
119 |
(nsamps != 1 && nsamps != 3)) |
120 |
quiterr("unsupported samples per pixel"); |
121 |
if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &hi) || hi != 8) |
122 |
quiterr("unsupported bits per sample"); |
123 |
if (TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &hi) && |
124 |
hi != (nsamps==1 ? 1 : 2)) |
125 |
quiterr("unsupported photometric interpretation"); |
126 |
if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pconfig) || |
127 |
(pconfig != 1 && pconfig != 2)) |
128 |
quiterr("unsupported planar configuration"); |
129 |
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &xmax) || |
130 |
!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &ymax)) |
131 |
quiterr("unknown input image resolution"); |
132 |
/* allocate scanlines */ |
133 |
scanin = (BYTE *)malloc(TIFFScanlineSize(tif)); |
134 |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
135 |
if (scanin == NULL || scanout == NULL) |
136 |
quiterr("out of memory in tiff2ra"); |
137 |
/* open output and write header */ |
138 |
if (outf != NULL && strcmp(outf, "-") && |
139 |
freopen(outf, "w", stdout) == NULL) |
140 |
quiterr("cannot open Radiance output file"); |
141 |
fputs(progname, stdout); |
142 |
if (bradj) |
143 |
printf(" -e %+d", bradj); |
144 |
fputs(" -r\n", stdout); |
145 |
fputformat(COLRFMT, stdout); |
146 |
putchar('\n'); |
147 |
fprtresolu(xmax, ymax, stdout); |
148 |
/* convert image */ |
149 |
if (nsamps == 1) |
150 |
pconfig = 1; |
151 |
for (y = 0; y < ymax; y++) { |
152 |
if (pconfig == 1) { |
153 |
if (TIFFReadScanline(tif, scanin, y, 0) < 0) |
154 |
goto readerr; |
155 |
if (nsamps == 1) |
156 |
for (x = 0; x < xmax; x++) |
157 |
scanout[x][RED] = |
158 |
scanout[x][GRN] = |
159 |
scanout[x][BLU] = scanin[x]; |
160 |
else |
161 |
for (x = 0; x < xmax; x++) { |
162 |
scanout[x][RED] = scanin[3*x]; |
163 |
scanout[x][GRN] = scanin[3*x+1]; |
164 |
scanout[x][BLU] = scanin[3*x+2]; |
165 |
} |
166 |
} else { |
167 |
if (TIFFReadScanline(tif, scanin, y, 0) < 0) |
168 |
goto readerr; |
169 |
for (x = 0; x < xmax; x++) |
170 |
scanout[x][RED] = scanin[x]; |
171 |
if (TIFFReadScanline(tif, scanin, y, 1) < 0) |
172 |
goto readerr; |
173 |
for (x = 0; x < xmax; x++) |
174 |
scanout[x][GRN] = scanin[x]; |
175 |
if (TIFFReadScanline(tif, scanin, y, 2) < 0) |
176 |
goto readerr; |
177 |
for (x = 0; x < xmax; x++) |
178 |
scanout[x][BLU] = scanin[x]; |
179 |
} |
180 |
gambs_colrs(scanout, xmax); |
181 |
if (bradj) |
182 |
shiftcolrs(scanout, xmax, bradj); |
183 |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
184 |
quiterr("error writing Radiance picture"); |
185 |
} |
186 |
/* clean up */ |
187 |
free((char *)scanin); |
188 |
free((char *)scanout); |
189 |
TIFFClose(tif); |
190 |
return; |
191 |
readerr: |
192 |
quiterr("error reading TIFF input"); |
193 |
} |
194 |
|
195 |
|
196 |
ra2tiff(inpf, outf) /* convert Radiance file to 24-bit TIFF */ |
197 |
char *inpf, *outf; |
198 |
{ |
199 |
TIFF *tif; |
200 |
int xmax, ymax; |
201 |
BYTE *scanout; |
202 |
COLR *scanin; |
203 |
register int x; |
204 |
int y; |
205 |
/* open Radiance file */ |
206 |
if (strcmp(inpf, "-") && freopen(inpf, "r", stdin) == NULL) |
207 |
quiterr("cannot open Radiance input file"); |
208 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
209 |
fgetresolu(&xmax, &ymax, stdin) < 0) |
210 |
quiterr("bad Radiance picture"); |
211 |
/* open TIFF file */ |
212 |
if ((tif = TIFFOpen(outf, "w")) == NULL) |
213 |
quiterr("cannot open TIFF output"); |
214 |
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (unsigned long)xmax); |
215 |
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (unsigned long)ymax); |
216 |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, greyscale ? 1 : 3); |
217 |
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
218 |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, greyscale ? 1 : 2); |
219 |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, 1); |
220 |
if (lzcomp) |
221 |
TIFFSetField(tif, TIFFTAG_COMPRESSION, (unsigned short)5); |
222 |
/* allocate scanlines */ |
223 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
224 |
scanout = (BYTE *)malloc(TIFFScanlineSize(tif)); |
225 |
if (scanin == NULL || scanout == NULL) |
226 |
quiterr("out of memory in ra2tiff"); |
227 |
/* convert image */ |
228 |
for (y = 0; y < ymax; y++) { |
229 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
230 |
quiterr("error reading Radiance picture"); |
231 |
if (bradj) |
232 |
shiftcolrs(scanin, xmax, bradj); |
233 |
if (greyscale) { |
234 |
for (x = 0; x < xmax; x++) |
235 |
scanin[x][GRN] = normbright(scanin[x]); |
236 |
colrs_gambs(scanin, xmax); |
237 |
for (x = 0; x < xmax; x++) |
238 |
scanout[x] = scanin[x][GRN]; |
239 |
} else { |
240 |
colrs_gambs(scanin, xmax); |
241 |
for (x = 0; x < xmax; x++) { |
242 |
scanout[3*x] = scanin[x][RED]; |
243 |
scanout[3*x+1] = scanin[x][GRN]; |
244 |
scanout[3*x+2] = scanin[x][BLU]; |
245 |
} |
246 |
} |
247 |
if (TIFFWriteScanline(tif, scanout, y, 0) < 0) |
248 |
quiterr("error writing TIFF output"); |
249 |
} |
250 |
/* clean up */ |
251 |
free((char *)scanin); |
252 |
free((char *)scanout); |
253 |
TIFFClose(tif); |
254 |
} |