ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.40
Committed: Sat Jun 7 05:09:46 2025 UTC (2 weeks, 4 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.39: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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