LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_write.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25/*
26 * TIFF Library.
27 *
28 * Scanline-oriented Write Support
29 */
35#include "tiffiop.h"
36
37#include <assert.h>
38#include <stdio.h>
39
44#define NOSTRIP ((uint32_t)(-1)) /* undefined state */
45
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)))
94
95static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module);
96static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data,
97 tmsize_t cc);
98
99int TIFFWriteScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)
100{
101 static const char module[] = "TIFFWriteScanline";
102 TIFFDirectory *td;
103 int status, imagegrew = 0;
104 uint32_t strip;
105
106 if (!WRITECHECKSTRIPS(tif, module))
107 return (-1);
108 /*
109 * Handle delayed allocation of data buffer. This
110 * permits it to be sized more intelligently (using
111 * directory information).
112 */
113 if (!BUFFERCHECK(tif))
114 return (-1);
115 tif->tif_flags |= TIFF_BUF4WRITE; /* not strictly sure this is right*/
116
117 td = &tif->tif_dir;
118 /*
119 * Extend image length if needed
120 * (but only for PlanarConfig=1).
121 */
122 if (row >= td->td_imagelength)
123 { /* extend image */
124 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
125 {
127 tif, module,
128 "Can not change \"ImageLength\" when using separate planes");
129 return (-1);
130 }
131 td->td_imagelength = row + 1;
132 imagegrew = 1;
133 }
134 /*
135 * Calculate strip and check for crossings.
136 */
137 if (td->td_rowsperstrip == 0)
138 {
139 TIFFErrorExtR(tif, module,
140 "Cannot compute strip: RowsPerStrip is zero");
141 return (-1);
142 }
143 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
144 {
145 uint64_t sample_offset;
146 uint64_t strip64;
147 if (sample >= td->td_samplesperpixel)
148 {
149 TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
150 (unsigned long)sample,
151 (unsigned long)td->td_samplesperpixel);
152 return (-1);
153 }
154 sample_offset =
155 _TIFFMultiply64(tif, sample, td->td_stripsperimage, module);
156 if (sample_offset == 0 && sample != 0 && td->td_stripsperimage != 0)
157 return (-1);
158 strip64 =
159 _TIFFAdd64(tif, sample_offset, row / td->td_rowsperstrip, module);
160 if (strip64 == 0 &&
161 (sample_offset != 0 || (row / td->td_rowsperstrip) != 0))
162 return (-1);
163 strip = _TIFFCastUInt64ToUInt32(tif, strip64, module);
164 if (strip == 0 && strip64 != 0)
165 return (-1);
166 }
167 else
168 strip = row / td->td_rowsperstrip;
169 /*
170 * Check strip array to make sure there's space. We don't support
171 * dynamically growing files that have data organized in separate
172 * bitplanes because it's too painful. In that case we require that
173 * the imagelength be set properly before the first write (so that the
174 * strips array will be fully allocated above).
175 */
176 if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
177 return (-1);
178 if (strip != tif->tif_dir.td_curstrip)
179 {
180 /*
181 * Changing strips -- flush any data present.
182 */
183 if (!TIFFFlushData(tif))
184 return (-1);
185 tif->tif_dir.td_curstrip = strip;
186 /*
187 * Watch out for a growing image. The value of strips/image
188 * will initially be 1 (since it can't be deduced until the
189 * imagelength is known).
190 */
191 if (strip >= td->td_stripsperimage && imagegrew)
192 td->td_stripsperimage =
193 TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip);
194 if (td->td_stripsperimage == 0)
195 {
196 TIFFErrorExtR(tif, module, "Zero strips per image");
197 return (-1);
198 }
199 if (TIFFSafeModMul32(tif, module, strip, td->td_stripsperimage,
200 td->td_rowsperstrip, tif->tif_dir.td_row))
201 return (-1);
202 if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
203 {
204 if (!(*tif->tif_setupencode)(tif))
205 return (-1);
207 }
208
209 tif->tif_rawcc = 0;
210 tif->tif_rawcp = tif->tif_rawdata;
211
212 /* this informs TIFFAppendToStrip() we have changed strip */
213 tif->tif_curoff = 0;
214
215 if (!(*tif->tif_preencode)(tif, sample))
216 return (-1);
218 }
219 /*
220 * Ensure the write is either sequential or at the
221 * beginning of a strip (or that we can randomly
222 * access the data -- i.e. no encoding).
223 */
224 if (row != tif->tif_dir.td_row)
225 {
226 if (row < tif->tif_dir.td_row)
227 {
228 /*
229 * Moving backwards within the same strip:
230 * backup to the start and then decode
231 * forward (below).
232 */
233 /* Coverity Scan warns about a potential division by zero given
234 * an above check against zero, but I believe this is a false
235 * positive.
236 */
237 assert(td->td_stripsperimage != 0);
238 if (TIFFSafeModMul32(tif, module, strip, td->td_stripsperimage,
239 td->td_rowsperstrip, tif->tif_dir.td_row))
240 return (-1);
241 tif->tif_rawcp = tif->tif_rawdata;
242 }
243 /*
244 * Seek forward to the desired row.
245 */
246 if (!(*tif->tif_seek)(tif, row - tif->tif_dir.td_row))
247 return (-1);
248 tif->tif_dir.td_row = row;
249 }
250
251 /* swab if needed - note that source buffer will be altered */
253 tif->tif_dir.td_scanlinesize, module))
254 return (-1);
255 tif->tif_postdecode(tif, (uint8_t *)buf, tif->tif_dir.td_scanlinesize);
256
257 status = (*tif->tif_encoderow)(tif, (uint8_t *)buf,
258 tif->tif_dir.td_scanlinesize, sample);
259
260 /* we are now poised at the beginning of the next row */
261 tif->tif_dir.td_row = row + 1;
262 return (status);
263}
285/* Make sure that at the first attempt of rewriting a tile/strip, we will have
286 */
287/* more bytes available in the output buffer than the previous byte count, */
288/* so that TIFFAppendToStrip() will detect the overflow when it is called the
289 * first */
290/* time if the new compressed tile is bigger than the older one. (GDAL #4771) */
291static int _TIFFReserveLargeEnoughWriteBuffer(TIFF *tif, uint32_t strip_or_tile)
292{
293 static const char module[] = "_TIFFReserveLargeEnoughWriteBuffer";
294 TIFFDirectory *td = &tif->tif_dir;
295
296 if (td->td_stripbytecount_p == NULL)
297 {
298 TIFFErrorExtR(tif, module, "Strip bytecount array pointer is NULL");
299 return 0;
300 }
301
302 if (strip_or_tile == NOSTRIP || strip_or_tile >= td->td_nstrips)
303 {
304 TIFFErrorExtR(tif, module, "Strip/tile number not valid");
305 return 0;
306 }
307
308 if (td->td_stripbytecount_p[strip_or_tile] > 0)
309 {
310 /* The +1 is to ensure at least one extra bytes */
311 /* The +4 is because the LZW encoder flushes 4 bytes before the limit */
312 uint64_t safe_buffer_size =
313 (uint64_t)(td->td_stripbytecount_p[strip_or_tile] + 1 + 4);
314 if (tif->tif_rawdatasize <= (tmsize_t)safe_buffer_size)
315 {
317 tif, NULL,
318 (tmsize_t)TIFFroundup_64(safe_buffer_size, 1024))))
319 return 0;
320 }
321 }
322 return 1;
323}
324
325/*
326 * Encode the supplied data and write it to the
327 * specified strip.
328 *
329 * NB: Image length must be setup before writing.
330 */
331tmsize_t TIFFWriteEncodedStrip(TIFF *tif, uint32_t strip, void *data,
332 tmsize_t cc)
333{
334 static const char module[] = "TIFFWriteEncodedStrip";
335 TIFFDirectory *td = &tif->tif_dir;
336 uint16_t sample;
337
338 if (!WRITECHECKSTRIPS(tif, module))
339 return ((tmsize_t)-1);
340 /*
341 * Check strip array to make sure there's space.
342 * We don't support dynamically growing files that
343 * have data organized in separate bitplanes because
344 * it's too painful. In that case we require that
345 * the imagelength be set properly before the first
346 * write (so that the strips array will be fully
347 * allocated above).
348 */
349 if (strip >= td->td_nstrips)
350 {
351 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
352 {
354 tif, module,
355 "Can not grow image by strips when using separate planes");
356 return ((tmsize_t)-1);
357 }
358 /* TIFFGrowStrips() below extends the strip arrays by exactly one
359 * entry, so only the index immediately past the end can be served. */
360 if (strip > td->td_nstrips)
361 {
362 TIFFErrorExtR(tif, module, "%lu: Strip out of range, max %lu",
363 (unsigned long)strip, (unsigned long)td->td_nstrips);
364 return ((tmsize_t)-1);
365 }
366 if (!TIFFGrowStrips(tif, 1, module))
367 return ((tmsize_t)-1);
368 td->td_stripsperimage =
369 TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip);
370 }
371 /*
372 * Handle delayed allocation of data buffer. This
373 * permits it to be sized according to the directory
374 * info.
375 */
376 if (!BUFFERCHECK(tif))
377 return ((tmsize_t)-1);
378
380
381 tif->tif_dir.td_curstrip = strip;
382
383 /* this informs TIFFAppendToStrip() we have changed or reset strip */
384 tif->tif_curoff = 0;
385
386 if (!_TIFFReserveLargeEnoughWriteBuffer(tif, strip))
387 {
388 return ((tmsize_t)(-1));
389 }
390
391 tif->tif_rawcc = 0;
392 tif->tif_rawcp = tif->tif_rawdata;
393
394 if (td->td_stripsperimage == 0)
395 {
396 TIFFErrorExtR(tif, module, "Zero strips per image");
397 return ((tmsize_t)-1);
398 }
399
400 if (TIFFSafeModMul32(tif, module, strip, td->td_stripsperimage,
401 td->td_rowsperstrip, tif->tif_dir.td_row))
402 return ((tmsize_t)-1);
403 if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
404 {
405 if (!(*tif->tif_setupencode)(tif))
406 return ((tmsize_t)-1);
408 }
409
410 tif->tif_flags &= ~TIFF_POSTENCODE;
411
412 /* shortcut to avoid an extra memcpy() */
413 if (td->td_compression == COMPRESSION_NONE)
414 {
415 /* swab if needed - note that source buffer will be altered */
416 if (!_TIFFCheckPostDecodeLength(tif, tif->tif_postdecode, cc, module))
417 return ((tmsize_t)(-1));
418 tif->tif_postdecode(tif, (uint8_t *)data, cc);
419
420 if (!isFillOrder(tif, td->td_fillorder) &&
421 (tif->tif_flags & TIFF_NOBITREV) == 0)
422 TIFFReverseBits((uint8_t *)data, cc);
423
424 if (cc > 0 && !TIFFAppendToStrip(tif, strip, (uint8_t *)data, cc))
425 return ((tmsize_t)-1);
426 return (cc);
427 }
428
429 sample = (uint16_t)(strip / td->td_stripsperimage);
430 if (!(*tif->tif_preencode)(tif, sample))
431 return ((tmsize_t)-1);
432
433 /* swab if needed - note that source buffer will be altered */
434 if (!_TIFFCheckPostDecodeLength(tif, tif->tif_postdecode, cc, module))
435 return ((tmsize_t)(-1));
436 tif->tif_postdecode(tif, (uint8_t *)data, cc);
437
438 if (!(*tif->tif_encodestrip)(tif, (uint8_t *)data, cc, sample))
439 return ((tmsize_t)-1);
440 if (!(*tif->tif_postencode)(tif))
441 return ((tmsize_t)-1);
442 if (!isFillOrder(tif, td->td_fillorder) &&
443 (tif->tif_flags & TIFF_NOBITREV) == 0)
445 if (tif->tif_rawcc > 0 &&
446 !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc))
447 return ((tmsize_t)-1);
448 tif->tif_rawcc = 0;
449 tif->tif_rawcp = tif->tif_rawdata;
450 return (cc);
451}
452
453/*
454 * Write the supplied data to the specified strip.
455 *
456 * NB: Image length must be setup before writing.
457 */
458tmsize_t TIFFWriteRawStrip(TIFF *tif, uint32_t strip, void *data, tmsize_t cc)
459{
460 static const char module[] = "TIFFWriteRawStrip";
461 TIFFDirectory *td = &tif->tif_dir;
462
463 if (cc < 0)
464 {
465 TIFFErrorExtR(tif, module, "Invalid data size %" TIFF_SSIZE_FORMAT, cc);
466 return ((tmsize_t)-1);
467 }
468 if (!WRITECHECKSTRIPS(tif, module))
469 return ((tmsize_t)-1);
470 /*
471 * Check strip array to make sure there's space.
472 * We don't support dynamically growing files that
473 * have data organized in separate bitplanes because
474 * it's too painful. In that case we require that
475 * the imagelength be set properly before the first
476 * write (so that the strips array will be fully
477 * allocated above).
478 */
479 if (strip >= td->td_nstrips)
480 {
481 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
482 {
484 tif, module,
485 "Can not grow image by strips when using separate planes");
486 return ((tmsize_t)-1);
487 }
488 /*
489 * Watch out for a growing image. The value of
490 * strips/image will initially be 1 (since it
491 * can't be deduced until the imagelength is known).
492 */
493 /* TIFFGrowStrips() below extends the strip arrays by exactly one
494 * entry, so only the index immediately past the end can be served. */
495 if (strip > td->td_nstrips)
496 {
497 TIFFErrorExtR(tif, module, "%lu: Strip out of range, max %lu",
498 (unsigned long)strip, (unsigned long)td->td_nstrips);
499 return ((tmsize_t)-1);
500 }
501 if (strip >= td->td_stripsperimage)
502 td->td_stripsperimage =
503 TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip);
504 if (!TIFFGrowStrips(tif, 1, module))
505 return ((tmsize_t)-1);
506 }
507
508 if (tif->tif_dir.td_curstrip != strip)
509 {
510 tif->tif_dir.td_curstrip = strip;
511
512 /* this informs TIFFAppendToStrip() we have changed or reset strip */
513 tif->tif_curoff = 0;
514 }
515
516 if (td->td_stripsperimage == 0)
517 {
518 TIFFErrorExtR(tif, module, "Zero strips per image");
519 return ((tmsize_t)-1);
520 }
521 if (TIFFSafeModMul32(tif, module, strip, td->td_stripsperimage,
522 td->td_rowsperstrip, tif->tif_dir.td_row))
523 return ((tmsize_t)-1);
524 return (TIFFAppendToStrip(tif, strip, (uint8_t *)data, cc) ? cc
525 : (tmsize_t)-1);
526}
527
528/*
529 * Write and compress a tile of data. The
530 * tile is selected by the (x,y,z,s) coordinates.
531 */
532tmsize_t TIFFWriteTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z,
533 uint16_t s)
534{
535 bool overflow = false;
536 uint32_t tile;
537
538 if (!TIFFCheckTile(tif, x, y, z, s))
539 return ((tmsize_t)(-1));
540 /* An overflow returns 0, which is also a valid tile. */
541 tile = _TIFFComputeTileExt(tif, x, y, z, s, &overflow);
542 if (overflow)
543 return ((tmsize_t)(-1));
544 /*
545 * NB: A tile size of -1 is used instead of tif_tilesize knowing
546 * that TIFFWriteEncodedTile will clamp this to the tile size.
547 * This is done because the tile size may not be defined until
548 * after the output buffer is setup in TIFFWriteBufferSetup.
549 */
550 return (TIFFWriteEncodedTile(tif, tile, buf, (tmsize_t)(-1)));
551}
552
553/*
554 * Encode the supplied data and write it to the
555 * specified tile. There must be space for the
556 * data. The function clamps individual writes
557 * to a tile to the tile size, but does not (and
558 * can not) check that multiple writes to the same
559 * tile do not write more than tile size data.
560 *
561 * NB: Image length must be setup before writing; this
562 * interface does not support automatically growing
563 * the image on each write (as TIFFWriteScanline does).
564 */
565tmsize_t TIFFWriteEncodedTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc)
566{
567 static const char module[] = "TIFFWriteEncodedTile";
568 TIFFDirectory *td;
569 uint16_t sample;
570 uint32_t howmany32;
571
572 if (!WRITECHECKTILES(tif, module))
573 return ((tmsize_t)(-1));
574 td = &tif->tif_dir;
575 if (tile >= td->td_nstrips)
576 {
577 TIFFErrorExtR(tif, module, "Tile %lu out of range, max %lu",
578 (unsigned long)tile, (unsigned long)td->td_nstrips);
579 return ((tmsize_t)(-1));
580 }
581 /*
582 * Handle delayed allocation of data buffer. This
583 * permits it to be sized more intelligently (using
584 * directory information).
585 */
586 if (!BUFFERCHECK(tif))
587 return ((tmsize_t)(-1));
588
590
591 tif->tif_dir.td_curtile = tile;
592
593 /* this informs TIFFAppendToStrip() we have changed or reset tile */
594 tif->tif_curoff = 0;
595
597 {
598 return ((tmsize_t)(-1));
599 }
600
601 tif->tif_rawcc = 0;
602 tif->tif_rawcp = tif->tif_rawdata;
603
604 /*
605 * Compute tiles per row & per column to compute
606 * current row and column
607 */
608 howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
609 if (howmany32 == 0)
610 {
611 TIFFErrorExtR(tif, module, "Zero tiles");
612 return ((tmsize_t)(-1));
613 }
614 if (TIFFSafeModMul32(tif, module, tile, howmany32, td->td_tilelength,
615 tif->tif_dir.td_row))
616 return ((tmsize_t)(-1));
617 howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
618 if (howmany32 == 0)
619 {
620 TIFFErrorExtR(tif, module, "Zero tiles");
621 return ((tmsize_t)(-1));
622 }
623 if (TIFFSafeModMul32(tif, module, tile, howmany32, td->td_tilewidth,
624 tif->tif_dir.td_col))
625 return ((tmsize_t)(-1));
626
627 if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
628 {
629 if (!(*tif->tif_setupencode)(tif))
630 return ((tmsize_t)(-1));
632 }
633 tif->tif_flags &= ~TIFF_POSTENCODE;
634
635 /*
636 * Clamp write amount to the tile size. This is mostly
637 * done so that callers can pass in some large number
638 * (e.g. -1) and have the tile size used instead.
639 */
640 if (cc < 1 || cc > tif->tif_dir.td_tilesize)
641 cc = tif->tif_dir.td_tilesize;
642
643 /* shortcut to avoid an extra memcpy() */
644 if (td->td_compression == COMPRESSION_NONE)
645 {
646 /* swab if needed - note that source buffer will be altered */
647 if (!_TIFFCheckPostDecodeLength(tif, tif->tif_postdecode, cc, module))
648 return ((tmsize_t)(-1));
649 tif->tif_postdecode(tif, (uint8_t *)data, cc);
650
651 if (!isFillOrder(tif, td->td_fillorder) &&
652 (tif->tif_flags & TIFF_NOBITREV) == 0)
653 TIFFReverseBits((uint8_t *)data, cc);
654
655 if (cc > 0 && !TIFFAppendToStrip(tif, tile, (uint8_t *)data, cc))
656 return ((tmsize_t)-1);
657 return (cc);
658 }
659
660 sample = (uint16_t)(tile / td->td_stripsperimage);
661 if (!(*tif->tif_preencode)(tif, sample))
662 return ((tmsize_t)(-1));
663 /* swab if needed - note that source buffer will be altered */
664 if (!_TIFFCheckPostDecodeLength(tif, tif->tif_postdecode, cc, module))
665 return ((tmsize_t)(-1));
666 tif->tif_postdecode(tif, (uint8_t *)data, cc);
667
668 if (!(*tif->tif_encodetile)(tif, (uint8_t *)data, cc, sample))
669 return ((tmsize_t)-1);
670 if (!(*tif->tif_postencode)(tif))
671 return ((tmsize_t)(-1));
672 if (!isFillOrder(tif, td->td_fillorder) &&
673 (tif->tif_flags & TIFF_NOBITREV) == 0)
674 TIFFReverseBits((uint8_t *)tif->tif_rawdata, tif->tif_rawcc);
675 if (tif->tif_rawcc > 0 &&
676 !TIFFAppendToStrip(tif, tile, tif->tif_rawdata, tif->tif_rawcc))
677 return ((tmsize_t)(-1));
678 tif->tif_rawcc = 0;
679 tif->tif_rawcp = tif->tif_rawdata;
680 return (cc);
681}
682
683/*
684 * Write the supplied data to the specified strip.
685 * There must be space for the data; we don't check
686 * if strips overlap!
687 *
688 * NB: Image length must be setup before writing; this
689 * interface does not support automatically growing
690 * the image on each write (as TIFFWriteScanline does).
691 */
692tmsize_t TIFFWriteRawTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc)
693{
694 static const char module[] = "TIFFWriteRawTile";
695
696 if (cc < 0)
697 {
698 TIFFErrorExtR(tif, module, "Invalid data size %" TIFF_SSIZE_FORMAT, cc);
699 return ((tmsize_t)(-1));
700 }
701 if (!WRITECHECKTILES(tif, module))
702 return ((tmsize_t)(-1));
703 if (tile >= tif->tif_dir.td_nstrips)
704 {
705 TIFFErrorExtR(tif, module, "Tile %lu out of range, max %lu",
706 (unsigned long)tile,
707 (unsigned long)tif->tif_dir.td_nstrips);
708 return ((tmsize_t)(-1));
709 }
710 return (TIFFAppendToStrip(tif, tile, (uint8_t *)data, cc) ? cc
711 : (tmsize_t)(-1));
712}
713
726#define isUnspecified(tif, f) \
727 (TIFFFieldSet(tif, f) && (tif)->tif_dir.td_imagelength == 0)
728
730{
731 TIFFDirectory *td = &tif->tif_dir;
732
733 if (isTiled(tif))
736 : TIFFNumberOfTiles(tif);
737 else
740 : TIFFNumberOfStrips(tif);
742 /* TIFFWriteDirectoryTagData has a limitation to 0x80000000U bytes */
743 if (td->td_nstrips >=
744 0x80000000U / ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
745 {
746 TIFFErrorExtR(tif, "TIFFSetupStrips",
747 "Too large Strip/Tile Offsets/ByteCounts arrays");
748 return 0;
749 }
752
753 if (td->td_stripoffset_p != NULL)
755 td->td_stripoffset_p = (uint64_t *)_TIFFCheckMalloc(
756 tif, td->td_nstrips, sizeof(uint64_t), "for \"StripOffsets\" array");
757 if (td->td_stripbytecount_p != NULL)
759 td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
760 tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
761 if (td->td_stripoffset_p == NULL || td->td_stripbytecount_p == NULL)
762 return (0);
763 /*
764 * Place data at the end-of-file
765 * (by setting offsets to zero).
766 */
768 (tmsize_t)((size_t)td->td_nstrips * sizeof(uint64_t)));
770 (tmsize_t)((size_t)td->td_nstrips * sizeof(uint64_t)));
773 return (1);
774}
775#undef isUnspecified
776
777/*
778 * Verify file is writable and that the directory
779 * information is setup properly. In doing the latter
780 * we also "freeze" the state of the directory so
781 * that important information is not changed.
782 */
783int TIFFWriteCheck(TIFF *tif, int tiles, const char *module)
784{
785 if (tif->tif_mode == O_RDONLY)
786 {
787 TIFFErrorExtR(tif, module, "File not open for writing");
788 return (0);
789 }
790 if (tiles ^ isTiled(tif))
791 {
792 TIFFErrorExtR(tif, module,
793 tiles ? "Can not write tiles to a striped image"
794 : "Can not write scanlines to a tiled image");
795 return (0);
796 }
797
798 _TIFFFillStriles(tif);
799
800 /*
801 * On the first write verify all the required information
802 * has been setup and initialize any data structures that
803 * had to wait until directory information was set.
804 * Note that a lot of our work is assumed to remain valid
805 * because we disallow any of the important parameters
806 * from changing after we start writing (i.e. once
807 * TIFF_BEENWRITING is set, TIFFSetField will only allow
808 * the image's length to be changed).
809 */
811 {
812 TIFFErrorExtR(tif, module,
813 "Must set \"ImageWidth\" before writing data");
814 return (0);
815 }
816 if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif))
817 {
818 tif->tif_dir.td_nstrips = 0;
819 TIFFErrorExtR(tif, module, "No space for %s arrays",
820 isTiled(tif) ? "tile" : "strip");
821 return (0);
822 }
823 if (isTiled(tif))
824 {
825 tif->tif_dir.td_tilesize = TIFFTileSize(tif);
826 if (tif->tif_dir.td_tilesize == 0)
827 return (0);
828 }
829 else
830 tif->tif_dir.td_tilesize = (tmsize_t)(-1);
832 if (tif->tif_dir.td_scanlinesize == 0)
833 return (0);
835
836 if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
844 !(tif->tif_flags & TIFF_DIRTYDIRECT))
845 {
847 {
848 /* Write macros skip TIFFWriteCheck() while this flag is set */
849 tif->tif_flags &= ~TIFF_BEENWRITING;
850 return (0);
851 }
852 }
853
854 return (1);
855}
856
857/*
858 * Setup the raw data buffer used for encoding.
859 */
860int TIFFWriteBufferSetup(TIFF *tif, void *bp, tmsize_t size)
861{
862 static const char module[] = "TIFFWriteBufferSetup";
863
864 /* Size -1 selects the library default; any other size must be positive. */
865 if (size != (tmsize_t)(-1) && size <= 0)
866 {
867 TIFFErrorExtR(tif, module,
868 "Invalid raw data buffer size %" TIFF_SSIZE_FORMAT, size);
869 return (0);
870 }
871
872 if (tif->tif_rawdata)
873 {
874 if (tif->tif_flags & TIFF_MYBUFFER)
875 {
876 _TIFFfreeExt(tif, tif->tif_rawdata);
877 tif->tif_flags &= ~TIFF_MYBUFFER;
878 }
879 tif->tif_rawdata = NULL;
880 tif->tif_rawdatasize = 0;
881 }
882 if (size == (tmsize_t)(-1))
883 {
884 size = (isTiled(tif) ? tif->tif_dir.td_tilesize : TIFFStripSize(tif));
885
886 /* Adds 10% margin for cases where compression would expand a bit */
887 if (size < TIFF_TMSIZE_T_MAX - size / 10)
888 size += size / 10;
889 /*
890 * Make raw data buffer at least 8K
891 */
892 if (size < 8 * 1024)
893 size = 8 * 1024;
894 bp = NULL; /* NB: force malloc */
895 }
896 if (bp == NULL)
897 {
898 bp = _TIFFmallocExt(tif, size);
899 if (bp == NULL)
900 {
901 TIFFErrorExtR(tif, module, "No space for output buffer");
902 return (0);
903 }
904 tif->tif_flags |= TIFF_MYBUFFER;
905 }
906 else
907 tif->tif_flags &= ~TIFF_MYBUFFER;
908 tif->tif_rawdata = (uint8_t *)bp;
909 tif->tif_rawdatasize = size;
910 tif->tif_rawcc = 0;
911 tif->tif_rawcp = tif->tif_rawdata;
913 return (1);
914}
936/*
937 * Grow the strip data structures by delta strips.
938 */
939static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module)
940{
941 TIFFDirectory *td = &tif->tif_dir;
942 uint64_t *new_stripoffset;
943 uint64_t *new_stripbytecount;
944
946 /*
947 * Keep the grown arrays within the same 0x80000000U bytes limitation of
948 * TIFFWriteDirectoryTagData() that TIFFSetupStrips() enforces for the
949 * serialised tag, and compute the in-memory size of the arrays with an
950 * overflow check so that it cannot wrap on 32-bit platforms.
951 */
952 const uint64_t new_nstrips = (uint64_t)td->td_nstrips + delta;
953 if (new_nstrips >=
954 0x80000000U / ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
955 {
956 TIFFErrorExtR(tif, module,
957 "Too large Strip/Tile Offsets/ByteCounts arrays");
958 return (0);
959 }
960 const tmsize_t arrays_size = _TIFFMultiplySSize(
961 tif, (tmsize_t)new_nstrips, (tmsize_t)sizeof(uint64_t), module);
962 if (arrays_size == 0)
963 return (0);
964 new_stripoffset =
965 (uint64_t *)_TIFFreallocExt(tif, td->td_stripoffset_p, arrays_size);
966 /*
967 * Update td_stripoffset_p immediately so the old pointer is not left
968 * dangling if the second realloc fails.
969 */
970 if (new_stripoffset)
971 td->td_stripoffset_p = new_stripoffset;
972 new_stripbytecount =
973 (uint64_t *)_TIFFreallocExt(tif, td->td_stripbytecount_p, arrays_size);
974 if (new_stripbytecount)
975 td->td_stripbytecount_p = new_stripbytecount;
976 if (new_stripoffset == NULL || new_stripbytecount == NULL)
977 {
978 td->td_nstrips = 0;
979 TIFFErrorExtR(tif, module, "No space to expand strip arrays");
980 return (0);
981 }
983 (tmsize_t)((size_t)delta * sizeof(uint64_t)));
985 (tmsize_t)((size_t)delta * sizeof(uint64_t)));
986 td->td_nstrips += delta;
988
989 return (1);
990}
1018/*
1019 * Append the data to the specified strip.
1020 */
1021static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data,
1022 tmsize_t cc)
1023{
1024 static const char module[] = "TIFFAppendToStrip";
1025 TIFFDirectory *td = &tif->tif_dir;
1026 uint64_t m;
1027 int64_t old_byte_count = -1;
1028
1029 /* Some security checks */
1030 if (td->td_stripoffset_p == NULL)
1031 {
1032 TIFFErrorExtR(tif, module, "Strip offset array pointer is NULL");
1033 return (0);
1034 }
1035 if (td->td_stripbytecount_p == NULL)
1036 {
1037 TIFFErrorExtR(tif, module, "Strip bytecount array pointer is NULL");
1038 return (0);
1039 }
1040 if (strip == NOSTRIP)
1041 {
1042 TIFFErrorExtR(tif, module, "Strip number not valid (NOSTRIP)");
1043 return (0);
1044 }
1045 if (strip >= td->td_nstrips)
1046 {
1047 TIFFErrorExtR(tif, module, "%lu: Strip out of range, max %lu",
1048 (unsigned long)strip, (unsigned long)td->td_nstrips);
1049 return (0);
1050 }
1051
1052 if (tif->tif_curoff == 0)
1053 tif->tif_lastvalidoff = 0;
1054
1055 if (td->td_stripoffset_p[strip] == 0 || tif->tif_curoff == 0)
1056 {
1057 assert(td->td_nstrips > 0);
1058
1059 if (td->td_stripbytecount_p[strip] != 0 &&
1060 td->td_stripoffset_p[strip] != 0 &&
1061 td->td_stripbytecount_p[strip] >= (uint64_t)cc)
1062 {
1063 /*
1064 * There is already tile data on disk, and the new tile
1065 * data we have will fit in the same space. The only
1066 * aspect of this that is risky is that there could be
1067 * more data to append to this strip before we are done
1068 * depending on how we are getting called.
1069 */
1070 if (!SeekOK(tif, td->td_stripoffset_p[strip]))
1071 {
1072 TIFFErrorExtR(tif, module, "Seek error at scanline %lu",
1073 (unsigned long)tif->tif_dir.td_row);
1074 return (0);
1075 }
1076
1077 tif->tif_lastvalidoff =
1078 td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip];
1079 }
1080 else
1081 {
1082 /*
1083 * Seek to end of file, and set that as our location to
1084 * write this strip.
1085 */
1086 td->td_stripoffset_p[strip] = TIFFSeekFile(tif, 0, SEEK_END);
1087 tif->tif_flags |= TIFF_DIRTYSTRIP;
1088 }
1089
1090 tif->tif_curoff = td->td_stripoffset_p[strip];
1091
1092 /*
1093 * We are starting a fresh strip/tile, so set the size to zero.
1094 */
1095 old_byte_count = (int64_t)td->td_stripbytecount_p[strip];
1096 td->td_stripbytecount_p[strip] = 0;
1097 }
1098
1099 m = tif->tif_curoff + (uint64_t)cc;
1100 if (!(tif->tif_flags & TIFF_BIGTIFF))
1101 m = (uint32_t)m;
1102 if ((m < tif->tif_curoff) || (m < (uint64_t)cc))
1103 {
1104 TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded");
1105 return (0);
1106 }
1107
1108 if (tif->tif_lastvalidoff != 0 && m > tif->tif_lastvalidoff &&
1109 td->td_stripbytecount_p[strip] > 0)
1110 {
1111 /* Ouch: we have detected that we are rewriting in place a strip/tile */
1112 /* with several calls to TIFFAppendToStrip(). The first call was with */
1113 /* a size smaller than the previous size of the strip/tile, so we */
1114 /* opted to rewrite in place, but a following call causes us to go */
1115 /* outsize of the strip/tile area, so we have to finally go for a */
1116 /* append-at-end-of-file strategy, and start by moving what we already
1117 */
1118 /* wrote. */
1119 tmsize_t tempSize;
1120 void *temp;
1121 uint64_t offsetRead;
1122 uint64_t offsetWrite;
1123 uint64_t toCopy = td->td_stripbytecount_p[strip];
1124
1125 if (toCopy < 1024 * 1024)
1126 tempSize = (tmsize_t)toCopy;
1127 else
1128 tempSize = 1024 * 1024;
1129
1130 offsetRead = td->td_stripoffset_p[strip];
1131 offsetWrite = TIFFSeekFile(tif, 0, SEEK_END);
1132
1133 m = offsetWrite + (uint64_t)toCopy + (uint64_t)cc;
1134 if (!(tif->tif_flags & TIFF_BIGTIFF) && m != (uint32_t)m)
1135 {
1136 TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded");
1137 return (0);
1138 }
1139
1140 temp = _TIFFmallocExt(tif, tempSize);
1141 if (temp == NULL)
1142 {
1143 TIFFErrorExtR(tif, module, "No space for output buffer");
1144 return (0);
1145 }
1146
1147 tif->tif_flags |= TIFF_DIRTYSTRIP;
1148
1149 td->td_stripoffset_p[strip] = offsetWrite;
1150 td->td_stripbytecount_p[strip] = 0;
1151
1152 /* Move data written by previous calls to us at end of file */
1153 while (toCopy > 0)
1154 {
1155 tmsize_t chunkSize =
1156 toCopy < (uint64_t)tempSize ? (tmsize_t)toCopy : tempSize;
1157 if (!SeekOK(tif, offsetRead))
1158 {
1159 TIFFErrorExtR(tif, module, "Seek error");
1160 _TIFFfreeExt(tif, temp);
1161 return (0);
1162 }
1163 if (!ReadOK(tif, temp, chunkSize))
1164 {
1165 TIFFErrorExtR(tif, module, "Cannot read");
1166 _TIFFfreeExt(tif, temp);
1167 return (0);
1168 }
1169 if (!SeekOK(tif, offsetWrite))
1170 {
1171 TIFFErrorExtR(tif, module, "Seek error");
1172 _TIFFfreeExt(tif, temp);
1173 return (0);
1174 }
1175 if (!WriteOK(tif, temp, chunkSize))
1176 {
1177 TIFFErrorExtR(tif, module, "Cannot write");
1178 _TIFFfreeExt(tif, temp);
1179 return (0);
1180 }
1181 offsetRead += (uint64_t)chunkSize;
1182 offsetWrite += (uint64_t)chunkSize;
1183 td->td_stripbytecount_p[strip] += (uint64_t)chunkSize;
1184 toCopy -= (uint64_t)chunkSize;
1185 }
1186 _TIFFfreeExt(tif, temp);
1187
1188 /* Append the data of this call */
1189 offsetWrite += (uint64_t)cc;
1190 m = offsetWrite;
1191 }
1192
1193 if (!WriteOK(tif, data, cc))
1194 {
1195 TIFFErrorExtR(tif, module, "Write error at scanline %lu",
1196 (unsigned long)tif->tif_dir.td_row);
1197 return (0);
1198 }
1199 tif->tif_curoff = m;
1200 td->td_stripbytecount_p[strip] += (uint64_t)cc;
1201
1202 if ((int64_t)td->td_stripbytecount_p[strip] != old_byte_count)
1203 tif->tif_flags |= TIFF_DIRTYSTRIP;
1204
1205 return (1);
1206}
1207
1208/*
1209 * Internal version of TIFFFlushData that can be
1210 * called by ``encodestrip routines'' w/o concern
1211 * for infinite recursion.
1212 */
1214{
1215 if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE)
1216 {
1217 if (!isFillOrder(tif, tif->tif_dir.td_fillorder) &&
1218 (tif->tif_flags & TIFF_NOBITREV) == 0)
1219 TIFFReverseBits((uint8_t *)tif->tif_rawdata, tif->tif_rawcc);
1220 if (!TIFFAppendToStrip(tif,
1221 isTiled(tif) ? tif->tif_dir.td_curtile
1222 : tif->tif_dir.td_curstrip,
1223 tif->tif_rawdata, tif->tif_rawcc))
1224 {
1225 /* We update those variables even in case of error since there's */
1226 /* code that doesn't really check the return code of this */
1227 /* function */
1228 tif->tif_rawcc = 0;
1229 tif->tif_rawcp = tif->tif_rawdata;
1230 return (0);
1231 }
1232 tif->tif_rawcc = 0;
1233 tif->tif_rawcp = tif->tif_rawdata;
1234 }
1235 return (1);
1236}
1237
1238/*
1239 * Set the current write offset. This should only be
1240 * used to set the offset to a known previous location
1241 * (very carefully), or to 0 so that the next write gets
1242 * appended to the end of the file.
1243 */
1245{
1246 tif->tif_curoff = off;
1247 tif->tif_lastvalidoff = 0;
1248}
uint32_t _TIFFCastUInt64ToUInt32(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
Definition tif_aux.c:153
uint64_t _TIFFAdd64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Check and add two unsigned operands.
Definition tif_aux.c:85
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:46
tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
Check and multiply two signed-size operands.
Definition tif_aux.c:91
#define TIFF_SSIZE_FORMAT
Provide the printf conversion fragment for tmsize_t, without a percent sign.
Definition tif_config.h:294
#define FIELD_STRIPOFFSETS
Index the strile offsets presence bit; not an on-disk tag number.
Definition tif_dir.h:519
#define FIELD_IMAGEDIMENSIONS
Index the image dimensions presence bit; not an on-disk tag number.
Definition tif_dir.h:419
#define TIFFFieldSet(tif, field)
Test a directory presence bit.
Definition tif_dir.h:662
#define FIELD_TILEDIMENSIONS
Index the tile dimensions presence bit; not an on-disk tag number.
Definition tif_dir.h:424
#define FIELD_ROWSPERSTRIP
Index the rows per strip presence bit; not an on-disk tag number.
Definition tif_dir.h:481
#define FIELD_STRIPBYTECOUNTS
Index the strile byte counts presence bit; not an on-disk tag number.
Definition tif_dir.h:514
#define TIFFSetFieldBit(tif, field)
Set a directory presence bit.
Definition tif_dir.h:675
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...
Definition tif_tile.c:52
#define BUFFERCHECK(tif)
Reuse an output buffer or allocate default encoding storage.
Definition tif_write.c:91
int _TIFFCheckPostDecodeLength(TIFF *tif, TIFFPostMethod tif_postdecode, tmsize_t cc, const char *modulename)
Check the byte count for a byte-swapping postdecode method.
Definition tif_read.c:2260
#define isUnspecified(tif, f)
Test the zero-image-length provisional strile case.
Definition tif_write.c:726
#define NOSTRIP
Local repeat of the NOSTRIP sentinel defined in tif_dir.h.
Definition tif_write.c:44
#define WRITECHECKTILES(tif, module)
Reuse write preparation or check tile organisation.
Definition tif_write.c:76
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
Definition tif_write.c:1213
static int _TIFFReserveLargeEnoughWriteBuffer(TIFF *tif, uint32_t strip_or_tile)
Reserve raw output capacity for a possible strile rewrite.
Definition tif_write.c:291
static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data, tmsize_t cc)
Append stored bytes or relocate a growing overwritten strile.
Definition tif_write.c:1021
static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module)
Grow contiguous strip offset/count arrays by a requested delta.
Definition tif_write.c:939
#define WRITECHECKSTRIPS(tif, module)
Reuse write preparation or check strip organisation.
Definition tif_write.c:60
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
Resize a handle-aware block or release it for a nonpositive size.
Definition tif_open.c:257
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Allocate a checked array with an allocation-failure diagnostic.
Definition tif_aux.c:274
#define TIFFroundup_64(x, y)
Round a 64-bit value up to a multiple without overflow checks.
Definition tiffiop.h:1142
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define WriteOK(tif, buf, size)
Test whether one client write transfers the entire requested byte count.
Definition tiffiop.h:1035
#define TIFF_MYBUFFER
Mark a raw buffer owned and released by the handle.
Definition tiffiop.h:451
#define TIFF_BUFFERSETUP
Mark completed data-buffer setup.
Definition tiffiop.h:426
#define ReadOK(tif, buf, size)
Test whether one client read transfers the entire requested byte count.
Definition tiffiop.h:1000
#define TIFF_CODERSETUP
Mark completed codec setup.
Definition tiffiop.h:431
#define TIFFSafeModMul32(tif, module, index, count, dimension, result)
Compute (index modulo count) times dimension with a checked product.
Definition tiffiop.h:1195
#define TIFF_POSTENCODE
Request a pending codec postencode call.
Definition tiffiop.h:471
#define TIFF_BUF4WRITE
Interpret the raw buffer and its byte count as pending output.
Definition tiffiop.h:513
#define SeekOK(tif, off)
Test whether an absolute seek reaches the requested file position.
Definition tiffiop.h:1017
#define isFillOrder(tif, o)
Test whether selected bit-fill-order bits are present in a handle.
Definition tiffiop.h:858
#define TIFF_BEENWRITING
Record that scanline or image-data writing has begun.
Definition tiffiop.h:436
#define TIFF_DIRTYSTRIP
Mark strip/tile offset or byte-count arrays that need writing.
Definition tiffiop.h:518
#define TIFFSeekFile(tif, off, whence)
Invoke the handle's client seek callback.
Definition tiffiop.h:920
#define TIFF_DIRTYDIRECT
Mark a current directory that needs writing.
Definition tiffiop.h:421
#define TIFF_BIGTIFF
Mark the BigTIFF file format.
Definition tiffiop.h:508
#define TIFFhowmany_32(x, y)
Compute a guarded 32-bit ceiling quotient.
Definition tiffiop.h:1056
#define TIFF_NOBITREV
Suppress automatic bit reversal.
Definition tiffiop.h:446
int _TIFFFillStriles(TIFF *tif)
Materialise deferred strile arrays when required.
void TIFFReverseBits(uint8_t *cp, tmsize_t n)
Reverse the bit order within each byte of a buffer.
Definition tif_swab.c:330
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
int TIFFForceStrileArrayWriting(TIFF *tif)
Patch on-file strile offset and byte-count payloads.
Definition tif_flush.c:81
int TIFFWriteBufferSetup(TIFF *tif, void *bp, tmsize_t size)
Replace the handle's encoding buffer.
Definition tif_write.c:860
int TIFFWriteCheck(TIFF *tif, int tiles, const char *module)
Validate and prepare geometry for image writing.
Definition tif_write.c:783
int TIFFSetupStrips(TIFF *tif)
Replace strile offset/count arrays from current geometry.
Definition tif_write.c:729
tmsize_t TIFFTileSize(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:307
tmsize_t TIFFStripSize(TIFF *tif)
Compute the decoded-size estimate for a nominal strip.
Definition tif_strip.c:244
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:383
uint32_t TIFFNumberOfStrips(TIFF *tif)
Compute the strip count from current image geometry.
Definition tif_strip.c:82
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.
Definition tif_tile.c:118
uint32_t TIFFNumberOfTiles(TIFF *tif)
Compute the tile count across image depth and sample planes.
Definition tif_tile.c:155
tmsize_t TIFFWriteRawTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc)
Write raw stored bytes to one tile.
Definition tif_write.c:692
tmsize_t TIFFWriteRawStrip(TIFF *tif, uint32_t strip, void *data, tmsize_t cc)
Write raw stored bytes to one strip.
Definition tif_write.c:458
void TIFFSetWriteOffset(TIFF *tif, toff_t off)
Change the cached next payload write offset.
Definition tif_write.c:1244
int TIFFWriteScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)
Encode and buffer one image scanline.
Definition tif_write.c:99
tmsize_t TIFFWriteEncodedTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc)
Write encoded samples to one tile.
Definition tif_write.c:565
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.
Definition tif_write.c:532
tmsize_t TIFFWriteEncodedStrip(TIFF *tif, uint32_t strip, void *data, tmsize_t cc)
Write encoded samples to one strip.
Definition tif_write.c:331
int TIFFFlushData(TIFF *tif)
Flush pending encoded image data without committing directory changes.
Definition tif_flush.c:151
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
#define COMPRESSION_NONE
Dump mode.
Definition tiff.h:302
#define PLANARCONFIG_SEPARATE
Separate planes of data.
Definition tiff.h:474
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
#define TIFF_TMSIZE_T_MAX
Define the largest positive size representable by tmsize_t.
Definition tiffio.h:91
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
Definition tiffio.h:94
uint64_t tdir_count
Number of file-type elements, normalised to host order.
Definition tif_dir.h:89
uint16_t tdir_type
On-disk TIFFDataType code.
Definition tif_dir.h:86
uint64_t toff_long8
Raw 64-bit view of inline bytes or offset storage.
Definition tif_dir.h:99
uint16_t tdir_tag
Directory tag identifier.
Definition tif_dir.h:84
union TIFFDirEntry::@0 tdir_offset
Overlay raw directory inline bytes and offset-width views.
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint16_t td_fillorder
FILLORDER_* bit order within file bytes.
Definition tif_dir.h:178
tmsize_t td_tilesize
Cached decoded tile size in bytes; minus one when invalidated.
Definition tif_dir.h:259
tmsize_t td_scanlinesize
Cached decoded scanline size in bytes; zero when not computed.
Definition tif_dir.h:245
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Definition tif_dir.h:240
TIFFDirEntry td_stripoffset_entry
Deferred offset entry; tag/type/count are swapped but offset storage is raw.
Definition tif_dir.h:301
uint32_t td_stripsperimage
Strips or tiles per sample plane, despite the historic name.
Definition tif_dir.h:269
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
Definition tif_dir.h:200
uint64_t * td_stripbytecount_p
Owned byte-count array/cache, possibly NULL or partial; use TIFFGetStrileByteCount().
Definition tif_dir.h:281
uint64_t * td_stripoffset_p
Owned offset array/cache, possibly NULL or partial; use TIFFGetStrileOffset().
Definition tif_dir.h:276
uint32_t td_col
Current pixel column; UINT32_MAX before traversal starts.
Definition tif_dir.h:253
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
uint32_t td_nstrips
Total strile count across all planes; logical array length.
Definition tif_dir.h:271
TIFFDirEntry td_stripbytecount_entry
Deferred byte-count entry; offset storage still requires swapping.
Definition tif_dir.h:305
uint32_t td_curtile
Current tile index, or NOTILE.
Definition tif_dir.h:256
uint32_t td_curstrip
Current strip index, or NOSTRIP.
Definition tif_dir.h:242
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
Definition tiffiop.h:649
uint64_t tif_lastvalidoff
Last byte-offset boundary permitted for rewriting strip data in place.
Definition tiffiop.h:611
TIFFSeekMethod tif_seek
Codec callback advancing within a strip.
Definition tiffiop.h:659
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
Definition tiffiop.h:653
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
Definition tiffiop.h:639
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
uint64_t tif_curoff
Tracked current file byte offset for read/write operations.
Definition tiffiop.h:606
TIFFPostMethod tif_postdecode
Callback transforming decoded data, for example by byte swapping.
Definition tiffiop.h:727
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
Definition tiffiop.h:632
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
Definition tiffiop.h:641
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
Definition tiffiop.h:645
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
Definition tiffiop.h:681
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
Definition tiffiop.h:686
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
int tif_mode
Current access mode using the O_* constants.
Definition tiffiop.h:400
Declare private library state, callbacks, and implementation helpers.