--- ray/src/px/ra_tiff.c 1998/10/08 11:21:52 2.16 +++ ray/src/px/ra_tiff.c 2004/01/02 12:47:01 2.27 @@ -1,25 +1,24 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: ra_tiff.c,v 2.27 2004/01/02 12:47:01 schorsch 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 +#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,10 +27,13 @@ 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) */ + char capdate[20]; /* capture date/time */ + char owner[256]; /* content owner */ uint16 comp; /* TIFF compression type */ uint16 phot; /* TIFF photometric type */ uint16 pconf; /* TIFF planar configuration */ @@ -52,12 +54,13 @@ struct { } r; /* Radiance scanline */ union { uint8 *bp; /* byte pointer */ + uint16 *wp; /* word pointer */ float *fp; /* float pointer */ char *p; /* generic pointer */ } t; /* TIFF scanline */ - int (*tf)(); /* translation procedure */ + void (*tf)(); /* translation procedure */ } cvts = { /* conversion structure */ - 0, COMPRESSION_NONE, PHOTOMETRIC_RGB, + 0, "", "", COMPRESSION_NONE, PHOTOMETRIC_RGB, PLANARCONFIG_CONTIG, GAMCOR, 0, 1, 1., 1., }; @@ -66,9 +69,15 @@ struct { #define CLR(f) (cvts.flags &= ~(f)) #define TGL(f) (cvts.flags ^= (f)) -int Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr(); -int Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry(); +void Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr(); +void Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry(); +void RRGGBB2Color(), GGry2Color(), Color2RRGGBB(), Color2GGry(); +#define RfGfBf2Color Luv2Color +#define Gryf2Color L2Color +#define Color2Gryf Color2L +#define Color2RfGfBf Color2Luv + short ortab[8] = { /* orientation conversion table */ YMAJOR|YDECR, YMAJOR|YDECR|XDECR, @@ -82,9 +91,14 @@ short ortab[8] = { /* orientation conversion table */ #define pixorder() ortab[cvts.orient-1] +extern char TMSTR[]; /* "CAPDATE=" from header.c */ +char OWNSTR[] = "OWNER="; + char *progname; +static gethfunc headline; + main(argc, argv) int argc; char *argv[]; @@ -114,6 +128,12 @@ char *argv[]; cvts.comp = COMPRESSION_SGILOG24; cvts.phot = PHOTOMETRIC_LOGLUV; break; + case 'w': /* 16-bit/primary output? */ + TGL(C_TWRD); + break; + case 'f': /* IEEE float output? */ + TGL(C_TFLT); + break; case 'b': /* greyscale output? */ TGL(C_GRY); break; @@ -144,14 +164,17 @@ doneopts: if (i != argc-2) goto userr; - - if (CHK(C_GRY)) /* consistency corrections */ + /* consistency checks */ + if (CHK(C_GRY)) { if (cvts.phot == PHOTOMETRIC_RGB) cvts.phot = PHOTOMETRIC_MINISBLACK; else { cvts.phot = PHOTOMETRIC_LOGL; cvts.comp = COMPRESSION_SGILOG; } + } + if (CHK(C_TWRD|C_TFLT) == (C_TWRD|C_TFLT)) + goto userr; ra2tiff(i, argv); } @@ -159,7 +182,7 @@ doneopts: exit(0); userr: fprintf(stderr, - "Usage: %s [-z|-L|-l][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n", + "Usage: %s [-z|-L|-l|-f|-w][-b][-e +/-stops][-g gamma] {in.pic|-} out.tif\n", progname); fprintf(stderr, " Or: %s -r [-x][-e +/-stops][-g gamma] in.tif [out.pic|-]\n", @@ -184,11 +207,12 @@ 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); - if (cvts.r.p == NULL | cvts.t.p == NULL) + if ((cvts.r.p == NULL) | (cvts.t.p == NULL)) quiterr("no memory to allocate scanline buffers"); } @@ -196,9 +220,10 @@ allocbufs() /* allocate scanline buffers */ initfromtif() /* initialize conversion from TIFF input */ { uint16 hi; + char *cp; float *fa, f1, f2; - CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_CXFM); + CLR(C_GRY|C_GAMMA|C_PRIM|C_RFLT|C_TFLT|C_TWRD|C_CXFM); TIFFGetFieldDefaulted(cvts.tif, TIFFTAG_PLANARCONFIG, &cvts.pconf); @@ -261,32 +286,58 @@ 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); } if (!TIFFGetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, &hi) || hi != 3) quiterr("unsupported samples per pixel for RGB"); - if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi) || - hi != 8) + if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi)) + hi = -1; + switch (hi) { + case 8: + cvts.tf = RGB2Colr; + break; + case 16: + cvts.tf = RRGGBB2Color; + SET(C_RFLT|C_TWRD); + break; + case 32: + cvts.tf = RfGfBf2Color; + SET(C_RFLT|C_TFLT); + break; + default: quiterr("unsupported bits per sample for RGB"); - cvts.tf = RGB2Colr; + } 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) - quiterr("unsupported bits per sample for greyscale"); - cvts.tf = Gry2Colr; + if (!TIFFGetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, &hi)) + hi = -1; + switch (hi) { + case 8: + cvts.tf = Gry2Colr; + break; + case 16: + cvts.tf = GGry2Color; + SET(C_RFLT|C_TWRD); + break; + case 32: + cvts.tf = Gryf2Color; + SET(C_RFLT|C_TFLT); + break; + default: + quiterr("unsupported bits per sample for Gray"); + } break; default: quiterr("unsupported photometric type"); @@ -299,6 +350,19 @@ initfromtif() /* initialize conversion from TIFF inpu if (!TIFFGetField(cvts.tif, TIFFTAG_STONITS, &cvts.stonits)) cvts.stonits = 1.; + + if (!TIFFGetField(cvts.tif, TIFFTAG_DATETIME, &cp)) + cvts.capdate[0] = '\0'; + else { + strncpy(cvts.capdate, cp, 19); + cvts.capdate[19] = '\0'; + } + if (!TIFFGetField(cvts.tif, TIFFTAG_ARTIST, &cp)) + cvts.owner[0] = '\0'; + else { + strncpy(cvts.owner, cp, sizeof(cvts.owner)); + cvts.owner[sizeof(cvts.owner)-1] = '\0'; + } /* add to Radiance header */ if (cvts.pixrat < .99 || cvts.pixrat > 1.01) fputaspect(cvts.pixrat, cvts.rfp); @@ -312,6 +376,10 @@ initfromtif() /* initialize conversion from TIFF inpu cvts.rfp); fputformat(COLRFMT, cvts.rfp); } + if (cvts.capdate[0]) + fprintf(cvts.rfp, "%s %s\n", TMSTR, cvts.capdate); + if (cvts.owner[0]) + fprintf(cvts.rfp, "%s %s\n", OWNSTR, cvts.owner); allocbufs(); /* allocate scanline buffers */ } @@ -347,12 +415,20 @@ char *av[]; } -int -headline(s) /* process Radiance input header line */ -char *s; +static int +headline( /* process Radiance input header line */ + char *s, + void *p +) { + static int tmstrlen = 0; + static int ownstrlen = 0; char fmt[32]; + if (!tmstrlen) + tmstrlen = strlen(TMSTR); + if (!ownstrlen) + ownstrlen = strlen(OWNSTR); if (formatval(fmt, s)) { if (!strcmp(fmt, COLRFMT)) CLR(C_XYZE); @@ -360,21 +436,43 @@ char *s; SET(C_XYZE); else quiterr("unrecognized input picture format"); - return; + return(1); } if (isexpos(s)) { cvts.stonits /= exposval(s); - return; + return(1); } if (isaspect(s)) { cvts.pixrat *= aspectval(s); - return; + return(1); } if (isprims(s)) { primsval(cvts.prims, s); SET(C_PRIM); - return; + return(1); } + if (isdate(s)) { + if (s[tmstrlen] == ' ') + strncpy(cvts.capdate, s+tmstrlen+1, 19); + else + strncpy(cvts.capdate, s+tmstrlen, 19); + cvts.capdate[19] = '\0'; + return(1); + } + if (!strncmp(s, OWNSTR, ownstrlen)) { + register char *cp = s + ownstrlen; + + while (isspace(*cp)) + ++cp; + strncpy(cvts.owner, cp, sizeof(cvts.owner)); + cvts.owner[sizeof(cvts.owner)-1] = '\0'; + for (cp = cvts.owner; *cp; cp++) + ; + while (cp > cvts.owner && isspace(cp[-1])) + *--cp = '\0'; + return(1); + } + return(0); } @@ -382,7 +480,9 @@ initfromrad() /* initialize input from a Radiance pi { int i1, i2, po; /* read Radiance header */ - CLR(C_RFLT|C_TFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM); + CLR(C_RFLT|C_XYZE|C_PRIM|C_GAMMA|C_CXFM); + cvts.capdate[0] = '\0'; + cvts.owner[0] = '\0'; cvts.stonits = 1.; cvts.pixrat = 1.; cvts.pconf = PLANARCONFIG_CONTIG; @@ -408,9 +508,10 @@ initfromrad() /* initialize input from a Radiance pi switch (cvts.phot) { case PHOTOMETRIC_LOGLUV: SET(C_RFLT|C_TFLT); - CLR(C_GRY); + CLR(C_GRY|C_TWRD); 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 && @@ -422,6 +523,7 @@ initfromrad() /* initialize input from a Radiance pi break; case PHOTOMETRIC_LOGL: SET(C_GRY|C_RFLT|C_TFLT); + CLR(C_TWRD); if (cvts.comp != COMPRESSION_SGILOG) quiterr("internal error 3 in initfromrad"); TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, @@ -433,7 +535,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); } @@ -443,12 +545,30 @@ initfromrad() /* initialize input from a Radiance pi TIFFSetField(cvts.tif, TIFFTAG_WHITEPOINT, (float *)cvts.prims[WHT]); } - cvts.tf = Colr2RGB; + if (CHK(C_TWRD)) { + cvts.tf = Color2RRGGBB; + SET(C_RFLT); + } else if (CHK(C_TFLT)) { + TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, + SAMPLEFORMAT_IEEEFP); + cvts.tf = Color2RfGfBf; + SET(C_RFLT); + } else + cvts.tf = Colr2RGB; break; case PHOTOMETRIC_MINISBLACK: SET(C_GRY|C_GAMMA|C_GAMUT); setcolrgam(cvts.gamcor); - cvts.tf = Colr2Gry; + if (CHK(C_TWRD)) { + cvts.tf = Color2GGry; + SET(C_RFLT); + } else if (CHK(C_TFLT)) { + TIFFSetField(cvts.tif, TIFFTAG_SAMPLEFORMAT, + SAMPLEFORMAT_IEEEFP); + cvts.tf = Color2Gryf; + SET(C_RFLT); + } else + cvts.tf = Colr2Gry; break; default: quiterr("internal error 4 in initfromrad"); @@ -458,7 +578,8 @@ initfromrad() /* initialize input from a Radiance pi TIFFSetField(cvts.tif, TIFFTAG_IMAGEWIDTH, cvts.xmax); TIFFSetField(cvts.tif, TIFFTAG_IMAGELENGTH, cvts.ymax); TIFFSetField(cvts.tif, TIFFTAG_SAMPLESPERPIXEL, CHK(C_GRY) ? 1 : 3); - TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, CHK(C_TFLT) ? 32 : 8); + TIFFSetField(cvts.tif, TIFFTAG_BITSPERSAMPLE, + CHK(C_TFLT) ? 32 : CHK(C_TWRD) ? 16 : 8); TIFFSetField(cvts.tif, TIFFTAG_XRESOLUTION, 72.); TIFFSetField(cvts.tif, TIFFTAG_YRESOLUTION, 72./cvts.pixrat); TIFFSetField(cvts.tif, TIFFTAG_ORIENTATION, cvts.orient); @@ -466,6 +587,10 @@ initfromrad() /* initialize input from a Radiance pi TIFFSetField(cvts.tif, TIFFTAG_PLANARCONFIG, cvts.pconf); TIFFSetField(cvts.tif, TIFFTAG_STONITS, cvts.stonits/pow(2.,(double)cvts.bradj)); + if (cvts.capdate[0]) + TIFFSetField(cvts.tif, TIFFTAG_DATETIME, cvts.capdate); + if (cvts.owner[0]) + TIFFSetField(cvts.tif, TIFFTAG_ARTIST, cvts.owner); if (cvts.comp == COMPRESSION_NONE) i1 = TIFFScanlineSize(cvts.tif); else @@ -502,22 +627,23 @@ char *av[]; } -int +void Luv2Color(y) /* read/convert/write Luv->COLOR scanline */ 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) quiterr("error reading TIFF input"); - + /* also works for float RGB */ for (x = cvts.xmax; x--; ) { - colval(cvts.r.colors[x],CIEX) = cvts.t.fp[3*x]; - colval(cvts.r.colors[x],CIEY) = cvts.t.fp[3*x + 1]; - colval(cvts.r.colors[x],CIEZ) = cvts.t.fp[3*x + 2]; + setcolor(cvts.r.colors[x], + cvts.t.fp[3*x], + cvts.t.fp[3*x + 1], + cvts.t.fp[3*x + 2]); if (CHK(C_CXFM)) colortrans(cvts.r.colors[x], cvts.cmat, cvts.r.colors[x]); @@ -536,37 +662,79 @@ uint32 y; } -int -L2Color(y) /* read/convert/write L16->COLOR scanline */ +void +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_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) - quiterr("internal error 1 in L2Color"); + 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--; ) - colval(cvts.r.colors[x],RED) = - colval(cvts.r.colors[x],GRN) = - colval(cvts.r.colors[x],BLU) = - cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.; + 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 +void +L2Color(y) /* read/convert/write Lfloat->COLOR scanline */ +uint32 y; +{ + float m = pow(2., (double)cvts.bradj); + register int x; + + 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) + quiterr("error reading TIFF input"); + /* also works for float greyscale */ + for (x = cvts.xmax; x--; ) { + register float f = cvts.t.fp[x]; + if (cvts.bradj) f *= m; + setcolor(cvts.r.colors[x], f, f, f); + } + if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) + quiterr("error writing Radiance picture"); +} + + +void RGB2Colr(y) /* read/convert/write RGB->COLR scanline */ 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) { @@ -615,13 +783,13 @@ readerr: } -int +void Gry2Colr(y) /* read/convert/write G8->COLR scanline */ 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) @@ -641,14 +809,78 @@ uint32 y; } -int -Color2L(y) /* read/convert/write COLOR->L16 scanline */ +void +GGry2Color(y) /* read/convert/write G16->COLOR scanline */ uint32 y; { - double m = pow(2.,(double)cvts.bradj); + int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); + double m; + register double d; register int x; - if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) + 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"); +} + + +void +Color2GGry(y) /* read/convert/write COLOR->G16 scanline */ +uint32 y; +{ + int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); + float m = pow(2.,(double)cvts.bradj); + register int x; + + if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD|C_GRY)) + quiterr("internal error 1 in Color2GGry"); + + if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) + quiterr("error reading Radiance picture"); + + for (x = cvts.xmax; x--; ) { + register float f = m*( CHK(C_XYZE) ? + colval(cvts.r.colors[x],CIEY) + : bright(cvts.r.colors[x]) ); + if (f <= 0) + cvts.t.wp[x] = 0; + else if (f >= 1) + cvts.t.wp[x] = 0xffff; + else if (dogamma) + cvts.t.wp[x] = (int)((float)(1L<<16) * + pow(f, 1./cvts.gamcor)); + else + cvts.t.wp[x] = (int)((float)(1L<<16) * f); + } + + if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) + quiterr("error writing TIFF output"); +} + + +void +Color2L(y) /* read/convert/write COLOR->Lfloat scanline */ +uint32 y; +{ + float m = pow(2.,(double)cvts.bradj); + register int x; + + 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) @@ -663,13 +895,13 @@ uint32 y; } -int +void Color2Luv(y) /* read/convert/write COLOR->Luv scanline */ 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) @@ -684,7 +916,7 @@ uint32 y; for (x = cvts.xmax; x--; ) scalecolor(cvts.r.colors[x], m); } - + /* also works for float RGB */ for (x = cvts.xmax; x--; ) { cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX); cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY); @@ -696,13 +928,46 @@ uint32 y; } -int +void +Color2RRGGBB(y) /* read/convert/write COLOR->RGB16 scanline */ +uint32 y; +{ + int dogamma = (cvts.gamcor < 0.99) | (cvts.gamcor > 1.01); + float m = pow(2.,(double)cvts.bradj); + register int x, i; + + if (CHK(C_RFLT|C_TFLT|C_TWRD|C_GRY) != (C_RFLT|C_TWRD)) + quiterr("internal error 1 in Color2RRGGBB"); + + if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) + quiterr("error reading Radiance picture"); + + for (x = cvts.xmax; x--; ) + for (i = 3; i--; ) { + register float f = m*colval(cvts.r.colors[x],i); + if (f <= 0) + cvts.t.wp[3*x + i] = 0; + else if (f >= 1) + cvts.t.wp[3*x + i] = 0xffff; + else if (dogamma) + cvts.t.wp[3*x + i] = (int)((float)(1L<<16) * + pow(f, 1./cvts.gamcor)); + else + cvts.t.wp[3*x + i] = (int)((float)(1L<<16)*f); + } + + if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) + quiterr("error writing TIFF output"); +} + + +void Colr2Gry(y) /* read/convert/write COLR->RGB scanline */ 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) @@ -722,14 +987,14 @@ uint32 y; } -int +void Colr2RGB(y) /* read/convert/write COLR->RGB scanline */ 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)