ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.38
Committed: Fri Oct 4 19:15:19 2024 UTC (7 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.37: +2 -2 lines
Log Message:
fix(ra_tiff): fixed logic for color matrix on spectral input

File Contents

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