ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.3
Committed: Tue Oct 27 08:54:37 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.2: +57 -40 lines
Log Message:
modified openpicture to store header information
changed getheader() to listen to return value of passed function

File Contents

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