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 "tiffio.h" |
14 |
|
#include "color.h" |
15 |
|
#include "tonemap.h" |
35 |
|
0 |
36 |
|
}; |
37 |
|
|
38 |
< |
extern FILE *openpicture(); |
38 |
> |
typedef struct { |
39 |
> |
FILE *fp; /* file pointer */ |
40 |
> |
char fmt[32]; /* picture format */ |
41 |
> |
double pa; /* pixel aspect ratio */ |
42 |
> |
RESOLU rs; /* picture resolution */ |
43 |
> |
} PICTURE; |
44 |
|
|
45 |
+ |
extern PICTURE *openpicture(); |
46 |
|
|
47 |
< |
main(argc, argv) |
48 |
< |
int argc; |
49 |
< |
char *argv[]; |
47 |
> |
#define closepicture(p) (fclose((p)->fp),free((void *)(p))) |
48 |
> |
|
49 |
> |
static gethfunc headline; |
50 |
> |
|
51 |
> |
|
52 |
> |
int |
53 |
> |
main( |
54 |
> |
int argc, |
55 |
> |
char *argv[] |
56 |
> |
) |
57 |
|
{ |
58 |
< |
FILE *fin = NULL; |
58 |
> |
PICTURE *pin = NULL; |
59 |
|
TIFF *tin = NULL; |
60 |
|
int i, rval; |
61 |
|
|
104 |
|
goto userr; |
105 |
|
} |
106 |
|
if (argc-i < 2) goto userr; |
107 |
< |
if ((fin = openpicture(argv[i])) == NULL && |
107 |
> |
if ((pin = openpicture(argv[i])) == NULL && |
108 |
|
(tin = TIFFOpen(argv[i], "r")) == NULL) { |
109 |
|
fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n", |
110 |
|
argv[0], argv[i]); |
115 |
|
argv[0], argv[i+1]); |
116 |
|
exit(1); |
117 |
|
} |
118 |
< |
if (fin != NULL) { |
119 |
< |
rval = tmap_picture(argv[i], fin); |
120 |
< |
fclose(fin); |
118 |
> |
if (pin != NULL) { |
119 |
> |
rval = tmap_picture(argv[i], pin); |
120 |
> |
closepicture(pin); |
121 |
|
} else { |
122 |
|
rval = tmap_tiff(argv[i], tin); |
123 |
|
TIFFClose(tin); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
< |
FILE * |
135 |
> |
static int |
136 |
> |
headline( /* process line from header */ |
137 |
> |
char *s, |
138 |
> |
void *pp |
139 |
> |
) |
140 |
> |
{ |
141 |
> |
register char *cp; |
142 |
> |
|
143 |
> |
for (cp = s; *cp; cp++) |
144 |
> |
if (*cp & 0x80) |
145 |
> |
return(-1); /* non-ascii in header */ |
146 |
> |
if (isaspect(s)) |
147 |
> |
((PICTURE *)pp)->pa *= aspectval(s); |
148 |
> |
else |
149 |
> |
formatval(((PICTURE *)pp)->fmt, s); |
150 |
> |
return(0); |
151 |
> |
} |
152 |
> |
|
153 |
> |
|
154 |
> |
PICTURE * |
155 |
|
openpicture(fname) /* open/check Radiance picture file */ |
156 |
|
char *fname; |
157 |
|
{ |
158 |
|
FILE *fp; |
159 |
< |
char inpfmt[32]; |
131 |
< |
int xsiz, ysiz; |
159 |
> |
register PICTURE *pp; |
160 |
|
register char *cp; |
161 |
|
/* check filename suffix */ |
162 |
|
if (fname == NULL) return(NULL); |
172 |
|
/* else try opening it */ |
173 |
|
if ((fp = fopen(fname, "r")) == NULL) |
174 |
|
return(NULL); |
175 |
< |
/* check format */ |
176 |
< |
strcpy(inpfmt, PICFMT); |
177 |
< |
if (checkheader(fp, inpfmt, NULL) < 0 || |
178 |
< |
fgetresolu(&xsiz, &ysiz, fp) < 0) { |
179 |
< |
fclose(fp); /* failed test -- close file */ |
175 |
> |
/* allocate struct */ |
176 |
> |
if ((pp = (PICTURE *)malloc(sizeof(PICTURE))) == NULL) |
177 |
> |
return(NULL); /* serious error -- should exit? */ |
178 |
> |
pp->fp = fp; pp->fmt[0] = '\0'; pp->pa = 1.; |
179 |
> |
/* load header */ |
180 |
> |
if (getheader(fp, headline, pp) < 0) { |
181 |
> |
closepicture(pp); |
182 |
|
return(NULL); |
183 |
|
} |
184 |
+ |
if (!pp->fmt[0]) /* assume RGBE if unspecified */ |
185 |
+ |
strcpy(pp->fmt, COLRFMT); |
186 |
+ |
if (!globmatch(PICFMT, pp->fmt) || !fgetsresolu(&pp->rs, fp)) { |
187 |
+ |
closepicture(pp); /* failed test -- close file */ |
188 |
+ |
return(NULL); |
189 |
+ |
} |
190 |
|
rewind(fp); /* passed test -- rewind file */ |
191 |
< |
return(fp); |
191 |
> |
return(pp); |
192 |
|
} |
193 |
|
|
194 |
|
|
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 |
– |
|
195 |
|
int |
196 |
< |
tmap_picture(fname, fp) /* tone map Radiance picture */ |
196 |
> |
tmap_picture(fname, pp) /* tone map Radiance picture */ |
197 |
|
char *fname; |
198 |
< |
FILE *fp; |
198 |
> |
register PICTURE *pp; |
199 |
|
{ |
173 |
– |
double pixrat; |
174 |
– |
int ord; |
200 |
|
uint16 orient; |
201 |
|
int xsiz, ysiz; |
202 |
|
BYTE *pix; |
203 |
|
/* read and tone map picture */ |
204 |
|
if (tmMapPicture(&pix, &xsiz, &ysiz, flags, |
205 |
< |
rgbp, gamv, lddyn, ldmax, fname, fp) != TM_E_OK) |
205 |
> |
rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK) |
206 |
|
return(-1); |
207 |
< |
/* get relevant header info. */ |
208 |
< |
rewind(fp); |
209 |
< |
pixrat = 1.; |
210 |
< |
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; |
207 |
> |
/* figure out TIFF orientation */ |
208 |
> |
for (orient = 8; --orient; ) |
209 |
> |
if (ortab[orient] == pp->rs.rt) |
210 |
> |
break; |
211 |
|
orient++; |
212 |
|
/* put out our image */ |
213 |
|
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
214 |
< |
72., 72./pixrat, 2, pix) != 0) |
214 |
> |
72., 72./pp->pa, 2, pix) != 0) |
215 |
|
return(-1); |
216 |
|
/* free data and we're done */ |
217 |
< |
free((char *)pix); |
217 |
> |
free((void *)pix); |
218 |
|
return(0); |
219 |
|
} |
220 |
|
|
229 |
|
BYTE *pix; |
230 |
|
/* check to make sure it's SGILOG */ |
231 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot); |
232 |
< |
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) |
232 |
> |
if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK)) |
233 |
|
flags |= TM_F_BW; |
234 |
|
/* read and tone map TIFF */ |
235 |
|
if (tmMapTIFF(&pix, &xsiz, &ysiz, flags, |
245 |
|
xres, yres, resunit, pix) != 0) |
246 |
|
return(-1); |
247 |
|
/* free data and we're done */ |
248 |
< |
free((char *)pix); |
248 |
> |
free((void *)pix); |
249 |
|
return(0); |
250 |
|
} |
251 |
|
|