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;
196 npixels = occ / sp->pixel_size;
201 assert(sp->tbuflen >= npixels);
202 tp = (
int16*) sp->tbuf;
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);
251 npixels = occ / sp->pixel_size;
256 assert(sp->tbuflen >= npixels);
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;
296 npixels = occ / sp->pixel_size;
301 assert(sp->tbuflen >= npixels);
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;
384 npixels = cc / sp->pixel_size;
389 tp = (
int16*) sp->tbuf;
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) {
410 while (rc < 127+2 && beg+rc < npixels &&
411 (tp[beg+rc] & mask) ==
b)
416 if (beg-i > 1 && beg-i <
MINRUN) {
419 while ((tp[j++] & mask) ==
b)
429 if ((j = beg-i) > 127) j = 127;
470 npixels = cc / sp->pixel_size;
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;
518 npixels = cc / sp->pixel_size;
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);
922 float* xyz = (
float*) op;
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));
970 float* xyz = (
float*) op;
989 else if (luv3[0] >= (1<<12)+3314)
992 Le = (luv3[0]-3314) >> 2;
994 Le =
itrunc(.25*(luv3[0]-3314.), sp->encode_meth);
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);
1063 float* xyz = (
float*) op;
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));
1108 float* xyz = (
float*) op;
1124 *luv++ = (
uint32)luv3[0] << 16 |
1132 *luv++ = (
uint32)luv3[0] << 16 |
1133 (
itrunc(luv3[1]*(
UVSCALE/(1<<15)), sp->encode_meth) << 8 & 0xff00) |
1134 (
itrunc(luv3[2]*(
UVSCALE/(1<<15)), sp->encode_meth) & 0xff);
1148 #define PACK(s,b,f) (((b)<<6)|((s)<<3)|(f)) 1165 multiply(
size_t m1,
size_t m2)
1169 if (m1 && bytes / m1 != m2)
1176 LogL16InitState(
TIFF* tif)
1180 static const char module[] =
"LogL16InitState";
1187 sp->user_datafmt = LogL16GuessDataFmt(td);
1188 switch (sp->user_datafmt) {
1190 sp->pixel_size =
sizeof (float);
1193 sp->pixel_size =
sizeof (
int16);
1196 sp->pixel_size =
sizeof (
uint8);
1200 "No support for converting user data format to LogL");
1222 #define PACK(a,b) (((a)<<3)|(b)) 1266 LogLuvInitState(
TIFF* tif)
1270 static const char module[] =
"LogLuvInitState";
1278 "SGILog compression cannot handle non-contiguous data");
1282 sp->user_datafmt = LogLuvGuessDataFmt(td);
1283 switch (sp->user_datafmt) {
1285 sp->pixel_size = 3*
sizeof (float);
1288 sp->pixel_size = 3*
sizeof (
int16);
1291 sp->pixel_size =
sizeof (
uint32);
1294 sp->pixel_size = 3*
sizeof (
uint8);
1298 "No support for converting user data format to LogLuv");
1312 LogLuvSetupDecode(
TIFF* tif)
1320 if (!LogLuvInitState(tif))
1324 switch (sp->user_datafmt) {
1326 sp->tfunc = Luv24toXYZ;
1329 sp->tfunc = Luv24toLuv48;
1332 sp->tfunc = Luv24toRGB;
1337 switch (sp->user_datafmt) {
1339 sp->tfunc = Luv32toXYZ;
1342 sp->tfunc = Luv32toLuv48;
1345 sp->tfunc = Luv32toRGB;
1351 if (!LogL16InitState(tif))
1354 switch (sp->user_datafmt) {
1359 sp->tfunc = L16toGry;
1365 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1373 LogLuvSetupEncode(
TIFF* tif)
1380 if (!LogLuvInitState(tif))
1384 switch (sp->user_datafmt) {
1386 sp->tfunc = Luv24fromXYZ;
1389 sp->tfunc = Luv24fromLuv48;
1398 switch (sp->user_datafmt) {
1400 sp->tfunc = Luv32fromXYZ;
1403 sp->tfunc = Luv32fromLuv48;
1413 if (!LogL16InitState(tif))
1416 switch (sp->user_datafmt) {
1418 sp->tfunc = L16fromY;
1428 "Inappropriate photometric interpretation %d for SGILog compression; %s",
1435 "SGILog compression supported only for %s, or raw data",
1441 LogLuvClose(
TIFF* tif)
1459 LogLuvCleanup(
TIFF* tif)
1477 LogLuvVSetField(
TIFF* tif,
ttag_t tag, va_list ap)
1484 sp->user_datafmt = va_arg(ap,
int);
1491 switch (sp->user_datafmt) {
1507 "Unknown data format %d for LogLuv compression",
1520 sp->encode_meth = va_arg(ap,
int);
1524 "Unknown encoding %d for LogLuv compression",
1530 return (*sp->vsetparent)(tif, tag, ap);
1535 LogLuvVGetField(
TIFF* tif,
ttag_t tag, va_list ap)
1541 *va_arg(ap,
int*) = sp->user_datafmt;
1544 return (*sp->vgetparent)(tif, tag, ap);
1558 static const char module[] =
"TIFFInitSGILog";
1574 sp->tfunc = _logLuvNop;
1601 "%s: No space for LogLuv state block", tif->
tif_name);
TIFFCodeMethod tif_decoderow
TIFFTagMethods tif_tagmethods
tsize_t TIFFTileRowSize(TIFF *tif)
for(n=1;n< outline->n_points;n++)
#define EncoderState(tif)
#define SGILOGDATAFMT_RAW
#define TIFFTAG_BITSPERSAMPLE
void XYZtoRGB24(float xyz[3], uint8 rgb[3])
TIFFCodeMethod tif_encodestrip
GLint GLint GLint GLint GLint GLint y
double LogL10toY(int p10)
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)
GLint GLint GLint GLint GLint x
struct logLuvState LogLuvState
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
GLboolean GLboolean GLboolean b
#define SGILOGDATAFMT_FLOAT
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)
GLdouble GLdouble GLdouble r
#define SAMPLEFORMAT_UINT
uint32 LogLuv32fromXYZ(float XYZ[3], int em)
#define DecoderState(tif)
#define SAMPLEFORMAT_IEEEFP
TIFFPostMethod tif_postdecode
#define TIFFTAG_SGILOGENCODE
void _TIFFMergeFieldInfo(TIFF *tif, const TIFFFieldInfo info[], int n)
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