26
#ifdef PIXARLOG_SUPPORT
90
#include "tif_predict.h"
104
#define CODE_MASK 0x7ff
107
static float LogK1, LogK2;
109
#define REPEAT(n, op) \
120
static void horizontalAccumulateF(uint16_t *wp, int n, int stride, float *op,
123
register unsigned int cr, cg, cb, ca, mask;
124
register float t0, t1, t2, t3;
131
t0 = ToLinearF[cr = (wp[0] & mask)];
132
t1 = ToLinearF[cg = (wp[1] & mask)];
133
t2 = ToLinearF[cb = (wp[2] & mask)];
143
t0 = ToLinearF[(cr += wp[0]) & mask];
144
t1 = ToLinearF[(cg += wp[1]) & mask];
145
t2 = ToLinearF[(cb += wp[2]) & mask];
151
else if (stride == 4)
153
t0 = ToLinearF[cr = (wp[0] & mask)];
154
t1 = ToLinearF[cg = (wp[1] & mask)];
155
t2 = ToLinearF[cb = (wp[2] & mask)];
156
t3 = ToLinearF[ca = (wp[3] & mask)];
167
t0 = ToLinearF[(cr += wp[0]) & mask];
168
t1 = ToLinearF[(cg += wp[1]) & mask];
169
t2 = ToLinearF[(cb += wp[2]) & mask];
170
t3 = ToLinearF[(ca += wp[3]) & mask];
179
REPEAT(stride, *op = ToLinearF[*wp & mask]; wp++; op++)
183
REPEAT(stride, wp[stride] += *wp; *op = ToLinearF[*wp & mask];
191
static void horizontalAccumulate12(uint16_t *wp, int n, int stride, int16_t *op,
194
register unsigned int cr, cg, cb, ca, mask;
195
register float t0, t1, t2, t3;
197
#define SCALE12 2048.0F
198
#define CLAMP12(t) (((t) < 3071) ? (uint16_t)(t) : 3071)
205
t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
206
t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
207
t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
217
t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
218
t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
219
t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
225
else if (stride == 4)
227
t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
228
t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
229
t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
230
t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12;
241
t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
242
t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
243
t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
244
t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
253
REPEAT(stride, t0 = ToLinearF[*wp & mask] * SCALE12;
254
*op = CLAMP12(t0); wp++; op++)
258
REPEAT(stride, wp[stride] += *wp;
259
t0 = ToLinearF[wp[stride] & mask] * SCALE12;
260
*op = CLAMP12(t0); wp++; op++)
267
static void horizontalAccumulate16(uint16_t *wp, int n, int stride,
268
uint16_t *op, uint16_t *ToLinear16)
270
register unsigned int cr, cg, cb, ca, mask;
277
op[0] = ToLinear16[cr = (wp[0] & mask)];
278
op[1] = ToLinear16[cg = (wp[1] & mask)];
279
op[2] = ToLinear16[cb = (wp[2] & mask)];
286
op[0] = ToLinear16[(cr += wp[0]) & mask];
287
op[1] = ToLinear16[(cg += wp[1]) & mask];
288
op[2] = ToLinear16[(cb += wp[2]) & mask];
291
else if (stride == 4)
293
op[0] = ToLinear16[cr = (wp[0] & mask)];
294
op[1] = ToLinear16[cg = (wp[1] & mask)];
295
op[2] = ToLinear16[cb = (wp[2] & mask)];
296
op[3] = ToLinear16[ca = (wp[3] & mask)];
303
op[0] = ToLinear16[(cr += wp[0]) & mask];
304
op[1] = ToLinear16[(cg += wp[1]) & mask];
305
op[2] = ToLinear16[(cb += wp[2]) & mask];
306
op[3] = ToLinear16[(ca += wp[3]) & mask];
311
REPEAT(stride, *op = ToLinear16[*wp & mask]; wp++; op++)
315
REPEAT(stride, wp[stride] += *wp; *op = ToLinear16[*wp & mask];
327
static void horizontalAccumulate11(uint16_t *wp, int n, int stride,
330
register unsigned int cr, cg, cb, ca, mask;
349
op[0] = (uint16_t)((cr += wp[0]) & mask);
350
op[1] = (uint16_t)((cg += wp[1]) & mask);
351
op[2] = (uint16_t)((cb += wp[2]) & mask);
354
else if (stride == 4)
370
op[0] = (uint16_t)((cr += wp[0]) & mask);
371
op[1] = (uint16_t)((cg += wp[1]) & mask);
372
op[2] = (uint16_t)((cb += wp[2]) & mask);
373
op[3] = (uint16_t)((ca += wp[3]) & mask);
378
REPEAT(stride, *op = *wp & mask; wp++; op++)
382
REPEAT(stride, wp[stride] += *wp; *op = *wp & mask; wp++; op++)
389
static void horizontalAccumulate8(uint16_t *wp, int n, int stride,
390
unsigned char *op, unsigned char *ToLinear8)
392
register unsigned int cr, cg, cb, ca, mask;
399
op[0] = ToLinear8[cr = (wp[0] & mask)];
400
op[1] = ToLinear8[cg = (wp[1] & mask)];
401
op[2] = ToLinear8[cb = (wp[2] & mask)];
408
op[0] = ToLinear8[(cr += wp[0]) & mask];
409
op[1] = ToLinear8[(cg += wp[1]) & mask];
410
op[2] = ToLinear8[(cb += wp[2]) & mask];
413
else if (stride == 4)
415
op[0] = ToLinear8[cr = (wp[0] & mask)];
416
op[1] = ToLinear8[cg = (wp[1] & mask)];
417
op[2] = ToLinear8[cb = (wp[2] & mask)];
418
op[3] = ToLinear8[ca = (wp[3] & mask)];
425
op[0] = ToLinear8[(cr += wp[0]) & mask];
426
op[1] = ToLinear8[(cg += wp[1]) & mask];
427
op[2] = ToLinear8[(cb += wp[2]) & mask];
428
op[3] = ToLinear8[(ca += wp[3]) & mask];
433
REPEAT(stride, *op = ToLinear8[*wp & mask]; wp++; op++)
437
REPEAT(stride, wp[stride] += *wp; *op = ToLinear8[*wp & mask];
445
static void horizontalAccumulate8abgr(uint16_t *wp, int n, int stride,
447
unsigned char *ToLinear8)
449
register unsigned int cr, cg, cb, ca, mask;
450
register unsigned char t0, t1, t2, t3;
458
t1 = ToLinear8[cb = (wp[2] & mask)];
459
t2 = ToLinear8[cg = (wp[1] & mask)];
460
t3 = ToLinear8[cr = (wp[0] & mask)];
471
t1 = ToLinear8[(cb += wp[2]) & mask];
472
t2 = ToLinear8[(cg += wp[1]) & mask];
473
t3 = ToLinear8[(cr += wp[0]) & mask];
479
else if (stride == 4)
481
t0 = ToLinear8[ca = (wp[3] & mask)];
482
t1 = ToLinear8[cb = (wp[2] & mask)];
483
t2 = ToLinear8[cg = (wp[1] & mask)];
484
t3 = ToLinear8[cr = (wp[0] & mask)];
495
t0 = ToLinear8[(ca += wp[3]) & mask];
496
t1 = ToLinear8[(cb += wp[2]) & mask];
497
t2 = ToLinear8[(cg += wp[1]) & mask];
498
t3 = ToLinear8[(cr += wp[0]) & mask];
507
REPEAT(stride, *op = ToLinear8[*wp & mask]; wp++; op++)
511
REPEAT(stride, wp[stride] += *wp; *op = ToLinear8[*wp & mask];
525
TIFFPredictorState predict;
533
#define PLSTATE_INIT 1
535
TIFFVSetMethod vgetparent;
536
TIFFVSetMethod vsetparent;
539
uint16_t *ToLinear16;
540
unsigned char *ToLinear8;
547
static int PixarLogMakeTables(TIFF *tif, PixarLogState *sp)
563
double b, c, linstep, v;
565
uint16_t *ToLinear16;
566
unsigned char *ToLinear8;
572
nlin = (int)(1. / c);
575
linstep = b * c * exp(1.);
577
LogK1 = (float)(1. / c);
578
LogK2 = (float)(1. / b);
579
lt2size = (int)(2. / linstep) + 1;
580
FromLT2 = (uint16_t *)_TIFFmallocExt(tif, lt2size * sizeof(uint16_t));
581
From14 = (uint16_t *)_TIFFmallocExt(tif, 16384 * sizeof(uint16_t));
582
From8 = (uint16_t *)_TIFFmallocExt(tif, 256 * sizeof(uint16_t));
583
ToLinearF = (float *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(float));
584
ToLinear16 = (uint16_t *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(uint16_t));
586
(unsigned char *)_TIFFmallocExt(tif, TSIZEP1 * sizeof(unsigned char));
587
if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
588
ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL)
591
_TIFFfreeExt(tif, FromLT2);
593
_TIFFfreeExt(tif, From14);
595
_TIFFfreeExt(tif, From8);
597
_TIFFfreeExt(tif, ToLinearF);
599
_TIFFfreeExt(tif, ToLinear16);
601
_TIFFfreeExt(tif, ToLinear8);
605
sp->ToLinearF = NULL;
606
sp->ToLinear16 = NULL;
607
sp->ToLinear8 = NULL;
613
for (i = 0; i < nlin; i++)
616
ToLinearF[j++] = (float)v;
619
for (i = nlin; i < TSIZE; i++)
620
ToLinearF[j++] = (float)(b * exp(c * i));
622
ToLinearF[2048] = ToLinearF[2047];
624
for (i = 0; i < TSIZEP1; i++)
626
v = ToLinearF[i] * 65535.0 + 0.5;
627
ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16_t)v;
628
v = ToLinearF[i] * 255.0 + 0.5;
629
ToLinear8[i] = (v > 255.0) ? 255 : (unsigned char)v;
633
for (i = 0; i < lt2size; i++)
635
if ((i * linstep) * (i * linstep) > ToLinearF[j] * ToLinearF[j + 1])
637
FromLT2[i] = (uint16_t)j;
646
for (i = 0; i < 16384; i++)
648
while ((i / 16383.) * (i / 16383.) > ToLinearF[j] * ToLinearF[j + 1])
650
From14[i] = (uint16_t)j;
654
for (i = 0; i < 256; i++)
656
while ((i / 255.) * (i / 255.) > ToLinearF[j] * ToLinearF[j + 1])
658
From8[i] = (uint16_t)j;
661
Fltsize = (float)(lt2size / 2);
663
sp->ToLinearF = ToLinearF;
664
sp->ToLinear16 = ToLinear16;
665
sp->ToLinear8 = ToLinear8;
666
sp->FromLT2 = FromLT2;
673
#define DecoderState(tif) ((PixarLogState *)(tif)->tif_data)
674
#define EncoderState(tif) ((PixarLogState *)(tif)->tif_data)
676
static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
677
static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
679
#define PIXARLOGDATAFMT_UNKNOWN -1
681
static int PixarLogGuessDataFmt(TIFFDirectory *td)
683
int guess = PIXARLOGDATAFMT_UNKNOWN;
684
int format = td->td_sampleformat;
689
switch (td->td_bitspersample)
692
if (format == SAMPLEFORMAT_IEEEFP)
693
guess = PIXARLOGDATAFMT_FLOAT;
696
if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
697
guess = PIXARLOGDATAFMT_16BIT;
700
if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
701
guess = PIXARLOGDATAFMT_12BITPICIO;
704
if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
705
guess = PIXARLOGDATAFMT_11BITLOG;
708
if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
709
guess = PIXARLOGDATAFMT_8BIT;
716
static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2)
718
return _TIFFMultiplySSize(NULL, m1, m2, NULL);
721
static tmsize_t add_ms(tmsize_t m1, tmsize_t m2)
723
assert(m1 >= 0 && m2 >= 0);
725
if (m1 == 0 || m2 == 0)
727
else if (m1 > TIFF_TMSIZE_T_MAX - m2)
733
static int PixarLogFixupTags(TIFF *tif)
739
static int PixarLogSetupDecode(TIFF *tif)
741
static const char module[] = "PixarLogSetupDecode";
742
TIFFDirectory *td = &tif->tif_dir;
743
PixarLogState *sp = DecoderState(tif);
745
uint32_t strip_height;
752
if ((sp->state & PLSTATE_INIT) != 0)
755
strip_height = td->td_rowsperstrip;
756
if (strip_height > td->td_imagelength)
757
strip_height = td->td_imagelength;
761
tif->tif_postdecode = _TIFFNoPostDecode;
766
(td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
768
tbuf_size = multiply_ms(
769
multiply_ms(multiply_ms(sp->stride, td->td_imagewidth), strip_height),
772
tbuf_size = add_ms(tbuf_size, sizeof(uint16_t) * sp->stride);
776
sp->tbuf = (uint16_t *)_TIFFmallocExt(tif, tbuf_size);
777
if (sp->tbuf == NULL)
779
sp->tbuf_size = tbuf_size;
780
if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
781
sp->user_datafmt = PixarLogGuessDataFmt(td);
782
if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
784
_TIFFfreeExt(tif, sp->tbuf);
787
TIFFErrorExtR(tif, module,
788
"PixarLog compression can't handle bits depth/data "
789
"format combination (depth: %" PRIu16 ")",
790
td->td_bitspersample);
794
if (inflateInit(&sp->stream) != Z_OK)
796
_TIFFfreeExt(tif, sp->tbuf);
799
TIFFErrorExtR(tif, module, "%s",
800
sp->stream.msg ? sp->stream.msg : "(null)");
805
sp->state |= PLSTATE_INIT;
813
static int PixarLogPreDecode(TIFF *tif, uint16_t s)
815
static const char module[] = "PixarLogPreDecode";
816
PixarLogState *sp = DecoderState(tif);
820
sp->stream.next_in = tif->tif_rawdata;
821
assert(sizeof(sp->stream.avail_in) == 4);
825
sp->stream.avail_in = (uInt)tif->tif_rawcc;
826
if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
828
TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
831
return (inflateReset(&sp->stream) == Z_OK);
834
static int PixarLogDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
836
static const char module[] = "PixarLogDecode";
837
TIFFDirectory *td = &tif->tif_dir;
838
PixarLogState *sp = DecoderState(tif);
844
switch (sp->user_datafmt)
846
case PIXARLOGDATAFMT_FLOAT:
847
nsamples = occ / sizeof(float);
849
case PIXARLOGDATAFMT_16BIT:
850
case PIXARLOGDATAFMT_12BITPICIO:
851
case PIXARLOGDATAFMT_11BITLOG:
852
nsamples = occ / sizeof(uint16_t);
854
case PIXARLOGDATAFMT_8BIT:
855
case PIXARLOGDATAFMT_8BITABGR:
859
TIFFErrorExtR(tif, module,
860
"%" PRIu16 " bit input not supported in PixarLog",
861
td->td_bitspersample);
865
llen = sp->stride * td->td_imagewidth;
870
sp->stream.next_in = tif->tif_rawcp;
871
sp->stream.avail_in = (uInt)tif->tif_rawcc;
873
sp->stream.next_out = (unsigned char *)sp->tbuf;
874
assert(sizeof(sp->stream.avail_out) == 4);
878
sp->stream.avail_out = (uInt)(nsamples * sizeof(uint16_t));
879
if (sp->stream.avail_out != nsamples * sizeof(uint16_t))
881
TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
885
if ((tmsize_t)sp->stream.avail_out > sp->tbuf_size)
887
TIFFErrorExtR(tif, module, "sp->stream.avail_out > sp->tbuf_size");
892
int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
893
if (state == Z_STREAM_END)
897
if (state == Z_DATA_ERROR)
900
tif, module, "Decoding error at scanline %" PRIu32 ", %s",
901
tif->tif_row, sp->stream.msg ? sp->stream.msg : "(null)");
906
TIFFErrorExtR(tif, module, "ZLib error: %s",
907
sp->stream.msg ? sp->stream.msg : "(null)");
910
} while (sp->stream.avail_out > 0);
913
if (sp->stream.avail_out != 0)
915
TIFFErrorExtR(tif, module,
916
"Not enough data at scanline %" PRIu32
918
tif->tif_row, sp->stream.avail_out);
922
tif->tif_rawcp = sp->stream.next_in;
923
tif->tif_rawcc = sp->stream.avail_in;
927
if (tif->tif_flags & TIFF_SWAB)
928
TIFFSwabArrayOfShort(up, nsamples);
937
TIFFWarningExtR(tif, module,
938
"stride %d is not a multiple of sample count, "
939
"%" TIFF_SSIZE_FORMAT ", data truncated.",
941
nsamples -= nsamples % llen;
944
for (i = 0; i < nsamples; i += llen, up += llen)
946
switch (sp->user_datafmt)
948
case PIXARLOGDATAFMT_FLOAT:
949
horizontalAccumulateF(up, llen, sp->stride, (float *)op,
951
op += llen * sizeof(float);
953
case PIXARLOGDATAFMT_16BIT:
954
horizontalAccumulate16(up, llen, sp->stride, (uint16_t *)op,
956
op += llen * sizeof(uint16_t);
958
case PIXARLOGDATAFMT_12BITPICIO:
959
horizontalAccumulate12(up, llen, sp->stride, (int16_t *)op,
961
op += llen * sizeof(int16_t);
963
case PIXARLOGDATAFMT_11BITLOG:
964
horizontalAccumulate11(up, llen, sp->stride, (uint16_t *)op);
965
op += llen * sizeof(uint16_t);
967
case PIXARLOGDATAFMT_8BIT:
968
horizontalAccumulate8(up, llen, sp->stride, (unsigned char *)op,
970
op += llen * sizeof(unsigned char);
972
case PIXARLOGDATAFMT_8BITABGR:
973
horizontalAccumulate8abgr(up, llen, sp->stride,
974
(unsigned char *)op, sp->ToLinear8);
975
op += llen * sizeof(unsigned char);
978
TIFFErrorExtR(tif, module, "Unsupported bits/sample: %" PRIu16,
979
td->td_bitspersample);
987
static int PixarLogSetupEncode(TIFF *tif)
989
static const char module[] = "PixarLogSetupEncode";
990
TIFFDirectory *td = &tif->tif_dir;
991
PixarLogState *sp = EncoderState(tif);
999
(td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
1002
multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
1003
td->td_rowsperstrip),
1008
sp->tbuf = (uint16_t *)_TIFFmallocExt(tif, tbuf_size);
1009
if (sp->tbuf == NULL)
1011
if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
1012
sp->user_datafmt = PixarLogGuessDataFmt(td);
1013
if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
1015
TIFFErrorExtR(tif, module,
1016
"PixarLog compression can't handle %" PRIu16
1017
" bit linear encodings",
1018
td->td_bitspersample);
1022
if (deflateInit(&sp->stream, sp->quality) != Z_OK)
1024
TIFFErrorExtR(tif, module, "%s",
1025
sp->stream.msg ? sp->stream.msg : "(null)");
1030
sp->state |= PLSTATE_INIT;
1038
static int PixarLogPreEncode(TIFF *tif, uint16_t s)
1040
static const char module[] = "PixarLogPreEncode";
1041
PixarLogState *sp = EncoderState(tif);
1045
sp->stream.next_out = tif->tif_rawdata;
1046
assert(sizeof(sp->stream.avail_out) == 4);
1050
sp->stream.avail_out = (uInt)tif->tif_rawdatasize;
1051
if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
1053
TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
1056
return (deflateReset(&sp->stream) == Z_OK);
1059
static void horizontalDifferenceF(float *ip, int n, int stride, uint16_t *wp,
1062
int32_t r1, g1, b1, a1, r2, g2, b2, a2, mask;
1063
float fltsize = Fltsize;
1066
((v < (float)0.) ? 0 \
1067
: (v < (float)2.) ? FromLT2[(int)(v * fltsize)] \
1068
: (v > (float)24.2) ? 2047 \
1069
: LogK1 * log(v * LogK2) + 0.5)
1076
r2 = wp[0] = (uint16_t)CLAMP(ip[0]);
1077
g2 = wp[1] = (uint16_t)CLAMP(ip[1]);
1078
b2 = wp[2] = (uint16_t)CLAMP(ip[2]);
1085
r1 = (int32_t)CLAMP(ip[0]);
1086
wp[0] = (uint16_t)((r1 - r2) & mask);
1088
g1 = (int32_t)CLAMP(ip[1]);
1089
wp[1] = (uint16_t)((g1 - g2) & mask);
1091
b1 = (int32_t)CLAMP(ip[2]);
1092
wp[2] = (uint16_t)((b1 - b2) & mask);
1096
else if (stride == 4)
1098
r2 = wp[0] = (uint16_t)CLAMP(ip[0]);
1099
g2 = wp[1] = (uint16_t)CLAMP(ip[1]);
1100
b2 = wp[2] = (uint16_t)CLAMP(ip[2]);
1101
a2 = wp[3] = (uint16_t)CLAMP(ip[3]);
1108
r1 = (int32_t)CLAMP(ip[0]);
1109
wp[0] = (uint16_t)((r1 - r2) & mask);
1111
g1 = (int32_t)CLAMP(ip[1]);
1112
wp[1] = (uint16_t)((g1 - g2) & mask);
1114
b1 = (int32_t)CLAMP(ip[2]);
1115
wp[2] = (uint16_t)((b1 - b2) & mask);
1117
a1 = (int32_t)CLAMP(ip[3]);
1118
wp[3] = (uint16_t)((a1 - a2) & mask);
1124
REPEAT(stride, wp[0] = (uint16_t)CLAMP(ip[0]); wp++; ip++)
1129
wp[0] = (uint16_t)(((int32_t)CLAMP(ip[0]) -
1130
(int32_t)CLAMP(ip[-stride])) &
1139
static void horizontalDifference16(unsigned short *ip, int n, int stride,
1140
unsigned short *wp, uint16_t *From14)
1142
register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1146
#define CLAMP(v) From14[(v) >> 2]
1153
r2 = wp[0] = CLAMP(ip[0]);
1154
g2 = wp[1] = CLAMP(ip[1]);
1155
b2 = wp[2] = CLAMP(ip[2]);
1163
wp[0] = (uint16_t)((r1 - r2) & mask);
1166
wp[1] = (uint16_t)((g1 - g2) & mask);
1169
wp[2] = (uint16_t)((b1 - b2) & mask);
1173
else if (stride == 4)
1175
r2 = wp[0] = CLAMP(ip[0]);
1176
g2 = wp[1] = CLAMP(ip[1]);
1177
b2 = wp[2] = CLAMP(ip[2]);
1178
a2 = wp[3] = CLAMP(ip[3]);
1186
wp[0] = (uint16_t)((r1 - r2) & mask);
1189
wp[1] = (uint16_t)((g1 - g2) & mask);
1192
wp[2] = (uint16_t)((b1 - b2) & mask);
1195
wp[3] = (uint16_t)((a1 - a2) & mask);
1201
REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
1206
wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) &
1215
static void horizontalDifference8(unsigned char *ip, int n, int stride,
1216
unsigned short *wp, uint16_t *From8)
1218
register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
1221
#define CLAMP(v) (From8[(v)])
1228
r2 = wp[0] = CLAMP(ip[0]);
1229
g2 = wp[1] = CLAMP(ip[1]);
1230
b2 = wp[2] = CLAMP(ip[2]);
1236
wp[3] = (uint16_t)((r1 - r2) & mask);
1239
wp[4] = (uint16_t)((g1 - g2) & mask);
1242
wp[5] = (uint16_t)((b1 - b2) & mask);
1248
else if (stride == 4)
1250
r2 = wp[0] = CLAMP(ip[0]);
1251
g2 = wp[1] = CLAMP(ip[1]);
1252
b2 = wp[2] = CLAMP(ip[2]);
1253
a2 = wp[3] = CLAMP(ip[3]);
1259
wp[4] = (uint16_t)((r1 - r2) & mask);
1262
wp[5] = (uint16_t)((g1 - g2) & mask);
1265
wp[6] = (uint16_t)((b1 - b2) & mask);
1268
wp[7] = (uint16_t)((a1 - a2) & mask);
1276
REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
1281
wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) &
1293
static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1295
static const char module[] = "PixarLogEncode";
1296
TIFFDirectory *td = &tif->tif_dir;
1297
PixarLogState *sp = EncoderState(tif);
1305
switch (sp->user_datafmt)
1307
case PIXARLOGDATAFMT_FLOAT:
1308
n = cc / sizeof(float);
1310
case PIXARLOGDATAFMT_16BIT:
1311
case PIXARLOGDATAFMT_12BITPICIO:
1312
case PIXARLOGDATAFMT_11BITLOG:
1313
n = cc / sizeof(uint16_t);
1315
case PIXARLOGDATAFMT_8BIT:
1316
case PIXARLOGDATAFMT_8BITABGR:
1320
TIFFErrorExtR(tif, module,
1321
"%" PRIu16 " bit input not supported in PixarLog",
1322
td->td_bitspersample);
1326
llen = sp->stride * td->td_imagewidth;
1328
if (n > ((tmsize_t)td->td_rowsperstrip * llen))
1330
TIFFErrorExtR(tif, module, "Too many input bytes provided");
1334
for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen)
1336
switch (sp->user_datafmt)
1338
case PIXARLOGDATAFMT_FLOAT:
1339
horizontalDifferenceF((float *)bp, llen, sp->stride, up,
1341
bp += llen * sizeof(float);
1343
case PIXARLOGDATAFMT_16BIT:
1344
horizontalDifference16((uint16_t *)bp, llen, sp->stride, up,
1346
bp += llen * sizeof(uint16_t);
1348
case PIXARLOGDATAFMT_8BIT:
1349
horizontalDifference8((unsigned char *)bp, llen, sp->stride, up,
1351
bp += llen * sizeof(unsigned char);
1354
TIFFErrorExtR(tif, module,
1355
"%" PRIu16 " bit input not supported in PixarLog",
1356
td->td_bitspersample);
1361
sp->stream.next_in = (unsigned char *)sp->tbuf;
1362
assert(sizeof(sp->stream.avail_in) == 4);
1366
sp->stream.avail_in = (uInt)(n * sizeof(uint16_t));
1367
if ((sp->stream.avail_in / sizeof(uint16_t)) != (uInt)n)
1369
TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
1375
if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
1377
TIFFErrorExtR(tif, module, "Encoder error: %s",
1378
sp->stream.msg ? sp->stream.msg : "(null)");
1381
if (sp->stream.avail_out == 0)
1383
tif->tif_rawcc = tif->tif_rawdatasize;
1384
if (!TIFFFlushData1(tif))
1386
sp->stream.next_out = tif->tif_rawdata;
1387
sp->stream.avail_out =
1392
} while (sp->stream.avail_in > 0);
1401
static int PixarLogPostEncode(TIFF *tif)
1403
static const char module[] = "PixarLogPostEncode";
1404
PixarLogState *sp = EncoderState(tif);
1407
sp->stream.avail_in = 0;
1411
state = deflate(&sp->stream, Z_FINISH);
1416
if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
1419
tif->tif_rawdatasize - sp->stream.avail_out;
1420
if (!TIFFFlushData1(tif))
1422
sp->stream.next_out = tif->tif_rawdata;
1423
sp->stream.avail_out =
1424
(uInt)tif->tif_rawdatasize;
1430
TIFFErrorExtR(tif, module, "ZLib error: %s",
1431
sp->stream.msg ? sp->stream.msg : "(null)");
1434
} while (state != Z_STREAM_END);
1438
static void PixarLogClose(TIFF *tif)
1440
PixarLogState *sp = (PixarLogState *)tif->tif_data;
1441
TIFFDirectory *td = &tif->tif_dir;
1453
if (sp->state & PLSTATE_INIT)
1463
td->td_bitspersample = 8;
1464
td->td_sampleformat = SAMPLEFORMAT_UINT;
1468
static void PixarLogCleanup(TIFF *tif)
1470
PixarLogState *sp = (PixarLogState *)tif->tif_data;
1474
(void)TIFFPredictorCleanup(tif);
1476
tif->tif_tagmethods.vgetfield = sp->vgetparent;
1477
tif->tif_tagmethods.vsetfield = sp->vsetparent;
1480
_TIFFfreeExt(tif, sp->FromLT2);
1482
_TIFFfreeExt(tif, sp->From14);
1484
_TIFFfreeExt(tif, sp->From8);
1486
_TIFFfreeExt(tif, sp->ToLinearF);
1488
_TIFFfreeExt(tif, sp->ToLinear16);
1490
_TIFFfreeExt(tif, sp->ToLinear8);
1491
if (sp->state & PLSTATE_INIT)
1493
if (tif->tif_mode == O_RDONLY)
1494
inflateEnd(&sp->stream);
1496
deflateEnd(&sp->stream);
1499
_TIFFfreeExt(tif, sp->tbuf);
1500
_TIFFfreeExt(tif, sp);
1501
tif->tif_data = NULL;
1503
_TIFFSetDefaultCompressionState(tif);
1506
static int PixarLogVSetField(TIFF *tif, uint32_t tag, va_list ap)
1508
static const char module[] = "PixarLogVSetField";
1509
PixarLogState *sp = (PixarLogState *)tif->tif_data;
1514
case TIFFTAG_PIXARLOGQUALITY:
1515
sp->quality = (int)va_arg(ap, int);
1516
if (tif->tif_mode != O_RDONLY && (sp->state & PLSTATE_INIT))
1518
if (deflateParams(&sp->stream, sp->quality,
1519
Z_DEFAULT_STRATEGY) != Z_OK)
1521
TIFFErrorExtR(tif, module, "ZLib error: %s",
1522
sp->stream.msg ? sp->stream.msg : "(null)");
1527
case TIFFTAG_PIXARLOGDATAFMT:
1528
sp->user_datafmt = (int)va_arg(ap, int);
1534
switch (sp->user_datafmt)
1536
case PIXARLOGDATAFMT_8BIT:
1537
case PIXARLOGDATAFMT_8BITABGR:
1538
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
1539
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1541
case PIXARLOGDATAFMT_11BITLOG:
1542
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1543
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1545
case PIXARLOGDATAFMT_12BITPICIO:
1546
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1547
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
1549
case PIXARLOGDATAFMT_16BIT:
1550
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
1551
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
1553
case PIXARLOGDATAFMT_FLOAT:
1554
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
1555
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT,
1556
SAMPLEFORMAT_IEEEFP);
1563
isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
1564
tif->tif_scanlinesize = TIFFScanlineSize(tif);
1568
result = (*sp->vsetparent)(tif, tag, ap);
1573
static int PixarLogVGetField(TIFF *tif, uint32_t tag, va_list ap)
1575
PixarLogState *sp = (PixarLogState *)tif->tif_data;
1579
case TIFFTAG_PIXARLOGQUALITY:
1580
*va_arg(ap, int *) = sp->quality;
1582
case TIFFTAG_PIXARLOGDATAFMT:
1583
*va_arg(ap, int *) = sp->user_datafmt;
1586
return (*sp->vgetparent)(tif, tag, ap);
1591
static const TIFFField pixarlogFields[] = {
1592
{TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
1593
TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
1594
{TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
1595
TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}};
1597
int TIFFInitPixarLog(TIFF *tif, int scheme)
1599
static const char module[] = "TIFFInitPixarLog";
1604
assert(scheme == COMPRESSION_PIXARLOG);
1609
if (!_TIFFMergeFields(tif, pixarlogFields, TIFFArrayCount(pixarlogFields)))
1611
TIFFErrorExtR(tif, module,
1612
"Merging PixarLog codec-specific tags failed");
1619
tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(PixarLogState));
1620
if (tif->tif_data == NULL)
1622
sp = (PixarLogState *)tif->tif_data;
1623
_TIFFmemset(sp, 0, sizeof(*sp));
1624
sp->stream.data_type = Z_BINARY;
1625
sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
1630
tif->tif_fixuptags = PixarLogFixupTags;
1631
tif->tif_setupdecode = PixarLogSetupDecode;
1632
tif->tif_predecode = PixarLogPreDecode;
1633
tif->tif_decoderow = PixarLogDecode;
1634
tif->tif_decodestrip = PixarLogDecode;
1635
tif->tif_decodetile = PixarLogDecode;
1636
tif->tif_setupencode = PixarLogSetupEncode;
1637
tif->tif_preencode = PixarLogPreEncode;
1638
tif->tif_postencode = PixarLogPostEncode;
1639
tif->tif_encoderow = PixarLogEncode;
1640
tif->tif_encodestrip = PixarLogEncode;
1641
tif->tif_encodetile = PixarLogEncode;
1642
tif->tif_close = PixarLogClose;
1643
tif->tif_cleanup = PixarLogCleanup;
1646
sp->vgetparent = tif->tif_tagmethods.vgetfield;
1647
tif->tif_tagmethods.vgetfield = PixarLogVGetField;
1648
sp->vsetparent = tif->tif_tagmethods.vsetfield;
1649
tif->tif_tagmethods.vsetfield = PixarLogVSetField;
1652
sp->quality = Z_DEFAULT_COMPRESSION;
1658
(void)TIFFPredictorInit(tif);
1663
PixarLogMakeTables(tif, sp);
1667
TIFFErrorExtR(tif, module, "No space for PixarLog state block");