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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: normtiff.c,v 3.12 2010/12/04 00:24:43 greg Exp $";
3 #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 #include <time.h>
11 #include <string.h>
12
13 #include "platform.h"
14 #include "tiffio.h"
15 #include "color.h"
16 #include "tonemap.h"
17 #include "tmaptiff.h"
18 #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 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
47 uint16 comp = COMPRESSION_NONE; /* TIFF compression mode */
48
49 #define closepicture(p) (fclose((p)->fp),free((void *)(p)))
50
51 static gethfunc headline;
52
53 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 uint16 ru, uby8 *pd);
59
60
61 int
62 main(
63 int argc,
64 char *argv[]
65 )
66 {
67 PICTURE *pin = NULL;
68 TIFF *tin = NULL;
69 int i, rval;
70
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 case 'z': /* LZW compression */
101 comp = COMPRESSION_LZW;
102 break;
103 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 if ((pin = openpicture(argv[i])) == NULL &&
120 (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 if (pin != NULL) {
131 rval = tmap_picture(argv[i], pin);
132 closepicture(pin);
133 } else {
134 rval = tmap_tiff(argv[i], tin);
135 TIFFClose(tin);
136 }
137 TIFFClose(tifout);
138 exit(rval==0 ? 0 : 1);
139 userr:
140 fprintf(stderr,
141 "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 argv[0]);
143 exit(1);
144 }
145
146
147 static int
148 headline( /* process line from header */
149 char *s,
150 void *pp
151 )
152 {
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 ((PICTURE *)pp)->pa *= aspectval(s);
160 else
161 formatval(((PICTURE *)pp)->fmt, s);
162 return(0);
163 }
164
165
166 static PICTURE *
167 openpicture( /* open/check Radiance picture file */
168 char *fname
169 )
170 {
171 FILE *fp;
172 register PICTURE *pp;
173 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 SET_FILE_BINARY(fp);
189 /* 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 if (getheader(fp, headline, pp) < 0) {
195 closepicture(pp);
196 return(NULL);
197 }
198 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 rewind(fp); /* passed test -- rewind file */
205 return(pp);
206 }
207
208
209 static int
210 tmap_picture( /* tone map Radiance picture */
211 char *fname,
212 register PICTURE *pp
213 )
214 {
215 uint16 orient;
216 double paspect = (pp->rs.rt & YMAJOR) ? pp->pa : 1./pp->pa;
217 int xsiz, ysiz;
218 uby8 *pix;
219 /* read and tone map picture */
220 if (tmMapPicture(&pix, &xsiz, &ysiz, flags,
221 rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK)
222 return(-1);
223 /* figure out TIFF orientation */
224 for (orient = 8; --orient; )
225 if (ortab[orient] == pp->rs.rt)
226 break;
227 orient++;
228 /* put out our image */
229 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
230 72., 72./paspect, 2, pix) != 0)
231 return(-1);
232 /* free data and we're done */
233 free((void *)pix);
234 return(0);
235 }
236
237
238 static int
239 tmap_tiff( /* tone map SGILOG TIFF */
240 char *fname,
241 TIFF *tp
242 )
243 {
244 float xres, yres;
245 uint16 orient, resunit, phot;
246 int xsiz, ysiz;
247 uby8 *pix;
248 /* check to make sure it's SGILOG */
249 TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot);
250 if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK))
251 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 return(-1);
256 /* 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 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
263 xres, yres, resunit, pix) != 0)
264 return(-1);
265 /* free data and we're done */
266 free((void *)pix);
267 return(0);
268 }
269
270
271 static int
272 putimage( /* write out our image */
273 uint16 or,
274 uint32 xs,
275 uint32 ys,
276 float xr,
277 float yr,
278 uint16 ru,
279 uby8 *pd
280 )
281 {
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 TIFFSetField(tifout, TIFFTAG_COMPRESSION, comp);
304 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 return(0); /* all done! */
323 writerr:
324 fputs("Error writing TIFF output\n", stderr);
325 return(-1);
326 }