5 |
|
* Tone map SGILOG TIFF or Radiance picture and output 24-bit RGB TIFF |
6 |
|
*/ |
7 |
|
|
8 |
– |
#include <stdio.h> |
8 |
|
#include <math.h> |
10 |
– |
#include <time.h> |
11 |
– |
#include <string.h> |
9 |
|
|
10 |
+ |
#include "rtio.h" |
11 |
+ |
#include "platform.h" |
12 |
|
#include "tiffio.h" |
13 |
|
#include "color.h" |
14 |
|
#include "tonemap.h" |
21 |
|
RGBPRIMP rgbp = stdprims; /* display primaries */ |
22 |
|
RGBPRIMS myprims; /* overriding display primaries */ |
23 |
|
double ldmax = 100.; /* maximum display luminance */ |
24 |
< |
double lddyn = 32.; /* display dynamic range */ |
24 |
> |
double lddyn = 100.; /* display dynamic range */ |
25 |
|
double gamv = 2.2; /* display gamma value */ |
26 |
|
|
27 |
|
short ortab[8] = { /* orientation conversion table */ |
37 |
|
|
38 |
|
typedef struct { |
39 |
|
FILE *fp; /* file pointer */ |
40 |
< |
char fmt[32]; /* picture format */ |
40 |
> |
char fmt[MAXFMTLEN]; /* picture format */ |
41 |
|
double pa; /* pixel aspect ratio */ |
42 |
|
RESOLU rs; /* picture resolution */ |
43 |
|
} PICTURE; |
44 |
|
|
45 |
< |
//extern PICTURE *openpicture(); |
45 |
> |
uint16 comp = COMPRESSION_NONE; /* TIFF compression mode */ |
46 |
|
|
47 |
< |
#define closepicture(p) (fclose((p)->fp),free((void *)(p))) |
47 |
> |
#define closepicture(p) (fclose((p)->fp),free(p)) |
48 |
|
|
49 |
|
static gethfunc headline; |
50 |
|
|
51 |
< |
static int headline(char *s, void *pp); |
52 |
< |
static PICTURE * openpicture(char *fname); |
53 |
< |
static int tmap_picture(char *fname, PICTURE *pp); |
54 |
< |
static int tmap_tiff(char *fname, TIFF *tp); |
55 |
< |
static int putimage(uint16 or, uint32 xs, uint32 ys, float xr, float yr, |
56 |
< |
uint16 ru, BYTE *pd); |
51 |
> |
static int headline(char *s, void *pp); |
52 |
> |
static PICTURE *openpicture(char *fname); |
53 |
> |
static int tmap_picture(char *fname, PICTURE *pp); |
54 |
> |
static int tmap_tiff(char *fname, TIFF *tp); |
55 |
> |
static int putimage(uint16 or, uint32 xs, uint32 ys, float xr, float yr, |
56 |
> |
uint16 ru, uby8 *pd); |
57 |
|
|
58 |
|
|
59 |
|
int |
95 |
|
if (argc-i < 2) goto userr; |
96 |
|
lddyn = atof(argv[++i]); |
97 |
|
break; |
98 |
+ |
case 'z': /* LZW compression */ |
99 |
+ |
comp = COMPRESSION_LZW; |
100 |
+ |
break; |
101 |
|
case 'p': /* set display primaries */ |
102 |
|
if (argc-i < 9) goto userr; |
103 |
|
myprims[RED][CIEX] = atof(argv[++i]); |
136 |
|
exit(rval==0 ? 0 : 1); |
137 |
|
userr: |
138 |
|
fprintf(stderr, |
139 |
< |
"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", |
139 |
> |
"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", |
140 |
|
argv[0]); |
141 |
|
exit(1); |
142 |
|
} |
148 |
|
void *pp |
149 |
|
) |
150 |
|
{ |
151 |
< |
register char *cp; |
151 |
> |
char *cp; |
152 |
|
|
153 |
|
for (cp = s; *cp; cp++) |
154 |
|
if (*cp & 0x80) |
167 |
|
) |
168 |
|
{ |
169 |
|
FILE *fp; |
170 |
< |
register PICTURE *pp; |
171 |
< |
register char *cp; |
170 |
> |
PICTURE *pp; |
171 |
> |
char *cp; |
172 |
|
/* check filename suffix */ |
173 |
|
if (fname == NULL) return(NULL); |
174 |
|
for (cp = fname; *cp; cp++) |
178 |
|
cp = fname; |
179 |
|
break; |
180 |
|
} |
181 |
< |
if (cp > fname && !strncmp(cp, "tif", 3)) |
181 |
> |
if (cp > fname && !strncasecmp(cp, "tif", 3)) |
182 |
|
return(NULL); /* assume it's a TIFF */ |
183 |
|
/* else try opening it */ |
184 |
|
if ((fp = fopen(fname, "r")) == NULL) |
185 |
|
return(NULL); |
186 |
+ |
SET_FILE_BINARY(fp); |
187 |
|
/* allocate struct */ |
188 |
|
if ((pp = (PICTURE *)malloc(sizeof(PICTURE))) == NULL) |
189 |
|
return(NULL); /* serious error -- should exit? */ |
195 |
|
} |
196 |
|
if (!pp->fmt[0]) /* assume RGBE if unspecified */ |
197 |
|
strcpy(pp->fmt, COLRFMT); |
198 |
< |
if (!globmatch(PICFMT, pp->fmt) || !fgetsresolu(&pp->rs, fp)) { |
198 |
> |
if ((!globmatch(PICFMT, pp->fmt) && strcmp(SPECFMT, pp->fmt)) |
199 |
> |
|| !fgetsresolu(&pp->rs, fp)) { |
200 |
|
closepicture(pp); /* failed test -- close file */ |
201 |
|
return(NULL); |
202 |
|
} |
208 |
|
static int |
209 |
|
tmap_picture( /* tone map Radiance picture */ |
210 |
|
char *fname, |
211 |
< |
register PICTURE *pp |
211 |
> |
PICTURE *pp |
212 |
|
) |
213 |
|
{ |
214 |
|
uint16 orient; |
215 |
+ |
double paspect = (pp->rs.rt & YMAJOR) ? pp->pa : 1./pp->pa; |
216 |
|
int xsiz, ysiz; |
217 |
< |
BYTE *pix; |
217 |
> |
uby8 *pix; |
218 |
|
/* read and tone map picture */ |
219 |
|
if (tmMapPicture(&pix, &xsiz, &ysiz, flags, |
220 |
|
rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK) |
226 |
|
orient++; |
227 |
|
/* put out our image */ |
228 |
|
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
229 |
< |
72., 72./pp->pa, 2, pix) != 0) |
229 |
> |
72., 72./paspect, 2, pix) != 0) |
230 |
|
return(-1); |
231 |
|
/* free data and we're done */ |
232 |
< |
free((void *)pix); |
232 |
> |
free(pix); |
233 |
|
return(0); |
234 |
|
} |
235 |
|
|
243 |
|
float xres, yres; |
244 |
|
uint16 orient, resunit, phot; |
245 |
|
int xsiz, ysiz; |
246 |
< |
BYTE *pix; |
246 |
> |
uby8 *pix; |
247 |
|
/* check to make sure it's SGILOG */ |
248 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot); |
249 |
|
if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK)) |
262 |
|
xres, yres, resunit, pix) != 0) |
263 |
|
return(-1); |
264 |
|
/* free data and we're done */ |
265 |
< |
free((void *)pix); |
265 |
> |
free(pix); |
266 |
|
return(0); |
267 |
|
} |
268 |
|
|
274 |
|
uint32 ys, |
275 |
|
float xr, |
276 |
|
float yr, |
277 |
< |
uint16 ru, |
278 |
< |
BYTE *pd |
277 |
> |
uint16 ru, |
278 |
> |
uby8 *pd |
279 |
|
) |
280 |
|
{ |
281 |
< |
register int y; |
281 |
> |
int y; |
282 |
|
uint32 rowsperstrip; |
283 |
|
|
284 |
|
TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
299 |
|
TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs); |
300 |
|
TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys); |
301 |
|
TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru); |
302 |
+ |
TIFFSetField(tifout, TIFFTAG_COMPRESSION, comp); |
303 |
|
TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr); |
304 |
|
TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr); |
305 |
|
TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |