1 |
– |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Program to convert between RADIANCE and TIFF files. |
6 |
< |
* Added experimental LogLuv encodings 7/97 (GWL). |
6 |
> |
* Added LogLuv encodings 7/97 (GWL). |
7 |
> |
* Added white-balance adjustment 10/01 (GW). |
8 |
|
*/ |
9 |
|
|
10 |
|
#include <stdio.h> |
11 |
|
#include <math.h> |
12 |
+ |
#include <time.h> |
13 |
|
#include "tiffio.h" |
14 |
|
#include "color.h" |
15 |
|
#include "resolu.h" |
16 |
|
|
17 |
|
#define GAMCOR 2.2 /* default gamma */ |
18 |
|
|
20 |
– |
#ifndef malloc |
21 |
– |
extern char *malloc(); |
22 |
– |
#endif |
19 |
|
/* conversion flags */ |
20 |
|
#define C_CXFM 0x1 /* needs color transformation */ |
21 |
|
#define C_GAMUT 0x2 /* needs gamut mapping */ |
24 |
|
#define C_XYZE 0x10 /* Radiance is XYZE */ |
25 |
|
#define C_RFLT 0x20 /* Radiance data is float */ |
26 |
|
#define C_TFLT 0x40 /* TIFF data is float */ |
27 |
< |
#define C_PRIM 0x80 /* has assigned primaries */ |
27 |
> |
#define C_TWRD 0x80 /* TIFF data is 16-bit */ |
28 |
> |
#define C_PRIM 0x100 /* has assigned primaries */ |
29 |
|
|
30 |
|
struct { |
31 |
|
uint16 flags; /* conversion flags (defined above) */ |
49 |
|
} r; /* Radiance scanline */ |
50 |
|
union { |
51 |
|
uint8 *bp; /* byte pointer */ |
52 |
+ |
uint16 *wp; /* word pointer */ |
53 |
|
float *fp; /* float pointer */ |
54 |
|
char *p; /* generic pointer */ |
55 |
|
} t; /* TIFF scanline */ |
66 |
|
|
67 |
|
int Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr(); |
68 |
|
int Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry(); |
69 |
+ |
int RRGGBB2Color(), GGry2Color(); |
70 |
|
|
71 |
|
short ortab[8] = { /* orientation conversion table */ |
72 |
|
YMAJOR|YDECR, |
183 |
|
int rsiz, tsiz; |
184 |
|
|
185 |
|
rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR); |
186 |
< |
tsiz = (CHK(C_TFLT) ? sizeof(float) : sizeof(uint8)) * |
186 |
> |
tsiz = (CHK(C_TFLT) ? sizeof(float) : |
187 |
> |
CHK(C_TWRD) ? sizeof(uint16) : 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); |
261 |
|
quiterr("unsupported photometric type"); |
262 |
|
/* fall through */ |
263 |
|
case PHOTOMETRIC_RGB: |
264 |
< |
SET(C_GAMMA|C_GAMUT); |
264 |
> |
SET(C_GAMMA); |
265 |
|
setcolrgam(cvts.gamcor); |
266 |
|
if (CHK(C_XYZE)) { |
267 |
< |
comprgb2xyzmat(cvts.cmat, |
267 |
> |
comprgb2xyzWBmat(cvts.cmat, |
268 |
|
CHK(C_PRIM) ? cvts.prims : stdprims); |
269 |
|
SET(C_CXFM); |
270 |
|
} |
272 |
|
hi != 3) |
273 |
|
quiterr("unsupported samples per pixel for RGB"); |
274 |
|
if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) || |
275 |
< |
hi != 8) |
275 |
> |
hi != 8 & hi != 16) |
276 |
|
quiterr("unsupported bits per sample for RGB"); |
277 |
< |
cvts.tf = RGB2Colr; |
277 |
> |
if (hi == 8) |
278 |
> |
cvts.tf = RGB2Colr; |
279 |
> |
else { |
280 |
> |
cvts.tf = RRGGBB2Color; |
281 |
> |
SET(C_RFLT|C_TWRD); |
282 |
> |
} |
283 |
|
break; |
284 |
|
case PHOTOMETRIC_MINISBLACK: |
285 |
< |
SET(C_GRY|C_GAMMA|C_GAMUT); |
285 |
> |
SET(C_GRY|C_GAMMA); |
286 |
|
setcolrgam(cvts.gamcor); |
287 |
|
cvts.pconf = PLANARCONFIG_CONTIG; |
288 |
|
if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || |
289 |
|
hi != 1) |
290 |
|
quiterr("unsupported samples per pixel for greyscale"); |
291 |
|
if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) || |
292 |
< |
hi != 8) |
292 |
> |
hi != 8 & hi != 16) |
293 |
|
quiterr("unsupported bits per sample for greyscale"); |
294 |
< |
cvts.tf = Gry2Colr; |
294 |
> |
if (hi == 8) |
295 |
> |
cvts.tf = Gry2Colr; |
296 |
> |
else { |
297 |
> |
cvts.tf = GGry2Color; |
298 |
> |
SET(C_RFLT|C_TWRD); |
299 |
> |
} |
300 |
|
break; |
301 |
|
default: |
302 |
|
quiterr("unsupported photometric type"); |
421 |
|
SET(C_RFLT|C_TFLT); |
422 |
|
CLR(C_GRY); |
423 |
|
if (!CHK(C_XYZE)) { |
424 |
< |
cpcolormat(cvts.cmat, rgb2xyzmat); |
424 |
> |
comprgb2xyzWBmat(cvts.cmat, |
425 |
> |
CHK(C_PRIM) ? cvts.prims : stdprims); |
426 |
|
SET(C_CXFM); |
427 |
|
} |
428 |
|
if (cvts.comp != COMPRESSION_SGILOG && |
445 |
|
CLR(C_GRY); |
446 |
|
setcolrgam(cvts.gamcor); |
447 |
|
if (CHK(C_XYZE)) { |
448 |
< |
compxyz2rgbmat(cvts.cmat, |
448 |
> |
compxyz2rgbWBmat(cvts.cmat, |
449 |
|
CHK(C_PRIM) ? cvts.prims : stdprims); |
450 |
|
SET(C_CXFM); |
451 |
|
} |
520 |
|
{ |
521 |
|
register int x; |
522 |
|
|
523 |
< |
if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY)) |
523 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) |
524 |
|
quiterr("internal error 1 in Luv2Color"); |
525 |
|
|
526 |
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
549 |
|
|
550 |
|
|
551 |
|
int |
552 |
+ |
RRGGBB2Color(y) /* read/convert/write RGB16->COLOR scanline */ |
553 |
+ |
uint32 y; |
554 |
+ |
{ |
555 |
+ |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
556 |
+ |
register double d; |
557 |
+ |
register int x; |
558 |
+ |
|
559 |
+ |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT)) |
560 |
+ |
quiterr("internal error 1 in RRGGBB2Color"); |
561 |
+ |
|
562 |
+ |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
563 |
+ |
quiterr("error reading TIFF input"); |
564 |
+ |
|
565 |
+ |
for (x = cvts.xmax; x--; ) { |
566 |
+ |
d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16)); |
567 |
+ |
if (dogamma) d = pow(d, cvts.gamcor); |
568 |
+ |
colval(cvts.r.colors[x],RED) = d; |
569 |
+ |
d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16)); |
570 |
+ |
if (dogamma) d = pow(d, cvts.gamcor); |
571 |
+ |
colval(cvts.r.colors[x],GRN) = d; |
572 |
+ |
d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16)); |
573 |
+ |
if (dogamma) d = pow(d, cvts.gamcor); |
574 |
+ |
colval(cvts.r.colors[x],BLU) = d; |
575 |
+ |
if (CHK(C_CXFM)) |
576 |
+ |
colortrans(cvts.r.colors[x], cvts.cmat, |
577 |
+ |
cvts.r.colors[x]); |
578 |
+ |
if (CHK(C_GAMUT)) |
579 |
+ |
clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1], |
580 |
+ |
CGAMUT_LOWER, cblack, cwhite); |
581 |
+ |
} |
582 |
+ |
if (cvts.bradj) { |
583 |
+ |
d = pow(2.,(double)cvts.bradj); |
584 |
+ |
for (x = cvts.xmax; x--; ) |
585 |
+ |
scalecolor(cvts.r.colors[x], d); |
586 |
+ |
} |
587 |
+ |
|
588 |
+ |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
589 |
+ |
quiterr("error writing Radiance picture"); |
590 |
+ |
} |
591 |
+ |
|
592 |
+ |
|
593 |
+ |
int |
594 |
|
L2Color(y) /* read/convert/write L16->COLOR scanline */ |
595 |
|
uint32 y; |
596 |
|
{ |
597 |
|
register int x; |
598 |
|
|
599 |
< |
if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
599 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
600 |
|
quiterr("internal error 1 in L2Color"); |
601 |
|
|
602 |
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
620 |
|
COLOR ctmp; |
621 |
|
register int x; |
622 |
|
|
623 |
< |
if (CHK(C_RFLT|C_TFLT|C_GRY)) |
623 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY)) |
624 |
|
quiterr("internal error 1 in RGB2Colr"); |
625 |
|
|
626 |
|
if (cvts.pconf == PLANARCONFIG_CONTIG) { |
675 |
|
{ |
676 |
|
register int x; |
677 |
|
|
678 |
< |
if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY)) |
678 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY) |
679 |
|
quiterr("internal error 1 in Gry2Colr"); |
680 |
|
|
681 |
|
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
696 |
|
|
697 |
|
|
698 |
|
int |
699 |
+ |
GGry2Color(y) /* read/convert/write G16->COLOR scanline */ |
700 |
+ |
uint32 y; |
701 |
+ |
{ |
702 |
+ |
int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; |
703 |
+ |
double m; |
704 |
+ |
register double d; |
705 |
+ |
register int x; |
706 |
+ |
|
707 |
+ |
if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD)) |
708 |
+ |
quiterr("internal error 1 in GGry2Color"); |
709 |
+ |
|
710 |
+ |
if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) |
711 |
+ |
quiterr("error reading TIFF input"); |
712 |
+ |
|
713 |
+ |
if (cvts.bradj) |
714 |
+ |
m = pow(2., (double)cvts.bradj); |
715 |
+ |
for (x = cvts.xmax; x--; ) { |
716 |
+ |
d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16)); |
717 |
+ |
if (dogamma) d = pow(d, cvts.gamcor); |
718 |
+ |
if (cvts.bradj) d *= m; |
719 |
+ |
colval(cvts.r.colors[x],RED) = |
720 |
+ |
colval(cvts.r.colors[x],GRN) = |
721 |
+ |
colval(cvts.r.colors[x],BLU) = d; |
722 |
+ |
} |
723 |
+ |
if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
724 |
+ |
quiterr("error writing Radiance picture"); |
725 |
+ |
} |
726 |
+ |
|
727 |
+ |
|
728 |
+ |
int |
729 |
|
Color2L(y) /* read/convert/write COLOR->L16 scanline */ |
730 |
|
uint32 y; |
731 |
|
{ |
732 |
|
double m = pow(2.,(double)cvts.bradj); |
733 |
|
register int x; |
734 |
|
|
735 |
< |
if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
735 |
> |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) |
736 |
|
quiterr("internal error 1 in Color2L"); |
737 |
|
|
738 |
|
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
753 |
|
{ |
754 |
|
register int x; |
755 |
|
|
756 |
< |
if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY)) |
756 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) |
757 |
|
quiterr("internal error 1 in Color2Luv"); |
758 |
|
|
759 |
|
if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) |
786 |
|
{ |
787 |
|
register int x; |
788 |
|
|
789 |
< |
if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY)) |
789 |
> |
if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY) |
790 |
|
quiterr("internal error 1 in Colr2Gry"); |
791 |
|
|
792 |
|
if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |
813 |
|
COLOR ctmp; |
814 |
|
register int x; |
815 |
|
|
816 |
< |
if (CHK(C_RFLT|C_TFLT|C_GRY)) |
816 |
> |
if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY)) |
817 |
|
quiterr("internal error 1 in Colr2RGB"); |
818 |
|
|
819 |
|
if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) |