ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.26
Committed: Sun Jul 27 22:12:03 2003 UTC (21 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.25: +6 -6 lines
Log Message:
Added grouping parens to reduce ambiguity warnings.

File Contents

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