ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
(Generate patch)

Comparing ray/src/px/normtiff.c (file contents):
Revision 3.1 by gwlarson, Mon Oct 26 17:05:28 1998 UTC vs.
Revision 3.7 by schorsch, Fri Jan 2 11:42:37 2004 UTC

# Line 1 | Line 1
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"
# Line 38 | Line 35 | short  ortab[8] = {            /* orientation conversion table */
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;
60 >        int     i, rval;
61  
62          for (i = 1; i < argc && argv[i][0] == '-'; i++)
63                  switch (argv[i][1]) {
# Line 94 | Line 104 | char   *argv[];
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]);
# Line 105 | Line 115 | char   *argv[];
115                                  argv[0], argv[i+1]);
116                  exit(1);
117          }
118 <        if (fin != NULL) {
119 <                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 <                tmap_tiff(argv[i], tin);
122 >                rval = tmap_tiff(argv[i], tin);
123                  TIFFClose(tin);
124          }
125          TIFFClose(tifout);
126 <        exit(0);
126 >        exit(rval==0 ? 0 : 1);
127   userr:
128          fprintf(stderr,
129   "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",
# Line 122 | Line 132 | userr:
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);
# Line 144 | Line 172 | char   *fname;
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  
195 < getpixrat(s, pr)                        /* get pixel aspect ratio */
196 < 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 */
195 > int
196 > tmap_picture(fname, pp)                 /* tone map Radiance picture */
197   char    *fname;
198 < FILE    *fp;
198 > register PICTURE        *pp;
199   {
172        double  pixrat;
173        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)
206 <                exit(1);
207 <                                        /* get relevant header info. */
208 <        rewind(fp);
209 <        pixrat = 1.;
210 <        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;
205 >                        rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK)
206 >                return(-1);
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 <        putimage(orient, (uint32)xsiz, (uint32)ysiz, 72., 72./pixrat, 2, pix);
213 >        if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
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  
221  
# Line 206 | Line 229 | TIFF   *tp;
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) {
210 <                fprintf(stderr, "%s: TIFF must be in SGILOG format\n", fname);
211 <                exit(1);
212 <        }
213 <        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,
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  
# Line 274 | Line 296 | BYTE   *pd;
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines