ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.39
Committed: Fri Jun 6 19:11:21 2025 UTC (4 hours, 56 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.38: +4 -6 lines
Log Message:
refactor: Making use of printargs() more consistent with fixargv0()

File Contents

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