44#define NOSTRIP ((uint32_t)(-1))
60#define WRITECHECKSTRIPS(tif, module) \
61 (((tif)->tif_flags & TIFF_BEENWRITING) || TIFFWriteCheck((tif), 0, module))
76#define WRITECHECKTILES(tif, module) \
77 (((tif)->tif_flags & TIFF_BEENWRITING) || TIFFWriteCheck((tif), 1, module))
91#define BUFFERCHECK(tif) \
92 ((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) || \
93 TIFFWriteBufferSetup((tif), NULL, (tmsize_t)(-1)))
101 static const char module[] = "TIFFWriteScanline";
103 int status, imagegrew = 0;
122 if (row >= td->td_imagelength)
128 "Can not change \"ImageLength\" when using separate planes");
131 td->td_imagelength = row + 1;
137 if (td->td_rowsperstrip == 0)
140 "Cannot compute strip: RowsPerStrip is zero");
145 uint64_t sample_offset;
147 if (sample >= td->td_samplesperpixel)
149 TIFFErrorExtR(tif, module,
"%lu: Sample out of range, max %lu",
150 (
unsigned long)sample,
151 (
unsigned long)td->td_samplesperpixel);
156 if (sample_offset == 0 && sample != 0 && td->td_stripsperimage != 0)
159 _TIFFAdd64(tif, sample_offset, row / td->td_rowsperstrip, module);
161 (sample_offset != 0 || (row / td->td_rowsperstrip) != 0))
164 if (strip == 0 && strip64 != 0)
168 strip = row / td->td_rowsperstrip;
191 if (strip >= td->td_stripsperimage && imagegrew)
192 td->td_stripsperimage =
194 if (td->td_stripsperimage == 0)
226 if (row < tif->tif_dir.td_row)
237 assert(td->td_stripsperimage != 0);
293 static const char module[] = "_TIFFReserveLargeEnoughWriteBuffer";
296 if (td->td_stripbytecount_p == NULL)
298 TIFFErrorExtR(tif, module,
"Strip bytecount array pointer is NULL");
302 if (strip_or_tile ==
NOSTRIP || strip_or_tile >= td->td_nstrips)
308 if (td->td_stripbytecount_p[strip_or_tile] > 0)
312 uint64_t safe_buffer_size =
313 (uint64_t)(td->td_stripbytecount_p[strip_or_tile] + 1 + 4);
334 static const char module[] = "TIFFWriteEncodedStrip";
349 if (strip >= td->td_nstrips)
355 "Can not grow image by strips when using separate planes");
360 if (strip > td->td_nstrips)
362 TIFFErrorExtR(tif, module,
"%lu: Strip out of range, max %lu",
363 (
unsigned long)strip, (
unsigned long)td->td_nstrips);
368 td->td_stripsperimage =
394 if (td->td_stripsperimage == 0)
429 sample = (uint16_t)(strip / td->td_stripsperimage);
460 static const char module[] = "TIFFWriteRawStrip";
479 if (strip >= td->td_nstrips)
485 "Can not grow image by strips when using separate planes");
495 if (strip > td->td_nstrips)
497 TIFFErrorExtR(tif, module,
"%lu: Strip out of range, max %lu",
498 (
unsigned long)strip, (
unsigned long)td->td_nstrips);
501 if (strip >= td->td_stripsperimage)
502 td->td_stripsperimage =
516 if (td->td_stripsperimage == 0)
535 bool overflow =
false;
567 static const char module[] = "TIFFWriteEncodedTile";
575 if (tile >= td->td_nstrips)
578 (
unsigned long)tile, (
unsigned long)td->td_nstrips);
608 howmany32 =
TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
660 sample = (uint16_t)(tile / td->td_stripsperimage);
694 static const char module[] = "TIFFWriteRawTile";
726#define isUnspecified(tif, f) \
727 (TIFFFieldSet(tif, f) && (tif)->tif_dir.td_imagelength == 0)
747 "Too large Strip/Tile Offsets/ByteCounts arrays");
756 tif, td->
td_nstrips,
sizeof(uint64_t),
"for \"StripOffsets\" array");
760 tif, td->
td_nstrips,
sizeof(uint64_t),
"for \"StripByteCounts\" array");
793 tiles ?
"Can not write tiles to a striped image"
794 :
"Can not write scanlines to a tiled image");
813 "Must set \"ImageWidth\" before writing data");
820 isTiled(tif) ?
"tile" :
"strip");
862 static const char module[] = "TIFFWriteBufferSetup";
865 if (size != (
tmsize_t)(-1) && size <= 0)
942 uint64_t *new_stripoffset;
943 uint64_t *new_stripbytecount;
952 const uint64_t new_nstrips = (uint64_t)td->
td_nstrips + delta;
957 "Too large Strip/Tile Offsets/ByteCounts arrays");
962 if (arrays_size == 0)
974 if (new_stripbytecount)
976 if (new_stripoffset == NULL || new_stripbytecount == NULL)
979 TIFFErrorExtR(tif, module,
"No space to expand strip arrays");
983 (
tmsize_t)((
size_t)delta *
sizeof(uint64_t)));
985 (
tmsize_t)((
size_t)delta *
sizeof(uint64_t)));
1024 static const char module[] = "TIFFAppendToStrip";
1027 int64_t old_byte_count = -1;
1030 if (td->td_stripoffset_p == NULL)
1032 TIFFErrorExtR(tif, module,
"Strip offset array pointer is NULL");
1035 if (td->td_stripbytecount_p == NULL)
1037 TIFFErrorExtR(tif, module,
"Strip bytecount array pointer is NULL");
1042 TIFFErrorExtR(tif, module,
"Strip number not valid (NOSTRIP)");
1045 if (strip >= td->td_nstrips)
1047 TIFFErrorExtR(tif, module,
"%lu: Strip out of range, max %lu",
1048 (
unsigned long)strip, (
unsigned long)td->td_nstrips);
1055 if (td->td_stripoffset_p[strip] == 0 || tif->
tif_curoff == 0)
1057 assert(td->td_nstrips > 0);
1059 if (td->td_stripbytecount_p[strip] != 0 &&
1060 td->td_stripoffset_p[strip] != 0 &&
1061 td->td_stripbytecount_p[strip] >= (uint64_t)cc)
1070 if (!
SeekOK(tif, td->td_stripoffset_p[strip]))
1078 td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip];
1086 td->td_stripoffset_p[strip] =
TIFFSeekFile(tif, 0, SEEK_END);
1090 tif->
tif_curoff = td->td_stripoffset_p[strip];
1095 old_byte_count = (int64_t)td->td_stripbytecount_p[strip];
1096 td->td_stripbytecount_p[strip] = 0;
1102 if ((m < tif->tif_curoff) || (m < (uint64_t)cc))
1104 TIFFErrorExtR(tif, module,
"Maximum TIFF file size exceeded");
1109 td->td_stripbytecount_p[strip] > 0)
1121 uint64_t offsetRead;
1122 uint64_t offsetWrite;
1123 uint64_t toCopy = td->td_stripbytecount_p[strip];
1125 if (toCopy < 1024 * 1024)
1128 tempSize = 1024 * 1024;
1130 offsetRead = td->td_stripoffset_p[strip];
1133 m = offsetWrite + (uint64_t)toCopy + (uint64_t)cc;
1136 TIFFErrorExtR(tif, module,
"Maximum TIFF file size exceeded");
1149 td->td_stripoffset_p[strip] = offsetWrite;
1150 td->td_stripbytecount_p[strip] = 0;
1156 toCopy < (uint64_t)tempSize ? (
tmsize_t)toCopy : tempSize;
1157 if (!
SeekOK(tif, offsetRead))
1163 if (!
ReadOK(tif, temp, chunkSize))
1169 if (!
SeekOK(tif, offsetWrite))
1175 if (!
WriteOK(tif, temp, chunkSize))
1181 offsetRead += (uint64_t)chunkSize;
1182 offsetWrite += (uint64_t)chunkSize;
1183 td->td_stripbytecount_p[strip] += (uint64_t)chunkSize;
1184 toCopy -= (uint64_t)chunkSize;
1189 offsetWrite += (uint64_t)cc;
1200 td->td_stripbytecount_p[strip] += (uint64_t)cc;
1202 if ((int64_t)td->td_stripbytecount_p[strip] != old_byte_count)
uint32_t _TIFFCastUInt64ToUInt32(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
uint64_t _TIFFAdd64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Check and add two unsigned operands.
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Multiply two unsigned values with an overflow check.
tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
Check and multiply two signed-size operands.
#define TIFF_SSIZE_FORMAT
Provide the printf conversion fragment for tmsize_t, without a percent sign.
#define FIELD_STRIPOFFSETS
Index the strile offsets presence bit; not an on-disk tag number.
#define FIELD_IMAGEDIMENSIONS
Index the image dimensions presence bit; not an on-disk tag number.
#define TIFFFieldSet(tif, field)
Test a directory presence bit.
#define FIELD_TILEDIMENSIONS
Index the tile dimensions presence bit; not an on-disk tag number.
#define FIELD_ROWSPERSTRIP
Index the rows per strip presence bit; not an on-disk tag number.
#define FIELD_STRIPBYTECOUNTS
Index the strile byte counts presence bit; not an on-disk tag number.
#define TIFFSetFieldBit(tif, field)
Set a directory presence bit.
uint32_t _TIFFComputeTileExt(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s, bool *pOverflowed)
Compute a tile index as TIFFComputeTile() does, and record an index overflow in a caller-provided fla...
#define BUFFERCHECK(tif)
Reuse an output buffer or allocate default encoding storage.
int _TIFFCheckPostDecodeLength(TIFF *tif, TIFFPostMethod tif_postdecode, tmsize_t cc, const char *modulename)
Check the byte count for a byte-swapping postdecode method.
#define isUnspecified(tif, f)
Test the zero-image-length provisional strile case.
#define NOSTRIP
Local repeat of the NOSTRIP sentinel defined in tif_dir.h.
#define WRITECHECKTILES(tif, module)
Reuse write preparation or check tile organisation.
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
static int _TIFFReserveLargeEnoughWriteBuffer(TIFF *tif, uint32_t strip_or_tile)
Reserve raw output capacity for a possible strile rewrite.
static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data, tmsize_t cc)
Append stored bytes or relocate a growing overwritten strile.
static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module)
Grow contiguous strip offset/count arrays by a requested delta.
#define WRITECHECKSTRIPS(tif, module)
Reuse write preparation or check strip organisation.
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
void * _TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
Resize a handle-aware block or release it for a nonpositive size.
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Allocate a checked array with an allocation-failure diagnostic.
#define TIFFroundup_64(x, y)
Round a 64-bit value up to a multiple without overflow checks.
#define isTiled(tif)
Test whether a handle uses tiled storage.
#define WriteOK(tif, buf, size)
Test whether one client write transfers the entire requested byte count.
#define TIFF_MYBUFFER
Mark a raw buffer owned and released by the handle.
#define TIFF_BUFFERSETUP
Mark completed data-buffer setup.
#define ReadOK(tif, buf, size)
Test whether one client read transfers the entire requested byte count.
#define TIFF_CODERSETUP
Mark completed codec setup.
#define TIFFSafeModMul32(tif, module, index, count, dimension, result)
Compute (index modulo count) times dimension with a checked product.
#define TIFF_POSTENCODE
Request a pending codec postencode call.
#define TIFF_BUF4WRITE
Interpret the raw buffer and its byte count as pending output.
#define SeekOK(tif, off)
Test whether an absolute seek reaches the requested file position.
#define isFillOrder(tif, o)
Test whether selected bit-fill-order bits are present in a handle.
#define TIFF_BEENWRITING
Record that scanline or image-data writing has begun.
#define TIFF_DIRTYSTRIP
Mark strip/tile offset or byte-count arrays that need writing.
#define TIFFSeekFile(tif, off, whence)
Invoke the handle's client seek callback.
#define TIFF_DIRTYDIRECT
Mark a current directory that needs writing.
#define TIFF_BIGTIFF
Mark the BigTIFF file format.
#define TIFFhowmany_32(x, y)
Compute a guarded 32-bit ceiling quotient.
#define TIFF_NOBITREV
Suppress automatic bit reversal.
void TIFFReverseBits(uint8_t *cp, tmsize_t n)
Reverse the bit order within each byte of a buffer.
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
int TIFFForceStrileArrayWriting(TIFF *tif)
Patch on-file strile offset and byte-count payloads.
int TIFFWriteBufferSetup(TIFF *tif, void *bp, tmsize_t size)
Replace the handle's encoding buffer.
int TIFFWriteCheck(TIFF *tif, int tiles, const char *module)
Validate and prepare geometry for image writing.
int TIFFSetupStrips(TIFF *tif)
Replace strile offset/count arrays from current geometry.
tmsize_t TIFFTileSize(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
tmsize_t TIFFStripSize(TIFF *tif)
Compute the decoded-size estimate for a nominal strip.
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
uint32_t TIFFNumberOfStrips(TIFF *tif)
Compute the strip count from current image geometry.
int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
Check pixel and separate-sample coordinates against image bounds.
uint32_t TIFFNumberOfTiles(TIFF *tif)
Compute the tile count across image depth and sample planes.
tmsize_t TIFFWriteRawTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc)
Write raw stored bytes to one tile.
tmsize_t TIFFWriteRawStrip(TIFF *tif, uint32_t strip, void *data, tmsize_t cc)
Write raw stored bytes to one strip.
void TIFFSetWriteOffset(TIFF *tif, toff_t off)
Change the cached next payload write offset.
int TIFFWriteScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)
Encode and buffer one image scanline.
tmsize_t TIFFWriteEncodedTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc)
Write encoded samples to one tile.
tmsize_t TIFFWriteTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
Encode the tile containing image coordinates.
tmsize_t TIFFWriteEncodedStrip(TIFF *tif, uint32_t strip, void *data, tmsize_t cc)
Write encoded samples to one strip.
int TIFFFlushData(TIFF *tif)
Flush pending encoded image data without committing directory changes.
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
#define TIFF_TMSIZE_T_MAX
Define the largest positive size representable by tmsize_t.
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
uint64_t tdir_count
Number of file-type elements, normalised to host order.
uint16_t tdir_type
On-disk TIFFDataType code.
uint64_t toff_long8
Raw 64-bit view of inline bytes or offset storage.
uint16_t tdir_tag
Directory tag identifier.
union TIFFDirEntry::@0 tdir_offset
Overlay raw directory inline bytes and offset-width views.
Store the current directory values and image traversal state.
uint16_t td_fillorder
FILLORDER_* bit order within file bytes.
tmsize_t td_tilesize
Cached decoded tile size in bytes; minus one when invalidated.
tmsize_t td_scanlinesize
Cached decoded scanline size in bytes; zero when not computed.
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
TIFFDirEntry td_stripoffset_entry
Deferred offset entry; tag/type/count are swapped but offset storage is raw.
uint32_t td_stripsperimage
Strips or tiles per sample plane, despite the historic name.
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
uint64_t * td_stripbytecount_p
Owned byte-count array/cache, possibly NULL or partial; use TIFFGetStrileByteCount().
uint64_t * td_stripoffset_p
Owned offset array/cache, possibly NULL or partial; use TIFFGetStrileOffset().
uint32_t td_col
Current pixel column; UINT32_MAX before traversal starts.
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
uint32_t td_nstrips
Total strile count across all planes; logical array length.
TIFFDirEntry td_stripbytecount_entry
Deferred byte-count entry; offset storage still requires swapping.
uint32_t td_curtile
Current tile index, or NOTILE.
uint32_t td_curstrip
Current strip index, or NOSTRIP.
Hold private state for one open TIFF library instance.
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
uint64_t tif_lastvalidoff
Last byte-offset boundary permitted for rewriting strip data in place.
TIFFSeekMethod tif_seek
Codec callback advancing within a strip.
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
uint64_t tif_curoff
Tracked current file byte offset for read/write operations.
TIFFPostMethod tif_postdecode
Callback transforming decoded data, for example by byte swapping.
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
TIFFDirectory tif_dir
Inline representation of the current image directory.
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
int tif_mode
Current access mode using the O_* constants.
Declare private library state, callbacks, and implementation helpers.