ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.7
Committed: Fri Jan 2 11:42:37 2004 UTC (20 years, 4 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.6: +18 -11 lines
Log Message:
Fixed typing/prototype of getheader() and its callback.

File Contents

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