ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/normtiff.c
Revision: 3.17
Committed: Wed Oct 2 15:58:56 2024 UTC (6 months, 4 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 3.16: +12 -11 lines
Log Message:
feat(normtiff): Added ability to ingest hyperspectral radiance pictures

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 greg 3.17 static const char RCSid[] = "$Id: normtiff.c,v 3.16 2021/04/07 21:13:52 greg Exp $";
3 gwlarson 3.1 #endif
4     /*
5     * Tone map SGILOG TIFF or Radiance picture and output 24-bit RGB TIFF
6     */
7    
8     #include <math.h>
9 schorsch 3.7
10 greg 3.15 #include "rtio.h"
11 greg 3.12 #include "platform.h"
12 gwlarson 3.1 #include "tiffio.h"
13     #include "color.h"
14     #include "tonemap.h"
15 schorsch 3.8 #include "tmaptiff.h"
16 gwlarson 3.1 #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 greg 3.16 double lddyn = 100.; /* display dynamic range */
25 gwlarson 3.1 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 gwlarson 3.3 typedef struct {
39     FILE *fp; /* file pointer */
40 greg 3.14 char fmt[MAXFMTLEN]; /* picture format */
41 gwlarson 3.3 double pa; /* pixel aspect ratio */
42     RESOLU rs; /* picture resolution */
43     } PICTURE;
44 gwlarson 3.1
45 greg 3.9 uint16 comp = COMPRESSION_NONE; /* TIFF compression mode */
46 gwlarson 3.1
47 greg 3.17 #define closepicture(p) (fclose((p)->fp),free(p))
48 gwlarson 3.3
49 schorsch 3.7 static gethfunc headline;
50    
51 greg 3.9 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 greg 3.13 uint16 ru, uby8 *pd);
57 schorsch 3.8
58 gwlarson 3.3
59 schorsch 3.7 int
60     main(
61     int argc,
62     char *argv[]
63     )
64 gwlarson 3.1 {
65 gwlarson 3.3 PICTURE *pin = NULL;
66 gwlarson 3.1 TIFF *tin = NULL;
67 gwlarson 3.2 int i, rval;
68 gwlarson 3.1
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 greg 3.9 case 'z': /* LZW compression */
99     comp = COMPRESSION_LZW;
100     break;
101 gwlarson 3.1 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 gwlarson 3.3 if ((pin = openpicture(argv[i])) == NULL &&
118 gwlarson 3.1 (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 gwlarson 3.3 if (pin != NULL) {
129     rval = tmap_picture(argv[i], pin);
130     closepicture(pin);
131 gwlarson 3.1 } else {
132 gwlarson 3.2 rval = tmap_tiff(argv[i], tin);
133 gwlarson 3.1 TIFFClose(tin);
134     }
135     TIFFClose(tifout);
136 gwlarson 3.2 exit(rval==0 ? 0 : 1);
137 gwlarson 3.1 userr:
138     fprintf(stderr,
139 greg 3.11 "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 gwlarson 3.1 argv[0]);
141     exit(1);
142     }
143    
144    
145 schorsch 3.7 static int
146     headline( /* process line from header */
147     char *s,
148     void *pp
149     )
150 gwlarson 3.3 {
151 greg 3.17 char *cp;
152 gwlarson 3.3
153     for (cp = s; *cp; cp++)
154     if (*cp & 0x80)
155     return(-1); /* non-ascii in header */
156     if (isaspect(s))
157 schorsch 3.7 ((PICTURE *)pp)->pa *= aspectval(s);
158 gwlarson 3.3 else
159 schorsch 3.7 formatval(((PICTURE *)pp)->fmt, s);
160 gwlarson 3.3 return(0);
161     }
162    
163    
164 schorsch 3.8 static PICTURE *
165     openpicture( /* open/check Radiance picture file */
166     char *fname
167     )
168 gwlarson 3.1 {
169     FILE *fp;
170 greg 3.17 PICTURE *pp;
171     char *cp;
172 gwlarson 3.1 /* 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 greg 3.17 if (cp > fname && !strncasecmp(cp, "tif", 3))
182 gwlarson 3.1 return(NULL); /* assume it's a TIFF */
183     /* else try opening it */
184     if ((fp = fopen(fname, "r")) == NULL)
185     return(NULL);
186 greg 3.12 SET_FILE_BINARY(fp);
187 gwlarson 3.3 /* 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 schorsch 3.7 if (getheader(fp, headline, pp) < 0) {
193 gwlarson 3.3 closepicture(pp);
194 gwlarson 3.1 return(NULL);
195     }
196 gwlarson 3.3 if (!pp->fmt[0]) /* assume RGBE if unspecified */
197     strcpy(pp->fmt, COLRFMT);
198 greg 3.17 if ((!globmatch(PICFMT, pp->fmt) && strcmp(SPECFMT, pp->fmt))
199     || !fgetsresolu(&pp->rs, fp)) {
200 gwlarson 3.3 closepicture(pp); /* failed test -- close file */
201     return(NULL);
202     }
203 gwlarson 3.1 rewind(fp); /* passed test -- rewind file */
204 gwlarson 3.3 return(pp);
205 gwlarson 3.1 }
206    
207    
208 schorsch 3.8 static int
209     tmap_picture( /* tone map Radiance picture */
210     char *fname,
211 greg 3.17 PICTURE *pp
212 schorsch 3.8 )
213 gwlarson 3.1 {
214     uint16 orient;
215 greg 3.10 double paspect = (pp->rs.rt & YMAJOR) ? pp->pa : 1./pp->pa;
216 gwlarson 3.1 int xsiz, ysiz;
217 greg 3.13 uby8 *pix;
218 gwlarson 3.1 /* read and tone map picture */
219     if (tmMapPicture(&pix, &xsiz, &ysiz, flags,
220 gwlarson 3.3 rgbp, gamv, lddyn, ldmax, fname, pp->fp) != TM_E_OK)
221 gwlarson 3.2 return(-1);
222 gwlarson 3.3 /* figure out TIFF orientation */
223     for (orient = 8; --orient; )
224 greg 3.4 if (ortab[orient] == pp->rs.rt)
225 gwlarson 3.3 break;
226 gwlarson 3.1 orient++;
227     /* put out our image */
228 gwlarson 3.2 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
229 greg 3.10 72., 72./paspect, 2, pix) != 0)
230 gwlarson 3.2 return(-1);
231 gwlarson 3.1 /* free data and we're done */
232 greg 3.17 free(pix);
233 gwlarson 3.2 return(0);
234 gwlarson 3.1 }
235    
236    
237 schorsch 3.8 static int
238     tmap_tiff( /* tone map SGILOG TIFF */
239     char *fname,
240     TIFF *tp
241     )
242 gwlarson 3.1 {
243     float xres, yres;
244     uint16 orient, resunit, phot;
245     int xsiz, ysiz;
246 greg 3.13 uby8 *pix;
247 gwlarson 3.1 /* check to make sure it's SGILOG */
248     TIFFGetFieldDefaulted(tp, TIFFTAG_PHOTOMETRIC, &phot);
249 schorsch 3.6 if ((phot == PHOTOMETRIC_LOGL) | (phot == PHOTOMETRIC_MINISBLACK))
250 gwlarson 3.1 flags |= TM_F_BW;
251     /* read and tone map TIFF */
252     if (tmMapTIFF(&pix, &xsiz, &ysiz, flags,
253     rgbp, gamv, lddyn, ldmax, fname, tp) != TM_E_OK)
254 gwlarson 3.2 return(-1);
255 gwlarson 3.1 /* get relevant tags */
256     TIFFGetFieldDefaulted(tp, TIFFTAG_RESOLUTIONUNIT, &resunit);
257     TIFFGetFieldDefaulted(tp, TIFFTAG_XRESOLUTION, &xres);
258     TIFFGetFieldDefaulted(tp, TIFFTAG_YRESOLUTION, &yres);
259     TIFFGetFieldDefaulted(tp, TIFFTAG_ORIENTATION, &orient);
260     /* put out our image */
261 gwlarson 3.2 if (putimage(orient, (uint32)xsiz, (uint32)ysiz,
262     xres, yres, resunit, pix) != 0)
263     return(-1);
264 gwlarson 3.1 /* free data and we're done */
265 greg 3.17 free(pix);
266 gwlarson 3.2 return(0);
267 gwlarson 3.1 }
268    
269    
270 schorsch 3.8 static int
271     putimage( /* write out our image */
272     uint16 or,
273     uint32 xs,
274     uint32 ys,
275     float xr,
276     float yr,
277 greg 3.9 uint16 ru,
278 greg 3.13 uby8 *pd
279 schorsch 3.8 )
280 gwlarson 3.1 {
281 greg 3.17 int y;
282 gwlarson 3.1 uint32 rowsperstrip;
283    
284     TIFFSetField(tifout, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
285     if (flags & TM_F_BW) {
286     TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC,
287     PHOTOMETRIC_MINISBLACK);
288     TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 1);
289     } else {
290     TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
291     TIFFSetField(tifout, TIFFTAG_SAMPLESPERPIXEL, 3);
292     }
293     if (rgbp != stdprims) {
294     TIFFSetField(tifout, TIFFTAG_PRIMARYCHROMATICITIES,
295     (float *)rgbp);
296     TIFFSetField(tifout, TIFFTAG_WHITEPOINT, (float *)rgbp[WHT]);
297     }
298     TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, 8);
299     TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, xs);
300     TIFFSetField(tifout, TIFFTAG_IMAGELENGTH, ys);
301     TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru);
302 greg 3.9 TIFFSetField(tifout, TIFFTAG_COMPRESSION, comp);
303 gwlarson 3.1 TIFFSetField(tifout, TIFFTAG_XRESOLUTION, xr);
304     TIFFSetField(tifout, TIFFTAG_YRESOLUTION, yr);
305     TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
306     TIFFSetField(tifout, TIFFTAG_ORIENTATION, or);
307     /* compute good strip size */
308     rowsperstrip = 8192/TIFFScanlineSize(tifout);
309     if (rowsperstrip < 1) rowsperstrip = 1;
310     TIFFSetField(tifout, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
311     /* write out scanlines */
312     if (flags & TM_F_BW) {
313     for (y = 0; y < ys; y++)
314     if (TIFFWriteScanline(tifout, pd + y*xs, y, 0) < 0)
315     goto writerr;
316     } else {
317     for (y = 0; y < ys; y++)
318     if (TIFFWriteScanline(tifout, pd + y*3*xs, y, 0) < 0)
319     goto writerr;
320     }
321 gwlarson 3.2 return(0); /* all done! */
322 gwlarson 3.1 writerr:
323     fputs("Error writing TIFF output\n", stderr);
324 gwlarson 3.2 return(-1);
325 gwlarson 3.1 }