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.11 by greg, Mon Nov 10 19:08:19 2008 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"
16 + #include "tmaptiff.h"
17   #include "resolu.h"
18  
19  
# Line 38 | Line 36 | short  ortab[8] = {            /* orientation conversion table */
36          0
37   };
38  
39 < extern FILE     *openpicture();
39 > typedef struct {
40 >        FILE    *fp;            /* file pointer */
41 >        char    fmt[32];        /* picture format */
42 >        double  pa;             /* pixel aspect ratio */
43 >        RESOLU  rs;             /* picture resolution */
44 > } PICTURE;
45  
46 + uint16  comp = COMPRESSION_NONE;        /* TIFF compression mode */
47  
48 < main(argc, argv)
49 < int     argc;
50 < char    *argv[];
48 > #define closepicture(p)         (fclose((p)->fp),free((void *)(p)))
49 >
50 > static gethfunc headline;
51 >
52 > static int headline(char *s, void *pp);
53 > static PICTURE *openpicture(char *fname);
54 > static int tmap_picture(char *fname, PICTURE *pp);
55 > static int tmap_tiff(char *fname, TIFF *tp);
56 > static int putimage(uint16 or, uint32 xs, uint32 ys, float xr, float yr,
57 >        uint16 ru, BYTE *pd);
58 >
59 >
60 > int
61 > main(
62 >        int     argc,
63 >        char    *argv[]
64 > )
65   {
66 <        FILE    *fin = NULL;
66 >        PICTURE *pin = NULL;
67          TIFF    *tin = NULL;
68 <        int     i;
68 >        int     i, rval;
69  
70          for (i = 1; i < argc && argv[i][0] == '-'; i++)
71                  switch (argv[i][1]) {
# Line 78 | Line 96 | char   *argv[];
96                          if (argc-i < 2) goto userr;
97                          lddyn = atof(argv[++i]);
98                          break;
99 +                case 'z':                       /* LZW compression */
100 +                        comp = COMPRESSION_LZW;
101 +                        break;
102                  case 'p':                       /* set display primaries */
103                          if (argc-i < 9) goto userr;
104                          myprims[RED][CIEX] = atof(argv[++i]);
# Line 94 | Line 115 | char   *argv[];
115                          goto userr;
116                  }
117          if (argc-i < 2) goto userr;
118 <        if ((fin = openpicture(argv[i])) == NULL &&
118 >        if ((pin = openpicture(argv[i])) == NULL &&
119                          (tin = TIFFOpen(argv[i], "r")) == NULL) {
120                  fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n",
121                                  argv[0], argv[i]);
# Line 105 | Line 126 | char   *argv[];
126                                  argv[0], argv[i+1]);
127                  exit(1);
128          }
129 <        if (fin != NULL) {
130 <                tmap_picture(argv[i], fin);
131 <                fclose(fin);
129 >        if (pin != NULL) {
130 >                rval = tmap_picture(argv[i], pin);
131 >                closepicture(pin);
132          } else {
133 <                tmap_tiff(argv[i], tin);
133 >                rval = tmap_tiff(argv[i], tin);
134                  TIFFClose(tin);
135          }
136          TIFFClose(tifout);
137 <        exit(0);
137 >        exit(rval==0 ? 0 : 1);
138   userr:
139          fprintf(stderr,
140 < "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",
140 > "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",
141                          argv[0]);
142          exit(1);
143   }
144  
145  
146 < FILE *
147 < openpicture(fname)                      /* open/check Radiance picture file */
148 < char    *fname;
146 > static int
147 > headline(                               /* process line from header */
148 >        char    *s,
149 >        void *pp
150 > )
151   {
152 +        register char   *cp;
153 +
154 +        for (cp = s; *cp; cp++)
155 +                if (*cp & 0x80)
156 +                        return(-1);     /* non-ascii in header */
157 +        if (isaspect(s))
158 +                ((PICTURE *)pp)->pa *= aspectval(s);
159 +        else
160 +                formatval(((PICTURE *)pp)->fmt, s);
161 +        return(0);
162 + }
163 +
164 +
165 + static PICTURE *
166 + openpicture(                    /* open/check Radiance picture file */
167 +        char    *fname
168 + )
169 + {
170          FILE    *fp;
171 <        char    inpfmt[32];
131 <        int     xsiz, ysiz;
171 >        register PICTURE        *pp;
172          register char   *cp;
173                                          /* check filename suffix */
174          if (fname == NULL) return(NULL);
# Line 144 | Line 184 | char   *fname;
184                                          /* else try opening it */
185          if ((fp = fopen(fname, "r")) == NULL)
186                  return(NULL);
187 <                                        /* check format */
188 <        strcpy(inpfmt, PICFMT);
189 <        if (checkheader(fp, inpfmt, NULL) < 0 ||
190 <                        fgetresolu(&xsiz, &ysiz, fp) < 0) {
191 <                fclose(fp);             /* failed test -- close file */
187 >                                        /* allocate struct */
188 >        if ((pp = (PICTURE *)malloc(sizeof(PICTURE))) == NULL)
189 >                return(NULL);           /* serious error -- should exit? */
190 >        pp->fp = fp; pp->fmt[0] = '\0'; pp->pa = 1.;
191 >                                        /* load header */
192 >        if (getheader(fp, headline, pp) < 0) {
193 >                closepicture(pp);
194                  return(NULL);
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)) {
199 +                closepicture(pp);       /* failed test -- close file */
200 +                return(NULL);
201 +        }
202          rewind(fp);                     /* passed test -- rewind file */
203 <        return(fp);
203 >        return(pp);
204   }
205  
206  
207 < getpixrat(s, pr)                        /* get pixel aspect ratio */
208 < char    *s;
209 < double  *pr;
207 > static int
208 > tmap_picture(                   /* tone map Radiance picture */
209 >        char    *fname,
210 >        register PICTURE        *pp
211 > )
212   {
163        if (isaspect(s))
164                *pr *= aspectval(s);
165 }
166
167
168 tmap_picture(fname, fp)                 /* tone map Radiance picture */
169 char    *fname;
170 FILE    *fp;
171 {
172        double  pixrat;
173        int     ord;
213          uint16  orient;
214 +        double  paspect = (pp->rs.rt & YMAJOR) ? pp->pa : 1./pp->pa;
215          int     xsiz, ysiz;
216          BYTE    *pix;
217                                          /* read and tone map picture */
218          if (tmMapPicture(&pix, &xsiz, &ysiz, flags,
219 <                        rgbp, gamv, lddyn, ldmax, fname, fp) != TM_E_OK)
220 <                exit(1);
221 <                                        /* get relevant header info. */
222 <        rewind(fp);
223 <        pixrat = 1.;
224 <        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;
219 >                        rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK)
220 >                return(-1);
221 >                                        /* figure out TIFF orientation */
222 >        for (orient = 8; --orient; )
223 >                if (ortab[orient] == pp->rs.rt)
224 >                        break;
225          orient++;
226                                          /* put out our image */
227 <        putimage(orient, (uint32)xsiz, (uint32)ysiz, 72., 72./pixrat, 2, pix);
227 >        if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
228 >                        72., 72./paspect, 2, pix) != 0)
229 >                return(-1);
230                                          /* free data and we're done */
231 <        free((char *)pix);
231 >        free((void *)pix);
232 >        return(0);
233   }
234  
235  
236 < tmap_tiff(fname, tp)                    /* tone map SGILOG TIFF */
237 < char    *fname;
238 < TIFF    *tp;
236 > static int
237 > tmap_tiff(                      /* tone map SGILOG TIFF */
238 >        char    *fname,
239 >        TIFF    *tp
240 > )
241   {
242          float   xres, yres;
243          uint16  orient, resunit, phot;
# Line 206 | Line 245 | TIFF   *tp;
245          BYTE    *pix;
246                                          /* check to make sure it's SGILOG */
247          TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot);
248 <        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)
248 >        if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK))
249                  flags |= TM_F_BW;
250                                          /* read and tone map TIFF */
251          if (tmMapTIFF(&pix, &xsiz, &ysiz, flags,
252                          rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK)
253 <                exit(1);
253 >                return(-1);
254                                          /* get relevant tags */
255          TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit);
256          TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres);
257          TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres);
258          TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient);
259                                          /* put out our image */
260 <        putimage(orient, (uint32)xsiz, (uint32)ysiz, xres, yres, resunit, pix);
260 >        if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
261 >                        xres, yres, resunit, pix) != 0)
262 >                return(-1);
263                                          /* free data and we're done */
264 <        free((char *)pix);
264 >        free((void *)pix);
265 >        return(0);
266   }
267  
268  
269 < putimage(or, xs, ys, xr, yr, ru, pd)    /* write out our image */
270 < uint16  or;
271 < uint32  xs, ys;
272 < float   xr, yr;
273 < uint16 ru;
274 < BYTE    *pd;
269 > static int
270 > putimage(       /* write out our image */
271 >        uint16  or,
272 >        uint32  xs,
273 >        uint32  ys,
274 >        float   xr,
275 >        float   yr,
276 >        uint16  ru,
277 >        BYTE    *pd
278 > )
279   {
280          register int    y;
281          uint32  rowsperstrip;
# Line 256 | Line 298 | BYTE   *pd;
298          TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs);
299          TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys);
300          TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru);
301 +        TIFFSetField(tifout, TIFFTAG_COMPRESSION, comp);
302          TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr);
303          TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr);
304          TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
# Line 274 | Line 317 | BYTE   *pd;
317                          if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0)
318                                  goto writerr;
319          }
320 <        return;                         /* all done! */
320 >        return(0);                      /* all done! */
321   writerr:
322          fputs("Error writing TIFF output\n", stderr);
323 <        exit(2);
323 >        return(-1);
324   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines