1 |
gwlarson |
3.1 |
#ifndef lint |
2 |
schorsch |
3.6 |
static const char RCSid[] = "$Id: normtiff.c,v 3.5 2003/07/14 04:56:54 greg Exp $"; |
3 |
gwlarson |
3.1 |
#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 |
greg |
3.4 |
#include <time.h> |
11 |
gwlarson |
3.1 |
#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 |
gwlarson |
3.3 |
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 |
gwlarson |
3.1 |
|
43 |
gwlarson |
3.3 |
extern PICTURE *openpicture(); |
44 |
gwlarson |
3.1 |
|
45 |
greg |
3.4 |
#define closepicture(p) (fclose((p)->fp),free((void *)(p))) |
46 |
gwlarson |
3.3 |
|
47 |
|
|
|
48 |
gwlarson |
3.1 |
main(argc, argv) |
49 |
|
|
int argc; |
50 |
|
|
char *argv[]; |
51 |
|
|
{ |
52 |
gwlarson |
3.3 |
PICTURE *pin = NULL; |
53 |
gwlarson |
3.1 |
TIFF *tin = NULL; |
54 |
gwlarson |
3.2 |
int i, rval; |
55 |
gwlarson |
3.1 |
|
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 |
gwlarson |
3.3 |
if ((pin = openpicture(argv[i])) == NULL && |
102 |
gwlarson |
3.1 |
(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 |
gwlarson |
3.3 |
if (pin != NULL) { |
113 |
|
|
rval = tmap_picture(argv[i], pin); |
114 |
|
|
closepicture(pin); |
115 |
gwlarson |
3.1 |
} else { |
116 |
gwlarson |
3.2 |
rval = tmap_tiff(argv[i], tin); |
117 |
gwlarson |
3.1 |
TIFFClose(tin); |
118 |
|
|
} |
119 |
|
|
TIFFClose(tifout); |
120 |
gwlarson |
3.2 |
exit(rval==0 ? 0 : 1); |
121 |
gwlarson |
3.1 |
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 |
gwlarson |
3.3 |
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 |
gwlarson |
3.1 |
openpicture(fname) /* open/check Radiance picture file */ |
149 |
|
|
char *fname; |
150 |
|
|
{ |
151 |
|
|
FILE *fp; |
152 |
gwlarson |
3.3 |
register PICTURE *pp; |
153 |
gwlarson |
3.1 |
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 |
gwlarson |
3.3 |
/* 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 |
greg |
3.4 |
if (getheader(fp, headline, (char *)pp) < 0) { |
174 |
gwlarson |
3.3 |
closepicture(pp); |
175 |
gwlarson |
3.1 |
return(NULL); |
176 |
|
|
} |
177 |
gwlarson |
3.3 |
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 |
gwlarson |
3.1 |
rewind(fp); /* passed test -- rewind file */ |
184 |
gwlarson |
3.3 |
return(pp); |
185 |
gwlarson |
3.1 |
} |
186 |
|
|
|
187 |
|
|
|
188 |
gwlarson |
3.2 |
int |
189 |
gwlarson |
3.3 |
tmap_picture(fname, pp) /* tone map Radiance picture */ |
190 |
gwlarson |
3.1 |
char *fname; |
191 |
gwlarson |
3.3 |
register PICTURE *pp; |
192 |
gwlarson |
3.1 |
{ |
193 |
|
|
uint16 orient; |
194 |
|
|
int xsiz, ysiz; |
195 |
|
|
BYTE *pix; |
196 |
|
|
/* read and tone map picture */ |
197 |
|
|
if (tmMapPicture(&pix, &xsiz, &ysiz, flags, |
198 |
gwlarson |
3.3 |
rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK) |
199 |
gwlarson |
3.2 |
return(-1); |
200 |
gwlarson |
3.3 |
/* figure out TIFF orientation */ |
201 |
|
|
for (orient = 8; --orient; ) |
202 |
greg |
3.4 |
if (ortab[orient] == pp->rs.rt) |
203 |
gwlarson |
3.3 |
break; |
204 |
gwlarson |
3.1 |
orient++; |
205 |
|
|
/* put out our image */ |
206 |
gwlarson |
3.2 |
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
207 |
gwlarson |
3.3 |
72., 72./pp->pa, 2, pix) != 0) |
208 |
gwlarson |
3.2 |
return(-1); |
209 |
gwlarson |
3.1 |
/* free data and we're done */ |
210 |
greg |
3.4 |
free((void *)pix); |
211 |
gwlarson |
3.2 |
return(0); |
212 |
gwlarson |
3.1 |
} |
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 |
schorsch |
3.6 |
if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK)) |
226 |
gwlarson |
3.1 |
flags |= TM_F_BW; |
227 |
|
|
/* read and tone map TIFF */ |
228 |
|
|
if (tmMapTIFF(&pix, &xsiz, &ysiz, flags, |
229 |
|
|
rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK) |
230 |
gwlarson |
3.2 |
return(-1); |
231 |
gwlarson |
3.1 |
/* get relevant tags */ |
232 |
|
|
TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit); |
233 |
|
|
TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres); |
234 |
|
|
TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres); |
235 |
|
|
TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient); |
236 |
|
|
/* put out our image */ |
237 |
gwlarson |
3.2 |
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
238 |
|
|
xres, yres, resunit, pix) != 0) |
239 |
|
|
return(-1); |
240 |
gwlarson |
3.1 |
/* free data and we're done */ |
241 |
greg |
3.4 |
free((void *)pix); |
242 |
gwlarson |
3.2 |
return(0); |
243 |
gwlarson |
3.1 |
} |
244 |
|
|
|
245 |
|
|
|
246 |
|
|
putimage(or, xs, ys, xr, yr, ru, pd) /* write out our image */ |
247 |
|
|
uint16 or; |
248 |
|
|
uint32 xs, ys; |
249 |
|
|
float xr, yr; |
250 |
|
|
uint16 ru; |
251 |
|
|
BYTE *pd; |
252 |
|
|
{ |
253 |
|
|
register int y; |
254 |
|
|
uint32 rowsperstrip; |
255 |
|
|
|
256 |
|
|
TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
257 |
|
|
if (flags & TM_F_BW) { |
258 |
|
|
TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, |
259 |
|
|
PHOTOMETRIC_MINISBLACK); |
260 |
|
|
TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 1); |
261 |
|
|
} else { |
262 |
|
|
TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
263 |
|
|
TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 3); |
264 |
|
|
} |
265 |
|
|
if (rgbp != stdprims) { |
266 |
|
|
TIFFSetField(tifout, TIFFTAG_PRIMARYCHROMATICITIES, |
267 |
|
|
(float *)rgbp); |
268 |
|
|
TIFFSetField(tifout, TIFFTAG_WHITEPOINT, (float *)rgbp[WHT]); |
269 |
|
|
} |
270 |
|
|
TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, 8); |
271 |
|
|
TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs); |
272 |
|
|
TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys); |
273 |
|
|
TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru); |
274 |
|
|
TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr); |
275 |
|
|
TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr); |
276 |
|
|
TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
277 |
|
|
TIFFSetField(tifout, TIFFTAG_ORIENTATION, or); |
278 |
|
|
/* compute good strip size */ |
279 |
|
|
rowsperstrip = 8192/TIFFScanlineSize(tifout); |
280 |
|
|
if (rowsperstrip < 1) rowsperstrip = 1; |
281 |
|
|
TIFFSetField(tifout, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
282 |
|
|
/* write out scanlines */ |
283 |
|
|
if (flags & TM_F_BW) { |
284 |
|
|
for (y = 0; y < ys; y++) |
285 |
|
|
if (TIFFWriteScanline(tifout, pd + y*xs, y, 0) < 0) |
286 |
|
|
goto writerr; |
287 |
|
|
} else { |
288 |
|
|
for (y = 0; y < ys; y++) |
289 |
|
|
if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0) |
290 |
|
|
goto writerr; |
291 |
|
|
} |
292 |
gwlarson |
3.2 |
return(0); /* all done! */ |
293 |
gwlarson |
3.1 |
writerr: |
294 |
|
|
fputs("Error writing TIFF output\n", stderr); |
295 |
gwlarson |
3.2 |
return(-1); |
296 |
gwlarson |
3.1 |
} |