ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.13
Committed: Fri May 20 02:06:39 2011 UTC (12 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R1, rad4R2P1
Changes since 3.12: +5 -5 lines
Log Message:
Changed every instance of BYTE to uby8 to avoid conflicts

File Contents

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