1 /* $Id: tif_dirwrite.c,v 1.89 2017-08-23 13:33:42 erouault Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
30 * Directory Write Support Routines.
36 #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
37 #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
39 extern void TIFFCvtNativeToIEEEFloat(TIFF
* tif
, uint32 n
, float* fp
);
40 extern void TIFFCvtNativeToIEEEDouble(TIFF
* tif
, uint32 n
, double* dp
);
43 static int TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
);
45 static int TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
47 static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
50 static int TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
);
51 static int TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
53 static int TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
55 static int TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
57 static int TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
60 static int TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
62 static int TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
);
64 static int TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
66 static int TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
67 static int TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
);
68 static int TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
70 static int TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
72 static int TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
);
74 static int TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
76 static int TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
77 static int TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
79 static int TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
82 static int TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
84 static int TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
);
86 static int TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
89 static int TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
);
91 static int TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
93 static int TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
);
95 static int TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
);
96 static int TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
97 static int TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
98 static int TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
100 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
102 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
104 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
107 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
109 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
111 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
113 static int TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
115 static int TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
117 static int TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
118 static int TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
119 static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
121 static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
123 static int TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
124 static int TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
125 static int TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
127 static int TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
);
128 static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
130 static int TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
132 static int TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
134 static int TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
136 static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
);
137 static int TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
138 static int TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
);
140 static int TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
142 static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
);
143 static int TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
144 static int TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
146 static int TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
148 static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
);
150 static int TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
);
152 static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
154 static int TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
);
156 static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
);
157 static int TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
158 static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
159 static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
161 static int TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
163 static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
165 static int TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
167 static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
168 static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
169 static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
171 static int TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
);
173 static int TIFFLinkDirectory(TIFF
*);
176 * Write the contents of the current directory
177 * to the specified file. This routine doesn't
178 * handle overwriting a directory with auxiliary
179 * storage that's been changed.
182 TIFFWriteDirectory(TIFF
* tif
)
184 return TIFFWriteDirectorySec(tif
,TRUE
,TRUE
,NULL
);
188 * Similar to TIFFWriteDirectory(), writes the directory out
189 * but leaves all data structures in memory so that it can be
190 * written again. This will make a partially written TIFF file
191 * readable before it is successfully completed/closed.
194 TIFFCheckpointDirectory(TIFF
* tif
)
197 /* Setup the strips arrays, if they haven't already been. */
198 if (tif
->tif_dir
.td_stripoffset
== NULL
)
199 (void) TIFFSetupStrips(tif
);
200 rc
= TIFFWriteDirectorySec(tif
,TRUE
,FALSE
,NULL
);
201 (void) TIFFSetWriteOffset(tif
, TIFFSeekFile(tif
, 0, SEEK_END
));
206 TIFFWriteCustomDirectory(TIFF
* tif
, uint64
* pdiroff
)
208 return TIFFWriteDirectorySec(tif
,FALSE
,FALSE
,pdiroff
);
212 * Similar to TIFFWriteDirectory(), but if the directory has already
213 * been written once, it is relocated to the end of the file, in case it
214 * has changed in size. Note that this will result in the loss of the
215 * previously used directory space.
218 TIFFRewriteDirectory( TIFF
*tif
)
220 static const char module
[] = "TIFFRewriteDirectory";
222 /* We don't need to do anything special if it hasn't been written. */
223 if( tif
->tif_diroff
== 0 )
224 return TIFFWriteDirectory( tif
);
227 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
228 * will cause it to be added after this directories current pre-link.
231 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
233 if (tif
->tif_header
.classic
.tiff_diroff
== tif
->tif_diroff
)
235 tif
->tif_header
.classic
.tiff_diroff
= 0;
238 TIFFSeekFile(tif
,4,SEEK_SET
);
239 if (!WriteOK(tif
, &(tif
->tif_header
.classic
.tiff_diroff
),4))
241 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
242 "Error updating TIFF header");
249 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
254 if (!SeekOK(tif
, nextdir
) ||
255 !ReadOK(tif
, &dircount
, 2)) {
256 TIFFErrorExt(tif
->tif_clientdata
, module
,
257 "Error fetching directory count");
260 if (tif
->tif_flags
& TIFF_SWAB
)
261 TIFFSwabShort(&dircount
);
262 (void) TIFFSeekFile(tif
,
263 nextdir
+2+dircount
*12, SEEK_SET
);
264 if (!ReadOK(tif
, &nextnextdir
, 4)) {
265 TIFFErrorExt(tif
->tif_clientdata
, module
,
266 "Error fetching directory link");
269 if (tif
->tif_flags
& TIFF_SWAB
)
270 TIFFSwabLong(&nextnextdir
);
271 if (nextnextdir
==tif
->tif_diroff
)
275 (void) TIFFSeekFile(tif
,
276 nextdir
+2+dircount
*12, SEEK_SET
);
277 if (!WriteOK(tif
, &m
, 4)) {
278 TIFFErrorExt(tif
->tif_clientdata
, module
,
279 "Error writing directory link");
291 if (tif
->tif_header
.big
.tiff_diroff
== tif
->tif_diroff
)
293 tif
->tif_header
.big
.tiff_diroff
= 0;
296 TIFFSeekFile(tif
,8,SEEK_SET
);
297 if (!WriteOK(tif
, &(tif
->tif_header
.big
.tiff_diroff
),8))
299 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
300 "Error updating TIFF header");
307 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
313 if (!SeekOK(tif
, nextdir
) ||
314 !ReadOK(tif
, &dircount64
, 8)) {
315 TIFFErrorExt(tif
->tif_clientdata
, module
,
316 "Error fetching directory count");
319 if (tif
->tif_flags
& TIFF_SWAB
)
320 TIFFSwabLong8(&dircount64
);
321 if (dircount64
>0xFFFF)
323 TIFFErrorExt(tif
->tif_clientdata
, module
,
324 "Sanity check on tag count failed, likely corrupt TIFF");
327 dircount
=(uint16
)dircount64
;
328 (void) TIFFSeekFile(tif
,
329 nextdir
+8+dircount
*20, SEEK_SET
);
330 if (!ReadOK(tif
, &nextnextdir
, 8)) {
331 TIFFErrorExt(tif
->tif_clientdata
, module
,
332 "Error fetching directory link");
335 if (tif
->tif_flags
& TIFF_SWAB
)
336 TIFFSwabLong8(&nextnextdir
);
337 if (nextnextdir
==tif
->tif_diroff
)
341 (void) TIFFSeekFile(tif
,
342 nextdir
+8+dircount
*20, SEEK_SET
);
343 if (!WriteOK(tif
, &m
, 8)) {
344 TIFFErrorExt(tif
->tif_clientdata
, module
,
345 "Error writing directory link");
357 * Now use TIFFWriteDirectory() normally.
360 return TIFFWriteDirectory( tif
);
364 TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
)
366 static const char module
[] = "TIFFWriteDirectorySec";
372 if (tif
->tif_mode
== O_RDONLY
)
375 _TIFFFillStriles( tif
);
378 * Clear write state so that subsequent images with
379 * different characteristics get the right buffers
384 if (tif
->tif_flags
& TIFF_POSTENCODE
)
386 tif
->tif_flags
&= ~TIFF_POSTENCODE
;
387 if (!(*tif
->tif_postencode
)(tif
))
389 TIFFErrorExt(tif
->tif_clientdata
,module
,
390 "Error post-encoding before directory write");
394 (*tif
->tif_close
)(tif
); /* shutdown encoder */
396 * Flush any data that might have been written
397 * by the compression close+cleanup routines. But
398 * be careful not to write stuff if we didn't add data
399 * in the previous steps as the "rawcc" data may well be
400 * a previously read tile/strip in mixed read/write mode.
402 if (tif
->tif_rawcc
> 0
403 && (tif
->tif_flags
& TIFF_BEENWRITING
) != 0 )
405 if( !TIFFFlushData1(tif
) )
407 TIFFErrorExt(tif
->tif_clientdata
, module
,
408 "Error flushing data before directory write");
412 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
)
414 _TIFFfree(tif
->tif_rawdata
);
415 tif
->tif_rawdata
= NULL
;
417 tif
->tif_rawdatasize
= 0;
418 tif
->tif_rawdataoff
= 0;
419 tif
->tif_rawdataloaded
= 0;
421 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
);
431 if (TIFFFieldSet(tif
,FIELD_IMAGEDIMENSIONS
))
433 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEWIDTH
,tif
->tif_dir
.td_imagewidth
))
435 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGELENGTH
,tif
->tif_dir
.td_imagelength
))
438 if (TIFFFieldSet(tif
,FIELD_TILEDIMENSIONS
))
440 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILEWIDTH
,tif
->tif_dir
.td_tilewidth
))
442 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILELENGTH
,tif
->tif_dir
.td_tilelength
))
445 if (TIFFFieldSet(tif
,FIELD_RESOLUTION
))
447 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XRESOLUTION
,tif
->tif_dir
.td_xresolution
))
449 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YRESOLUTION
,tif
->tif_dir
.td_yresolution
))
452 if (TIFFFieldSet(tif
,FIELD_POSITION
))
454 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XPOSITION
,tif
->tif_dir
.td_xposition
))
456 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YPOSITION
,tif
->tif_dir
.td_yposition
))
459 if (TIFFFieldSet(tif
,FIELD_SUBFILETYPE
))
461 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_SUBFILETYPE
,tif
->tif_dir
.td_subfiletype
))
464 if (TIFFFieldSet(tif
,FIELD_BITSPERSAMPLE
))
466 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_BITSPERSAMPLE
,tif
->tif_dir
.td_bitspersample
))
469 if (TIFFFieldSet(tif
,FIELD_COMPRESSION
))
471 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_COMPRESSION
,tif
->tif_dir
.td_compression
))
474 if (TIFFFieldSet(tif
,FIELD_PHOTOMETRIC
))
476 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PHOTOMETRIC
,tif
->tif_dir
.td_photometric
))
479 if (TIFFFieldSet(tif
,FIELD_THRESHHOLDING
))
481 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_THRESHHOLDING
,tif
->tif_dir
.td_threshholding
))
484 if (TIFFFieldSet(tif
,FIELD_FILLORDER
))
486 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_FILLORDER
,tif
->tif_dir
.td_fillorder
))
489 if (TIFFFieldSet(tif
,FIELD_ORIENTATION
))
491 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_ORIENTATION
,tif
->tif_dir
.td_orientation
))
494 if (TIFFFieldSet(tif
,FIELD_SAMPLESPERPIXEL
))
496 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_SAMPLESPERPIXEL
,tif
->tif_dir
.td_samplesperpixel
))
499 if (TIFFFieldSet(tif
,FIELD_ROWSPERSTRIP
))
501 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_ROWSPERSTRIP
,tif
->tif_dir
.td_rowsperstrip
))
504 if (TIFFFieldSet(tif
,FIELD_MINSAMPLEVALUE
))
506 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MINSAMPLEVALUE
,tif
->tif_dir
.td_minsamplevalue
))
509 if (TIFFFieldSet(tif
,FIELD_MAXSAMPLEVALUE
))
511 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MAXSAMPLEVALUE
,tif
->tif_dir
.td_maxsamplevalue
))
514 if (TIFFFieldSet(tif
,FIELD_PLANARCONFIG
))
516 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PLANARCONFIG
,tif
->tif_dir
.td_planarconfig
))
519 if (TIFFFieldSet(tif
,FIELD_RESOLUTIONUNIT
))
521 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_RESOLUTIONUNIT
,tif
->tif_dir
.td_resolutionunit
))
524 if (TIFFFieldSet(tif
,FIELD_PAGENUMBER
))
526 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_PAGENUMBER
,2,&tif
->tif_dir
.td_pagenumber
[0]))
529 if (TIFFFieldSet(tif
,FIELD_STRIPBYTECOUNTS
))
533 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount
))
538 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount
))
542 if (TIFFFieldSet(tif
,FIELD_STRIPOFFSETS
))
546 /* td_stripoffset might be NULL in an odd OJPEG case. See
547 * tif_dirread.c around line 3634.
549 * If a) compression is OJPEG, b) it's not a tiled TIFF,
550 * and c) the number of strips is 1,
551 * then we tolerate the absence of stripoffsets tag,
552 * because, presumably, all required data is in the
553 * JpegInterchangeFormat stream.
554 * We can get here when using tiffset on such a file.
555 * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
557 if (tif
->tif_dir
.td_stripoffset
!= NULL
&&
558 !TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
563 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
567 if (TIFFFieldSet(tif
,FIELD_COLORMAP
))
569 if (!TIFFWriteDirectoryTagColormap(tif
,&ndir
,dir
))
572 if (TIFFFieldSet(tif
,FIELD_EXTRASAMPLES
))
574 if (tif
->tif_dir
.td_extrasamples
)
578 TIFFGetFieldDefaulted(tif
,TIFFTAG_EXTRASAMPLES
,&na
,&nb
);
579 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_EXTRASAMPLES
,na
,nb
))
583 if (TIFFFieldSet(tif
,FIELD_SAMPLEFORMAT
))
585 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_SAMPLEFORMAT
,tif
->tif_dir
.td_sampleformat
))
588 if (TIFFFieldSet(tif
,FIELD_SMINSAMPLEVALUE
))
590 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMINSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_sminsamplevalue
))
593 if (TIFFFieldSet(tif
,FIELD_SMAXSAMPLEVALUE
))
595 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMAXSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_smaxsamplevalue
))
598 if (TIFFFieldSet(tif
,FIELD_IMAGEDEPTH
))
600 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEDEPTH
,tif
->tif_dir
.td_imagedepth
))
603 if (TIFFFieldSet(tif
,FIELD_TILEDEPTH
))
605 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_TILEDEPTH
,tif
->tif_dir
.td_tiledepth
))
608 if (TIFFFieldSet(tif
,FIELD_HALFTONEHINTS
))
610 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_HALFTONEHINTS
,2,&tif
->tif_dir
.td_halftonehints
[0]))
613 if (TIFFFieldSet(tif
,FIELD_YCBCRSUBSAMPLING
))
615 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_YCBCRSUBSAMPLING
,2,&tif
->tif_dir
.td_ycbcrsubsampling
[0]))
618 if (TIFFFieldSet(tif
,FIELD_YCBCRPOSITIONING
))
620 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_YCBCRPOSITIONING
,tif
->tif_dir
.td_ycbcrpositioning
))
623 if (TIFFFieldSet(tif
,FIELD_REFBLACKWHITE
))
625 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,TIFFTAG_REFERENCEBLACKWHITE
,6,tif
->tif_dir
.td_refblackwhite
))
628 if (TIFFFieldSet(tif
,FIELD_TRANSFERFUNCTION
))
630 if (!TIFFWriteDirectoryTagTransferfunction(tif
,&ndir
,dir
))
633 if (TIFFFieldSet(tif
,FIELD_INKNAMES
))
635 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,TIFFTAG_INKNAMES
,tif
->tif_dir
.td_inknameslen
,tif
->tif_dir
.td_inknames
))
638 if (TIFFFieldSet(tif
,FIELD_SUBIFD
))
640 if (!TIFFWriteDirectoryTagSubifd(tif
,&ndir
,dir
))
645 for (n
=0; n
<tif
->tif_nfields
; n
++) {
647 o
= tif
->tif_fields
[n
];
648 if ((o
->field_bit
>=FIELD_CODEC
)&&(TIFFFieldSet(tif
,o
->field_bit
)))
650 switch (o
->get_field_type
)
652 case TIFF_SETGET_ASCII
:
656 assert(o
->field_type
==TIFF_ASCII
);
657 assert(o
->field_readcount
==TIFF_VARIABLE
);
658 assert(o
->field_passcount
==0);
659 TIFFGetField(tif
,o
->field_tag
,&pb
);
660 pa
=(uint32
)(strlen(pb
));
661 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,pa
,pb
))
665 case TIFF_SETGET_UINT16
:
668 assert(o
->field_type
==TIFF_SHORT
);
669 assert(o
->field_readcount
==1);
670 assert(o
->field_passcount
==0);
671 TIFFGetField(tif
,o
->field_tag
,&p
);
672 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,p
))
676 case TIFF_SETGET_UINT32
:
679 assert(o
->field_type
==TIFF_LONG
);
680 assert(o
->field_readcount
==1);
681 assert(o
->field_passcount
==0);
682 TIFFGetField(tif
,o
->field_tag
,&p
);
683 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,p
))
687 case TIFF_SETGET_C32_UINT8
:
691 assert(o
->field_type
==TIFF_UNDEFINED
);
692 assert(o
->field_readcount
==TIFF_VARIABLE2
);
693 assert(o
->field_passcount
==1);
694 TIFFGetField(tif
,o
->field_tag
,&pa
,&pb
);
695 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,pa
,pb
))
700 assert(0); /* we should never get here */
707 for (m
=0; m
<(uint32
)(tif
->tif_dir
.td_customValueCount
); m
++)
709 uint16 tag
= (uint16
)tif
->tif_dir
.td_customValues
[m
].info
->field_tag
;
710 uint32 count
= tif
->tif_dir
.td_customValues
[m
].count
;
711 switch (tif
->tif_dir
.td_customValues
[m
].info
->field_type
)
714 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
718 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
722 if (!TIFFWriteDirectoryTagByteArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
726 if (!TIFFWriteDirectoryTagSbyteArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
730 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
734 if (!TIFFWriteDirectoryTagSshortArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
738 if (!TIFFWriteDirectoryTagLongArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
742 if (!TIFFWriteDirectoryTagSlongArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
746 if (!TIFFWriteDirectoryTagLong8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
750 if (!TIFFWriteDirectoryTagSlong8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
754 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
758 if (!TIFFWriteDirectoryTagSrationalArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
762 if (!TIFFWriteDirectoryTagFloatArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
766 if (!TIFFWriteDirectoryTagDoubleArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
770 if (!TIFFWriteDirectoryTagIfdArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
774 if (!TIFFWriteDirectoryTagIfdIfd8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
778 assert(0); /* we should never get here */
784 dir
=_TIFFmalloc(ndir
*sizeof(TIFFDirEntry
));
787 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
792 if ((tif
->tif_diroff
==0)&&(!TIFFLinkDirectory(tif
)))
796 tif
->tif_diroff
=(TIFFSeekFile(tif
,0,SEEK_END
)+1)&(~((toff_t
)1));
798 *pdiroff
=tif
->tif_diroff
;
799 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
803 tif
->tif_dataoff
=tif
->tif_diroff
+dirsize
;
804 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
805 tif
->tif_dataoff
=(uint32
)tif
->tif_dataoff
;
806 if ((tif
->tif_dataoff
<tif
->tif_diroff
)||(tif
->tif_dataoff
<(uint64
)dirsize
))
808 TIFFErrorExt(tif
->tif_clientdata
,module
,"Maximum TIFF file size exceeded");
811 if (tif
->tif_dataoff
&1)
818 if (TIFFFieldSet(tif
,FIELD_SUBIFD
)&&(tif
->tif_subifdoff
==0))
822 for (na
=0, nb
=dir
; ; na
++, nb
++)
826 TIFFErrorExt(tif
->tif_clientdata
,module
,
827 "Cannot find SubIFD tag");
830 if (nb
->tdir_tag
==TIFFTAG_SUBIFD
)
833 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
834 tif
->tif_subifdoff
=tif
->tif_diroff
+2+na
*12+8;
836 tif
->tif_subifdoff
=tif
->tif_diroff
+8+na
*20+12;
839 dirmem
=_TIFFmalloc(dirsize
);
842 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
845 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
851 *(uint16
*)n
=(uint16
)ndir
;
852 if (tif
->tif_flags
&TIFF_SWAB
)
853 TIFFSwabShort((uint16
*)n
);
856 for (m
=0; m
<ndir
; m
++)
858 *(uint16
*)n
=o
->tdir_tag
;
859 if (tif
->tif_flags
&TIFF_SWAB
)
860 TIFFSwabShort((uint16
*)n
);
862 *(uint16
*)n
=o
->tdir_type
;
863 if (tif
->tif_flags
&TIFF_SWAB
)
864 TIFFSwabShort((uint16
*)n
);
866 nTmp
= (uint32
)o
->tdir_count
;
867 _TIFFmemcpy(n
,&nTmp
,4);
868 if (tif
->tif_flags
&TIFF_SWAB
)
869 TIFFSwabLong((uint32
*)n
);
871 /* This is correct. The data has been */
872 /* swabbed previously in TIFFWriteDirectoryTagData */
873 _TIFFmemcpy(n
,&o
->tdir_offset
,4);
877 nTmp
= (uint32
)tif
->tif_nextdiroff
;
878 if (tif
->tif_flags
&TIFF_SWAB
)
880 _TIFFmemcpy(n
,&nTmp
,4);
888 if (tif
->tif_flags
&TIFF_SWAB
)
889 TIFFSwabLong8((uint64
*)n
);
892 for (m
=0; m
<ndir
; m
++)
894 *(uint16
*)n
=o
->tdir_tag
;
895 if (tif
->tif_flags
&TIFF_SWAB
)
896 TIFFSwabShort((uint16
*)n
);
898 *(uint16
*)n
=o
->tdir_type
;
899 if (tif
->tif_flags
&TIFF_SWAB
)
900 TIFFSwabShort((uint16
*)n
);
902 _TIFFmemcpy(n
,&o
->tdir_count
,8);
903 if (tif
->tif_flags
&TIFF_SWAB
)
904 TIFFSwabLong8((uint64
*)n
);
906 _TIFFmemcpy(n
,&o
->tdir_offset
,8);
910 _TIFFmemcpy(n
,&tif
->tif_nextdiroff
,8);
911 if (tif
->tif_flags
&TIFF_SWAB
)
912 TIFFSwabLong8((uint64
*)n
);
916 if (!SeekOK(tif
,tif
->tif_diroff
))
918 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing directory");
921 if (!WriteOK(tif
,dirmem
,(tmsize_t
)dirsize
))
923 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing directory");
929 TIFFFreeDirectory(tif
);
930 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
931 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
932 (*tif
->tif_cleanup
)(tif
);
934 * Reset directory-related state for subsequent
937 TIFFCreateDirectory(tif
);
948 static float TIFFClampDoubleToFloat( double val
)
957 static int8
TIFFClampDoubleToInt8( double val
)
961 if( val
< -128 || val
!= val
)
966 static int16
TIFFClampDoubleToInt16( double val
)
970 if( val
< -32768 || val
!= val
)
975 static int32
TIFFClampDoubleToInt32( double val
)
977 if( val
> 0x7FFFFFFF )
979 if( val
< -0x7FFFFFFF-1 || val
!= val
)
980 return -0x7FFFFFFF-1;
984 static uint8
TIFFClampDoubleToUInt8( double val
)
988 if( val
> 255 || val
!= val
)
993 static uint16
TIFFClampDoubleToUInt16( double val
)
997 if( val
> 65535 || val
!= val
)
1002 static uint32
TIFFClampDoubleToUInt32( double val
)
1006 if( val
> 0xFFFFFFFFU
|| val
!= val
)
1012 TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1014 static const char module
[] = "TIFFWriteDirectoryTagSampleformatArray";
1018 conv
= _TIFFmalloc(count
*sizeof(double));
1021 TIFFErrorExt(tif
->tif_clientdata
, module
, "Out of memory");
1025 switch (tif
->tif_dir
.td_sampleformat
)
1027 case SAMPLEFORMAT_IEEEFP
:
1028 if (tif
->tif_dir
.td_bitspersample
<=32)
1030 for (i
= 0; i
< count
; ++i
)
1031 ((float*)conv
)[i
] = TIFFClampDoubleToFloat(value
[i
]);
1032 ok
= TIFFWriteDirectoryTagFloatArray(tif
,ndir
,dir
,tag
,count
,(float*)conv
);
1036 ok
= TIFFWriteDirectoryTagDoubleArray(tif
,ndir
,dir
,tag
,count
,value
);
1039 case SAMPLEFORMAT_INT
:
1040 if (tif
->tif_dir
.td_bitspersample
<=8)
1042 for (i
= 0; i
< count
; ++i
)
1043 ((int8
*)conv
)[i
] = TIFFClampDoubleToInt8(value
[i
]);
1044 ok
= TIFFWriteDirectoryTagSbyteArray(tif
,ndir
,dir
,tag
,count
,(int8
*)conv
);
1046 else if (tif
->tif_dir
.td_bitspersample
<=16)
1048 for (i
= 0; i
< count
; ++i
)
1049 ((int16
*)conv
)[i
] = TIFFClampDoubleToInt16(value
[i
]);
1050 ok
= TIFFWriteDirectoryTagSshortArray(tif
,ndir
,dir
,tag
,count
,(int16
*)conv
);
1054 for (i
= 0; i
< count
; ++i
)
1055 ((int32
*)conv
)[i
] = TIFFClampDoubleToInt32(value
[i
]);
1056 ok
= TIFFWriteDirectoryTagSlongArray(tif
,ndir
,dir
,tag
,count
,(int32
*)conv
);
1059 case SAMPLEFORMAT_UINT
:
1060 if (tif
->tif_dir
.td_bitspersample
<=8)
1062 for (i
= 0; i
< count
; ++i
)
1063 ((uint8
*)conv
)[i
] = TIFFClampDoubleToUInt8(value
[i
]);
1064 ok
= TIFFWriteDirectoryTagByteArray(tif
,ndir
,dir
,tag
,count
,(uint8
*)conv
);
1066 else if (tif
->tif_dir
.td_bitspersample
<=16)
1068 for (i
= 0; i
< count
; ++i
)
1069 ((uint16
*)conv
)[i
] = TIFFClampDoubleToUInt16(value
[i
]);
1070 ok
= TIFFWriteDirectoryTagShortArray(tif
,ndir
,dir
,tag
,count
,(uint16
*)conv
);
1074 for (i
= 0; i
< count
; ++i
)
1075 ((uint32
*)conv
)[i
] = TIFFClampDoubleToUInt32(value
[i
]);
1076 ok
= TIFFWriteDirectoryTagLongArray(tif
,ndir
,dir
,tag
,count
,(uint32
*)conv
);
1089 TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1091 switch (tif
->tif_dir
.td_sampleformat
)
1093 case SAMPLEFORMAT_IEEEFP
:
1094 if (tif
->tif_dir
.td_bitspersample
<=32)
1095 return(TIFFWriteDirectoryTagFloatPerSample(tif
,ndir
,dir
,tag
,(float)value
));
1097 return(TIFFWriteDirectoryTagDoublePerSample(tif
,ndir
,dir
,tag
,value
));
1098 case SAMPLEFORMAT_INT
:
1099 if (tif
->tif_dir
.td_bitspersample
<=8)
1100 return(TIFFWriteDirectoryTagSbytePerSample(tif
,ndir
,dir
,tag
,(int8
)value
));
1101 else if (tif
->tif_dir
.td_bitspersample
<=16)
1102 return(TIFFWriteDirectoryTagSshortPerSample(tif
,ndir
,dir
,tag
,(int16
)value
));
1104 return(TIFFWriteDirectoryTagSlongPerSample(tif
,ndir
,dir
,tag
,(int32
)value
));
1105 case SAMPLEFORMAT_UINT
:
1106 if (tif
->tif_dir
.td_bitspersample
<=8)
1107 return(TIFFWriteDirectoryTagBytePerSample(tif
,ndir
,dir
,tag
,(uint8
)value
));
1108 else if (tif
->tif_dir
.td_bitspersample
<=16)
1109 return(TIFFWriteDirectoryTagShortPerSample(tif
,ndir
,dir
,tag
,(uint16
)value
));
1111 return(TIFFWriteDirectoryTagLongPerSample(tif
,ndir
,dir
,tag
,(uint32
)value
));
1119 TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1126 return(TIFFWriteDirectoryTagCheckedAscii(tif
,ndir
,dir
,tag
,count
,value
));
1130 TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1137 return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif
,ndir
,dir
,tag
,count
,value
));
1142 TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1149 return(TIFFWriteDirectoryTagCheckedByte(tif
,ndir
,dir
,tag
,value
));
1154 TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1161 return(TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,count
,value
));
1166 TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1168 static const char module
[] = "TIFFWriteDirectoryTagBytePerSample";
1178 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint8
));
1181 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1184 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1186 o
=TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1194 TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1201 return(TIFFWriteDirectoryTagCheckedSbyte(tif
,ndir
,dir
,tag
,value
));
1206 TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1213 return(TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,count
,value
));
1218 TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1220 static const char module
[] = "TIFFWriteDirectoryTagSbytePerSample";
1230 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int8
));
1233 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1236 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1238 o
=TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1245 TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1252 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,value
));
1256 TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1263 return(TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,value
));
1267 TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1269 static const char module
[] = "TIFFWriteDirectoryTagShortPerSample";
1279 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint16
));
1282 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1285 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1287 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1294 TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1301 return(TIFFWriteDirectoryTagCheckedSshort(tif
,ndir
,dir
,tag
,value
));
1306 TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1313 return(TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,count
,value
));
1318 TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1320 static const char module
[] = "TIFFWriteDirectoryTagSshortPerSample";
1330 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int16
));
1333 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1336 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1338 o
=TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1345 TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1352 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1356 TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1363 return(TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,value
));
1368 TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1370 static const char module
[] = "TIFFWriteDirectoryTagLongPerSample";
1380 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint32
));
1383 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1386 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1388 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1396 TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1403 return(TIFFWriteDirectoryTagCheckedSlong(tif
,ndir
,dir
,tag
,value
));
1408 TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
1415 return(TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,count
,value
));
1420 TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1422 static const char module
[] = "TIFFWriteDirectoryTagSlongPerSample";
1432 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int32
));
1435 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1438 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1440 o
=TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1448 TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
1455 return(TIFFWriteDirectoryTagCheckedLong8(tif
,ndir
,dir
,tag
,value
));
1460 TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1467 return(TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1472 TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
1479 return(TIFFWriteDirectoryTagCheckedSlong8(tif
,ndir
,dir
,tag
,value
));
1484 TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
1491 return(TIFFWriteDirectoryTagCheckedSlong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1495 TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1502 return(TIFFWriteDirectoryTagCheckedRational(tif
,ndir
,dir
,tag
,value
));
1506 TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1513 return(TIFFWriteDirectoryTagCheckedRationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1517 TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1524 return(TIFFWriteDirectoryTagCheckedSrationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1528 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1535 return(TIFFWriteDirectoryTagCheckedFloat(tif
,ndir
,dir
,tag
,value
));
1539 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1546 return(TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,count
,value
));
1550 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1552 static const char module
[] = "TIFFWriteDirectoryTagFloatPerSample";
1562 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(float));
1565 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1568 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1570 o
=TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1577 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1584 return(TIFFWriteDirectoryTagCheckedDouble(tif
,ndir
,dir
,tag
,value
));
1588 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1595 return(TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,count
,value
));
1599 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1601 static const char module
[] = "TIFFWriteDirectoryTagDoublePerSample";
1611 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(double));
1614 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1617 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1619 o
=TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1626 TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1633 return(TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,value
));
1638 TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1645 return(TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,tag
,count
,value
));
1650 TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1658 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,(uint16
)value
));
1660 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1663 /************************************************************************/
1664 /* TIFFWriteDirectoryTagLongLong8Array() */
1666 /* Write out LONG8 array as LONG8 for BigTIFF or LONG for */
1667 /* Classic TIFF with some checking. */
1668 /************************************************************************/
1671 TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1673 static const char module
[] = "TIFFWriteDirectoryTagLongLong8Array";
1680 /* is this just a counting pass? */
1687 /* We always write LONG8 for BigTIFF, no checking needed. */
1688 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1689 return TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,
1693 ** For classic tiff we want to verify everything is in range for LONG
1694 ** and convert to long format.
1697 p
= _TIFFmalloc(count
*sizeof(uint32
));
1700 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1704 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1708 TIFFErrorExt(tif
->tif_clientdata
,module
,
1709 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1716 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1722 /************************************************************************/
1723 /* TIFFWriteDirectoryTagIfdIfd8Array() */
1725 /* Write either IFD8 or IFD array depending on file type. */
1726 /************************************************************************/
1729 TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1731 static const char module
[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1738 /* is this just a counting pass? */
1745 /* We always write IFD8 for BigTIFF, no checking needed. */
1746 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1747 return TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,
1751 ** For classic tiff we want to verify everything is in range for IFD
1752 ** and convert to long format.
1755 p
= _TIFFmalloc(count
*sizeof(uint32
));
1758 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1762 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1766 TIFFErrorExt(tif
->tif_clientdata
,module
,
1767 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1774 o
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,p
);
1782 TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1784 static const char module
[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1795 for (ma
=value
, mb
=0; mb
<count
; ma
++, mb
++)
1797 if ((n
==0)&&(*ma
>0xFFFF))
1799 if ((n
==1)&&(*ma
>0xFFFFFFFF))
1809 p
=_TIFFmalloc(count
*sizeof(uint16
));
1812 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1815 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1817 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,p
);
1824 p
=_TIFFmalloc(count
*sizeof(uint32
));
1827 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1830 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1832 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1838 o
=TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
);
1844 TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1846 static const char module
[] = "TIFFWriteDirectoryTagColormap";
1855 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1856 n
=_TIFFmalloc(3*m
*sizeof(uint16
));
1859 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1862 _TIFFmemcpy(&n
[0],tif
->tif_dir
.td_colormap
[0],m
*sizeof(uint16
));
1863 _TIFFmemcpy(&n
[m
],tif
->tif_dir
.td_colormap
[1],m
*sizeof(uint16
));
1864 _TIFFmemcpy(&n
[2*m
],tif
->tif_dir
.td_colormap
[2],m
*sizeof(uint16
));
1865 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_COLORMAP
,3*m
,n
);
1871 TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1873 static const char module
[] = "TIFFWriteDirectoryTagTransferfunction";
1883 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1884 n
=tif
->tif_dir
.td_samplesperpixel
-tif
->tif_dir
.td_extrasamples
;
1886 * Check if the table can be written as a single column,
1887 * or if it must be written as 3 columns. Note that we
1888 * write a 3-column tag if there are 2 samples/pixel and
1889 * a single column of data won't suffice--hmm.
1895 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
)))
1900 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
)))
1905 o
=_TIFFmalloc(n
*m
*sizeof(uint16
));
1908 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1911 _TIFFmemcpy(&o
[0],tif
->tif_dir
.td_transferfunction
[0],m
*sizeof(uint16
));
1913 _TIFFmemcpy(&o
[m
],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
));
1915 _TIFFmemcpy(&o
[2*m
],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
));
1916 p
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_TRANSFERFUNCTION
,n
*m
,o
);
1922 TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1924 static const char module
[] = "TIFFWriteDirectoryTagSubifd";
1927 if (tif
->tif_dir
.td_nsubifd
==0)
1935 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1941 o
=_TIFFmalloc(tif
->tif_dir
.td_nsubifd
*sizeof(uint32
));
1944 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1947 pa
=tif
->tif_dir
.td_subifd
;
1949 for (p
=0; p
< tif
->tif_dir
.td_nsubifd
; p
++)
1953 /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which is illegal) */
1954 if( *pa
> 0xFFFFFFFFUL
)
1956 TIFFErrorExt(tif
->tif_clientdata
,module
,"Illegal value for SubIFD tag");
1960 *pb
++=(uint32
)(*pa
++);
1962 n
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,o
);
1966 n
=TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,tif
->tif_dir
.td_subifd
);
1970 * Total hack: if this directory includes a SubIFD
1971 * tag then force the next <n> directories to be
1972 * written as ``sub directories'' of this one. This
1973 * is used to write things like thumbnails and
1974 * image masks that one wants to keep out of the
1975 * normal directory linkage access mechanism.
1977 tif
->tif_flags
|=TIFF_INSUBIFD
;
1978 tif
->tif_nsubifd
=tif
->tif_dir
.td_nsubifd
;
1979 if (tif
->tif_dir
.td_nsubifd
==1)
1980 tif
->tif_subifdoff
=0;
1982 tif
->tif_subifdoff
=m
;
1987 TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1989 assert(sizeof(char)==1);
1990 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_ASCII
,count
,count
,value
));
1994 TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1996 assert(sizeof(uint8
)==1);
1997 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_UNDEFINED
,count
,count
,value
));
2002 TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
2004 assert(sizeof(uint8
)==1);
2005 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,1,1,&value
));
2010 TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
2012 assert(sizeof(uint8
)==1);
2013 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,count
,count
,value
));
2018 TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
2020 assert(sizeof(int8
)==1);
2021 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,1,1,&value
));
2026 TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
2028 assert(sizeof(int8
)==1);
2029 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,count
,count
,value
));
2033 TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
2036 assert(sizeof(uint16
)==2);
2038 if (tif
->tif_flags
&TIFF_SWAB
)
2040 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,1,2,&m
));
2044 TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
2046 assert(count
<0x80000000);
2047 assert(sizeof(uint16
)==2);
2048 if (tif
->tif_flags
&TIFF_SWAB
)
2049 TIFFSwabArrayOfShort(value
,count
);
2050 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,count
,count
*2,value
));
2055 TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
2058 assert(sizeof(int16
)==2);
2060 if (tif
->tif_flags
&TIFF_SWAB
)
2061 TIFFSwabShort((uint16
*)(&m
));
2062 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,1,2,&m
));
2067 TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
2069 assert(count
<0x80000000);
2070 assert(sizeof(int16
)==2);
2071 if (tif
->tif_flags
&TIFF_SWAB
)
2072 TIFFSwabArrayOfShort((uint16
*)value
,count
);
2073 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,count
,count
*2,value
));
2077 TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
2080 assert(sizeof(uint32
)==4);
2082 if (tif
->tif_flags
&TIFF_SWAB
)
2084 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,1,4,&m
));
2088 TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2090 assert(count
<0x40000000);
2091 assert(sizeof(uint32
)==4);
2092 if (tif
->tif_flags
&TIFF_SWAB
)
2093 TIFFSwabArrayOfLong(value
,count
);
2094 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,count
,count
*4,value
));
2099 TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
2102 assert(sizeof(int32
)==4);
2104 if (tif
->tif_flags
&TIFF_SWAB
)
2105 TIFFSwabLong((uint32
*)(&m
));
2106 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,1,4,&m
));
2111 TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
2113 assert(count
<0x40000000);
2114 assert(sizeof(int32
)==4);
2115 if (tif
->tif_flags
&TIFF_SWAB
)
2116 TIFFSwabArrayOfLong((uint32
*)value
,count
);
2117 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,count
,count
*4,value
));
2122 TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
2125 assert(sizeof(uint64
)==8);
2126 if( !(tif
->tif_flags
&TIFF_BIGTIFF
) ) {
2127 TIFFErrorExt(tif
->tif_clientdata
,"TIFFWriteDirectoryTagCheckedLong8","LONG8 not allowed for ClassicTIFF");
2131 if (tif
->tif_flags
&TIFF_SWAB
)
2133 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,1,8,&m
));
2138 TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2140 assert(count
<0x20000000);
2141 assert(sizeof(uint64
)==8);
2142 if( !(tif
->tif_flags
&TIFF_BIGTIFF
) ) {
2143 TIFFErrorExt(tif
->tif_clientdata
,"TIFFWriteDirectoryTagCheckedLong8Array","LONG8 not allowed for ClassicTIFF");
2146 if (tif
->tif_flags
&TIFF_SWAB
)
2147 TIFFSwabArrayOfLong8(value
,count
);
2148 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,count
,count
*8,value
));
2153 TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
2156 assert(sizeof(int64
)==8);
2157 if( !(tif
->tif_flags
&TIFF_BIGTIFF
) ) {
2158 TIFFErrorExt(tif
->tif_clientdata
,"TIFFWriteDirectoryTagCheckedSlong8","SLONG8 not allowed for ClassicTIFF");
2162 if (tif
->tif_flags
&TIFF_SWAB
)
2163 TIFFSwabLong8((uint64
*)(&m
));
2164 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,1,8,&m
));
2169 TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
2171 assert(count
<0x20000000);
2172 assert(sizeof(int64
)==8);
2173 if( !(tif
->tif_flags
&TIFF_BIGTIFF
) ) {
2174 TIFFErrorExt(tif
->tif_clientdata
,"TIFFWriteDirectoryTagCheckedSlong8Array","SLONG8 not allowed for ClassicTIFF");
2177 if (tif
->tif_flags
&TIFF_SWAB
)
2178 TIFFSwabArrayOfLong8((uint64
*)value
,count
);
2179 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,count
,count
*8,value
));
2183 TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2185 static const char module
[] = "TIFFWriteDirectoryTagCheckedRational";
2187 assert(sizeof(uint32
)==4);
2190 TIFFErrorExt(tif
->tif_clientdata
,module
,"Negative value is illegal");
2193 else if( value
!= value
)
2195 TIFFErrorExt(tif
->tif_clientdata
,module
,"Not-a-number value is illegal");
2198 else if (value
==0.0)
2203 else if (value
<= 0xFFFFFFFFU
&& value
==(double)(uint32
)value
)
2210 m
[0]=(uint32
)(value
*0xFFFFFFFF);
2216 m
[1]=(uint32
)(0xFFFFFFFF/value
);
2218 if (tif
->tif_flags
&TIFF_SWAB
)
2220 TIFFSwabLong(&m
[0]);
2221 TIFFSwabLong(&m
[1]);
2223 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,1,8,&m
[0]));
2227 TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2229 static const char module
[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2235 assert(sizeof(uint32
)==4);
2236 m
=_TIFFmalloc(count
*2*sizeof(uint32
));
2239 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2242 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2244 if (*na
<=0.0 || *na
!= *na
)
2249 else if (*na
>= 0 && *na
<= (float)0xFFFFFFFFU
&&
2250 *na
==(float)(uint32
)(*na
))
2252 nb
[0]=(uint32
)(*na
);
2257 nb
[0]=(uint32
)((double)(*na
)*0xFFFFFFFF);
2263 nb
[1]=(uint32
)((double)0xFFFFFFFF/(*na
));
2266 if (tif
->tif_flags
&TIFF_SWAB
)
2267 TIFFSwabArrayOfLong(m
,count
*2);
2268 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,count
,count
*8,&m
[0]);
2274 TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2276 static const char module
[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2282 assert(sizeof(int32
)==4);
2283 m
=_TIFFmalloc(count
*2*sizeof(int32
));
2286 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2289 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2293 if (*na
==(int32
)(*na
))
2300 nb
[0]=-(int32
)((double)(-*na
)*0x7FFFFFFF);
2306 nb
[1]=(int32
)((double)0x7FFFFFFF/(-*na
));
2311 if (*na
==(int32
)(*na
))
2318 nb
[0]=(int32
)((double)(*na
)*0x7FFFFFFF);
2324 nb
[1]=(int32
)((double)0x7FFFFFFF/(*na
));
2328 if (tif
->tif_flags
&TIFF_SWAB
)
2329 TIFFSwabArrayOfLong((uint32
*)m
,count
*2);
2330 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SRATIONAL
,count
,count
*8,&m
[0]);
2337 TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
2340 assert(sizeof(float)==4);
2342 TIFFCvtNativeToIEEEFloat(tif
,1,&m
);
2343 if (tif
->tif_flags
&TIFF_SWAB
)
2345 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,1,4,&m
));
2350 TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2352 assert(count
<0x40000000);
2353 assert(sizeof(float)==4);
2354 TIFFCvtNativeToIEEEFloat(tif
,count
,&value
);
2355 if (tif
->tif_flags
&TIFF_SWAB
)
2356 TIFFSwabArrayOfFloat(value
,count
);
2357 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,count
,count
*4,value
));
2362 TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2365 assert(sizeof(double)==8);
2367 TIFFCvtNativeToIEEEDouble(tif
,1,&m
);
2368 if (tif
->tif_flags
&TIFF_SWAB
)
2370 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,1,8,&m
));
2375 TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
2377 assert(count
<0x20000000);
2378 assert(sizeof(double)==8);
2379 TIFFCvtNativeToIEEEDouble(tif
,count
,&value
);
2380 if (tif
->tif_flags
&TIFF_SWAB
)
2381 TIFFSwabArrayOfDouble(value
,count
);
2382 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,count
,count
*8,value
));
2386 TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2388 assert(count
<0x40000000);
2389 assert(sizeof(uint32
)==4);
2390 if (tif
->tif_flags
&TIFF_SWAB
)
2391 TIFFSwabArrayOfLong(value
,count
);
2392 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD
,count
,count
*4,value
));
2396 TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2398 assert(count
<0x20000000);
2399 assert(sizeof(uint64
)==8);
2400 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2401 if (tif
->tif_flags
&TIFF_SWAB
)
2402 TIFFSwabArrayOfLong8(value
,count
);
2403 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD8
,count
,count
*8,value
));
2407 TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
)
2409 static const char module
[] = "TIFFWriteDirectoryTagData";
2414 assert(dir
[m
].tdir_tag
!=tag
);
2415 if (dir
[m
].tdir_tag
>tag
)
2422 for (n
=*ndir
; n
>m
; n
--)
2425 dir
[m
].tdir_tag
=tag
;
2426 dir
[m
].tdir_type
=datatype
;
2427 dir
[m
].tdir_count
=count
;
2428 dir
[m
].tdir_offset
.toff_long8
= 0;
2429 if (datalength
<=((tif
->tif_flags
&TIFF_BIGTIFF
)?0x8U
:0x4U
))
2430 _TIFFmemcpy(&dir
[m
].tdir_offset
,data
,datalength
);
2434 na
=tif
->tif_dataoff
;
2436 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2438 if ((nb
<na
)||(nb
<datalength
))
2440 TIFFErrorExt(tif
->tif_clientdata
,module
,"Maximum TIFF file size exceeded");
2443 if (!SeekOK(tif
,na
))
2445 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing tag data");
2448 assert(datalength
<0x80000000UL
);
2449 if (!WriteOK(tif
,data
,(tmsize_t
)datalength
))
2451 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing tag data");
2454 tif
->tif_dataoff
=nb
;
2455 if (tif
->tif_dataoff
&1)
2457 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2461 if (tif
->tif_flags
&TIFF_SWAB
)
2463 _TIFFmemcpy(&dir
[m
].tdir_offset
,&o
,4);
2467 dir
[m
].tdir_offset
.toff_long8
= na
;
2468 if (tif
->tif_flags
&TIFF_SWAB
)
2469 TIFFSwabLong8(&dir
[m
].tdir_offset
.toff_long8
);
2477 * Link the current directory into the directory chain for the file.
2480 TIFFLinkDirectory(TIFF
* tif
)
2482 static const char module
[] = "TIFFLinkDirectory";
2484 tif
->tif_diroff
= (TIFFSeekFile(tif
,0,SEEK_END
)+1) & (~((toff_t
)1));
2489 if (tif
->tif_flags
& TIFF_INSUBIFD
)
2491 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2494 m
= (uint32
)tif
->tif_diroff
;
2495 if (tif
->tif_flags
& TIFF_SWAB
)
2497 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2498 if (!WriteOK(tif
, &m
, 4)) {
2499 TIFFErrorExt(tif
->tif_clientdata
, module
,
2500 "Error writing SubIFD directory link");
2504 * Advance to the next SubIFD or, if this is
2505 * the last one configured, revert back to the
2506 * normal directory linkage.
2508 if (--tif
->tif_nsubifd
)
2509 tif
->tif_subifdoff
+= 4;
2511 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2517 m
= tif
->tif_diroff
;
2518 if (tif
->tif_flags
& TIFF_SWAB
)
2520 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2521 if (!WriteOK(tif
, &m
, 8)) {
2522 TIFFErrorExt(tif
->tif_clientdata
, module
,
2523 "Error writing SubIFD directory link");
2527 * Advance to the next SubIFD or, if this is
2528 * the last one configured, revert back to the
2529 * normal directory linkage.
2531 if (--tif
->tif_nsubifd
)
2532 tif
->tif_subifdoff
+= 8;
2534 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2539 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2543 m
= (uint32
)(tif
->tif_diroff
);
2544 if (tif
->tif_flags
& TIFF_SWAB
)
2546 if (tif
->tif_header
.classic
.tiff_diroff
== 0) {
2548 * First directory, overwrite offset in header.
2550 tif
->tif_header
.classic
.tiff_diroff
= (uint32
) tif
->tif_diroff
;
2551 (void) TIFFSeekFile(tif
,4, SEEK_SET
);
2552 if (!WriteOK(tif
, &m
, 4)) {
2553 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2554 "Error writing TIFF header");
2560 * Not the first directory, search to the last and append.
2562 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
2567 if (!SeekOK(tif
, nextdir
) ||
2568 !ReadOK(tif
, &dircount
, 2)) {
2569 TIFFErrorExt(tif
->tif_clientdata
, module
,
2570 "Error fetching directory count");
2573 if (tif
->tif_flags
& TIFF_SWAB
)
2574 TIFFSwabShort(&dircount
);
2575 (void) TIFFSeekFile(tif
,
2576 nextdir
+2+dircount
*12, SEEK_SET
);
2577 if (!ReadOK(tif
, &nextnextdir
, 4)) {
2578 TIFFErrorExt(tif
->tif_clientdata
, module
,
2579 "Error fetching directory link");
2582 if (tif
->tif_flags
& TIFF_SWAB
)
2583 TIFFSwabLong(&nextnextdir
);
2586 (void) TIFFSeekFile(tif
,
2587 nextdir
+2+dircount
*12, SEEK_SET
);
2588 if (!WriteOK(tif
, &m
, 4)) {
2589 TIFFErrorExt(tif
->tif_clientdata
, module
,
2590 "Error writing directory link");
2595 nextdir
=nextnextdir
;
2602 m
= tif
->tif_diroff
;
2603 if (tif
->tif_flags
& TIFF_SWAB
)
2605 if (tif
->tif_header
.big
.tiff_diroff
== 0) {
2607 * First directory, overwrite offset in header.
2609 tif
->tif_header
.big
.tiff_diroff
= tif
->tif_diroff
;
2610 (void) TIFFSeekFile(tif
,8, SEEK_SET
);
2611 if (!WriteOK(tif
, &m
, 8)) {
2612 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2613 "Error writing TIFF header");
2619 * Not the first directory, search to the last and append.
2621 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
2627 if (!SeekOK(tif
, nextdir
) ||
2628 !ReadOK(tif
, &dircount64
, 8)) {
2629 TIFFErrorExt(tif
->tif_clientdata
, module
,
2630 "Error fetching directory count");
2633 if (tif
->tif_flags
& TIFF_SWAB
)
2634 TIFFSwabLong8(&dircount64
);
2635 if (dircount64
>0xFFFF)
2637 TIFFErrorExt(tif
->tif_clientdata
, module
,
2638 "Sanity check on tag count failed, likely corrupt TIFF");
2641 dircount
=(uint16
)dircount64
;
2642 (void) TIFFSeekFile(tif
,
2643 nextdir
+8+dircount
*20, SEEK_SET
);
2644 if (!ReadOK(tif
, &nextnextdir
, 8)) {
2645 TIFFErrorExt(tif
->tif_clientdata
, module
,
2646 "Error fetching directory link");
2649 if (tif
->tif_flags
& TIFF_SWAB
)
2650 TIFFSwabLong8(&nextnextdir
);
2653 (void) TIFFSeekFile(tif
,
2654 nextdir
+8+dircount
*20, SEEK_SET
);
2655 if (!WriteOK(tif
, &m
, 8)) {
2656 TIFFErrorExt(tif
->tif_clientdata
, module
,
2657 "Error writing directory link");
2662 nextdir
=nextnextdir
;
2668 /************************************************************************/
2669 /* TIFFRewriteField() */
2671 /* Rewrite a field in the directory on disk without regard to */
2672 /* updating the TIFF directory structure in memory. Currently */
2673 /* only supported for field that already exist in the on-disk */
2674 /* directory. Mainly used for updating stripoffset / */
2675 /* stripbytecount values after the directory is already on */
2678 /* Returns zero on failure, and one on success. */
2679 /************************************************************************/
2682 _TIFFRewriteField(TIFF
* tif
, uint16 tag
, TIFFDataType in_datatype
,
2683 tmsize_t count
, void* data
)
2685 static const char module
[] = "TIFFResetField";
2686 /* const TIFFField* fip = NULL; */
2689 uint8 direntry_raw
[20];
2690 uint16 entry_tag
= 0;
2691 uint16 entry_type
= 0;
2692 uint64 entry_count
= 0;
2693 uint64 entry_offset
= 0;
2694 int value_in_entry
= 0;
2696 uint8
*buf_to_write
= NULL
;
2697 TIFFDataType datatype
;
2699 /* -------------------------------------------------------------------- */
2700 /* Find field definition. */
2701 /* -------------------------------------------------------------------- */
2702 /*fip =*/ TIFFFindField(tif
, tag
, TIFF_ANY
);
2704 /* -------------------------------------------------------------------- */
2705 /* Do some checking this is a straight forward case. */
2706 /* -------------------------------------------------------------------- */
2709 TIFFErrorExt( tif
->tif_clientdata
, module
,
2710 "Memory mapped files not currently supported for this operation." );
2714 if( tif
->tif_diroff
== 0 )
2716 TIFFErrorExt( tif
->tif_clientdata
, module
,
2717 "Attempt to reset field on directory not already on disk." );
2721 /* -------------------------------------------------------------------- */
2722 /* Read the directory entry count. */
2723 /* -------------------------------------------------------------------- */
2724 if (!SeekOK(tif
, tif
->tif_diroff
)) {
2725 TIFFErrorExt(tif
->tif_clientdata
, module
,
2726 "%s: Seek error accessing TIFF directory",
2731 read_offset
= tif
->tif_diroff
;
2733 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2735 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) {
2736 TIFFErrorExt(tif
->tif_clientdata
, module
,
2737 "%s: Can not read TIFF directory count",
2741 if (tif
->tif_flags
& TIFF_SWAB
)
2742 TIFFSwabShort(&dircount
);
2747 if (!ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
2748 TIFFErrorExt(tif
->tif_clientdata
, module
,
2749 "%s: Can not read TIFF directory count",
2753 if (tif
->tif_flags
& TIFF_SWAB
)
2754 TIFFSwabLong8(&dircount64
);
2755 dircount
= (uint16
)dircount64
;
2760 /* -------------------------------------------------------------------- */
2761 /* Read through directory to find target tag. */
2762 /* -------------------------------------------------------------------- */
2763 while( dircount
> 0 )
2765 if (!ReadOK(tif
, direntry_raw
, dirsize
)) {
2766 TIFFErrorExt(tif
->tif_clientdata
, module
,
2767 "%s: Can not read TIFF directory entry.",
2772 memcpy( &entry_tag
, direntry_raw
+ 0, sizeof(uint16
) );
2773 if (tif
->tif_flags
&TIFF_SWAB
)
2774 TIFFSwabShort( &entry_tag
);
2776 if( entry_tag
== tag
)
2779 read_offset
+= dirsize
;
2782 if( entry_tag
!= tag
)
2784 TIFFErrorExt(tif
->tif_clientdata
, module
,
2785 "%s: Could not find tag %d.",
2786 tif
->tif_name
, tag
);
2790 /* -------------------------------------------------------------------- */
2791 /* Extract the type, count and offset for this entry. */
2792 /* -------------------------------------------------------------------- */
2793 memcpy( &entry_type
, direntry_raw
+ 2, sizeof(uint16
) );
2794 if (tif
->tif_flags
&TIFF_SWAB
)
2795 TIFFSwabShort( &entry_type
);
2797 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2801 memcpy( &value
, direntry_raw
+ 4, sizeof(uint32
) );
2802 if (tif
->tif_flags
&TIFF_SWAB
)
2803 TIFFSwabLong( &value
);
2804 entry_count
= value
;
2806 memcpy( &value
, direntry_raw
+ 8, sizeof(uint32
) );
2807 if (tif
->tif_flags
&TIFF_SWAB
)
2808 TIFFSwabLong( &value
);
2809 entry_offset
= value
;
2813 memcpy( &entry_count
, direntry_raw
+ 4, sizeof(uint64
) );
2814 if (tif
->tif_flags
&TIFF_SWAB
)
2815 TIFFSwabLong8( &entry_count
);
2817 memcpy( &entry_offset
, direntry_raw
+ 12, sizeof(uint64
) );
2818 if (tif
->tif_flags
&TIFF_SWAB
)
2819 TIFFSwabLong8( &entry_offset
);
2822 /* -------------------------------------------------------------------- */
2823 /* What data type do we want to write this as? */
2824 /* -------------------------------------------------------------------- */
2825 if( TIFFDataWidth(in_datatype
) == 8 && !(tif
->tif_flags
&TIFF_BIGTIFF
) )
2827 if( in_datatype
== TIFF_LONG8
)
2828 datatype
= TIFF_LONG
;
2829 else if( in_datatype
== TIFF_SLONG8
)
2830 datatype
= TIFF_SLONG
;
2831 else if( in_datatype
== TIFF_IFD8
)
2832 datatype
= TIFF_IFD
;
2834 datatype
= in_datatype
;
2837 datatype
= in_datatype
;
2839 /* -------------------------------------------------------------------- */
2840 /* Prepare buffer of actual data to write. This includes */
2841 /* swabbing as needed. */
2842 /* -------------------------------------------------------------------- */
2844 (uint8
*)_TIFFCheckMalloc(tif
, count
, TIFFDataWidth(datatype
),
2845 "for field buffer.");
2849 if( datatype
== in_datatype
)
2850 memcpy( buf_to_write
, data
, count
* TIFFDataWidth(datatype
) );
2851 else if( datatype
== TIFF_SLONG
&& in_datatype
== TIFF_SLONG8
)
2855 for( i
= 0; i
< count
; i
++ )
2857 ((int32
*) buf_to_write
)[i
] =
2858 (int32
) ((int64
*) data
)[i
];
2859 if( (int64
) ((int32
*) buf_to_write
)[i
] != ((int64
*) data
)[i
] )
2861 _TIFFfree( buf_to_write
);
2862 TIFFErrorExt( tif
->tif_clientdata
, module
,
2863 "Value exceeds 32bit range of output type." );
2868 else if( (datatype
== TIFF_LONG
&& in_datatype
== TIFF_LONG8
)
2869 || (datatype
== TIFF_IFD
&& in_datatype
== TIFF_IFD8
) )
2873 for( i
= 0; i
< count
; i
++ )
2875 ((uint32
*) buf_to_write
)[i
] =
2876 (uint32
) ((uint64
*) data
)[i
];
2877 if( (uint64
) ((uint32
*) buf_to_write
)[i
] != ((uint64
*) data
)[i
] )
2879 _TIFFfree( buf_to_write
);
2880 TIFFErrorExt( tif
->tif_clientdata
, module
,
2881 "Value exceeds 32bit range of output type." );
2887 if( TIFFDataWidth(datatype
) > 1 && (tif
->tif_flags
&TIFF_SWAB
) )
2889 if( TIFFDataWidth(datatype
) == 2 )
2890 TIFFSwabArrayOfShort( (uint16
*) buf_to_write
, count
);
2891 else if( TIFFDataWidth(datatype
) == 4 )
2892 TIFFSwabArrayOfLong( (uint32
*) buf_to_write
, count
);
2893 else if( TIFFDataWidth(datatype
) == 8 )
2894 TIFFSwabArrayOfLong8( (uint64
*) buf_to_write
, count
);
2897 /* -------------------------------------------------------------------- */
2898 /* Is this a value that fits into the directory entry? */
2899 /* -------------------------------------------------------------------- */
2900 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2902 if( TIFFDataWidth(datatype
) * count
<= 4 )
2904 entry_offset
= read_offset
+ 8;
2910 if( TIFFDataWidth(datatype
) * count
<= 8 )
2912 entry_offset
= read_offset
+ 12;
2917 /* -------------------------------------------------------------------- */
2918 /* If the tag type, and count match, then we just write it out */
2919 /* over the old values without altering the directory entry at */
2921 /* -------------------------------------------------------------------- */
2922 if( entry_count
== (uint64
)count
&& entry_type
== (uint16
) datatype
)
2924 if (!SeekOK(tif
, entry_offset
)) {
2925 _TIFFfree( buf_to_write
);
2926 TIFFErrorExt(tif
->tif_clientdata
, module
,
2927 "%s: Seek error accessing TIFF directory",
2931 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2932 _TIFFfree( buf_to_write
);
2933 TIFFErrorExt(tif
->tif_clientdata
, module
,
2934 "Error writing directory link");
2938 _TIFFfree( buf_to_write
);
2942 /* -------------------------------------------------------------------- */
2943 /* Otherwise, we write the new tag data at the end of the file. */
2944 /* -------------------------------------------------------------------- */
2945 if( !value_in_entry
)
2947 entry_offset
= TIFFSeekFile(tif
,0,SEEK_END
);
2949 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2950 _TIFFfree( buf_to_write
);
2951 TIFFErrorExt(tif
->tif_clientdata
, module
,
2952 "Error writing directory link");
2958 memcpy( &entry_offset
, buf_to_write
, count
*TIFFDataWidth(datatype
));
2961 _TIFFfree( buf_to_write
);
2964 /* -------------------------------------------------------------------- */
2965 /* Adjust the directory entry. */
2966 /* -------------------------------------------------------------------- */
2967 entry_type
= datatype
;
2968 memcpy( direntry_raw
+ 2, &entry_type
, sizeof(uint16
) );
2969 if (tif
->tif_flags
&TIFF_SWAB
)
2970 TIFFSwabShort( (uint16
*) (direntry_raw
+ 2) );
2972 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2976 value
= (uint32
) entry_count
;
2977 memcpy( direntry_raw
+ 4, &value
, sizeof(uint32
) );
2978 if (tif
->tif_flags
&TIFF_SWAB
)
2979 TIFFSwabLong( (uint32
*) (direntry_raw
+ 4) );
2981 value
= (uint32
) entry_offset
;
2982 memcpy( direntry_raw
+ 8, &value
, sizeof(uint32
) );
2983 if (tif
->tif_flags
&TIFF_SWAB
)
2984 TIFFSwabLong( (uint32
*) (direntry_raw
+ 8) );
2988 memcpy( direntry_raw
+ 4, &entry_count
, sizeof(uint64
) );
2989 if (tif
->tif_flags
&TIFF_SWAB
)
2990 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 4) );
2992 memcpy( direntry_raw
+ 12, &entry_offset
, sizeof(uint64
) );
2993 if (tif
->tif_flags
&TIFF_SWAB
)
2994 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 12) );
2997 /* -------------------------------------------------------------------- */
2998 /* Write the directory entry out to disk. */
2999 /* -------------------------------------------------------------------- */
3000 if (!SeekOK(tif
, read_offset
)) {
3001 TIFFErrorExt(tif
->tif_clientdata
, module
,
3002 "%s: Seek error accessing TIFF directory",
3007 if (!WriteOK(tif
, direntry_raw
,dirsize
))
3009 TIFFErrorExt(tif
->tif_clientdata
, module
,
3010 "%s: Can not write TIFF directory entry.",
3017 /* vim: set ts=8 sts=8 sw=8 noet: */