| 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; |
| 54 |
> |
int i, rval; |
| 55 |
|
|
| 56 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
| 57 |
|
switch (argv[i][1]) { |
| 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 |
< |
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 |
< |
tmap_tiff(argv[i], tin); |
| 116 |
> |
rval = tmap_tiff(argv[i], tin); |
| 117 |
|
TIFFClose(tin); |
| 118 |
|
} |
| 119 |
|
TIFFClose(tifout); |
| 120 |
< |
exit(0); |
| 120 |
> |
exit(rval==0 ? 0 : 1); |
| 121 |
|
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", |
| 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 |
|
|
| 188 |
< |
getpixrat(s, pr) /* get pixel aspect ratio */ |
| 189 |
< |
char *s; |
| 161 |
< |
double *pr; |
| 162 |
< |
{ |
| 163 |
< |
if (isaspect(s)) |
| 164 |
< |
*pr *= aspectval(s); |
| 165 |
< |
} |
| 166 |
< |
|
| 167 |
< |
|
| 168 |
< |
tmap_picture(fname, fp) /* tone map Radiance picture */ |
| 188 |
> |
int |
| 189 |
> |
tmap_picture(fname, pp) /* tone map Radiance picture */ |
| 190 |
|
char *fname; |
| 191 |
< |
FILE *fp; |
| 191 |
> |
register PICTURE *pp; |
| 192 |
|
{ |
| 172 |
– |
double pixrat; |
| 173 |
– |
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) |
| 199 |
< |
exit(1); |
| 200 |
< |
/* get relevant header info. */ |
| 201 |
< |
rewind(fp); |
| 202 |
< |
pixrat = 1.; |
| 203 |
< |
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; |
| 198 |
> |
rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK) |
| 199 |
> |
return(-1); |
| 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 |
< |
putimage(orient, (uint32)xsiz, (uint32)ysiz, 72., 72./pixrat, 2, pix); |
| 206 |
> |
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
| 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 |
|
|
| 214 |
|
|
| 223 |
|
/* check to make sure it's SGILOG */ |
| 224 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot); |
| 225 |
|
if (phot != PHOTOMETRIC_LOGLUV && phot != PHOTOMETRIC_LOGL) { |
| 226 |
< |
fprintf(stderr, "%s: TIFF must be in SGILOG format\n", fname); |
| 227 |
< |
exit(1); |
| 226 |
> |
if (!(flags & TM_F_NOSTDERR)) { |
| 227 |
> |
fputs(fname, stderr); |
| 228 |
> |
fputs(": TIFF must be in SGILOG format\n", stderr); |
| 229 |
> |
} |
| 230 |
> |
return(-1); |
| 231 |
|
} |
| 232 |
|
if (phot == PHOTOMETRIC_LOGL) |
| 233 |
|
flags |= TM_F_BW; |
| 234 |
|
/* read and tone map TIFF */ |
| 235 |
|
if (tmMapTIFF(&pix, &xsiz, &ysiz, flags, |
| 236 |
|
rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK) |
| 237 |
< |
exit(1); |
| 237 |
> |
return(-1); |
| 238 |
|
/* get relevant tags */ |
| 239 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit); |
| 240 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres); |
| 241 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres); |
| 242 |
|
TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient); |
| 243 |
|
/* put out our image */ |
| 244 |
< |
putimage(orient, (uint32)xsiz, (uint32)ysiz, xres, yres, resunit, pix); |
| 244 |
> |
if (putimage(orient, (uint32)xsiz, (uint32)ysiz, |
| 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 |
|
|
| 252 |
|
|
| 296 |
|
if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0) |
| 297 |
|
goto writerr; |
| 298 |
|
} |
| 299 |
< |
return; /* all done! */ |
| 299 |
> |
return(0); /* all done! */ |
| 300 |
|
writerr: |
| 301 |
|
fputs("Error writing TIFF output\n", stderr); |
| 302 |
< |
exit(2); |
| 302 |
> |
return(-1); |
| 303 |
|
} |