38#if defined(__x86_64__) || (defined(_M_X64) && !defined(_M_ARM64EC))
42#define PredictorState(tif) ((TIFFPredictorState *)(tif)->tif_data)
85 static const char module[] = "PredictorSetup";
90 switch (sp->predictor)
99 "Horizontal differencing \"Predictor\" not "
100 "supported with %" PRIu16
"-bit samples",
110 "Floating point \"Predictor\" not supported with %" PRIu16
120 "Floating point \"Predictor\" not supported with %" PRIu16
127 TIFFErrorExtR(tif, module,
"\"Predictor\" value %d not supported",
157 if (sp->rowsize == 0)
389#define REPEAT4(n, op) \
395 for (i = n - 4; i > 0; i--) \
441 const uint8_t *previous)
445 memcpy(&thisval, current,
sizeof(thisval));
446 memcpy(&prevval, previous,
sizeof(prevval));
448 (uint16_t)(((
unsigned int)thisval + (
unsigned int)prevval) & 0xffff);
449 memcpy(current, &thisval,
sizeof(thisval));
468 const uint8_t *previous)
472 memcpy(&thisval, current,
sizeof(thisval));
473 memcpy(&prevval, previous,
sizeof(prevval));
475 memcpy(current, &thisval,
sizeof(thisval));
494 const uint8_t *previous)
498 memcpy(&thisval, current,
sizeof(thisval));
499 memcpy(&prevval, previous,
sizeof(prevval));
501 memcpy(current, &thisval,
sizeof(thisval));
601 if ((cc % stride) != 0)
614 uint32_t acc = cp[0];
616 for (; i < cc - 3; i += 4)
618 cp[i + 0] = (uint8_t)((acc += cp[i + 0]) & 0xff);
619 cp[i + 1] = (uint8_t)((acc += cp[i + 1]) & 0xff);
620 cp[i + 2] = (uint8_t)((acc += cp[i + 2]) & 0xff);
621 cp[i + 3] = (uint8_t)((acc += cp[i + 3]) & 0xff);
625 cp[i + 0] = (uint8_t)((acc += cp[i + 0]) & 0xff);
628 else if (stride == 3)
634 for (; i < cc; i += stride)
636 cp[i + 0] = (uint8_t)((cr += cp[i + 0]) & 0xff);
637 cp[i + 1] = (uint8_t)((cg += cp[i + 1]) & 0xff);
638 cp[i + 2] = (uint8_t)((cb += cp[i + 2]) & 0xff);
641 else if (stride == 4)
648 for (; i < cc; i += stride)
650 cp[i + 0] = (uint8_t)((cr += cp[i + 0]) & 0xff);
651 cp[i + 1] = (uint8_t)((cg += cp[i + 1]) & 0xff);
652 cp[i + 2] = (uint8_t)((cb += cp[i + 2]) & 0xff);
653 cp[i + 3] = (uint8_t)((ca += cp[i + 3]) & 0xff);
662 cp[stride] = (uint8_t)((cp[stride] + *cp) & 0xff);
721 if ((cc % (2 * stride)) != 0)
791 if ((cc % (4 * stride)) != 0)
861 if ((cc % (8 * stride)) != 0)
912 if (cc % (bps * stride) != 0)
928 cp[1] = (uint8_t)((cp[1] + cp[0]) & 0xff); \
931 for (; count > 8; count -= 8)
942 for (; count > 1; count -= 1)
950 while (count > stride)
952 REPEAT4(stride, cp[stride] = (uint8_t)((cp[stride] + cp[0]) & 0xff);
962#if defined(__x86_64__) || (defined(_M_X64) && !defined(_M_ARM64EC))
966 for (; count + 15 < wc; count += 16)
971 _mm_loadu_si128((
const __m128i *)(tmp + count + 3 * wc));
973 _mm_loadu_si128((
const __m128i *)(tmp + count + 2 * wc));
975 _mm_loadu_si128((
const __m128i *)(tmp + count + 1 * wc));
977 _mm_loadu_si128((
const __m128i *)(tmp + count + 0 * wc));
979 __m128i tmp0 = _mm_unpacklo_epi8(xmm0, xmm1);
981 __m128i tmp1 = _mm_unpackhi_epi8(xmm0, xmm1);
983 __m128i tmp2 = _mm_unpacklo_epi8(xmm2, xmm3);
985 __m128i tmp3 = _mm_unpackhi_epi8(xmm2, xmm3);
988 __m128i tmp2_0 = _mm_unpacklo_epi16(tmp0, tmp2);
989 __m128i tmp2_1 = _mm_unpackhi_epi16(tmp0, tmp2);
990 __m128i tmp2_2 = _mm_unpacklo_epi16(tmp1, tmp3);
991 __m128i tmp2_3 = _mm_unpackhi_epi16(tmp1, tmp3);
992 _mm_storeu_si128((__m128i *)(cp + 4 * count + 0 * 16), tmp2_0);
993 _mm_storeu_si128((__m128i *)(cp + 4 * count + 1 * 16), tmp2_1);
994 _mm_storeu_si128((__m128i *)(cp + 4 * count + 2 * 16), tmp2_2);
995 _mm_storeu_si128((__m128i *)(cp + 4 * count + 3 * 16), tmp2_3);
1000 for (; count < wc; count++)
1003 for (
byte = 0;
byte < bps;
byte++)
1006 cp[bps * count + byte] = tmp[
byte * wc + count];
1008 cp[bps * count + byte] = tmp[(bps -
byte - 1) * wc + count];
1045 if ((*sp->
decoderow)(tif, op0, occ0, s))
1089 assert(rowsize > 0);
1090 if ((occ0 % rowsize) != 0)
1093 "occ0%rowsize != 0");
1131 unsigned char *cp = (
unsigned char *)cp0;
1133 if ((cc % stride) != 0)
1147 unsigned int r1, g1, b1;
1148 unsigned int r2 = cp[0];
1149 unsigned int g2 = cp[1];
1150 unsigned int b2 = cp[2];
1154 cp[3] = (
unsigned char)((r1 - r2) & 0xff);
1157 cp[4] = (
unsigned char)((g1 - g2) & 0xff);
1160 cp[5] = (
unsigned char)((b1 - b2) & 0xff);
1163 }
while ((cc -= 3) > 0);
1165 else if (stride == 4)
1167 unsigned int r1, g1, b1, a1;
1168 unsigned int r2 = cp[0];
1169 unsigned int g2 = cp[1];
1170 unsigned int b2 = cp[2];
1171 unsigned int a2 = cp[3];
1175 cp[4] = (
unsigned char)((r1 - r2) & 0xff);
1178 cp[5] = (
unsigned char)((g1 - g2) & 0xff);
1181 cp[6] = (
unsigned char)((b1 - b2) & 0xff);
1184 cp[7] = (
unsigned char)((a1 - a2) & 0xff);
1187 }
while ((cc -= 4) > 0);
1196 (
unsigned char)((cp[stride] - cp[0]) & 0xff);
1198 }
while ((cc -= stride) > 0);
1226 uint16_t *wp = (uint16_t *)cp0;
1229 if ((cc % (2 * stride)) != 0)
1241 REPEAT4(stride, wp[stride] = (uint16_t)(((
unsigned int)wp[stride] -
1242 (
unsigned int)wp[0]) &
1270 uint16_t *wp = (uint16_t *)cp0;
1302 uint32_t *wp = (uint32_t *)cp0;
1305 if ((cc % (4 * stride)) != 0)
1307 TIFFErrorExtR(tif,
"horDiff32",
"%s",
"(cc%(4*stride))!=0");
1317 REPEAT4(stride, wp[stride] -= wp[0]; wp--)
1343 uint32_t *wp = (uint32_t *)cp0;
1375 uint64_t *wp = (uint64_t *)cp0;
1378 if ((cc % (8 * stride)) != 0)
1380 TIFFErrorExtR(tif,
"horDiff64",
"%s",
"(cc%(8*stride))!=0");
1390 REPEAT4(stride, wp[stride] -= wp[0]; wp--)
1416 uint64_t *wp = (uint64_t *)cp0;
1455 uint8_t *cp = (uint8_t *)cp0;
1458 if ((cc % (bps * stride)) != 0)
1469 for (count = 0; count < wc; count++)
1472 for (
byte = 0;
byte < bps;
byte++)
1475 cp[
byte * wc + count] = tmp[bps * count + byte];
1477 cp[(bps -
byte - 1) * wc + count] = tmp[bps * count +
byte];
1483 cp = (uint8_t *)cp0;
1484 cp += cc - stride - 1;
1485 for (count = cc; count > stride; count -= stride)
1487 cp[stride] = (
unsigned char)((cp[stride] - cp[0]) & 0xff);
1512 static const char module[] = "PredictorEncodeRow";
1514 uint8_t *working_copy;
1518 assert(sp->encodepfunc != NULL);
1519 assert(sp->encoderow != NULL);
1527 if (working_copy == NULL)
1530 "Out of memory allocating %" PRId64
" byte temp buffer.",
1534 memcpy(working_copy, bp, (
size_t)cc);
1536 if (!(*sp->encodepfunc)(tif, working_copy, cc))
1541 result_code = (*sp->encoderow)(tif, working_copy, cc, s);
1569 static const char module[] = "PredictorEncodeTile";
1571 uint8_t *working_copy;
1577 assert(sp->encodepfunc != NULL);
1578 assert(sp->encodetile != NULL);
1585 if (working_copy == NULL)
1588 "Out of memory allocating %" PRId64
" byte temp buffer.",
1592 memcpy(working_copy, bp0, (
size_t)cc0);
1595 rowsize = sp->rowsize;
1596 assert(rowsize > 0);
1597 if ((cc0 % rowsize) != 0)
1599 TIFFErrorExtR(tif,
"PredictorEncodeTile",
"%s",
"(cc0%rowsize)!=0");
1605 if (!(*sp->encodepfunc)(tif, bp, rowsize))
1613 result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
1620#define FIELD_PREDICTOR (FIELD_CODEC + 0)
1699 *va_arg(ap, uint16_t *) = (uint16_t)sp->
predictor;
1728 fprintf(fd,
" Predictor: ");
1732 fprintf(fd,
"none ");
1735 fprintf(fd,
"horizontal differencing ");
1738 fprintf(fd,
"floating point predictor ");
1761 "Merging Predictor codec-specific tags failed");
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Multiply two unsigned values with an overflow check.
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Merge borrowed field definitions into the lookup table.
#define TIFFFieldSet(tif, field)
Test a directory presence bit.
#define TIFFSetFieldBit(tif, field)
Set a directory presence bit.
@ TIFF_SETGET_UINT16
Unsigned 16-bit scalar storage.
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
Leave decoded bytes unchanged.
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Accumulate 16-bit horizontal predictor samples in place.
static void PredictorSwabByteStream64(uint8_t *cp, tmsize_t n)
Reverse each 64-bit sample in an unaligned byte stream.
static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Swap and difference 16-bit horizontal predictor samples in place.
static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Difference 16-bit horizontal predictor samples in place.
static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
Decode one row then undo prediction in place.
#define PredictorState(tif)
Borrow the predictor prefix of codec-owned tif_data.
static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Difference 32-bit horizontal predictor samples in place.
static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Apply floating-point byte-plane prediction on one row.
int TIFFPredictorCleanup(TIFF *tif)
Restore saved Predictor tag print and setup hooks.
static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Difference 64-bit horizontal predictor samples in place.
#define REPEAT4(n, op)
Repeat a statement using an unrolled small-count switch.
static void PredictorPrintDir(TIFF *tif, FILE *fd, long flags)
Print predictor information then chain the parent printer.
static int PredictorSetupDecode(TIFF *tif)
Chain parent decoder setup and install predictor decoding.
static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Swap and accumulate 64-bit horizontal predictor samples in place.
static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Accumulate 64-bit horizontal predictor samples in place.
static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Swap and accumulate 16-bit horizontal predictor samples in place.
static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Difference 8-bit horizontal predictor samples in place.
static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Swap and difference 64-bit horizontal predictor samples in place.
static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Transform a temporary row copy then call the parent encoder.
static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Undo floating-point byte-plane prediction on one row.
static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW void PredictorAccumulate32NativeUnaligned(uint8_t *current, const uint8_t *previous)
Add two unaligned native-order 32-bit samples modulo their width.
static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW void PredictorAccumulate16NativeUnaligned(uint8_t *current, const uint8_t *previous)
Add two unaligned native-order 16-bit samples modulo their width.
static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Accumulate 8-bit horizontal predictor samples in place.
static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Swap and accumulate 32-bit horizontal predictor samples in place.
#define FIELD_PREDICTOR
Reserve the codec field bit for Predictor.
static void PredictorSwabByteStream32(uint8_t *cp, tmsize_t n)
Reverse each 32-bit sample in an unaligned byte stream.
#define OP
Accumulate the next byte in the floating-point fast path.
static int PredictorSetupEncode(TIFF *tif)
Chain parent encoder setup and install predictor encoding.
static int PredictorSetup(TIFF *tif)
Validate predictor format and cache stride and row extent.
int TIFFPredictorInit(TIFF *tif)
Install Predictor tag and setup hooks around a codec.
static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Swap and difference 32-bit horizontal predictor samples in place.
static const TIFFField predictFields[]
Define the codec-owned Predictor field metadata.
static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
Accumulate 32-bit horizontal predictor samples in place.
static int PredictorVGetField(TIFF *tif, uint32_t tag, va_list ap)
Fetch Predictor or delegate another variadic tag query.
static void PredictorSwabByteStream16(uint8_t *cp, tmsize_t n)
Reverse each 16-bit sample in an unaligned byte stream.
static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
Decode a strile then undo prediction row by row.
static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW void PredictorAccumulate64NativeUnaligned(uint8_t *current, const uint8_t *previous)
Add two unaligned native-order 64-bit samples modulo their width.
static int PredictorVSetField(TIFF *tif, uint32_t tag, va_list ap)
Store Predictor or delegate another variadic tag assignment.
static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0, uint16_t s)
Transform a temporary strile copy then call the parent encoder.
#define isTiled(tif)
Test whether a handle uses tiled storage.
#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
Suppress unsigned-overflow instrumentation on supported Clang functions.
#define FALSE
Represent the legacy internal false value.
#define TIFF_DIRTYDIRECT
Mark a current directory that needs writing.
#define TIFF_SWAB
Request swapping multibyte file values to or from host order.
#define TIFFhowmany8_64(x)
Compute the byte count needed to hold a 64-bit bit count.
#define TIFFArrayCount(a)
Compute the element count of an actual array.
void TIFFSwabArrayOfLong8(uint64_t *lp, tmsize_t n)
Reverse the byte order of each array element.
void TIFFSwabArrayOfShort(uint16_t *wp, tmsize_t n)
Reverse the byte order of each array element.
void TIFFSwabArrayOfLong(uint32_t *lp, tmsize_t n)
Reverse the byte order of each array element.
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
int uint16_vap
Represent a uint16_t value after default variadic argument promotion.
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
@ TIFF_SHORT
Unsigned 16-bit integer.
Store the current directory values and image traversal state.
uint32_t td_tilewidth
Tile width in pixels.
uint16_t td_bitspersample
Bits in each sample.
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
uint16_t td_sampleformat
SAMPLEFORMAT_* interpretation of samples.
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Prefix codec state with Predictor method chains and row geometry.
tmsize_t stride
Positive channel stride in samples, not bytes, for active transforms.
TIFFEncodeDecodeMethod decodepfunc
Selected in-place row accumulator, NULL before setup.
TIFFVSetMethod vsetparent
Saved borrowed tag setter.
TIFFCodeMethod encoderow
Saved parent row encoder.
TIFFVGetMethod vgetparent
Saved borrowed tag getter.
TIFFCodeMethod encodetile
Saved parent strile encoder invoked by the shared wrapper.
TIFFCodeMethod encodestrip
Saved parent strip encoder; shared strile wrapper dispatch currently uses encodetile.
TIFFCodeMethod decodestrip
Saved parent strip decoder; shared strile wrapper dispatch currently uses decodetile.
tmsize_t rowsize
Positive decoded row byte extent for active strile transforms.
int predictor
Predictor tag value; validated at setup.
TIFFCodeMethod decodetile
Saved parent strile decoder invoked by the shared wrapper.
TIFFCodeMethod decoderow
Saved parent row decoder.
TIFFPrintMethod printdir
Saved optional borrowed directory printer.
TIFFBoolMethod setupdecode
Saved borrowed decoder setup method.
TIFFBoolMethod setupencode
Saved borrowed encoder setup method.
TIFFEncodeDecodeMethod encodepfunc
Selected in-place row differencer, NULL before setup.
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
TIFFPrintMethod printdir
Optional codec/extension directory-print callback.
Describe a field's file type and internal argument contract.
Hold private state for one open TIFF library instance.
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
TIFFPostMethod tif_postdecode
Callback transforming decoded data, for example by byte swapping.
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
TIFFDirectory tif_dir
Inline representation of the current image directory.
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Define the predictor prefix and method-chain contracts.
Declare private library state, callbacks, and implementation helpers.