ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.10
Committed: Tue Sep 26 12:26:02 2006 UTC (17 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R8, rad3R9
Changes since 3.9: +3 -2 lines
Log Message:
Fixed bug in pixel aspect ratio for some image orientations

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 greg 3.10 static const char RCSid[] = "$Id: normtiff.c,v 3.9 2005/06/14 22:23:31 greg Exp $";
3 gwlarson 3.1 #endif
4     /*
5     * Tone map SGILOG TIFF or Radiance picture and output 24-bit RGB TIFF
6     */
7    
8     #include <stdio.h>
9     #include <math.h>
10 greg 3.4 #include <time.h>
11 schorsch 3.7 #include <string.h>
12    
13 gwlarson 3.1 #include "tiffio.h"
14     #include "color.h"
15     #include "tonemap.h"
16 schorsch 3.8 #include "tmaptiff.h"
17 gwlarson 3.1 #include "resolu.h"
18    
19    
20     TIFF *tifout; /* TIFF output */
21     int flags = TM_F_CAMERA; /* tone-mapping flags */
22     RGBPRIMP rgbp = stdprims; /* display primaries */
23     RGBPRIMS myprims; /* overriding display primaries */
24     double ldmax = 100.; /* maximum display luminance */
25     double lddyn = 32.; /* display dynamic range */
26     double gamv = 2.2; /* display gamma value */
27    
28     short ortab[8] = { /* orientation conversion table */
29     YMAJOR|YDECR,
30     YMAJOR|YDECR|XDECR,
31     YMAJOR|XDECR,
32     YMAJOR,
33     YDECR,
34     XDECR|YDECR,
35     XDECR,
36     0
37     };
38    
39 gwlarson 3.3 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 gwlarson 3.1
46 greg 3.9 uint16 comp = COMPRESSION_NONE; /* TIFF compression mode */
47 gwlarson 3.1
48 greg 3.4 #define closepicture(p) (fclose((p)->fp),free((void *)(p)))
49 gwlarson 3.3
50 schorsch 3.7 static gethfunc headline;
51    
52 greg 3.9 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 schorsch 3.8 uint16 ru, BYTE *pd);
58    
59 gwlarson 3.3
60 schorsch 3.7 int
61     main(
62     int argc,
63     char *argv[]
64     )
65 gwlarson 3.1 {
66 gwlarson 3.3 PICTURE *pin = NULL;
67 gwlarson 3.1 TIFF *tin = NULL;
68 gwlarson 3.2 int i, rval;
69 gwlarson 3.1
70     for (i = 1; i < argc && argv[i][0] == '-'; i++)
71     switch (argv[i][1]) {
72     case 'h': /* human observer settings */
73     flags = TM_F_HUMAN;
74     break;
75     case 's': /* toggle human contrast */
76     flags ^= TM_F_HCONTR;
77     break;
78     case 'c': /* toggle mesopic sens. */
79     flags ^= TM_F_MESOPIC;
80     break;
81     case 'l': /* toggle linear mapping */
82     flags ^= TM_F_LINEAR;
83     break;
84     case 'b': /* toggle greyscale output */
85     flags ^= TM_F_BW;
86     break;
87     case 'g': /* set display gamma */
88     if (argc-i < 2) goto userr;
89     gamv = atof(argv[++i]);
90     break;
91     case 'u': /* set display maximum */
92     if (argc-i < 2) goto userr;
93     ldmax = atof(argv[++i]);
94     break;
95     case 'd': /* set display dynamic range */
96     if (argc-i < 2) goto userr;
97     lddyn = atof(argv[++i]);
98     break;
99 greg 3.9 case 'z': /* LZW compression */
100     comp = COMPRESSION_LZW;
101     break;
102 gwlarson 3.1 case 'p': /* set display primaries */
103     if (argc-i < 9) goto userr;
104     myprims[RED][CIEX] = atof(argv[++i]);
105     myprims[RED][CIEY] = atof(argv[++i]);
106     myprims[GRN][CIEX] = atof(argv[++i]);
107     myprims[GRN][CIEY] = atof(argv[++i]);
108     myprims[BLU][CIEX] = atof(argv[++i]);
109     myprims[BLU][CIEY] = atof(argv[++i]);
110     myprims[WHT][CIEX] = atof(argv[++i]);
111     myprims[WHT][CIEY] = atof(argv[++i]);
112     rgbp = myprims;
113     break;
114     default:
115     goto userr;
116     }
117     if (argc-i < 2) goto userr;
118 gwlarson 3.3 if ((pin = openpicture(argv[i])) == NULL &&
119 gwlarson 3.1 (tin = TIFFOpen(argv[i], "r")) == NULL) {
120     fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n",
121     argv[0], argv[i]);
122     exit(1);
123     }
124     if ((tifout = TIFFOpen(argv[i+1], "w")) == NULL) {
125     fprintf(stderr, "%s: cannot open output TIFF \"%s\"\n",
126     argv[0], argv[i+1]);
127     exit(1);
128     }
129 gwlarson 3.3 if (pin != NULL) {
130     rval = tmap_picture(argv[i], pin);
131     closepicture(pin);
132 gwlarson 3.1 } else {
133 gwlarson 3.2 rval = tmap_tiff(argv[i], tin);
134 gwlarson 3.1 TIFFClose(tin);
135     }
136     TIFFClose(tifout);
137 gwlarson 3.2 exit(rval==0 ? 0 : 1);
138 gwlarson 3.1 userr:
139     fprintf(stderr,
140 greg 3.9 "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|pic} output.tif\n",
141 gwlarson 3.1 argv[0]);
142     exit(1);
143     }
144    
145    
146 schorsch 3.7 static int
147     headline( /* process line from header */
148     char *s,
149     void *pp
150     )
151 gwlarson 3.3 {
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 schorsch 3.7 ((PICTURE *)pp)->pa *= aspectval(s);
159 gwlarson 3.3 else
160 schorsch 3.7 formatval(((PICTURE *)pp)->fmt, s);
161 gwlarson 3.3 return(0);
162     }
163    
164    
165 schorsch 3.8 static PICTURE *
166     openpicture( /* open/check Radiance picture file */
167     char *fname
168     )
169 gwlarson 3.1 {
170     FILE *fp;
171 gwlarson 3.3 register PICTURE *pp;
172 gwlarson 3.1 register char *cp;
173     /* check filename suffix */
174     if (fname == NULL) return(NULL);
175     for (cp = fname; *cp; cp++)
176     ;
177     while (cp > fname && cp[-1] != '.')
178     if (*--cp == '/') {
179     cp = fname;
180     break;
181     }
182     if (cp > fname && !strncmp(cp, "tif", 3))
183     return(NULL); /* assume it's a TIFF */
184     /* else try opening it */
185     if ((fp = fopen(fname, "r")) == NULL)
186     return(NULL);
187 gwlarson 3.3 /* 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 schorsch 3.7 if (getheader(fp, headline, pp) < 0) {
193 gwlarson 3.3 closepicture(pp);
194 gwlarson 3.1 return(NULL);
195     }
196 gwlarson 3.3 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 gwlarson 3.1 rewind(fp); /* passed test -- rewind file */
203 gwlarson 3.3 return(pp);
204 gwlarson 3.1 }
205    
206    
207 schorsch 3.8 static int
208     tmap_picture( /* tone map Radiance picture */
209     char *fname,
210     register PICTURE *pp
211     )
212 gwlarson 3.1 {
213     uint16 orient;
214 greg 3.10 double paspect = (pp->rs.rt & YMAJOR) ? pp->pa : 1./pp->pa;
215 gwlarson 3.1 int xsiz, ysiz;
216     BYTE *pix;
217     /* read and tone map picture */
218     if (tmMapPicture(&pix, &xsiz, &ysiz, flags,
219 gwlarson 3.3 rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK)
220 gwlarson 3.2 return(-1);
221 gwlarson 3.3 /* figure out TIFF orientation */
222     for (orient = 8; --orient; )
223 greg 3.4 if (ortab[orient] == pp->rs.rt)
224 gwlarson 3.3 break;
225 gwlarson 3.1 orient++;
226     /* put out our image */
227 gwlarson 3.2 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
228 greg 3.10 72., 72./paspect, 2, pix) != 0)
229 gwlarson 3.2 return(-1);
230 gwlarson 3.1 /* free data and we're done */
231 greg 3.4 free((void *)pix);
232 gwlarson 3.2 return(0);
233 gwlarson 3.1 }
234    
235    
236 schorsch 3.8 static int
237     tmap_tiff( /* tone map SGILOG TIFF */
238     char *fname,
239     TIFF *tp
240     )
241 gwlarson 3.1 {
242     float xres, yres;
243     uint16 orient, resunit, phot;
244     int xsiz, ysiz;
245     BYTE *pix;
246     /* check to make sure it's SGILOG */
247     TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot);
248 schorsch 3.6 if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK))
249 gwlarson 3.1 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 gwlarson 3.2 return(-1);
254 gwlarson 3.1 /* 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 gwlarson 3.2 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
261     xres, yres, resunit, pix) != 0)
262     return(-1);
263 gwlarson 3.1 /* free data and we're done */
264 greg 3.4 free((void *)pix);
265 gwlarson 3.2 return(0);
266 gwlarson 3.1 }
267    
268    
269 schorsch 3.8 static int
270     putimage( /* write out our image */
271     uint16 or,
272     uint32 xs,
273     uint32 ys,
274     float xr,
275     float yr,
276 greg 3.9 uint16 ru,
277 schorsch 3.8 BYTE *pd
278     )
279 gwlarson 3.1 {
280     register int y;
281     uint32 rowsperstrip;
282    
283     TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
284     if (flags & TM_F_BW) {
285     TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC,
286     PHOTOMETRIC_MINISBLACK);
287     TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 1);
288     } else {
289     TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
290     TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 3);
291     }
292     if (rgbp != stdprims) {
293     TIFFSetField(tifout, TIFFTAG_PRIMARYCHROMATICITIES,
294     (float *)rgbp);
295     TIFFSetField(tifout, TIFFTAG_WHITEPOINT, (float *)rgbp[WHT]);
296     }
297     TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, 8);
298     TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs);
299     TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys);
300     TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru);
301 greg 3.9 TIFFSetField(tifout, TIFFTAG_COMPRESSION, comp);
302 gwlarson 3.1 TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr);
303     TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr);
304     TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
305     TIFFSetField(tifout, TIFFTAG_ORIENTATION, or);
306     /* compute good strip size */
307     rowsperstrip = 8192/TIFFScanlineSize(tifout);
308     if (rowsperstrip < 1) rowsperstrip = 1;
309     TIFFSetField(tifout, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
310     /* write out scanlines */
311     if (flags & TM_F_BW) {
312     for (y = 0; y < ys; y++)
313     if (TIFFWriteScanline(tifout, pd + y*xs, y, 0) < 0)
314     goto writerr;
315     } else {
316     for (y = 0; y < ys; y++)
317     if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0)
318     goto writerr;
319     }
320 gwlarson 3.2 return(0); /* all done! */
321 gwlarson 3.1 writerr:
322     fputs("Error writing TIFF output\n", stderr);
323 gwlarson 3.2 return(-1);
324 gwlarson 3.1 }