/src/gdal/frmts/gtiff/libtiff/tif_write.c
Line | Count | Source |
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 | | */ |
30 | | #include "tiffiop.h" |
31 | | #include <stdio.h> |
32 | | |
33 | | #define STRIPINCR 20 /* expansion factor on strip array */ |
34 | | |
35 | 0 | #define NOSTRIP ((uint32_t)(-1)) /* undefined state */ |
36 | | |
37 | | #define WRITECHECKSTRIPS(tif, module) \ |
38 | 0 | (((tif)->tif_flags & TIFF_BEENWRITING) || TIFFWriteCheck((tif), 0, module)) |
39 | | #define WRITECHECKTILES(tif, module) \ |
40 | 0 | (((tif)->tif_flags & TIFF_BEENWRITING) || TIFFWriteCheck((tif), 1, module)) |
41 | | #define BUFFERCHECK(tif) \ |
42 | 0 | ((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) || \ |
43 | 0 | TIFFWriteBufferSetup((tif), NULL, (tmsize_t)-1)) |
44 | | |
45 | | static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module); |
46 | | static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data, |
47 | | tmsize_t cc); |
48 | | |
49 | | int TIFFWriteScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample) |
50 | 0 | { |
51 | 0 | static const char module[] = "TIFFWriteScanline"; |
52 | 0 | TIFFDirectory *td; |
53 | 0 | int status, imagegrew = 0; |
54 | 0 | uint32_t strip; |
55 | |
|
56 | 0 | if (!WRITECHECKSTRIPS(tif, module)) |
57 | 0 | 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 | 0 | if (!BUFFERCHECK(tif)) |
64 | 0 | return (-1); |
65 | 0 | tif->tif_flags |= TIFF_BUF4WRITE; /* not strictly sure this is right*/ |
66 | |
|
67 | 0 | td = &tif->tif_dir; |
68 | | /* |
69 | | * Extend image length if needed |
70 | | * (but only for PlanarConfig=1). |
71 | | */ |
72 | 0 | if (row >= td->td_imagelength) |
73 | 0 | { /* extend image */ |
74 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
75 | 0 | { |
76 | 0 | TIFFErrorExtR( |
77 | 0 | tif, module, |
78 | 0 | "Can not change \"ImageLength\" when using separate planes"); |
79 | 0 | return (-1); |
80 | 0 | } |
81 | 0 | td->td_imagelength = row + 1; |
82 | 0 | imagegrew = 1; |
83 | 0 | } |
84 | | /* |
85 | | * Calculate strip and check for crossings. |
86 | | */ |
87 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
88 | 0 | { |
89 | 0 | if (sample >= td->td_samplesperpixel) |
90 | 0 | { |
91 | 0 | TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu", |
92 | 0 | (unsigned long)sample, |
93 | 0 | (unsigned long)td->td_samplesperpixel); |
94 | 0 | return (-1); |
95 | 0 | } |
96 | 0 | strip = sample * td->td_stripsperimage + row / td->td_rowsperstrip; |
97 | 0 | } |
98 | 0 | else |
99 | 0 | strip = row / td->td_rowsperstrip; |
100 | | /* |
101 | | * Check strip array to make sure there's space. We don't support |
102 | | * dynamically growing files that have data organized in separate |
103 | | * bitplanes because it's too painful. In that case we require that |
104 | | * the imagelength be set properly before the first write (so that the |
105 | | * strips array will be fully allocated above). |
106 | | */ |
107 | 0 | if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module)) |
108 | 0 | return (-1); |
109 | 0 | if (strip != tif->tif_curstrip) |
110 | 0 | { |
111 | | /* |
112 | | * Changing strips -- flush any data present. |
113 | | */ |
114 | 0 | if (!TIFFFlushData(tif)) |
115 | 0 | return (-1); |
116 | 0 | tif->tif_curstrip = strip; |
117 | | /* |
118 | | * Watch out for a growing image. The value of strips/image |
119 | | * will initially be 1 (since it can't be deduced until the |
120 | | * imagelength is known). |
121 | | */ |
122 | 0 | if (strip >= td->td_stripsperimage && imagegrew) |
123 | 0 | td->td_stripsperimage = |
124 | 0 | TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); |
125 | 0 | if (td->td_stripsperimage == 0) |
126 | 0 | { |
127 | 0 | TIFFErrorExtR(tif, module, "Zero strips per image"); |
128 | 0 | return (-1); |
129 | 0 | } |
130 | 0 | tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
131 | 0 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) |
132 | 0 | { |
133 | 0 | if (!(*tif->tif_setupencode)(tif)) |
134 | 0 | return (-1); |
135 | 0 | tif->tif_flags |= TIFF_CODERSETUP; |
136 | 0 | } |
137 | | |
138 | 0 | tif->tif_rawcc = 0; |
139 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
140 | | |
141 | | /* this informs TIFFAppendToStrip() we have changed strip */ |
142 | 0 | tif->tif_curoff = 0; |
143 | |
|
144 | 0 | if (!(*tif->tif_preencode)(tif, sample)) |
145 | 0 | return (-1); |
146 | 0 | tif->tif_flags |= TIFF_POSTENCODE; |
147 | 0 | } |
148 | | /* |
149 | | * Ensure the write is either sequential or at the |
150 | | * beginning of a strip (or that we can randomly |
151 | | * access the data -- i.e. no encoding). |
152 | | */ |
153 | 0 | if (row != tif->tif_row) |
154 | 0 | { |
155 | 0 | if (row < tif->tif_row) |
156 | 0 | { |
157 | | /* |
158 | | * Moving backwards within the same strip: |
159 | | * backup to the start and then decode |
160 | | * forward (below). |
161 | | */ |
162 | 0 | tif->tif_row = |
163 | 0 | (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
164 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
165 | 0 | } |
166 | | /* |
167 | | * Seek forward to the desired row. |
168 | | */ |
169 | 0 | if (!(*tif->tif_seek)(tif, row - tif->tif_row)) |
170 | 0 | return (-1); |
171 | 0 | tif->tif_row = row; |
172 | 0 | } |
173 | | |
174 | | /* swab if needed - note that source buffer will be altered */ |
175 | 0 | tif->tif_postdecode(tif, (uint8_t *)buf, tif->tif_scanlinesize); |
176 | |
|
177 | 0 | status = (*tif->tif_encoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize, |
178 | 0 | sample); |
179 | | |
180 | | /* we are now poised at the beginning of the next row */ |
181 | 0 | tif->tif_row = row + 1; |
182 | 0 | return (status); |
183 | 0 | } |
184 | | |
185 | | /* Make sure that at the first attempt of rewriting a tile/strip, we will have |
186 | | */ |
187 | | /* more bytes available in the output buffer than the previous byte count, */ |
188 | | /* so that TIFFAppendToStrip() will detect the overflow when it is called the |
189 | | * first */ |
190 | | /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */ |
191 | | static int _TIFFReserveLargeEnoughWriteBuffer(TIFF *tif, uint32_t strip_or_tile) |
192 | 0 | { |
193 | 0 | TIFFDirectory *td = &tif->tif_dir; |
194 | 0 | if (td->td_stripbytecount_p[strip_or_tile] > 0) |
195 | 0 | { |
196 | | /* The +1 is to ensure at least one extra bytes */ |
197 | | /* The +4 is because the LZW encoder flushes 4 bytes before the limit */ |
198 | 0 | uint64_t safe_buffer_size = |
199 | 0 | (uint64_t)(td->td_stripbytecount_p[strip_or_tile] + 1 + 4); |
200 | 0 | if (tif->tif_rawdatasize <= (tmsize_t)safe_buffer_size) |
201 | 0 | { |
202 | 0 | if (!(TIFFWriteBufferSetup( |
203 | 0 | tif, NULL, |
204 | 0 | (tmsize_t)TIFFroundup_64(safe_buffer_size, 1024)))) |
205 | 0 | return 0; |
206 | 0 | } |
207 | 0 | } |
208 | 0 | return 1; |
209 | 0 | } |
210 | | |
211 | | /* |
212 | | * Encode the supplied data and write it to the |
213 | | * specified strip. |
214 | | * |
215 | | * NB: Image length must be setup before writing. |
216 | | */ |
217 | | tmsize_t TIFFWriteEncodedStrip(TIFF *tif, uint32_t strip, void *data, |
218 | | tmsize_t cc) |
219 | 0 | { |
220 | 0 | static const char module[] = "TIFFWriteEncodedStrip"; |
221 | 0 | TIFFDirectory *td = &tif->tif_dir; |
222 | 0 | uint16_t sample; |
223 | |
|
224 | 0 | if (!WRITECHECKSTRIPS(tif, module)) |
225 | 0 | return ((tmsize_t)-1); |
226 | | /* |
227 | | * Check strip array to make sure there's space. |
228 | | * We don't support dynamically growing files that |
229 | | * have data organized in separate bitplanes because |
230 | | * it's too painful. In that case we require that |
231 | | * the imagelength be set properly before the first |
232 | | * write (so that the strips array will be fully |
233 | | * allocated above). |
234 | | */ |
235 | 0 | if (strip >= td->td_nstrips) |
236 | 0 | { |
237 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
238 | 0 | { |
239 | 0 | TIFFErrorExtR( |
240 | 0 | tif, module, |
241 | 0 | "Can not grow image by strips when using separate planes"); |
242 | 0 | return ((tmsize_t)-1); |
243 | 0 | } |
244 | 0 | if (!TIFFGrowStrips(tif, 1, module)) |
245 | 0 | return ((tmsize_t)-1); |
246 | 0 | td->td_stripsperimage = |
247 | 0 | TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); |
248 | 0 | } |
249 | | /* |
250 | | * Handle delayed allocation of data buffer. This |
251 | | * permits it to be sized according to the directory |
252 | | * info. |
253 | | */ |
254 | 0 | if (!BUFFERCHECK(tif)) |
255 | 0 | return ((tmsize_t)-1); |
256 | | |
257 | 0 | tif->tif_flags |= TIFF_BUF4WRITE; |
258 | |
|
259 | 0 | tif->tif_curstrip = strip; |
260 | | |
261 | | /* this informs TIFFAppendToStrip() we have changed or reset strip */ |
262 | 0 | tif->tif_curoff = 0; |
263 | |
|
264 | 0 | if (!_TIFFReserveLargeEnoughWriteBuffer(tif, strip)) |
265 | 0 | { |
266 | 0 | return ((tmsize_t)(-1)); |
267 | 0 | } |
268 | | |
269 | 0 | tif->tif_rawcc = 0; |
270 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
271 | |
|
272 | 0 | if (td->td_stripsperimage == 0) |
273 | 0 | { |
274 | 0 | TIFFErrorExtR(tif, module, "Zero strips per image"); |
275 | 0 | return ((tmsize_t)-1); |
276 | 0 | } |
277 | | |
278 | 0 | tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
279 | 0 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) |
280 | 0 | { |
281 | 0 | if (!(*tif->tif_setupencode)(tif)) |
282 | 0 | return ((tmsize_t)-1); |
283 | 0 | tif->tif_flags |= TIFF_CODERSETUP; |
284 | 0 | } |
285 | | |
286 | 0 | tif->tif_flags &= ~TIFF_POSTENCODE; |
287 | | |
288 | | /* shortcut to avoid an extra memcpy() */ |
289 | 0 | if (td->td_compression == COMPRESSION_NONE) |
290 | 0 | { |
291 | | /* swab if needed - note that source buffer will be altered */ |
292 | 0 | tif->tif_postdecode(tif, (uint8_t *)data, cc); |
293 | |
|
294 | 0 | if (!isFillOrder(tif, td->td_fillorder) && |
295 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
296 | 0 | TIFFReverseBits((uint8_t *)data, cc); |
297 | |
|
298 | 0 | if (cc > 0 && !TIFFAppendToStrip(tif, strip, (uint8_t *)data, cc)) |
299 | 0 | return ((tmsize_t)-1); |
300 | 0 | return (cc); |
301 | 0 | } |
302 | | |
303 | 0 | sample = (uint16_t)(strip / td->td_stripsperimage); |
304 | 0 | if (!(*tif->tif_preencode)(tif, sample)) |
305 | 0 | return ((tmsize_t)-1); |
306 | | |
307 | | /* swab if needed - note that source buffer will be altered */ |
308 | 0 | tif->tif_postdecode(tif, (uint8_t *)data, cc); |
309 | |
|
310 | 0 | if (!(*tif->tif_encodestrip)(tif, (uint8_t *)data, cc, sample)) |
311 | 0 | return ((tmsize_t)-1); |
312 | 0 | if (!(*tif->tif_postencode)(tif)) |
313 | 0 | return ((tmsize_t)-1); |
314 | 0 | if (!isFillOrder(tif, td->td_fillorder) && |
315 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
316 | 0 | TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc); |
317 | 0 | if (tif->tif_rawcc > 0 && |
318 | 0 | !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc)) |
319 | 0 | return ((tmsize_t)-1); |
320 | 0 | tif->tif_rawcc = 0; |
321 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
322 | 0 | return (cc); |
323 | 0 | } |
324 | | |
325 | | /* |
326 | | * Write the supplied data to the specified strip. |
327 | | * |
328 | | * NB: Image length must be setup before writing. |
329 | | */ |
330 | | tmsize_t TIFFWriteRawStrip(TIFF *tif, uint32_t strip, void *data, tmsize_t cc) |
331 | 0 | { |
332 | 0 | static const char module[] = "TIFFWriteRawStrip"; |
333 | 0 | TIFFDirectory *td = &tif->tif_dir; |
334 | |
|
335 | 0 | if (!WRITECHECKSTRIPS(tif, module)) |
336 | 0 | return ((tmsize_t)-1); |
337 | | /* |
338 | | * Check strip array to make sure there's space. |
339 | | * We don't support dynamically growing files that |
340 | | * have data organized in separate bitplanes because |
341 | | * it's too painful. In that case we require that |
342 | | * the imagelength be set properly before the first |
343 | | * write (so that the strips array will be fully |
344 | | * allocated above). |
345 | | */ |
346 | 0 | if (strip >= td->td_nstrips) |
347 | 0 | { |
348 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
349 | 0 | { |
350 | 0 | TIFFErrorExtR( |
351 | 0 | tif, module, |
352 | 0 | "Can not grow image by strips when using separate planes"); |
353 | 0 | return ((tmsize_t)-1); |
354 | 0 | } |
355 | | /* |
356 | | * Watch out for a growing image. The value of |
357 | | * strips/image will initially be 1 (since it |
358 | | * can't be deduced until the imagelength is known). |
359 | | */ |
360 | 0 | if (strip >= td->td_stripsperimage) |
361 | 0 | td->td_stripsperimage = |
362 | 0 | TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); |
363 | 0 | if (!TIFFGrowStrips(tif, 1, module)) |
364 | 0 | return ((tmsize_t)-1); |
365 | 0 | } |
366 | | |
367 | 0 | if (tif->tif_curstrip != strip) |
368 | 0 | { |
369 | 0 | tif->tif_curstrip = strip; |
370 | | |
371 | | /* this informs TIFFAppendToStrip() we have changed or reset strip */ |
372 | 0 | tif->tif_curoff = 0; |
373 | 0 | } |
374 | |
|
375 | 0 | if (td->td_stripsperimage == 0) |
376 | 0 | { |
377 | 0 | TIFFErrorExtR(tif, module, "Zero strips per image"); |
378 | 0 | return ((tmsize_t)-1); |
379 | 0 | } |
380 | 0 | tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
381 | 0 | return (TIFFAppendToStrip(tif, strip, (uint8_t *)data, cc) ? cc |
382 | 0 | : (tmsize_t)-1); |
383 | 0 | } |
384 | | |
385 | | /* |
386 | | * Write and compress a tile of data. The |
387 | | * tile is selected by the (x,y,z,s) coordinates. |
388 | | */ |
389 | | tmsize_t TIFFWriteTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z, |
390 | | uint16_t s) |
391 | 0 | { |
392 | 0 | if (!TIFFCheckTile(tif, x, y, z, s)) |
393 | 0 | return ((tmsize_t)(-1)); |
394 | | /* |
395 | | * NB: A tile size of -1 is used instead of tif_tilesize knowing |
396 | | * that TIFFWriteEncodedTile will clamp this to the tile size. |
397 | | * This is done because the tile size may not be defined until |
398 | | * after the output buffer is setup in TIFFWriteBufferSetup. |
399 | | */ |
400 | 0 | return (TIFFWriteEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf, |
401 | 0 | (tmsize_t)(-1))); |
402 | 0 | } |
403 | | |
404 | | /* |
405 | | * Encode the supplied data and write it to the |
406 | | * specified tile. There must be space for the |
407 | | * data. The function clamps individual writes |
408 | | * to a tile to the tile size, but does not (and |
409 | | * can not) check that multiple writes to the same |
410 | | * tile do not write more than tile size data. |
411 | | * |
412 | | * NB: Image length must be setup before writing; this |
413 | | * interface does not support automatically growing |
414 | | * the image on each write (as TIFFWriteScanline does). |
415 | | */ |
416 | | tmsize_t TIFFWriteEncodedTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc) |
417 | 0 | { |
418 | 0 | static const char module[] = "TIFFWriteEncodedTile"; |
419 | 0 | TIFFDirectory *td; |
420 | 0 | uint16_t sample; |
421 | 0 | uint32_t howmany32; |
422 | |
|
423 | 0 | if (!WRITECHECKTILES(tif, module)) |
424 | 0 | return ((tmsize_t)(-1)); |
425 | 0 | td = &tif->tif_dir; |
426 | 0 | if (tile >= td->td_nstrips) |
427 | 0 | { |
428 | 0 | TIFFErrorExtR(tif, module, "Tile %lu out of range, max %lu", |
429 | 0 | (unsigned long)tile, (unsigned long)td->td_nstrips); |
430 | 0 | return ((tmsize_t)(-1)); |
431 | 0 | } |
432 | | /* |
433 | | * Handle delayed allocation of data buffer. This |
434 | | * permits it to be sized more intelligently (using |
435 | | * directory information). |
436 | | */ |
437 | 0 | if (!BUFFERCHECK(tif)) |
438 | 0 | return ((tmsize_t)(-1)); |
439 | | |
440 | 0 | tif->tif_flags |= TIFF_BUF4WRITE; |
441 | |
|
442 | 0 | tif->tif_curtile = tile; |
443 | | |
444 | | /* this informs TIFFAppendToStrip() we have changed or reset tile */ |
445 | 0 | tif->tif_curoff = 0; |
446 | |
|
447 | 0 | if (!_TIFFReserveLargeEnoughWriteBuffer(tif, tile)) |
448 | 0 | { |
449 | 0 | return ((tmsize_t)(-1)); |
450 | 0 | } |
451 | | |
452 | 0 | tif->tif_rawcc = 0; |
453 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
454 | | |
455 | | /* |
456 | | * Compute tiles per row & per column to compute |
457 | | * current row and column |
458 | | */ |
459 | 0 | howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength); |
460 | 0 | if (howmany32 == 0) |
461 | 0 | { |
462 | 0 | TIFFErrorExtR(tif, module, "Zero tiles"); |
463 | 0 | return ((tmsize_t)(-1)); |
464 | 0 | } |
465 | 0 | tif->tif_row = (tile % howmany32) * td->td_tilelength; |
466 | 0 | howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth); |
467 | 0 | if (howmany32 == 0) |
468 | 0 | { |
469 | 0 | TIFFErrorExtR(tif, module, "Zero tiles"); |
470 | 0 | return ((tmsize_t)(-1)); |
471 | 0 | } |
472 | 0 | tif->tif_col = (tile % howmany32) * td->td_tilewidth; |
473 | |
|
474 | 0 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) |
475 | 0 | { |
476 | 0 | if (!(*tif->tif_setupencode)(tif)) |
477 | 0 | return ((tmsize_t)(-1)); |
478 | 0 | tif->tif_flags |= TIFF_CODERSETUP; |
479 | 0 | } |
480 | 0 | tif->tif_flags &= ~TIFF_POSTENCODE; |
481 | | |
482 | | /* |
483 | | * Clamp write amount to the tile size. This is mostly |
484 | | * done so that callers can pass in some large number |
485 | | * (e.g. -1) and have the tile size used instead. |
486 | | */ |
487 | 0 | if (cc < 1 || cc > tif->tif_tilesize) |
488 | 0 | cc = tif->tif_tilesize; |
489 | | |
490 | | /* shortcut to avoid an extra memcpy() */ |
491 | 0 | if (td->td_compression == COMPRESSION_NONE) |
492 | 0 | { |
493 | | /* swab if needed - note that source buffer will be altered */ |
494 | 0 | tif->tif_postdecode(tif, (uint8_t *)data, cc); |
495 | |
|
496 | 0 | if (!isFillOrder(tif, td->td_fillorder) && |
497 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
498 | 0 | TIFFReverseBits((uint8_t *)data, cc); |
499 | |
|
500 | 0 | if (cc > 0 && !TIFFAppendToStrip(tif, tile, (uint8_t *)data, cc)) |
501 | 0 | return ((tmsize_t)-1); |
502 | 0 | return (cc); |
503 | 0 | } |
504 | | |
505 | 0 | sample = (uint16_t)(tile / td->td_stripsperimage); |
506 | 0 | if (!(*tif->tif_preencode)(tif, sample)) |
507 | 0 | return ((tmsize_t)(-1)); |
508 | | /* swab if needed - note that source buffer will be altered */ |
509 | 0 | tif->tif_postdecode(tif, (uint8_t *)data, cc); |
510 | |
|
511 | 0 | if (!(*tif->tif_encodetile)(tif, (uint8_t *)data, cc, sample)) |
512 | 0 | return ((tmsize_t)-1); |
513 | 0 | if (!(*tif->tif_postencode)(tif)) |
514 | 0 | return ((tmsize_t)(-1)); |
515 | 0 | if (!isFillOrder(tif, td->td_fillorder) && |
516 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
517 | 0 | TIFFReverseBits((uint8_t *)tif->tif_rawdata, tif->tif_rawcc); |
518 | 0 | if (tif->tif_rawcc > 0 && |
519 | 0 | !TIFFAppendToStrip(tif, tile, tif->tif_rawdata, tif->tif_rawcc)) |
520 | 0 | return ((tmsize_t)(-1)); |
521 | 0 | tif->tif_rawcc = 0; |
522 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
523 | 0 | return (cc); |
524 | 0 | } |
525 | | |
526 | | /* |
527 | | * Write the supplied data to the specified strip. |
528 | | * There must be space for the data; we don't check |
529 | | * if strips overlap! |
530 | | * |
531 | | * NB: Image length must be setup before writing; this |
532 | | * interface does not support automatically growing |
533 | | * the image on each write (as TIFFWriteScanline does). |
534 | | */ |
535 | | tmsize_t TIFFWriteRawTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc) |
536 | 0 | { |
537 | 0 | static const char module[] = "TIFFWriteRawTile"; |
538 | |
|
539 | 0 | if (!WRITECHECKTILES(tif, module)) |
540 | 0 | return ((tmsize_t)(-1)); |
541 | 0 | if (tile >= tif->tif_dir.td_nstrips) |
542 | 0 | { |
543 | 0 | TIFFErrorExtR(tif, module, "Tile %lu out of range, max %lu", |
544 | 0 | (unsigned long)tile, |
545 | 0 | (unsigned long)tif->tif_dir.td_nstrips); |
546 | 0 | return ((tmsize_t)(-1)); |
547 | 0 | } |
548 | 0 | return (TIFFAppendToStrip(tif, tile, (uint8_t *)data, cc) ? cc |
549 | 0 | : (tmsize_t)(-1)); |
550 | 0 | } |
551 | | |
552 | | #define isUnspecified(tif, f) \ |
553 | 0 | (TIFFFieldSet(tif, f) && (tif)->tif_dir.td_imagelength == 0) |
554 | | |
555 | | int TIFFSetupStrips(TIFF *tif) |
556 | 0 | { |
557 | 0 | TIFFDirectory *td = &tif->tif_dir; |
558 | |
|
559 | 0 | if (isTiled(tif)) |
560 | 0 | td->td_stripsperimage = isUnspecified(tif, FIELD_TILEDIMENSIONS) |
561 | 0 | ? td->td_samplesperpixel |
562 | 0 | : TIFFNumberOfTiles(tif); |
563 | 0 | else |
564 | 0 | td->td_stripsperimage = isUnspecified(tif, FIELD_ROWSPERSTRIP) |
565 | 0 | ? td->td_samplesperpixel |
566 | 0 | : TIFFNumberOfStrips(tif); |
567 | 0 | td->td_nstrips = td->td_stripsperimage; |
568 | | /* TIFFWriteDirectoryTagData has a limitation to 0x80000000U bytes */ |
569 | 0 | if (td->td_nstrips >= |
570 | 0 | 0x80000000U / ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U)) |
571 | 0 | { |
572 | 0 | TIFFErrorExtR(tif, "TIFFSetupStrips", |
573 | 0 | "Too large Strip/Tile Offsets/ByteCounts arrays"); |
574 | 0 | return 0; |
575 | 0 | } |
576 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
577 | 0 | td->td_stripsperimage /= td->td_samplesperpixel; |
578 | |
|
579 | 0 | if (td->td_stripoffset_p != NULL) |
580 | 0 | _TIFFfreeExt(tif, td->td_stripoffset_p); |
581 | 0 | td->td_stripoffset_p = (uint64_t *)_TIFFCheckMalloc( |
582 | 0 | tif, td->td_nstrips, sizeof(uint64_t), "for \"StripOffsets\" array"); |
583 | 0 | if (td->td_stripbytecount_p != NULL) |
584 | 0 | _TIFFfreeExt(tif, td->td_stripbytecount_p); |
585 | 0 | td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc( |
586 | 0 | tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array"); |
587 | 0 | if (td->td_stripoffset_p == NULL || td->td_stripbytecount_p == NULL) |
588 | 0 | return (0); |
589 | | /* |
590 | | * Place data at the end-of-file |
591 | | * (by setting offsets to zero). |
592 | | */ |
593 | 0 | _TIFFmemset(td->td_stripoffset_p, 0, td->td_nstrips * sizeof(uint64_t)); |
594 | 0 | _TIFFmemset(td->td_stripbytecount_p, 0, td->td_nstrips * sizeof(uint64_t)); |
595 | 0 | TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS); |
596 | 0 | TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); |
597 | 0 | return (1); |
598 | 0 | } |
599 | | #undef isUnspecified |
600 | | |
601 | | /* |
602 | | * Verify file is writable and that the directory |
603 | | * information is setup properly. In doing the latter |
604 | | * we also "freeze" the state of the directory so |
605 | | * that important information is not changed. |
606 | | */ |
607 | | int TIFFWriteCheck(TIFF *tif, int tiles, const char *module) |
608 | 0 | { |
609 | 0 | if (tif->tif_mode == O_RDONLY) |
610 | 0 | { |
611 | 0 | TIFFErrorExtR(tif, module, "File not open for writing"); |
612 | 0 | return (0); |
613 | 0 | } |
614 | 0 | if (tiles ^ isTiled(tif)) |
615 | 0 | { |
616 | 0 | TIFFErrorExtR(tif, module, |
617 | 0 | tiles ? "Can not write tiles to a striped image" |
618 | 0 | : "Can not write scanlines to a tiled image"); |
619 | 0 | return (0); |
620 | 0 | } |
621 | | |
622 | 0 | _TIFFFillStriles(tif); |
623 | | |
624 | | /* |
625 | | * On the first write verify all the required information |
626 | | * has been setup and initialize any data structures that |
627 | | * had to wait until directory information was set. |
628 | | * Note that a lot of our work is assumed to remain valid |
629 | | * because we disallow any of the important parameters |
630 | | * from changing after we start writing (i.e. once |
631 | | * TIFF_BEENWRITING is set, TIFFSetField will only allow |
632 | | * the image's length to be changed). |
633 | | */ |
634 | 0 | if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) |
635 | 0 | { |
636 | 0 | TIFFErrorExtR(tif, module, |
637 | 0 | "Must set \"ImageWidth\" before writing data"); |
638 | 0 | return (0); |
639 | 0 | } |
640 | 0 | if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) |
641 | 0 | { |
642 | 0 | tif->tif_dir.td_nstrips = 0; |
643 | 0 | TIFFErrorExtR(tif, module, "No space for %s arrays", |
644 | 0 | isTiled(tif) ? "tile" : "strip"); |
645 | 0 | return (0); |
646 | 0 | } |
647 | 0 | if (isTiled(tif)) |
648 | 0 | { |
649 | 0 | tif->tif_tilesize = TIFFTileSize(tif); |
650 | 0 | if (tif->tif_tilesize == 0) |
651 | 0 | return (0); |
652 | 0 | } |
653 | 0 | else |
654 | 0 | tif->tif_tilesize = (tmsize_t)(-1); |
655 | 0 | tif->tif_scanlinesize = TIFFScanlineSize(tif); |
656 | 0 | if (tif->tif_scanlinesize == 0) |
657 | 0 | return (0); |
658 | 0 | tif->tif_flags |= TIFF_BEENWRITING; |
659 | |
|
660 | 0 | if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 && |
661 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_count == 0 && |
662 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_type == 0 && |
663 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 && |
664 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 && |
665 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 && |
666 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 && |
667 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 && |
668 | 0 | !(tif->tif_flags & TIFF_DIRTYDIRECT)) |
669 | 0 | { |
670 | 0 | TIFFForceStrileArrayWriting(tif); |
671 | 0 | } |
672 | |
|
673 | 0 | return (1); |
674 | 0 | } |
675 | | |
676 | | /* |
677 | | * Setup the raw data buffer used for encoding. |
678 | | */ |
679 | | int TIFFWriteBufferSetup(TIFF *tif, void *bp, tmsize_t size) |
680 | 0 | { |
681 | 0 | static const char module[] = "TIFFWriteBufferSetup"; |
682 | |
|
683 | 0 | if (tif->tif_rawdata) |
684 | 0 | { |
685 | 0 | if (tif->tif_flags & TIFF_MYBUFFER) |
686 | 0 | { |
687 | 0 | _TIFFfreeExt(tif, tif->tif_rawdata); |
688 | 0 | tif->tif_flags &= ~TIFF_MYBUFFER; |
689 | 0 | } |
690 | 0 | tif->tif_rawdata = NULL; |
691 | 0 | } |
692 | 0 | if (size == (tmsize_t)(-1)) |
693 | 0 | { |
694 | 0 | size = (isTiled(tif) ? tif->tif_tilesize : TIFFStripSize(tif)); |
695 | | |
696 | | /* Adds 10% margin for cases where compression would expand a bit */ |
697 | 0 | if (size < TIFF_TMSIZE_T_MAX - size / 10) |
698 | 0 | size += size / 10; |
699 | | /* |
700 | | * Make raw data buffer at least 8K |
701 | | */ |
702 | 0 | if (size < 8 * 1024) |
703 | 0 | size = 8 * 1024; |
704 | 0 | bp = NULL; /* NB: force malloc */ |
705 | 0 | } |
706 | 0 | if (bp == NULL) |
707 | 0 | { |
708 | 0 | bp = _TIFFmallocExt(tif, size); |
709 | 0 | if (bp == NULL) |
710 | 0 | { |
711 | 0 | TIFFErrorExtR(tif, module, "No space for output buffer"); |
712 | 0 | return (0); |
713 | 0 | } |
714 | 0 | tif->tif_flags |= TIFF_MYBUFFER; |
715 | 0 | } |
716 | 0 | else |
717 | 0 | tif->tif_flags &= ~TIFF_MYBUFFER; |
718 | 0 | tif->tif_rawdata = (uint8_t *)bp; |
719 | 0 | tif->tif_rawdatasize = size; |
720 | 0 | tif->tif_rawcc = 0; |
721 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
722 | 0 | tif->tif_flags |= TIFF_BUFFERSETUP; |
723 | 0 | return (1); |
724 | 0 | } |
725 | | |
726 | | /* |
727 | | * Grow the strip data structures by delta strips. |
728 | | */ |
729 | | static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module) |
730 | 0 | { |
731 | 0 | TIFFDirectory *td = &tif->tif_dir; |
732 | 0 | uint64_t *new_stripoffset; |
733 | 0 | uint64_t *new_stripbytecount; |
734 | |
|
735 | 0 | assert(td->td_planarconfig == PLANARCONFIG_CONTIG); |
736 | 0 | new_stripoffset = (uint64_t *)_TIFFreallocExt( |
737 | 0 | tif, td->td_stripoffset_p, (td->td_nstrips + delta) * sizeof(uint64_t)); |
738 | 0 | new_stripbytecount = (uint64_t *)_TIFFreallocExt( |
739 | 0 | tif, td->td_stripbytecount_p, |
740 | 0 | (td->td_nstrips + delta) * sizeof(uint64_t)); |
741 | 0 | if (new_stripoffset == NULL || new_stripbytecount == NULL) |
742 | 0 | { |
743 | 0 | if (new_stripoffset) |
744 | 0 | _TIFFfreeExt(tif, new_stripoffset); |
745 | 0 | if (new_stripbytecount) |
746 | 0 | _TIFFfreeExt(tif, new_stripbytecount); |
747 | 0 | td->td_nstrips = 0; |
748 | 0 | TIFFErrorExtR(tif, module, "No space to expand strip arrays"); |
749 | 0 | return (0); |
750 | 0 | } |
751 | 0 | td->td_stripoffset_p = new_stripoffset; |
752 | 0 | td->td_stripbytecount_p = new_stripbytecount; |
753 | 0 | _TIFFmemset(td->td_stripoffset_p + td->td_nstrips, 0, |
754 | 0 | delta * sizeof(uint64_t)); |
755 | 0 | _TIFFmemset(td->td_stripbytecount_p + td->td_nstrips, 0, |
756 | 0 | delta * sizeof(uint64_t)); |
757 | 0 | td->td_nstrips += delta; |
758 | 0 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
759 | |
|
760 | 0 | return (1); |
761 | 0 | } |
762 | | |
763 | | /* |
764 | | * Append the data to the specified strip. |
765 | | */ |
766 | | static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data, |
767 | | tmsize_t cc) |
768 | 0 | { |
769 | 0 | static const char module[] = "TIFFAppendToStrip"; |
770 | 0 | TIFFDirectory *td = &tif->tif_dir; |
771 | 0 | uint64_t m; |
772 | 0 | int64_t old_byte_count = -1; |
773 | | |
774 | | /* Some security checks */ |
775 | 0 | if (td->td_stripoffset_p == NULL) |
776 | 0 | { |
777 | 0 | TIFFErrorExtR(tif, module, "Strip offset array pointer is NULL"); |
778 | 0 | return (0); |
779 | 0 | } |
780 | 0 | if (td->td_stripbytecount_p == NULL) |
781 | 0 | { |
782 | 0 | TIFFErrorExtR(tif, module, "Strip bytecount array pointer is NULL"); |
783 | 0 | return (0); |
784 | 0 | } |
785 | 0 | if (strip == NOSTRIP) |
786 | 0 | { |
787 | 0 | TIFFErrorExtR(tif, module, "Strip number not valid (NOSTRIP)"); |
788 | 0 | return (0); |
789 | 0 | } |
790 | | |
791 | 0 | if (tif->tif_curoff == 0) |
792 | 0 | tif->tif_lastvalidoff = 0; |
793 | |
|
794 | 0 | if (td->td_stripoffset_p[strip] == 0 || tif->tif_curoff == 0) |
795 | 0 | { |
796 | 0 | assert(td->td_nstrips > 0); |
797 | |
|
798 | 0 | if (td->td_stripbytecount_p[strip] != 0 && |
799 | 0 | td->td_stripoffset_p[strip] != 0 && |
800 | 0 | td->td_stripbytecount_p[strip] >= (uint64_t)cc) |
801 | 0 | { |
802 | | /* |
803 | | * There is already tile data on disk, and the new tile |
804 | | * data we have will fit in the same space. The only |
805 | | * aspect of this that is risky is that there could be |
806 | | * more data to append to this strip before we are done |
807 | | * depending on how we are getting called. |
808 | | */ |
809 | 0 | if (!SeekOK(tif, td->td_stripoffset_p[strip])) |
810 | 0 | { |
811 | 0 | TIFFErrorExtR(tif, module, "Seek error at scanline %lu", |
812 | 0 | (unsigned long)tif->tif_row); |
813 | 0 | return (0); |
814 | 0 | } |
815 | | |
816 | 0 | tif->tif_lastvalidoff = |
817 | 0 | td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip]; |
818 | 0 | } |
819 | 0 | else |
820 | 0 | { |
821 | | /* |
822 | | * Seek to end of file, and set that as our location to |
823 | | * write this strip. |
824 | | */ |
825 | 0 | td->td_stripoffset_p[strip] = TIFFSeekFile(tif, 0, SEEK_END); |
826 | 0 | tif->tif_flags |= TIFF_DIRTYSTRIP; |
827 | 0 | } |
828 | | |
829 | 0 | tif->tif_curoff = td->td_stripoffset_p[strip]; |
830 | | |
831 | | /* |
832 | | * We are starting a fresh strip/tile, so set the size to zero. |
833 | | */ |
834 | 0 | old_byte_count = (int64_t)td->td_stripbytecount_p[strip]; |
835 | 0 | td->td_stripbytecount_p[strip] = 0; |
836 | 0 | } |
837 | | |
838 | 0 | m = tif->tif_curoff + (uint64_t)cc; |
839 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
840 | 0 | m = (uint32_t)m; |
841 | 0 | if ((m < tif->tif_curoff) || (m < (uint64_t)cc)) |
842 | 0 | { |
843 | 0 | TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded"); |
844 | 0 | return (0); |
845 | 0 | } |
846 | | |
847 | 0 | if (tif->tif_lastvalidoff != 0 && m > tif->tif_lastvalidoff && |
848 | 0 | td->td_stripbytecount_p[strip] > 0) |
849 | 0 | { |
850 | | /* Ouch: we have detected that we are rewriting in place a strip/tile */ |
851 | | /* with several calls to TIFFAppendToStrip(). The first call was with */ |
852 | | /* a size smaller than the previous size of the strip/tile, so we */ |
853 | | /* opted to rewrite in place, but a following call causes us to go */ |
854 | | /* outsize of the strip/tile area, so we have to finally go for a */ |
855 | | /* append-at-end-of-file strategy, and start by moving what we already |
856 | | */ |
857 | | /* wrote. */ |
858 | 0 | tmsize_t tempSize; |
859 | 0 | void *temp; |
860 | 0 | uint64_t offsetRead; |
861 | 0 | uint64_t offsetWrite; |
862 | 0 | uint64_t toCopy = td->td_stripbytecount_p[strip]; |
863 | |
|
864 | 0 | if (toCopy < 1024 * 1024) |
865 | 0 | tempSize = (tmsize_t)toCopy; |
866 | 0 | else |
867 | 0 | tempSize = 1024 * 1024; |
868 | |
|
869 | 0 | offsetRead = td->td_stripoffset_p[strip]; |
870 | 0 | offsetWrite = TIFFSeekFile(tif, 0, SEEK_END); |
871 | |
|
872 | 0 | m = offsetWrite + (uint64_t)toCopy + (uint64_t)cc; |
873 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF) && m != (uint32_t)m) |
874 | 0 | { |
875 | 0 | TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded"); |
876 | 0 | return (0); |
877 | 0 | } |
878 | | |
879 | 0 | temp = _TIFFmallocExt(tif, tempSize); |
880 | 0 | if (temp == NULL) |
881 | 0 | { |
882 | 0 | TIFFErrorExtR(tif, module, "No space for output buffer"); |
883 | 0 | return (0); |
884 | 0 | } |
885 | | |
886 | 0 | tif->tif_flags |= TIFF_DIRTYSTRIP; |
887 | |
|
888 | 0 | td->td_stripoffset_p[strip] = offsetWrite; |
889 | 0 | td->td_stripbytecount_p[strip] = 0; |
890 | | |
891 | | /* Move data written by previous calls to us at end of file */ |
892 | 0 | while (toCopy > 0) |
893 | 0 | { |
894 | 0 | if (!SeekOK(tif, offsetRead)) |
895 | 0 | { |
896 | 0 | TIFFErrorExtR(tif, module, "Seek error"); |
897 | 0 | _TIFFfreeExt(tif, temp); |
898 | 0 | return (0); |
899 | 0 | } |
900 | 0 | if (!ReadOK(tif, temp, tempSize)) |
901 | 0 | { |
902 | 0 | TIFFErrorExtR(tif, module, "Cannot read"); |
903 | 0 | _TIFFfreeExt(tif, temp); |
904 | 0 | return (0); |
905 | 0 | } |
906 | 0 | if (!SeekOK(tif, offsetWrite)) |
907 | 0 | { |
908 | 0 | TIFFErrorExtR(tif, module, "Seek error"); |
909 | 0 | _TIFFfreeExt(tif, temp); |
910 | 0 | return (0); |
911 | 0 | } |
912 | 0 | if (!WriteOK(tif, temp, tempSize)) |
913 | 0 | { |
914 | 0 | TIFFErrorExtR(tif, module, "Cannot write"); |
915 | 0 | _TIFFfreeExt(tif, temp); |
916 | 0 | return (0); |
917 | 0 | } |
918 | 0 | offsetRead += (uint64_t)tempSize; |
919 | 0 | offsetWrite += (uint64_t)tempSize; |
920 | 0 | td->td_stripbytecount_p[strip] += (uint64_t)tempSize; |
921 | 0 | toCopy -= (uint64_t)tempSize; |
922 | 0 | } |
923 | 0 | _TIFFfreeExt(tif, temp); |
924 | | |
925 | | /* Append the data of this call */ |
926 | 0 | offsetWrite += (uint64_t)cc; |
927 | 0 | m = offsetWrite; |
928 | 0 | } |
929 | | |
930 | 0 | if (!WriteOK(tif, data, cc)) |
931 | 0 | { |
932 | 0 | TIFFErrorExtR(tif, module, "Write error at scanline %lu", |
933 | 0 | (unsigned long)tif->tif_row); |
934 | 0 | return (0); |
935 | 0 | } |
936 | 0 | tif->tif_curoff = m; |
937 | 0 | td->td_stripbytecount_p[strip] += (uint64_t)cc; |
938 | |
|
939 | 0 | if ((int64_t)td->td_stripbytecount_p[strip] != old_byte_count) |
940 | 0 | tif->tif_flags |= TIFF_DIRTYSTRIP; |
941 | |
|
942 | 0 | return (1); |
943 | 0 | } |
944 | | |
945 | | /* |
946 | | * Internal version of TIFFFlushData that can be |
947 | | * called by ``encodestrip routines'' w/o concern |
948 | | * for infinite recursion. |
949 | | */ |
950 | | int TIFFFlushData1(TIFF *tif) |
951 | 0 | { |
952 | 0 | if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE) |
953 | 0 | { |
954 | 0 | if (!isFillOrder(tif, tif->tif_dir.td_fillorder) && |
955 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
956 | 0 | TIFFReverseBits((uint8_t *)tif->tif_rawdata, tif->tif_rawcc); |
957 | 0 | if (!TIFFAppendToStrip( |
958 | 0 | tif, isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip, |
959 | 0 | tif->tif_rawdata, tif->tif_rawcc)) |
960 | 0 | { |
961 | | /* We update those variables even in case of error since there's */ |
962 | | /* code that doesn't really check the return code of this */ |
963 | | /* function */ |
964 | 0 | tif->tif_rawcc = 0; |
965 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
966 | 0 | return (0); |
967 | 0 | } |
968 | 0 | tif->tif_rawcc = 0; |
969 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
970 | 0 | } |
971 | 0 | return (1); |
972 | 0 | } |
973 | | |
974 | | /* |
975 | | * Set the current write offset. This should only be |
976 | | * used to set the offset to a known previous location |
977 | | * (very carefully), or to 0 so that the next write gets |
978 | | * appended to the end of the file. |
979 | | */ |
980 | | void TIFFSetWriteOffset(TIFF *tif, toff_t off) |
981 | 0 | { |
982 | 0 | tif->tif_curoff = off; |
983 | 0 | tif->tif_lastvalidoff = 0; |
984 | 0 | } |