1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Tone map SGILOG TIFF or Radiance picture and output 24-bit RGB TIFF |
6 |
*/ |
7 |
|
8 |
#include <stdio.h> |
9 |
#include <math.h> |
10 |
#include <time.h> |
11 |
#include "tiffio.h" |
12 |
#include "color.h" |
13 |
#include "tonemap.h" |
14 |
#include "resolu.h" |
15 |
|
16 |
|
17 |
TIFF *tifout; /* TIFF output */ |
18 |
int flags = TM_F_CAMERA; /* tone-mapping flags */ |
19 |
RGBPRIMP rgbp = stdprims; /* display primaries */ |
20 |
RGBPRIMS myprims; /* overriding display primaries */ |
21 |
double ldmax = 100.; /* maximum display luminance */ |
22 |
double lddyn = 32.; /* display dynamic range */ |
23 |
double gamv = 2.2; /* display gamma value */ |
24 |
|
25 |
short ortab[8] = { /* orientation conversion table */ |
26 |
YMAJOR|YDECR, |
27 |
YMAJOR|YDECR|XDECR, |
28 |
YMAJOR|XDECR, |
29 |
YMAJOR, |
30 |
YDECR, |
31 |
XDECR|YDECR, |
32 |
XDECR, |
33 |
0 |
34 |
}; |
35 |
|
36 |
typedef struct { |
37 |
FILE *fp; /* file pointer */ |
38 |
char fmt[32]; /* picture format */ |
39 |
double pa; /* pixel aspect ratio */ |
40 |
RESOLU rs; /* picture resolution */ |
41 |
} PICTURE; |
42 |
|
43 |
extern PICTURE *openpicture(); |
44 |
|
45 |
#define closepicture(p) (fclose((p)->fp),free((void *)(p))) |
46 |
|
47 |
|
48 |
main(argc, argv) |
49 |
int argc; |
50 |
char *argv[]; |
51 |
{ |
52 |
PICTURE *pin = NULL; |
53 |
TIFF *tin = NULL; |
54 |
int i, rval; |
55 |
|
56 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
57 |
switch (argv[i][1]) { |
58 |
case 'h': /* human observer settings */ |
59 |
flags = TM_F_HUMAN; |
60 |
break; |
61 |
case 's': /* toggle human contrast */ |
62 |
flags ^= TM_F_HCONTR; |
63 |
break; |
64 |
case 'c': /* toggle mesopic sens. */ |
65 |
flags ^= TM_F_MESOPIC; |
66 |
break; |
67 |
case 'l': /* toggle linear mapping */ |
68 |
flags ^= TM_F_LINEAR; |
69 |
break; |
70 |
case 'b': /* toggle greyscale output */ |
71 |
flags ^= TM_F_BW; |
72 |
break; |
73 |
case 'g': /* set display gamma */ |
74 |
if (argc-i < 2) goto userr; |
75 |
gamv = atof(argv[++i]); |
76 |
break; |
77 |
case 'u': /* set display maximum */ |
78 |
if (argc-i < 2) goto userr; |
79 |
ldmax = atof(argv[++i]); |
80 |
break; |
81 |
case 'd': /* set display dynamic range */ |
82 |
if (argc-i < 2) goto userr; |
83 |
lddyn = atof(argv[++i]); |
84 |
break; |
85 |
case 'p': /* set display primaries */ |
86 |
if (argc-i < 9) goto userr; |
87 |
myprims[RED][CIEX] = atof(argv[++i]); |
88 |
myprims[RED][CIEY] = atof(argv[++i]); |
89 |
myprims[GRN][CIEX] = atof(argv[++i]); |
90 |
myprims[GRN][CIEY] = atof(argv[++i]); |
91 |
myprims[BLU][CIEX] = atof(argv[++i]); |
92 |
myprims[BLU][CIEY] = atof(argv[++i]); |
93 |
myprims[WHT][CIEX] = atof(argv[++i]); |
94 |
myprims[WHT][CIEY] = atof(argv[++i]); |
95 |
rgbp = myprims; |
96 |
break; |
97 |
default: |
98 |
goto userr; |
99 |
} |
100 |
if (argc-i < 2) goto userr; |
101 |
if ((pin = openpicture(argv[i])) == NULL && |
102 |
(tin = TIFFOpen(argv[i], "r")) == NULL) { |
103 |
fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n", |
104 |
argv[0], argv[i]); |
105 |
exit(1); |
106 |
} |
107 |
if ((tifout = TIFFOpen(argv[i+1], "w")) == NULL) { |
108 |
fprintf(stderr, "%s: cannot open output TIFF \"%s\"\n", |
109 |
argv[0], argv[i+1]); |
110 |
exit(1); |
111 |
} |
112 |
if (pin != NULL) { |
113 |
rval = tmap_picture(argv[i], pin); |
114 |
closepicture(pin); |
115 |
} else { |
116 |
rval = tmap_tiff(argv[i], tin); |
117 |
TIFFClose(tin); |
118 |
} |
119 |
TIFFClose(tifout); |
120 |
exit(rval==0 ? 0 : 1); |
121 |
userr: |
122 |
fprintf(stderr, |
123 |
"Usage: %s [-h][-s][-c][-l][-b][-g gv][-d ld][-u lm][-p xr yr xg yg xb yb xw yw] input.{tif|pic} output.tif\n", |
124 |
argv[0]); |
125 |
exit(1); |
126 |
} |
127 |
|
128 |
|
129 |
int |
130 |
headline(s, pp) /* process line from header */ |
131 |
char *s; |
132 |
register PICTURE *pp; |
133 |
{ |
134 |
register char *cp; |
135 |
|
136 |
for (cp = s; *cp; cp++) |
137 |
if (*cp & 0x80) |
138 |
return(-1); /* non-ascii in header */ |
139 |
if (isaspect(s)) |
140 |
pp->pa *= aspectval(s); |
141 |
else |
142 |
formatval(pp->fmt, s); |
143 |
return(0); |
144 |
} |
145 |
|
146 |
|
147 |
PICTURE * |
148 |
openpicture(fname) /* open/check Radiance picture file */ |
149 |
char *fname; |
150 |
{ |
151 |
FILE *fp; |
152 |
register PICTURE *pp; |
153 |
register char *cp; |
154 |
/* check filename suffix */ |
155 |
if (fname == NULL) return(NULL); |
156 |
for (cp = fname; *cp; cp++) |
157 |
; |
158 |
while (cp > fname && cp[-1] != '.') |
159 |
if (*--cp == '/') { |
160 |
cp = fname; |
161 |
break; |
162 |
} |
163 |
if (cp > fname && !strncmp(cp, "tif", 3)) |
164 |
return(NULL); /* assume it's a TIFF */ |
165 |
/* else try opening it */ |
166 |
if ((fp = fopen(fname, "r")) == NULL) |
167 |
return(NULL); |
168 |
/* allocate struct */ |
169 |
if ((pp = (PICTURE *)malloc(sizeof(PICTURE))) == NULL) |
170 |
return(NULL); /* serious error -- should exit? */ |
171 |
pp->fp = fp; pp->fmt[0] = '\0'; pp->pa = 1.; |
172 |
/* load header */ |
173 |
if (getheader(fp, headline, (char *)pp) < 0) { |
174 |
closepicture(pp); |
175 |
return(NULL); |
176 |
} |
177 |
if (!pp->fmt[0]) /* assume RGBE if unspecified */ |
178 |
strcpy(pp->fmt, COLRFMT); |
179 |
if (!globmatch(PICFMT, pp->fmt) || !fgetsresolu(&pp->rs, fp)) { |
180 |
closepicture(pp); /* failed test -- close file */ |
181 |
return(NULL); |
182 |
} |
183 |
rewind(fp); /* passed test -- rewind file */ |
184 |
return(pp); |
185 |
} |
186 |
|
187 |
|
188 |
int |
189 |
tmap_picture(fname, pp) /* tone map Radiance picture */ |
190 |
char *fname; |
191 |
register PICTURE *pp; |
192 |
{ |
193 |
uint16 orient; |
194 |
int xsiz, ysiz; |
195 |
BYTE *pix; |
196 |
/* read and tone map picture */ |
197 |
if (tmMapPicture(&pix, &xsiz, &ysiz, flags, |
198 |
rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK) |
199 |
return(-1); |
200 |
/* figure out TIFF orientation */ |
201 |
for (orient = 8; --orient; ) |
202 |
if (ortab[orient] == pp->rs.rt) |
203 |
break; |
204 |
orient++; |
205 |
/* put out our image */ |
206 |
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
207 |
72., 72./pp->pa, 2, pix) != 0) |
208 |
return(-1); |
209 |
/* free data and we're done */ |
210 |
free((void *)pix); |
211 |
return(0); |
212 |
} |
213 |
|
214 |
|
215 |
tmap_tiff(fname, tp) /* tone map SGILOG TIFF */ |
216 |
char *fname; |
217 |
TIFF *tp; |
218 |
{ |
219 |
float xres, yres; |
220 |
uint16 orient, resunit, phot; |
221 |
int xsiz, ysiz; |
222 |
BYTE *pix; |
223 |
/* check to make sure it's SGILOG */ |
224 |
TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot); |
225 |
if (phot != PHOTOMETRIC_LOGLUV && phot != PHOTOMETRIC_LOGL) { |
226 |
if (!(flags & TM_F_NOSTDERR)) { |
227 |
fputs(fname, stderr); |
228 |
fputs(": TIFF must be in SGILOG format\n", stderr); |
229 |
} |
230 |
return(-1); |
231 |
} |
232 |
if (phot == PHOTOMETRIC_LOGL) |
233 |
flags |= TM_F_BW; |
234 |
/* read and tone map TIFF */ |
235 |
if (tmMapTIFF(&pix, &xsiz, &ysiz, flags, |
236 |
rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK) |
237 |
return(-1); |
238 |
/* get relevant tags */ |
239 |
TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit); |
240 |
TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres); |
241 |
TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres); |
242 |
TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient); |
243 |
/* put out our image */ |
244 |
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
245 |
xres, yres, resunit, pix) != 0) |
246 |
return(-1); |
247 |
/* free data and we're done */ |
248 |
free((void *)pix); |
249 |
return(0); |
250 |
} |
251 |
|
252 |
|
253 |
putimage(or, xs, ys, xr, yr, ru, pd) /* write out our image */ |
254 |
uint16 or; |
255 |
uint32 xs, ys; |
256 |
float xr, yr; |
257 |
uint16 ru; |
258 |
BYTE *pd; |
259 |
{ |
260 |
register int y; |
261 |
uint32 rowsperstrip; |
262 |
|
263 |
TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
264 |
if (flags & TM_F_BW) { |
265 |
TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, |
266 |
PHOTOMETRIC_MINISBLACK); |
267 |
TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 1); |
268 |
} else { |
269 |
TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
270 |
TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 3); |
271 |
} |
272 |
if (rgbp != stdprims) { |
273 |
TIFFSetField(tifout, TIFFTAG_PRIMARYCHROMATICITIES, |
274 |
(float *)rgbp); |
275 |
TIFFSetField(tifout, TIFFTAG_WHITEPOINT, (float *)rgbp[WHT]); |
276 |
} |
277 |
TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, 8); |
278 |
TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs); |
279 |
TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys); |
280 |
TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru); |
281 |
TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr); |
282 |
TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr); |
283 |
TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
284 |
TIFFSetField(tifout, TIFFTAG_ORIENTATION, or); |
285 |
/* compute good strip size */ |
286 |
rowsperstrip = 8192/TIFFScanlineSize(tifout); |
287 |
if (rowsperstrip < 1) rowsperstrip = 1; |
288 |
TIFFSetField(tifout, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
289 |
/* write out scanlines */ |
290 |
if (flags & TM_F_BW) { |
291 |
for (y = 0; y < ys; y++) |
292 |
if (TIFFWriteScanline(tifout, pd + y*xs, y, 0) < 0) |
293 |
goto writerr; |
294 |
} else { |
295 |
for (y = 0; y < ys; y++) |
296 |
if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0) |
297 |
goto writerr; |
298 |
} |
299 |
return(0); /* all done! */ |
300 |
writerr: |
301 |
fputs("Error writing TIFF output\n", stderr); |
302 |
return(-1); |
303 |
} |