ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.10
Committed: Thu Jul 31 21:26:45 1997 UTC (26 years, 9 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.9: +80 -64 lines
Log Message:
modified TIFF library with new photometric types

File Contents

# Content
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Program to convert between RADIANCE and TIFF files.
9 * Added experimental LogLuv encodings 7/97 (GWL).
10 */
11
12 #include <stdio.h>
13 #include <math.h>
14 #include "tiffio.h"
15 #include "color.h"
16 #include "resolu.h"
17
18 #define GAMCOR 2.2 /* default gamma */
19
20 #ifndef malloc
21 extern char *malloc();
22 #endif
23 /* conversion flags */
24 #define C_CXFM 0x1 /* needs color transformation */
25 #define C_GAMUT 0x2 /* needs gamut mapping */
26 #define C_GAMMA 0x4 /* needs gamma correction */
27 #define C_GRY 0x8 /* TIFF is greyscale */
28 #define C_XYZE 0x10 /* Radiance is XYZE */
29 #define C_RFLT 0x20 /* Radiance data is float */
30 #define C_TFLT 0x40 /* TIFF data is float */
31 #define C_PRIM 0x80 /* has assigned primaries */
32
33 struct {
34 uint16 flags; /* conversion flags (defined above) */
35 uint16 comp; /* TIFF compression type */
36 uint16 phot; /* TIFF photometric type */
37 uint16 pconf; /* TIFF planar configuration */
38 float gamcor; /* gamma correction value */
39 short bradj; /* Radiance exposure adjustment (stops) */
40 uint16 orient; /* visual orientation (TIFF spec.) */
41 float stonits; /* input conversion to nits */
42 float pixrat; /* pixel aspect ratio */
43 FILE *rfp; /* Radiance stream pointer */
44 TIFF *tif; /* TIFF pointer */
45 uint32 xmax, ymax; /* image dimensions */
46 COLORMAT cmat; /* color transformation matrix */
47 RGBPRIMS prims; /* RGB primaries */
48 union {
49 COLR *colrs; /* 4-byte ???E pointer */
50 COLOR *colors; /* float array pointer */
51 char *p; /* generic pointer */
52 } r; /* Radiance scanline */
53 union {
54 uint8 *bp; /* byte pointer */
55 float *fp; /* float pointer */
56 char *p; /* generic pointer */
57 } t; /* TIFF scanline */
58 int (*tf)(); /* translation procedure */
59 } cvts = { /* conversion structure */
60 0, COMPRESSION_NONE, PHOTOMETRIC_RGB,
61 PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1.,
62 };
63
64 #define CHK(f) (cvts.flags & (f))
65 #define SET(f) (cvts.flags |= (f))
66 #define CLR(f) (cvts.flags &= ~(f))
67 #define TGL(f) (cvts.flags ^= (f))
68
69 int Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr();
70 int Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry();
71
72 short ortab[8] = { /* orientation conversion table */
73 YMAJOR|YDECR,
74 YMAJOR|YDECR|XDECR,
75 YMAJOR|XDECR,
76 YMAJOR,
77 YDECR,
78 XDECR|YDECR,
79 XDECR,
80 0
81 };
82
83 #define pixorder() ortab[cvts.orient-1]
84
85 char *progname;
86
87
88 main(argc, argv)
89 int argc;
90 char *argv[];
91 {
92 int reverse = 0;
93 int i;
94
95 progname = argv[0];
96
97 for (i = 1; i < argc; i++)
98 if (argv[i][0] == '-')
99 switch (argv[i][1]) {
100 case 'g': /* gamma correction */
101 cvts.gamcor = atof(argv[++i]);
102 break;
103 case 'x': /* XYZE Radiance output */
104 TGL(C_XYZE);
105 break;
106 case 'z': /* LZW compressed output */
107 cvts.comp = COMPRESSION_LZW;
108 break;
109 case 'L': /* LogLuv 32-bit output */
110 cvts.comp = COMPRESSION_SGILOG;
111 cvts.phot = PHOTOMETRIC_LOGLUV;
112 break;
113 case 'l': /* LogLuv 24-bit output */
114 cvts.comp = COMPRESSION_SGILOG24;
115 cvts.phot = PHOTOMETRIC_LOGLUV;
116 break;
117 case 'b': /* greyscale output? */
118 TGL(C_GRY);
119 break;
120 case 'e': /* exposure adjustment */
121 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
122 goto userr;
123 cvts.bradj = atoi(argv[++i]);
124 break;
125 case 'r': /* reverse conversion? */
126 reverse = !reverse;
127 break;
128 case '\0':
129 goto doneopts;
130 default:
131 goto userr;
132 }
133 else
134 break;
135 doneopts:
136 if (reverse) {
137
138 if (i != argc-2 && i != argc-1)
139 goto userr;
140
141 tiff2ra(i, argv);
142
143 } else {
144
145 if (i != argc-2)
146 goto userr;
147
148 if (CHK(C_GRY)) /* consistency corrections */
149 if (cvts.phot == PHOTOMETRIC_RGB)
150 cvts.phot = PHOTOMETRIC_MINISBLACK;
151 else {
152 cvts.phot = PHOTOMETRIC_LOGL;
153 cvts.comp = COMPRESSION_SGILOG;
154 }
155
156 ra2tiff(i, argv);
157 }
158
159 exit(0);
160 userr:
161 fprintf(stderr,
162 "Usage: %s [-z|-L|-l][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n",
163 progname);
164 fprintf(stderr,
165 " Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\n",
166 progname);
167 exit(1);
168 }
169
170
171 quiterr(err) /* print message and exit */
172 char *err;
173 {
174 if (err != NULL) {
175 fprintf(stderr, "%s: %s\n", progname, err);
176 exit(1);
177 }
178 exit(0);
179 }
180
181
182 allocbufs() /* allocate scanline buffers */
183 {
184 int rsiz, tsiz;
185
186 rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR);
187 tsiz = (CHK(C_TFLT) ? sizeof(float) : sizeof(uint8)) *
188 (CHK(C_GRY) ? 1 : 3);
189 cvts.r.p = (char *)malloc(rsiz*cvts.xmax);
190 cvts.t.p = (char *)malloc(tsiz*cvts.xmax);
191 if (cvts.r.p == NULL | cvts.t.p == NULL)
192 quiterr("no memory to allocate scanline buffers");
193 }
194
195
196 initfromtif() /* initialize conversion from TIFF input */
197 {
198 uint16 hi;
199 float *fa, f1, f2;
200
201 CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_CXFM);
202
203 TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf);
204
205 if (TIFFGetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES, &fa)) {
206 cvts.prims[RED][CIEX] = fa[0];
207 cvts.prims[RED][CIEY] = fa[1];
208 cvts.prims[GRN][CIEX] = fa[2];
209 cvts.prims[GRN][CIEY] = fa[3];
210 cvts.prims[BLU][CIEX] = fa[4];
211 cvts.prims[BLU][CIEY] = fa[5];
212 cvts.prims[WHT][CIEX] = 1./3.;
213 cvts.prims[WHT][CIEY] = 1./3.;
214 if (TIFFGetField(cvts.tif, TIFFTAG_WHITEPOINT, &fa)) {
215 cvts.prims[WHT][CIEX] = fa[0];
216 cvts.prims[WHT][CIEY] = fa[1];
217 }
218 SET(C_PRIM);
219 }
220
221 if (!TIFFGetField(cvts.tif, TIFFTAG_COMPRESSION, &cvts.comp))
222 cvts.comp = COMPRESSION_NONE;
223
224 if (TIFFGetField(cvts.tif, TIFFTAG_XRESOLUTION, &f1) &&
225 TIFFGetField(cvts.tif, TIFFTAG_YRESOLUTION, &f2))
226 cvts.pixrat = f1/f2;
227
228 TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_ORIENTATION, &cvts.orient);
229
230 if (!TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PHOTOMETRIC, &cvts.phot))
231 quiterr("TIFF has unspecified photometric type");
232
233 switch (cvts.phot) {
234 case PHOTOMETRIC_LOGLUV:
235 SET(C_RFLT|C_TFLT);
236 if (!CHK(C_XYZE)) {
237 cpcolormat(cvts.cmat, xyz2rgbmat);
238 SET(C_CXFM|C_GAMUT);
239 } else if (cvts.comp == COMPRESSION_SGILOG)
240 SET(C_GAMUT);
241 if (cvts.pconf != PLANARCONFIG_CONTIG)
242 quiterr("cannot handle separate Luv planes");
243 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
244 SGILOGDATAFMT_FLTXYZ);
245 cvts.tf = Luv2Color;
246 break;
247 case PHOTOMETRIC_LOGL:
248 SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT);
249 cvts.pconf = PLANARCONFIG_CONTIG;
250 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
251 SGILOGDATAFMT_FLTY);
252 cvts.tf = L2Color;
253 break;
254 case PHOTOMETRIC_RGB:
255 SET(C_GAMMA|C_GAMUT);
256 setcolrgam(cvts.gamcor);
257 if (CHK(C_XYZE)) {
258 comprgb2xyzmat(cvts.cmat,
259 CHK(C_PRIM) ? cvts.prims : stdprims);
260 SET(C_CXFM);
261 }
262 if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
263 hi != 3)
264 quiterr("unsupported samples per pixel for RGB");
265 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
266 hi != 8)
267 quiterr("unsupported bits per sample for RGB");
268 cvts.tf = RGB2Colr;
269 break;
270 case PHOTOMETRIC_MINISBLACK:
271 SET(C_GRY|C_GAMMA|C_GAMUT);
272 cvts.pconf = PLANARCONFIG_CONTIG;
273 if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
274 hi != 1)
275 quiterr("unsupported samples per pixel for greyscale");
276 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
277 hi != 8)
278 quiterr("unsupported bits per sample for greyscale");
279 cvts.tf = Gry2Colr;
280 break;
281 default:
282 quiterr("unsupported photometric type");
283 break;
284 }
285
286 if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) ||
287 !TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax))
288 quiterr("unknown input image resolution");
289
290 if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits))
291 cvts.stonits = 1.;
292 /* add to Radiance header */
293 if (cvts.pixrat < .99 || cvts.pixrat > 1.01)
294 fputaspect(cvts.pixrat, cvts.rfp);
295 if (CHK(C_XYZE)) {
296 fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
297 fputformat(CIEFMT, cvts.rfp);
298 } else {
299 if (CHK(C_PRIM))
300 fputprims(cvts.prims, cvts.rfp);
301 fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
302 cvts.rfp);
303 fputformat(COLRFMT, cvts.rfp);
304 }
305
306 allocbufs(); /* allocate scanline buffers */
307 }
308
309
310 tiff2ra(ac, av) /* convert TIFF image to Radiance picture */
311 int ac;
312 char *av[];
313 {
314 int32 y;
315 /* open TIFF input */
316 if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL)
317 quiterr("cannot open TIFF input");
318 /* open Radiance output */
319 if (av[ac+1] == NULL || !strcmp(av[ac+1], "-"))
320 cvts.rfp = stdout;
321 else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL)
322 quiterr("cannot open Radiance output picture");
323 /* start output header */
324 newheader("RADIANCE", cvts.rfp);
325 printargs(ac, av, cvts.rfp);
326
327 initfromtif(); /* initialize conversion */
328
329 fputc('\n', cvts.rfp); /* finish Radiance header */
330 fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp);
331
332 for (y = 0; y < cvts.ymax; y++) /* convert image */
333 (*cvts.tf)(y);
334 /* clean up */
335 fclose(cvts.rfp);
336 TIFFClose(cvts.tif);
337 }
338
339
340 int
341 headline(s) /* process Radiance input header line */
342 char *s;
343 {
344 char fmt[32];
345
346 if (formatval(fmt, s)) {
347 if (!strcmp(fmt, COLRFMT))
348 CLR(C_XYZE);
349 else if (!strcmp(fmt, CIEFMT))
350 SET(C_XYZE);
351 else
352 quiterr("unrecognized input picture format");
353 return;
354 }
355 if (isexpos(s)) {
356 cvts.stonits /= exposval(s);
357 return;
358 }
359 if (isaspect(s)) {
360 cvts.pixrat *= aspectval(s);
361 return;
362 }
363 if (isprims(s)) {
364 primsval(cvts.prims, s);
365 SET(C_PRIM);
366 return;
367 }
368 }
369
370
371 initfromrad() /* initialize input from a Radiance picture */
372 {
373 int i1, i2, po;
374 /* read Radiance header */
375 CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
376 cvts.stonits = 1.;
377 cvts.pixrat = 1.;
378 cvts.pconf = PLANARCONFIG_CONTIG;
379 getheader(cvts.rfp, headline, NULL);
380 if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
381 quiterr("bad Radiance picture");
382 cvts.xmax = i1; cvts.ymax = i2;
383 for (i1 = 0; i1 < 8; i1++) /* interpret orientation */
384 if (ortab[i1] == po) {
385 cvts.orient = i1 + 1;
386 break;
387 }
388 if (i1 >= 8)
389 quiterr("internal error 1 in initfromrad");
390 if (!(po & YMAJOR))
391 cvts.pixrat = 1./cvts.pixrat;
392 if (!CHK(C_XYZE))
393 cvts.stonits *= WHTEFFICACY;
394 /* set up conversion */
395 TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp);
396 TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
397
398 switch (cvts.phot) {
399 case PHOTOMETRIC_LOGLUV:
400 SET(C_RFLT|C_TFLT);
401 CLR(C_GRY);
402 if (!CHK(C_XYZE)) {
403 cpcolormat(cvts.cmat, rgb2xyzmat);
404 SET(C_CXFM);
405 }
406 if (cvts.comp != COMPRESSION_SGILOG &&
407 cvts.comp != COMPRESSION_SGILOG24)
408 quiterr("internal error 2 in initfromrad");
409 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
410 SGILOGDATAFMT_FLTXYZ);
411 cvts.tf = Color2Luv;
412 break;
413 case PHOTOMETRIC_LOGL:
414 SET(C_GRY|C_RFLT|C_TFLT);
415 if (cvts.comp != COMPRESSION_SGILOG)
416 quiterr("internal error 3 in initfromrad");
417 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
418 SGILOGDATAFMT_FLTY);
419 cvts.tf = Color2L;
420 break;
421 case PHOTOMETRIC_RGB:
422 SET(C_GAMMA|C_GAMUT);
423 CLR(C_GRY);
424 setcolrgam(cvts.gamcor);
425 if (CHK(C_XYZE)) {
426 compxyz2rgbmat(cvts.cmat,
427 CHK(C_PRIM) ? cvts.prims : stdprims);
428 SET(C_CXFM);
429 }
430 if (CHK(C_PRIM)) {
431 TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES,
432 (float *)cvts.prims);
433 TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
434 (float *)cvts.prims[WHT]);
435 }
436 cvts.tf = Colr2RGB;
437 break;
438 case PHOTOMETRIC_MINISBLACK:
439 SET(C_GRY|C_GAMMA|C_GAMUT);
440 setcolrgam(cvts.gamcor);
441 cvts.tf = Colr2Gry;
442 break;
443 default:
444 quiterr("internal error 4 in initfromrad");
445 break;
446 }
447 /* set other TIFF fields */
448 TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
449 TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
450 TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
451 TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, CHK(C_TFLT) ? 32 : 8);
452 TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
453 TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
454 TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
455 TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2);
456 TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
457 TIFFSetField(cvts.tif, TIFFTAG_STONITS,
458 cvts.stonits/pow(2.,(double)cvts.bradj));
459 if (cvts.comp == COMPRESSION_NONE)
460 i1 = TIFFScanlineSize(cvts.tif);
461 else
462 i1 = 3*cvts.xmax; /* conservative guess */
463 i2 = 8192/i1; /* compute good strip size */
464 if (i2 < 1) i2 = 1;
465 TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2);
466
467 allocbufs(); /* allocate scanline buffers */
468 }
469
470
471 ra2tiff(ac, av) /* convert Radiance picture to TIFF image */
472 int ac;
473 char *av[];
474 {
475 uint32 y;
476 /* open Radiance file */
477 if (!strcmp(av[ac], "-"))
478 cvts.rfp = stdin;
479 else if ((cvts.rfp = fopen(av[ac], "r")) == NULL)
480 quiterr("cannot open Radiance input picture");
481 /* open TIFF file */
482 if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
483 quiterr("cannot open TIFF output");
484
485 initfromrad(); /* initialize conversion */
486
487 for (y = 0; y < cvts.ymax; y++) /* convert image */
488 (*cvts.tf)(y);
489 /* clean up */
490 TIFFClose(cvts.tif);
491 fclose(cvts.rfp);
492 }
493
494
495 int
496 Luv2Color(y) /* read/convert/write Luv->COLOR scanline */
497 uint32 y;
498 {
499 register int x;
500
501 if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
502 quiterr("internal error 1 in Luv2Color");
503
504 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
505 quiterr("error reading TIFF input");
506
507 for (x = cvts.xmax; x--; ) {
508 cvts.r.colors[x][RED] = cvts.t.fp[3*x];
509 cvts.r.colors[x][GRN] = cvts.t.fp[3*x + 1];
510 cvts.r.colors[x][BLU] = cvts.t.fp[3*x + 2];
511 if (CHK(C_CXFM))
512 colortrans(cvts.r.colors[x], cvts.cmat,
513 cvts.r.colors[x]);
514 if (CHK(C_GAMUT))
515 clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
516 CGAMUT_LOWER, cblack, cwhite);
517 }
518 if (cvts.bradj) {
519 double m = pow(2.,(double)cvts.bradj);
520 for (x = cvts.xmax; x--; )
521 scalecolor(cvts.r.colors[x], m);
522 }
523
524 if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
525 quiterr("error writing Radiance picture");
526 }
527
528
529 int
530 L2Color(y) /* read/convert/write L16->COLOR scanline */
531 uint32 y;
532 {
533 register int x;
534
535 if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
536 quiterr("internal error 1 in L2Color");
537
538 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
539 quiterr("error reading TIFF input");
540
541 for (x = cvts.xmax; x--; )
542 cvts.r.colors[x][RED] =
543 cvts.r.colors[x][GRN] =
544 cvts.r.colors[x][BLU] = cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.;
545
546 if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
547 quiterr("error writing Radiance picture");
548 }
549
550
551 int
552 RGB2Colr(y) /* read/convert/write RGB->COLR scanline */
553 uint32 y;
554 {
555 COLOR ctmp;
556 register int x;
557
558 if (CHK(C_RFLT|C_TFLT|C_GRY))
559 quiterr("internal error 1 in RGB2Colr");
560
561 if (cvts.pconf == PLANARCONFIG_CONTIG) {
562 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
563 goto readerr;
564 for (x = cvts.xmax; x--; ) {
565 cvts.r.colrs[x][RED] = cvts.t.bp[3*x];
566 cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1];
567 cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2];
568 }
569 } else {
570 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
571 goto readerr;
572 if (TIFFReadScanline(cvts.tif,
573 (tidata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
574 goto readerr;
575 if (TIFFReadScanline(cvts.tif,
576 (tidata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
577 goto readerr;
578 for (x = cvts.xmax; x--; ) {
579 cvts.r.colrs[x][RED] = cvts.t.bp[x];
580 cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x];
581 cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x];
582 }
583 }
584
585 gambs_colrs(cvts.r.colrs, cvts.xmax);
586 if (CHK(C_CXFM))
587 for (x = cvts.xmax; x--; ) {
588 colr_color(ctmp, cvts.r.colrs[x]);
589 colortrans(ctmp, cvts.cmat, ctmp);
590 if (CHK(C_GAMUT)) /* !CHK(C_XYZE) */
591 clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER,
592 cblack, cwhite);
593 setcolr(cvts.r.colrs[x], colval(ctmp,RED),
594 colval(ctmp,GRN), colval(ctmp,BLU));
595 }
596 if (cvts.bradj)
597 shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
598
599 if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
600 quiterr("error writing Radiance picture");
601 return;
602 readerr:
603 quiterr("error reading TIFF input");
604 }
605
606
607 int
608 Gry2Colr(y) /* read/convert/write G8->COLR scanline */
609 uint32 y;
610 {
611 register int x;
612
613 if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
614 quiterr("internal error 1 in Gry2Colr");
615
616 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
617 quiterr("error reading TIFF input");
618
619 for (x = cvts.xmax; x--; )
620 cvts.r.colrs[x][RED] =
621 cvts.r.colrs[x][GRN] =
622 cvts.r.colrs[x][BLU] = cvts.t.bp[x];
623
624 gambs_colrs(cvts.r.colrs, cvts.xmax);
625 if (cvts.bradj)
626 shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
627
628 if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
629 quiterr("error writing Radiance picture");
630 }
631
632
633 int
634 Color2L(y) /* read/convert/write COLOR->L16 scanline */
635 uint32 y;
636 {
637 double m = pow(2.,(double)cvts.bradj);
638 register int x;
639
640 if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | !CHK(C_GRY))
641 quiterr("internal error 1 in Color2L");
642
643 if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
644 quiterr("error reading Radiance picture");
645
646 for (x = cvts.xmax; x--; )
647 cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY)
648 : bright(cvts.r.colors[x]) );
649
650 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
651 quiterr("error writing TIFF output");
652 }
653
654
655 int
656 Color2Luv(y) /* read/convert/write COLOR->Luv scanline */
657 uint32 y;
658 {
659 register int x;
660
661 if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
662 quiterr("internal error 1 in Color2Luv");
663
664 if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
665 quiterr("error reading Radiance picture");
666
667 if (CHK(C_CXFM))
668 for (x = cvts.xmax; x--; )
669 colortrans(cvts.r.colors[x], cvts.cmat,
670 cvts.r.colors[x]);
671 if (cvts.bradj) {
672 double m = pow(2.,(double)cvts.bradj);
673 for (x = cvts.xmax; x--; )
674 scalecolor(cvts.r.colors[x], m);
675 }
676
677 for (x = cvts.xmax; x--; ) {
678 cvts.t.fp[3*x] = colval(cvts.r.colors[x],RED);
679 cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],GRN);
680 cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],BLU);
681 }
682
683 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
684 quiterr("error writing TIFF output");
685 }
686
687
688 int
689 Colr2Gry(y) /* read/convert/write COLR->RGB scanline */
690 uint32 y;
691 {
692 register int x;
693
694 if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
695 quiterr("internal error 1 in Colr2Gry");
696
697 if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
698 quiterr("error reading Radiance picture");
699
700 if (cvts.bradj)
701 shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
702 for (x = cvts.xmax; x--; )
703 colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]);
704 colrs_gambs(cvts.r.colrs, cvts.xmax);
705
706 for (x = cvts.xmax; x--; )
707 cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY);
708
709 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
710 quiterr("error writing TIFF output");
711 }
712
713
714 int
715 Colr2RGB(y) /* read/convert/write COLR->RGB scanline */
716 uint32 y;
717 {
718 COLOR ctmp;
719 register int x;
720
721 if (CHK(C_RFLT|C_TFLT|C_GRY))
722 quiterr("internal error 1 in Colr2RGB");
723
724 if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
725 quiterr("error reading Radiance picture");
726
727 if (cvts.bradj)
728 shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
729 if (CHK(C_CXFM))
730 for (x = cvts.xmax; x--; ) {
731 colr_color(ctmp, cvts.r.colrs[x]);
732 colortrans(ctmp, cvts.cmat, ctmp);
733 if (CHK(C_GAMUT))
734 clipgamut(ctmp, bright(ctmp), CGAMUT,
735 cblack, cwhite);
736 setcolr(cvts.r.colrs[x], colval(ctmp,RED),
737 colval(ctmp,GRN), colval(ctmp,BLU));
738 }
739 colrs_gambs(cvts.r.colrs, cvts.xmax);
740
741 for (x = cvts.xmax; x--; ) {
742 cvts.t.bp[3*x] = cvts.r.colrs[x][RED];
743 cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN];
744 cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU];
745 }
746
747 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
748 quiterr("error writing TIFF output");
749 }