1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Tone map SGILOG TIFF or Radiance picture and output 24-bit RGB TIFF |
6 |
|
*/ |
7 |
|
|
11 |
– |
#undef NOPROTO |
12 |
– |
#define NOPROTO 1 |
13 |
– |
|
8 |
|
#include <stdio.h> |
9 |
|
#include <math.h> |
10 |
+ |
#include <time.h> |
11 |
+ |
#include <string.h> |
12 |
+ |
|
13 |
+ |
#include "platform.h" |
14 |
|
#include "tiffio.h" |
15 |
|
#include "color.h" |
16 |
|
#include "tonemap.h" |
17 |
+ |
#include "tmaptiff.h" |
18 |
|
#include "resolu.h" |
19 |
|
|
20 |
|
|
37 |
|
0 |
38 |
|
}; |
39 |
|
|
40 |
< |
extern FILE *openpicture(); |
40 |
> |
typedef struct { |
41 |
> |
FILE *fp; /* file pointer */ |
42 |
> |
char fmt[MAXFMTLEN]; /* picture format */ |
43 |
> |
double pa; /* pixel aspect ratio */ |
44 |
> |
RESOLU rs; /* picture resolution */ |
45 |
> |
} PICTURE; |
46 |
|
|
47 |
+ |
uint16 comp = COMPRESSION_NONE; /* TIFF compression mode */ |
48 |
|
|
49 |
< |
main(argc, argv) |
50 |
< |
int argc; |
51 |
< |
char *argv[]; |
49 |
> |
#define closepicture(p) (fclose((p)->fp),free((void *)(p))) |
50 |
> |
|
51 |
> |
static gethfunc headline; |
52 |
> |
|
53 |
> |
static int headline(char *s, void *pp); |
54 |
> |
static PICTURE *openpicture(char *fname); |
55 |
> |
static int tmap_picture(char *fname, PICTURE *pp); |
56 |
> |
static int tmap_tiff(char *fname, TIFF *tp); |
57 |
> |
static int putimage(uint16 or, uint32 xs, uint32 ys, float xr, float yr, |
58 |
> |
uint16 ru, uby8 *pd); |
59 |
> |
|
60 |
> |
|
61 |
> |
int |
62 |
> |
main( |
63 |
> |
int argc, |
64 |
> |
char *argv[] |
65 |
> |
) |
66 |
|
{ |
67 |
< |
FILE *fin = NULL; |
67 |
> |
PICTURE *pin = NULL; |
68 |
|
TIFF *tin = NULL; |
69 |
< |
int i; |
69 |
> |
int i, rval; |
70 |
|
|
71 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
72 |
|
switch (argv[i][1]) { |
97 |
|
if (argc-i < 2) goto userr; |
98 |
|
lddyn = atof(argv[++i]); |
99 |
|
break; |
100 |
+ |
case 'z': /* LZW compression */ |
101 |
+ |
comp = COMPRESSION_LZW; |
102 |
+ |
break; |
103 |
|
case 'p': /* set display primaries */ |
104 |
|
if (argc-i < 9) goto userr; |
105 |
|
myprims[RED][CIEX] = atof(argv[++i]); |
116 |
|
goto userr; |
117 |
|
} |
118 |
|
if (argc-i < 2) goto userr; |
119 |
< |
if ((fin = openpicture(argv[i])) == NULL && |
119 |
> |
if ((pin = openpicture(argv[i])) == NULL && |
120 |
|
(tin = TIFFOpen(argv[i], "r")) == NULL) { |
121 |
|
fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n", |
122 |
|
argv[0], argv[i]); |
127 |
|
argv[0], argv[i+1]); |
128 |
|
exit(1); |
129 |
|
} |
130 |
< |
if (fin != NULL) { |
131 |
< |
tmap_picture(argv[i], fin); |
132 |
< |
fclose(fin); |
130 |
> |
if (pin != NULL) { |
131 |
> |
rval = tmap_picture(argv[i], pin); |
132 |
> |
closepicture(pin); |
133 |
|
} else { |
134 |
< |
tmap_tiff(argv[i], tin); |
134 |
> |
rval = tmap_tiff(argv[i], tin); |
135 |
|
TIFFClose(tin); |
136 |
|
} |
137 |
|
TIFFClose(tifout); |
138 |
< |
exit(0); |
138 |
> |
exit(rval==0 ? 0 : 1); |
139 |
|
userr: |
140 |
|
fprintf(stderr, |
141 |
< |
"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", |
141 |
> |
"Usage: %s [-h][-s][-c][-l][-b][-g gv][-d ld][-u lm][-z][-p xr yr xg yg xb yb xw yw] input.{tif|hdr} output.tif\n", |
142 |
|
argv[0]); |
143 |
|
exit(1); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
< |
FILE * |
148 |
< |
openpicture(fname) /* open/check Radiance picture file */ |
149 |
< |
char *fname; |
147 |
> |
static int |
148 |
> |
headline( /* process line from header */ |
149 |
> |
char *s, |
150 |
> |
void *pp |
151 |
> |
) |
152 |
|
{ |
153 |
+ |
register char *cp; |
154 |
+ |
|
155 |
+ |
for (cp = s; *cp; cp++) |
156 |
+ |
if (*cp & 0x80) |
157 |
+ |
return(-1); /* non-ascii in header */ |
158 |
+ |
if (isaspect(s)) |
159 |
+ |
((PICTURE *)pp)->pa *= aspectval(s); |
160 |
+ |
else |
161 |
+ |
formatval(((PICTURE *)pp)->fmt, s); |
162 |
+ |
return(0); |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
|
166 |
+ |
static PICTURE * |
167 |
+ |
openpicture( /* open/check Radiance picture file */ |
168 |
+ |
char *fname |
169 |
+ |
) |
170 |
+ |
{ |
171 |
|
FILE *fp; |
172 |
< |
char inpfmt[32]; |
131 |
< |
int xsiz, ysiz; |
172 |
> |
register PICTURE *pp; |
173 |
|
register char *cp; |
174 |
|
/* check filename suffix */ |
175 |
|
if (fname == NULL) return(NULL); |
185 |
|
/* else try opening it */ |
186 |
|
if ((fp = fopen(fname, "r")) == NULL) |
187 |
|
return(NULL); |
188 |
< |
/* check format */ |
189 |
< |
strcpy(inpfmt, PICFMT); |
190 |
< |
if (checkheader(fp, inpfmt, NULL) < 0 || |
191 |
< |
fgetresolu(&xsiz, &ysiz, fp) < 0) { |
192 |
< |
fclose(fp); /* failed test -- close file */ |
188 |
> |
SET_FILE_BINARY(fp); |
189 |
> |
/* allocate struct */ |
190 |
> |
if ((pp = (PICTURE *)malloc(sizeof(PICTURE))) == NULL) |
191 |
> |
return(NULL); /* serious error -- should exit? */ |
192 |
> |
pp->fp = fp; pp->fmt[0] = '\0'; pp->pa = 1.; |
193 |
> |
/* load header */ |
194 |
> |
if (getheader(fp, headline, pp) < 0) { |
195 |
> |
closepicture(pp); |
196 |
|
return(NULL); |
197 |
|
} |
198 |
+ |
if (!pp->fmt[0]) /* assume RGBE if unspecified */ |
199 |
+ |
strcpy(pp->fmt, COLRFMT); |
200 |
+ |
if (!globmatch(PICFMT, pp->fmt) || !fgetsresolu(&pp->rs, fp)) { |
201 |
+ |
closepicture(pp); /* failed test -- close file */ |
202 |
+ |
return(NULL); |
203 |
+ |
} |
204 |
|
rewind(fp); /* passed test -- rewind file */ |
205 |
< |
return(fp); |
205 |
> |
return(pp); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
< |
getpixrat(s, pr) /* get pixel aspect ratio */ |
210 |
< |
char *s; |
211 |
< |
double *pr; |
209 |
> |
static int |
210 |
> |
tmap_picture( /* tone map Radiance picture */ |
211 |
> |
char *fname, |
212 |
> |
register PICTURE *pp |
213 |
> |
) |
214 |
|
{ |
163 |
– |
if (isaspect(s)) |
164 |
– |
*pr *= aspectval(s); |
165 |
– |
} |
166 |
– |
|
167 |
– |
|
168 |
– |
tmap_picture(fname, fp) /* tone map Radiance picture */ |
169 |
– |
char *fname; |
170 |
– |
FILE *fp; |
171 |
– |
{ |
172 |
– |
double pixrat; |
173 |
– |
int ord; |
215 |
|
uint16 orient; |
216 |
+ |
double paspect = (pp->rs.rt & YMAJOR) ? pp->pa : 1./pp->pa; |
217 |
|
int xsiz, ysiz; |
218 |
< |
BYTE *pix; |
218 |
> |
uby8 *pix; |
219 |
|
/* read and tone map picture */ |
220 |
|
if (tmMapPicture(&pix, &xsiz, &ysiz, flags, |
221 |
< |
rgbp, gamv, lddyn, ldmax, fname, fp) != TM_E_OK) |
222 |
< |
exit(1); |
223 |
< |
/* get relevant header info. */ |
224 |
< |
rewind(fp); |
225 |
< |
pixrat = 1.; |
226 |
< |
getheader(fp, getpixrat, &pixrat); |
185 |
< |
if ((ord = fgetresolu(&xsiz, &ysiz, fp)) < 0) |
186 |
< |
orient = 0; |
187 |
< |
else |
188 |
< |
for (orient = 8; --orient; ) |
189 |
< |
if (ortab[orient] == ord) |
190 |
< |
break; |
221 |
> |
rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK) |
222 |
> |
return(-1); |
223 |
> |
/* figure out TIFF orientation */ |
224 |
> |
for (orient = 8; --orient; ) |
225 |
> |
if (ortab[orient] == pp->rs.rt) |
226 |
> |
break; |
227 |
|
orient++; |
228 |
|
/* put out our image */ |
229 |
< |
putimage(orient, (uint32)xsiz, (uint32)ysiz, 72., 72./pixrat, 2, pix); |
229 |
> |
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
230 |
> |
72., 72./paspect, 2, pix) != 0) |
231 |
> |
return(-1); |
232 |
|
/* free data and we're done */ |
233 |
< |
free((char *)pix); |
233 |
> |
free((void *)pix); |
234 |
> |
return(0); |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
< |
tmap_tiff(fname, tp) /* tone map SGILOG TIFF */ |
239 |
< |
char *fname; |
240 |
< |
TIFF *tp; |
238 |
> |
static int |
239 |
> |
tmap_tiff( /* tone map SGILOG TIFF */ |
240 |
> |
char *fname, |
241 |
> |
TIFF *tp |
242 |
> |
) |
243 |
|
{ |
244 |
|
float xres, yres; |
245 |
|
uint16 orient, resunit, phot; |
246 |
|
int xsiz, ysiz; |
247 |
< |
BYTE *pix; |
247 |
> |
uby8 *pix; |
248 |
|
/* check to make sure it's SGILOG */ |
249 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot); |
250 |
< |
if (phot != PHOTOMETRIC_LOGLUV && phot != PHOTOMETRIC_LOGL) { |
210 |
< |
fprintf(stderr, "%s: TIFF must be in SGILOG format\n", fname); |
211 |
< |
exit(1); |
212 |
< |
} |
213 |
< |
if (phot == PHOTOMETRIC_LOGL) |
250 |
> |
if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK)) |
251 |
|
flags |= TM_F_BW; |
252 |
|
/* read and tone map TIFF */ |
253 |
|
if (tmMapTIFF(&pix, &xsiz, &ysiz, flags, |
254 |
|
rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK) |
255 |
< |
exit(1); |
255 |
> |
return(-1); |
256 |
|
/* get relevant tags */ |
257 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit); |
258 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres); |
259 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres); |
260 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient); |
261 |
|
/* put out our image */ |
262 |
< |
putimage(orient, (uint32)xsiz, (uint32)ysiz, xres, yres, resunit, pix); |
262 |
> |
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
263 |
> |
xres, yres, resunit, pix) != 0) |
264 |
> |
return(-1); |
265 |
|
/* free data and we're done */ |
266 |
< |
free((char *)pix); |
266 |
> |
free((void *)pix); |
267 |
> |
return(0); |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
< |
putimage(or, xs, ys, xr, yr, ru, pd) /* write out our image */ |
272 |
< |
uint16 or; |
273 |
< |
uint32 xs, ys; |
274 |
< |
float xr, yr; |
275 |
< |
uint16 ru; |
276 |
< |
BYTE *pd; |
271 |
> |
static int |
272 |
> |
putimage( /* write out our image */ |
273 |
> |
uint16 or, |
274 |
> |
uint32 xs, |
275 |
> |
uint32 ys, |
276 |
> |
float xr, |
277 |
> |
float yr, |
278 |
> |
uint16 ru, |
279 |
> |
uby8 *pd |
280 |
> |
) |
281 |
|
{ |
282 |
|
register int y; |
283 |
|
uint32 rowsperstrip; |
300 |
|
TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs); |
301 |
|
TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys); |
302 |
|
TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru); |
303 |
+ |
TIFFSetField(tifout, TIFFTAG_COMPRESSION, comp); |
304 |
|
TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr); |
305 |
|
TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr); |
306 |
|
TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
319 |
|
if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0) |
320 |
|
goto writerr; |
321 |
|
} |
322 |
< |
return; /* all done! */ |
322 |
> |
return(0); /* all done! */ |
323 |
|
writerr: |
324 |
|
fputs("Error writing TIFF output\n", stderr); |
325 |
< |
exit(2); |
325 |
> |
return(-1); |
326 |
|
} |