1 /* $Id: tif_dirwrite.c,v 1.42 2007-04-04 04:16:07 joris 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 * Directory Write Support Routines. 31 */ 32 #include "tiffiop.h" 33 34 #ifdef NDEF 35 36 #ifdef HAVE_IEEEFP 37 # define TIFFCvtNativeToIEEEFloat(tif, n, fp) 38 # define TIFFCvtNativeToIEEEDouble(tif, n, dp) 39 #else 40 extern void TIFFCvtNativeToIEEEFloat(TIFF*, uint32, float*); 41 extern void TIFFCvtNativeToIEEEDouble(TIFF*, uint32, double*); 42 #endif 43 44 static int TIFFWriteNormalTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*); 45 static void TIFFSetupShortLong(TIFF*, ttag_t, TIFFDirEntry*, uint32); 46 static void TIFFSetupShort(TIFF*, ttag_t, TIFFDirEntry*, uint16); 47 static int TIFFSetupShortPair(TIFF*, ttag_t, TIFFDirEntry*); 48 static int TIFFWritePerSampleShorts(TIFF*, ttag_t, TIFFDirEntry*); 49 static int TIFFWritePerSampleAnys(TIFF*, TIFFDataType, ttag_t, TIFFDirEntry*); 50 static int TIFFWriteShortTable(TIFF*, ttag_t, TIFFDirEntry*, uint32, uint16**); 51 static int TIFFWriteShortArray(TIFF*, TIFFDirEntry*, uint16*); 52 static int TIFFWriteLongArray(TIFF *, TIFFDirEntry*, uint32*); 53 static int TIFFWriteRationalArray(TIFF *, TIFFDirEntry*, float*); 54 static int TIFFWriteFloatArray(TIFF *, TIFFDirEntry*, float*); 55 static int TIFFWriteDoubleArray(TIFF *, TIFFDirEntry*, double*); 56 static int TIFFWriteByteArray(TIFF*, TIFFDirEntry*, char*); 57 static int TIFFWriteAnyArray(TIFF*, 58 TIFFDataType, ttag_t, TIFFDirEntry*, uint32, double*); 59 static int TIFFWriteTransferFunction(TIFF*, TIFFDirEntry*); 60 static int TIFFWriteInkNames(TIFF*, TIFFDirEntry*); 61 static int TIFFWriteData(TIFF*, TIFFDirEntry*, char*); 62 static int TIFFLinkDirectory(TIFF*); 63 64 #define WriteRationalPair(type, tag1, v1, tag2, v2) { \ 65 TIFFWriteRational((tif), (type), (tag1), (dir), (v1)) \ 66 TIFFWriteRational((tif), (type), (tag2), (dir)+1, (v2)) \ 67 (dir)++; \ 68 } 69 #define TIFFWriteRational(tif, type, tag, dir, v) \ 70 (dir)->tdir_tag = (tag); \ 71 (dir)->tdir_type = (type); \ 72 (dir)->tdir_count = 1; \ 73 if (!TIFFWriteRationalArray((tif), (dir), &(v))) \ 74 goto bad; 75 76 #endif 77 78 /* 79 * Write the contents of the current directory 80 * to the specified file. This routine doesn't 81 * handle overwriting a directory with auxiliary 82 * storage that's been changed. 83 */ 84 static int 85 _TIFFWriteDirectory(TIFF* tif, int done) 86 { 87 assert(0); 88 #ifdef NDEF 89 uint16 dircount; 90 toff_t diroff; 91 ttag_t tag; 92 uint32 nfields; 93 tsize_t dirsize; 94 char* data; 95 TIFFDirEntry* dir; 96 TIFFDirectory* td; 97 unsigned long b, fields[FIELD_SETLONGS]; 98 int fi, nfi; 99 100 if (tif->tif_mode == O_RDONLY) 101 return (1); 102 /* 103 * Clear write state so that subsequent images with 104 * different characteristics get the right buffers 105 * setup for them. 106 */ 107 if (done) 108 { 109 if (tif->tif_flags & TIFF_POSTENCODE) { 110 tif->tif_flags &= ~TIFF_POSTENCODE; 111 if (!(*tif->tif_postencode)(tif)) { 112 TIFFErrorExt(tif->tif_clientdata, 113 tif->tif_name, 114 "Error post-encoding before directory write"); 115 return (0); 116 } 117 } 118 (*tif->tif_close)(tif); /* shutdown encoder */ 119 /* 120 * Flush any data that might have been written 121 * by the compression close+cleanup routines. 122 */ 123 if (tif->tif_rawcc > 0 124 && (tif->tif_flags & TIFF_BEENWRITING) != 0 125 && !TIFFFlushData1(tif)) { 126 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 127 "Error flushing data before directory write"); 128 return (0); 129 } 130 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) { 131 _TIFFfree(tif->tif_rawdata); 132 tif->tif_rawdata = NULL; 133 tif->tif_rawcc = 0; 134 tif->tif_rawdatasize = 0; 135 } 136 tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP); 137 } 138 139 td = &tif->tif_dir; 140 /* 141 * Size the directory so that we can calculate 142 * offsets for the data items that aren't kept 143 * in-place in each field. 144 */ 145 nfields = 0; 146 for (b = 0; b <= FIELD_LAST; b++) 147 if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM) 148 nfields += (b < FIELD_SUBFILETYPE ? 2 : 1); 149 nfields += td->td_customValueCount; 150 dirsize = nfields * sizeof (TIFFDirEntry); 151 data = (char*) _TIFFmalloc(dirsize); 152 if (data == NULL) { 153 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 154 "Cannot write directory, out of space"); 155 return (0); 156 } 157 /* 158 * Directory hasn't been placed yet, put 159 * it at the end of the file and link it 160 * into the existing directory structure. 161 */ 162 if (tif->tif_diroff == 0 && !TIFFLinkDirectory(tif)) 163 goto bad; 164 tif->tif_dataoff = (toff_t)( 165 tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t)); 166 if (tif->tif_dataoff & 1) 167 tif->tif_dataoff++; 168 (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET); 169 tif->tif_curdir++; 170 dir = (TIFFDirEntry*) data; 171 /* 172 * Setup external form of directory 173 * entries and write data items. 174 */ 175 _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields)); 176 /* 177 * Write out ExtraSamples tag only if 178 * extra samples are present in the data. 179 */ 180 if (FieldSet(fields, FIELD_EXTRASAMPLES) && !td->td_extrasamples) { 181 ResetFieldBit(fields, FIELD_EXTRASAMPLES); 182 nfields--; 183 dirsize -= sizeof (TIFFDirEntry); 184 } /*XXX*/ 185 for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) { ddd 186 const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi]; 187 188 /* 189 * For custom fields, we test to see if the custom field 190 * is set or not. For normal fields, we just use the 191 * FieldSet test. 192 */ 193 if( fip->field_bit == FIELD_CUSTOM ) 194 { 195 int ci, is_set = FALSE; 196 197 for( ci = 0; ci < td->td_customValueCount; ci++ ) 198 is_set |= (td->td_customValues[ci].info == fip); 199 200 if( !is_set ) 201 continue; 202 } 203 else if (!FieldSet(fields, fip->field_bit)) 204 continue; 205 206 /* 207 * Handle other fields. 208 */ 209 switch (fip->field_bit) 210 { 211 case FIELD_STRIPOFFSETS: 212 /* 213 * We use one field bit for both strip and tile 214 215 * offsets, and so must be careful in selecting 216 * the appropriate field descriptor (so that tags 217 * are written in sorted order). 218 */ 219 tag = isTiled(tif) ? 220 TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS; 221 if (tag != fip->field_tag) 222 continue; 223 224 dir->tdir_tag = (uint16) tag; 225 dir->tdir_type = (uint16) TIFF_LONG; 226 dir->tdir_count = (uint32) td->td_nstrips; ddd 227 if (!TIFFWriteLongArray(tif, dir, td->td_stripoffset)) ddd 228 goto bad; 229 break; 230 case FIELD_STRIPBYTECOUNTS: 231 /* 232 * We use one field bit for both strip and tile 233 * byte counts, and so must be careful in selecting 234 * the appropriate field descriptor (so that tags 235 * are written in sorted order). 236 */ 237 tag = isTiled(tif) ? 238 TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS; 239 if (tag != fip->field_tag) 240 continue; 241 242 dir->tdir_tag = (uint16) tag; 243 dir->tdir_type = (uint16) TIFF_LONG; 244 dir->tdir_count = (uint32) td->td_nstrips; ddd 245 if (!TIFFWriteLongArray(tif, dir, td->td_stripbytecount)) 246 goto bad; 247 break; 248 case FIELD_ROWSPERSTRIP: 249 TIFFSetupShortLong(tif, TIFFTAG_ROWSPERSTRIP, 250 dir, td->td_rowsperstrip); 251 break; 252 case FIELD_COLORMAP: 253 if (!TIFFWriteShortTable(tif, TIFFTAG_COLORMAP, dir, 254 3, td->td_colormap)) 255 goto bad; 256 break; 257 case FIELD_IMAGEDIMENSIONS: 258 TIFFSetupShortLong(tif, TIFFTAG_IMAGEWIDTH, 259 dir++, td->td_imagewidth); 260 TIFFSetupShortLong(tif, TIFFTAG_IMAGELENGTH, 261 dir, td->td_imagelength); 262 break; 263 case FIELD_TILEDIMENSIONS: 264 TIFFSetupShortLong(tif, TIFFTAG_TILEWIDTH, 265 dir++, td->td_tilewidth); 266 TIFFSetupShortLong(tif, TIFFTAG_TILELENGTH, 267 dir, td->td_tilelength); 268 break; 269 case FIELD_COMPRESSION: 270 TIFFSetupShort(tif, TIFFTAG_COMPRESSION, 271 dir, td->td_compression); 272 break; 273 case FIELD_PHOTOMETRIC: 274 TIFFSetupShort(tif, TIFFTAG_PHOTOMETRIC, 275 dir, td->td_photometric); 276 break; 277 case FIELD_POSITION: 278 WriteRationalPair(TIFF_RATIONAL, 279 TIFFTAG_XPOSITION, td->td_xposition, 280 TIFFTAG_YPOSITION, td->td_yposition); 281 break; 282 case FIELD_RESOLUTION: 283 WriteRationalPair(TIFF_RATIONAL, 284 TIFFTAG_XRESOLUTION, td->td_xresolution, 285 TIFFTAG_YRESOLUTION, td->td_yresolution); 286 break; 287 case FIELD_BITSPERSAMPLE: 288 case FIELD_MINSAMPLEVALUE: 289 case FIELD_MAXSAMPLEVALUE: 290 case FIELD_SAMPLEFORMAT: 291 if (!TIFFWritePerSampleShorts(tif, fip->field_tag, dir)) 292 goto bad; 293 break; 294 case FIELD_SMINSAMPLEVALUE: 295 case FIELD_SMAXSAMPLEVALUE: 296 if (!TIFFWritePerSampleAnys(tif, 297 _TIFFSampleToTagType(tif), fip->field_tag, dir)) 298 goto bad; 299 break; 300 case FIELD_PAGENUMBER: 301 case FIELD_HALFTONEHINTS: 302 case FIELD_YCBCRSUBSAMPLING: 303 if (!TIFFSetupShortPair(tif, fip->field_tag, dir)) 304 goto bad; 305 break; 306 case FIELD_INKNAMES: 307 if (!TIFFWriteInkNames(tif, dir)) 308 goto bad; 309 break; 310 case FIELD_TRANSFERFUNCTION: 311 if (!TIFFWriteTransferFunction(tif, dir)) 312 goto bad; 313 break; 314 case FIELD_SUBIFD: 315 /* 316 * XXX: Always write this field using LONG type 317 * for backward compatibility. 318 */ 319 dir->tdir_tag = (uint16) fip->field_tag; 320 dir->tdir_type = (uint16) TIFF_LONG; 321 dir->tdir_count = (uint32) td->td_nsubifd; 322 if (!TIFFWriteLongArray(tif, dir, td->td_subifd)) 323 goto bad; 324 /* 325 * Total hack: if this directory includes a SubIFD 326 * tag then force the next <n> directories to be 327 * written as ``sub directories'' of this one. This 328 * is used to write things like thumbnails and 329 * image masks that one wants to keep out of the 330 * normal directory linkage access mechanism. 331 */ 332 if (dir->tdir_count > 0) { 333 tif->tif_flags |= TIFF_INSUBIFD; 334 tif->tif_nsubifd = (uint16) dir->tdir_count; 335 if (dir->tdir_count > 1) 336 tif->tif_subifdoff = dir->tdir_offset; 337 else 338 tif->tif_subifdoff = (uint32)( 339 tif->tif_diroff 340 + sizeof (uint16) 341 + ((char*)&dir->tdir_offset-data)); 342 } 343 break; 344 default: 345 /* XXX: Should be fixed and removed. */ 346 if (fip->field_tag == TIFFTAG_DOTRANGE) { 347 if (!TIFFSetupShortPair(tif, fip->field_tag, dir)) 348 goto bad; 349 } 350 else if (!TIFFWriteNormalTag(tif, dir, fip)) 351 goto bad; 352 break; 353 } 354 dir++; 355 356 if( fip->field_bit != FIELD_CUSTOM ) 357 ResetFieldBit(fields, fip->field_bit); 358 } 359 360 /* 361 * Write directory. 362 */ 363 dircount = (uint16) nfields; 364 diroff = (uint32) tif->tif_nextdiroff; 365 if (tif->tif_flags & TIFF_SWAB) { 366 /* 367 * The file's byte order is opposite to the 368 * native machine architecture. We overwrite 369 * the directory information with impunity 370 * because it'll be released below after we 371 * write it to the file. Note that all the 372 * other tag construction routines assume that 373 * we do this byte-swapping; i.e. they only 374 * byte-swap indirect data. 375 */ 376 for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) { 377 TIFFSwabArrayOfShort(&dir->tdir_tag, 2); ddd 378 TIFFSwabArrayOfLong(&dir->tdir_count, 2); ddd 379 } 380 dircount = (uint16) nfields; 381 TIFFSwabShort(&dircount); 382 TIFFSwabLong(&diroff); 383 } 384 (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET); 385 if (!WriteOK(tif, &dircount, sizeof (dircount))) { 386 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 387 "Error writing directory count"); 388 goto bad; 389 } 390 if (!WriteOK(tif, data, dirsize)) { 391 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 392 "Error writing directory contents"); 393 goto bad; 394 } 395 if (!WriteOK(tif, &diroff, sizeof (uint32))) { 396 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 397 "Error writing directory link"); 398 goto bad; 399 } 400 if (done) { 401 TIFFFreeDirectory(tif); 402 tif->tif_flags &= ~TIFF_DIRTYDIRECT; 403 (*tif->tif_cleanup)(tif); 404 405 /* 406 * Reset directory-related state for subsequent 407 * directories. 408 */ 409 TIFFCreateDirectory(tif); 410 } 411 _TIFFfree(data); 412 return (1); 413 bad: 414 _TIFFfree(data); 415 return (0); 416 #endif 417 } 418 #undef WriteRationalPair 419 420 int 421 TIFFWriteDirectory(TIFF* tif) 422 { 423 return _TIFFWriteDirectory(tif, TRUE); 424 } 425 426 #ifdef NDEF 427 428 /* 429 * Similar to TIFFWriteDirectory(), writes the directory out 430 * but leaves all data structures in memory so that it can be 431 * written again. This will make a partially written TIFF file 432 * readable before it is successfully completed/closed. 433 */ 434 int 435 TIFFCheckpointDirectory(TIFF* tif) 436 { 437 int rc; 438 /* Setup the strips arrays, if they haven't already been. */ 439 if (tif->tif_dir.td_stripoffset == NULL) 440 (void) TIFFSetupStrips(tif); 441 rc = _TIFFWriteDirectory(tif, FALSE); 442 (void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END)); 443 return rc; 444 } 445 446 static int 447 _TIFFWriteCustomDirectory(TIFF* tif, toff_t *pdiroff) 448 { 449 uint16 dircount; 450 uint32 nfields; 451 tsize_t dirsize; 452 char* data; 453 TIFFDirEntry* dir; 454 TIFFDirectory* td; 455 unsigned long b, fields[FIELD_SETLONGS]; 456 int fi, nfi; 457 458 if (tif->tif_mode == O_RDONLY) 459 return (1); 460 461 td = &tif->tif_dir; 462 /* 463 * Size the directory so that we can calculate 464 * offsets for the data items that aren't kept 465 * in-place in each field. 466 */ 467 nfields = 0; 468 for (b = 0; b <= FIELD_LAST; b++) 469 if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM) 470 nfields += (b < FIELD_SUBFILETYPE ? 2 : 1); 471 nfields += td->td_customValueCount; 472 dirsize = nfields * sizeof (TIFFDirEntry); 473 data = (char*) _TIFFmalloc(dirsize); 474 if (data == NULL) { 475 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 476 "Cannot write directory, out of space"); 477 return (0); 478 } 479 /* 480 * Put the directory at the end of the file. 481 */ 482 tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1; 483 tif->tif_dataoff = (toff_t)( 484 tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t)); 485 if (tif->tif_dataoff & 1) 486 tif->tif_dataoff++; 487 (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET); 488 dir = (TIFFDirEntry*) data; 489 /* 490 * Setup external form of directory 491 * entries and write data items. 492 */ 493 _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields)); 494 495 for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) { ddd 496 const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi]; 497 498 /* 499 * For custom fields, we test to see if the custom field 500 * is set or not. For normal fields, we just use the 501 * FieldSet test. 502 */ 503 if( fip->field_bit == FIELD_CUSTOM ) 504 { 505 int ci, is_set = FALSE; 506 507 for( ci = 0; ci < td->td_customValueCount; ci++ ) 508 is_set |= (td->td_customValues[ci].info == fip); 509 510 if( !is_set ) 511 continue; 512 } 513 else if (!FieldSet(fields, fip->field_bit)) 514 continue; 515 516 if( fip->field_bit != FIELD_CUSTOM ) 517 ResetFieldBit(fields, fip->field_bit); 518 } 519 520 /* 521 * Write directory. 522 */ 523 dircount = (uint16) nfields; 524 *pdiroff = (uint32) tif->tif_nextdiroff; 525 if (tif->tif_flags & TIFF_SWAB) { 526 /* 527 * The file's byte order is opposite to the 528 * native machine architecture. We overwrite 529 * the directory information with impunity 530 * because it'll be released below after we 531 * write it to the file. Note that all the 532 * other tag construction routines assume that 533 * we do this byte-swapping; i.e. they only 534 * byte-swap indirect data. 535 */ 536 for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) { 537 TIFFSwabArrayOfShort(&dir->tdir_tag, 2); ddd 538 TIFFSwabArrayOfLong(&dir->tdir_count, 2); ddd 539 } 540 dircount = (uint16) nfields; 541 TIFFSwabShort(&dircount); 542 TIFFSwabLong(pdiroff); 543 } 544 (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET); 545 if (!WriteOK(tif, &dircount, sizeof (dircount))) { 546 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 547 "Error writing directory count"); 548 goto bad; 549 } 550 if (!WriteOK(tif, data, dirsize)) { 551 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 552 "Error writing directory contents"); 553 goto bad; 554 } 555 if (!WriteOK(tif, pdiroff, sizeof (uint32))) { 556 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 557 "Error writing directory link"); 558 goto bad; 559 } 560 _TIFFfree(data); 561 return (1); 562 bad: 563 _TIFFfree(data); 564 return (0); 565 } 566 567 int 568 TIFFWriteCustomDirectory(TIFF* tif, toff_t *pdiroff) 569 { 570 return _TIFFWriteCustomDirectory(tif, pdiroff); 571 } 572 573 /* 574 * Process tags that are not special cased. 575 */ 576 static int 577 TIFFWriteNormalTag(TIFF* tif, TIFFDirEntry* dir, const TIFFFieldInfo* fip) 578 { 579 uint16 wc = (uint16) fip->field_writecount; 580 uint32 wc2; 581 582 dir->tdir_tag = (uint16) fip->field_tag; 583 dir->tdir_type = (uint16) fip->field_type; 584 dir->tdir_count = wc; 585 586 switch (fip->field_type) { 587 case TIFF_SHORT: 588 case TIFF_SSHORT: 589 if (fip->field_passcount) { 590 uint16* wp; 591 if (wc == (uint16) TIFF_VARIABLE2) { 592 TIFFGetField(tif, fip->field_tag, &wc2, &wp); 593 dir->tdir_count = wc2; 594 } else { /* Assume TIFF_VARIABLE */ 595 TIFFGetField(tif, fip->field_tag, &wc, &wp); 596 dir->tdir_count = wc; 597 } 598 if (!TIFFWriteShortArray(tif, dir, wp)) 599 return 0; 600 } else { 601 if (wc == 1) { 602 uint16 sv; 603 TIFFGetField(tif, fip->field_tag, &sv); 604 dir->tdir_offset = 605 TIFFInsertData(tif, dir->tdir_type, sv); 606 } else { 607 uint16* wp; 608 TIFFGetField(tif, fip->field_tag, &wp); 609 if (!TIFFWriteShortArray(tif, dir, wp)) 610 return 0; 611 } 612 } 613 break; 614 case TIFF_LONG: 615 case TIFF_SLONG: 616 case TIFF_IFD: 617 if (fip->field_passcount) { 618 uint32* lp; 619 if (wc == (uint16) TIFF_VARIABLE2) { 620 TIFFGetField(tif, fip->field_tag, &wc2, &lp); 621 dir->tdir_count = wc2; 622 } else { /* Assume TIFF_VARIABLE */ 623 TIFFGetField(tif, fip->field_tag, &wc, &lp); 624 dir->tdir_count = wc; 625 } 626 if (!TIFFWriteLongArray(tif, dir, lp)) 627 return 0; 628 } else { 629 if (wc == 1) { 630 /* XXX handle LONG->SHORT conversion */ 631 TIFFGetField(tif, fip->field_tag, 632 &dir->tdir_offset); 633 } else { 634 uint32* lp; 635 TIFFGetField(tif, fip->field_tag, &lp); 636 if (!TIFFWriteLongArray(tif, dir, lp)) 637 return 0; 638 } 639 } 640 break; 641 case TIFF_RATIONAL: 642 case TIFF_SRATIONAL: 643 if (fip->field_passcount) { 644 float* fp; 645 if (wc == (uint16) TIFF_VARIABLE2) { 646 TIFFGetField(tif, fip->field_tag, &wc2, &fp); 647 dir->tdir_count = wc2; 648 } else { /* Assume TIFF_VARIABLE */ 649 TIFFGetField(tif, fip->field_tag, &wc, &fp); 650 dir->tdir_count = wc; 651 } 652 if (!TIFFWriteRationalArray(tif, dir, fp)) 653 return 0; 654 } else { 655 if (wc == 1) { 656 float fv; 657 TIFFGetField(tif, fip->field_tag, &fv); 658 if (!TIFFWriteRationalArray(tif, dir, &fv)) 659 return 0; 660 } else { 661 float* fp; 662 TIFFGetField(tif, fip->field_tag, &fp); 663 if (!TIFFWriteRationalArray(tif, dir, fp)) 664 return 0; 665 } 666 } 667 break; 668 case TIFF_FLOAT: 669 if (fip->field_passcount) { 670 float* fp; 671 if (wc == (uint16) TIFF_VARIABLE2) { 672 TIFFGetField(tif, fip->field_tag, &wc2, &fp); 673 dir->tdir_count = wc2; 674 } else { /* Assume TIFF_VARIABLE */ 675 TIFFGetField(tif, fip->field_tag, &wc, &fp); 676 dir->tdir_count = wc; 677 } 678 if (!TIFFWriteFloatArray(tif, dir, fp)) 679 return 0; 680 } else { 681 if (wc == 1) { 682 float fv; 683 TIFFGetField(tif, fip->field_tag, &fv); 684 if (!TIFFWriteFloatArray(tif, dir, &fv)) 685 return 0; 686 } else { 687 float* fp; 688 TIFFGetField(tif, fip->field_tag, &fp); 689 if (!TIFFWriteFloatArray(tif, dir, fp)) 690 return 0; 691 } 692 } 693 break; 694 case TIFF_DOUBLE: 695 if (fip->field_passcount) { 696 double* dp; 697 if (wc == (uint16) TIFF_VARIABLE2) { 698 TIFFGetField(tif, fip->field_tag, &wc2, &dp); 699 dir->tdir_count = wc2; 700 } else { /* Assume TIFF_VARIABLE */ 701 TIFFGetField(tif, fip->field_tag, &wc, &dp); 702 dir->tdir_count = wc; 703 } 704 if (!TIFFWriteDoubleArray(tif, dir, dp)) 705 return 0; 706 } else { 707 if (wc == 1) { 708 double dv; 709 TIFFGetField(tif, fip->field_tag, &dv); 710 if (!TIFFWriteDoubleArray(tif, dir, &dv)) 711 return 0; 712 } else { 713 double* dp; 714 TIFFGetField(tif, fip->field_tag, &dp); 715 if (!TIFFWriteDoubleArray(tif, dir, dp)) 716 return 0; 717 } 718 } 719 break; 720 case TIFF_ASCII: 721 { 722 char* cp; 723 if (fip->field_passcount) 724 { 725 if( wc == (uint16) TIFF_VARIABLE2 ) 726 TIFFGetField(tif, fip->field_tag, &wc2, &cp); 727 else 728 TIFFGetField(tif, fip->field_tag, &wc, &cp); 729 } 730 else 731 TIFFGetField(tif, fip->field_tag, &cp); 732 733 dir->tdir_count = (uint32) (strlen(cp) + 1); 734 if (!TIFFWriteByteArray(tif, dir, cp)) 735 return (0); 736 } 737 break; 738 739 case TIFF_BYTE: 740 case TIFF_SBYTE: 741 if (fip->field_passcount) { 742 char* cp; 743 if (wc == (uint16) TIFF_VARIABLE2) { 744 TIFFGetField(tif, fip->field_tag, &wc2, &cp); 745 dir->tdir_count = wc2; 746 } else { /* Assume TIFF_VARIABLE */ 747 TIFFGetField(tif, fip->field_tag, &wc, &cp); 748 dir->tdir_count = wc; 749 } 750 if (!TIFFWriteByteArray(tif, dir, cp)) 751 return 0; 752 } else { 753 if (wc == 1) { 754 char cv; 755 TIFFGetField(tif, fip->field_tag, &cv); 756 if (!TIFFWriteByteArray(tif, dir, &cv)) 757 return 0; 758 } else { 759 char* cp; 760 TIFFGetField(tif, fip->field_tag, &cp); 761 if (!TIFFWriteByteArray(tif, dir, cp)) 762 return 0; 763 } 764 } 765 break; 766 767 case TIFF_UNDEFINED: 768 { char* cp; 769 if (wc == (unsigned short) TIFF_VARIABLE) { 770 TIFFGetField(tif, fip->field_tag, &wc, &cp); 771 dir->tdir_count = wc; 772 } else if (wc == (unsigned short) TIFF_VARIABLE2) { 773 TIFFGetField(tif, fip->field_tag, &wc2, &cp); 774 dir->tdir_count = wc2; 775 } else 776 TIFFGetField(tif, fip->field_tag, &cp); 777 if (!TIFFWriteByteArray(tif, dir, cp)) 778 return (0); 779 } 780 break; 781 782 case TIFF_NOTYPE: 783 break; 784 } 785 return (1); 786 } 787 788 /* 789 * Setup a directory entry with either a SHORT 790 * or LONG type according to the value. 791 */ 792 static void 793 TIFFSetupShortLong(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint32 v) 794 { 795 dir->tdir_tag = (uint16) tag; 796 dir->tdir_count = 1; 797 if (v > 0xffffL) { 798 dir->tdir_type = (short) TIFF_LONG; 799 dir->tdir_offset = v; 800 } else { 801 dir->tdir_type = (short) TIFF_SHORT; 802 dir->tdir_offset = TIFFInsertData(tif, (int) TIFF_SHORT, v); 803 } 804 } 805 806 /* 807 * Setup a SHORT directory entry 808 */ 809 static void 810 TIFFSetupShort(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint16 v) 811 { 812 dir->tdir_tag = (uint16) tag; 813 dir->tdir_count = 1; 814 dir->tdir_type = (short) TIFF_SHORT; 815 dir->tdir_offset = TIFFInsertData(tif, (int) TIFF_SHORT, v); 816 } 817 #undef MakeShortDirent 818 819 #define NITEMS(x) (sizeof (x) / sizeof (x[0])) 820 /* 821 * Setup a directory entry that references a 822 * samples/pixel array of SHORT values and 823 * (potentially) write the associated indirect 824 * values. 825 */ 826 static int 827 TIFFWritePerSampleShorts(TIFF* tif, ttag_t tag, TIFFDirEntry* dir) 828 { 829 uint16 buf[10], v; 830 uint16* w = buf; 831 uint16 i, samples = tif->tif_dir.td_samplesperpixel; 832 int status; 833 834 if (samples > NITEMS(buf)) { 835 w = (uint16*) _TIFFmalloc(samples * sizeof (uint16)); 836 if (w == NULL) { 837 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 838 "No space to write per-sample shorts"); 839 return (0); 840 } 841 } 842 TIFFGetField(tif, tag, &v); 843 for (i = 0; i < samples; i++) 844 w[i] = v; 845 846 dir->tdir_tag = (uint16) tag; 847 dir->tdir_type = (uint16) TIFF_SHORT; 848 dir->tdir_count = samples; 849 status = TIFFWriteShortArray(tif, dir, w); 850 if (w != buf) 851 _TIFFfree((char*) w); 852 return (status); 853 } 854 855 /* 856 * Setup a directory entry that references a samples/pixel array of ``type'' 857 * values and (potentially) write the associated indirect values. The source 858 * data from TIFFGetField() for the specified tag must be returned as double. 859 */ 860 static int 861 TIFFWritePerSampleAnys(TIFF* tif, 862 TIFFDataType type, ttag_t tag, TIFFDirEntry* dir) 863 { 864 double buf[10], v; 865 double* w = buf; 866 uint16 i, samples = tif->tif_dir.td_samplesperpixel; 867 int status; 868 869 if (samples > NITEMS(buf)) { 870 w = (double*) _TIFFmalloc(samples * sizeof (double)); 871 if (w == NULL) { 872 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 873 "No space to write per-sample values"); 874 return (0); 875 } 876 } 877 TIFFGetField(tif, tag, &v); 878 for (i = 0; i < samples; i++) 879 w[i] = v; 880 status = TIFFWriteAnyArray(tif, type, tag, dir, samples, w); 881 if (w != buf) 882 _TIFFfree(w); 883 return (status); 884 } 885 #undef NITEMS 886 887 /* 888 * Setup a pair of shorts that are returned by 889 * value, rather than as a reference to an array. 890 */ 891 static int 892 TIFFSetupShortPair(TIFF* tif, ttag_t tag, TIFFDirEntry* dir) 893 { 894 uint16 v[2]; 895 896 TIFFGetField(tif, tag, &v[0], &v[1]); 897 898 dir->tdir_tag = (uint16) tag; 899 dir->tdir_type = (uint16) TIFF_SHORT; 900 dir->tdir_count = 2; 901 return (TIFFWriteShortArray(tif, dir, v)); 902 } 903 904 /* 905 * Setup a directory entry for an NxM table of shorts, 906 * where M is known to be 2**bitspersample, and write 907 * the associated indirect data. 908 */ 909 static int 910 TIFFWriteShortTable(TIFF* tif, 911 ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16** table) 912 { 913 uint32 i, off; 914 915 dir->tdir_tag = (uint16) tag; 916 dir->tdir_type = (short) TIFF_SHORT; 917 /* XXX -- yech, fool TIFFWriteData */ 918 dir->tdir_count = (uint32) (1L<<tif->tif_dir.td_bitspersample); 919 off = tif->tif_dataoff; 920 for (i = 0; i < n; i++) 921 if (!TIFFWriteData(tif, dir, (char *)table[i])) 922 return (0); 923 dir->tdir_count *= n; 924 dir->tdir_offset = off; 925 return (1); 926 } 927 928 /* 929 * Write/copy data associated with an ASCII or opaque tag value. 930 */ 931 static int 932 TIFFWriteByteArray(TIFF* tif, TIFFDirEntry* dir, char* cp) 933 { 934 if (dir->tdir_count > 4) { 935 if (!TIFFWriteData(tif, dir, cp)) 936 return (0); 937 } else 938 _TIFFmemcpy(&dir->tdir_offset, cp, dir->tdir_count); 939 return (1); 940 } 941 942 /* 943 * Setup a directory entry of an array of SHORT 944 * or SSHORT and write the associated indirect values. 945 */ 946 static int 947 TIFFWriteShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v) 948 { 949 if (dir->tdir_count <= 2) { 950 if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) { 951 dir->tdir_offset = (uint32) ((long) v[0] << 16); 952 if (dir->tdir_count == 2) 953 dir->tdir_offset |= v[1] & 0xffff; 954 } else { 955 dir->tdir_offset = v[0] & 0xffff; 956 if (dir->tdir_count == 2) 957 dir->tdir_offset |= (long) v[1] << 16; 958 } 959 return (1); 960 } else 961 return (TIFFWriteData(tif, dir, (char*) v)); 962 } 963 964 /* 965 * Setup a directory entry of an array of LONG 966 * or SLONG and write the associated indirect values. 967 */ 968 static int 969 TIFFWriteLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v) 970 { 971 if (dir->tdir_count == 1) { 972 dir->tdir_offset = v[0]; 973 return (1); 974 } else 975 return (TIFFWriteData(tif, dir, (char*) v)); 976 } 977 978 /* 979 * Setup a directory entry of an array of RATIONAL 980 * or SRATIONAL and write the associated indirect values. 981 */ 982 static int 983 TIFFWriteRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v) 984 { 985 uint32 i; 986 uint32* t; 987 int status; 988 989 t = (uint32*) _TIFFmalloc(2 * dir->tdir_count * sizeof (uint32)); 990 if (t == NULL) { 991 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 992 "No space to write RATIONAL array"); 993 return (0); 994 } 995 for (i = 0; i < dir->tdir_count; i++) { 996 float fv = v[i]; 997 int sign = 1; 998 uint32 den; 999 1000 if (fv < 0) { 1001 if (dir->tdir_type == TIFF_RATIONAL) { 1002 TIFFWarningExt(tif->tif_clientdata, 1003 tif->tif_name, 1004 "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL", 1005 _TIFFFieldWithTag(tif,dir->tdir_tag)->field_name, 1006 fv); 1007 fv = 0; 1008 } else 1009 fv = -fv, sign = -1; 1010 } 1011 den = 1L; 1012 if (fv > 0) { 1013 while (fv < 1L<<(31-3) && den < 1L<<(31-3)) 1014 fv *= 1<<3, den *= 1L<<3; 1015 } 1016 t[2*i+0] = (uint32) (sign * (fv + 0.5)); 1017 t[2*i+1] = den; 1018 } 1019 status = TIFFWriteData(tif, dir, (char *)t); 1020 _TIFFfree((char*) t); 1021 return (status); 1022 } 1023 1024 static int 1025 TIFFWriteFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v) 1026 { 1027 TIFFCvtNativeToIEEEFloat(tif, dir->tdir_count, v); 1028 if (dir->tdir_count == 1) { 1029 dir->tdir_offset = *(uint32*) &v[0]; 1030 return (1); 1031 } else 1032 return (TIFFWriteData(tif, dir, (char*) v)); 1033 } 1034 1035 static int 1036 TIFFWriteDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v) 1037 { 1038 TIFFCvtNativeToIEEEDouble(tif, dir->tdir_count, v); 1039 return (TIFFWriteData(tif, dir, (char*) v)); 1040 } 1041 1042 /* 1043 * Write an array of ``type'' values for a specified tag (i.e. this is a tag 1044 * which is allowed to have different types, e.g. SMaxSampleType). 1045 * Internally the data values are represented as double since a double can 1046 * hold any of the TIFF tag types (yes, this should really be an abstract 1047 * type tany_t for portability). The data is converted into the specified 1048 * type in a temporary buffer and then handed off to the appropriate array 1049 * writer. 1050 */ 1051 static int 1052 TIFFWriteAnyArray(TIFF* tif, 1053 TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v) 1054 { 1055 char buf[10 * sizeof(double)]; 1056 char* w = buf; 1057 int i, status = 0; 1058 1059 if (n * TIFFDataWidth(type) > sizeof buf) { 1060 w = (char*) _TIFFmalloc(n * TIFFDataWidth(type)); 1061 if (w == NULL) { 1062 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 1063 "No space to write array"); 1064 return (0); 1065 } 1066 } 1067 1068 dir->tdir_tag = (uint16) tag; 1069 dir->tdir_type = (uint16) type; 1070 dir->tdir_count = n; 1071 1072 switch (type) { 1073 case TIFF_BYTE: 1074 { 1075 uint8* bp = (uint8*) w; 1076 for (i = 0; i < (int) n; i++) 1077 bp[i] = (uint8) v[i]; 1078 if (!TIFFWriteByteArray(tif, dir, (char*) bp)) 1079 goto out; 1080 } 1081 break; 1082 case TIFF_SBYTE: 1083 { 1084 int8* bp = (int8*) w; 1085 for (i = 0; i < (int) n; i++) 1086 bp[i] = (int8) v[i]; 1087 if (!TIFFWriteByteArray(tif, dir, (char*) bp)) 1088 goto out; 1089 } 1090 break; 1091 case TIFF_SHORT: 1092 { 1093 uint16* bp = (uint16*) w; 1094 for (i = 0; i < (int) n; i++) 1095 bp[i] = (uint16) v[i]; 1096 if (!TIFFWriteShortArray(tif, dir, (uint16*)bp)) 1097 goto out; 1098 } 1099 break; 1100 case TIFF_SSHORT: 1101 { 1102 int16* bp = (int16*) w; 1103 for (i = 0; i < (int) n; i++) 1104 bp[i] = (int16) v[i]; 1105 if (!TIFFWriteShortArray(tif, dir, (uint16*)bp)) 1106 goto out; 1107 } 1108 break; 1109 case TIFF_LONG: 1110 { 1111 uint32* bp = (uint32*) w; 1112 for (i = 0; i < (int) n; i++) 1113 bp[i] = (uint32) v[i]; 1114 if (!TIFFWriteLongArray(tif, dir, bp)) 1115 goto out; 1116 } 1117 break; 1118 case TIFF_SLONG: 1119 { 1120 int32* bp = (int32*) w; 1121 for (i = 0; i < (int) n; i++) 1122 bp[i] = (int32) v[i]; 1123 if (!TIFFWriteLongArray(tif, dir, (uint32*) bp)) 1124 goto out; 1125 } 1126 break; 1127 case TIFF_FLOAT: 1128 { 1129 float* bp = (float*) w; 1130 for (i = 0; i < (int) n; i++) 1131 bp[i] = (float) v[i]; 1132 if (!TIFFWriteFloatArray(tif, dir, bp)) 1133 goto out; 1134 } 1135 break; 1136 case TIFF_DOUBLE: 1137 return (TIFFWriteDoubleArray(tif, dir, v)); 1138 default: 1139 /* TIFF_NOTYPE */ 1140 /* TIFF_ASCII */ 1141 /* TIFF_UNDEFINED */ 1142 /* TIFF_RATIONAL */ 1143 /* TIFF_SRATIONAL */ 1144 goto out; 1145 } 1146 status = 1; 1147 out: 1148 if (w != buf) 1149 _TIFFfree(w); 1150 return (status); 1151 } 1152 1153 static int 1154 TIFFWriteTransferFunction(TIFF* tif, TIFFDirEntry* dir) 1155 { 1156 TIFFDirectory* td = &tif->tif_dir; 1157 tsize_t n = (1L<<td->td_bitspersample) * sizeof (uint16); 1158 uint16** tf = td->td_transferfunction; 1159 int ncols; 1160 1161 /* 1162 * Check if the table can be written as a single column, 1163 * or if it must be written as 3 columns. Note that we 1164 * write a 3-column tag if there are 2 samples/pixel and 1165 * a single column of data won't suffice--hmm. 1166 */ 1167 switch (td->td_samplesperpixel - td->td_extrasamples) { 1168 default: if (_TIFFmemcmp(tf[0], tf[2], n)) { ncols = 3; break; } 1169 case 2: if (_TIFFmemcmp(tf[0], tf[1], n)) { ncols = 3; break; } 1170 case 1: case 0: ncols = 1; 1171 } 1172 return (TIFFWriteShortTable(tif, 1173 TIFFTAG_TRANSFERFUNCTION, dir, ncols, tf)); 1174 } 1175 1176 static int 1177 TIFFWriteInkNames(TIFF* tif, TIFFDirEntry* dir) 1178 { 1179 TIFFDirectory* td = &tif->tif_dir; 1180 1181 dir->tdir_tag = TIFFTAG_INKNAMES; 1182 dir->tdir_type = (short) TIFF_ASCII; 1183 dir->tdir_count = td->td_inknameslen; 1184 return (TIFFWriteByteArray(tif, dir, td->td_inknames)); 1185 } 1186 1187 /* 1188 * Write a contiguous directory item. 1189 */ 1190 static int 1191 TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp) 1192 { 1193 tsize_t cc; 1194 1195 if (tif->tif_flags & TIFF_SWAB) { 1196 switch (dir->tdir_type) { 1197 case TIFF_SHORT: 1198 case TIFF_SSHORT: 1199 TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count); ddd 1200 break; 1201 case TIFF_LONG: 1202 case TIFF_SLONG: 1203 case TIFF_FLOAT: 1204 TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count); ddd 1205 break; 1206 case TIFF_RATIONAL: 1207 case TIFF_SRATIONAL: 1208 TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count); ddd 1209 break; 1210 case TIFF_DOUBLE: 1211 TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count); ddd 1212 break; 1213 } 1214 } 1215 dir->tdir_offset = tif->tif_dataoff; 1216 cc = dir->tdir_count * TIFFDataWidth((TIFFDataType) dir->tdir_type); 1217 if (SeekOK(tif, dir->tdir_offset) && 1218 WriteOK(tif, cp, cc)) { 1219 tif->tif_dataoff += (cc + 1) & ~1; 1220 return (1); 1221 } 1222 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 1223 "Error writing data for field \"%s\"", 1224 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); 1225 return (0); 1226 } 1227 1228 /* 1229 * Similar to TIFFWriteDirectory(), but if the directory has already 1230 * been written once, it is relocated to the end of the file, in case it 1231 * has changed in size. Note that this will result in the loss of the 1232 * previously used directory space. 1233 */ 1234 1235 int 1236 TIFFRewriteDirectory( TIFF *tif ) 1237 { 1238 static const char module[] = "TIFFRewriteDirectory"; 1239 1240 /* We don't need to do anything special if it hasn't been written. */ 1241 if( tif->tif_diroff == 0 ) 1242 return TIFFWriteDirectory( tif ); 1243 1244 /* 1245 * Find and zero the pointer to this directory, so that TIFFLinkDirectory 1246 * will cause it to be added after this directories current pre-link. 1247 */ 1248 1249 if (!(tif->tif_flags&TIFF_BIGTIFF)) 1250 { 1251 /* Is it the first directory in the file? */ 1252 if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff) 1253 { 1254 tif->tif_header.classic.tiff_diroff = 0; 1255 tif->tif_diroff = 0; 1256 1257 TIFFSeekFile(tif, (toff_t)(sizeof(TIFFHeaderClassic)-sizeof(uint32)), 1258 SEEK_SET); 1259 if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff), 1260 sizeof (tif->tif_diroff))) 1261 { 1262 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 1263 "Error updating TIFF header"); 1264 return (0); 1265 } 1266 } 1267 else 1268 { 1269 toff_t nextdir, off; 1270 1271 nextdir = tif->tif_header.classic.tiff_diroff; 1272 do { 1273 uint16 dircount; 1274 1275 if (!SeekOK(tif, nextdir) || 1276 !ReadOK(tif, &dircount, sizeof (dircount))) { 1277 TIFFErrorExt(tif->tif_clientdata, module, 1278 "Error fetching directory count"); 1279 return (0); 1280 } 1281 if (tif->tif_flags & TIFF_SWAB) 1282 TIFFSwabShort(&dircount); 1283 (void) TIFFSeekFile(tif, 1284 dircount * sizeof (TIFFDirEntry), SEEK_CUR); 1285 if (!ReadOK(tif, &nextdir, sizeof (nextdir))) { 1286 TIFFErrorExt(tif->tif_clientdata, module, 1287 "Error fetching directory link"); 1288 return (0); 1289 } 1290 if (tif->tif_flags & TIFF_SWAB) 1291 TIFFSwabLong(&nextdir); 1292 } while (nextdir != tif->tif_diroff && nextdir != 0); 1293 off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */ 1294 (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET); 1295 tif->tif_diroff = 0; 1296 if (!WriteOK(tif, &(tif->tif_diroff), sizeof (nextdir))) { 1297 TIFFErrorExt(tif->tif_clientdata, module, 1298 "Error writing directory link"); 1299 return (0); 1300 } 1301 } 1302 } 1303 else 1304 { 1305 /* TODO */ 1306 TIFFErrorExt(tif->tif_clientdata,module,"TIFFRewriteDirectory not implemented yet for BigTIFF"); 1307 return(0); 1308 } 1309 1310 /* 1311 * Now use TIFFWriteDirectory() normally. 1312 */ 1313 1314 return TIFFWriteDirectory( tif ); 1315 } 1316 1317 1318 /* 1319 * Link the current directory into the directory chain for the file. 1320 */ 1321 static int 1322 TIFFLinkDirectory(TIFF* tif) 1323 { 1324 static const char module[] = "TIFFLinkDirectory"; 1325 toff_t nextdir; 1326 toff_t diroff, off; 1327 1328 tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1; 1329 diroff = tif->tif_diroff; 1330 if (tif->tif_flags & TIFF_SWAB) 1331 TIFFSwabLong(&diroff); 1332 1333 /* 1334 * Handle SubIFDs 1335 */ 1336 if (tif->tif_flags & TIFF_INSUBIFD) { 1337 (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET); 1338 if (!WriteOK(tif, &diroff, sizeof (diroff))) { 1339 TIFFErrorExt(tif->tif_clientdata, module, 1340 "%s: Error writing SubIFD directory link", 1341 tif->tif_name); 1342 return (0); 1343 } 1344 /* 1345 * Advance to the next SubIFD or, if this is 1346 * the last one configured, revert back to the 1347 * normal directory linkage. 1348 */ 1349 if (--tif->tif_nsubifd) 1350 tif->tif_subifdoff += sizeof (diroff); 1351 else 1352 tif->tif_flags &= ~TIFF_INSUBIFD; 1353 return (1); 1354 } 1355 1356 if (!(tif->tif_flags&TIFF_BIGTIFF)) 1357 { 1358 if (tif->tif_header.classic.tiff_diroff == 0) { 1359 /* 1360 * First directory, overwrite offset in header. 1361 */ 1362 tif->tif_header.classic.tiff_diroff = tif->tif_diroff; 1363 (void) TIFFSeekFile(tif, 1364 (toff_t)(sizeof(TIFFHeaderClassic)-sizeof(uint32)), 1365 SEEK_SET); 1366 if (!WriteOK(tif, &diroff, sizeof (diroff))) { 1367 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 1368 "Error writing TIFF header"); 1369 return (0); 1370 } 1371 return (1); 1372 } 1373 /* 1374 * Not the first directory, search to the last and append. 1375 */ 1376 nextdir = tif->tif_header.classic.tiff_diroff; 1377 do { 1378 uint16 dircount; 1379 1380 if (!SeekOK(tif, nextdir) || 1381 !ReadOK(tif, &dircount, sizeof (dircount))) { 1382 TIFFErrorExt(tif->tif_clientdata, module, 1383 "Error fetching directory count"); 1384 return (0); 1385 } 1386 if (tif->tif_flags & TIFF_SWAB) 1387 TIFFSwabShort(&dircount); 1388 (void) TIFFSeekFile(tif, 1389 dircount * sizeof (TIFFDirEntry), SEEK_CUR); 1390 if (!ReadOK(tif, &nextdir, sizeof (nextdir))) { 1391 TIFFErrorExt(tif->tif_clientdata, module, 1392 "Error fetching directory link"); 1393 return (0); 1394 } 1395 if (tif->tif_flags & TIFF_SWAB) 1396 TIFFSwabLong(&nextdir); 1397 } while (nextdir != 0); 1398 off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */ 1399 (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET); 1400 if (!WriteOK(tif, &diroff, sizeof (diroff))) { 1401 TIFFErrorExt(tif->tif_clientdata, module, 1402 "Error writing directory link"); 1403 return (0); 1404 } 1405 } 1406 else 1407 { 1408 /* TODO */ 1409 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 1410 "NIY for BigTIFF"); 1411 return (0); 1412 } 1413 return (1); 1414 } 1415 1416 #endif 1417 1418 /* vim: set ts=8 sts=8 sw=8 noet: */ 1419