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++)
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;
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;
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];
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];
282 wp[
stride] += *wp; *op = ToLinear16[*wp&
mask]; wp++; 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;
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;
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];
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];
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];
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];
443 wp[
stride] += *wp; *op = ToLinear8[*wp&
mask]; wp++; op++)
462 #define PLSTATE_INIT 1 469 unsigned char *ToLinear8;
477 PixarLogMakeTables(PixarLogState *sp)
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) {
527 sp->ToLinearF =
NULL;
528 sp->ToLinear16 =
NULL;
529 sp->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);
580 sp->ToLinearF = ToLinearF;
581 sp->ToLinear16 = ToLinear16;
582 sp->ToLinear8 = ToLinear8;
583 sp->FromLT2 = FromLT2;
590 #define DecoderState(tif) ((PixarLogState*) (tif)->tif_data) 591 #define EncoderState(tif) ((PixarLogState*) (tif)->tif_data) 596 #define N(a) (sizeof(a)/sizeof(a[0])) 597 #define PIXARLOGDATAFMT_UNKNOWN -1 635 multiply(
size_t m1,
size_t m2)
639 if (m1 && bytes / m1 != m2)
646 PixarLogSetupDecode(
TIFF* tif)
651 static const char module[] =
"PixarLogSetupDecode";
663 tbuf_size = multiply(multiply(multiply(sp->stride, td->
td_imagewidth),
668 if (sp->tbuf ==
NULL)
671 sp->user_datafmt = PixarLogGuessDataFmt(td);
674 "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
708 static const char module[] =
"PixarLogDecode";
709 int i, nsamples, llen;
712 switch (sp->user_datafmt) {
714 nsamples = occ /
sizeof(float);
719 nsamples = occ /
sizeof(
uint16);
727 "%d bit input not supported in PixarLog",
736 sp->stream.next_out = (
unsigned char *) sp->tbuf;
737 sp->stream.avail_out = nsamples *
sizeof(
uint16);
745 "%s: Decoding error at scanline %d, %s",
756 }
while (sp->stream.avail_out > 0);
759 if (sp->stream.avail_out != 0) {
761 "%s: Not enough data at scanline %d (short %d bytes)",
771 for (i = 0; i < nsamples; i += llen, up += llen) {
772 switch (sp->user_datafmt) {
774 horizontalAccumulateF(up, llen, sp->stride,
775 (
float *)op, sp->ToLinearF);
776 op += llen *
sizeof(float);
779 horizontalAccumulate16(up, llen, sp->stride,
780 (
uint16 *)op, sp->ToLinear16);
781 op += llen *
sizeof(
uint16);
784 horizontalAccumulate12(up, llen, sp->stride,
785 (
int16 *)op, sp->ToLinearF);
786 op += llen *
sizeof(
int16);
789 horizontalAccumulate11(up, llen, sp->stride,
791 op += llen *
sizeof(
uint16);
794 horizontalAccumulate8(up, llen, sp->stride,
795 (
unsigned char *)op, sp->ToLinear8);
796 op += llen *
sizeof(
unsigned char);
799 horizontalAccumulate8abgr(up, llen, sp->stride,
800 (
unsigned char *)op, sp->ToLinear8);
801 op += llen *
sizeof(
unsigned char);
805 "PixarLogDecode: unsupported bits/sample: %d",
815 PixarLogSetupEncode(
TIFF* tif)
820 static const char module[] =
"PixarLogSetupEncode";
828 tbuf_size = multiply(multiply(multiply(sp->stride, td->
td_imagewidth),
833 if (sp->tbuf ==
NULL)
836 sp->user_datafmt = PixarLogGuessDataFmt(td);
870 int32 r1, g1, b1, a1, r2, g2, b2, a2,
mask;
871 float fltsize = Fltsize;
873 #define CLAMP(v) ( (v<(float)0.) ? 0 \ 874 : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \ 875 : (v>(float)24.2) ? 2047 \ 876 : LogK1*log(v*LogK2) + 0.5 ) 889 r1 = (int32)
CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
890 g1 = (int32)
CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
891 b1 = (int32)
CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
903 r1 = (int32)
CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
904 g1 = (int32)
CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
905 b1 = (int32)
CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
906 a1 = (int32)
CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
925 horizontalDifference16(
unsigned short *ip,
int n,
int stride,
926 unsigned short *wp,
uint16 *From14)
928 register int r1, g1, b1, a1, r2, g2, b2, a2,
mask;
932 #define CLAMP(v) From14[(v) >> 2] 937 r2 = wp[0] =
CLAMP(ip[0]); g2 = wp[1] =
CLAMP(ip[1]);
938 b2 = wp[2] =
CLAMP(ip[2]);
944 r1 =
CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
945 g1 =
CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
946 b1 =
CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
949 r2 = wp[0] =
CLAMP(ip[0]); g2 = wp[1] =
CLAMP(ip[1]);
950 b2 = wp[2] =
CLAMP(ip[2]); a2 = wp[3] =
CLAMP(ip[3]);
956 r1 =
CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
957 g1 =
CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
958 b1 =
CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
959 a1 =
CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
979 horizontalDifference8(
unsigned char *ip,
int n,
int stride,
980 unsigned short *wp,
uint16 *From8)
982 register int r1, g1, b1, a1, r2, g2, b2, a2,
mask;
985 #define CLAMP(v) (From8[(v)]) 990 r2 = wp[0] =
CLAMP(ip[0]); g2 = wp[1] =
CLAMP(ip[1]);
991 b2 = wp[2] =
CLAMP(ip[2]);
995 r1 =
CLAMP(ip[3]); wp[3] = (r1-r2) & mask; r2 = r1;
996 g1 =
CLAMP(ip[4]); wp[4] = (g1-g2) & mask; g2 = g1;
997 b1 =
CLAMP(ip[5]); wp[5] = (b1-b2) & mask; b2 = b1;
1001 }
else if (
stride == 4) {
1002 r2 = wp[0] =
CLAMP(ip[0]); g2 = wp[1] =
CLAMP(ip[1]);
1003 b2 = wp[2] =
CLAMP(ip[2]); a2 = wp[3] =
CLAMP(ip[3]);
1007 r1 =
CLAMP(ip[4]); wp[4] = (r1-r2) & mask; r2 = r1;
1008 g1 =
CLAMP(ip[5]); wp[5] = (g1-g2) & mask; g2 = g1;
1009 b1 =
CLAMP(ip[6]); wp[6] = (b1-b2) & mask; b2 = b1;
1010 a1 =
CLAMP(ip[7]); wp[7] = (a1-a2) & mask; a2 = a1;
1016 ip +=
n + stride - 1;
1038 static const char module[] =
"PixarLogEncode";
1040 unsigned short * up;
1044 switch (sp->user_datafmt) {
1046 n = cc /
sizeof(float);
1059 "%d bit input not supported in PixarLog",
1066 for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
1067 switch (sp->user_datafmt) {
1069 horizontalDifferenceF((
float *)bp, llen,
1070 sp->stride, up, sp->FromLT2);
1071 bp += llen *
sizeof(float);
1074 horizontalDifference16((
uint16 *)bp, llen,
1075 sp->stride, up, sp->From14);
1076 bp += llen *
sizeof(
uint16);
1079 horizontalDifference8((
unsigned char *)bp, llen,
1080 sp->stride, up, sp->From8);
1081 bp += llen *
sizeof(
unsigned char);
1085 "%d bit input not supported in PixarLog",
1091 sp->stream.next_in = (
unsigned char *) sp->tbuf;
1092 sp->stream.avail_in = n *
sizeof(
uint16);
1100 if (sp->stream.avail_out == 0) {
1106 }
while (sp->stream.avail_in > 0);
1116 PixarLogPostEncode(
TIFF* tif)
1119 static const char module[] =
"PixarLogPostEncode";
1122 sp->stream.avail_in = 0;
1147 PixarLogClose(
TIFF* tif)
1162 PixarLogCleanup(
TIFF* tif)
1164 PixarLogState* sp = (PixarLogState*) tif->
tif_data;
1173 if (sp->FromLT2)
_TIFFfree(sp->FromLT2);
1176 if (sp->ToLinearF)
_TIFFfree(sp->ToLinearF);
1177 if (sp->ToLinear16)
_TIFFfree(sp->ToLinear16);
1178 if (sp->ToLinear8)
_TIFFfree(sp->ToLinear8);
1194 PixarLogVSetField(
TIFF* tif,
ttag_t tag, va_list ap)
1196 PixarLogState *sp = (PixarLogState *)tif->
tif_data;
1198 static const char module[] =
"PixarLogVSetField";
1202 sp->quality = va_arg(ap,
int);
1213 sp->user_datafmt = va_arg(ap,
int);
1219 switch (sp->user_datafmt) {
1250 result = (*sp->vsetparent)(tif, tag, ap);
1256 PixarLogVGetField(
TIFF* tif,
ttag_t tag, va_list ap)
1258 PixarLogState *sp = (PixarLogState *)tif->
tif_data;
1262 *va_arg(ap,
int*) = sp->quality;
1265 *va_arg(ap,
int*) = sp->user_datafmt;
1268 return (*sp->vgetparent)(tif, tag, ap);
1291 sp = (PixarLogState*) tif->
tif_data;
1332 PixarLogMakeTables(sp);
1337 "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
#define DecoderState(tif)
#define EncoderState(tif)
TIFFCodeMethod tif_encodestrip
TIFFCodeMethod tif_encoderow
#define PIXARLOGDATAFMT_FLOAT
tsize_t TIFFScanlineSize(TIFF *tif)
#define Z_DEFAULT_COMPRESSION
#define PIXARLOGDATAFMT_8BITABGR
void TIFFSwabArrayOfShort(uint16 *wp, register unsigned long n)
int ZEXPORT inflateReset(z_streamp strm)
int TIFFPredictorCleanup(TIFF *tif)
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
int ZEXPORT deflateEnd(z_streamp strm)
GLboolean GLboolean GLboolean b
TIFFCodeMethod tif_decodestrip
#define inflateInit(strm)
TIFFCodeMethod tif_encodetile
#define SAMPLEFORMAT_VOID
TIFFPreMethod tif_preencode
#define SAMPLEFORMAT_UINT
TIFFBoolMethod tif_postencode
int ZEXPORT inflateSync(z_streamp strm)
#define deflateInit(strm, level)
#define SAMPLEFORMAT_IEEEFP
int ZEXPORT inflate(z_streamp strm, int flush)
TIFFPostMethod tif_postdecode
#define COMPRESSION_PIXARLOG
void _TIFFMergeFieldInfo(TIFF *tif, const TIFFFieldInfo info[], int n)
#define Z_DEFAULT_STRATEGY
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
TIFFCodeMethod tif_decodetile
int ZEXPORT deflate(z_streamp strm, int flush)
TIFFVoidMethod tif_cleanup
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)
int TIFFSetField(TIFF *tif, ttag_t tag,...)
#define TIFFTAG_PIXARLOGDATAFMT
void * _TIFFmalloc(tsize_t s)
#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)
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
int ZEXPORT inflateEnd(z_streamp strm)
#define PIXARLOGDATAFMT_12BITPICIO
#define PIXARLOGDATAFMT_UNKNOWN
#define PLANARCONFIG_CONTIG
TIFFBoolMethod tif_setupdecode