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.2 by gwlarson, Mon Oct 26 17:30:50 1998 UTC vs.
Revision 3.15 by greg, Fri Jul 19 17:37:56 2019 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
14 #include <stdio.h>
8   #include <math.h>
9 +
10 + #include "rtio.h"
11 + #include "platform.h"
12   #include "tiffio.h"
13   #include "color.h"
14   #include "tonemap.h"
15 + #include "tmaptiff.h"
16   #include "resolu.h"
17  
18  
# 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[MAXFMTLEN]; /* picture format */
41 >        double  pa;             /* pixel aspect ratio */
42 >        RESOLU  rs;             /* picture resolution */
43 > } PICTURE;
44  
45 + uint16  comp = COMPRESSION_NONE;        /* TIFF compression mode */
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 > static int headline(char *s, void *pp);
52 > static PICTURE *openpicture(char *fname);
53 > static int tmap_picture(char *fname, PICTURE *pp);
54 > static int tmap_tiff(char *fname, TIFF *tp);
55 > static int putimage(uint16 or, uint32 xs, uint32 ys, float xr, float yr,
56 >        uint16 ru, uby8 *pd);
57 >
58 >
59 > int
60 > main(
61 >        int     argc,
62 >        char    *argv[]
63 > )
64   {
65 <        FILE    *fin = NULL;
65 >        PICTURE *pin = NULL;
66          TIFF    *tin = NULL;
67          int     i, rval;
68  
# Line 78 | Line 95 | char   *argv[];
95                          if (argc-i < 2) goto userr;
96                          lddyn = atof(argv[++i]);
97                          break;
98 +                case 'z':                       /* LZW compression */
99 +                        comp = COMPRESSION_LZW;
100 +                        break;
101                  case 'p':                       /* set display primaries */
102                          if (argc-i < 9) goto userr;
103                          myprims[RED][CIEX] = atof(argv[++i]);
# Line 94 | Line 114 | char   *argv[];
114                          goto userr;
115                  }
116          if (argc-i < 2) goto userr;
117 <        if ((fin = openpicture(argv[i])) == NULL &&
117 >        if ((pin = openpicture(argv[i])) == NULL &&
118                          (tin = TIFFOpen(argv[i], "r")) == NULL) {
119                  fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n",
120                                  argv[0], argv[i]);
# Line 105 | Line 125 | char   *argv[];
125                                  argv[0], argv[i+1]);
126                  exit(1);
127          }
128 <        if (fin != NULL) {
129 <                rval = tmap_picture(argv[i], fin);
130 <                fclose(fin);
128 >        if (pin != NULL) {
129 >                rval = tmap_picture(argv[i], pin);
130 >                closepicture(pin);
131          } else {
132                  rval = tmap_tiff(argv[i], tin);
133                  TIFFClose(tin);
# Line 116 | Line 136 | char   *argv[];
136          exit(rval==0 ? 0 : 1);
137   userr:
138          fprintf(stderr,
139 < "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",
139 > "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",
140                          argv[0]);
141          exit(1);
142   }
143  
144  
145 < FILE *
146 < openpicture(fname)                      /* open/check Radiance picture file */
147 < char    *fname;
145 > static int
146 > headline(                               /* process line from header */
147 >        char    *s,
148 >        void *pp
149 > )
150   {
151 +        register char   *cp;
152 +
153 +        for (cp = s; *cp; cp++)
154 +                if (*cp & 0x80)
155 +                        return(-1);     /* non-ascii in header */
156 +        if (isaspect(s))
157 +                ((PICTURE *)pp)->pa *= aspectval(s);
158 +        else
159 +                formatval(((PICTURE *)pp)->fmt, s);
160 +        return(0);
161 + }
162 +
163 +
164 + static PICTURE *
165 + openpicture(                    /* open/check Radiance picture file */
166 +        char    *fname
167 + )
168 + {
169          FILE    *fp;
170 <        char    inpfmt[32];
131 <        int     xsiz, ysiz;
170 >        register PICTURE        *pp;
171          register char   *cp;
172                                          /* check filename suffix */
173          if (fname == NULL) return(NULL);
# Line 144 | Line 183 | char   *fname;
183                                          /* else try opening it */
184          if ((fp = fopen(fname, "r")) == NULL)
185                  return(NULL);
186 <                                        /* check format */
187 <        strcpy(inpfmt, PICFMT);
188 <        if (checkheader(fp, inpfmt, NULL) < 0 ||
189 <                        fgetresolu(&xsiz, &ysiz, fp) < 0) {
190 <                fclose(fp);             /* failed test -- close file */
186 >        SET_FILE_BINARY(fp);
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 int
169 tmap_picture(fname, fp)                 /* tone map Radiance picture */
170 char    *fname;
171 FILE    *fp;
172 {
173        double  pixrat;
174        int     ord;
213          uint16  orient;
214 +        double  paspect = (pp->rs.rt & YMAJOR) ? pp->pa : 1./pp->pa;
215          int     xsiz, ysiz;
216 <        BYTE    *pix;
216 >        uby8    *pix;
217                                          /* read and tone map picture */
218          if (tmMapPicture(&pix, &xsiz, &ysiz, flags,
219 <                        rgbp, gamv, lddyn, ldmax, fname, fp) != TM_E_OK)
219 >                        rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK)
220                  return(-1);
221 <                                        /* get relevant header info. */
222 <        rewind(fp);
223 <        pixrat = 1.;
224 <        getheader(fp, getpixrat, &pixrat);
186 <        if ((ord = fgetresolu(&xsiz, &ysiz, fp)) < 0)
187 <                orient = 0;
188 <        else
189 <                for (orient = 8; --orient; )
190 <                        if (ortab[orient] == ord)
191 <                                break;
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          if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
228 <                        72., 72./pixrat, 2, pix) != 0)
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;
244          int     xsiz, ysiz;
245 <        BYTE    *pix;
245 >        uby8    *pix;
246                                          /* check to make sure it's SGILOG */
247          TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot);
248 <        if (phot != PHOTOMETRIC_LOGLUV && phot != PHOTOMETRIC_LOGL) {
214 <                if (!(flags & TM_F_NOSTDERR)) {
215 <                        fputs(fname, stderr);
216 <                        fputs(": TIFF must be in SGILOG format\n", stderr);
217 <                }
218 <                return(-1);
219 <        }
220 <        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,
# Line 233 | Line 261 | TIFF   *tp;
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 >        uby8    *pd
278 > )
279   {
280          register int    y;
281          uint32  rowsperstrip;
# Line 266 | 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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines