ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.2
Committed: Mon Oct 26 17:30:50 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.1: +22 -12 lines
Log Message:
improved error reporting methods

File Contents

# Content
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 extern FILE *openpicture();
42
43
44 main(argc, argv)
45 int argc;
46 char *argv[];
47 {
48 FILE *fin = NULL;
49 TIFF *tin = NULL;
50 int i, rval;
51
52 for (i = 1; i < argc && argv[i][0] == '-'; i++)
53 switch (argv[i][1]) {
54 case 'h': /* human observer settings */
55 flags = TM_F_HUMAN;
56 break;
57 case 's': /* toggle human contrast */
58 flags ^= TM_F_HCONTR;
59 break;
60 case 'c': /* toggle mesopic sens. */
61 flags ^= TM_F_MESOPIC;
62 break;
63 case 'l': /* toggle linear mapping */
64 flags ^= TM_F_LINEAR;
65 break;
66 case 'b': /* toggle greyscale output */
67 flags ^= TM_F_BW;
68 break;
69 case 'g': /* set display gamma */
70 if (argc-i < 2) goto userr;
71 gamv = atof(argv[++i]);
72 break;
73 case 'u': /* set display maximum */
74 if (argc-i < 2) goto userr;
75 ldmax = atof(argv[++i]);
76 break;
77 case 'd': /* set display dynamic range */
78 if (argc-i < 2) goto userr;
79 lddyn = atof(argv[++i]);
80 break;
81 case 'p': /* set display primaries */
82 if (argc-i < 9) goto userr;
83 myprims[RED][CIEX] = atof(argv[++i]);
84 myprims[RED][CIEY] = atof(argv[++i]);
85 myprims[GRN][CIEX] = atof(argv[++i]);
86 myprims[GRN][CIEY] = atof(argv[++i]);
87 myprims[BLU][CIEX] = atof(argv[++i]);
88 myprims[BLU][CIEY] = atof(argv[++i]);
89 myprims[WHT][CIEX] = atof(argv[++i]);
90 myprims[WHT][CIEY] = atof(argv[++i]);
91 rgbp = myprims;
92 break;
93 default:
94 goto userr;
95 }
96 if (argc-i < 2) goto userr;
97 if ((fin = openpicture(argv[i])) == NULL &&
98 (tin = TIFFOpen(argv[i], "r")) == NULL) {
99 fprintf(stderr, "%s: cannot open or interpret file \"%s\"\n",
100 argv[0], argv[i]);
101 exit(1);
102 }
103 if ((tifout = TIFFOpen(argv[i+1], "w")) == NULL) {
104 fprintf(stderr, "%s: cannot open output TIFF \"%s\"\n",
105 argv[0], argv[i+1]);
106 exit(1);
107 }
108 if (fin != NULL) {
109 rval = tmap_picture(argv[i], fin);
110 fclose(fin);
111 } else {
112 rval = tmap_tiff(argv[i], tin);
113 TIFFClose(tin);
114 }
115 TIFFClose(tifout);
116 exit(rval==0 ? 0 : 1);
117 userr:
118 fprintf(stderr,
119 "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",
120 argv[0]);
121 exit(1);
122 }
123
124
125 FILE *
126 openpicture(fname) /* open/check Radiance picture file */
127 char *fname;
128 {
129 FILE *fp;
130 char inpfmt[32];
131 int xsiz, ysiz;
132 register char *cp;
133 /* check filename suffix */
134 if (fname == NULL) return(NULL);
135 for (cp = fname; *cp; cp++)
136 ;
137 while (cp > fname && cp[-1] != '.')
138 if (*--cp == '/') {
139 cp = fname;
140 break;
141 }
142 if (cp > fname && !strncmp(cp, "tif", 3))
143 return(NULL); /* assume it's a TIFF */
144 /* else try opening it */
145 if ((fp = fopen(fname, "r")) == NULL)
146 return(NULL);
147 /* check format */
148 strcpy(inpfmt, PICFMT);
149 if (checkheader(fp, inpfmt, NULL) < 0 ||
150 fgetresolu(&xsiz, &ysiz, fp) < 0) {
151 fclose(fp); /* failed test -- close file */
152 return(NULL);
153 }
154 rewind(fp); /* passed test -- rewind file */
155 return(fp);
156 }
157
158
159 getpixrat(s, pr) /* get pixel aspect ratio */
160 char *s;
161 double *pr;
162 {
163 if (isaspect(s))
164 *pr *= aspectval(s);
165 }
166
167
168 int
169 tmap_picture(fname, fp) /* tone map Radiance picture */
170 char *fname;
171 FILE *fp;
172 {
173 double pixrat;
174 int ord;
175 uint16 orient;
176 int xsiz, ysiz;
177 BYTE *pix;
178 /* read and tone map picture */
179 if (tmMapPicture(&pix, &xsiz, &ysiz, flags,
180 rgbp, gamv, lddyn, ldmax, fname, fp) != TM_E_OK)
181 return(-1);
182 /* get relevant header info. */
183 rewind(fp);
184 pixrat = 1.;
185 getheader(fp, getpixrat, &pixrat);
186 if ((ord = fgetresolu(&xsiz, &ysiz, fp)) < 0)
187 orient = 0;
188 else
189 for (orient = 8; --orient; )
190 if (ortab[orient] == ord)
191 break;
192 orient++;
193 /* put out our image */
194 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
195 72., 72./pixrat, 2, pix) != 0)
196 return(-1);
197 /* free data and we're done */
198 free((char *)pix);
199 return(0);
200 }
201
202
203 tmap_tiff(fname, tp) /* tone map SGILOG TIFF */
204 char *fname;
205 TIFF *tp;
206 {
207 float xres, yres;
208 uint16 orient, resunit, phot;
209 int xsiz, ysiz;
210 BYTE *pix;
211 /* check to make sure it's SGILOG */
212 TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot);
213 if (phot != PHOTOMETRIC_LOGLUV && phot != PHOTOMETRIC_LOGL) {
214 if (!(flags & TM_F_NOSTDERR)) {
215 fputs(fname, stderr);
216 fputs(": TIFF must be in SGILOG format\n", stderr);
217 }
218 return(-1);
219 }
220 if (phot == PHOTOMETRIC_LOGL)
221 flags |= TM_F_BW;
222 /* read and tone map TIFF */
223 if (tmMapTIFF(&pix, &xsiz, &ysiz, flags,
224 rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK)
225 return(-1);
226 /* get relevant tags */
227 TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit);
228 TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres);
229 TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres);
230 TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient);
231 /* put out our image */
232 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
233 xres, yres, resunit, pix) != 0)
234 return(-1);
235 /* free data and we're done */
236 free((char *)pix);
237 return(0);
238 }
239
240
241 putimage(or, xs, ys, xr, yr, ru, pd) /* write out our image */
242 uint16 or;
243 uint32 xs, ys;
244 float xr, yr;
245 uint16 ru;
246 BYTE *pd;
247 {
248 register int y;
249 uint32 rowsperstrip;
250
251 TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
252 if (flags & TM_F_BW) {
253 TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC,
254 PHOTOMETRIC_MINISBLACK);
255 TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 1);
256 } else {
257 TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
258 TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 3);
259 }
260 if (rgbp != stdprims) {
261 TIFFSetField(tifout, TIFFTAG_PRIMARYCHROMATICITIES,
262 (float *)rgbp);
263 TIFFSetField(tifout, TIFFTAG_WHITEPOINT, (float *)rgbp[WHT]);
264 }
265 TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, 8);
266 TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs);
267 TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys);
268 TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru);
269 TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr);
270 TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr);
271 TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
272 TIFFSetField(tifout, TIFFTAG_ORIENTATION, or);
273 /* compute good strip size */
274 rowsperstrip = 8192/TIFFScanlineSize(tifout);
275 if (rowsperstrip < 1) rowsperstrip = 1;
276 TIFFSetField(tifout, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
277 /* write out scanlines */
278 if (flags & TM_F_BW) {
279 for (y = 0; y < ys; y++)
280 if (TIFFWriteScanline(tifout, pd + y*xs, y, 0) < 0)
281 goto writerr;
282 } else {
283 for (y = 0; y < ys; y++)
284 if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0)
285 goto writerr;
286 }
287 return(0); /* all done! */
288 writerr:
289 fputs("Error writing TIFF output\n", stderr);
290 return(-1);
291 }