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