ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_tiff.c
Revision: 2.19
Committed: Wed Jun 2 10:57:53 1999 UTC (24 years, 11 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.18: +16 -14 lines
Log Message:
fixed bug in allocated buffer size

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