ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.15
Committed: Fri Jul 19 17:37:56 2019 UTC (4 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 3.14: +2 -4 lines
Log Message:
Moved declarations and definitions for header.c from resolu.h to rtio.h

File Contents

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