| OLD | NEW |
| 1 /* $Id: tif_write.c,v 1.37 2012-08-13 22:10:17 fwarmerdam Exp $ */ | 1 /* $Id: tif_write.c,v 1.42 2015-06-07 23:00:23 bfriesen Exp $ */ |
| 2 | 2 |
| 3 /* | 3 /* |
| 4 * Copyright (c) 1988-1997 Sam Leffler | 4 * Copyright (c) 1988-1997 Sam Leffler |
| 5 * Copyright (c) 1991-1997 Silicon Graphics, Inc. | 5 * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 6 * | 6 * |
| 7 * Permission to use, copy, modify, distribute, and sell this software and | 7 * Permission to use, copy, modify, distribute, and sell this software and |
| 8 * its documentation for any purpose is hereby granted without fee, provided | 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 | 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 | 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 | 11 * Sam Leffler and Silicon Graphics may not be used in any advertising or |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 return (-1); | 108 return (-1); |
| 109 tif->tif_curstrip = strip; | 109 tif->tif_curstrip = strip; |
| 110 /* | 110 /* |
| 111 * Watch out for a growing image. The value of strips/image | 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 | 112 * will initially be 1 (since it can't be deduced until the |
| 113 * imagelength is known). | 113 * imagelength is known). |
| 114 */ | 114 */ |
| 115 if (strip >= td->td_stripsperimage && imagegrew) | 115 if (strip >= td->td_stripsperimage && imagegrew) |
| 116 td->td_stripsperimage = | 116 td->td_stripsperimage = |
| 117 TIFFhowmany_32(td->td_imagelength,td->td_rowsperstri
p); | 117 TIFFhowmany_32(td->td_imagelength,td->td_rowsperstri
p); |
| 118 if (td->td_stripsperimage == 0) { |
| 119 TIFFErrorExt(tif->tif_clientdata, module, "Zero strips p
er image"); |
| 120 return (-1); |
| 121 } |
| 118 tif->tif_row = | 122 tif->tif_row = |
| 119 (strip % td->td_stripsperimage) * td->td_rowsperstrip; | 123 (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
| 120 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { | 124 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { |
| 121 if (!(*tif->tif_setupencode)(tif)) | 125 if (!(*tif->tif_setupencode)(tif)) |
| 122 return (-1); | 126 return (-1); |
| 123 tif->tif_flags |= TIFF_CODERSETUP; | 127 tif->tif_flags |= TIFF_CODERSETUP; |
| 124 } | 128 } |
| 125 | 129 |
| 126 tif->tif_rawcc = 0; | 130 tif->tif_rawcc = 0; |
| 127 tif->tif_rawcp = tif->tif_rawdata; | 131 tif->tif_rawcp = tif->tif_rawdata; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 * Handle delayed allocation of data buffer. This | 217 * Handle delayed allocation of data buffer. This |
| 214 * permits it to be sized according to the directory | 218 * permits it to be sized according to the directory |
| 215 * info. | 219 * info. |
| 216 */ | 220 */ |
| 217 if (!BUFFERCHECK(tif)) | 221 if (!BUFFERCHECK(tif)) |
| 218 return ((tmsize_t) -1); | 222 return ((tmsize_t) -1); |
| 219 | 223 |
| 220 tif->tif_flags |= TIFF_BUF4WRITE; | 224 tif->tif_flags |= TIFF_BUF4WRITE; |
| 221 tif->tif_curstrip = strip; | 225 tif->tif_curstrip = strip; |
| 222 | 226 |
| 227 if (td->td_stripsperimage == 0) { |
| 228 TIFFErrorExt(tif->tif_clientdata, module, "Zero strips per image
"); |
| 229 return ((tmsize_t) -1); |
| 230 } |
| 231 |
| 223 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; | 232 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
| 224 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { | 233 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { |
| 225 if (!(*tif->tif_setupencode)(tif)) | 234 if (!(*tif->tif_setupencode)(tif)) |
| 226 return ((tmsize_t) -1); | 235 return ((tmsize_t) -1); |
| 227 tif->tif_flags |= TIFF_CODERSETUP; | 236 tif->tif_flags |= TIFF_CODERSETUP; |
| 228 } | 237 } |
| 229 | 238 |
| 230 if( td->td_stripbytecount[strip] > 0 ) | 239 if( td->td_stripbytecount[strip] > 0 ) |
| 231 { | 240 { |
| 232 /* Make sure that at the first attempt of rewriting the tile, we wil
l have */ | 241 /* Make sure that at the first attempt of rewriting the tile, we wil
l have */ |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 * strips/image will initially be 1 (since it | 313 * strips/image will initially be 1 (since it |
| 305 * can't be deduced until the imagelength is known). | 314 * can't be deduced until the imagelength is known). |
| 306 */ | 315 */ |
| 307 if (strip >= td->td_stripsperimage) | 316 if (strip >= td->td_stripsperimage) |
| 308 td->td_stripsperimage = | 317 td->td_stripsperimage = |
| 309 TIFFhowmany_32(td->td_imagelength,td->td_rowsperstri
p); | 318 TIFFhowmany_32(td->td_imagelength,td->td_rowsperstri
p); |
| 310 if (!TIFFGrowStrips(tif, 1, module)) | 319 if (!TIFFGrowStrips(tif, 1, module)) |
| 311 return ((tmsize_t) -1); | 320 return ((tmsize_t) -1); |
| 312 } | 321 } |
| 313 tif->tif_curstrip = strip; | 322 tif->tif_curstrip = strip; |
| 323 if (td->td_stripsperimage == 0) { |
| 324 TIFFErrorExt(tif->tif_clientdata, module,"Zero strips per image"
); |
| 325 return ((tmsize_t) -1); |
| 326 } |
| 314 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; | 327 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
| 315 return (TIFFAppendToStrip(tif, strip, (uint8*) data, cc) ? | 328 return (TIFFAppendToStrip(tif, strip, (uint8*) data, cc) ? |
| 316 cc : (tmsize_t) -1); | 329 cc : (tmsize_t) -1); |
| 317 } | 330 } |
| 318 | 331 |
| 319 /* | 332 /* |
| 320 * Write and compress a tile of data. The | 333 * Write and compress a tile of data. The |
| 321 * tile is selected by the (x,y,z,s) coordinates. | 334 * tile is selected by the (x,y,z,s) coordinates. |
| 322 */ | 335 */ |
| 323 tmsize_t | 336 tmsize_t |
| (...skipping 22 matching lines...) Expand all Loading... |
| 346 * NB: Image length must be setup before writing; this | 359 * NB: Image length must be setup before writing; this |
| 347 * interface does not support automatically growing | 360 * interface does not support automatically growing |
| 348 * the image on each write (as TIFFWriteScanline does). | 361 * the image on each write (as TIFFWriteScanline does). |
| 349 */ | 362 */ |
| 350 tmsize_t | 363 tmsize_t |
| 351 TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc) | 364 TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc) |
| 352 { | 365 { |
| 353 static const char module[] = "TIFFWriteEncodedTile"; | 366 static const char module[] = "TIFFWriteEncodedTile"; |
| 354 TIFFDirectory *td; | 367 TIFFDirectory *td; |
| 355 uint16 sample; | 368 uint16 sample; |
| 369 uint32 howmany32; |
| 356 | 370 |
| 357 if (!WRITECHECKTILES(tif, module)) | 371 if (!WRITECHECKTILES(tif, module)) |
| 358 return ((tmsize_t)(-1)); | 372 return ((tmsize_t)(-1)); |
| 359 td = &tif->tif_dir; | 373 td = &tif->tif_dir; |
| 360 if (tile >= td->td_nstrips) { | 374 if (tile >= td->td_nstrips) { |
| 361 TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range
, max %lu", | 375 TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range
, max %lu", |
| 362 (unsigned long) tile, (unsigned long) td->td_nstrips); | 376 (unsigned long) tile, (unsigned long) td->td_nstrips); |
| 363 return ((tmsize_t)(-1)); | 377 return ((tmsize_t)(-1)); |
| 364 } | 378 } |
| 365 /* | 379 /* |
| 366 * Handle delayed allocation of data buffer. This | 380 * Handle delayed allocation of data buffer. This |
| 367 * permits it to be sized more intelligently (using | 381 * permits it to be sized more intelligently (using |
| 368 * directory information). | 382 * directory information). |
| 369 */ | 383 */ |
| 370 if (!BUFFERCHECK(tif)) | 384 if (!BUFFERCHECK(tif)) |
| 371 return ((tmsize_t)(-1)); | 385 return ((tmsize_t)(-1)); |
| 372 | 386 |
| 373 tif->tif_flags |= TIFF_BUF4WRITE; | 387 tif->tif_flags |= TIFF_BUF4WRITE; |
| 374 tif->tif_curtile = tile; | 388 tif->tif_curtile = tile; |
| 375 | 389 |
| 376 if( td->td_stripbytecount[tile] > 0 ) | 390 if( td->td_stripbytecount[tile] > 0 ) |
| 377 { | 391 { |
| 378 /* Make sure that at the first attempt of rewriting the tile, we wil
l have */ | 392 /* Make sure that at the first attempt of rewriting the tile, we wil
l have */ |
| 379 /* more bytes available in the output buffer than the previous byte
count, */ | 393 /* more bytes available in the output buffer than the previous byte
count, */ |
| 380 /* so that TIFFAppendToStrip() will detect the overflow when it is c
alled the first */ | 394 /* so that TIFFAppendToStrip() will detect the overflow when it is c
alled the first */ |
| 381 /* time if the new compressed tile is bigger than the older one. (GD
AL #4771) */ | 395 /* time if the new compressed tile is bigger than the older one. (GD
AL #4771) */ |
| 382 if( tif->tif_rawdatasize <= (tmsize_t)td->td_stripbytecount[tile] ) | 396 if( tif->tif_rawdatasize <= (tmsize_t) td->td_stripbytecount[tile] ) |
| 383 { | 397 { |
| 384 if( !(TIFFWriteBufferSetup(tif, NULL, | 398 if( !(TIFFWriteBufferSetup(tif, NULL, |
| 385 (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[tile
] + 1), 1024))) ) | 399 (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[tile
] + 1), 1024))) ) |
| 386 return ((tmsize_t)(-1)); | 400 return ((tmsize_t)(-1)); |
| 387 } | 401 } |
| 388 | 402 |
| 389 /* Force TIFFAppendToStrip() to consider placing data at end | 403 /* Force TIFFAppendToStrip() to consider placing data at end |
| 390 of file. */ | 404 of file. */ |
| 391 tif->tif_curoff = 0; | 405 tif->tif_curoff = 0; |
| 392 } | 406 } |
| 393 | 407 |
| 394 tif->tif_rawcc = 0; | 408 » tif->tif_rawcc = 0; |
| 395 tif->tif_rawcp = tif->tif_rawdata; | 409 » tif->tif_rawcp = tif->tif_rawdata; |
| 396 | 410 |
| 397 /* | 411 /* |
| 398 * Compute tiles per row & per column to compute | 412 * Compute tiles per row & per column to compute |
| 399 * current row and column | 413 * current row and column |
| 400 */ | 414 */ |
| 401 » tif->tif_row = (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelen
gth)) | 415 howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength); |
| 402 » » * td->td_tilelength; | 416 if (howmany32 == 0) { |
| 403 » tif->tif_col = (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidt
h)) | 417 TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles"); |
| 404 » » * td->td_tilewidth; | 418 return ((tmsize_t)(-1)); |
| 419 } |
| 420 » tif->tif_row = (tile % howmany32) * td->td_tilelength; |
| 421 howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth); |
| 422 if (howmany32 == 0) { |
| 423 TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles"); |
| 424 return ((tmsize_t)(-1)); |
| 425 } |
| 426 » tif->tif_col = (tile % howmany32) * td->td_tilewidth; |
| 405 | 427 |
| 406 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { | 428 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { |
| 407 if (!(*tif->tif_setupencode)(tif)) | 429 if (!(*tif->tif_setupencode)(tif)) |
| 408 return ((tmsize_t)(-1)); | 430 return ((tmsize_t)(-1)); |
| 409 tif->tif_flags |= TIFF_CODERSETUP; | 431 tif->tif_flags |= TIFF_CODERSETUP; |
| 410 } | 432 } |
| 411 tif->tif_flags &= ~TIFF_POSTENCODE; | 433 tif->tif_flags &= ~TIFF_POSTENCODE; |
| 412 sample = (uint16)(tile/td->td_stripsperimage); | 434 sample = (uint16)(tile/td->td_stripsperimage); |
| 413 if (!(*tif->tif_preencode)(tif, sample)) | 435 if (!(*tif->tif_preencode)(tif, sample)) |
| 414 return ((tmsize_t)(-1)); | 436 return ((tmsize_t)(-1)); |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 } | 784 } |
| 763 | 785 |
| 764 /* vim: set ts=8 sts=8 sw=8 noet: */ | 786 /* vim: set ts=8 sts=8 sw=8 noet: */ |
| 765 /* | 787 /* |
| 766 * Local Variables: | 788 * Local Variables: |
| 767 * mode: c | 789 * mode: c |
| 768 * c-basic-offset: 8 | 790 * c-basic-offset: 8 |
| 769 * fill-column: 78 | 791 * fill-column: 78 |
| 770 * End: | 792 * End: |
| 771 */ | 793 */ |
| 772 | |
| OLD | NEW |