--- ray/src/px/ra_tiff.c 1997/07/31 21:26:45 2.10 +++ ray/src/px/ra_tiff.c 1999/05/30 17:35:08 2.18 @@ -38,7 +38,7 @@ struct { float gamcor; /* gamma correction value */ short bradj; /* Radiance exposure adjustment (stops) */ uint16 orient; /* visual orientation (TIFF spec.) */ - float stonits; /* input conversion to nits */ + double stonits; /* input conversion to nits */ float pixrat; /* pixel aspect ratio */ FILE *rfp; /* Radiance stream pointer */ TIFF *tif; /* TIFF pointer */ @@ -52,6 +52,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 +69,7 @@ struct { int Luv2Color(), L2Color(), RGB2Colr(), Gry2Colr(); int Color2Luv(), Color2L(), Colr2RGB(), Colr2Gry(); +int RRGGBB2Color(), GGry2Color(); short ortab[8] = { /* orientation conversion table */ YMAJOR|YDECR, @@ -241,18 +243,27 @@ initfromtif() /* initialize conversion from TIFF inpu if (cvts.pconf != PLANARCONFIG_CONTIG) quiterr("cannot handle separate Luv planes"); TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, - SGILOGDATAFMT_FLTXYZ); + SGILOGDATAFMT_FLOAT); cvts.tf = Luv2Color; break; case PHOTOMETRIC_LOGL: SET(C_GRY|C_RFLT|C_TFLT|C_GAMUT); cvts.pconf = PLANARCONFIG_CONTIG; TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, - SGILOGDATAFMT_FLTY); + SGILOGDATAFMT_FLOAT); cvts.tf = L2Color; break; + case PHOTOMETRIC_YCBCR: + if (cvts.comp == COMPRESSION_JPEG && + cvts.pconf == PLANARCONFIG_CONTIG) { + TIFFSetField(cvts.tif, TIFFTAG_JPEGCOLORMODE, + JPEGCOLORMODE_RGB); + cvts.phot = PHOTOMETRIC_RGB; + } else + 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, @@ -263,20 +274,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); + } 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); + } break; default: quiterr("unsupported photometric type"); @@ -350,21 +372,22 @@ 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); } + return(0); } @@ -407,7 +430,7 @@ initfromrad() /* initialize input from a Radiance pi cvts.comp != COMPRESSION_SGILOG24) quiterr("internal error 2 in initfromrad"); TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, - SGILOGDATAFMT_FLTXYZ); + SGILOGDATAFMT_FLOAT); cvts.tf = Color2Luv; break; case PHOTOMETRIC_LOGL: @@ -415,7 +438,7 @@ initfromrad() /* initialize input from a Radiance pi if (cvts.comp != COMPRESSION_SGILOG) quiterr("internal error 3 in initfromrad"); TIFFSetField(cvts.tif, TIFFTAG_SGILOGDATAFMT, - SGILOGDATAFMT_FLTY); + SGILOGDATAFMT_FLOAT); cvts.tf = Color2L; break; case PHOTOMETRIC_RGB: @@ -498,16 +521,16 @@ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY)) + if (CHK(C_RFLT|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"); for (x = cvts.xmax; x--; ) { - cvts.r.colors[x][RED] = cvts.t.fp[3*x]; - cvts.r.colors[x][GRN] = cvts.t.fp[3*x + 1]; - cvts.r.colors[x][BLU] = cvts.t.fp[3*x + 2]; + 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]; if (CHK(C_CXFM)) colortrans(cvts.r.colors[x], cvts.cmat, cvts.r.colors[x]); @@ -527,6 +550,48 @@ 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_TFLT|C_GRY) != 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; { @@ -539,9 +604,10 @@ uint32 y; quiterr("error reading TIFF input"); for (x = cvts.xmax; x--; ) - cvts.r.colors[x][RED] = - cvts.r.colors[x][GRN] = - cvts.r.colors[x][BLU] = cvts.t.fp[x] > 0. ? cvts.t.fp[x] : 0.; + 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.; if (fwritescan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) quiterr("error writing Radiance picture"); @@ -570,10 +636,10 @@ uint32 y; if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) goto readerr; if (TIFFReadScanline(cvts.tif, - (tidata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0) + (tdata_t)(cvts.t.bp + cvts.xmax), y, 1) < 0) goto readerr; if (TIFFReadScanline(cvts.tif, - (tidata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0) + (tdata_t)(cvts.t.bp + 2*cvts.xmax), y, 2) < 0) goto readerr; for (x = cvts.xmax; x--; ) { cvts.r.colrs[x][RED] = cvts.t.bp[x]; @@ -610,7 +676,7 @@ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY)) + if (CHK(C_RFLT|C_TFLT|C_GRY) != C_GRY) quiterr("internal error 1 in Gry2Colr"); if (TIFFReadScanline(cvts.tif, cvts.t.p, y, 0) < 0) @@ -631,13 +697,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_GRY|C_RFLT) != (C_GRY|C_RFLT)) + 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_RFLT|C_TFLT) | !CHK(C_GRY)) + if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT|C_GRY)) quiterr("internal error 1 in Color2L"); if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) @@ -658,7 +754,7 @@ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT) != (C_RFLT|C_TFLT) | CHK(C_GRY)) + if (CHK(C_RFLT|C_TFLT|C_GRY) != (C_RFLT|C_TFLT)) quiterr("internal error 1 in Color2Luv"); if (freadscan(cvts.r.colors, cvts.xmax, cvts.rfp) < 0) @@ -675,9 +771,9 @@ uint32 y; } for (x = cvts.xmax; x--; ) { - cvts.t.fp[3*x] = colval(cvts.r.colors[x],RED); - cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],GRN); - cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],BLU); + cvts.t.fp[3*x] = colval(cvts.r.colors[x],CIEX); + cvts.t.fp[3*x+1] = colval(cvts.r.colors[x],CIEY); + cvts.t.fp[3*x+2] = colval(cvts.r.colors[x],CIEZ); } if (TIFFWriteScanline(cvts.tif, cvts.t.p, y, 0) < 0) @@ -691,7 +787,7 @@ uint32 y; { register int x; - if (CHK(C_RFLT|C_TFLT) | !CHK(C_GRY)) + if (CHK(C_RFLT|C_TFLT|C_GRY) != C_GRY) quiterr("internal error 1 in Colr2Gry"); if (freadcolrs(cvts.r.colrs, cvts.xmax, cvts.rfp) < 0)