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 "tiffio.h" |
12 |
|
#include "color.h" |
13 |
|
#include "tonemap.h" |
33 |
|
0 |
34 |
|
}; |
35 |
|
|
36 |
< |
extern FILE *openpicture(); |
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 |
< |
FILE *fin = NULL; |
52 |
> |
PICTURE *pin = NULL; |
53 |
|
TIFF *tin = NULL; |
54 |
|
int i, rval; |
55 |
|
|
98 |
|
goto userr; |
99 |
|
} |
100 |
|
if (argc-i < 2) goto userr; |
101 |
< |
if ((fin = openpicture(argv[i])) == NULL && |
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]); |
109 |
|
argv[0], argv[i+1]); |
110 |
|
exit(1); |
111 |
|
} |
112 |
< |
if (fin != NULL) { |
113 |
< |
rval = tmap_picture(argv[i], fin); |
114 |
< |
fclose(fin); |
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); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
< |
FILE * |
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 |
< |
char inpfmt[32]; |
131 |
< |
int xsiz, ysiz; |
152 |
> |
register PICTURE *pp; |
153 |
|
register char *cp; |
154 |
|
/* check filename suffix */ |
155 |
|
if (fname == NULL) return(NULL); |
165 |
|
/* else try opening it */ |
166 |
|
if ((fp = fopen(fname, "r")) == NULL) |
167 |
|
return(NULL); |
168 |
< |
/* check format */ |
169 |
< |
strcpy(inpfmt, PICFMT); |
170 |
< |
if (checkheader(fp, inpfmt, NULL) < 0 || |
171 |
< |
fgetresolu(&xsiz, &ysiz, fp) < 0) { |
172 |
< |
fclose(fp); /* failed test -- close file */ |
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(fp); |
184 |
> |
return(pp); |
185 |
|
} |
186 |
|
|
187 |
|
|
159 |
– |
getpixrat(s, pr) /* get pixel aspect ratio */ |
160 |
– |
char *s; |
161 |
– |
double *pr; |
162 |
– |
{ |
163 |
– |
if (isaspect(s)) |
164 |
– |
*pr *= aspectval(s); |
165 |
– |
} |
166 |
– |
|
167 |
– |
|
188 |
|
int |
189 |
< |
tmap_picture(fname, fp) /* tone map Radiance picture */ |
189 |
> |
tmap_picture(fname, pp) /* tone map Radiance picture */ |
190 |
|
char *fname; |
191 |
< |
FILE *fp; |
191 |
> |
register PICTURE *pp; |
192 |
|
{ |
173 |
– |
double pixrat; |
174 |
– |
int ord; |
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, fp) != TM_E_OK) |
198 |
> |
rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK) |
199 |
|
return(-1); |
200 |
< |
/* get relevant header info. */ |
201 |
< |
rewind(fp); |
202 |
< |
pixrat = 1.; |
203 |
< |
getheader(fp, getpixrat, &pixrat); |
186 |
< |
if ((ord = fgetresolu(&xsiz, &ysiz, fp)) < 0) |
187 |
< |
orient = 0; |
188 |
< |
else |
189 |
< |
for (orient = 8; --orient; ) |
190 |
< |
if (ortab[orient] == ord) |
191 |
< |
break; |
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./pixrat, 2, pix) != 0) |
207 |
> |
72., 72./pp->pa, 2, pix) != 0) |
208 |
|
return(-1); |
209 |
|
/* free data and we're done */ |
210 |
< |
free((char *)pix); |
210 |
> |
free((void *)pix); |
211 |
|
return(0); |
212 |
|
} |
213 |
|
|
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) { |
214 |
< |
if (!(flags & TM_F_NOSTDERR)) { |
215 |
< |
fputs(fname, stderr); |
216 |
< |
fputs(": TIFF must be in SGILOG format\n", stderr); |
217 |
< |
} |
218 |
< |
return(-1); |
219 |
< |
} |
220 |
< |
if (phot == PHOTOMETRIC_LOGL) |
225 |
> |
if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK)) |
226 |
|
flags |= TM_F_BW; |
227 |
|
/* read and tone map TIFF */ |
228 |
|
if (tmMapTIFF(&pix, &xsiz, &ysiz, flags, |
238 |
|
xres, yres, resunit, pix) != 0) |
239 |
|
return(-1); |
240 |
|
/* free data and we're done */ |
241 |
< |
free((char *)pix); |
241 |
> |
free((void *)pix); |
242 |
|
return(0); |
243 |
|
} |
244 |
|
|