ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.17
Committed: Tue Oct 27 09:08:27 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.16: +5 -4 lines
Log Message:
changed getheader() to listen to return value of passed function

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 double 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_FLOAT);
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_FLOAT);
252 cvts.tf = L2Color;
253 break;
254 case PHOTOMETRIC_YCBCR:
255 if (cvts.comp == COMPRESSION_JPEG &&
256 cvts.pconf == PLANARCONFIG_CONTIG) {
257 TIFFSetField(cvts.tif, TIFFTAG_JPEGCOLORMODE,
258 JPEGCOLORMODE_RGB);
259 cvts.phot = PHOTOMETRIC_RGB;
260 } else
261 quiterr("unsupported photometric type");
262 /* fall through */
263 case PHOTOMETRIC_RGB:
264 SET(C_GAMMA|C_GAMUT);
265 setcolrgam(cvts.gamcor);
266 if (CHK(C_XYZE)) {
267 comprgb2xyzmat(cvts.cmat,
268 CHK(C_PRIM) ? cvts.prims : stdprims);
269 SET(C_CXFM);
270 }
271 if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
272 hi != 3)
273 quiterr("unsupported samples per pixel for RGB");
274 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
275 hi != 8)
276 quiterr("unsupported bits per sample for RGB");
277 cvts.tf = RGB2Colr;
278 break;
279 case PHOTOMETRIC_MINISBLACK:
280 SET(C_GRY|C_GAMMA|C_GAMUT);
281 setcolrgam(cvts.gamcor);
282 cvts.pconf = PLANARCONFIG_CONTIG;
283 if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) ||
284 hi != 1)
285 quiterr("unsupported samples per pixel for greyscale");
286 if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) ||
287 hi != 8)
288 quiterr("unsupported bits per sample for greyscale");
289 cvts.tf = Gry2Colr;
290 break;
291 default:
292 quiterr("unsupported photometric type");
293 break;
294 }
295
296 if (!TIFFGetField(cvts.tif, TIFFTAG_IMAGEWIDTH, &cvts.xmax) ||
297 !TIFFGetField(cvts.tif, TIFFTAG_IMAGELENGTH, &cvts.ymax))
298 quiterr("unknown input image resolution");
299
300 if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits))
301 cvts.stonits = 1.;
302 /* add to Radiance header */
303 if (cvts.pixrat < .99 || cvts.pixrat > 1.01)
304 fputaspect(cvts.pixrat, cvts.rfp);
305 if (CHK(C_XYZE)) {
306 fputexpos(pow(2.,(double)cvts.bradj)/cvts.stonits, cvts.rfp);
307 fputformat(CIEFMT, cvts.rfp);
308 } else {
309 if (CHK(C_PRIM))
310 fputprims(cvts.prims, cvts.rfp);
311 fputexpos(WHTEFFICACY*pow(2.,(double)cvts.bradj)/cvts.stonits,
312 cvts.rfp);
313 fputformat(COLRFMT, cvts.rfp);
314 }
315
316 allocbufs(); /* allocate scanline buffers */
317 }
318
319
320 tiff2ra(ac, av) /* convert TIFF image to Radiance picture */
321 int ac;
322 char *av[];
323 {
324 int32 y;
325 /* open TIFF input */
326 if ((cvts.tif = TIFFOpen(av[ac], "r")) == NULL)
327 quiterr("cannot open TIFF input");
328 /* open Radiance output */
329 if (av[ac+1] == NULL || !strcmp(av[ac+1], "-"))
330 cvts.rfp = stdout;
331 else if ((cvts.rfp = fopen(av[ac+1], "w")) == NULL)
332 quiterr("cannot open Radiance output picture");
333 /* start output header */
334 newheader("RADIANCE", cvts.rfp);
335 printargs(ac, av, cvts.rfp);
336
337 initfromtif(); /* initialize conversion */
338
339 fputc('\n', cvts.rfp); /* finish Radiance header */
340 fputresolu(pixorder(), (int)cvts.xmax, (int)cvts.ymax, cvts.rfp);
341
342 for (y = 0; y < cvts.ymax; y++) /* convert image */
343 (*cvts.tf)(y);
344 /* clean up */
345 fclose(cvts.rfp);
346 TIFFClose(cvts.tif);
347 }
348
349
350 int
351 headline(s) /* process Radiance input header line */
352 char *s;
353 {
354 char fmt[32];
355
356 if (formatval(fmt, s)) {
357 if (!strcmp(fmt, COLRFMT))
358 CLR(C_XYZE);
359 else if (!strcmp(fmt, CIEFMT))
360 SET(C_XYZE);
361 else
362 quiterr("unrecognized input picture format");
363 return(1);
364 }
365 if (isexpos(s)) {
366 cvts.stonits /= exposval(s);
367 return(1);
368 }
369 if (isaspect(s)) {
370 cvts.pixrat *= aspectval(s);
371 return(1);
372 }
373 if (isprims(s)) {
374 primsval(cvts.prims, s);
375 SET(C_PRIM);
376 return(1);
377 }
378 return(0);
379 }
380
381
382 initfromrad() /* initialize input from a Radiance picture */
383 {
384 int i1, i2, po;
385 /* read Radiance header */
386 CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM);
387 cvts.stonits = 1.;
388 cvts.pixrat = 1.;
389 cvts.pconf = PLANARCONFIG_CONTIG;
390 getheader(cvts.rfp, headline, NULL);
391 if ((po = fgetresolu(&i1, &i2, cvts.rfp)) < 0)
392 quiterr("bad Radiance picture");
393 cvts.xmax = i1; cvts.ymax = i2;
394 for (i1 = 0; i1 < 8; i1++) /* interpret orientation */
395 if (ortab[i1] == po) {
396 cvts.orient = i1 + 1;
397 break;
398 }
399 if (i1 >= 8)
400 quiterr("internal error 1 in initfromrad");
401 if (!(po & YMAJOR))
402 cvts.pixrat = 1./cvts.pixrat;
403 if (!CHK(C_XYZE))
404 cvts.stonits *= WHTEFFICACY;
405 /* set up conversion */
406 TIFFSetField(cvts.tif, TIFFTAG_COMPRESSION, cvts.comp);
407 TIFFSetField(cvts.tif, TIFFTAG_PHOTOMETRIC, cvts.phot);
408
409 switch (cvts.phot) {
410 case PHOTOMETRIC_LOGLUV:
411 SET(C_RFLT|C_TFLT);
412 CLR(C_GRY);
413 if (!CHK(C_XYZE)) {
414 cpcolormat(cvts.cmat, rgb2xyzmat);
415 SET(C_CXFM);
416 }
417 if (cvts.comp != COMPRESSION_SGILOG &&
418 cvts.comp != COMPRESSION_SGILOG24)
419 quiterr("internal error 2 in initfromrad");
420 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
421 SGILOGDATAFMT_FLOAT);
422 cvts.tf = Color2Luv;
423 break;
424 case PHOTOMETRIC_LOGL:
425 SET(C_GRY|C_RFLT|C_TFLT);
426 if (cvts.comp != COMPRESSION_SGILOG)
427 quiterr("internal error 3 in initfromrad");
428 TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT,
429 SGILOGDATAFMT_FLOAT);
430 cvts.tf = Color2L;
431 break;
432 case PHOTOMETRIC_RGB:
433 SET(C_GAMMA|C_GAMUT);
434 CLR(C_GRY);
435 setcolrgam(cvts.gamcor);
436 if (CHK(C_XYZE)) {
437 compxyz2rgbmat(cvts.cmat,
438 CHK(C_PRIM) ? cvts.prims : stdprims);
439 SET(C_CXFM);
440 }
441 if (CHK(C_PRIM)) {
442 TIFFSetField(cvts.tif, TIFFTAG_PRIMARYCHROMATICITIES,
443 (float *)cvts.prims);
444 TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT,
445 (float *)cvts.prims[WHT]);
446 }
447 cvts.tf = Colr2RGB;
448 break;
449 case PHOTOMETRIC_MINISBLACK:
450 SET(C_GRY|C_GAMMA|C_GAMUT);
451 setcolrgam(cvts.gamcor);
452 cvts.tf = Colr2Gry;
453 break;
454 default:
455 quiterr("internal error 4 in initfromrad");
456 break;
457 }
458 /* set other TIFF fields */
459 TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax);
460 TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax);
461 TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3);
462 TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, CHK(C_TFLT) ? 32 : 8);
463 TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.);
464 TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat);
465 TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient);
466 TIFFSetField(cvts.tif, TIFFTAG_RESOLUTIONUNIT, 2);
467 TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf);
468 TIFFSetField(cvts.tif, TIFFTAG_STONITS,
469 cvts.stonits/pow(2.,(double)cvts.bradj));
470 if (cvts.comp == COMPRESSION_NONE)
471 i1 = TIFFScanlineSize(cvts.tif);
472 else
473 i1 = 3*cvts.xmax; /* conservative guess */
474 i2 = 8192/i1; /* compute good strip size */
475 if (i2 < 1) i2 = 1;
476 TIFFSetField(cvts.tif, TIFFTAG_ROWSPERSTRIP, (uint32)i2);
477
478 allocbufs(); /* allocate scanline buffers */
479 }
480
481
482 ra2tiff(ac, av) /* convert Radiance picture to TIFF image */
483 int ac;
484 char *av[];
485 {
486 uint32 y;
487 /* open Radiance file */
488 if (!strcmp(av[ac], "-"))
489 cvts.rfp = stdin;
490 else if ((cvts.rfp = fopen(av[ac], "r")) == NULL)
491 quiterr("cannot open Radiance input picture");
492 /* open TIFF file */
493 if ((cvts.tif = TIFFOpen(av[ac+1], "w")) == NULL)
494 quiterr("cannot open TIFF output");
495
496 initfromrad(); /* initialize conversion */
497
498 for (y = 0; y < cvts.ymax; y++) /* convert image */
499 (*cvts.tf)(y);
500 /* clean up */
501 TIFFClose(cvts.tif);
502 fclose(cvts.rfp);
503 }
504
505
506 int
507 Luv2Color(y) /* read/convert/write Luv->COLOR scanline */
508 uint32 y;
509 {
510 register int x;
511
512 if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
513 quiterr("internal error 1 in Luv2Color");
514
515 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
516 quiterr("error reading TIFF input");
517
518 for (x = cvts.xmax; x--; ) {
519 colval(cvts.r.colors[x],CIEX) = cvts.t.fp[3*x];
520 colval(cvts.r.colors[x],CIEY) = cvts.t.fp[3*x + 1];
521 colval(cvts.r.colors[x],CIEZ) = cvts.t.fp[3*x + 2];
522 if (CHK(C_CXFM))
523 colortrans(cvts.r.colors[x], cvts.cmat,
524 cvts.r.colors[x]);
525 if (CHK(C_GAMUT))
526 clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1],
527 CGAMUT_LOWER, cblack, cwhite);
528 }
529 if (cvts.bradj) {
530 double m = pow(2.,(double)cvts.bradj);
531 for (x = cvts.xmax; x--; )
532 scalecolor(cvts.r.colors[x], m);
533 }
534
535 if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
536 quiterr("error writing Radiance picture");
537 }
538
539
540 int
541 L2Color(y) /* read/convert/write L16->COLOR scanline */
542 uint32 y;
543 {
544 register int x;
545
546 if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
547 quiterr("internal error 1 in L2Color");
548
549 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
550 quiterr("error reading TIFF input");
551
552 for (x = cvts.xmax; x--; )
553 colval(cvts.r.colors[x],RED) =
554 colval(cvts.r.colors[x],GRN) =
555 colval(cvts.r.colors[x],BLU) =
556 cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.;
557
558 if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
559 quiterr("error writing Radiance picture");
560 }
561
562
563 int
564 RGB2Colr(y) /* read/convert/write RGB->COLR scanline */
565 uint32 y;
566 {
567 COLOR ctmp;
568 register int x;
569
570 if (CHK(C_RFLT|C_TFLT|C_GRY))
571 quiterr("internal error 1 in RGB2Colr");
572
573 if (cvts.pconf == PLANARCONFIG_CONTIG) {
574 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
575 goto readerr;
576 for (x = cvts.xmax; x--; ) {
577 cvts.r.colrs[x][RED] = cvts.t.bp[3*x];
578 cvts.r.colrs[x][GRN] = cvts.t.bp[3*x + 1];
579 cvts.r.colrs[x][BLU] = cvts.t.bp[3*x + 2];
580 }
581 } else {
582 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
583 goto readerr;
584 if (TIFFReadScanline(cvts.tif,
585 (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0)
586 goto readerr;
587 if (TIFFReadScanline(cvts.tif,
588 (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0)
589 goto readerr;
590 for (x = cvts.xmax; x--; ) {
591 cvts.r.colrs[x][RED] = cvts.t.bp[x];
592 cvts.r.colrs[x][GRN] = cvts.t.bp[cvts.xmax + x];
593 cvts.r.colrs[x][BLU] = cvts.t.bp[2*cvts.xmax + x];
594 }
595 }
596
597 gambs_colrs(cvts.r.colrs, cvts.xmax);
598 if (CHK(C_CXFM))
599 for (x = cvts.xmax; x--; ) {
600 colr_color(ctmp, cvts.r.colrs[x]);
601 colortrans(ctmp, cvts.cmat, ctmp);
602 if (CHK(C_GAMUT)) /* !CHK(C_XYZE) */
603 clipgamut(ctmp, bright(ctmp), CGAMUT_LOWER,
604 cblack, cwhite);
605 setcolr(cvts.r.colrs[x], colval(ctmp,RED),
606 colval(ctmp,GRN), colval(ctmp,BLU));
607 }
608 if (cvts.bradj)
609 shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
610
611 if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
612 quiterr("error writing Radiance picture");
613 return;
614 readerr:
615 quiterr("error reading TIFF input");
616 }
617
618
619 int
620 Gry2Colr(y) /* read/convert/write G8->COLR scanline */
621 uint32 y;
622 {
623 register int x;
624
625 if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
626 quiterr("internal error 1 in Gry2Colr");
627
628 if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0)
629 quiterr("error reading TIFF input");
630
631 for (x = cvts.xmax; x--; )
632 cvts.r.colrs[x][RED] =
633 cvts.r.colrs[x][GRN] =
634 cvts.r.colrs[x][BLU] = cvts.t.bp[x];
635
636 gambs_colrs(cvts.r.colrs, cvts.xmax);
637 if (cvts.bradj)
638 shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
639
640 if (fwritecolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
641 quiterr("error writing Radiance picture");
642 }
643
644
645 int
646 Color2L(y) /* read/convert/write COLOR->L16 scanline */
647 uint32 y;
648 {
649 double m = pow(2.,(double)cvts.bradj);
650 register int x;
651
652 if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY))
653 quiterr("internal error 1 in Color2L");
654
655 if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
656 quiterr("error reading Radiance picture");
657
658 for (x = cvts.xmax; x--; )
659 cvts.t.fp[x] = m*( CHK(C_XYZE) ? colval(cvts.r.colors[x],CIEY)
660 : bright(cvts.r.colors[x]) );
661
662 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
663 quiterr("error writing TIFF output");
664 }
665
666
667 int
668 Color2Luv(y) /* read/convert/write COLOR->Luv scanline */
669 uint32 y;
670 {
671 register int x;
672
673 if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY))
674 quiterr("internal error 1 in Color2Luv");
675
676 if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0)
677 quiterr("error reading Radiance picture");
678
679 if (CHK(C_CXFM))
680 for (x = cvts.xmax; x--; )
681 colortrans(cvts.r.colors[x], cvts.cmat,
682 cvts.r.colors[x]);
683 if (cvts.bradj) {
684 double m = pow(2.,(double)cvts.bradj);
685 for (x = cvts.xmax; x--; )
686 scalecolor(cvts.r.colors[x], m);
687 }
688
689 for (x = cvts.xmax; x--; ) {
690 cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX);
691 cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY);
692 cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ);
693 }
694
695 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
696 quiterr("error writing TIFF output");
697 }
698
699
700 int
701 Colr2Gry(y) /* read/convert/write COLR->RGB scanline */
702 uint32 y;
703 {
704 register int x;
705
706 if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY))
707 quiterr("internal error 1 in Colr2Gry");
708
709 if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
710 quiterr("error reading Radiance picture");
711
712 if (cvts.bradj)
713 shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
714 for (x = cvts.xmax; x--; )
715 colval(cvts.r.colrs[x],CIEY) = normbright(cvts.r.colrs[x]);
716 colrs_gambs(cvts.r.colrs, cvts.xmax);
717
718 for (x = cvts.xmax; x--; )
719 cvts.t.bp[x] = colval(cvts.r.colrs[x],CIEY);
720
721 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
722 quiterr("error writing TIFF output");
723 }
724
725
726 int
727 Colr2RGB(y) /* read/convert/write COLR->RGB scanline */
728 uint32 y;
729 {
730 COLOR ctmp;
731 register int x;
732
733 if (CHK(C_RFLT|C_TFLT|C_GRY))
734 quiterr("internal error 1 in Colr2RGB");
735
736 if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)
737 quiterr("error reading Radiance picture");
738
739 if (cvts.bradj)
740 shiftcolrs(cvts.r.colrs, cvts.xmax, cvts.bradj);
741 if (CHK(C_CXFM))
742 for (x = cvts.xmax; x--; ) {
743 colr_color(ctmp, cvts.r.colrs[x]);
744 colortrans(ctmp, cvts.cmat, ctmp);
745 if (CHK(C_GAMUT))
746 clipgamut(ctmp, bright(ctmp), CGAMUT,
747 cblack, cwhite);
748 setcolr(cvts.r.colrs[x], colval(ctmp,RED),
749 colval(ctmp,GRN), colval(ctmp,BLU));
750 }
751 colrs_gambs(cvts.r.colrs, cvts.xmax);
752
753 for (x = cvts.xmax; x--; ) {
754 cvts.t.bp[3*x] = cvts.r.colrs[x][RED];
755 cvts.t.bp[3*x+1] = cvts.r.colrs[x][GRN];
756 cvts.t.bp[3*x+2] = cvts.r.colrs[x][BLU];
757 }
758
759 if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0)
760 quiterr("error writing TIFF output");
761 }