| 1 |
/* When writing data to TIFF files, it is often useful to store application- |
| 2 |
specific data in a private TIFF directory so that the tags don't need to |
| 3 |
be registered and won't conflict with other people's user-defined tags. |
| 4 |
One needs to have a registered public tag which contains some amount of |
| 5 |
raw data. That raw data, however, is interpreted at an independent, |
| 6 |
separate, private tiff directory. This file provides some routines which |
| 7 |
will be useful for converting that data from its raw binary form into |
| 8 |
the proper form for your application. |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* Copyright (c) 1988-1996 Sam Leffler |
| 13 |
* Copyright (c) 1991-1996 Silicon Graphics, Inc. |
| 14 |
* Copyright (c( 1996 USAF Phillips Laboratory |
| 15 |
* |
| 16 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 17 |
* its documentation for any purpose is hereby granted without fee, provided |
| 18 |
* that (i) the above copyright notices and this permission notice appear in |
| 19 |
* all copies of the software and related documentation, and (ii) the names of |
| 20 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 21 |
* publicity relating to the software without the specific, prior written |
| 22 |
* permission of Sam Leffler and Silicon Graphics. |
| 23 |
* |
| 24 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 25 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 26 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 27 |
* |
| 28 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 29 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 30 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 31 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 32 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 33 |
* OF THIS SOFTWARE. |
| 34 |
*/ |
| 35 |
|
| 36 |
/* |
| 37 |
* TIFF Library. |
| 38 |
* |
| 39 |
* These routines written by Conrad J. Poelman on a single late-night of |
| 40 |
* March 20-21, 1996. |
| 41 |
* |
| 42 |
* The entire purpose of this file is to provide a single external function, |
| 43 |
* TIFFWritePrivateDataSubDirectory(). This function is intended for use |
| 44 |
* in writing a private subdirectory structure into a TIFF file. The |
| 45 |
* actual reading of data from the structure is handled by the getFieldFn(), |
| 46 |
* which is passed to TIFFWritePrivateDataSubDirectory() as a parameter. The |
| 47 |
* idea is to enable any application wishing to read private subdirectories to |
| 48 |
* do so easily using this function, without modifying the TIFF library. |
| 49 |
* |
| 50 |
* The astute observer will notice that only two functions are at all different |
| 51 |
* from the original tif_dirwrite.c file: TIFFWritePrivateDataSubDirectory()and |
| 52 |
* TIFFWriteNormalSubTag(). All the other stuff that makes this file so huge |
| 53 |
* is only necessary because all of those functions are declared static in |
| 54 |
* tif_dirwrite.c, so we have to totally duplicate them in order to use them. |
| 55 |
* |
| 56 |
* Oh, also please note the bug-fix in the routine TIFFWriteNormalSubTag(), |
| 57 |
* which equally should be applied to TIFFWriteNormalTag(). |
| 58 |
* |
| 59 |
*/ |
| 60 |
#include "tiffiop.h" |
| 61 |
|
| 62 |
#if HAVE_IEEEFP |
| 63 |
#define TIFFCvtNativeToIEEEFloat(tif, n, fp) |
| 64 |
#define TIFFCvtNativeToIEEEDouble(tif, n, dp) |
| 65 |
#else |
| 66 |
extern void TIFFCvtNativeToIEEEFloat(TIFF *, uint32_t, float *); |
| 67 |
extern void TIFFCvtNativeToIEEEDouble(TIFF *, uint32_t, double *); |
| 68 |
#endif |
| 69 |
|
| 70 |
static int TIFFWriteNormalTag(TIFF *, TIFFDirEntry *, const TIFFFieldInfo *); |
| 71 |
static int TIFFWriteNormalSubTag(TIFF *, TIFFDirEntry *, const TIFFFieldInfo *, |
| 72 |
int (*getFieldFn)(TIFF *tif, ttag_t tag, ...)); |
| 73 |
static void TIFFSetupShortLong(TIFF *, ttag_t, TIFFDirEntry *, uint32_t); |
| 74 |
static int TIFFSetupShortPair(TIFF *, ttag_t, TIFFDirEntry *); |
| 75 |
static int TIFFWritePerSampleShorts(TIFF *, ttag_t, TIFFDirEntry *); |
| 76 |
static int TIFFWritePerSampleAnys(TIFF *, TIFFDataType, ttag_t, TIFFDirEntry *); |
| 77 |
static int TIFFWriteShortTable(TIFF *, ttag_t, TIFFDirEntry *, uint32_t, |
| 78 |
uint16_t **); |
| 79 |
static int TIFFWriteShortArray(TIFF *, TIFFDataType, ttag_t, TIFFDirEntry *, |
| 80 |
uint32_t, uint16_t *); |
| 81 |
static int TIFFWriteLongArray(TIFF *, TIFFDataType, ttag_t, TIFFDirEntry *, |
| 82 |
uint32_t, uint32_t *); |
| 83 |
static int TIFFWriteRationalArray(TIFF *, TIFFDataType, ttag_t, TIFFDirEntry *, |
| 84 |
uint32_t, float *); |
| 85 |
static int TIFFWriteFloatArray(TIFF *, TIFFDataType, ttag_t, TIFFDirEntry *, |
| 86 |
uint32_t, float *); |
| 87 |
static int TIFFWriteDoubleArray(TIFF *, TIFFDataType, ttag_t, TIFFDirEntry *, |
| 88 |
uint32_t, double *); |
| 89 |
static int TIFFWriteByteArray(TIFF *, TIFFDirEntry *, char *); |
| 90 |
static int TIFFWriteAnyArray(TIFF *, TIFFDataType, ttag_t, TIFFDirEntry *, |
| 91 |
uint32_t, double *); |
| 92 |
#ifdef COLORIMETRY_SUPPORT |
| 93 |
static int TIFFWriteTransferFunction(TIFF *, TIFFDirEntry *); |
| 94 |
#endif |
| 95 |
static int TIFFWriteData(TIFF *, TIFFDirEntry *, char *); |
| 96 |
static int TIFFLinkDirectory(TIFF *); |
| 97 |
|
| 98 |
#define WriteRationalPair(type, tag1, v1, tag2, v2) \ |
| 99 |
{ \ |
| 100 |
if (!TIFFWriteRational(tif, type, tag1, dir, v1)) \ |
| 101 |
goto bad; \ |
| 102 |
if (!TIFFWriteRational(tif, type, tag2, dir + 1, v2)) \ |
| 103 |
goto bad; \ |
| 104 |
dir++; \ |
| 105 |
} |
| 106 |
#define TIFFWriteRational(tif, type, tag, dir, v) \ |
| 107 |
TIFFWriteRationalArray((tif), (type), (tag), (dir), 1, &(v)) |
| 108 |
#ifndef TIFFWriteRational |
| 109 |
static int TIFFWriteRational(TIFF *, TIFFDataType, ttag_t, TIFFDirEntry *, |
| 110 |
float); |
| 111 |
#endif |
| 112 |
|
| 113 |
/* This function will write an entire directory to the disk, and return the |
| 114 |
offset value indicating where in the file it wrote the beginning of the |
| 115 |
directory structure. This is NOT the same as the offset value before |
| 116 |
calling this function, because some of the fields may have caused various |
| 117 |
data items to be written out BEFORE writing the directory structure. |
| 118 |
|
| 119 |
This code was basically written by ripping of the TIFFWriteDirectory() |
| 120 |
code and generalizing it, using RPS's TIFFWritePliIfd() code for |
| 121 |
inspiration. My original goal was to make this code general enough that |
| 122 |
the original TIFFWriteDirectory() could be rewritten to just call this |
| 123 |
function with the appropriate field and field-accessing arguments. |
| 124 |
|
| 125 |
However, now I realize that there's a lot of code that gets executed for |
| 126 |
the main, standard TIFF directories that does not apply to special |
| 127 |
private subdirectories, so such a reimplementation for the sake of |
| 128 |
eliminating redundant or duplicate code is probably not possible, |
| 129 |
unless we also pass in a Main flag to indicate which type of handling |
| 130 |
to do, which would be kind of a hack. I've marked those places where I |
| 131 |
changed or ripped out code which would have to be re-inserted to |
| 132 |
generalize this function. If it can be done in a clean and graceful way, |
| 133 |
it would be a great way to generalize the TIFF library. Otherwise, I'll |
| 134 |
just leave this code here where it duplicates but remains on top of and |
| 135 |
hopefully mostly independent of the main TIFF library. |
| 136 |
|
| 137 |
The caller will probably want to free the sub directory structure after |
| 138 |
returning from this call, since otherwise once written out, the user |
| 139 |
is likely to forget about it and leave data lying around. |
| 140 |
*/ |
| 141 |
toff_t TIFFWritePrivateDataSubDirectory( |
| 142 |
TIFF *tif, uint32_t pdir_fieldsset[], int pdir_fields_last, |
| 143 |
TIFFFieldInfo *field_info, int (*getFieldFn)(TIFF *tif, ttag_t tag, ...)) |
| 144 |
{ |
| 145 |
uint16_t dircount; |
| 146 |
uint32_t diroff, nextdiroff; |
| 147 |
ttag_t tag; |
| 148 |
uint32_t nfields; |
| 149 |
tsize_t dirsize; |
| 150 |
char *data; |
| 151 |
TIFFDirEntry *dir; |
| 152 |
u_long b, *fields, fields_size; |
| 153 |
toff_t directory_offset; |
| 154 |
TIFFFieldInfo *fip; |
| 155 |
|
| 156 |
/* |
| 157 |
* Deleted out all of the encoder flushing and such code from here - |
| 158 |
* not necessary for subdirectories. |
| 159 |
*/ |
| 160 |
|
| 161 |
/* Finish writing out any image data. */ |
| 162 |
TIFFFlushData(tif); |
| 163 |
|
| 164 |
/* |
| 165 |
* Size the directory so that we can calculate |
| 166 |
* offsets for the data items that aren't kept |
| 167 |
* in-place in each field. |
| 168 |
*/ |
| 169 |
nfields = 0; |
| 170 |
for (b = 0; b <= pdir_fields_last; b++) |
| 171 |
if (FieldSet(pdir_fieldsset, b)) |
| 172 |
/* Deleted code to make size of first 4 tags 2 |
| 173 |
instead of 1. */ |
| 174 |
nfields += 1; |
| 175 |
dirsize = nfields * sizeof(TIFFDirEntry); |
| 176 |
data = (char *)_TIFFmalloc(dirsize); |
| 177 |
if (data == NULL) |
| 178 |
{ |
| 179 |
TIFFErrorExtR(tif, tif->tif_name, |
| 180 |
"Cannot write private subdirectory, out of space"); |
| 181 |
return (0); |
| 182 |
} |
| 183 |
/* |
| 184 |
* Place directory in data section of the file. If there isn't one |
| 185 |
* yet, place it at the end of the file. The directory is treated as |
| 186 |
* data, so we don't link it into the directory structure at all. |
| 187 |
*/ |
| 188 |
if (tif->tif_dataoff == 0) |
| 189 |
tif->tif_dataoff = (TIFFSeekFile(tif, (toff_t)0, SEEK_END) + 1) & ~1; |
| 190 |
diroff = tif->tif_dataoff; |
| 191 |
tif->tif_dataoff = |
| 192 |
(toff_t)(diroff + sizeof(uint16_t) + dirsize + sizeof(toff_t)); |
| 193 |
if (tif->tif_dataoff & 1) |
| 194 |
tif->tif_dataoff++; |
| 195 |
(void)TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET); |
| 196 |
/*tif->tif_curdir++;*/ |
| 197 |
dir = (TIFFDirEntry *)data; |
| 198 |
/* |
| 199 |
* Setup external form of directory |
| 200 |
* entries and write data items. |
| 201 |
*/ |
| 202 |
/* |
| 203 |
* We make a local copy of the fieldsset here so that we don't mess |
| 204 |
* up the original one when we call ResetFieldBit(). But I'm not sure |
| 205 |
* why the original code calls ResetFieldBit(), since we're already |
| 206 |
* going through the fields in order... |
| 207 |
* |
| 208 |
* fields_size is the number of uint32_t's we will need to hold the |
| 209 |
* bit-mask for all of the fields. If our highest field number is |
| 210 |
* 100, then we'll need 100 / (8*4)+1 == 4 uint32_t's to hold the |
| 211 |
* fieldset. |
| 212 |
* |
| 213 |
* Unlike the original code, we allocate fields dynamically based |
| 214 |
* on the requested pdir_fields_last value, allowing private |
| 215 |
* data subdirectories to contain more than the built-in code's limit |
| 216 |
* of 95 tags in a directory. |
| 217 |
*/ |
| 218 |
fields_size = pdir_fields_last / (8 * sizeof(uint32_t)) + 1; |
| 219 |
fields = _TIFFmalloc(fields_size * sizeof(uint32_t)); |
| 220 |
_TIFFmemcpy(fields, pdir_fieldsset, fields_size * sizeof(uint32_t)); |
| 221 |
|
| 222 |
/* Deleted "write out extra samples tag" code here. */ |
| 223 |
|
| 224 |
/* Deleted code for checking a billion little special cases for the |
| 225 |
* standard TIFF tags. Should add a general mechanism for overloading |
| 226 |
* write function for each field, just like Brian kept telling me!!! |
| 227 |
*/ |
| 228 |
for (fip = field_info; fip->field_tag; fip++) |
| 229 |
{ |
| 230 |
/* Deleted code to check for FIELD_IGNORE!! */ |
| 231 |
if (/* fip->field_bit == FIELD_IGNORE || */ |
| 232 |
!FieldSet(fields, fip->field_bit)) |
| 233 |
continue; |
| 234 |
if (!TIFFWriteNormalSubTag(tif, dir, fip, getFieldFn)) |
| 235 |
goto bad; |
| 236 |
dir++; |
| 237 |
ResetFieldBit(fields, fip->field_bit); |
| 238 |
} |
| 239 |
|
| 240 |
/* Now we've written all of the referenced data, and are about to |
| 241 |
write the main directory structure, so grab the tif_dataoff value |
| 242 |
now so we can remember where we wrote the directory. */ |
| 243 |
directory_offset = tif->tif_dataoff; |
| 244 |
|
| 245 |
/* |
| 246 |
* Write directory. |
| 247 |
*/ |
| 248 |
dircount = (uint16_t)nfields; |
| 249 |
/* Deleted code to link to the next directory - we set it to zero! */ |
| 250 |
nextdiroff = 0; |
| 251 |
if (tif->tif_flags & TIFF_SWAB) |
| 252 |
{ |
| 253 |
/* |
| 254 |
* The file's byte order is opposite to the |
| 255 |
* native machine architecture. We overwrite |
| 256 |
* the directory information with impunity |
| 257 |
* because it'll be released below after we |
| 258 |
* write it to the file. Note that all the |
| 259 |
* other tag construction routines assume that |
| 260 |
* we do this byte-swapping; i.e. they only |
| 261 |
* byte-swap indirect data. |
| 262 |
*/ |
| 263 |
for (dir = (TIFFDirEntry *)data; dircount; dir++, dircount--) |
| 264 |
{ |
| 265 |
TIFFSwabArrayOfShort(&dir->tdir_tag, 2); |
| 266 |
TIFFSwabArrayOfLong(&dir->tdir_count, 2); |
| 267 |
} |
| 268 |
dircount = (uint16_t)nfields; |
| 269 |
TIFFSwabShort(&dircount); |
| 270 |
TIFFSwabLong(&nextdiroff); |
| 271 |
} |
| 272 |
|
| 273 |
(void)TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET); |
| 274 |
if (!WriteOK(tif, &dircount, sizeof(dircount))) |
| 275 |
{ |
| 276 |
TIFFErrorExtR(tif, tif->tif_name, |
| 277 |
"Error writing private subdirectory count"); |
| 278 |
goto bad; |
| 279 |
} |
| 280 |
if (!WriteOK(tif, data, dirsize)) |
| 281 |
{ |
| 282 |
TIFFErrorExtR(tif, tif->tif_name, |
| 283 |
"Error writing private subdirectory contents"); |
| 284 |
goto bad; |
| 285 |
} |
| 286 |
if (!WriteOK(tif, &nextdiroff, sizeof(nextdiroff))) |
| 287 |
{ |
| 288 |
TIFFErrorExtR(tif, tif->tif_name, |
| 289 |
"Error writing private subdirectory link"); |
| 290 |
goto bad; |
| 291 |
} |
| 292 |
tif->tif_dataoff += sizeof(dircount) + dirsize + sizeof(nextdiroff); |
| 293 |
|
| 294 |
_TIFFfree(data); |
| 295 |
_TIFFfree(fields); |
| 296 |
tif->tif_flags &= ~TIFF_DIRTYDIRECT; |
| 297 |
|
| 298 |
#if (0) |
| 299 |
/* This stuff commented out because I don't think we want it for |
| 300 |
subdirectories, but I could be wrong. */ |
| 301 |
(*tif->tif_cleanup)(tif); |
| 302 |
|
| 303 |
/* |
| 304 |
* Reset directory-related state for subsequent |
| 305 |
* directories. |
| 306 |
*/ |
| 307 |
TIFFDefaultDirectory(tif); |
| 308 |
tif->tif_curoff = 0; |
| 309 |
tif->tif_row = (uint32_t)-1; |
| 310 |
tif->tif_curstrip = (tstrip_t)-1; |
| 311 |
#endif |
| 312 |
|
| 313 |
return (directory_offset); |
| 314 |
bad: |
| 315 |
_TIFFfree(data); |
| 316 |
_TIFFfree(fields); |
| 317 |
return (0); |
| 318 |
} |
| 319 |
#undef WriteRationalPair |
| 320 |
|
| 321 |
/* |
| 322 |
* Process tags that are not special cased. |
| 323 |
*/ |
| 324 |
/* The standard function TIFFWriteNormalTag() could definitely be replaced |
| 325 |
with a simple call to this function, just adding TIFFGetField() as the |
| 326 |
last argument. */ |
| 327 |
static int TIFFWriteNormalSubTag(TIFF *tif, TIFFDirEntry *dir, |
| 328 |
const TIFFFieldInfo *fip, |
| 329 |
int (*getFieldFn)(TIFF *tif, ttag_t tag, ...)) |
| 330 |
{ |
| 331 |
u_short wc = (u_short)fip->field_writecount; |
| 332 |
|
| 333 |
dir->tdir_tag = fip->field_tag; |
| 334 |
dir->tdir_type = (u_short)fip->field_type; |
| 335 |
dir->tdir_count = wc; |
| 336 |
#define WRITEF(x, y) x(tif, fip->field_type, fip->field_tag, dir, wc, y) |
| 337 |
switch (fip->field_type) |
| 338 |
{ |
| 339 |
case TIFF_SHORT: |
| 340 |
case TIFF_SSHORT: |
| 341 |
if (wc > 1) |
| 342 |
{ |
| 343 |
uint16_t *wp; |
| 344 |
if (wc == (u_short)TIFF_VARIABLE) |
| 345 |
{ |
| 346 |
(*getFieldFn)(tif, fip->field_tag, &wc, &wp); |
| 347 |
dir->tdir_count = wc; |
| 348 |
} |
| 349 |
else |
| 350 |
(*getFieldFn)(tif, fip->field_tag, &wp); |
| 351 |
if (!WRITEF(TIFFWriteShortArray, wp)) |
| 352 |
return (0); |
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
uint16_t sv; |
| 357 |
(*getFieldFn)(tif, fip->field_tag, &sv); |
| 358 |
dir->tdir_offset = TIFFInsertData(tif, dir->tdir_type, sv); |
| 359 |
} |
| 360 |
break; |
| 361 |
case TIFF_LONG: |
| 362 |
case TIFF_SLONG: |
| 363 |
if (wc > 1) |
| 364 |
{ |
| 365 |
uint32_t *lp; |
| 366 |
if (wc == (u_short)TIFF_VARIABLE) |
| 367 |
{ |
| 368 |
(*getFieldFn)(tif, fip->field_tag, &wc, &lp); |
| 369 |
dir->tdir_count = wc; |
| 370 |
} |
| 371 |
else |
| 372 |
(*getFieldFn)(tif, fip->field_tag, &lp); |
| 373 |
if (!WRITEF(TIFFWriteLongArray, lp)) |
| 374 |
return (0); |
| 375 |
} |
| 376 |
else |
| 377 |
{ |
| 378 |
/* XXX handle LONG->SHORT conversion */ |
| 379 |
(*getFieldFn)(tif, fip->field_tag, &dir->tdir_offset); |
| 380 |
} |
| 381 |
break; |
| 382 |
case TIFF_RATIONAL: |
| 383 |
case TIFF_SRATIONAL: |
| 384 |
if (wc > 1) |
| 385 |
{ |
| 386 |
float *fp; |
| 387 |
if (wc == (u_short)TIFF_VARIABLE) |
| 388 |
{ |
| 389 |
(*getFieldFn)(tif, fip->field_tag, &wc, &fp); |
| 390 |
dir->tdir_count = wc; |
| 391 |
} |
| 392 |
else |
| 393 |
(*getFieldFn)(tif, fip->field_tag, &fp); |
| 394 |
if (!WRITEF(TIFFWriteRationalArray, fp)) |
| 395 |
return (0); |
| 396 |
} |
| 397 |
else |
| 398 |
{ |
| 399 |
float fv; |
| 400 |
(*getFieldFn)(tif, fip->field_tag, &fv); |
| 401 |
if (!WRITEF(TIFFWriteRationalArray, &fv)) |
| 402 |
return (0); |
| 403 |
} |
| 404 |
break; |
| 405 |
case TIFF_FLOAT: |
| 406 |
if (wc > 1) |
| 407 |
{ |
| 408 |
float *fp; |
| 409 |
if (wc == (u_short)TIFF_VARIABLE) |
| 410 |
{ |
| 411 |
(*getFieldFn)(tif, fip->field_tag, &wc, &fp); |
| 412 |
dir->tdir_count = wc; |
| 413 |
} |
| 414 |
else |
| 415 |
(*getFieldFn)(tif, fip->field_tag, &fp); |
| 416 |
if (!WRITEF(TIFFWriteFloatArray, fp)) |
| 417 |
return (0); |
| 418 |
} |
| 419 |
else |
| 420 |
{ |
| 421 |
float fv; |
| 422 |
(*getFieldFn)(tif, fip->field_tag, &fv); |
| 423 |
if (!WRITEF(TIFFWriteFloatArray, &fv)) |
| 424 |
return (0); |
| 425 |
} |
| 426 |
break; |
| 427 |
case TIFF_DOUBLE: |
| 428 |
/* Hey - I think this is a bug, or at least a "gross |
| 429 |
inconsistency", in the TIFF library. Look at the original |
| 430 |
TIFF library code below within the "#if (0) ... #else". |
| 431 |
Just from the type of *dp, you can see that this code |
| 432 |
expects TIFFGetField() to be handed a double ** for |
| 433 |
any TIFF_DOUBLE tag, even for the constant wc==1 case. |
| 434 |
This is totally inconsistent with other fields (like |
| 435 |
TIFF_FLOAT, above) and is also inconsistent with the |
| 436 |
TIFFSetField() function for TIFF_DOUBLEs, which expects |
| 437 |
to be passed a single double by value for the wc==1 case. |
| 438 |
(See the handling of TIFFFetchNormalTag() in tif_dirread.c |
| 439 |
for an example.) Maybe this function was written before |
| 440 |
TIFFWriteDoubleArray() was written, not that that's an |
| 441 |
excuse. Anyway, the new code below is a trivial modification |
| 442 |
of the TIFF_FLOAT code above. The fact that even single |
| 443 |
doubles get written out in the data segment and get an |
| 444 |
offset value stored is irrelevant here - that is all |
| 445 |
handled by TIFFWriteDoubleArray(). */ |
| 446 |
#if (0) |
| 447 |
{ |
| 448 |
double *dp; |
| 449 |
if (wc == (u_short)TIFF_VARIABLE) |
| 450 |
{ |
| 451 |
(*getFieldFn)(tif, fip->field_tag, &wc, &dp); |
| 452 |
dir->tdir_count = wc; |
| 453 |
} |
| 454 |
else |
| 455 |
(*getFieldFn)(tif, fip->field_tag, &dp); |
| 456 |
TIFFCvtNativeToIEEEDouble(tif, wc, dp); |
| 457 |
if (!TIFFWriteData(tif, dir, (char *)dp)) |
| 458 |
return (0); |
| 459 |
} |
| 460 |
#else |
| 461 |
if (wc > 1) |
| 462 |
{ |
| 463 |
double *dp; |
| 464 |
if (wc == (u_short)TIFF_VARIABLE) |
| 465 |
{ |
| 466 |
(*getFieldFn)(tif, fip->field_tag, &wc, &dp); |
| 467 |
dir->tdir_count = wc; |
| 468 |
} |
| 469 |
else |
| 470 |
(*getFieldFn)(tif, fip->field_tag, &dp); |
| 471 |
if (!WRITEF(TIFFWriteDoubleArray, dp)) |
| 472 |
return (0); |
| 473 |
} |
| 474 |
else |
| 475 |
{ |
| 476 |
double dv; |
| 477 |
(*getFieldFn)(tif, fip->field_tag, &dv); |
| 478 |
if (!WRITEF(TIFFWriteDoubleArray, &dv)) |
| 479 |
return (0); |
| 480 |
} |
| 481 |
#endif |
| 482 |
break; |
| 483 |
case TIFF_ASCII: |
| 484 |
{ |
| 485 |
char *cp; |
| 486 |
(*getFieldFn)(tif, fip->field_tag, &cp); |
| 487 |
dir->tdir_count = (uint32_t)(strlen(cp) + 1); |
| 488 |
if (!TIFFWriteByteArray(tif, dir, cp)) |
| 489 |
return (0); |
| 490 |
} |
| 491 |
break; |
| 492 |
case TIFF_UNDEFINED: |
| 493 |
{ |
| 494 |
char *cp; |
| 495 |
if (wc == (u_short)TIFF_VARIABLE) |
| 496 |
{ |
| 497 |
(*getFieldFn)(tif, fip->field_tag, &wc, &cp); |
| 498 |
dir->tdir_count = wc; |
| 499 |
} |
| 500 |
else |
| 501 |
(*getFieldFn)(tif, fip->field_tag, &cp); |
| 502 |
if (!TIFFWriteByteArray(tif, dir, cp)) |
| 503 |
return (0); |
| 504 |
} |
| 505 |
break; |
| 506 |
} |
| 507 |
return (1); |
| 508 |
} |
| 509 |
#undef WRITEF |
| 510 |
|
| 511 |
/* Everything after this is exactly duplicated from the standard tif_dirwrite.c |
| 512 |
file, necessitated by the fact that they are declared static there so |
| 513 |
we can't call them! |
| 514 |
*/ |
| 515 |
/* |
| 516 |
* Setup a directory entry with either a SHORT |
| 517 |
* or LONG type according to the value. |
| 518 |
*/ |
| 519 |
static void TIFFSetupShortLong(TIFF *tif, ttag_t tag, TIFFDirEntry *dir, |
| 520 |
uint32_t v) |
| 521 |
{ |
| 522 |
dir->tdir_tag = tag; |
| 523 |
dir->tdir_count = 1; |
| 524 |
if (v > 0xffffL) |
| 525 |
{ |
| 526 |
dir->tdir_type = (short)TIFF_LONG; |
| 527 |
dir->tdir_offset = v; |
| 528 |
} |
| 529 |
else |
| 530 |
{ |
| 531 |
dir->tdir_type = (short)TIFF_SHORT; |
| 532 |
dir->tdir_offset = TIFFInsertData(tif, (int)TIFF_SHORT, v); |
| 533 |
} |
| 534 |
} |
| 535 |
#undef MakeShortDirent |
| 536 |
|
| 537 |
#ifndef TIFFWriteRational |
| 538 |
/* |
| 539 |
* Setup a RATIONAL directory entry and |
| 540 |
* write the associated indirect value. |
| 541 |
*/ |
| 542 |
static int TIFFWriteRational(TIFF *tif, TIFFDataType type, ttag_t tag, |
| 543 |
TIFFDirEntry *dir, float v) |
| 544 |
{ |
| 545 |
return (TIFFWriteRationalArray(tif, type, tag, dir, 1, &v)); |
| 546 |
} |
| 547 |
#endif |
| 548 |
|
| 549 |
#define NITEMS(x) (sizeof(x) / sizeof(x[0])) |
| 550 |
/* |
| 551 |
* Setup a directory entry that references a |
| 552 |
* samples/pixel array of SHORT values and |
| 553 |
* (potentially) write the associated indirect |
| 554 |
* values. |
| 555 |
*/ |
| 556 |
static int TIFFWritePerSampleShorts(TIFF *tif, ttag_t tag, TIFFDirEntry *dir) |
| 557 |
{ |
| 558 |
uint16_t buf[10], v; |
| 559 |
uint16_t *w = buf; |
| 560 |
int i, status, samples = tif->tif_dir.td_samplesperpixel; |
| 561 |
|
| 562 |
if (samples > NITEMS(buf)) |
| 563 |
w = (uint16_t *)_TIFFmalloc(samples * sizeof(uint16_t)); |
| 564 |
TIFFGetField(tif, tag, &v); |
| 565 |
for (i = 0; i < samples; i++) |
| 566 |
w[i] = v; |
| 567 |
status = TIFFWriteShortArray(tif, TIFF_SHORT, tag, dir, samples, w); |
| 568 |
if (w != buf) |
| 569 |
_TIFFfree((char *)w); |
| 570 |
return (status); |
| 571 |
} |
| 572 |
|
| 573 |
/* |
| 574 |
* Setup a directory entry that references a samples/pixel array of ``type'' |
| 575 |
* values and (potentially) write the associated indirect values. The source |
| 576 |
* data from TIFFGetField() for the specified tag must be returned as double. |
| 577 |
*/ |
| 578 |
static int TIFFWritePerSampleAnys(TIFF *tif, TIFFDataType type, ttag_t tag, |
| 579 |
TIFFDirEntry *dir) |
| 580 |
{ |
| 581 |
double buf[10], v; |
| 582 |
double *w = buf; |
| 583 |
int i, status; |
| 584 |
int samples = (int)tif->tif_dir.td_samplesperpixel; |
| 585 |
|
| 586 |
if (samples > NITEMS(buf)) |
| 587 |
w = (double *)_TIFFmalloc(samples * sizeof(double)); |
| 588 |
TIFFGetField(tif, tag, &v); |
| 589 |
for (i = 0; i < samples; i++) |
| 590 |
w[i] = v; |
| 591 |
status = TIFFWriteAnyArray(tif, type, tag, dir, samples, w); |
| 592 |
if (w != buf) |
| 593 |
_TIFFfree(w); |
| 594 |
return (status); |
| 595 |
} |
| 596 |
#undef NITEMS |
| 597 |
|
| 598 |
/* |
| 599 |
* Setup a pair of shorts that are returned by |
| 600 |
* value, rather than as a reference to an array. |
| 601 |
*/ |
| 602 |
static int TIFFSetupShortPair(TIFF *tif, ttag_t tag, TIFFDirEntry *dir) |
| 603 |
{ |
| 604 |
uint16_t v[2]; |
| 605 |
|
| 606 |
TIFFGetField(tif, tag, &v[0], &v[1]); |
| 607 |
return (TIFFWriteShortArray(tif, TIFF_SHORT, tag, dir, 2, v)); |
| 608 |
} |
| 609 |
|
| 610 |
/* |
| 611 |
* Setup a directory entry for an NxM table of shorts, |
| 612 |
* where M is known to be 2**bitspersample, and write |
| 613 |
* the associated indirect data. |
| 614 |
*/ |
| 615 |
static int TIFFWriteShortTable(TIFF *tif, ttag_t tag, TIFFDirEntry *dir, |
| 616 |
uint32_t n, uint16_t **table) |
| 617 |
{ |
| 618 |
uint32_t i, off; |
| 619 |
|
| 620 |
dir->tdir_tag = tag; |
| 621 |
dir->tdir_type = (short)TIFF_SHORT; |
| 622 |
/* XXX -- yech, fool TIFFWriteData */ |
| 623 |
dir->tdir_count = (uint32_t)(1L << tif->tif_dir.td_bitspersample); |
| 624 |
off = tif->tif_dataoff; |
| 625 |
for (i = 0; i < n; i++) |
| 626 |
if (!TIFFWriteData(tif, dir, (char *)table[i])) |
| 627 |
return (0); |
| 628 |
dir->tdir_count *= n; |
| 629 |
dir->tdir_offset = off; |
| 630 |
return (1); |
| 631 |
} |
| 632 |
|
| 633 |
/* |
| 634 |
* Write/copy data associated with an ASCII or opaque tag value. |
| 635 |
*/ |
| 636 |
static int TIFFWriteByteArray(TIFF *tif, TIFFDirEntry *dir, char *cp) |
| 637 |
{ |
| 638 |
if (dir->tdir_count > 4) |
| 639 |
{ |
| 640 |
if (!TIFFWriteData(tif, dir, cp)) |
| 641 |
return (0); |
| 642 |
} |
| 643 |
else |
| 644 |
_TIFFmemcpy(&dir->tdir_offset, cp, dir->tdir_count); |
| 645 |
return (1); |
| 646 |
} |
| 647 |
|
| 648 |
/* |
| 649 |
* Setup a directory entry of an array of SHORT |
| 650 |
* or SSHORT and write the associated indirect values. |
| 651 |
*/ |
| 652 |
static int TIFFWriteShortArray(TIFF *tif, TIFFDataType type, ttag_t tag, |
| 653 |
TIFFDirEntry *dir, uint32_t n, uint16_t *v) |
| 654 |
{ |
| 655 |
dir->tdir_tag = tag; |
| 656 |
dir->tdir_type = (short)type; |
| 657 |
dir->tdir_count = n; |
| 658 |
if (n <= 2) |
| 659 |
{ |
| 660 |
if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) |
| 661 |
{ |
| 662 |
dir->tdir_offset = (uint32_t)((long)v[0] << 16); |
| 663 |
if (n == 2) |
| 664 |
dir->tdir_offset |= v[1] & 0xffff; |
| 665 |
} |
| 666 |
else |
| 667 |
{ |
| 668 |
dir->tdir_offset = v[0] & 0xffff; |
| 669 |
if (n == 2) |
| 670 |
dir->tdir_offset |= (long)v[1] << 16; |
| 671 |
} |
| 672 |
return (1); |
| 673 |
} |
| 674 |
else |
| 675 |
return (TIFFWriteData(tif, dir, (char *)v)); |
| 676 |
} |
| 677 |
|
| 678 |
/* |
| 679 |
* Setup a directory entry of an array of LONG |
| 680 |
* or SLONG and write the associated indirect values. |
| 681 |
*/ |
| 682 |
static int TIFFWriteLongArray(TIFF *tif, TIFFDataType type, ttag_t tag, |
| 683 |
TIFFDirEntry *dir, uint32_t n, uint32_t *v) |
| 684 |
{ |
| 685 |
dir->tdir_tag = tag; |
| 686 |
dir->tdir_type = (short)type; |
| 687 |
dir->tdir_count = n; |
| 688 |
if (n == 1) |
| 689 |
{ |
| 690 |
dir->tdir_offset = v[0]; |
| 691 |
return (1); |
| 692 |
} |
| 693 |
else |
| 694 |
return (TIFFWriteData(tif, dir, (char *)v)); |
| 695 |
} |
| 696 |
|
| 697 |
/* |
| 698 |
* Setup a directory entry of an array of RATIONAL |
| 699 |
* or SRATIONAL and write the associated indirect values. |
| 700 |
*/ |
| 701 |
static int TIFFWriteRationalArray(TIFF *tif, TIFFDataType type, ttag_t tag, |
| 702 |
TIFFDirEntry *dir, uint32_t n, float *v) |
| 703 |
{ |
| 704 |
uint32_t i; |
| 705 |
uint32_t *t; |
| 706 |
int status; |
| 707 |
|
| 708 |
dir->tdir_tag = tag; |
| 709 |
dir->tdir_type = (short)type; |
| 710 |
dir->tdir_count = n; |
| 711 |
t = (uint32_t *)_TIFFmalloc(2 * n * sizeof(uint32_t)); |
| 712 |
for (i = 0; i < n; i++) |
| 713 |
{ |
| 714 |
float fv = v[i]; |
| 715 |
int sign = 1; |
| 716 |
uint32_t den; |
| 717 |
|
| 718 |
if (fv < 0) |
| 719 |
{ |
| 720 |
if (type == TIFF_RATIONAL) |
| 721 |
{ |
| 722 |
TIFFWarning(tif->tif_name, |
| 723 |
"\"%s\": Information lost writing value (%g) as " |
| 724 |
"(unsigned) RATIONAL", |
| 725 |
_TIFFFieldWithTag(tif, tag)->field_name, v); |
| 726 |
fv = 0; |
| 727 |
} |
| 728 |
else |
| 729 |
fv = -fv, sign = -1; |
| 730 |
} |
| 731 |
den = 1L; |
| 732 |
if (fv > 0) |
| 733 |
{ |
| 734 |
while (fv < 1L << (31 - 3) && den < 1L << (31 - 3)) |
| 735 |
fv *= 1 << 3, den *= 1L << 3; |
| 736 |
} |
| 737 |
t[2 * i + 0] = sign * (fv + 0.5); |
| 738 |
t[2 * i + 1] = den; |
| 739 |
} |
| 740 |
status = TIFFWriteData(tif, dir, (char *)t); |
| 741 |
_TIFFfree((char *)t); |
| 742 |
return (status); |
| 743 |
} |
| 744 |
|
| 745 |
static int TIFFWriteFloatArray(TIFF *tif, TIFFDataType type, ttag_t tag, |
| 746 |
TIFFDirEntry *dir, uint32_t n, float *v) |
| 747 |
{ |
| 748 |
dir->tdir_tag = tag; |
| 749 |
dir->tdir_type = (short)type; |
| 750 |
dir->tdir_count = n; |
| 751 |
TIFFCvtNativeToIEEEFloat(tif, n, v); |
| 752 |
if (n == 1) |
| 753 |
{ |
| 754 |
dir->tdir_offset = *(uint32_t *)&v[0]; |
| 755 |
return (1); |
| 756 |
} |
| 757 |
else |
| 758 |
return (TIFFWriteData(tif, dir, (char *)v)); |
| 759 |
} |
| 760 |
|
| 761 |
static int TIFFWriteDoubleArray(TIFF *tif, TIFFDataType type, ttag_t tag, |
| 762 |
TIFFDirEntry *dir, uint32_t n, double *v) |
| 763 |
{ |
| 764 |
dir->tdir_tag = tag; |
| 765 |
dir->tdir_type = (short)type; |
| 766 |
dir->tdir_count = n; |
| 767 |
TIFFCvtNativeToIEEEDouble(tif, n, v); |
| 768 |
return (TIFFWriteData(tif, dir, (char *)v)); |
| 769 |
} |
| 770 |
|
| 771 |
/* |
| 772 |
* Write an array of ``type'' values for a specified tag (i.e. this is a tag |
| 773 |
* which is allowed to have different types, e.g. SMaxSampleType). |
| 774 |
* Internally the data values are represented as double since a double can |
| 775 |
* hold any of the TIFF tag types (yes, this should really be an abstract |
| 776 |
* type tany_t for portability). The data is converted into the specified |
| 777 |
* type in a temporary buffer and then handed off to the appropriate array |
| 778 |
* writer. |
| 779 |
*/ |
| 780 |
static int TIFFWriteAnyArray(TIFF *tif, TIFFDataType type, ttag_t tag, |
| 781 |
TIFFDirEntry *dir, uint32_t n, double *v) |
| 782 |
{ |
| 783 |
char buf[10 * sizeof(double)]; |
| 784 |
char *w = buf; |
| 785 |
int i, status = 0; |
| 786 |
|
| 787 |
if (n * TIFFDataWidth(type) > sizeof buf) |
| 788 |
w = (char *)_TIFFmalloc(n * TIFFDataWidth(type)); |
| 789 |
switch (type) |
| 790 |
{ |
| 791 |
case TIFF_BYTE: |
| 792 |
{ |
| 793 |
unsigned char *bp = (unsigned char *)w; |
| 794 |
for (i = 0; i < n; i++) |
| 795 |
bp[i] = (unsigned char)v[i]; |
| 796 |
dir->tdir_tag = tag; |
| 797 |
dir->tdir_type = (short)type; |
| 798 |
dir->tdir_count = n; |
| 799 |
if (!TIFFWriteByteArray(tif, dir, (char *)bp)) |
| 800 |
goto out; |
| 801 |
} |
| 802 |
break; |
| 803 |
case TIFF_SBYTE: |
| 804 |
{ |
| 805 |
signed char *bp = (signed char *)w; |
| 806 |
for (i = 0; i < n; i++) |
| 807 |
bp[i] = (signed char)v[i]; |
| 808 |
dir->tdir_tag = tag; |
| 809 |
dir->tdir_type = (short)type; |
| 810 |
dir->tdir_count = n; |
| 811 |
if (!TIFFWriteByteArray(tif, dir, (char *)bp)) |
| 812 |
goto out; |
| 813 |
} |
| 814 |
break; |
| 815 |
case TIFF_SHORT: |
| 816 |
{ |
| 817 |
uint16_t *bp = (uint16_t *)w; |
| 818 |
for (i = 0; i < n; i++) |
| 819 |
bp[i] = (uint16_t)v[i]; |
| 820 |
if (!TIFFWriteShortArray(tif, type, tag, dir, n, (uint16_t *)bp)) |
| 821 |
goto out; |
| 822 |
} |
| 823 |
break; |
| 824 |
case TIFF_SSHORT: |
| 825 |
{ |
| 826 |
int16_t *bp = (int16_t *)w; |
| 827 |
for (i = 0; i < n; i++) |
| 828 |
bp[i] = (int16_t)v[i]; |
| 829 |
if (!TIFFWriteShortArray(tif, type, tag, dir, n, (uint16_t *)bp)) |
| 830 |
goto out; |
| 831 |
} |
| 832 |
break; |
| 833 |
case TIFF_LONG: |
| 834 |
{ |
| 835 |
uint32_t *bp = (uint32_t *)w; |
| 836 |
for (i = 0; i < n; i++) |
| 837 |
bp[i] = (uint32_t)v[i]; |
| 838 |
if (!TIFFWriteLongArray(tif, type, tag, dir, n, bp)) |
| 839 |
goto out; |
| 840 |
} |
| 841 |
break; |
| 842 |
case TIFF_SLONG: |
| 843 |
{ |
| 844 |
int32_t *bp = (int32_t *)w; |
| 845 |
for (i = 0; i < n; i++) |
| 846 |
bp[i] = (int32_t)v[i]; |
| 847 |
if (!TIFFWriteLongArray(tif, type, tag, dir, n, (uint32_t *)bp)) |
| 848 |
goto out; |
| 849 |
} |
| 850 |
break; |
| 851 |
case TIFF_FLOAT: |
| 852 |
{ |
| 853 |
float *bp = (float *)w; |
| 854 |
for (i = 0; i < n; i++) |
| 855 |
bp[i] = (float)v[i]; |
| 856 |
if (!TIFFWriteFloatArray(tif, type, tag, dir, n, bp)) |
| 857 |
goto out; |
| 858 |
} |
| 859 |
break; |
| 860 |
case TIFF_DOUBLE: |
| 861 |
return (TIFFWriteDoubleArray(tif, type, tag, dir, n, v)); |
| 862 |
default: |
| 863 |
/* TIFF_NOTYPE */ |
| 864 |
/* TIFF_ASCII */ |
| 865 |
/* TIFF_UNDEFINED */ |
| 866 |
/* TIFF_RATIONAL */ |
| 867 |
/* TIFF_SRATIONAL */ |
| 868 |
goto out; |
| 869 |
} |
| 870 |
status = 1; |
| 871 |
out: |
| 872 |
if (w != buf) |
| 873 |
_TIFFfree(w); |
| 874 |
return (status); |
| 875 |
} |
| 876 |
|
| 877 |
#ifdef COLORIMETRY_SUPPORT |
| 878 |
static int TIFFWriteTransferFunction(TIFF *tif, TIFFDirEntry *dir) |
| 879 |
{ |
| 880 |
TIFFDirectory *td = &tif->tif_dir; |
| 881 |
tsize_t n = (1L << td->td_bitspersample) * sizeof(uint16_t); |
| 882 |
uint16_t **tf = td->td_transferfunction; |
| 883 |
int ncols; |
| 884 |
|
| 885 |
/* |
| 886 |
* Check if the table can be written as a single column, |
| 887 |
* or if it must be written as 3 columns. Note that we |
| 888 |
* write a 3-column tag if there are 2 samples/pixel and |
| 889 |
* a single column of data won't suffice--hmm. |
| 890 |
*/ |
| 891 |
switch (td->td_samplesperpixel - td->td_extrasamples) |
| 892 |
{ |
| 893 |
default: |
| 894 |
if (_TIFFmemcmp(tf[0], tf[2], n)) |
| 895 |
{ |
| 896 |
ncols = 3; |
| 897 |
break; |
| 898 |
} |
| 899 |
case 2: |
| 900 |
if (_TIFFmemcmp(tf[0], tf[1], n)) |
| 901 |
{ |
| 902 |
ncols = 3; |
| 903 |
break; |
| 904 |
} |
| 905 |
case 1: |
| 906 |
case 0: |
| 907 |
ncols = 1; |
| 908 |
} |
| 909 |
return (TIFFWriteShortTable(tif, TIFFTAG_TRANSFERFUNCTION, dir, ncols, tf)); |
| 910 |
} |
| 911 |
#endif |
| 912 |
|
| 913 |
/* |
| 914 |
* Write a contiguous directory item. |
| 915 |
*/ |
| 916 |
static int TIFFWriteData(TIFF *tif, TIFFDirEntry *dir, char *cp) |
| 917 |
{ |
| 918 |
tsize_t cc; |
| 919 |
|
| 920 |
if (tif->tif_flags & TIFF_SWAB) |
| 921 |
{ |
| 922 |
switch (dir->tdir_type) |
| 923 |
{ |
| 924 |
case TIFF_SHORT: |
| 925 |
case TIFF_SSHORT: |
| 926 |
TIFFSwabArrayOfShort((uint16_t *)cp, dir->tdir_count); |
| 927 |
break; |
| 928 |
case TIFF_LONG: |
| 929 |
case TIFF_SLONG: |
| 930 |
case TIFF_FLOAT: |
| 931 |
TIFFSwabArrayOfLong((uint32_t *)cp, dir->tdir_count); |
| 932 |
break; |
| 933 |
case TIFF_RATIONAL: |
| 934 |
case TIFF_SRATIONAL: |
| 935 |
TIFFSwabArrayOfLong((uint32_t *)cp, 2 * dir->tdir_count); |
| 936 |
break; |
| 937 |
case TIFF_DOUBLE: |
| 938 |
TIFFSwabArrayOfDouble((double *)cp, dir->tdir_count); |
| 939 |
break; |
| 940 |
} |
| 941 |
} |
| 942 |
dir->tdir_offset = tif->tif_dataoff; |
| 943 |
cc = dir->tdir_count * TIFFDataWidth(dir->tdir_type); |
| 944 |
if (SeekOK(tif, dir->tdir_offset) && WriteOK(tif, cp, cc)) |
| 945 |
{ |
| 946 |
tif->tif_dataoff += (cc + 1) & ~1; |
| 947 |
return (1); |
| 948 |
} |
| 949 |
TIFFErrorExtR(tif, tif->tif_name, "Error writing data for field \"%s\"", |
| 950 |
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); |
| 951 |
return (0); |
| 952 |
} |
| 953 |
|
| 954 |
/* |
| 955 |
* Link the current directory into the |
| 956 |
* directory chain for the file. |
| 957 |
*/ |
| 958 |
static int TIFFLinkDirectory(TIFF *tif) |
| 959 |
{ |
| 960 |
static const char module[] = "TIFFLinkDirectory"; |
| 961 |
uint32_t nextdir; |
| 962 |
uint32_t diroff; |
| 963 |
|
| 964 |
tif->tif_diroff = (TIFFSeekFile(tif, (toff_t)0, SEEK_END) + 1) & ~1; |
| 965 |
diroff = (uint32_t)tif->tif_diroff; |
| 966 |
if (tif->tif_flags & TIFF_SWAB) |
| 967 |
TIFFSwabLong(&diroff); |
| 968 |
#if SUBIFD_SUPPORT |
| 969 |
if (tif->tif_flags & TIFF_INSUBIFD) |
| 970 |
{ |
| 971 |
(void)TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET); |
| 972 |
if (!WriteOK(tif, &diroff, sizeof(diroff))) |
| 973 |
{ |
| 974 |
TIFFErrorExtR(tif, module, |
| 975 |
"%s: Error writing SubIFD directory link", |
| 976 |
tif->tif_name); |
| 977 |
return (0); |
| 978 |
} |
| 979 |
/* |
| 980 |
* Advance to the next SubIFD or, if this is |
| 981 |
* the last one configured, revert back to the |
| 982 |
* normal directory linkage. |
| 983 |
*/ |
| 984 |
if (--tif->tif_nsubifd) |
| 985 |
tif->tif_subifdoff += sizeof(diroff); |
| 986 |
else |
| 987 |
tif->tif_flags &= ~TIFF_INSUBIFD; |
| 988 |
return (1); |
| 989 |
} |
| 990 |
#endif |
| 991 |
if (tif->tif_header.tiff_diroff == 0) |
| 992 |
{ |
| 993 |
/* |
| 994 |
* First directory, overwrite offset in header. |
| 995 |
*/ |
| 996 |
tif->tif_header.tiff_diroff = (uint32_t)tif->tif_diroff; |
| 997 |
#define HDROFF(f) ((toff_t) & (((TIFFHeader *)0)->f)) |
| 998 |
(void)TIFFSeekFile(tif, HDROFF(tiff_diroff), SEEK_SET); |
| 999 |
if (!WriteOK(tif, &diroff, sizeof(diroff))) |
| 1000 |
{ |
| 1001 |
TIFFErrorExtR(tif, tif->tif_name, "Error writing TIFF header"); |
| 1002 |
return (0); |
| 1003 |
} |
| 1004 |
return (1); |
| 1005 |
} |
| 1006 |
/* |
| 1007 |
* Not the first directory, search to the last and append. |
| 1008 |
*/ |
| 1009 |
nextdir = tif->tif_header.tiff_diroff; |
| 1010 |
do |
| 1011 |
{ |
| 1012 |
uint16_t dircount; |
| 1013 |
|
| 1014 |
if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, sizeof(dircount))) |
| 1015 |
{ |
| 1016 |
TIFFErrorExtR(tif, module, "Error fetching directory count"); |
| 1017 |
return (0); |
| 1018 |
} |
| 1019 |
if (tif->tif_flags & TIFF_SWAB) |
| 1020 |
TIFFSwabShort(&dircount); |
| 1021 |
(void)TIFFSeekFile(tif, dircount * sizeof(TIFFDirEntry), SEEK_CUR); |
| 1022 |
if (!ReadOK(tif, &nextdir, sizeof(nextdir))) |
| 1023 |
{ |
| 1024 |
TIFFErrorExtR(tif, module, "Error fetching directory link"); |
| 1025 |
return (0); |
| 1026 |
} |
| 1027 |
if (tif->tif_flags & TIFF_SWAB) |
| 1028 |
TIFFSwabLong(&nextdir); |
| 1029 |
} while (nextdir != 0); |
| 1030 |
(void)TIFFSeekFile(tif, -(toff_t)sizeof(nextdir), SEEK_CUR); |
| 1031 |
if (!WriteOK(tif, &diroff, sizeof(diroff))) |
| 1032 |
{ |
| 1033 |
TIFFErrorExtR(tif, module, "Error writing directory link"); |
| 1034 |
return (0); |
| 1035 |
} |
| 1036 |
return (1); |
| 1037 |
} |