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

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.27 static const char RCSid[] = "$Id: ra_tiff.c,v 2.26 2003/07/27 22:12:03 schorsch Exp $";
3 greg 1.1 #endif
4     /*
5 gregl 2.9 * Program to convert between RADIANCE and TIFF files.
6 greg 2.20 * Added LogLuv encodings 7/97 (GWL).
7     * Added white-balance adjustment 10/01 (GW).
8 greg 1.1 */
9    
10     #include <stdio.h>
11 greg 2.3 #include <math.h>
12 greg 2.22 #include <ctype.h>
13 greg 2.24 #include <time.h>
14 schorsch 2.27 #include <string.h>
15    
16 greg 1.1 #include "tiffio.h"
17     #include "color.h"
18 greg 1.6 #include "resolu.h"
19    
20 greg 2.8 #define GAMCOR 2.2 /* default gamma */
21    
22 gregl 2.9 /* conversion flags */
23     #define C_CXFM 0x1 /* needs color transformation */
24     #define C_GAMUT 0x2 /* needs gamut mapping */
25     #define C_GAMMA 0x4 /* needs gamma correction */
26     #define C_GRY 0x8 /* TIFF is greyscale */
27     #define C_XYZE 0x10 /* Radiance is XYZE */
28     #define C_RFLT 0x20 /* Radiance data is float */
29     #define C_TFLT 0x40 /* TIFF data is float */
30 gwlarson 2.19 #define C_TWRD 0x80 /* TIFF data is 16-bit */
31     #define C_PRIM 0x100 /* has assigned primaries */
32 greg 1.1
33 gregl 2.9 struct {
34     uint16 flags; /* conversion flags (defined above) */
35 greg 2.22 char capdate[20]; /* capture date/time */
36     char owner[256]; /* content owner */
37 gregl 2.9 uint16 comp; /* TIFF compression type */
38 gregl 2.10 uint16 phot; /* TIFF photometric type */
39 gregl 2.9 uint16 pconf; /* TIFF planar configuration */
40     float gamcor; /* gamma correction value */
41     short bradj; /* Radiance exposure adjustment (stops) */
42     uint16 orient; /* visual orientation (TIFF spec.) */
43 gregl 2.11 double stonits; /* input conversion to nits */
44 gregl 2.9 float pixrat; /* pixel aspect ratio */
45     FILE *rfp; /* Radiance stream pointer */
46     TIFF *tif; /* TIFF pointer */
47     uint32 xmax, ymax; /* image dimensions */
48     COLORMAT cmat; /* color transformation matrix */
49     RGBPRIMS prims; /* RGB primaries */
50     union {
51     COLR *colrs; /* 4-byte ???E pointer */
52     COLOR *colors; /* float array pointer */
53     char *p; /* generic pointer */
54     } r; /* Radiance scanline */
55     union {
56     uint8 *bp; /* byte pointer */
57 gwlarson 2.18 uint16 *wp; /* word pointer */
58 gregl 2.9 float *fp; /* float pointer */
59     char *p; /* generic pointer */
60     } t; /* TIFF scanline */
61 greg 2.21 void (*tf)(); /* translation procedure */
62 gregl 2.9 } cvts = { /* conversion structure */
63 greg 2.22 0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB,
64 gregl 2.10 PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1.,
65 gregl 2.9 };
66 greg 1.4
67 gregl 2.9 #define CHK(f) (cvts.flags & (f))
68     #define SET(f) (cvts.flags |= (f))
69     #define CLR(f) (cvts.flags &= ~(f))
70     #define TGL(f) (cvts.flags ^= (f))
71 greg 1.5
72 greg 2.21 void Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr();
73     void Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry();
74     void RRGGBB2Color(), GGry2Color(), Color2RRGGBB(), Color2GGry();
75    
76     #define RfGfBf2Color Luv2Color
77     #define Gryf2Color L2Color
78     #define Color2Gryf Color2L
79     #define Color2RfGfBf Color2Luv
80 greg 1.1
81 gregl 2.9 short ortab[8] = { /* orientation conversion table */
82     YMAJOR|YDECR,
83     YMAJOR|YDECR|XDECR,
84     YMAJOR|XDECR,
85     YMAJOR,
86     YDECR,
87     XDECR|YDECR,
88     XDECR,
89     0
90     };
91 greg 1.1
92 gregl 2.9 #define pixorder() ortab[cvts.orient-1]
93    
94 greg 2.22 extern char TMSTR[]; /* "CAPDATE=" from header.c */
95     char OWNSTR[] = "OWNER=";
96    
97 greg 1.1 char *progname;
98    
99 schorsch 2.27 static gethfunc headline;
100    
101 greg 1.1
102     main(argc, argv)
103     int argc;
104     char *argv[];
105     {
106     int reverse = 0;
107     int i;
108    
109     progname = argv[0];
110    
111     for (i = 1; i < argc; i++)
112     if (argv[i][0] == '-')
113     switch (argv[i][1]) {
114 gregl 2.9 case 'g': /* gamma correction */
115     cvts.gamcor = atof(argv[++i]);
116 greg 1.1 break;
117 gregl 2.9 case 'x': /* XYZE Radiance output */
118     TGL(C_XYZE);
119 greg 1.4 break;
120 gregl 2.9 case 'z': /* LZW compressed output */
121     cvts.comp = COMPRESSION_LZW;
122 greg 1.5 break;
123 gregl 2.9 case 'L': /* LogLuv 32-bit output */
124 gregl 2.10 cvts.comp = COMPRESSION_SGILOG;
125     cvts.phot = PHOTOMETRIC_LOGLUV;
126 gregl 2.9 break;
127     case 'l': /* LogLuv 24-bit output */
128 gregl 2.10 cvts.comp = COMPRESSION_SGILOG24;
129     cvts.phot = PHOTOMETRIC_LOGLUV;
130 gregl 2.9 break;
131 greg 2.21 case 'w': /* 16-bit/primary output? */
132     TGL(C_TWRD);
133     break;
134     case 'f': /* IEEE float output? */
135     TGL(C_TFLT);
136     break;
137 gregl 2.9 case 'b': /* greyscale output? */
138     TGL(C_GRY);
139     break;
140     case 'e': /* exposure adjustment */
141 greg 1.1 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
142     goto userr;
143 gregl 2.9 cvts.bradj = atoi(argv[++i]);
144 greg 1.1 break;
145 gregl 2.9 case 'r': /* reverse conversion? */
146 greg 1.1 reverse = !reverse;
147     break;
148     case '\0':
149     goto doneopts;
150     default:
151     goto userr;
152     }
153     else
154     break;
155     doneopts:
156 gregl 2.9 if (reverse) {
157 greg 1.1
158     if (i != argc-2 && i != argc-1)
159     goto userr;
160 gregl 2.9
161     tiff2ra(i, argv);
162    
163     } else {
164    
165 greg 1.1 if (i != argc-2)
166     goto userr;
167 greg 2.21 /* consistency checks */
168 schorsch 2.25 if (CHK(C_GRY)) {
169 gregl 2.10 if (cvts.phot == PHOTOMETRIC_RGB)
170     cvts.phot = PHOTOMETRIC_MINISBLACK;
171     else {
172     cvts.phot = PHOTOMETRIC_LOGL;
173     cvts.comp = COMPRESSION_SGILOG;
174     }
175 schorsch 2.25 }
176 greg 2.21 if (CHK(C_TWRD|C_TFLT) == (C_TWRD|C_TFLT))
177     goto userr;
178 gregl 2.9
179     ra2tiff(i, argv);
180     }
181    
182 greg 1.1 exit(0);
183     userr:
184 greg 1.4 fprintf(stderr,
185 greg 2.21 "Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n",
186 greg 1.1 progname);
187 gregl 2.9 fprintf(stderr,
188     " Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\n",
189     progname);
190 greg 1.1 exit(1);
191     }
192    
193    
194     quiterr(err) /* print message and exit */
195     char *err;
196     {
197     if (err != NULL) {
198     fprintf(stderr, "%s: %s\n", progname, err);
199     exit(1);
200     }
201     exit(0);
202     }
203    
204    
205 gregl 2.9 allocbufs() /* allocate scanline buffers */
206 greg 1.1 {
207 gregl 2.9 int rsiz, tsiz;
208    
209     rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR);
210 gwlarson 2.19 tsiz = (CHK(C_TFLT) ? sizeof(float) :
211     CHK(C_TWRD) ? sizeof(uint16) : sizeof(uint8)) *
212 gregl 2.9 (CHK(C_GRY) ? 1 : 3);
213     cvts.r.p = (char *)malloc(rsiz*cvts.xmax);
214     cvts.t.p = (char *)malloc(tsiz*cvts.xmax);
215 schorsch 2.26 if ((cvts.r.p == NULL) | (cvts.t.p == NULL))
216 gregl 2.9 quiterr("no memory to allocate scanline buffers");
217     }
218    
219    
220     initfromtif() /* initialize conversion from TIFF input */
221     {
222     uint16 hi;
223 greg 2.22 char *cp;
224 gregl 2.9 float *fa, f1, f2;
225    
226 greg 2.21 CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_TWRD|C_CXFM);
227 gregl 2.9
228     TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf);
229    
230     if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) {
231     cvts.prims[RED][CIEX] = fa[0];
232     cvts.prims[RED][CIEY] = fa[1];
233     cvts.prims[GRN][CIEX] = fa[2];
234     cvts.prims[GRN][CIEY] = fa[3];
235     cvts.prims[BLU][CIEX] = fa[4];
236     cvts.prims[BLU][CIEY] = fa[5];
237     cvts.prims[WHT][CIEX] = 1./3.;
238     cvts.prims[WHT][CIEY] = 1./3.;
239     if (TIFFGetField(cvts.tif, TIFFTAG_WHITEPOINT, &fa)) {
240     cvts.prims[WHT][CIEX] = fa[0];
241     cvts.prims[WHT][CIEY] = fa[1];
242     }
243     SET(C_PRIM);
244     }
245    
246     if (!TIFFGetField(cvts.tif, TIFFTAG_COMPRESSION, &cvts.comp))
247     cvts.comp = COMPRESSION_NONE;
248    
249     if (TIFFGetField(cvts.tif, TIFFTAG_XRESOLUTION, &f1) &&
250     TIFFGetField(cvts.tif, TIFFTAG_YRESOLUTION, &f2))
251     cvts.pixrat = f1/f2;
252    
253 gregl 2.10 TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient);
254 gregl 2.9
255 gregl 2.10 if (!TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PHOTOMETRIC, &cvts.phot))
256     quiterr("TIFF has unspecified photometric type");
257    
258     switch (cvts.phot) {
259     case PHOTOMETRIC_LOGLUV:
260 gregl 2.9 SET(C_RFLT|C_TFLT);
261     if (!CHK(C_XYZE)) {
262     cpcolormat(cvts.cmat, xyz2rgbmat);
263     SET(C_CXFM|C_GAMUT);
264 gregl 2.10 } else if (cvts.comp == COMPRESSION_SGILOG)
265 gregl 2.9 SET(C_GAMUT);
266     if (cvts.pconf != PLANARCONFIG_CONTIG)
267     quiterr("cannot handle separate Luv planes");
268 gregl 2.10 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
269 gregl 2.12 SGILOGDATAFMT_FLOAT);
270 gregl 2.9 cvts.tf = Luv2Color;
271     break;
272 gregl 2.10 case PHOTOMETRIC_LOGL:
273     SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT);
274     cvts.pconf = PLANARCONFIG_CONTIG;
275     TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
276 gregl 2.12 SGILOGDATAFMT_FLOAT);
277 gregl 2.10 cvts.tf = L2Color;
278     break;
279 gregl 2.11 case PHOTOMETRIC_YCBCR:
280     if (cvts.comp == COMPRESSION_JPEG &&
281     cvts.pconf == PLANARCONFIG_CONTIG) {
282     TIFFSetField(cvts.tif, TIFFTAG_JPEGCOLORMODE,
283     JPEGCOLORMODE_RGB);
284     cvts.phot = PHOTOMETRIC_RGB;
285     } else
286     quiterr("unsupported photometric type");
287     /* fall through */
288 gregl 2.10 case PHOTOMETRIC_RGB:
289 gwlarson 2.18 SET(C_GAMMA);
290 gregl 2.9 setcolrgam(cvts.gamcor);
291     if (CHK(C_XYZE)) {
292 greg 2.20 comprgb2xyzWBmat(cvts.cmat,
293 gregl 2.9 CHK(C_PRIM) ? cvts.prims : stdprims);
294     SET(C_CXFM);
295     }
296     if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
297 gregl 2.10 hi != 3)
298     quiterr("unsupported samples per pixel for RGB");
299 greg 2.21 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi))
300     hi = -1;
301     switch (hi) {
302     case 8:
303 gwlarson 2.18 cvts.tf = RGB2Colr;
304 greg 2.21 break;
305     case 16:
306 gwlarson 2.18 cvts.tf = RRGGBB2Color;
307 gwlarson 2.19 SET(C_RFLT|C_TWRD);
308 greg 2.21 break;
309     case 32:
310     cvts.tf = RfGfBf2Color;
311     SET(C_RFLT|C_TFLT);
312     break;
313     default:
314     quiterr("unsupported bits per sample for RGB");
315 gwlarson 2.18 }
316 gregl 2.9 break;
317 gregl 2.10 case PHOTOMETRIC_MINISBLACK:
318 gwlarson 2.18 SET(C_GRY|C_GAMMA);
319 gregl 2.15 setcolrgam(cvts.gamcor);
320 gregl 2.10 cvts.pconf = PLANARCONFIG_CONTIG;
321     if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
322     hi != 1)
323     quiterr("unsupported samples per pixel for greyscale");
324 greg 2.21 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi))
325     hi = -1;
326     switch (hi) {
327     case 8:
328 gwlarson 2.18 cvts.tf = Gry2Colr;
329 greg 2.21 break;
330     case 16:
331 gwlarson 2.18 cvts.tf = GGry2Color;
332 gwlarson 2.19 SET(C_RFLT|C_TWRD);
333 greg 2.21 break;
334     case 32:
335     cvts.tf = Gryf2Color;
336     SET(C_RFLT|C_TFLT);
337     break;
338     default:
339     quiterr("unsupported bits per sample for Gray");
340 gwlarson 2.18 }
341 gregl 2.10 break;
342     default:
343     quiterr("unsupported photometric type");
344     break;
345 gregl 2.9 }
346    
347     if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) ||
348     !TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax))
349 greg 1.1 quiterr("unknown input image resolution");
350 gregl 2.9
351     if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits))
352     cvts.stonits = 1.;
353 greg 2.22
354     if (!TIFFGetField(cvts.tif, TIFFTAG_DATETIME, &cp))
355     cvts.capdate[0] = '\0';
356     else {
357     strncpy(cvts.capdate, cp, 19);
358     cvts.capdate[19] = '\0';
359     }
360     if (!TIFFGetField(cvts.tif, TIFFTAG_ARTIST, &cp))
361     cvts.owner[0] = '\0';
362     else {
363     strncpy(cvts.owner, cp, sizeof(cvts.owner));
364     cvts.owner[sizeof(cvts.owner)-1] = '\0';
365     }
366 gregl 2.9 /* add to Radiance header */
367     if (cvts.pixrat < .99 || cvts.pixrat > 1.01)
368     fputaspect(cvts.pixrat, cvts.rfp);
369     if (CHK(C_XYZE)) {
370     fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
371     fputformat(CIEFMT, cvts.rfp);
372     } else {
373     if (CHK(C_PRIM))
374     fputprims(cvts.prims, cvts.rfp);
375     fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
376     cvts.rfp);
377     fputformat(COLRFMT, cvts.rfp);
378     }
379 greg 2.22 if (cvts.capdate[0])
380     fprintf(cvts.rfp, "%s %s\n", TMSTR, cvts.capdate);
381     if (cvts.owner[0])
382     fprintf(cvts.rfp, "%s %s\n", OWNSTR, cvts.owner);
383 gregl 2.9
384     allocbufs(); /* allocate scanline buffers */
385     }
386    
387    
388     tiff2ra(ac, av) /* convert TIFF image to Radiance picture */
389     int ac;
390     char *av[];
391     {
392     int32 y;
393     /* open TIFF input */
394     if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL)
395     quiterr("cannot open TIFF input");
396     /* open Radiance output */
397     if (av[ac+1] == NULL || !strcmp(av[ac+1], "-"))
398     cvts.rfp = stdout;
399     else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL)
400     quiterr("cannot open Radiance output picture");
401     /* start output header */
402     newheader("RADIANCE", cvts.rfp);
403     printargs(ac, av, cvts.rfp);
404    
405     initfromtif(); /* initialize conversion */
406    
407     fputc('\n', cvts.rfp); /* finish Radiance header */
408     fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp);
409    
410     for (y = 0; y < cvts.ymax; y++) /* convert image */
411     (*cvts.tf)(y);
412     /* clean up */
413     fclose(cvts.rfp);
414     TIFFClose(cvts.tif);
415     }
416    
417    
418 schorsch 2.27 static int
419     headline( /* process Radiance input header line */
420     char *s,
421     void *p
422     )
423 gregl 2.9 {
424 greg 2.22 static int tmstrlen = 0;
425     static int ownstrlen = 0;
426 gregl 2.9 char fmt[32];
427    
428 greg 2.22 if (!tmstrlen)
429     tmstrlen = strlen(TMSTR);
430     if (!ownstrlen)
431     ownstrlen = strlen(OWNSTR);
432 gregl 2.9 if (formatval(fmt, s)) {
433     if (!strcmp(fmt, COLRFMT))
434     CLR(C_XYZE);
435     else if (!strcmp(fmt, CIEFMT))
436     SET(C_XYZE);
437     else
438     quiterr("unrecognized input picture format");
439 gwlarson 2.17 return(1);
440 gregl 2.9 }
441     if (isexpos(s)) {
442     cvts.stonits /= exposval(s);
443 gwlarson 2.17 return(1);
444 gregl 2.9 }
445     if (isaspect(s)) {
446     cvts.pixrat *= aspectval(s);
447 gwlarson 2.17 return(1);
448 gregl 2.9 }
449     if (isprims(s)) {
450     primsval(cvts.prims, s);
451     SET(C_PRIM);
452 gwlarson 2.17 return(1);
453 gregl 2.9 }
454 greg 2.22 if (isdate(s)) {
455     if (s[tmstrlen] == ' ')
456     strncpy(cvts.capdate, s+tmstrlen+1, 19);
457     else
458     strncpy(cvts.capdate, s+tmstrlen, 19);
459     cvts.capdate[19] = '\0';
460     return(1);
461     }
462     if (!strncmp(s, OWNSTR, ownstrlen)) {
463     register char *cp = s + ownstrlen;
464    
465     while (isspace(*cp))
466     ++cp;
467     strncpy(cvts.owner, cp, sizeof(cvts.owner));
468     cvts.owner[sizeof(cvts.owner)-1] = '\0';
469     for (cp = cvts.owner; *cp; cp++)
470     ;
471     while (cp > cvts.owner && isspace(cp[-1]))
472     *--cp = '\0';
473     return(1);
474     }
475 gwlarson 2.17 return(0);
476 gregl 2.9 }
477    
478    
479     initfromrad() /* initialize input from a Radiance picture */
480     {
481     int i1, i2, po;
482     /* read Radiance header */
483 greg 2.21 CLR(C_RFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
484 greg 2.22 cvts.capdate[0] = '\0';
485     cvts.owner[0] = '\0';
486 gregl 2.9 cvts.stonits = 1.;
487     cvts.pixrat = 1.;
488     cvts.pconf = PLANARCONFIG_CONTIG;
489     getheader(cvts.rfp, headline, NULL);
490     if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
491     quiterr("bad Radiance picture");
492     cvts.xmax = i1; cvts.ymax = i2;
493     for (i1 = 0; i1 < 8; i1++) /* interpret orientation */
494     if (ortab[i1] == po) {
495     cvts.orient = i1 + 1;
496     break;
497 greg 1.1 }
498 gregl 2.9 if (i1 >= 8)
499     quiterr("internal error 1 in initfromrad");
500     if (!(po & YMAJOR))
501     cvts.pixrat = 1./cvts.pixrat;
502     if (!CHK(C_XYZE))
503     cvts.stonits *= WHTEFFICACY;
504     /* set up conversion */
505     TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp);
506 gregl 2.10 TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
507 gregl 2.9
508 gregl 2.10 switch (cvts.phot) {
509     case PHOTOMETRIC_LOGLUV:
510 gregl 2.9 SET(C_RFLT|C_TFLT);
511 greg 2.21 CLR(C_GRY|C_TWRD);
512 gregl 2.9 if (!CHK(C_XYZE)) {
513 greg 2.20 comprgb2xyzWBmat(cvts.cmat,
514     CHK(C_PRIM) ? cvts.prims : stdprims);
515 gregl 2.9 SET(C_CXFM);
516     }
517 gregl 2.10 if (cvts.comp != COMPRESSION_SGILOG &&
518     cvts.comp != COMPRESSION_SGILOG24)
519     quiterr("internal error 2 in initfromrad");
520     TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
521 gregl 2.12 SGILOGDATAFMT_FLOAT);
522 gregl 2.9 cvts.tf = Color2Luv;
523     break;
524 gregl 2.10 case PHOTOMETRIC_LOGL:
525     SET(C_GRY|C_RFLT|C_TFLT);
526 greg 2.21 CLR(C_TWRD);
527 gregl 2.10 if (cvts.comp != COMPRESSION_SGILOG)
528     quiterr("internal error 3 in initfromrad");
529     TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
530 gregl 2.12 SGILOGDATAFMT_FLOAT);
531 gregl 2.10 cvts.tf = Color2L;
532     break;
533     case PHOTOMETRIC_RGB:
534 gregl 2.9 SET(C_GAMMA|C_GAMUT);
535 gregl 2.10 CLR(C_GRY);
536 gregl 2.9 setcolrgam(cvts.gamcor);
537     if (CHK(C_XYZE)) {
538 greg 2.20 compxyz2rgbWBmat(cvts.cmat,
539 gregl 2.9 CHK(C_PRIM) ? cvts.prims : stdprims);
540     SET(C_CXFM);
541     }
542     if (CHK(C_PRIM)) {
543     TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES,
544     (float *)cvts.prims);
545     TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
546     (float *)cvts.prims[WHT]);
547     }
548 greg 2.21 if (CHK(C_TWRD)) {
549     cvts.tf = Color2RRGGBB;
550     SET(C_RFLT);
551     } else if (CHK(C_TFLT)) {
552 greg 2.23 TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
553     SAMPLEFORMAT_IEEEFP);
554 greg 2.21 cvts.tf = Color2RfGfBf;
555     SET(C_RFLT);
556     } else
557     cvts.tf = Colr2RGB;
558 gregl 2.9 break;
559 gregl 2.10 case PHOTOMETRIC_MINISBLACK:
560     SET(C_GRY|C_GAMMA|C_GAMUT);
561     setcolrgam(cvts.gamcor);
562 greg 2.21 if (CHK(C_TWRD)) {
563     cvts.tf = Color2GGry;
564     SET(C_RFLT);
565     } else if (CHK(C_TFLT)) {
566 greg 2.23 TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
567     SAMPLEFORMAT_IEEEFP);
568 greg 2.21 cvts.tf = Color2Gryf;
569     SET(C_RFLT);
570     } else
571     cvts.tf = Colr2Gry;
572 gregl 2.10 break;
573     default:
574     quiterr("internal error 4 in initfromrad");
575     break;
576 greg 1.1 }
577 gregl 2.9 /* set other TIFF fields */
578     TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
579     TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
580     TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
581 greg 2.21 TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE,
582     CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8);
583 gregl 2.9 TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
584     TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
585     TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
586     TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2);
587     TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
588     TIFFSetField(cvts.tif, TIFFTAG_STONITS,
589     cvts.stonits/pow(2.,(double)cvts.bradj));
590 greg 2.22 if (cvts.capdate[0])
591     TIFFSetField(cvts.tif, TIFFTAG_DATETIME, cvts.capdate);
592     if (cvts.owner[0])
593     TIFFSetField(cvts.tif, TIFFTAG_ARTIST, cvts.owner);
594 gregl 2.10 if (cvts.comp == COMPRESSION_NONE)
595     i1 = TIFFScanlineSize(cvts.tif);
596     else
597     i1 = 3*cvts.xmax; /* conservative guess */
598 gregl 2.9 i2 = 8192/i1; /* compute good strip size */
599     if (i2 < 1) i2 = 1;
600     TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2);
601    
602     allocbufs(); /* allocate scanline buffers */
603     }
604    
605    
606     ra2tiff(ac, av) /* convert Radiance picture to TIFF image */
607     int ac;
608     char *av[];
609     {
610     uint32 y;
611     /* open Radiance file */
612     if (!strcmp(av[ac], "-"))
613     cvts.rfp = stdin;
614     else if ((cvts.rfp = fopen(av[ac], "r")) == NULL)
615     quiterr("cannot open Radiance input picture");
616     /* open TIFF file */
617     if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
618     quiterr("cannot open TIFF output");
619    
620     initfromrad(); /* initialize conversion */
621    
622     for (y = 0; y < cvts.ymax; y++) /* convert image */
623     (*cvts.tf)(y);
624 greg 1.1 /* clean up */
625 gregl 2.9 TIFFClose(cvts.tif);
626     fclose(cvts.rfp);
627     }
628    
629    
630 greg 2.21 void
631 gregl 2.9 Luv2Color(y) /* read/convert/write Luv->COLOR scanline */
632     uint32 y;
633     {
634     register int x;
635    
636 gwlarson 2.19 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
637 gregl 2.9 quiterr("internal error 1 in Luv2Color");
638    
639     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
640     quiterr("error reading TIFF input");
641 greg 2.21 /* also works for float RGB */
642 gregl 2.9 for (x = cvts.xmax; x--; ) {
643 greg 2.21 setcolor(cvts.r.colors[x],
644     cvts.t.fp[3*x],
645     cvts.t.fp[3*x + 1],
646     cvts.t.fp[3*x + 2]);
647 gregl 2.9 if (CHK(C_CXFM))
648     colortrans(cvts.r.colors[x], cvts.cmat,
649     cvts.r.colors[x]);
650     if (CHK(C_GAMUT))
651     clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
652     CGAMUT_LOWER, cblack, cwhite);
653     }
654     if (cvts.bradj) {
655     double m = pow(2.,(double)cvts.bradj);
656     for (x = cvts.xmax; x--; )
657     scalecolor(cvts.r.colors[x], m);
658     }
659    
660     if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
661     quiterr("error writing Radiance picture");
662     }
663    
664    
665 greg 2.21 void
666 gwlarson 2.18 RRGGBB2Color(y) /* read/convert/write RGB16->COLOR scanline */
667     uint32 y;
668     {
669 schorsch 2.26 int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
670 gwlarson 2.18 register double d;
671     register int x;
672    
673 gwlarson 2.19 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT))
674 gwlarson 2.18 quiterr("internal error 1 in RRGGBB2Color");
675    
676     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
677     quiterr("error reading TIFF input");
678    
679     for (x = cvts.xmax; x--; ) {
680     d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16));
681     if (dogamma) d = pow(d, cvts.gamcor);
682     colval(cvts.r.colors[x],RED) = d;
683     d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16));
684     if (dogamma) d = pow(d, cvts.gamcor);
685     colval(cvts.r.colors[x],GRN) = d;
686     d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16));
687     if (dogamma) d = pow(d, cvts.gamcor);
688     colval(cvts.r.colors[x],BLU) = d;
689     if (CHK(C_CXFM))
690     colortrans(cvts.r.colors[x], cvts.cmat,
691     cvts.r.colors[x]);
692     if (CHK(C_GAMUT))
693     clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
694     CGAMUT_LOWER, cblack, cwhite);
695     }
696     if (cvts.bradj) {
697     d = pow(2.,(double)cvts.bradj);
698     for (x = cvts.xmax; x--; )
699     scalecolor(cvts.r.colors[x], d);
700     }
701    
702     if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
703     quiterr("error writing Radiance picture");
704     }
705    
706    
707 greg 2.21 void
708     L2Color(y) /* read/convert/write Lfloat->COLOR scanline */
709 gregl 2.9 uint32 y;
710     {
711 greg 2.21 float m = pow(2., (double)cvts.bradj);
712 gregl 2.9 register int x;
713    
714 gwlarson 2.19 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
715 gregl 2.9 quiterr("internal error 1 in L2Color");
716    
717     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
718     quiterr("error reading TIFF input");
719 greg 2.21 /* also works for float greyscale */
720     for (x = cvts.xmax; x--; ) {
721     register float f = cvts.t.fp[x];
722     if (cvts.bradj) f *= m;
723     setcolor(cvts.r.colors[x], f, f, f);
724     }
725 gregl 2.9 if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
726     quiterr("error writing Radiance picture");
727     }
728    
729    
730 greg 2.21 void
731 gregl 2.9 RGB2Colr(y) /* read/convert/write RGB->COLR scanline */
732     uint32 y;
733     {
734     COLOR ctmp;
735     register int x;
736    
737 gwlarson 2.19 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY))
738 gregl 2.9 quiterr("internal error 1 in RGB2Colr");
739    
740     if (cvts.pconf == PLANARCONFIG_CONTIG) {
741     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
742     goto readerr;
743     for (x = cvts.xmax; x--; ) {
744     cvts.r.colrs[x][RED] = cvts.t.bp[3*x];
745     cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1];
746     cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2];
747     }
748     } else {
749     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
750     goto readerr;
751     if (TIFFReadScanline(cvts.tif,
752 gregl 2.14 (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
753 gregl 2.9 goto readerr;
754     if (TIFFReadScanline(cvts.tif,
755 gregl 2.14 (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
756 gregl 2.9 goto readerr;
757     for (x = cvts.xmax; x--; ) {
758     cvts.r.colrs[x][RED] = cvts.t.bp[x];
759     cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x];
760     cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x];
761     }
762     }
763    
764     gambs_colrs(cvts.r.colrs, cvts.xmax);
765     if (CHK(C_CXFM))
766     for (x = cvts.xmax; x--; ) {
767     colr_color(ctmp, cvts.r.colrs[x]);
768     colortrans(ctmp, cvts.cmat, ctmp);
769     if (CHK(C_GAMUT)) /* !CHK(C_XYZE) */
770     clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER,
771     cblack, cwhite);
772     setcolr(cvts.r.colrs[x], colval(ctmp,RED),
773     colval(ctmp,GRN), colval(ctmp,BLU));
774     }
775     if (cvts.bradj)
776     shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
777    
778     if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
779     quiterr("error writing Radiance picture");
780 greg 1.1 return;
781     readerr:
782     quiterr("error reading TIFF input");
783     }
784    
785    
786 greg 2.21 void
787 gregl 2.9 Gry2Colr(y) /* read/convert/write G8->COLR scanline */
788     uint32 y;
789 greg 1.1 {
790     register int x;
791 gregl 2.9
792 gwlarson 2.19 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
793 gregl 2.9 quiterr("internal error 1 in Gry2Colr");
794    
795     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
796     quiterr("error reading TIFF input");
797    
798     for (x = cvts.xmax; x--; )
799     cvts.r.colrs[x][RED] =
800     cvts.r.colrs[x][GRN] =
801     cvts.r.colrs[x][BLU] = cvts.t.bp[x];
802    
803     gambs_colrs(cvts.r.colrs, cvts.xmax);
804     if (cvts.bradj)
805     shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
806    
807     if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
808     quiterr("error writing Radiance picture");
809     }
810    
811    
812 greg 2.21 void
813 gwlarson 2.18 GGry2Color(y) /* read/convert/write G16->COLOR scanline */
814     uint32 y;
815     {
816 schorsch 2.26 int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
817 gwlarson 2.18 double m;
818     register double d;
819     register int x;
820    
821 gwlarson 2.19 if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD))
822 gwlarson 2.18 quiterr("internal error 1 in GGry2Color");
823    
824     if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
825     quiterr("error reading TIFF input");
826    
827     if (cvts.bradj)
828     m = pow(2., (double)cvts.bradj);
829     for (x = cvts.xmax; x--; ) {
830     d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16));
831     if (dogamma) d = pow(d, cvts.gamcor);
832     if (cvts.bradj) d *= m;
833     colval(cvts.r.colors[x],RED) =
834     colval(cvts.r.colors[x],GRN) =
835     colval(cvts.r.colors[x],BLU) = d;
836     }
837     if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
838     quiterr("error writing Radiance picture");
839     }
840    
841    
842 greg 2.21 void
843     Color2GGry(y) /* read/convert/write COLOR->G16 scanline */
844     uint32 y;
845     {
846 schorsch 2.26 int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
847 greg 2.21 float m = pow(2.,(double)cvts.bradj);
848     register int x;
849    
850     if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD|C_GRY))
851     quiterr("internal error 1 in Color2GGry");
852    
853     if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
854     quiterr("error reading Radiance picture");
855    
856     for (x = cvts.xmax; x--; ) {
857     register float f = m*( CHK(C_XYZE) ?
858     colval(cvts.r.colors[x],CIEY)
859     : bright(cvts.r.colors[x]) );
860     if (f <= 0)
861     cvts.t.wp[x] = 0;
862     else if (f >= 1)
863     cvts.t.wp[x] = 0xffff;
864     else if (dogamma)
865     cvts.t.wp[x] = (int)((float)(1L<<16) *
866     pow(f, 1./cvts.gamcor));
867     else
868     cvts.t.wp[x] = (int)((float)(1L<<16) * f);
869     }
870    
871     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
872     quiterr("error writing TIFF output");
873     }
874    
875    
876     void
877     Color2L(y) /* read/convert/write COLOR->Lfloat scanline */
878 gregl 2.9 uint32 y;
879     {
880 greg 2.21 float m = pow(2.,(double)cvts.bradj);
881 gregl 2.9 register int x;
882    
883 gwlarson 2.19 if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
884 gregl 2.9 quiterr("internal error 1 in Color2L");
885    
886     if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
887     quiterr("error reading Radiance picture");
888    
889     for (x = cvts.xmax; x--; )
890     cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY)
891     : bright(cvts.r.colors[x]) );
892    
893     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
894     quiterr("error writing TIFF output");
895     }
896    
897    
898 greg 2.21 void
899 gregl 2.9 Color2Luv(y) /* read/convert/write COLOR->Luv scanline */
900     uint32 y;
901     {
902     register int x;
903    
904 gwlarson 2.19 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
905 gregl 2.9 quiterr("internal error 1 in Color2Luv");
906    
907     if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
908     quiterr("error reading Radiance picture");
909    
910     if (CHK(C_CXFM))
911     for (x = cvts.xmax; x--; )
912     colortrans(cvts.r.colors[x], cvts.cmat,
913     cvts.r.colors[x]);
914     if (cvts.bradj) {
915     double m = pow(2.,(double)cvts.bradj);
916     for (x = cvts.xmax; x--; )
917     scalecolor(cvts.r.colors[x], m);
918     }
919 greg 2.21 /* also works for float RGB */
920 gregl 2.9 for (x = cvts.xmax; x--; ) {
921 gwlarson 2.16 cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX);
922     cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY);
923     cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ);
924 gregl 2.9 }
925    
926     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
927     quiterr("error writing TIFF output");
928     }
929    
930    
931 greg 2.21 void
932     Color2RRGGBB(y) /* read/convert/write COLOR->RGB16 scanline */
933     uint32 y;
934     {
935 schorsch 2.26 int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
936 greg 2.21 float m = pow(2.,(double)cvts.bradj);
937     register int x, i;
938    
939     if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD))
940     quiterr("internal error 1 in Color2RRGGBB");
941    
942     if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
943     quiterr("error reading Radiance picture");
944    
945     for (x = cvts.xmax; x--; )
946     for (i = 3; i--; ) {
947     register float f = m*colval(cvts.r.colors[x],i);
948     if (f <= 0)
949     cvts.t.wp[3*x + i] = 0;
950     else if (f >= 1)
951     cvts.t.wp[3*x + i] = 0xffff;
952     else if (dogamma)
953     cvts.t.wp[3*x + i] = (int)((float)(1L<<16) *
954     pow(f, 1./cvts.gamcor));
955     else
956     cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f);
957     }
958    
959     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
960     quiterr("error writing TIFF output");
961     }
962    
963    
964     void
965 gregl 2.9 Colr2Gry(y) /* read/convert/write COLR->RGB scanline */
966     uint32 y;
967     {
968     register int x;
969    
970 gwlarson 2.19 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
971 gregl 2.9 quiterr("internal error 1 in Colr2Gry");
972    
973     if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
974     quiterr("error reading Radiance picture");
975    
976     if (cvts.bradj)
977     shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
978     for (x = cvts.xmax; x--; )
979     colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]);
980     colrs_gambs(cvts.r.colrs, cvts.xmax);
981    
982     for (x = cvts.xmax; x--; )
983     cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY);
984    
985     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
986     quiterr("error writing TIFF output");
987     }
988    
989    
990 greg 2.21 void
991 gregl 2.9 Colr2RGB(y) /* read/convert/write COLR->RGB scanline */
992     uint32 y;
993     {
994     COLOR ctmp;
995     register int x;
996    
997 gwlarson 2.19 if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY))
998 gregl 2.9 quiterr("internal error 1 in Colr2RGB");
999    
1000     if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
1001     quiterr("error reading Radiance picture");
1002    
1003     if (cvts.bradj)
1004     shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
1005     if (CHK(C_CXFM))
1006     for (x = cvts.xmax; x--; ) {
1007     colr_color(ctmp, cvts.r.colrs[x]);
1008     colortrans(ctmp, cvts.cmat, ctmp);
1009     if (CHK(C_GAMUT))
1010     clipgamut(ctmp, bright(ctmp), CGAMUT,
1011     cblack, cwhite);
1012     setcolr(cvts.r.colrs[x], colval(ctmp,RED),
1013     colval(ctmp,GRN), colval(ctmp,BLU));
1014 greg 1.1 }
1015 gregl 2.9 colrs_gambs(cvts.r.colrs, cvts.xmax);
1016    
1017     for (x = cvts.xmax; x--; ) {
1018     cvts.t.bp[3*x] = cvts.r.colrs[x][RED];
1019     cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN];
1020     cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU];
1021 greg 1.1 }
1022 gregl 2.9
1023     if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1024     quiterr("error writing TIFF output");
1025 greg 1.1 }