28 #ifdef PIXARLOG_SUPPORT 103 #define CODE_MASK 0x7ff 105 static float Fltsize;
106 static float LogK1, LogK2;
108 #define REPEAT(n, op) { int i; i=n; do { i--; op; } while (i>0); } 111 horizontalAccumulateF(
uint16 *wp,
int n,
int stride,
float *op,
114 register unsigned int cr, cg, cb, ca, mask;
115 register float t0, t1, t2, t3;
120 t0 = ToLinearF[cr = wp[0]];
121 t1 = ToLinearF[cg = wp[1]];
122 t2 = ToLinearF[cb = wp[2]];
131 t0 = ToLinearF[(cr += wp[0]) & mask];
132 t1 = ToLinearF[(cg += wp[1]) & mask];
133 t2 = ToLinearF[(cb += wp[2]) & mask];
138 }
else if (stride == 4) {
139 t0 = ToLinearF[cr = wp[0]];
140 t1 = ToLinearF[cg = wp[1]];
141 t2 = ToLinearF[cb = wp[2]];
142 t3 = ToLinearF[ca = wp[3]];
152 t0 = ToLinearF[(cr += wp[0]) & mask];
153 t1 = ToLinearF[(cg += wp[1]) & mask];
154 t2 = ToLinearF[(cb += wp[2]) & mask];
155 t3 = ToLinearF[(ca += wp[3]) & mask];
162 REPEAT(stride, *op = ToLinearF[*wp&mask]; wp++; op++)
166 wp[stride] += *wp; *op = ToLinearF[*wp&mask]; wp++; op++)
174 horizontalAccumulate12(
uint16 *wp,
int n,
int stride,
int16 *op,
177 register unsigned int cr, cg, cb, ca, mask;
178 register float t0, t1, t2, t3;
180 #define SCALE12 2048.0F 181 #define CLAMP12(t) (((t) < 3071) ? (uint16) (t) : 3071) 186 t0 = ToLinearF[cr = wp[0]] *
SCALE12;
187 t1 = ToLinearF[cg = wp[1]] *
SCALE12;
188 t2 = ToLinearF[cb = wp[2]] *
SCALE12;
197 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
198 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
199 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
204 }
else if (stride == 4) {
205 t0 = ToLinearF[cr = wp[0]] *
SCALE12;
206 t1 = ToLinearF[cg = wp[1]] *
SCALE12;
207 t2 = ToLinearF[cb = wp[2]] *
SCALE12;
208 t3 = ToLinearF[ca = wp[3]] *
SCALE12;
218 t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
219 t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
220 t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
221 t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
233 wp[stride] += *wp; t0 = ToLinearF[wp[stride]&mask]*
SCALE12;
242 horizontalAccumulate16(
uint16 *wp,
int n,
int stride,
uint16 *op,
245 register unsigned int cr, cg, cb, ca, mask;
250 op[0] = ToLinear16[cr = wp[0]];
251 op[1] = ToLinear16[cg = wp[1]];
252 op[2] = ToLinear16[cb = wp[2]];
258 op[0] = ToLinear16[(cr += wp[0]) & mask];
259 op[1] = ToLinear16[(cg += wp[1]) & mask];
260 op[2] = ToLinear16[(cb += wp[2]) & mask];
262 }
else if (stride == 4) {
263 op[0] = ToLinear16[cr = wp[0]];
264 op[1] = ToLinear16[cg = wp[1]];
265 op[2] = ToLinear16[cb = wp[2]];
266 op[3] = ToLinear16[ca = wp[3]];
272 op[0] = ToLinear16[(cr += wp[0]) & mask];
273 op[1] = ToLinear16[(cg += wp[1]) & mask];
274 op[2] = ToLinear16[(cb += wp[2]) & mask];
275 op[3] = ToLinear16[(ca += wp[3]) & mask];
278 REPEAT(stride, *op = ToLinear16[*wp&mask]; wp++; op++)
282 wp[stride] += *wp; *op = ToLinear16[*wp&mask]; wp++; op++)
294 horizontalAccumulate11(
uint16 *wp,
int n,
int stride,
uint16 *op)
296 register unsigned int cr, cg, cb, ca, mask;
301 op[0] = cr = wp[0]; op[1] = cg = wp[1]; op[2] = cb = wp[2];
307 op[0] = (cr += wp[0]) & mask;
308 op[1] = (cg += wp[1]) & mask;
309 op[2] = (cb += wp[2]) & mask;
311 }
else if (stride == 4) {
312 op[0] = cr = wp[0]; op[1] = cg = wp[1];
313 op[2] = cb = wp[2]; op[3] = ca = wp[3];
319 op[0] = (cr += wp[0]) & mask;
320 op[1] = (cg += wp[1]) & mask;
321 op[2] = (cb += wp[2]) & mask;
322 op[3] = (ca += wp[3]) & mask;
325 REPEAT(stride, *op = *wp&mask; wp++; op++)
329 wp[stride] += *wp; *op = *wp&mask; wp++; op++)
337 horizontalAccumulate8(
uint16 *wp,
int n,
int stride,
unsigned char *op,
338 unsigned char *ToLinear8)
340 register unsigned int cr, cg, cb, ca, mask;
345 op[0] = ToLinear8[cr = wp[0]];
346 op[1] = ToLinear8[cg = wp[1]];
347 op[2] = ToLinear8[cb = wp[2]];
353 op[0] = ToLinear8[(cr += wp[0]) & mask];
354 op[1] = ToLinear8[(cg += wp[1]) & mask];
355 op[2] = ToLinear8[(cb += wp[2]) & mask];
357 }
else if (stride == 4) {
358 op[0] = ToLinear8[cr = wp[0]];
359 op[1] = ToLinear8[cg = wp[1]];
360 op[2] = ToLinear8[cb = wp[2]];
361 op[3] = ToLinear8[ca = wp[3]];
367 op[0] = ToLinear8[(cr += wp[0]) & mask];
368 op[1] = ToLinear8[(cg += wp[1]) & mask];
369 op[2] = ToLinear8[(cb += wp[2]) & mask];
370 op[3] = ToLinear8[(ca += wp[3]) & mask];
373 REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
377 wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
386 horizontalAccumulate8abgr(
uint16 *wp,
int n,
int stride,
unsigned char *op,
387 unsigned char *ToLinear8)
389 register unsigned int cr, cg, cb, ca, mask;
390 register unsigned char t0, t1, t2, t3;
396 t1 = ToLinear8[cb = wp[2]];
397 t2 = ToLinear8[cg = wp[1]];
398 t3 = ToLinear8[cr = wp[0]];
408 t1 = ToLinear8[(cb += wp[2]) & mask];
409 t2 = ToLinear8[(cg += wp[1]) & mask];
410 t3 = ToLinear8[(cr += wp[0]) & mask];
415 }
else if (stride == 4) {
416 t0 = ToLinear8[ca = wp[3]];
417 t1 = ToLinear8[cb = wp[2]];
418 t2 = ToLinear8[cg = wp[1]];
419 t3 = ToLinear8[cr = wp[0]];
429 t0 = ToLinear8[(ca += wp[3]) & mask];
430 t1 = ToLinear8[(cb += wp[2]) & mask];
431 t2 = ToLinear8[(cg += wp[1]) & mask];
432 t3 = ToLinear8[(cr += wp[0]) & mask];
439 REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
443 wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
462 #define PLSTATE_INIT 1 493 double b, c, linstep, v;
496 unsigned char *ToLinear8;
505 linstep = b*c*exp(1.);
507 LogK1 = (float)(1./c);
508 LogK2 = (float)(1./b);
509 lt2size = (int)(2./linstep) + 1;
516 if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
517 ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL) {
535 for (i = 0; i < nlin; i++) {
537 ToLinearF[j++] = (float)v;
540 for (i = nlin; i <
TSIZE; i++)
541 ToLinearF[j++] = (
float)(b*exp(c*i));
543 ToLinearF[2048] = ToLinearF[2047];
545 for (i = 0; i <
TSIZEP1; i++) {
546 v = ToLinearF[i]*65535.0 + 0.5;
547 ToLinear16[i] = (v > 65535.0) ? 65535 : (
uint16)v;
548 v = ToLinearF[i]*255.0 + 0.5;
549 ToLinear8[i] = (v > 255.0) ? 255 : (
unsigned char)v;
553 for (i = 0; i < lt2size; i++) {
554 if ((i*linstep)*(i*linstep) > ToLinearF[j]*ToLinearF[j+1])
565 for (i = 0; i < 16384; i++) {
566 while ((i/16383.)*(i/16383.) > ToLinearF[j]*ToLinearF[j+1])
572 for (i = 0; i < 256; i++) {
573 while ((i/255.)*(i/255.) > ToLinearF[j]*ToLinearF[j+1])
578 Fltsize = (float)(lt2size/2);
590 #define DecoderState(tif) ((PixarLogState*) (tif)->tif_data) 591 #define EncoderState(tif) ((PixarLogState*) (tif)->tif_data) 596 #define PIXARLOGDATAFMT_UNKNOWN -1 634 multiply(
size_t m1,
size_t m2)
636 uint32 bytes = m1 * m2;
638 if (m1 && bytes / m1 != m2)
645 PixarLogSetupDecode(
TIFF* tif)
650 static const char module[] =
"PixarLogSetupDecode";
667 if (sp->
tbuf == NULL)
673 "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
707 static const char module[] =
"PixarLogDecode";
708 int i, nsamples, llen;
713 nsamples = occ /
sizeof(float);
718 nsamples = occ /
sizeof(
uint16);
726 "%d bit input not supported in PixarLog",
744 "%s: Decoding error at scanline %d, %s",
760 "%s: Not enough data at scanline %d (short %d bytes)",
775 if (nsamples % llen) {
777 "%s: stride %d is not a multiple of sample count, " 778 "%d, data truncated.", tif->
tif_name, llen, nsamples);
779 nsamples -= nsamples % llen;
782 for (i = 0; i < nsamples; i += llen, up += llen) {
785 horizontalAccumulateF(up, llen, sp->
stride,
787 op += llen *
sizeof(float);
790 horizontalAccumulate16(up, llen, sp->
stride,
792 op += llen *
sizeof(
uint16);
795 horizontalAccumulate12(up, llen, sp->
stride,
797 op += llen *
sizeof(
int16);
800 horizontalAccumulate11(up, llen, sp->
stride,
802 op += llen *
sizeof(
uint16);
805 horizontalAccumulate8(up, llen, sp->
stride,
807 op += llen *
sizeof(
unsigned char);
810 horizontalAccumulate8abgr(up, llen, sp->
stride,
812 op += llen *
sizeof(
unsigned char);
816 "PixarLogDecode: unsupported bits/sample: %d",
826 PixarLogSetupEncode(
TIFF* tif)
831 static const char module[] =
"PixarLogSetupEncode";
844 if (sp->
tbuf == NULL)
878 horizontalDifferenceF(
float *ip,
int n,
int stride,
uint16 *wp,
uint16 *FromLT2)
881 int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
882 float fltsize = Fltsize;
884 #define CLAMP(v) ( (v<(float)0.) ? 0 \ 885 : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \ 886 : (v>(float)24.2) ? 2047 \ 887 : LogK1*log(v*LogK2) + 0.5 ) 900 r1 = (int32)
CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
901 g1 = (int32)
CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
902 b1 = (int32)
CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
904 }
else if (stride == 4) {
914 r1 = (int32)
CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
915 g1 = (int32)
CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
916 b1 = (int32)
CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
917 a1 = (int32)
CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
936 horizontalDifference16(
unsigned short *ip,
int n,
int stride,
937 unsigned short *wp,
uint16 *From14)
939 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
943 #define CLAMP(v) From14[(v) >> 2] 948 r2 = wp[0] =
CLAMP(ip[0]); g2 = wp[1] =
CLAMP(ip[1]);
949 b2 = wp[2] =
CLAMP(ip[2]);
955 r1 =
CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
956 g1 =
CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
957 b1 =
CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
959 }
else if (stride == 4) {
960 r2 = wp[0] =
CLAMP(ip[0]); g2 = wp[1] =
CLAMP(ip[1]);
961 b2 = wp[2] =
CLAMP(ip[2]); a2 = wp[3] =
CLAMP(ip[3]);
967 r1 =
CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
968 g1 =
CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
969 b1 =
CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
970 a1 =
CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
990 horizontalDifference8(
unsigned char *ip,
int n,
int stride,
991 unsigned short *wp,
uint16 *From8)
993 register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
996 #define CLAMP(v) (From8[(v)]) 1001 r2 = wp[0] =
CLAMP(ip[0]); g2 = wp[1] =
CLAMP(ip[1]);
1002 b2 = wp[2] =
CLAMP(ip[2]);
1006 r1 =
CLAMP(ip[3]); wp[3] = (r1-r2) & mask; r2 = r1;
1007 g1 =
CLAMP(ip[4]); wp[4] = (g1-g2) & mask; g2 = g1;
1008 b1 =
CLAMP(ip[5]); wp[5] = (b1-b2) & mask; b2 = b1;
1012 }
else if (stride == 4) {
1013 r2 = wp[0] =
CLAMP(ip[0]); g2 = wp[1] =
CLAMP(ip[1]);
1014 b2 = wp[2] =
CLAMP(ip[2]); a2 = wp[3] =
CLAMP(ip[3]);
1018 r1 =
CLAMP(ip[4]); wp[4] = (r1-r2) & mask; r2 = r1;
1019 g1 =
CLAMP(ip[5]); wp[5] = (g1-g2) & mask; g2 = g1;
1020 b1 =
CLAMP(ip[6]); wp[6] = (b1-b2) & mask; b2 = b1;
1021 a1 =
CLAMP(ip[7]); wp[7] = (a1-a2) & mask; a2 = a1;
1026 wp += n + stride - 1;
1027 ip += n + stride - 1;
1031 wp[stride] -= wp[0];
1049 static const char module[] =
"PixarLogEncode";
1051 unsigned short * up;
1057 n = cc /
sizeof(float);
1070 "%d bit input not supported in PixarLog",
1077 for (i = 0, up = sp->
tbuf; i < n; i += llen, up += llen) {
1080 horizontalDifferenceF((
float *)bp, llen,
1082 bp += llen *
sizeof(float);
1085 horizontalDifference16((
uint16 *)bp, llen,
1087 bp += llen *
sizeof(
uint16);
1090 horizontalDifference8((
unsigned char *)bp, llen,
1092 bp += llen *
sizeof(
unsigned char);
1096 "%d bit input not supported in PixarLog",
1127 PixarLogPostEncode(
TIFF* tif)
1130 static const char module[] =
"PixarLogPostEncode";
1158 PixarLogClose(
TIFF* tif)
1173 PixarLogCleanup(
TIFF* tif)
1205 PixarLogVSetField(
TIFF* tif,
ttag_t tag, va_list ap)
1209 static const char module[] =
"PixarLogVSetField";
1213 sp->
quality = va_arg(ap,
int);
1267 PixarLogVGetField(
TIFF* tif,
ttag_t tag, va_list ap)
1273 *va_arg(ap,
int*) = sp->
quality;
1292 static const char module[] =
"TIFFInitPixarLog";
1304 "Merging PixarLog codec-specific tags failed");
1354 PixarLogMakeTables(sp);
1359 "No space for PixarLog state block");
#define PIXARLOGDATAFMT_16BIT
TIFFCodeMethod tif_decoderow
TIFFTagMethods tif_tagmethods
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
TIFFPreMethod tif_predecode
#define TIFFTAG_BITSPERSAMPLE
void TIFFWarningExt(thandle_t fd, const char *module, const char *fmt,...)
int ZEXPORT inflateReset(z_streamp strm)
#define DecoderState(tif)
#define EncoderState(tif)
TIFFCodeMethod tif_encodestrip
TIFFCodeMethod tif_encoderow
#define PIXARLOGDATAFMT_FLOAT
tsize_t TIFFScanlineSize(TIFF *tif)
#define PIXARLOGDATAFMT_8BITABGR
void TIFFSwabArrayOfShort(uint16 *wp, register unsigned long n)
int TIFFPredictorCleanup(TIFF *tif)
int ZEXPORT deflateEnd(z_streamp strm)
TIFFPredictorState predict
TIFFCodeMethod tif_decodestrip
TIFFCodeMethod tif_encodetile
#define SAMPLEFORMAT_VOID
TIFFPreMethod tif_preencode
#define SAMPLEFORMAT_UINT
TIFFBoolMethod tif_postencode
#define SAMPLEFORMAT_IEEEFP
TIFFPostMethod tif_postdecode
#define COMPRESSION_PIXARLOG
int _TIFFMergeFieldInfo(TIFF *, const TIFFFieldInfo[], int)
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
int ZEXPORT inflateSync(z_streamp strm)
TIFFCodeMethod tif_decodetile
int ZEXPORT deflate(z_streamp strm, int flush)
TIFFVoidMethod tif_cleanup
#define TIFFArrayCount(a)
void _TIFFmemset(tdata_t p, int v, tsize_t c)
void _TIFFSetDefaultCompressionState(TIFF *tif)
void _TIFFNoPostDecode(TIFF *tif, tidata_t buf, tsize_t cc)
uint16 td_samplesperpixel
int TIFFInitPixarLog(TIFF *tif, int scheme)
int TIFFPredictorInit(TIFF *tif)
#define PIXARLOGDATAFMT_11BITLOG
tsize_t TIFFTileSize(TIFF *tif)
unsigned char * ToLinear8
TIFFVSetMethod vsetparent
TIFFVSetMethod vgetparent
#define deflateInit(strm, level)
#define inflateInit(strm)
int TIFFSetField(TIFF *tif, ttag_t tag,...)
#define TIFFTAG_PIXARLOGDATAFMT
void * _TIFFmalloc(tsize_t s)
int ZEXPORT inflate(z_streamp strm, int flush)
#define Z_DEFAULT_STRATEGY
#define PIXARLOGDATAFMT_8BIT
int ZEXPORT deflateReset(z_streamp strm)
void _TIFFfree(tdata_t p)
#define TIFFTAG_SAMPLEFORMAT
int(* TIFFVSetMethod)(TIFF *, ttag_t, va_list)
#define TIFFTAG_PIXARLOGQUALITY
TIFFBoolMethod tif_setupencode
int TIFFFlushData1(TIFF *tif)
int ZEXPORT inflateEnd(z_streamp strm)
#define PIXARLOGDATAFMT_12BITPICIO
#define Z_DEFAULT_COMPRESSION
#define PIXARLOGDATAFMT_UNKNOWN
#define PLANARCONFIG_CONTIG
TIFFBoolMethod tif_setupdecode