--- ray/src/px/normtiff.c 1998/10/26 17:30:50 3.2 +++ ray/src/px/normtiff.c 2011/05/20 02:06:39 3.13 @@ -1,21 +1,20 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: normtiff.c,v 3.13 2011/05/20 02:06:39 greg Exp $"; #endif - /* * Tone map SGILOG TIFF or Radiance picture and output 24-bit RGB TIFF */ -#undef NOPROTO -#define NOPROTO 1 - #include #include +#include +#include + +#include "platform.h" #include "tiffio.h" #include "color.h" #include "tonemap.h" +#include "tmaptiff.h" #include "resolu.h" @@ -38,14 +37,34 @@ short ortab[8] = { /* orientation conversion table */ 0 }; -extern FILE *openpicture(); +typedef struct { + FILE *fp; /* file pointer */ + char fmt[32]; /* picture format */ + double pa; /* pixel aspect ratio */ + RESOLU rs; /* picture resolution */ +} PICTURE; +uint16 comp = COMPRESSION_NONE; /* TIFF compression mode */ -main(argc, argv) -int argc; -char *argv[]; +#define closepicture(p) (fclose((p)->fp),free((void *)(p))) + +static gethfunc headline; + +static int headline(char *s, void *pp); +static PICTURE *openpicture(char *fname); +static int tmap_picture(char *fname, PICTURE *pp); +static int tmap_tiff(char *fname, TIFF *tp); +static int putimage(uint16 or, uint32 xs, uint32 ys, float xr, float yr, + uint16 ru, uby8 *pd); + + +int +main( + int argc, + char *argv[] +) { - FILE *fin = NULL; + PICTURE *pin = NULL; TIFF *tin = NULL; int i, rval; @@ -78,6 +97,9 @@ char *argv[]; if (argc-i < 2) goto userr; lddyn = atof(argv[++i]); break; + case 'z': /* LZW compression */ + comp = COMPRESSION_LZW; + break; case 'p': /* set display primaries */ if (argc-i < 9) goto userr; myprims[RED][CIEX] = atof(argv[++i]); @@ -94,7 +116,7 @@ char *argv[]; goto userr; } if (argc-i < 2) goto userr; - if ((fin = openpicture(argv[i])) == NULL && + if ((pin = openpicture(argv[i])) == NULL && (tin = TIFFOpen(argv[i], "r")) == NULL) { fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n", argv[0], argv[i]); @@ -105,9 +127,9 @@ char *argv[]; argv[0], argv[i+1]); exit(1); } - if (fin != NULL) { - rval = tmap_picture(argv[i], fin); - fclose(fin); + if (pin != NULL) { + rval = tmap_picture(argv[i], pin); + closepicture(pin); } else { rval = tmap_tiff(argv[i], tin); TIFFClose(tin); @@ -116,19 +138,38 @@ char *argv[]; exit(rval==0 ? 0 : 1); userr: fprintf(stderr, -"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", +"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", argv[0]); exit(1); } -FILE * -openpicture(fname) /* open/check Radiance picture file */ -char *fname; +static int +headline( /* process line from header */ + char *s, + void *pp +) { + register char *cp; + + for (cp = s; *cp; cp++) + if (*cp & 0x80) + return(-1); /* non-ascii in header */ + if (isaspect(s)) + ((PICTURE *)pp)->pa *= aspectval(s); + else + formatval(((PICTURE *)pp)->fmt, s); + return(0); +} + + +static PICTURE * +openpicture( /* open/check Radiance picture file */ + char *fname +) +{ FILE *fp; - char inpfmt[32]; - int xsiz, ysiz; + register PICTURE *pp; register char *cp; /* check filename suffix */ if (fname == NULL) return(NULL); @@ -144,80 +185,69 @@ char *fname; /* else try opening it */ if ((fp = fopen(fname, "r")) == NULL) return(NULL); - /* check format */ - strcpy(inpfmt, PICFMT); - if (checkheader(fp, inpfmt, NULL) < 0 || - fgetresolu(&xsiz, &ysiz, fp) < 0) { - fclose(fp); /* failed test -- close file */ + SET_FILE_BINARY(fp); + /* allocate struct */ + if ((pp = (PICTURE *)malloc(sizeof(PICTURE))) == NULL) + return(NULL); /* serious error -- should exit? */ + pp->fp = fp; pp->fmt[0] = '\0'; pp->pa = 1.; + /* load header */ + if (getheader(fp, headline, pp) < 0) { + closepicture(pp); return(NULL); } + if (!pp->fmt[0]) /* assume RGBE if unspecified */ + strcpy(pp->fmt, COLRFMT); + if (!globmatch(PICFMT, pp->fmt) || !fgetsresolu(&pp->rs, fp)) { + closepicture(pp); /* failed test -- close file */ + return(NULL); + } rewind(fp); /* passed test -- rewind file */ - return(fp); + return(pp); } -getpixrat(s, pr) /* get pixel aspect ratio */ -char *s; -double *pr; +static int +tmap_picture( /* tone map Radiance picture */ + char *fname, + register PICTURE *pp +) { - if (isaspect(s)) - *pr *= aspectval(s); -} - - -int -tmap_picture(fname, fp) /* tone map Radiance picture */ -char *fname; -FILE *fp; -{ - double pixrat; - int ord; uint16 orient; + double paspect = (pp->rs.rt & YMAJOR) ? pp->pa : 1./pp->pa; int xsiz, ysiz; - BYTE *pix; + uby8 *pix; /* read and tone map picture */ if (tmMapPicture(&pix, &xsiz, &ysiz, flags, - rgbp, gamv, lddyn, ldmax, fname, fp) != TM_E_OK) + rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK) return(-1); - /* get relevant header info. */ - rewind(fp); - pixrat = 1.; - getheader(fp, getpixrat, &pixrat); - if ((ord = fgetresolu(&xsiz, &ysiz, fp)) < 0) - orient = 0; - else - for (orient = 8; --orient; ) - if (ortab[orient] == ord) - break; + /* figure out TIFF orientation */ + for (orient = 8; --orient; ) + if (ortab[orient] == pp->rs.rt) + break; orient++; /* put out our image */ if (putimage(orient, (uint32)xsiz, (uint32)ysiz, - 72., 72./pixrat, 2, pix) != 0) + 72., 72./paspect, 2, pix) != 0) return(-1); /* free data and we're done */ - free((char *)pix); + free((void *)pix); return(0); } -tmap_tiff(fname, tp) /* tone map SGILOG TIFF */ -char *fname; -TIFF *tp; +static int +tmap_tiff( /* tone map SGILOG TIFF */ + char *fname, + TIFF *tp +) { float xres, yres; uint16 orient, resunit, phot; int xsiz, ysiz; - BYTE *pix; + uby8 *pix; /* check to make sure it's SGILOG */ TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot); - if (phot != PHOTOMETRIC_LOGLUV && phot != PHOTOMETRIC_LOGL) { - if (!(flags & TM_F_NOSTDERR)) { - fputs(fname, stderr); - fputs(": TIFF must be in SGILOG format\n", stderr); - } - return(-1); - } - if (phot == PHOTOMETRIC_LOGL) + if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK)) flags |= TM_F_BW; /* read and tone map TIFF */ if (tmMapTIFF(&pix, &xsiz, &ysiz, flags, @@ -233,17 +263,21 @@ TIFF *tp; xres, yres, resunit, pix) != 0) return(-1); /* free data and we're done */ - free((char *)pix); + free((void *)pix); return(0); } -putimage(or, xs, ys, xr, yr, ru, pd) /* write out our image */ -uint16 or; -uint32 xs, ys; -float xr, yr; -uint16 ru; -BYTE *pd; +static int +putimage( /* write out our image */ + uint16 or, + uint32 xs, + uint32 ys, + float xr, + float yr, + uint16 ru, + uby8 *pd +) { register int y; uint32 rowsperstrip; @@ -266,6 +300,7 @@ BYTE *pd; TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs); TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys); TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru); + TIFFSetField(tifout, TIFFTAG_COMPRESSION, comp); TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr); TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr); TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);