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, 3 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_tiff.c,v 2.39 2025/06/06 19:11:21 greg Exp $";
3 #endif
4 /*
5 * Program to convert between RADIANCE and TIFF files.
6 * Added LogLuv encodings 7/97 (GWL).
7 * Added white-balance adjustment 10/01 (GW).
8 */
9
10 #include <math.h>
11 #include <ctype.h>
12 #include "rtio.h"
13 #include "platform.h"
14 #include "tiffio.h"
15 #include "color.h"
16 #include "resolu.h"
17
18 #define GAMCOR 2.2 /* default gamma */
19
20 /* conversion flags */
21 #define C_CXFM 0x1 /* needs color transformation */
22 #define C_GAMUT 0x2 /* needs gamut mapping */
23 #define C_GAMMA 0x4 /* needs gamma correction */
24 #define C_GRY 0x8 /* TIFF is greyscale */
25 #define C_XYZE 0x10 /* Radiance is XYZE */
26 #define C_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
32 typedef void colcvf_t(uint32 y);
33
34 struct CONVST {
35 uint16 flags; /* conversion flags (defined above) */
36 char capdate[20]; /* capture date/time */
37 char owner[256]; /* content owner */
38 uint16 comp; /* TIFF compression type */
39 uint16 phot; /* TIFF photometric type */
40 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 double stonits; /* input conversion to nits */
45 float pixrat; /* pixel aspect ratio */
46 int ncc; /* # color components for spectral */
47 float wpt[4]; /* wavelength partitions */
48 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 void *p; /* generic pointer */
57 } r; /* Radiance scanline */
58 union {
59 uint8 *bp; /* byte pointer */
60 uint16 *wp; /* word pointer */
61 float *fp; /* float pointer */
62 void *p; /* generic pointer */
63 } t; /* TIFF scanline */
64 colcvf_t *tf; /* translation procedure */
65 } cvts = { /* conversion structure */
66 0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB,
67 PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., 3,
68 };
69
70 #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
75 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 static void ReadRadScan(struct CONVST *cvp);
87
88
89 #define RfGfBf2Color Luv2Color
90 #define Gryf2Color L2Color
91 #define Color2Gryf Color2L
92 #define Color2RfGfBf Color2Luv
93
94 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
105 #define pixorder() ortab[cvts.orient-1]
106
107 extern char TMSTR[]; /* "CAPDATE=" from header.c */
108 char OWNSTR[] = "OWNER=";
109
110
111 int
112 main(int argc, char *argv[])
113 {
114 int reverse = 0;
115 int i;
116 /* set global progname */
117 fixargv0(argv[0]);
118
119 for (i = 1; i < argc; i++)
120 if (argv[i][0] == '-')
121 switch (argv[i][1]) {
122 case 'g': /* gamma correction */
123 cvts.gamcor = atof(argv[++i]);
124 break;
125 case 'x': /* XYZE Radiance output */
126 TGL(C_XYZE);
127 break;
128 case 'z': /* LZW compressed output */
129 cvts.comp = COMPRESSION_LZW;
130 break;
131 case 'L': /* LogLuv 32-bit output */
132 cvts.comp = COMPRESSION_SGILOG;
133 cvts.phot = PHOTOMETRIC_LOGLUV;
134 break;
135 case 'l': /* LogLuv 24-bit output */
136 cvts.comp = COMPRESSION_SGILOG24;
137 cvts.phot = PHOTOMETRIC_LOGLUV;
138 break;
139 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 case 'b': /* greyscale output? */
146 TGL(C_GRY);
147 break;
148 case 'e': /* exposure adjustment */
149 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
150 goto userr;
151 cvts.bradj = atoi(argv[++i]);
152 break;
153 case 'r': /* reverse conversion? */
154 reverse = !reverse;
155 break;
156 case '\0':
157 goto doneopts;
158 default:
159 goto userr;
160 }
161 else
162 break;
163 doneopts:
164 if (reverse) {
165
166 if (i != argc-2 && i != argc-1)
167 goto userr;
168
169 tiff2ra(i, argv);
170
171 } else {
172
173 if (i != argc-2)
174 goto userr;
175 /* consistency checks */
176 if (CHK(C_GRY)) {
177 if (cvts.phot == PHOTOMETRIC_RGB)
178 cvts.phot = PHOTOMETRIC_MINISBLACK;
179 else {
180 cvts.phot = PHOTOMETRIC_LOGL;
181 cvts.comp = COMPRESSION_SGILOG;
182 }
183 }
184 if (CHK(C_TWRD|C_TFLT) == (C_TWRD|C_TFLT))
185 goto userr;
186
187 ra2tiff(i, argv);
188 }
189
190 exit(0);
191 userr:
192 fprintf(stderr,
193 "Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.hdr|-} out.tif\n",
194 progname);
195 fprintf(stderr,
196 " Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.hdr|-]\n",
197 progname);
198 exit(1);
199 }
200
201
202 static void
203 quiterr( /* print message and exit */
204 char *err
205 )
206 {
207 if (err != NULL) {
208 fprintf(stderr, "%s: %s\n", progname, err);
209 exit(1);
210 }
211 exit(0);
212 }
213
214
215 static void
216 allocbufs(void) /* allocate scanline buffers */
217 {
218 int rsiz, tsiz;
219
220 rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR);
221 tsiz = (CHK(C_TFLT) ? sizeof(float) :
222 CHK(C_TWRD) ? sizeof(uint16) : sizeof(uint8)) *
223 (CHK(C_GRY) ? 1 : 3);
224 cvts.r.p = malloc(rsiz*cvts.xmax);
225 cvts.t.p = malloc(tsiz*cvts.xmax);
226 if ((cvts.r.p == NULL) | (cvts.t.p == NULL))
227 quiterr("no memory to allocate scanline buffers");
228 }
229
230
231 static void
232 initfromtif(void) /* initialize conversion from TIFF input */
233 {
234 uint16 hi;
235 char *cp;
236 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 TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient);
264
265 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 SET(C_RFLT|C_TFLT);
271 if (!CHK(C_XYZE)) {
272 cpcolormat(cvts.cmat, xyz2rgbmat);
273 SET(C_CXFM|C_GAMUT);
274 } else if (cvts.comp == COMPRESSION_SGILOG)
275 SET(C_GAMUT); /* may be outside XYZ gamut */
276 if (cvts.pconf != PLANARCONFIG_CONTIG)
277 quiterr("cannot handle separate Luv planes");
278 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
279 SGILOGDATAFMT_FLOAT);
280 cvts.tf = Luv2Color;
281 break;
282 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 SGILOGDATAFMT_FLOAT);
287 cvts.tf = L2Color;
288 break;
289 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 case PHOTOMETRIC_RGB:
299 SET(C_GAMMA);
300 setcolrgam(cvts.gamcor);
301 if (CHK(C_XYZE)) {
302 comprgb2xyzWBmat(cvts.cmat,
303 CHK(C_PRIM) ? cvts.prims : stdprims);
304 SET(C_CXFM);
305 }
306 if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
307 hi != 3)
308 quiterr("unsupported samples per pixel for RGB");
309 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi))
310 hi = -1;
311 switch (hi) {
312 case 8:
313 cvts.tf = RGB2Colr;
314 break;
315 case 16:
316 cvts.tf = RRGGBB2Color;
317 SET(C_RFLT|C_TWRD);
318 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 }
326 break;
327 case PHOTOMETRIC_MINISBLACK:
328 SET(C_GRY|C_GAMMA);
329 setcolrgam(cvts.gamcor);
330 cvts.pconf = PLANARCONFIG_CONTIG;
331 if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
332 hi != 1)
333 quiterr("unsupported samples per pixel for greyscale");
334 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi))
335 hi = -1;
336 switch (hi) {
337 case 8:
338 cvts.tf = Gry2Colr;
339 break;
340 case 16:
341 cvts.tf = GGry2Color;
342 SET(C_RFLT|C_TWRD);
343 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 }
351 break;
352 default:
353 quiterr("unsupported photometric type");
354 break;
355 }
356
357 if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) ||
358 !TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax))
359 quiterr("unknown input image resolution");
360
361 if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits))
362 cvts.stonits = -1.;
363
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 /* 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 if (cvts.stonits > .0)
381 fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
382 fputformat(CIEFMT, cvts.rfp);
383 } else {
384 if (CHK(C_PRIM))
385 fputprims(cvts.prims, cvts.rfp);
386 if (cvts.stonits > .0)
387 fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
388 cvts.rfp);
389 fputformat(COLRFMT, cvts.rfp);
390 }
391 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
396 allocbufs(); /* allocate scanline buffers */
397 }
398
399
400 static void
401 tiff2ra( /* convert TIFF image to Radiance picture */
402 int ac,
403 char *av[]
404 )
405 {
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 SET_FILE_BINARY(cvts.rfp);
416 /* 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 static int
434 headline( /* process Radiance input header line */
435 char *s,
436 void *p
437 )
438 {
439 static int tmstrlen = 0;
440 static int ownstrlen = 0;
441 struct CONVST *cvp = (struct CONVST *)p;
442 char fmt[MAXFMTLEN];
443
444 if (!tmstrlen) {
445 tmstrlen = strlen(TMSTR);
446 ownstrlen = strlen(OWNSTR);
447 }
448 if (formatval(fmt, s)) {
449 CLR(C_XYZE|C_SPEC);
450 if (!strcmp(fmt, CIEFMT))
451 SET(C_XYZE);
452 else if (!strcmp(fmt, SPECFMT))
453 SET(C_SPEC);
454 else if (strcmp(fmt, COLRFMT))
455 return(-1);
456 return(1);
457 }
458 if (isexpos(s)) {
459 cvp->stonits /= exposval(s);
460 return(1);
461 }
462 if (isaspect(s)) {
463 cvp->pixrat *= aspectval(s);
464 return(1);
465 }
466 if (isprims(s)) {
467 primsval(cvp->prims, s);
468 SET(C_PRIM);
469 return(1);
470 }
471 if (isdate(s)) {
472 if (s[tmstrlen] == ' ')
473 strncpy(cvp->capdate, s+tmstrlen+1, 19);
474 else
475 strncpy(cvp->capdate, s+tmstrlen, 19);
476 cvp->capdate[19] = '\0';
477 return(1);
478 }
479 if (!strncmp(s, OWNSTR, ownstrlen)) {
480 char *cp = s + ownstrlen;
481
482 while (isspace(*cp))
483 ++cp;
484 strncpy(cvp->owner, cp, sizeof(cvp->owner));
485 cvp->owner[sizeof(cvp->owner)-1] = '\0';
486 for (cp = cvp->owner; *cp; cp++)
487 ;
488 while (cp > cvp->owner && isspace(cp[-1]))
489 *--cp = '\0';
490 return(1);
491 }
492 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 return(0);
499 }
500
501
502 static void
503 initfromrad(void) /* initialize input from a Radiance picture */
504 {
505 int i1, i2, po;
506 /* read Radiance header */
507 memcpy(cvts.wpt, WLPART, sizeof(cvts.wpt));
508 if (getheader(cvts.rfp, headline, &cvts) < 0 ||
509 (po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
510 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 }
517 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 TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
526
527 switch (cvts.phot) {
528 case PHOTOMETRIC_LOGLUV:
529 SET(C_RFLT|C_TFLT);
530 CLR(C_GRY|C_TWRD);
531 if (!CHK(C_XYZE|C_SPEC)) {
532 comprgb2xyzWBmat(cvts.cmat,
533 CHK(C_PRIM) ? cvts.prims : stdprims);
534 SET(C_CXFM);
535 }
536 if (cvts.comp != COMPRESSION_SGILOG &&
537 cvts.comp != COMPRESSION_SGILOG24)
538 quiterr("internal error 2 in initfromrad");
539 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
540 SGILOGDATAFMT_FLOAT);
541 cvts.tf = Color2Luv;
542 break;
543 case PHOTOMETRIC_LOGL:
544 SET(C_GRY|C_RFLT|C_TFLT);
545 CLR(C_TWRD);
546 if (cvts.comp != COMPRESSION_SGILOG)
547 quiterr("internal error 3 in initfromrad");
548 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
549 SGILOGDATAFMT_FLOAT);
550 cvts.tf = Color2L;
551 break;
552 case PHOTOMETRIC_RGB:
553 SET(C_GAMMA|C_GAMUT);
554 CLR(C_GRY);
555 setcolrgam(cvts.gamcor);
556 if (CHK(C_TWRD|C_TFLT) ? CHK(C_XYZE|C_SPEC) : CHK(C_XYZE)) {
557 compxyz2rgbWBmat(cvts.cmat,
558 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 if (CHK(C_TWRD)) {
568 cvts.tf = Color2RRGGBB;
569 SET(C_RFLT);
570 } else if (CHK(C_TFLT)) {
571 TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
572 SAMPLEFORMAT_IEEEFP);
573 cvts.tf = Color2RfGfBf;
574 SET(C_RFLT);
575 CLR(C_GAMUT);
576 } else
577 cvts.tf = Colr2RGB;
578 break;
579 case PHOTOMETRIC_MINISBLACK:
580 SET(C_GRY|C_GAMMA|C_GAMUT);
581 setcolrgam(cvts.gamcor);
582 if (CHK(C_TWRD)) {
583 cvts.tf = Color2GGry;
584 SET(C_RFLT);
585 } else if (CHK(C_TFLT)) {
586 TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT,
587 SAMPLEFORMAT_IEEEFP);
588 cvts.tf = Color2Gryf;
589 SET(C_RFLT);
590 } else
591 cvts.tf = Colr2Gry;
592 break;
593 default:
594 quiterr("internal error 4 in initfromrad");
595 break;
596 }
597 /* 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 TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE,
602 CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8);
603 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 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 if (cvts.comp == COMPRESSION_NONE)
615 i1 = TIFFScanlineSize(cvts.tif);
616 else
617 i1 = 3*cvts.xmax; /* conservative guess */
618 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 static void
627 ra2tiff( /* convert Radiance picture to TIFF image */
628 int ac,
629 char *av[]
630 )
631 {
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 SET_FILE_BINARY(cvts.rfp);
639 /* 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 /* clean up */
648 TIFFClose(cvts.tif);
649 fclose(cvts.rfp);
650 }
651
652
653 static void
654 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 Luv2Color( /* read/convert/write Luv->COLOR scanline */
693 uint32 y
694 )
695 {
696 int x;
697
698 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
699 quiterr("internal error 1 in Luv2Color");
700
701 if (cvts.pconf != PLANARCONFIG_CONTIG)
702 quiterr("cannot handle separate 32-bit color planes");
703
704 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
705 quiterr("error reading TIFF input");
706 /* also works for float RGB */
707 for (x = cvts.xmax; x--; ) {
708 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 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 static void
731 RRGGBB2Color( /* read/convert/write RGB16->COLOR scanline */
732 uint32 y
733 )
734 {
735 int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
736 double d;
737 int x;
738
739 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT))
740 quiterr("internal error 1 in RRGGBB2Color");
741
742 if (cvts.pconf != PLANARCONFIG_CONTIG)
743 quiterr("cannot handle separate 16-bit color planes");
744
745 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 static void
774 L2Color( /* read/convert/write Lfloat->COLOR scanline */
775 uint32 y
776 )
777 {
778 float m = pow(2., (double)cvts.bradj);
779 int x;
780
781 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
782 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 /* also works for float greyscale */
787 for (x = cvts.xmax; x--; ) {
788 float f = cvts.t.fp[x];
789 if (cvts.bradj) f *= m;
790 setcolor(cvts.r.colors[x], f, f, f);
791 }
792 if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
793 quiterr("error writing Radiance picture");
794 }
795
796
797 static void
798 RGB2Colr( /* read/convert/write RGB->COLR scanline */
799 uint32 y
800 )
801 {
802 COLOR ctmp;
803 int x;
804
805 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY))
806 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 (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
821 goto readerr;
822 if (TIFFReadScanline(cvts.tif,
823 (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
824 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 return;
849 readerr:
850 quiterr("error reading TIFF input");
851 }
852
853
854 static void
855 Gry2Colr( /* read/convert/write G8->COLR scanline */
856 uint32 y
857 )
858 {
859 int x;
860
861 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
862 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 static void
882 GGry2Color( /* read/convert/write G16->COLOR scanline */
883 uint32 y
884 )
885 {
886 int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
887 double m;
888 double d;
889 int x;
890
891 if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD))
892 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 static void
913 Color2GGry( /* read/convert/write COLOR->G16 scanline */
914 uint32 y
915 )
916 {
917 int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
918 float m = pow(2.,(double)cvts.bradj);
919 int x;
920
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 ReadRadScan(&cvts);
925
926 for (x = cvts.xmax; x--; ) {
927 float f = m*( CHK(C_XYZE) ?
928 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 static void
947 Color2L( /* read/convert/write COLOR->Lfloat scanline */
948 uint32 y
949 )
950 {
951 float m = pow(2.,(double)cvts.bradj);
952 int x;
953
954 if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
955 quiterr("internal error 1 in Color2L");
956
957 ReadRadScan(&cvts);
958
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 static void
969 Color2Luv( /* read/convert/write COLOR->Luv scanline */
970 uint32 y
971 )
972 {
973 int x;
974
975 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT))
976 quiterr("internal error 1 in Color2Luv");
977
978 ReadRadScan(&cvts);
979
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 /* also works for float RGB */
990 for (x = cvts.xmax; x--; ) {
991 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 }
995
996 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
997 quiterr("error writing TIFF output");
998 }
999
1000
1001 static void
1002 Color2RRGGBB( /* read/convert/write COLOR->RGB16 scanline */
1003 uint32 y
1004 )
1005 {
1006 int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01);
1007 float m = pow(2.,(double)cvts.bradj);
1008 int x, i;
1009
1010 if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD))
1011 quiterr("internal error 1 in Color2RRGGBB");
1012
1013 ReadRadScan(&cvts);
1014
1015 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 clipgamut(cvts.r.colors[x], bright(cvts.r.colors[x]),
1021 CGAMUT_LOWER, cblack, cwhite);
1022 }
1023 for (i = 3; i--; ) {
1024 float f = m*colval(cvts.r.colors[x],i);
1025 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 }
1036
1037 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1038 quiterr("error writing TIFF output");
1039 }
1040
1041
1042 static void
1043 Colr2Gry( /* read/convert/write COLR->RGB scanline */
1044 uint32 y
1045 )
1046 {
1047 int x;
1048
1049 if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY)
1050 quiterr("internal error 1 in Colr2Gry");
1051
1052 if (fread2colrs(cvts.r.colrs, cvts.xmax, cvts.rfp,
1053 cvts.ncc, cvts.wpt) < 0)
1054 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 static void
1071 Colr2RGB( /* read/convert/write COLR->RGB scanline */
1072 uint32 y
1073 )
1074 {
1075 COLOR ctmp;
1076 int x;
1077
1078 if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY))
1079 quiterr("internal error 1 in Colr2RGB");
1080
1081 if (fread2colrs(cvts.r.colrs, cvts.xmax, cvts.rfp,
1082 cvts.ncc, cvts.wpt) < 0)
1083 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 }
1097 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 }
1104
1105 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
1106 quiterr("error writing TIFF output");
1107 }