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