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