ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.1
Committed: Mon Oct 26 17:05:28 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

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;
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 tmap_picture(argv[i], fin);
110 fclose(fin);
111 } else {
112 tmap_tiff(argv[i], tin);
113 TIFFClose(tin);
114 }
115 TIFFClose(tifout);
116 exit(0);
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 tmap_picture(fname, fp) /* tone map Radiance picture */
169 char *fname;
170 FILE *fp;
171 {
172 double pixrat;
173 int ord;
174 uint16 orient;
175 int xsiz, ysiz;
176 BYTE *pix;
177 /* read and tone map picture */
178 if (tmMapPicture(&pix, &xsiz, &ysiz, flags,
179 rgbp, gamv, lddyn, ldmax, fname, fp) != TM_E_OK)
180 exit(1);
181 /* get relevant header info. */
182 rewind(fp);
183 pixrat = 1.;
184 getheader(fp, getpixrat, &pixrat);
185 if ((ord = fgetresolu(&xsiz, &ysiz, fp)) < 0)
186 orient = 0;
187 else
188 for (orient = 8; --orient; )
189 if (ortab[orient] == ord)
190 break;
191 orient++;
192 /* put out our image */
193 putimage(orient, (uint32)xsiz, (uint32)ysiz, 72., 72./pixrat, 2, pix);
194 /* free data and we're done */
195 free((char *)pix);
196 }
197
198
199 tmap_tiff(fname, tp) /* tone map SGILOG TIFF */
200 char *fname;
201 TIFF *tp;
202 {
203 float xres, yres;
204 uint16 orient, resunit, phot;
205 int xsiz, ysiz;
206 BYTE *pix;
207 /* check to make sure it's SGILOG */
208 TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot);
209 if (phot != PHOTOMETRIC_LOGLUV && phot != PHOTOMETRIC_LOGL) {
210 fprintf(stderr, "%s: TIFF must be in SGILOG format\n", fname);
211 exit(1);
212 }
213 if (phot == PHOTOMETRIC_LOGL)
214 flags |= TM_F_BW;
215 /* read and tone map TIFF */
216 if (tmMapTIFF(&pix, &xsiz, &ysiz, flags,
217 rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK)
218 exit(1);
219 /* get relevant tags */
220 TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit);
221 TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres);
222 TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres);
223 TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient);
224 /* put out our image */
225 putimage(orient, (uint32)xsiz, (uint32)ysiz, xres, yres, resunit, pix);
226 /* free data and we're done */
227 free((char *)pix);
228 }
229
230
231 putimage(or, xs, ys, xr, yr, ru, pd) /* write out our image */
232 uint16 or;
233 uint32 xs, ys;
234 float xr, yr;
235 uint16 ru;
236 BYTE *pd;
237 {
238 register int y;
239 uint32 rowsperstrip;
240
241 TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
242 if (flags & TM_F_BW) {
243 TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC,
244 PHOTOMETRIC_MINISBLACK);
245 TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 1);
246 } else {
247 TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
248 TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 3);
249 }
250 if (rgbp != stdprims) {
251 TIFFSetField(tifout, TIFFTAG_PRIMARYCHROMATICITIES,
252 (float *)rgbp);
253 TIFFSetField(tifout, TIFFTAG_WHITEPOINT, (float *)rgbp[WHT]);
254 }
255 TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, 8);
256 TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs);
257 TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys);
258 TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru);
259 TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr);
260 TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr);
261 TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
262 TIFFSetField(tifout, TIFFTAG_ORIENTATION, or);
263 /* compute good strip size */
264 rowsperstrip = 8192/TIFFScanlineSize(tifout);
265 if (rowsperstrip < 1) rowsperstrip = 1;
266 TIFFSetField(tifout, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
267 /* write out scanlines */
268 if (flags & TM_F_BW) {
269 for (y = 0; y < ys; y++)
270 if (TIFFWriteScanline(tifout, pd + y*xs, y, 0) < 0)
271 goto writerr;
272 } else {
273 for (y = 0; y < ys; y++)
274 if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0)
275 goto writerr;
276 }
277 return; /* all done! */
278 writerr:
279 fputs("Error writing TIFF output\n", stderr);
280 exit(2);
281 }