ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.8
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 3.7: +34 -18 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 schorsch 3.8 static const char RCSid[] = "$Id: normtiff.c,v 3.7 2004/01/02 11:42:37 schorsch 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 schorsch 3.8 //extern PICTURE *openpicture();
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 schorsch 3.8 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 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     case 'p': /* set display primaries */
100     if (argc-i < 9) goto userr;
101     myprims[RED][CIEX] = atof(argv[++i]);
102     myprims[RED][CIEY] = atof(argv[++i]);
103     myprims[GRN][CIEX] = atof(argv[++i]);
104     myprims[GRN][CIEY] = atof(argv[++i]);
105     myprims[BLU][CIEX] = atof(argv[++i]);
106     myprims[BLU][CIEY] = atof(argv[++i]);
107     myprims[WHT][CIEX] = atof(argv[++i]);
108     myprims[WHT][CIEY] = atof(argv[++i]);
109     rgbp = myprims;
110     break;
111     default:
112     goto userr;
113     }
114     if (argc-i < 2) goto userr;
115 gwlarson 3.3 if ((pin = openpicture(argv[i])) == NULL &&
116 gwlarson 3.1 (tin = TIFFOpen(argv[i], "r")) == NULL) {
117     fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n",
118     argv[0], argv[i]);
119     exit(1);
120     }
121     if ((tifout = TIFFOpen(argv[i+1], "w")) == NULL) {
122     fprintf(stderr, "%s: cannot open output TIFF \"%s\"\n",
123     argv[0], argv[i+1]);
124     exit(1);
125     }
126 gwlarson 3.3 if (pin != NULL) {
127     rval = tmap_picture(argv[i], pin);
128     closepicture(pin);
129 gwlarson 3.1 } else {
130 gwlarson 3.2 rval = tmap_tiff(argv[i], tin);
131 gwlarson 3.1 TIFFClose(tin);
132     }
133     TIFFClose(tifout);
134 gwlarson 3.2 exit(rval==0 ? 0 : 1);
135 gwlarson 3.1 userr:
136     fprintf(stderr,
137     "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",
138     argv[0]);
139     exit(1);
140     }
141    
142    
143 schorsch 3.7 static int
144     headline( /* process line from header */
145     char *s,
146     void *pp
147     )
148 gwlarson 3.3 {
149     register char *cp;
150    
151     for (cp = s; *cp; cp++)
152     if (*cp & 0x80)
153     return(-1); /* non-ascii in header */
154     if (isaspect(s))
155 schorsch 3.7 ((PICTURE *)pp)->pa *= aspectval(s);
156 gwlarson 3.3 else
157 schorsch 3.7 formatval(((PICTURE *)pp)->fmt, s);
158 gwlarson 3.3 return(0);
159     }
160    
161    
162 schorsch 3.8 static PICTURE *
163     openpicture( /* open/check Radiance picture file */
164     char *fname
165     )
166 gwlarson 3.1 {
167     FILE *fp;
168 gwlarson 3.3 register PICTURE *pp;
169 gwlarson 3.1 register char *cp;
170     /* check filename suffix */
171     if (fname == NULL) return(NULL);
172     for (cp = fname; *cp; cp++)
173     ;
174     while (cp > fname && cp[-1] != '.')
175     if (*--cp == '/') {
176     cp = fname;
177     break;
178     }
179     if (cp > fname && !strncmp(cp, "tif", 3))
180     return(NULL); /* assume it's a TIFF */
181     /* else try opening it */
182     if ((fp = fopen(fname, "r")) == NULL)
183     return(NULL);
184 gwlarson 3.3 /* allocate struct */
185     if ((pp = (PICTURE *)malloc(sizeof(PICTURE))) == NULL)
186     return(NULL); /* serious error -- should exit? */
187     pp->fp = fp; pp->fmt[0] = '\0'; pp->pa = 1.;
188     /* load header */
189 schorsch 3.7 if (getheader(fp, headline, pp) < 0) {
190 gwlarson 3.3 closepicture(pp);
191 gwlarson 3.1 return(NULL);
192     }
193 gwlarson 3.3 if (!pp->fmt[0]) /* assume RGBE if unspecified */
194     strcpy(pp->fmt, COLRFMT);
195     if (!globmatch(PICFMT, pp->fmt) || !fgetsresolu(&pp->rs, fp)) {
196     closepicture(pp); /* failed test -- close file */
197     return(NULL);
198     }
199 gwlarson 3.1 rewind(fp); /* passed test -- rewind file */
200 gwlarson 3.3 return(pp);
201 gwlarson 3.1 }
202    
203    
204 schorsch 3.8 static int
205     tmap_picture( /* tone map Radiance picture */
206     char *fname,
207     register PICTURE *pp
208     )
209 gwlarson 3.1 {
210     uint16 orient;
211     int xsiz, ysiz;
212     BYTE *pix;
213     /* read and tone map picture */
214     if (tmMapPicture(&pix, &xsiz, &ysiz, flags,
215 gwlarson 3.3 rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK)
216 gwlarson 3.2 return(-1);
217 gwlarson 3.3 /* figure out TIFF orientation */
218     for (orient = 8; --orient; )
219 greg 3.4 if (ortab[orient] == pp->rs.rt)
220 gwlarson 3.3 break;
221 gwlarson 3.1 orient++;
222     /* put out our image */
223 gwlarson 3.2 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
224 gwlarson 3.3 72., 72./pp->pa, 2, pix) != 0)
225 gwlarson 3.2 return(-1);
226 gwlarson 3.1 /* free data and we're done */
227 greg 3.4 free((void *)pix);
228 gwlarson 3.2 return(0);
229 gwlarson 3.1 }
230    
231    
232 schorsch 3.8 static int
233     tmap_tiff( /* tone map SGILOG TIFF */
234     char *fname,
235     TIFF *tp
236     )
237 gwlarson 3.1 {
238     float xres, yres;
239     uint16 orient, resunit, phot;
240     int xsiz, ysiz;
241     BYTE *pix;
242     /* check to make sure it's SGILOG */
243     TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot);
244 schorsch 3.6 if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK))
245 gwlarson 3.1 flags |= TM_F_BW;
246     /* read and tone map TIFF */
247     if (tmMapTIFF(&pix, &xsiz, &ysiz, flags,
248     rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK)
249 gwlarson 3.2 return(-1);
250 gwlarson 3.1 /* get relevant tags */
251     TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit);
252     TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres);
253     TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres);
254     TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient);
255     /* put out our image */
256 gwlarson 3.2 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
257     xres, yres, resunit, pix) != 0)
258     return(-1);
259 gwlarson 3.1 /* free data and we're done */
260 greg 3.4 free((void *)pix);
261 gwlarson 3.2 return(0);
262 gwlarson 3.1 }
263    
264    
265 schorsch 3.8 static int
266     putimage( /* write out our image */
267     uint16 or,
268     uint32 xs,
269     uint32 ys,
270     float xr,
271     float yr,
272     uint16 ru,
273     BYTE *pd
274     )
275 gwlarson 3.1 {
276     register int y;
277     uint32 rowsperstrip;
278    
279     TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
280     if (flags & TM_F_BW) {
281     TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC,
282     PHOTOMETRIC_MINISBLACK);
283     TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 1);
284     } else {
285     TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
286     TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 3);
287     }
288     if (rgbp != stdprims) {
289     TIFFSetField(tifout, TIFFTAG_PRIMARYCHROMATICITIES,
290     (float *)rgbp);
291     TIFFSetField(tifout, TIFFTAG_WHITEPOINT, (float *)rgbp[WHT]);
292     }
293     TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, 8);
294     TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs);
295     TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys);
296     TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru);
297     TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr);
298     TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr);
299     TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
300     TIFFSetField(tifout, TIFFTAG_ORIENTATION, or);
301     /* compute good strip size */
302     rowsperstrip = 8192/TIFFScanlineSize(tifout);
303     if (rowsperstrip < 1) rowsperstrip = 1;
304     TIFFSetField(tifout, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
305     /* write out scanlines */
306     if (flags & TM_F_BW) {
307     for (y = 0; y < ys; y++)
308     if (TIFFWriteScanline(tifout, pd + y*xs, y, 0) < 0)
309     goto writerr;
310     } else {
311     for (y = 0; y < ys; y++)
312     if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0)
313     goto writerr;
314     }
315 gwlarson 3.2 return(0); /* all done! */
316 gwlarson 3.1 writerr:
317     fputs("Error writing TIFF output\n", stderr);
318 gwlarson 3.2 return(-1);
319 gwlarson 3.1 }