1 /* $Id: tif_write.c,v 1.46 2016-12-03 21:57:44 erouault Exp $ */
2
3 /*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * TIFF Library.
29 *
30 * Scanline-oriented Write Support
31 */
32 #include "tiffiop.h"
33 #include <stdio.h>
34
35 #define STRIPINCR 20 /* expansion factor on strip array */
36
37 #define WRITECHECKSTRIPS(tif, module) \
38 (((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),0,module))
39 #define WRITECHECKTILES(tif, module) \
40 (((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),1,module))
41 #define BUFFERCHECK(tif) \
42 ((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) || \
43 TIFFWriteBufferSetup((tif), NULL, (tmsize_t) -1))
44
45 static int TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module);
46 static int TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc);
47
48 int
TIFFWriteScanline(TIFF * tif,void * buf,uint32 row,uint16 sample)49 TIFFWriteScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
50 {
51 static const char module[] = "TIFFWriteScanline";
52 register TIFFDirectory *td;
53 int status, imagegrew = 0;
54 uint32 strip;
55
56 if (!WRITECHECKSTRIPS(tif, module))
57 return (-1);
58 /*
59 * Handle delayed allocation of data buffer. This
60 * permits it to be sized more intelligently (using
61 * directory information).
62 */
63 if (!BUFFERCHECK(tif))
64 return (-1);
65 tif->tif_flags |= TIFF_BUF4WRITE; /* not strictly sure this is right*/
66
67 td = &tif->tif_dir;
68 /*
69 * Extend image length if needed
70 * (but only for PlanarConfig=1).
71 */
72 if (row >= td->td_imagelength) { /* extend image */
73 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
74 TIFFErrorExt(tif->tif_clientdata, module,
75 "Can not change \"ImageLength\" when using separate planes");
76 return (-1);
77 }
78 td->td_imagelength = row+1;
79 imagegrew = 1;
80 }
81 /*
82 * Calculate strip and check for crossings.
83 */
84 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
85 if (sample >= td->td_samplesperpixel) {
86 TIFFErrorExt(tif->tif_clientdata, module,
87 "%lu: Sample out of range, max %lu",
88 (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
89 return (-1);
90 }
91 strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
92 } else
93 strip = row / td->td_rowsperstrip;
94 /*
95 * Check strip array to make sure there's space. We don't support
96 * dynamically growing files that have data organized in separate
97 * bitplanes because it's too painful. In that case we require that
98 * the imagelength be set properly before the first write (so that the
99 * strips array will be fully allocated above).
100 */
101 if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
102 return (-1);
103 if (strip != tif->tif_curstrip) {
104 /*
105 * Changing strips -- flush any data present.
106 */
107 if (!TIFFFlushData(tif))
108 return (-1);
109 tif->tif_curstrip = strip;
110 /*
111 * Watch out for a growing image. The value of strips/image
112 * will initially be 1 (since it can't be deduced until the
113 * imagelength is known).
114 */
115 if (strip >= td->td_stripsperimage && imagegrew)
116 td->td_stripsperimage =
117 TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
118 if (td->td_stripsperimage == 0) {
119 TIFFErrorExt(tif->tif_clientdata, module, "Zero strips per image");
120 return (-1);
121 }
122 tif->tif_row =
123 (strip % td->td_stripsperimage) * td->td_rowsperstrip;
124 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
125 if (!(*tif->tif_setupencode)(tif))
126 return (-1);
127 tif->tif_flags |= TIFF_CODERSETUP;
128 }
129
130 tif->tif_rawcc = 0;
131 tif->tif_rawcp = tif->tif_rawdata;
132
133 if( td->td_stripbytecount[strip] > 0 )
134 {
135 /* if we are writing over existing tiles, zero length */
136 td->td_stripbytecount[strip] = 0;
137
138 /* this forces TIFFAppendToStrip() to do a seek */
139 tif->tif_curoff = 0;
140 }
141
142 if (!(*tif->tif_preencode)(tif, sample))
143 return (-1);
144 tif->tif_flags |= TIFF_POSTENCODE;
145 }
146 /*
147 * Ensure the write is either sequential or at the
148 * beginning of a strip (or that we can randomly
149 * access the data -- i.e. no encoding).
150 */
151 if (row != tif->tif_row) {
152 if (row < tif->tif_row) {
153 /*
154 * Moving backwards within the same strip:
155 * backup to the start and then decode
156 * forward (below).
157 */
158 tif->tif_row = (strip % td->td_stripsperimage) *
159 td->td_rowsperstrip;
160 tif->tif_rawcp = tif->tif_rawdata;
161 }
162 /*
163 * Seek forward to the desired row.
164 */
165 if (!(*tif->tif_seek)(tif, row - tif->tif_row))
166 return (-1);
167 tif->tif_row = row;
168 }
169
170 /* swab if needed - note that source buffer will be altered */
171 tif->tif_postdecode( tif, (uint8*) buf, tif->tif_scanlinesize );
172
173 status = (*tif->tif_encoderow)(tif, (uint8*) buf,
174 tif->tif_scanlinesize, sample);
175
176 /* we are now poised at the beginning of the next row */
177 tif->tif_row = row + 1;
178 return (status);
179 }
180
181 /*
182 * Encode the supplied data and write it to the
183 * specified strip.
184 *
185 * NB: Image length must be setup before writing.
186 */
187 tmsize_t
TIFFWriteEncodedStrip(TIFF * tif,uint32 strip,void * data,tmsize_t cc)188 TIFFWriteEncodedStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
189 {
190 static const char module[] = "TIFFWriteEncodedStrip";
191 TIFFDirectory *td = &tif->tif_dir;
192 uint16 sample;
193
194 if (!WRITECHECKSTRIPS(tif, module))
195 return ((tmsize_t) -1);
196 /*
197 * Check strip array to make sure there's space.
198 * We don't support dynamically growing files that
199 * have data organized in separate bitplanes because
200 * it's too painful. In that case we require that
201 * the imagelength be set properly before the first
202 * write (so that the strips array will be fully
203 * allocated above).
204 */
205 if (strip >= td->td_nstrips) {
206 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
207 TIFFErrorExt(tif->tif_clientdata, module,
208 "Can not grow image by strips when using separate planes");
209 return ((tmsize_t) -1);
210 }
211 if (!TIFFGrowStrips(tif, 1, module))
212 return ((tmsize_t) -1);
213 td->td_stripsperimage =
214 TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip);
215 }
216 /*
217 * Handle delayed allocation of data buffer. This
218 * permits it to be sized according to the directory
219 * info.
220 */
221 if (!BUFFERCHECK(tif))
222 return ((tmsize_t) -1);
223
224 tif->tif_flags |= TIFF_BUF4WRITE;
225 tif->tif_curstrip = strip;
226
227 if (td->td_stripsperimage == 0) {
228 TIFFErrorExt(tif->tif_clientdata, module, "Zero strips per image");
229 return ((tmsize_t) -1);
230 }
231
232 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
233 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
234 if (!(*tif->tif_setupencode)(tif))
235 return ((tmsize_t) -1);
236 tif->tif_flags |= TIFF_CODERSETUP;
237 }
238
239 if( td->td_stripbytecount[strip] > 0 )
240 {
241 /* Make sure that at the first attempt of rewriting the tile, we will have */
242 /* more bytes available in the output buffer than the previous byte count, */
243 /* so that TIFFAppendToStrip() will detect the overflow when it is called the first */
244 /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */
245 if( tif->tif_rawdatasize <= (tmsize_t)td->td_stripbytecount[strip] )
246 {
247 if( !(TIFFWriteBufferSetup(tif, NULL,
248 (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[strip] + 1), 1024))) )
249 return ((tmsize_t)(-1));
250 }
251
252 /* Force TIFFAppendToStrip() to consider placing data at end
253 of file. */
254 tif->tif_curoff = 0;
255 }
256
257 tif->tif_rawcc = 0;
258 tif->tif_rawcp = tif->tif_rawdata;
259
260 tif->tif_flags &= ~TIFF_POSTENCODE;
261
262 /* shortcut to avoid an extra memcpy() */
263 if( td->td_compression == COMPRESSION_NONE )
264 {
265 /* swab if needed - note that source buffer will be altered */
266 tif->tif_postdecode( tif, (uint8*) data, cc );
267
268 if (!isFillOrder(tif, td->td_fillorder) &&
269 (tif->tif_flags & TIFF_NOBITREV) == 0)
270 TIFFReverseBits((uint8*) data, cc);
271
272 if (cc > 0 &&
273 !TIFFAppendToStrip(tif, strip, (uint8*) data, cc))
274 return ((tmsize_t) -1);
275 return (cc);
276 }
277
278 sample = (uint16)(strip / td->td_stripsperimage);
279 if (!(*tif->tif_preencode)(tif, sample))
280 return ((tmsize_t) -1);
281
282 /* swab if needed - note that source buffer will be altered */
283 tif->tif_postdecode( tif, (uint8*) data, cc );
284
285 if (!(*tif->tif_encodestrip)(tif, (uint8*) data, cc, sample))
286 return ((tmsize_t) -1);
287 if (!(*tif->tif_postencode)(tif))
288 return ((tmsize_t) -1);
289 if (!isFillOrder(tif, td->td_fillorder) &&
290 (tif->tif_flags & TIFF_NOBITREV) == 0)
291 TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc);
292 if (tif->tif_rawcc > 0 &&
293 !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc))
294 return ((tmsize_t) -1);
295 tif->tif_rawcc = 0;
296 tif->tif_rawcp = tif->tif_rawdata;
297 return (cc);
298 }
299
300 /*
301 * Write the supplied data to the specified strip.
302 *
303 * NB: Image length must be setup before writing.
304 */
305 tmsize_t
TIFFWriteRawStrip(TIFF * tif,uint32 strip,void * data,tmsize_t cc)306 TIFFWriteRawStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
307 {
308 static const char module[] = "TIFFWriteRawStrip";
309 TIFFDirectory *td = &tif->tif_dir;
310
311 if (!WRITECHECKSTRIPS(tif, module))
312 return ((tmsize_t) -1);
313 /*
314 * Check strip array to make sure there's space.
315 * We don't support dynamically growing files that
316 * have data organized in separate bitplanes because
317 * it's too painful. In that case we require that
318 * the imagelength be set properly before the first
319 * write (so that the strips array will be fully
320 * allocated above).
321 */
322 if (strip >= td->td_nstrips) {
323 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
324 TIFFErrorExt(tif->tif_clientdata, module,
325 "Can not grow image by strips when using separate planes");
326 return ((tmsize_t) -1);
327 }
328 /*
329 * Watch out for a growing image. The value of
330 * strips/image will initially be 1 (since it
331 * can't be deduced until the imagelength is known).
332 */
333 if (strip >= td->td_stripsperimage)
334 td->td_stripsperimage =
335 TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
336 if (!TIFFGrowStrips(tif, 1, module))
337 return ((tmsize_t) -1);
338 }
339 tif->tif_curstrip = strip;
340 if (td->td_stripsperimage == 0) {
341 TIFFErrorExt(tif->tif_clientdata, module,"Zero strips per image");
342 return ((tmsize_t) -1);
343 }
344 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
345 return (TIFFAppendToStrip(tif, strip, (uint8*) data, cc) ?
346 cc : (tmsize_t) -1);
347 }
348
349 /*
350 * Write and compress a tile of data. The
351 * tile is selected by the (x,y,z,s) coordinates.
352 */
353 tmsize_t
TIFFWriteTile(TIFF * tif,void * buf,uint32 x,uint32 y,uint32 z,uint16 s)354 TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
355 {
356 if (!TIFFCheckTile(tif, x, y, z, s))
357 return ((tmsize_t)(-1));
358 /*
359 * NB: A tile size of -1 is used instead of tif_tilesize knowing
360 * that TIFFWriteEncodedTile will clamp this to the tile size.
361 * This is done because the tile size may not be defined until
362 * after the output buffer is setup in TIFFWriteBufferSetup.
363 */
364 return (TIFFWriteEncodedTile(tif,
365 TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
366 }
367
368 /*
369 * Encode the supplied data and write it to the
370 * specified tile. There must be space for the
371 * data. The function clamps individual writes
372 * to a tile to the tile size, but does not (and
373 * can not) check that multiple writes to the same
374 * tile do not write more than tile size data.
375 *
376 * NB: Image length must be setup before writing; this
377 * interface does not support automatically growing
378 * the image on each write (as TIFFWriteScanline does).
379 */
380 tmsize_t
TIFFWriteEncodedTile(TIFF * tif,uint32 tile,void * data,tmsize_t cc)381 TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
382 {
383 static const char module[] = "TIFFWriteEncodedTile";
384 TIFFDirectory *td;
385 uint16 sample;
386 uint32 howmany32;
387
388 if (!WRITECHECKTILES(tif, module))
389 return ((tmsize_t)(-1));
390 td = &tif->tif_dir;
391 if (tile >= td->td_nstrips) {
392 TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
393 (unsigned long) tile, (unsigned long) td->td_nstrips);
394 return ((tmsize_t)(-1));
395 }
396 /*
397 * Handle delayed allocation of data buffer. This
398 * permits it to be sized more intelligently (using
399 * directory information).
400 */
401 if (!BUFFERCHECK(tif))
402 return ((tmsize_t)(-1));
403
404 tif->tif_flags |= TIFF_BUF4WRITE;
405 tif->tif_curtile = tile;
406
407 if( td->td_stripbytecount[tile] > 0 )
408 {
409 /* Make sure that at the first attempt of rewriting the tile, we will have */
410 /* more bytes available in the output buffer than the previous byte count, */
411 /* so that TIFFAppendToStrip() will detect the overflow when it is called the first */
412 /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */
413 if( tif->tif_rawdatasize <= (tmsize_t) td->td_stripbytecount[tile] )
414 {
415 if( !(TIFFWriteBufferSetup(tif, NULL,
416 (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[tile] + 1), 1024))) )
417 return ((tmsize_t)(-1));
418 }
419
420 /* Force TIFFAppendToStrip() to consider placing data at end
421 of file. */
422 tif->tif_curoff = 0;
423 }
424
425 tif->tif_rawcc = 0;
426 tif->tif_rawcp = tif->tif_rawdata;
427
428 /*
429 * Compute tiles per row & per column to compute
430 * current row and column
431 */
432 howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
433 if (howmany32 == 0) {
434 TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
435 return ((tmsize_t)(-1));
436 }
437 tif->tif_row = (tile % howmany32) * td->td_tilelength;
438 howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
439 if (howmany32 == 0) {
440 TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
441 return ((tmsize_t)(-1));
442 }
443 tif->tif_col = (tile % howmany32) * td->td_tilewidth;
444
445 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
446 if (!(*tif->tif_setupencode)(tif))
447 return ((tmsize_t)(-1));
448 tif->tif_flags |= TIFF_CODERSETUP;
449 }
450 tif->tif_flags &= ~TIFF_POSTENCODE;
451
452 /*
453 * Clamp write amount to the tile size. This is mostly
454 * done so that callers can pass in some large number
455 * (e.g. -1) and have the tile size used instead.
456 */
457 if ( cc < 1 || cc > tif->tif_tilesize)
458 cc = tif->tif_tilesize;
459
460 /* shortcut to avoid an extra memcpy() */
461 if( td->td_compression == COMPRESSION_NONE )
462 {
463 /* swab if needed - note that source buffer will be altered */
464 tif->tif_postdecode( tif, (uint8*) data, cc );
465
466 if (!isFillOrder(tif, td->td_fillorder) &&
467 (tif->tif_flags & TIFF_NOBITREV) == 0)
468 TIFFReverseBits((uint8*) data, cc);
469
470 if (cc > 0 &&
471 !TIFFAppendToStrip(tif, tile, (uint8*) data, cc))
472 return ((tmsize_t) -1);
473 return (cc);
474 }
475
476 sample = (uint16)(tile/td->td_stripsperimage);
477 if (!(*tif->tif_preencode)(tif, sample))
478 return ((tmsize_t)(-1));
479 /* swab if needed - note that source buffer will be altered */
480 tif->tif_postdecode( tif, (uint8*) data, cc );
481
482 if (!(*tif->tif_encodetile)(tif, (uint8*) data, cc, sample))
483 return ((tmsize_t) -1);
484 if (!(*tif->tif_postencode)(tif))
485 return ((tmsize_t)(-1));
486 if (!isFillOrder(tif, td->td_fillorder) &&
487 (tif->tif_flags & TIFF_NOBITREV) == 0)
488 TIFFReverseBits((uint8*)tif->tif_rawdata, tif->tif_rawcc);
489 if (tif->tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile,
490 tif->tif_rawdata, tif->tif_rawcc))
491 return ((tmsize_t)(-1));
492 tif->tif_rawcc = 0;
493 tif->tif_rawcp = tif->tif_rawdata;
494 return (cc);
495 }
496
497 /*
498 * Write the supplied data to the specified strip.
499 * There must be space for the data; we don't check
500 * if strips overlap!
501 *
502 * NB: Image length must be setup before writing; this
503 * interface does not support automatically growing
504 * the image on each write (as TIFFWriteScanline does).
505 */
506 tmsize_t
TIFFWriteRawTile(TIFF * tif,uint32 tile,void * data,tmsize_t cc)507 TIFFWriteRawTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
508 {
509 static const char module[] = "TIFFWriteRawTile";
510
511 if (!WRITECHECKTILES(tif, module))
512 return ((tmsize_t)(-1));
513 if (tile >= tif->tif_dir.td_nstrips) {
514 TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
515 (unsigned long) tile,
516 (unsigned long) tif->tif_dir.td_nstrips);
517 return ((tmsize_t)(-1));
518 }
519 return (TIFFAppendToStrip(tif, tile, (uint8*) data, cc) ?
520 cc : (tmsize_t)(-1));
521 }
522
523 #define isUnspecified(tif, f) \
524 (TIFFFieldSet(tif,f) && (tif)->tif_dir.td_imagelength == 0)
525
526 int
TIFFSetupStrips(TIFF * tif)527 TIFFSetupStrips(TIFF* tif)
528 {
529 TIFFDirectory* td = &tif->tif_dir;
530
531 if (isTiled(tif))
532 td->td_stripsperimage =
533 isUnspecified(tif, FIELD_TILEDIMENSIONS) ?
534 td->td_samplesperpixel : TIFFNumberOfTiles(tif);
535 else
536 td->td_stripsperimage =
537 isUnspecified(tif, FIELD_ROWSPERSTRIP) ?
538 td->td_samplesperpixel : TIFFNumberOfStrips(tif);
539 td->td_nstrips = td->td_stripsperimage;
540 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
541 td->td_stripsperimage /= td->td_samplesperpixel;
542 td->td_stripoffset = (uint64 *)
543 _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
544 "for \"StripOffsets\" array");
545 td->td_stripbytecount = (uint64 *)
546 _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
547 "for \"StripByteCounts\" array");
548 if (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)
549 return (0);
550 /*
551 * Place data at the end-of-file
552 * (by setting offsets to zero).
553 */
554 _TIFFmemset(td->td_stripoffset, 0, td->td_nstrips*sizeof (uint64));
555 _TIFFmemset(td->td_stripbytecount, 0, td->td_nstrips*sizeof (uint64));
556 TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
557 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
558 return (1);
559 }
560 #undef isUnspecified
561
562 /*
563 * Verify file is writable and that the directory
564 * information is setup properly. In doing the latter
565 * we also "freeze" the state of the directory so
566 * that important information is not changed.
567 */
568 int
TIFFWriteCheck(TIFF * tif,int tiles,const char * module)569 TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
570 {
571 if (tif->tif_mode == O_RDONLY) {
572 TIFFErrorExt(tif->tif_clientdata, module, "File not open for writing");
573 return (0);
574 }
575 if (tiles ^ isTiled(tif)) {
576 TIFFErrorExt(tif->tif_clientdata, module, tiles ?
577 "Can not write tiles to a stripped image" :
578 "Can not write scanlines to a tiled image");
579 return (0);
580 }
581
582 _TIFFFillStriles( tif );
583
584 /*
585 * On the first write verify all the required information
586 * has been setup and initialize any data structures that
587 * had to wait until directory information was set.
588 * Note that a lot of our work is assumed to remain valid
589 * because we disallow any of the important parameters
590 * from changing after we start writing (i.e. once
591 * TIFF_BEENWRITING is set, TIFFSetField will only allow
592 * the image's length to be changed).
593 */
594 if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
595 TIFFErrorExt(tif->tif_clientdata, module,
596 "Must set \"ImageWidth\" before writing data");
597 return (0);
598 }
599 if (tif->tif_dir.td_samplesperpixel == 1) {
600 /*
601 * Planarconfiguration is irrelevant in case of single band
602 * images and need not be included. We will set it anyway,
603 * because this field is used in other parts of library even
604 * in the single band case.
605 */
606 if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))
607 tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
608 } else {
609 if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
610 TIFFErrorExt(tif->tif_clientdata, module,
611 "Must set \"PlanarConfiguration\" before writing data");
612 return (0);
613 }
614 }
615 if (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) {
616 tif->tif_dir.td_nstrips = 0;
617 TIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",
618 isTiled(tif) ? "tile" : "strip");
619 return (0);
620 }
621 if (isTiled(tif))
622 {
623 tif->tif_tilesize = TIFFTileSize(tif);
624 if (tif->tif_tilesize == 0)
625 return (0);
626 }
627 else
628 tif->tif_tilesize = (tmsize_t)(-1);
629 tif->tif_scanlinesize = TIFFScanlineSize(tif);
630 if (tif->tif_scanlinesize == 0)
631 return (0);
632 tif->tif_flags |= TIFF_BEENWRITING;
633 return (1);
634 }
635
636 /*
637 * Setup the raw data buffer used for encoding.
638 */
639 int
TIFFWriteBufferSetup(TIFF * tif,void * bp,tmsize_t size)640 TIFFWriteBufferSetup(TIFF* tif, void* bp, tmsize_t size)
641 {
642 static const char module[] = "TIFFWriteBufferSetup";
643
644 if (tif->tif_rawdata) {
645 if (tif->tif_flags & TIFF_MYBUFFER) {
646 _TIFFfree(tif->tif_rawdata);
647 tif->tif_flags &= ~TIFF_MYBUFFER;
648 }
649 tif->tif_rawdata = NULL;
650 }
651 if (size == (tmsize_t)(-1)) {
652 size = (isTiled(tif) ?
653 tif->tif_tilesize : TIFFStripSize(tif));
654 /*
655 * Make raw data buffer at least 8K
656 */
657 if (size < 8*1024)
658 size = 8*1024;
659 bp = NULL; /* NB: force malloc */
660 }
661 if (bp == NULL) {
662 bp = _TIFFmalloc(size);
663 if (bp == NULL) {
664 TIFFErrorExt(tif->tif_clientdata, module, "No space for output buffer");
665 return (0);
666 }
667 tif->tif_flags |= TIFF_MYBUFFER;
668 } else
669 tif->tif_flags &= ~TIFF_MYBUFFER;
670 tif->tif_rawdata = (uint8*) bp;
671 tif->tif_rawdatasize = size;
672 tif->tif_rawcc = 0;
673 tif->tif_rawcp = tif->tif_rawdata;
674 tif->tif_flags |= TIFF_BUFFERSETUP;
675 return (1);
676 }
677
678 /*
679 * Grow the strip data structures by delta strips.
680 */
681 static int
TIFFGrowStrips(TIFF * tif,uint32 delta,const char * module)682 TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module)
683 {
684 TIFFDirectory *td = &tif->tif_dir;
685 uint64* new_stripoffset;
686 uint64* new_stripbytecount;
687
688 assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
689 new_stripoffset = (uint64*)_TIFFrealloc(td->td_stripoffset,
690 (td->td_nstrips + delta) * sizeof (uint64));
691 new_stripbytecount = (uint64*)_TIFFrealloc(td->td_stripbytecount,
692 (td->td_nstrips + delta) * sizeof (uint64));
693 if (new_stripoffset == NULL || new_stripbytecount == NULL) {
694 if (new_stripoffset)
695 _TIFFfree(new_stripoffset);
696 if (new_stripbytecount)
697 _TIFFfree(new_stripbytecount);
698 td->td_nstrips = 0;
699 TIFFErrorExt(tif->tif_clientdata, module, "No space to expand strip arrays");
700 return (0);
701 }
702 td->td_stripoffset = new_stripoffset;
703 td->td_stripbytecount = new_stripbytecount;
704 _TIFFmemset(td->td_stripoffset + td->td_nstrips,
705 0, delta*sizeof (uint64));
706 _TIFFmemset(td->td_stripbytecount + td->td_nstrips,
707 0, delta*sizeof (uint64));
708 td->td_nstrips += delta;
709 tif->tif_flags |= TIFF_DIRTYDIRECT;
710
711 return (1);
712 }
713
714 /*
715 * Append the data to the specified strip.
716 */
717 static int
TIFFAppendToStrip(TIFF * tif,uint32 strip,uint8 * data,tmsize_t cc)718 TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc)
719 {
720 static const char module[] = "TIFFAppendToStrip";
721 TIFFDirectory *td = &tif->tif_dir;
722 uint64 m;
723 int64 old_byte_count = -1;
724
725 if (td->td_stripoffset[strip] == 0 || tif->tif_curoff == 0) {
726 assert(td->td_nstrips > 0);
727
728 if( td->td_stripbytecount[strip] != 0
729 && td->td_stripoffset[strip] != 0
730 && td->td_stripbytecount[strip] >= (uint64) cc )
731 {
732 /*
733 * There is already tile data on disk, and the new tile
734 * data we have will fit in the same space. The only
735 * aspect of this that is risky is that there could be
736 * more data to append to this strip before we are done
737 * depending on how we are getting called.
738 */
739 if (!SeekOK(tif, td->td_stripoffset[strip])) {
740 TIFFErrorExt(tif->tif_clientdata, module,
741 "Seek error at scanline %lu",
742 (unsigned long)tif->tif_row);
743 return (0);
744 }
745 }
746 else
747 {
748 /*
749 * Seek to end of file, and set that as our location to
750 * write this strip.
751 */
752 td->td_stripoffset[strip] = TIFFSeekFile(tif, 0, SEEK_END);
753 tif->tif_flags |= TIFF_DIRTYSTRIP;
754 }
755
756 tif->tif_curoff = td->td_stripoffset[strip];
757
758 /*
759 * We are starting a fresh strip/tile, so set the size to zero.
760 */
761 old_byte_count = td->td_stripbytecount[strip];
762 td->td_stripbytecount[strip] = 0;
763 }
764
765 m = tif->tif_curoff+cc;
766 if (!(tif->tif_flags&TIFF_BIGTIFF))
767 m = (uint32)m;
768 if ((m<tif->tif_curoff)||(m<(uint64)cc))
769 {
770 TIFFErrorExt(tif->tif_clientdata, module, "Maximum TIFF file size exceeded");
771 return (0);
772 }
773 if (!WriteOK(tif, data, cc)) {
774 TIFFErrorExt(tif->tif_clientdata, module, "Write error at scanline %lu",
775 (unsigned long) tif->tif_row);
776 return (0);
777 }
778 tif->tif_curoff = m;
779 td->td_stripbytecount[strip] += cc;
780
781 if( (int64) td->td_stripbytecount[strip] != old_byte_count )
782 tif->tif_flags |= TIFF_DIRTYSTRIP;
783
784 return (1);
785 }
786
787 /*
788 * Internal version of TIFFFlushData that can be
789 * called by ``encodestrip routines'' w/o concern
790 * for infinite recursion.
791 */
792 int
TIFFFlushData1(TIFF * tif)793 TIFFFlushData1(TIFF* tif)
794 {
795 if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE ) {
796 if (!isFillOrder(tif, tif->tif_dir.td_fillorder) &&
797 (tif->tif_flags & TIFF_NOBITREV) == 0)
798 TIFFReverseBits((uint8*)tif->tif_rawdata,
799 tif->tif_rawcc);
800 if (!TIFFAppendToStrip(tif,
801 isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip,
802 tif->tif_rawdata, tif->tif_rawcc))
803 {
804 /* We update those variables even in case of error since there's */
805 /* code that doesn't really check the return code of this */
806 /* function */
807 tif->tif_rawcc = 0;
808 tif->tif_rawcp = tif->tif_rawdata;
809 return (0);
810 }
811 tif->tif_rawcc = 0;
812 tif->tif_rawcp = tif->tif_rawdata;
813 }
814 return (1);
815 }
816
817 /*
818 * Set the current write offset. This should only be
819 * used to set the offset to a known previous location
820 * (very carefully), or to 0 so that the next write gets
821 * appended to the end of the file.
822 */
823 void
TIFFSetWriteOffset(TIFF * tif,toff_t off)824 TIFFSetWriteOffset(TIFF* tif, toff_t off)
825 {
826 tif->tif_curoff = off;
827 }
828
829 /* vim: set ts=8 sts=8 sw=8 noet: */
830 /*
831 * Local Variables:
832 * mode: c
833 * c-basic-offset: 8
834 * fill-column: 78
835 * End:
836 */
837