ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.24
Committed: Wed Jul 16 01:32:53 2003 UTC (20 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.23: +2 -1 lines
Log Message:
Minor compile fixes

File Contents

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