ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.21
Committed: Tue Jul 1 19:04:08 2003 UTC (20 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.20: +162 -49 lines
Log Message:
Added -f and -w options to ra_tiff to produce float and 16-bit output

File Contents

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