173 #define DecoderState(tif) ((LogLuvState*) (tif)->tif_data) 174 #define EncoderState(tif) ((LogLuvState*) (tif)->tif_data) 176 #define SGILOGDATAFMT_UNKNOWN -1 187 int shft, i, npixels;
201 assert(sp->
tbuflen >= npixels);
209 for (shft = 2*8; (shft -= 8) >= 0; ) {
210 for (i = 0; i < npixels && cc > 0; )
212 rc = *bp++ + (2-128);
213 b = (
int16)(*bp++ << shft);
215 while (rc-- && i < npixels)
219 while (--cc && rc-- && i < npixels)
220 tp[i++] |= (
int16)*bp++ << shft;
224 "LogL16Decode: Not enough data at row %d (short %d pixels)",
231 (*sp->
tfunc)(sp, op, npixels);
256 assert(sp->
tbuflen >= npixels);
257 tp = (uint32 *) sp->
tbuf;
262 for (i = 0; i < npixels && cc > 0; i++) {
263 tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2];
271 "LogLuvDecode24: Not enough data at row %d (short %d pixels)",
275 (*sp->
tfunc)(sp, op, npixels);
286 int shft, i, npixels;
301 assert(sp->
tbuflen >= npixels);
302 tp = (uint32*) sp->
tbuf;
309 for (shft = 4*8; (shft -= 8) >= 0; ) {
310 for (i = 0; i < npixels && cc > 0; )
312 rc = *bp++ + (2-128);
313 b = (uint32)*bp++ << shft;
315 while (rc-- && i < npixels)
319 while (--cc && rc-- && i < npixels)
320 tp[i++] |= (uint32)*bp++ << shft;
324 "LogLuvDecode32: Not enough data at row %d (short %d pixels)",
331 (*sp->
tfunc)(sp, op, npixels);
347 assert(cc%rowlen == 0);
349 bp += rowlen, cc -= rowlen;
363 assert(cc%rowlen == 0);
365 bp += rowlen, cc -= rowlen;
376 int shft, i, j, npixels;
380 int occ, rc=0, mask, beg;
390 assert(sp->
tbuflen >= npixels);
391 (*sp->
tfunc)(sp, bp, npixels);
396 for (shft = 2*8; (shft -= 8) >= 0; )
397 for (i = 0; i < npixels; i += rc) {
407 for (beg = i; beg < npixels; beg += rc) {
408 b = (
int16) (tp[beg] & mask);
410 while (rc < 127+2 && beg+rc < npixels &&
411 (tp[beg+rc] & mask) ==
b)
416 if (beg-i > 1 && beg-i <
MINRUN) {
417 b = (
int16) (tp[i] & mask);
419 while ((tp[j++] & mask) ==
b)
429 if ((j = beg-i) > 127) j = 127;
475 tp = (uint32*) sp->
tbuf;
476 assert(sp->
tbuflen >= npixels);
477 (*sp->
tfunc)(sp, bp, npixels);
482 for (i = npixels; i--; ) {
509 int shft, i, j, npixels;
513 int occ, rc=0, mask, beg;
523 tp = (uint32*) sp->
tbuf;
524 assert(sp->
tbuflen >= npixels);
525 (*sp->
tfunc)(sp, bp, npixels);
530 for (shft = 4*8; (shft -= 8) >= 0; )
531 for (i = 0; i < npixels; i += rc) {
541 for (beg = i; beg < npixels; beg += rc) {
544 while (rc < 127+2 && beg+rc < npixels &&
545 (tp[beg+rc] & mask) ==
b)
550 if (beg-i > 1 && beg-i <
MINRUN) {
553 while ((tp[j++] & mask) ==
b)
563 if ((j = beg-i) > 127) j = 127;
600 assert(cc%rowlen == 0);
602 bp += rowlen, cc -= rowlen;
615 assert(cc%rowlen == 0);
617 bp += rowlen, cc -= rowlen;
628 #define U_NEU 0.210526316 629 #define V_NEU 0.473684211 634 #define M_LN2 0.69314718055994530942 637 #define M_PI 3.14159265358979323846 639 #define log2(x) ((1./M_LN2)*log(x)) 640 #define exp2(x) exp(M_LN2*(x)) 642 #define itrunc(x,m) ((m)==SGILOGENCODE_NODITHER ? \ 644 (int)((x) + rand()*(1./RAND_MAX) - .5)) 652 int Le = p16 & 0x7fff;
658 return (!(p16 & 0x8000) ? Y : -Y);
667 if (Y >= 1.8371976e19)
669 if (Y <= -1.8371976e19)
671 if (Y > 5.4136769e-20)
673 if (Y < -5.4136769e-20)
674 return (~0x7fff |
itrunc(256.*(
log2(-Y) + 64.), em));
682 float* yp = (
float*) op;
696 *gp++ = (
uint8) ((Y <= 0.) ? 0 : (Y >= 1.) ? 255 : (int)(256.*sqrt(Y)));
704 float* yp = (
float*) op;
718 r = 2.690*xyz[0] + -1.276*xyz[1] + -0.414*xyz[2];
719 g = -1.022*xyz[0] + 1.978*xyz[1] + 0.044*xyz[2];
720 b = 0.061*xyz[0] + -0.224*xyz[1] + 1.163*xyz[2];
723 rgb[0] = (
uint8)((r<=0.) ? 0 : (r >= 1.) ? 255 : (int)(256.*sqrt(r)));
724 rgb[1] = (
uint8)((g<=0.) ? 0 : (g >= 1.) ? 255 : (int)(256.*sqrt(g)));
725 rgb[2] = (
uint8)((b<=0.) ? 0 : (b >= 1.) ? 255 : (int)(256.*sqrt(b)));
747 else if (Y <= .00024283)
754 #define uv2ang(u, v) ( (NANGLES*.499999999/M_PI) \ 755 * atan2((v)-V_NEU,(u)-U_NEU) + .5*NANGLES ) 758 oog_encode(
double u,
double v)
761 static int initialized = 0;
765 double eps[
NANGLES], ua, va, ang, epsa;
769 for (vi =
UV_NVS; vi--; ) {
771 ustep = uv_row[vi].nus-1;
772 if (vi ==
UV_NVS-1 || vi == 0 || ustep <= 0)
774 for (ui = uv_row[vi].
nus-1; ui >= 0; ui -= ustep) {
775 ua = uv_row[vi].ustart + (ui+.5)*
UV_SQSIZ;
778 epsa = fabs(ang - (i+.5));
780 oog_table[i] = uv_row[vi].ncum + ui;
788 for (i1 = 1; i1 <
NANGLES/2; i1++)
791 for (i2 = 1; i2 <
NANGLES/2; i2++)
804 return (oog_table[i]);
819 return oog_encode(u, v);
822 return oog_encode(u, v);
823 if (u < uv_row[vi].
ustart)
824 return oog_encode(u, v);
826 if (ui >= uv_row[vi].
nus)
827 return oog_encode(u, v);
829 return (uv_row[vi].
ncum + ui);
845 while (upper - lower > 1) {
846 vi = (lower + upper) >> 1;
847 ui = c - uv_row[vi].ncum;
858 ui = c - uv_row[vi].ncum;
859 *up = uv_row[vi].ustart + (ui+.5)*
UV_SQSIZ;
871 double L, u, v, s, x, y;
875 XYZ[0] = XYZ[1] = XYZ[2] = 0.;
883 s = 1./(6.*u - 16.*v + 12.);
887 XYZ[0] = (float)(x/y * L);
889 XYZ[2] = (float)((1.-x-y)/y * L);
903 s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2];
904 if (!Le || s <= 0.) {
915 return (Le << 14 | Ce);
921 uint32* luv = (uint32*) sp->
tbuf;
922 float* xyz = (
float*) op;
934 uint32* luv = (uint32*) sp->
tbuf;
940 *luv3++ = (
int16)((*luv >> 12 & 0xffd) + 13314);
941 if (
uv_decode(&u, &v, *luv&0x3fff) < 0) {
945 *luv3++ = (
int16)(u * (1L<<15));
946 *luv3++ = (
int16)(v * (1L<<15));
954 uint32* luv = (uint32*) sp->
tbuf;
969 uint32* luv = (uint32*) sp->
tbuf;
970 float* xyz = (
float*) op;
981 uint32* luv = (uint32*) sp->
tbuf;
989 else if (luv3[0] >= (1<<12)+3314)
992 Le = (luv3[0]-3314) >> 2;
996 Ce =
uv_encode((luv3[1]+.5)/(1<<15), (luv3[2]+.5)/(1<<15),
1000 *luv++ = (uint32)Le << 14 | Ce;
1011 double L, u, v, s, x, y;
1015 XYZ[0] = XYZ[1] = XYZ[2] = 0.;
1019 u = 1./
UVSCALE * ((p>>8 & 0xff) + .5);
1020 v = 1./
UVSCALE * ((p & 0xff) + .5);
1021 s = 1./(6.*u - 16.*v + 12.);
1025 XYZ[0] = (float)(x/y * L);
1027 XYZ[2] = (float)((1.-x-y)/y * L);
1036 unsigned int Le, ue, ve;
1041 s = XYZ[0] + 15.*XYZ[1] + 3.*XYZ[2];
1042 if (!Le || s <= 0.) {
1049 if (u <= 0.) ue = 0;
1051 if (ue > 255) ue = 255;
1052 if (v <= 0.) ve = 0;
1054 if (ve > 255) ve = 255;
1056 return (Le << 16 | ue << 8 | ve);
1062 uint32* luv = (uint32*) sp->
tbuf;
1063 float* xyz = (
float*) op;
1074 uint32* luv = (uint32*) sp->
tbuf;
1080 *luv3++ = (
int16)(*luv >> 16);
1081 u = 1./
UVSCALE * ((*luv>>8 & 0xff) + .5);
1082 v = 1./
UVSCALE * ((*luv & 0xff) + .5);
1083 *luv3++ = (
int16)(u * (1L<<15));
1084 *luv3++ = (
int16)(v * (1L<<15));
1092 uint32* luv = (uint32*) sp->
tbuf;
1107 uint32* luv = (uint32*) sp->
tbuf;
1108 float* xyz = (
float*) op;
1119 uint32* luv = (uint32*) sp->
tbuf;
1124 *luv++ = (uint32)luv3[0] << 16 |
1125 (luv3[1]*(uint32)(
UVSCALE+.5) >> 7 & 0xff00) |
1126 (luv3[2]*(uint32)(
UVSCALE+.5) >> 15 & 0xff);
1132 *luv++ = (uint32)luv3[0] << 16 |
1142 (void) sp; (void) op; (void) n;
1148 #define PACK(s,b,f) (((b)<<6)|((s)<<3)|(f)) 1165 multiply(
size_t m1,
size_t m2)
1167 uint32 bytes = m1 * m2;
1169 if (m1 && bytes / m1 != m2)
1176 LogL16InitState(
TIFF* tif)
1180 static const char module[] =
"LogL16InitState";
1200 "No support for converting user data format to LogL");
1225 #define PACK(a,b) (((a)<<3)|(b)) 1269 LogLuvInitState(
TIFF* tif)
1273 static const char module[] =
"LogLuvInitState";
1281 "SGILog compression cannot handle non-contiguous data");
1301 "No support for converting user data format to LogLuv");
1308 if (multiply(sp->
tbuflen, sizeof (uint32)) == 0 ||
1318 LogLuvSetupDecode(
TIFF* tif)
1326 if (!LogLuvInitState(tif))
1332 sp->
tfunc = Luv24toXYZ;
1335 sp->
tfunc = Luv24toLuv48;
1338 sp->
tfunc = Luv24toRGB;
1345 sp->
tfunc = Luv32toXYZ;
1348 sp->
tfunc = Luv32toLuv48;
1351 sp->
tfunc = Luv32toRGB;
1357 if (!LogL16InitState(tif))
1365 sp->
tfunc = L16toGry;
1371 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1379 LogLuvSetupEncode(
TIFF* tif)
1386 if (!LogLuvInitState(tif))
1392 sp->
tfunc = Luv24fromXYZ;
1395 sp->
tfunc = Luv24fromLuv48;
1406 sp->
tfunc = Luv32fromXYZ;
1409 sp->
tfunc = Luv32fromLuv48;
1419 if (!LogL16InitState(tif))
1424 sp->
tfunc = L16fromY;
1434 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1441 "SGILog compression supported only for %s, or raw data",
1447 LogLuvClose(
TIFF* tif)
1465 LogLuvCleanup(
TIFF* tif)
1483 LogLuvVSetField(
TIFF* tif,
ttag_t tag, va_list ap)
1513 "Unknown data format %d for LogLuv compression",
1530 "Unknown encoding %d for LogLuv compression",
1541 LogLuvVGetField(
TIFF* tif,
ttag_t tag, va_list ap)
1564 static const char module[] =
"TIFFInitSGILog";
1575 "Merging SGILog codec-specific tags failed");
1590 sp->
tfunc = _logLuvNop;
1617 "%s: No space for LogLuv state block", tif->
tif_name);
TIFFCodeMethod tif_decoderow
TIFFTagMethods tif_tagmethods
tsize_t TIFFTileRowSize(TIFF *tif)
#define EncoderState(tif)
#define SGILOGDATAFMT_RAW
#define TIFFTAG_BITSPERSAMPLE
void XYZtoRGB24(float xyz[3], uint8 rgb[3])
TIFFCodeMethod tif_encodestrip
double LogL10toY(int p10)
TIFFVSetMethod vsetparent
TIFFCodeMethod tif_encoderow
#define COMPRESSION_SGILOG24
#define TIFFTAG_SGILOGDATAFMT
tsize_t TIFFScanlineSize(TIFF *tif)
#define PHOTOMETRIC_LOGLUV
int uv_decode(double *up, double *vp, int c)
#define SGILOGDATAFMT_FLOAT
void(* tfunc)(LogLuvState *, tidata_t, int)
TIFFCodeMethod tif_decodestrip
#define TIFFTAG_SAMPLESPERPIXEL
TIFFCodeMethod tif_encodetile
#define SAMPLEFORMAT_VOID
int LogL16fromY(double Y, int em)
uint32 LogLuv24fromXYZ(float XYZ[3], int em)
TIFFVSetMethod vgetparent
#define SAMPLEFORMAT_UINT
uint32 LogLuv32fromXYZ(float XYZ[3], int em)
#define DecoderState(tif)
#define SAMPLEFORMAT_IEEEFP
TIFFPostMethod tif_postdecode
#define TIFFTAG_SGILOGENCODE
int _TIFFMergeFieldInfo(TIFF *, const TIFFFieldInfo[], int)
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
TIFFCodeMethod tif_decodetile
#define COMPRESSION_SGILOG
#define SGILOGENCODE_NODITHER
TIFFVoidMethod tif_cleanup
#define TIFFArrayCount(a)
void _TIFFmemset(tdata_t p, int v, tsize_t c)
#define SGILOGDATAFMT_16BIT
void _TIFFSetDefaultCompressionState(TIFF *tif)
void LogLuv24toXYZ(uint32 p, float XYZ[3])
#define SGILOGDATAFMT_8BIT
void _TIFFNoPostDecode(TIFF *tif, tidata_t buf, tsize_t cc)
uint16 td_samplesperpixel
unsigned char tidataval_t
#define SGILOGDATAFMT_UNKNOWN
#define SGILOGENCODE_RANDITHER
tsize_t TIFFTileSize(TIFF *tif)
int TIFFInitSGILog(TIFF *tif, int scheme)
int TIFFSetField(TIFF *tif, ttag_t tag,...)
void * _TIFFmalloc(tsize_t s)
void LogLuv32toXYZ(uint32 p, float XYZ[3])
void _TIFFfree(tdata_t p)
double LogL16toY(int p16)
#define TIFFTAG_SAMPLEFORMAT
int(* TIFFVSetMethod)(TIFF *, ttag_t, va_list)
TIFFBoolMethod tif_setupencode
int uv_encode(double u, double v, int em)
int TIFFFlushData1(TIFF *tif)
#define PLANARCONFIG_CONTIG
int LogL10fromY(double Y, int em)
TIFFBoolMethod tif_setupdecode