--- ray/src/px/ra_tiff.c 1998/10/27 09:08:27 2.17 +++ ray/src/px/ra_tiff.c 2003/02/22 02:07:28 2.20 @@ -1,25 +1,21 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: ra_tiff.c,v 2.20 2003/02/22 02:07:28 greg Exp $"; #endif - /* * Program to convert between RADIANCE and TIFF files. - * Added experimental LogLuv encodings 7/97 (GWL). + * Added LogLuv encodings 7/97 (GWL). + * Added white-balance adjustment 10/01 (GW). */ #include #include +#include #include "tiffio.h" #include "color.h" #include "resolu.h" #define GAMCOR 2.2 /* default gamma */ -#ifndef malloc -extern char *malloc(); -#endif /* conversion flags */ #define C_CXFM 0x1 /* needs color transformation */ #define C_GAMUT 0x2 /* needs gamut mapping */ @@ -28,7 +24,8 @@ extern char *malloc(); #define C_XYZE 0x10 /* Radiance is XYZE */ #define C_RFLT 0x20 /* Radiance data is float */ #define C_TFLT 0x40 /* TIFF data is float */ -#define C_PRIM 0x80 /* has assigned primaries */ +#define C_TWRD 0x80 /* TIFF data is 16-bit */ +#define C_PRIM 0x100 /* has assigned primaries */ struct { uint16 flags; /* conversion flags (defined above) */ @@ -52,6 +49,7 @@ struct { } r; /* Radiance scanline */ union { uint8 *bp; /* byte pointer */ + uint16 *wp; /* word pointer */ float *fp; /* float pointer */ char *p; /* generic pointer */ } t; /* TIFF scanline */ @@ -68,6 +66,7 @@ struct { int Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr(); int Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry(); +int RRGGBB2Color(), GGry2Color(); short ortab[8] = { /* orientation conversion table */ YMAJOR|YDECR, @@ -184,7 +183,8 @@ allocbufs() /* allocate scanline buffers */ int rsiz, tsiz; rsiz = CHK(C_RFLT) ? sizeof(COLOR) : sizeof(COLR); - tsiz = (CHK(C_TFLT) ? sizeof(float) : sizeof(uint8)) * + tsiz = (CHK(C_TFLT) ? sizeof(float) : + CHK(C_TWRD) ? sizeof(uint16) : sizeof(uint8)) * (CHK(C_GRY) ? 1 : 3); cvts.r.p = (char *)malloc(rsiz*cvts.xmax); cvts.t.p = (char *)malloc(tsiz*cvts.xmax); @@ -261,10 +261,10 @@ initfromtif() /* initialize conversion from TIFF inpu quiterr("unsupported photometric type"); /* fall through */ case PHOTOMETRIC_RGB: - SET(C_GAMMA|C_GAMUT); + SET(C_GAMMA); setcolrgam(cvts.gamcor); if (CHK(C_XYZE)) { - comprgb2xyzmat(cvts.cmat, + comprgb2xyzWBmat(cvts.cmat, CHK(C_PRIM) ? cvts.prims : stdprims); SET(C_CXFM); } @@ -272,21 +272,31 @@ initfromtif() /* initialize conversion from TIFF inpu hi != 3) quiterr("unsupported samples per pixel for RGB"); if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) || - hi != 8) + hi != 8 & hi != 16) quiterr("unsupported bits per sample for RGB"); - cvts.tf = RGB2Colr; + if (hi == 8) + cvts.tf = RGB2Colr; + else { + cvts.tf = RRGGBB2Color; + SET(C_RFLT|C_TWRD); + } break; case PHOTOMETRIC_MINISBLACK: - SET(C_GRY|C_GAMMA|C_GAMUT); + SET(C_GRY|C_GAMMA); setcolrgam(cvts.gamcor); cvts.pconf = PLANARCONFIG_CONTIG; if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 1) quiterr("unsupported samples per pixel for greyscale"); if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) || - hi != 8) + hi != 8 & hi != 16) quiterr("unsupported bits per sample for greyscale"); - cvts.tf = Gry2Colr; + if (hi == 8) + cvts.tf = Gry2Colr; + else { + cvts.tf = GGry2Color; + SET(C_RFLT|C_TWRD); + } break; default: quiterr("unsupported photometric type"); @@ -411,7 +421,8 @@ initfromrad() /* initialize input from a Radiance pi SET(C_RFLT|C_TFLT); CLR(C_GRY); if (!CHK(C_XYZE)) { - cpcolormat(cvts.cmat, rgb2xyzmat); + comprgb2xyzWBmat(cvts.cmat, + CHK(C_PRIM) ? cvts.prims : stdprims); SET(C_CXFM); } if (cvts.comp != COMPRESSION_SGILOG && @@ -434,7 +445,7 @@ initfromrad() /* initialize input from a Radiance pi CLR(C_GRY); setcolrgam(cvts.gamcor); if (CHK(C_XYZE)) { - compxyz2rgbmat(cvts.cmat, + compxyz2rgbWBmat(cvts.cmat, CHK(C_PRIM) ? cvts.prims : stdprims); SET(C_CXFM); } @@ -509,7 +520,7 @@ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY)) + if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) quiterr("internal error 1 in Luv2Color"); if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) @@ -538,12 +549,54 @@ uint32 y; int +RRGGBB2Color(y) /* read/convert/write RGB16->COLOR scanline */ +uint32 y; +{ + int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; + register double d; + register int x; + + if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_TWRD|C_RFLT)) + quiterr("internal error 1 in RRGGBB2Color"); + + if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) + quiterr("error reading TIFF input"); + + for (x = cvts.xmax; x--; ) { + d = (cvts.t.wp[3*x] + 0.5)*(1./(1L<<16)); + if (dogamma) d = pow(d, cvts.gamcor); + colval(cvts.r.colors[x],RED) = d; + d = (cvts.t.wp[3*x + 1] + 0.5)*(1./(1L<<16)); + if (dogamma) d = pow(d, cvts.gamcor); + colval(cvts.r.colors[x],GRN) = d; + d = (cvts.t.wp[3*x + 2] + 0.5)*(1./(1L<<16)); + if (dogamma) d = pow(d, cvts.gamcor); + colval(cvts.r.colors[x],BLU) = d; + if (CHK(C_CXFM)) + colortrans(cvts.r.colors[x], cvts.cmat, + cvts.r.colors[x]); + if (CHK(C_GAMUT)) + clipgamut(cvts.r.colors[x], cvts.t.fp[3*x + 1], + CGAMUT_LOWER, cblack, cwhite); + } + if (cvts.bradj) { + d = pow(2.,(double)cvts.bradj); + for (x = cvts.xmax; x--; ) + scalecolor(cvts.r.colors[x], d); + } + + if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) + quiterr("error writing Radiance picture"); +} + + +int L2Color(y) /* read/convert/write L16->COLOR scanline */ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) + if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) quiterr("internal error 1 in L2Color"); if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) @@ -567,7 +620,7 @@ uint32 y; COLOR ctmp; register int x; - if (CHK(C_RFLT|C_TFLT|C_GRY)) + if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY)) quiterr("internal error 1 in RGB2Colr"); if (cvts.pconf == PLANARCONFIG_CONTIG) { @@ -622,7 +675,7 @@ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY)) + if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY) quiterr("internal error 1 in Gry2Colr"); if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) @@ -643,13 +696,43 @@ uint32 y; int +GGry2Color(y) /* read/convert/write G16->COLOR scanline */ +uint32 y; +{ + int dogamma = cvts.gamcor < 0.99 | cvts.gamcor > 1.01; + double m; + register double d; + register int x; + + if (CHK(C_TFLT|C_TWRD|C_GRY|C_RFLT) != (C_GRY|C_RFLT|C_TWRD)) + quiterr("internal error 1 in GGry2Color"); + + if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) + quiterr("error reading TIFF input"); + + if (cvts.bradj) + m = pow(2., (double)cvts.bradj); + for (x = cvts.xmax; x--; ) { + d = (cvts.t.wp[x] + 0.5)*(1./(1L<<16)); + if (dogamma) d = pow(d, cvts.gamcor); + if (cvts.bradj) d *= m; + colval(cvts.r.colors[x],RED) = + colval(cvts.r.colors[x],GRN) = + colval(cvts.r.colors[x],BLU) = d; + } + if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) + quiterr("error writing Radiance picture"); +} + + +int Color2L(y) /* read/convert/write COLOR->L16 scanline */ uint32 y; { double m = pow(2.,(double)cvts.bradj); register int x; - if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) + if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) quiterr("internal error 1 in Color2L"); if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) @@ -670,7 +753,7 @@ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY)) + if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) quiterr("internal error 1 in Color2Luv"); if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) @@ -703,7 +786,7 @@ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY)) + if (CHK(C_RFLT|C_TWRD|C_TFLT|C_GRY) != C_GRY) quiterr("internal error 1 in Colr2Gry"); if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0) @@ -730,7 +813,7 @@ uint32 y; COLOR ctmp; register int x; - if (CHK(C_RFLT|C_TFLT|C_GRY)) + if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY)) quiterr("internal error 1 in Colr2RGB"); if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)