1 /* $Id: tif_dirwrite.c,v 1.77 2012-07-06 19:18:31 bfriesen 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 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
551 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
555 if (TIFFFieldSet(tif
,FIELD_COLORMAP
))
557 if (!TIFFWriteDirectoryTagColormap(tif
,&ndir
,dir
))
560 if (TIFFFieldSet(tif
,FIELD_EXTRASAMPLES
))
562 if (tif
->tif_dir
.td_extrasamples
)
566 TIFFGetFieldDefaulted(tif
,TIFFTAG_EXTRASAMPLES
,&na
,&nb
);
567 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_EXTRASAMPLES
,na
,nb
))
571 if (TIFFFieldSet(tif
,FIELD_SAMPLEFORMAT
))
573 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_SAMPLEFORMAT
,tif
->tif_dir
.td_sampleformat
))
576 if (TIFFFieldSet(tif
,FIELD_SMINSAMPLEVALUE
))
578 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMINSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_sminsamplevalue
))
581 if (TIFFFieldSet(tif
,FIELD_SMAXSAMPLEVALUE
))
583 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMAXSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_smaxsamplevalue
))
586 if (TIFFFieldSet(tif
,FIELD_IMAGEDEPTH
))
588 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEDEPTH
,tif
->tif_dir
.td_imagedepth
))
591 if (TIFFFieldSet(tif
,FIELD_TILEDEPTH
))
593 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_TILEDEPTH
,tif
->tif_dir
.td_tiledepth
))
596 if (TIFFFieldSet(tif
,FIELD_HALFTONEHINTS
))
598 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_HALFTONEHINTS
,2,&tif
->tif_dir
.td_halftonehints
[0]))
601 if (TIFFFieldSet(tif
,FIELD_YCBCRSUBSAMPLING
))
603 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_YCBCRSUBSAMPLING
,2,&tif
->tif_dir
.td_ycbcrsubsampling
[0]))
606 if (TIFFFieldSet(tif
,FIELD_YCBCRPOSITIONING
))
608 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_YCBCRPOSITIONING
,tif
->tif_dir
.td_ycbcrpositioning
))
611 if (TIFFFieldSet(tif
,FIELD_REFBLACKWHITE
))
613 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,TIFFTAG_REFERENCEBLACKWHITE
,6,tif
->tif_dir
.td_refblackwhite
))
616 if (TIFFFieldSet(tif
,FIELD_TRANSFERFUNCTION
))
618 if (!TIFFWriteDirectoryTagTransferfunction(tif
,&ndir
,dir
))
621 if (TIFFFieldSet(tif
,FIELD_INKNAMES
))
623 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,TIFFTAG_INKNAMES
,tif
->tif_dir
.td_inknameslen
,tif
->tif_dir
.td_inknames
))
626 if (TIFFFieldSet(tif
,FIELD_SUBIFD
))
628 if (!TIFFWriteDirectoryTagSubifd(tif
,&ndir
,dir
))
633 for (n
=0; n
<tif
->tif_nfields
; n
++) {
635 o
= tif
->tif_fields
[n
];
636 if ((o
->field_bit
>=FIELD_CODEC
)&&(TIFFFieldSet(tif
,o
->field_bit
)))
638 switch (o
->get_field_type
)
640 case TIFF_SETGET_ASCII
:
644 assert(o
->field_type
==TIFF_ASCII
);
645 assert(o
->field_readcount
==TIFF_VARIABLE
);
646 assert(o
->field_passcount
==0);
647 TIFFGetField(tif
,o
->field_tag
,&pb
);
648 pa
=(uint32
)(strlen(pb
));
649 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,o
->field_tag
,pa
,pb
))
653 case TIFF_SETGET_UINT16
:
656 assert(o
->field_type
==TIFF_SHORT
);
657 assert(o
->field_readcount
==1);
658 assert(o
->field_passcount
==0);
659 TIFFGetField(tif
,o
->field_tag
,&p
);
660 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,o
->field_tag
,p
))
664 case TIFF_SETGET_UINT32
:
667 assert(o
->field_type
==TIFF_LONG
);
668 assert(o
->field_readcount
==1);
669 assert(o
->field_passcount
==0);
670 TIFFGetField(tif
,o
->field_tag
,&p
);
671 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,o
->field_tag
,p
))
675 case TIFF_SETGET_C32_UINT8
:
679 assert(o
->field_type
==TIFF_UNDEFINED
);
680 assert(o
->field_readcount
==TIFF_VARIABLE2
);
681 assert(o
->field_passcount
==1);
682 TIFFGetField(tif
,o
->field_tag
,&pa
,&pb
);
683 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,o
->field_tag
,pa
,pb
))
688 assert(0); /* we should never get here */
695 for (m
=0; m
<(uint32
)(tif
->tif_dir
.td_customValueCount
); m
++)
697 switch (tif
->tif_dir
.td_customValues
[m
].info
->field_type
)
700 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
704 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
708 if (!TIFFWriteDirectoryTagByteArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
712 if (!TIFFWriteDirectoryTagSbyteArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
716 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
720 if (!TIFFWriteDirectoryTagSshortArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
724 if (!TIFFWriteDirectoryTagLongArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
728 if (!TIFFWriteDirectoryTagSlongArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
732 if (!TIFFWriteDirectoryTagLong8Array(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
736 if (!TIFFWriteDirectoryTagSlong8Array(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
740 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
744 if (!TIFFWriteDirectoryTagSrationalArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
748 if (!TIFFWriteDirectoryTagFloatArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
752 if (!TIFFWriteDirectoryTagDoubleArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
756 if (!TIFFWriteDirectoryTagIfdArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
760 if (!TIFFWriteDirectoryTagIfdIfd8Array(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
764 assert(0); /* we should never get here */
770 dir
=_TIFFmalloc(ndir
*sizeof(TIFFDirEntry
));
773 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
778 if ((tif
->tif_diroff
==0)&&(!TIFFLinkDirectory(tif
)))
782 tif
->tif_diroff
=(TIFFSeekFile(tif
,0,SEEK_END
)+1)&(~1);
784 *pdiroff
=tif
->tif_diroff
;
785 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
789 tif
->tif_dataoff
=tif
->tif_diroff
+dirsize
;
790 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
791 tif
->tif_dataoff
=(uint32
)tif
->tif_dataoff
;
792 if ((tif
->tif_dataoff
<tif
->tif_diroff
)||(tif
->tif_dataoff
<(uint64
)dirsize
))
794 TIFFErrorExt(tif
->tif_clientdata
,module
,"Maximum TIFF file size exceeded");
797 if (tif
->tif_dataoff
&1)
804 if (TIFFFieldSet(tif
,FIELD_SUBIFD
)&&(tif
->tif_subifdoff
==0))
808 for (na
=0, nb
=dir
; ; na
++, nb
++)
811 if (nb
->tdir_tag
==TIFFTAG_SUBIFD
)
814 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
815 tif
->tif_subifdoff
=tif
->tif_diroff
+2+na
*12+8;
817 tif
->tif_subifdoff
=tif
->tif_diroff
+8+na
*20+12;
820 dirmem
=_TIFFmalloc(dirsize
);
823 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
826 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
833 if (tif
->tif_flags
&TIFF_SWAB
)
834 TIFFSwabShort((uint16
*)n
);
837 for (m
=0; m
<ndir
; m
++)
839 *(uint16
*)n
=o
->tdir_tag
;
840 if (tif
->tif_flags
&TIFF_SWAB
)
841 TIFFSwabShort((uint16
*)n
);
843 *(uint16
*)n
=o
->tdir_type
;
844 if (tif
->tif_flags
&TIFF_SWAB
)
845 TIFFSwabShort((uint16
*)n
);
847 nTmp
= (uint32
)o
->tdir_count
;
848 _TIFFmemcpy(n
,&nTmp
,4);
849 if (tif
->tif_flags
&TIFF_SWAB
)
850 TIFFSwabLong((uint32
*)n
);
852 /* This is correct. The data has been */
853 /* swabbed previously in TIFFWriteDirectoryTagData */
854 _TIFFmemcpy(n
,&o
->tdir_offset
,4);
858 nTmp
= (uint32
)tif
->tif_nextdiroff
;
859 if (tif
->tif_flags
&TIFF_SWAB
)
861 _TIFFmemcpy(n
,&nTmp
,4);
869 if (tif
->tif_flags
&TIFF_SWAB
)
870 TIFFSwabLong8((uint64
*)n
);
873 for (m
=0; m
<ndir
; m
++)
875 *(uint16
*)n
=o
->tdir_tag
;
876 if (tif
->tif_flags
&TIFF_SWAB
)
877 TIFFSwabShort((uint16
*)n
);
879 *(uint16
*)n
=o
->tdir_type
;
880 if (tif
->tif_flags
&TIFF_SWAB
)
881 TIFFSwabShort((uint16
*)n
);
883 _TIFFmemcpy(n
,&o
->tdir_count
,8);
884 if (tif
->tif_flags
&TIFF_SWAB
)
885 TIFFSwabLong8((uint64
*)n
);
887 _TIFFmemcpy(n
,&o
->tdir_offset
,8);
891 _TIFFmemcpy(n
,&tif
->tif_nextdiroff
,8);
892 if (tif
->tif_flags
&TIFF_SWAB
)
893 TIFFSwabLong8((uint64
*)n
);
897 if (!SeekOK(tif
,tif
->tif_diroff
))
899 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing directory");
902 if (!WriteOK(tif
,dirmem
,(tmsize_t
)dirsize
))
904 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing directory");
910 TIFFFreeDirectory(tif
);
911 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
912 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
913 (*tif
->tif_cleanup
)(tif
);
915 * Reset directory-related state for subsequent
918 TIFFCreateDirectory(tif
);
930 TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
932 static const char module
[] = "TIFFWriteDirectoryTagSampleformatArray";
936 conv
= _TIFFmalloc(count
*sizeof(double));
939 TIFFErrorExt(tif
->tif_clientdata
, module
, "Out of memory");
943 switch (tif
->tif_dir
.td_sampleformat
)
945 case SAMPLEFORMAT_IEEEFP
:
946 if (tif
->tif_dir
.td_bitspersample
<=32)
948 for (i
= 0; i
< count
; ++i
)
949 ((float*)conv
)[i
] = (float)value
[i
];
950 ok
= TIFFWriteDirectoryTagFloatArray(tif
,ndir
,dir
,tag
,count
,(float*)conv
);
954 ok
= TIFFWriteDirectoryTagDoubleArray(tif
,ndir
,dir
,tag
,count
,value
);
957 case SAMPLEFORMAT_INT
:
958 if (tif
->tif_dir
.td_bitspersample
<=8)
960 for (i
= 0; i
< count
; ++i
)
961 ((int8
*)conv
)[i
] = (int8
)value
[i
];
962 ok
= TIFFWriteDirectoryTagSbyteArray(tif
,ndir
,dir
,tag
,count
,(int8
*)conv
);
964 else if (tif
->tif_dir
.td_bitspersample
<=16)
966 for (i
= 0; i
< count
; ++i
)
967 ((int16
*)conv
)[i
] = (int16
)value
[i
];
968 ok
= TIFFWriteDirectoryTagSshortArray(tif
,ndir
,dir
,tag
,count
,(int16
*)conv
);
972 for (i
= 0; i
< count
; ++i
)
973 ((int32
*)conv
)[i
] = (int32
)value
[i
];
974 ok
= TIFFWriteDirectoryTagSlongArray(tif
,ndir
,dir
,tag
,count
,(int32
*)conv
);
977 case SAMPLEFORMAT_UINT
:
978 if (tif
->tif_dir
.td_bitspersample
<=8)
980 for (i
= 0; i
< count
; ++i
)
981 ((uint8
*)conv
)[i
] = (uint8
)value
[i
];
982 ok
= TIFFWriteDirectoryTagByteArray(tif
,ndir
,dir
,tag
,count
,(uint8
*)conv
);
984 else if (tif
->tif_dir
.td_bitspersample
<=16)
986 for (i
= 0; i
< count
; ++i
)
987 ((uint16
*)conv
)[i
] = (uint16
)value
[i
];
988 ok
= TIFFWriteDirectoryTagShortArray(tif
,ndir
,dir
,tag
,count
,(uint16
*)conv
);
992 for (i
= 0; i
< count
; ++i
)
993 ((uint32
*)conv
)[i
] = (uint32
)value
[i
];
994 ok
= TIFFWriteDirectoryTagLongArray(tif
,ndir
,dir
,tag
,count
,(uint32
*)conv
);
1007 TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1009 switch (tif
->tif_dir
.td_sampleformat
)
1011 case SAMPLEFORMAT_IEEEFP
:
1012 if (tif
->tif_dir
.td_bitspersample
<=32)
1013 return(TIFFWriteDirectoryTagFloatPerSample(tif
,ndir
,dir
,tag
,(float)value
));
1015 return(TIFFWriteDirectoryTagDoublePerSample(tif
,ndir
,dir
,tag
,value
));
1016 case SAMPLEFORMAT_INT
:
1017 if (tif
->tif_dir
.td_bitspersample
<=8)
1018 return(TIFFWriteDirectoryTagSbytePerSample(tif
,ndir
,dir
,tag
,(int8
)value
));
1019 else if (tif
->tif_dir
.td_bitspersample
<=16)
1020 return(TIFFWriteDirectoryTagSshortPerSample(tif
,ndir
,dir
,tag
,(int16
)value
));
1022 return(TIFFWriteDirectoryTagSlongPerSample(tif
,ndir
,dir
,tag
,(int32
)value
));
1023 case SAMPLEFORMAT_UINT
:
1024 if (tif
->tif_dir
.td_bitspersample
<=8)
1025 return(TIFFWriteDirectoryTagBytePerSample(tif
,ndir
,dir
,tag
,(uint8
)value
));
1026 else if (tif
->tif_dir
.td_bitspersample
<=16)
1027 return(TIFFWriteDirectoryTagShortPerSample(tif
,ndir
,dir
,tag
,(uint16
)value
));
1029 return(TIFFWriteDirectoryTagLongPerSample(tif
,ndir
,dir
,tag
,(uint32
)value
));
1037 TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1044 return(TIFFWriteDirectoryTagCheckedAscii(tif
,ndir
,dir
,tag
,count
,value
));
1048 TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1055 return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif
,ndir
,dir
,tag
,count
,value
));
1060 TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1067 return(TIFFWriteDirectoryTagCheckedByte(tif
,ndir
,dir
,tag
,value
));
1072 TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1079 return(TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,count
,value
));
1084 TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1086 static const char module
[] = "TIFFWriteDirectoryTagBytePerSample";
1096 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint8
));
1099 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1102 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1104 o
=TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1112 TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1119 return(TIFFWriteDirectoryTagCheckedSbyte(tif
,ndir
,dir
,tag
,value
));
1124 TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1131 return(TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,count
,value
));
1136 TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1138 static const char module
[] = "TIFFWriteDirectoryTagSbytePerSample";
1148 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int8
));
1151 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1154 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1156 o
=TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1163 TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1170 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,value
));
1174 TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1181 return(TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,value
));
1185 TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1187 static const char module
[] = "TIFFWriteDirectoryTagShortPerSample";
1197 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint16
));
1200 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1203 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1205 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1212 TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1219 return(TIFFWriteDirectoryTagCheckedSshort(tif
,ndir
,dir
,tag
,value
));
1224 TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1231 return(TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,count
,value
));
1236 TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1238 static const char module
[] = "TIFFWriteDirectoryTagSshortPerSample";
1248 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int16
));
1251 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1254 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1256 o
=TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1263 TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1270 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1274 TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1281 return(TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,value
));
1286 TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1288 static const char module
[] = "TIFFWriteDirectoryTagLongPerSample";
1298 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint32
));
1301 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1304 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1306 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1314 TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1321 return(TIFFWriteDirectoryTagCheckedSlong(tif
,ndir
,dir
,tag
,value
));
1326 TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
1333 return(TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,count
,value
));
1338 TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1340 static const char module
[] = "TIFFWriteDirectoryTagSlongPerSample";
1350 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int32
));
1353 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1356 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1358 o
=TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1366 TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
1373 return(TIFFWriteDirectoryTagCheckedLong8(tif
,ndir
,dir
,tag
,value
));
1378 TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1385 return(TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1390 TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
1397 return(TIFFWriteDirectoryTagCheckedSlong8(tif
,ndir
,dir
,tag
,value
));
1402 TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
1409 return(TIFFWriteDirectoryTagCheckedSlong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1413 TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1420 return(TIFFWriteDirectoryTagCheckedRational(tif
,ndir
,dir
,tag
,value
));
1424 TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1431 return(TIFFWriteDirectoryTagCheckedRationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1435 TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1442 return(TIFFWriteDirectoryTagCheckedSrationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1446 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1453 return(TIFFWriteDirectoryTagCheckedFloat(tif
,ndir
,dir
,tag
,value
));
1457 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1464 return(TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,count
,value
));
1468 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1470 static const char module
[] = "TIFFWriteDirectoryTagFloatPerSample";
1480 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(float));
1483 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1486 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1488 o
=TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1495 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1502 return(TIFFWriteDirectoryTagCheckedDouble(tif
,ndir
,dir
,tag
,value
));
1506 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1513 return(TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,count
,value
));
1517 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1519 static const char module
[] = "TIFFWriteDirectoryTagDoublePerSample";
1529 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(double));
1532 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1535 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1537 o
=TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1544 TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1551 return(TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,value
));
1556 TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1563 return(TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,tag
,count
,value
));
1568 TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1576 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,(uint16
)value
));
1578 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1581 /************************************************************************/
1582 /* TIFFWriteDirectoryTagLongLong8Array() */
1584 /* Write out LONG8 array as LONG8 for BigTIFF or LONG for */
1585 /* Classic TIFF with some checking. */
1586 /************************************************************************/
1589 TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1591 static const char module
[] = "TIFFWriteDirectoryTagLongLong8Array";
1598 /* is this just a counting pass? */
1605 /* We always write LONG8 for BigTIFF, no checking needed. */
1606 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1607 return TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,
1611 ** For classic tiff we want to verify everything is in range for LONG
1612 ** and convert to long format.
1615 p
= _TIFFmalloc(count
*sizeof(uint32
));
1618 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1622 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1626 TIFFErrorExt(tif
->tif_clientdata
,module
,
1627 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1634 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1640 /************************************************************************/
1641 /* TIFFWriteDirectoryTagIfdIfd8Array() */
1643 /* Write either IFD8 or IFD array depending on file type. */
1644 /************************************************************************/
1647 TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1649 static const char module
[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1656 /* is this just a counting pass? */
1663 /* We always write IFD8 for BigTIFF, no checking needed. */
1664 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1665 return TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,
1669 ** For classic tiff we want to verify everything is in range for IFD
1670 ** and convert to long format.
1673 p
= _TIFFmalloc(count
*sizeof(uint32
));
1676 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1680 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1684 TIFFErrorExt(tif
->tif_clientdata
,module
,
1685 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1692 o
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,p
);
1700 TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1702 static const char module
[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1713 for (ma
=value
, mb
=0; mb
<count
; ma
++, mb
++)
1715 if ((n
==0)&&(*ma
>0xFFFF))
1717 if ((n
==1)&&(*ma
>0xFFFFFFFF))
1727 p
=_TIFFmalloc(count
*sizeof(uint16
));
1730 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1733 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1735 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,p
);
1742 p
=_TIFFmalloc(count
*sizeof(uint32
));
1745 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1748 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1750 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1756 o
=TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
);
1762 TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1764 static const char module
[] = "TIFFWriteDirectoryTagColormap";
1773 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1774 n
=_TIFFmalloc(3*m
*sizeof(uint16
));
1777 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1780 _TIFFmemcpy(&n
[0],tif
->tif_dir
.td_colormap
[0],m
*sizeof(uint16
));
1781 _TIFFmemcpy(&n
[m
],tif
->tif_dir
.td_colormap
[1],m
*sizeof(uint16
));
1782 _TIFFmemcpy(&n
[2*m
],tif
->tif_dir
.td_colormap
[2],m
*sizeof(uint16
));
1783 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_COLORMAP
,3*m
,n
);
1789 TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1791 static const char module
[] = "TIFFWriteDirectoryTagTransferfunction";
1801 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1802 n
=tif
->tif_dir
.td_samplesperpixel
-tif
->tif_dir
.td_extrasamples
;
1804 * Check if the table can be written as a single column,
1805 * or if it must be written as 3 columns. Note that we
1806 * write a 3-column tag if there are 2 samples/pixel and
1807 * a single column of data won't suffice--hmm.
1813 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
)))
1818 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
)))
1823 o
=_TIFFmalloc(n
*m
*sizeof(uint16
));
1826 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1829 _TIFFmemcpy(&o
[0],tif
->tif_dir
.td_transferfunction
[0],m
*sizeof(uint16
));
1831 _TIFFmemcpy(&o
[m
],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
));
1833 _TIFFmemcpy(&o
[2*m
],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
));
1834 p
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_TRANSFERFUNCTION
,n
*m
,o
);
1840 TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1842 static const char module
[] = "TIFFWriteDirectoryTagSubifd";
1845 if (tif
->tif_dir
.td_nsubifd
==0)
1853 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1859 o
=_TIFFmalloc(tif
->tif_dir
.td_nsubifd
*sizeof(uint32
));
1862 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1865 pa
=tif
->tif_dir
.td_subifd
;
1867 for (p
=0; p
< tif
->tif_dir
.td_nsubifd
; p
++)
1870 assert(*pa
<= 0xFFFFFFFFUL
);
1871 *pb
++=(uint32
)(*pa
++);
1873 n
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,o
);
1877 n
=TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,tif
->tif_dir
.td_subifd
);
1881 * Total hack: if this directory includes a SubIFD
1882 * tag then force the next <n> directories to be
1883 * written as ``sub directories'' of this one. This
1884 * is used to write things like thumbnails and
1885 * image masks that one wants to keep out of the
1886 * normal directory linkage access mechanism.
1888 tif
->tif_flags
|=TIFF_INSUBIFD
;
1889 tif
->tif_nsubifd
=tif
->tif_dir
.td_nsubifd
;
1890 if (tif
->tif_dir
.td_nsubifd
==1)
1891 tif
->tif_subifdoff
=0;
1893 tif
->tif_subifdoff
=m
;
1898 TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1900 assert(sizeof(char)==1);
1901 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_ASCII
,count
,count
,value
));
1905 TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1907 assert(sizeof(uint8
)==1);
1908 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_UNDEFINED
,count
,count
,value
));
1913 TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1915 assert(sizeof(uint8
)==1);
1916 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,1,1,&value
));
1921 TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1923 assert(sizeof(uint8
)==1);
1924 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,count
,count
,value
));
1929 TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1931 assert(sizeof(int8
)==1);
1932 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,1,1,&value
));
1937 TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1939 assert(sizeof(int8
)==1);
1940 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,count
,count
,value
));
1944 TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1947 assert(sizeof(uint16
)==2);
1949 if (tif
->tif_flags
&TIFF_SWAB
)
1951 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,1,2,&m
));
1955 TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1957 assert(count
<0x80000000);
1958 assert(sizeof(uint16
)==2);
1959 if (tif
->tif_flags
&TIFF_SWAB
)
1960 TIFFSwabArrayOfShort(value
,count
);
1961 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,count
,count
*2,value
));
1966 TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1969 assert(sizeof(int16
)==2);
1971 if (tif
->tif_flags
&TIFF_SWAB
)
1972 TIFFSwabShort((uint16
*)(&m
));
1973 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,1,2,&m
));
1978 TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1980 assert(count
<0x80000000);
1981 assert(sizeof(int16
)==2);
1982 if (tif
->tif_flags
&TIFF_SWAB
)
1983 TIFFSwabArrayOfShort((uint16
*)value
,count
);
1984 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,count
,count
*2,value
));
1988 TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1991 assert(sizeof(uint32
)==4);
1993 if (tif
->tif_flags
&TIFF_SWAB
)
1995 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,1,4,&m
));
1999 TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2001 assert(count
<0x40000000);
2002 assert(sizeof(uint32
)==4);
2003 if (tif
->tif_flags
&TIFF_SWAB
)
2004 TIFFSwabArrayOfLong(value
,count
);
2005 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,count
,count
*4,value
));
2010 TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
2013 assert(sizeof(int32
)==4);
2015 if (tif
->tif_flags
&TIFF_SWAB
)
2016 TIFFSwabLong((uint32
*)(&m
));
2017 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,1,4,&m
));
2022 TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
2024 assert(count
<0x40000000);
2025 assert(sizeof(int32
)==4);
2026 if (tif
->tif_flags
&TIFF_SWAB
)
2027 TIFFSwabArrayOfLong((uint32
*)value
,count
);
2028 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,count
,count
*4,value
));
2033 TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
2036 assert(sizeof(uint64
)==8);
2037 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2039 if (tif
->tif_flags
&TIFF_SWAB
)
2041 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,1,8,&m
));
2046 TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2048 assert(count
<0x20000000);
2049 assert(sizeof(uint64
)==8);
2050 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2051 if (tif
->tif_flags
&TIFF_SWAB
)
2052 TIFFSwabArrayOfLong8(value
,count
);
2053 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,count
,count
*8,value
));
2058 TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
2061 assert(sizeof(int64
)==8);
2062 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2064 if (tif
->tif_flags
&TIFF_SWAB
)
2065 TIFFSwabLong8((uint64
*)(&m
));
2066 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,1,8,&m
));
2071 TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
2073 assert(count
<0x20000000);
2074 assert(sizeof(int64
)==8);
2075 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2076 if (tif
->tif_flags
&TIFF_SWAB
)
2077 TIFFSwabArrayOfLong8((uint64
*)value
,count
);
2078 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,count
,count
*8,value
));
2082 TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2086 assert(sizeof(uint32
)==4);
2092 else if (value
==(double)(uint32
)value
)
2099 m
[0]=(uint32
)(value
*0xFFFFFFFF);
2105 m
[1]=(uint32
)(0xFFFFFFFF/value
);
2107 if (tif
->tif_flags
&TIFF_SWAB
)
2109 TIFFSwabLong(&m
[0]);
2110 TIFFSwabLong(&m
[1]);
2112 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,1,8,&m
[0]));
2116 TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2118 static const char module
[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2124 assert(sizeof(uint32
)==4);
2125 m
=_TIFFmalloc(count
*2*sizeof(uint32
));
2128 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2131 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2138 else if (*na
==(float)(uint32
)(*na
))
2140 nb
[0]=(uint32
)(*na
);
2145 nb
[0]=(uint32
)((*na
)*0xFFFFFFFF);
2151 nb
[1]=(uint32
)(0xFFFFFFFF/(*na
));
2154 if (tif
->tif_flags
&TIFF_SWAB
)
2155 TIFFSwabArrayOfLong(m
,count
*2);
2156 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,count
,count
*8,&m
[0]);
2162 TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2164 static const char module
[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2170 assert(sizeof(int32
)==4);
2171 m
=_TIFFmalloc(count
*2*sizeof(int32
));
2174 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2177 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2181 if (*na
==(int32
)(*na
))
2188 nb
[0]=-(int32
)((-*na
)*0x7FFFFFFF);
2194 nb
[1]=(int32
)(0x7FFFFFFF/(-*na
));
2199 if (*na
==(int32
)(*na
))
2206 nb
[0]=(int32
)((*na
)*0x7FFFFFFF);
2212 nb
[1]=(int32
)(0x7FFFFFFF/(*na
));
2216 if (tif
->tif_flags
&TIFF_SWAB
)
2217 TIFFSwabArrayOfLong((uint32
*)m
,count
*2);
2218 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SRATIONAL
,count
,count
*8,&m
[0]);
2225 TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
2228 assert(sizeof(float)==4);
2230 TIFFCvtNativeToIEEEFloat(tif
,1,&m
);
2231 if (tif
->tif_flags
&TIFF_SWAB
)
2233 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,1,4,&m
));
2238 TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2240 assert(count
<0x40000000);
2241 assert(sizeof(float)==4);
2242 TIFFCvtNativeToIEEEFloat(tif
,count
,&value
);
2243 if (tif
->tif_flags
&TIFF_SWAB
)
2244 TIFFSwabArrayOfFloat(value
,count
);
2245 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,count
,count
*4,value
));
2250 TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2253 assert(sizeof(double)==8);
2255 TIFFCvtNativeToIEEEDouble(tif
,1,&m
);
2256 if (tif
->tif_flags
&TIFF_SWAB
)
2258 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,1,8,&m
));
2263 TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
2265 assert(count
<0x20000000);
2266 assert(sizeof(double)==8);
2267 TIFFCvtNativeToIEEEDouble(tif
,count
,&value
);
2268 if (tif
->tif_flags
&TIFF_SWAB
)
2269 TIFFSwabArrayOfDouble(value
,count
);
2270 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,count
,count
*8,value
));
2274 TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2276 assert(count
<0x40000000);
2277 assert(sizeof(uint32
)==4);
2278 if (tif
->tif_flags
&TIFF_SWAB
)
2279 TIFFSwabArrayOfLong(value
,count
);
2280 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD
,count
,count
*4,value
));
2284 TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2286 assert(count
<0x20000000);
2287 assert(sizeof(uint64
)==8);
2288 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2289 if (tif
->tif_flags
&TIFF_SWAB
)
2290 TIFFSwabArrayOfLong8(value
,count
);
2291 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD8
,count
,count
*8,value
));
2295 TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
)
2297 static const char module
[] = "TIFFWriteDirectoryTagData";
2302 assert(dir
[m
].tdir_tag
!=tag
);
2303 if (dir
[m
].tdir_tag
>tag
)
2310 for (n
=*ndir
; n
>m
; n
--)
2313 dir
[m
].tdir_tag
=tag
;
2314 dir
[m
].tdir_type
=datatype
;
2315 dir
[m
].tdir_count
=count
;
2316 dir
[m
].tdir_offset
.toff_long8
= 0;
2317 if (datalength
<=((tif
->tif_flags
&TIFF_BIGTIFF
)?0x8U
:0x4U
))
2318 _TIFFmemcpy(&dir
[m
].tdir_offset
,data
,datalength
);
2322 na
=tif
->tif_dataoff
;
2324 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2326 if ((nb
<na
)||(nb
<datalength
))
2328 TIFFErrorExt(tif
->tif_clientdata
,module
,"Maximum TIFF file size exceeded");
2331 if (!SeekOK(tif
,na
))
2333 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing tag data");
2336 assert(datalength
<0x80000000UL
);
2337 if (!WriteOK(tif
,data
,(tmsize_t
)datalength
))
2339 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing tag data");
2342 tif
->tif_dataoff
=nb
;
2343 if (tif
->tif_dataoff
&1)
2345 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2349 if (tif
->tif_flags
&TIFF_SWAB
)
2351 _TIFFmemcpy(&dir
[m
].tdir_offset
,&o
,4);
2355 dir
[m
].tdir_offset
.toff_long8
= na
;
2356 if (tif
->tif_flags
&TIFF_SWAB
)
2357 TIFFSwabLong8(&dir
[m
].tdir_offset
.toff_long8
);
2365 * Link the current directory into the directory chain for the file.
2368 TIFFLinkDirectory(TIFF
* tif
)
2370 static const char module
[] = "TIFFLinkDirectory";
2372 tif
->tif_diroff
= (TIFFSeekFile(tif
,0,SEEK_END
)+1) &~ 1;
2377 if (tif
->tif_flags
& TIFF_INSUBIFD
)
2379 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2382 m
= (uint32
)tif
->tif_diroff
;
2383 if (tif
->tif_flags
& TIFF_SWAB
)
2385 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2386 if (!WriteOK(tif
, &m
, 4)) {
2387 TIFFErrorExt(tif
->tif_clientdata
, module
,
2388 "Error writing SubIFD directory link");
2392 * Advance to the next SubIFD or, if this is
2393 * the last one configured, revert back to the
2394 * normal directory linkage.
2396 if (--tif
->tif_nsubifd
)
2397 tif
->tif_subifdoff
+= 4;
2399 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2405 m
= tif
->tif_diroff
;
2406 if (tif
->tif_flags
& TIFF_SWAB
)
2408 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2409 if (!WriteOK(tif
, &m
, 8)) {
2410 TIFFErrorExt(tif
->tif_clientdata
, module
,
2411 "Error writing SubIFD directory link");
2415 * Advance to the next SubIFD or, if this is
2416 * the last one configured, revert back to the
2417 * normal directory linkage.
2419 if (--tif
->tif_nsubifd
)
2420 tif
->tif_subifdoff
+= 8;
2422 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2427 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2431 m
= (uint32
)(tif
->tif_diroff
);
2432 if (tif
->tif_flags
& TIFF_SWAB
)
2434 if (tif
->tif_header
.classic
.tiff_diroff
== 0) {
2436 * First directory, overwrite offset in header.
2438 tif
->tif_header
.classic
.tiff_diroff
= (uint32
) tif
->tif_diroff
;
2439 (void) TIFFSeekFile(tif
,4, SEEK_SET
);
2440 if (!WriteOK(tif
, &m
, 4)) {
2441 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2442 "Error writing TIFF header");
2448 * Not the first directory, search to the last and append.
2450 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
2455 if (!SeekOK(tif
, nextdir
) ||
2456 !ReadOK(tif
, &dircount
, 2)) {
2457 TIFFErrorExt(tif
->tif_clientdata
, module
,
2458 "Error fetching directory count");
2461 if (tif
->tif_flags
& TIFF_SWAB
)
2462 TIFFSwabShort(&dircount
);
2463 (void) TIFFSeekFile(tif
,
2464 nextdir
+2+dircount
*12, SEEK_SET
);
2465 if (!ReadOK(tif
, &nextnextdir
, 4)) {
2466 TIFFErrorExt(tif
->tif_clientdata
, module
,
2467 "Error fetching directory link");
2470 if (tif
->tif_flags
& TIFF_SWAB
)
2471 TIFFSwabLong(&nextnextdir
);
2474 (void) TIFFSeekFile(tif
,
2475 nextdir
+2+dircount
*12, SEEK_SET
);
2476 if (!WriteOK(tif
, &m
, 4)) {
2477 TIFFErrorExt(tif
->tif_clientdata
, module
,
2478 "Error writing directory link");
2483 nextdir
=nextnextdir
;
2490 m
= tif
->tif_diroff
;
2491 if (tif
->tif_flags
& TIFF_SWAB
)
2493 if (tif
->tif_header
.big
.tiff_diroff
== 0) {
2495 * First directory, overwrite offset in header.
2497 tif
->tif_header
.big
.tiff_diroff
= tif
->tif_diroff
;
2498 (void) TIFFSeekFile(tif
,8, SEEK_SET
);
2499 if (!WriteOK(tif
, &m
, 8)) {
2500 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2501 "Error writing TIFF header");
2507 * Not the first directory, search to the last and append.
2509 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
2515 if (!SeekOK(tif
, nextdir
) ||
2516 !ReadOK(tif
, &dircount64
, 8)) {
2517 TIFFErrorExt(tif
->tif_clientdata
, module
,
2518 "Error fetching directory count");
2521 if (tif
->tif_flags
& TIFF_SWAB
)
2522 TIFFSwabLong8(&dircount64
);
2523 if (dircount64
>0xFFFF)
2525 TIFFErrorExt(tif
->tif_clientdata
, module
,
2526 "Sanity check on tag count failed, likely corrupt TIFF");
2529 dircount
=(uint16
)dircount64
;
2530 (void) TIFFSeekFile(tif
,
2531 nextdir
+8+dircount
*20, SEEK_SET
);
2532 if (!ReadOK(tif
, &nextnextdir
, 8)) {
2533 TIFFErrorExt(tif
->tif_clientdata
, module
,
2534 "Error fetching directory link");
2537 if (tif
->tif_flags
& TIFF_SWAB
)
2538 TIFFSwabLong8(&nextnextdir
);
2541 (void) TIFFSeekFile(tif
,
2542 nextdir
+8+dircount
*20, SEEK_SET
);
2543 if (!WriteOK(tif
, &m
, 8)) {
2544 TIFFErrorExt(tif
->tif_clientdata
, module
,
2545 "Error writing directory link");
2550 nextdir
=nextnextdir
;
2556 /************************************************************************/
2557 /* TIFFRewriteField() */
2559 /* Rewrite a field in the directory on disk without regard to */
2560 /* updating the TIFF directory structure in memory. Currently */
2561 /* only supported for field that already exist in the on-disk */
2562 /* directory. Mainly used for updating stripoffset / */
2563 /* stripbytecount values after the directory is already on */
2566 /* Returns zero on failure, and one on success. */
2567 /************************************************************************/
2570 _TIFFRewriteField(TIFF
* tif
, uint16 tag
, TIFFDataType in_datatype
,
2571 tmsize_t count
, void* data
)
2573 static const char module
[] = "TIFFResetField";
2574 /* const TIFFField* fip = NULL; */
2577 uint8 direntry_raw
[20];
2578 uint16 entry_tag
= 0;
2579 uint16 entry_type
= 0;
2580 uint64 entry_count
= 0;
2581 uint64 entry_offset
= 0;
2582 int value_in_entry
= 0;
2584 uint8
*buf_to_write
= NULL
;
2585 TIFFDataType datatype
;
2587 /* -------------------------------------------------------------------- */
2588 /* Find field definition. */
2589 /* -------------------------------------------------------------------- */
2590 /*fip =*/ TIFFFindField(tif
, tag
, TIFF_ANY
);
2592 /* -------------------------------------------------------------------- */
2593 /* Do some checking this is a straight forward case. */
2594 /* -------------------------------------------------------------------- */
2597 TIFFErrorExt( tif
->tif_clientdata
, module
,
2598 "Memory mapped files not currently supported for this operation." );
2602 if( tif
->tif_diroff
== 0 )
2604 TIFFErrorExt( tif
->tif_clientdata
, module
,
2605 "Attempt to reset field on directory not already on disk." );
2609 /* -------------------------------------------------------------------- */
2610 /* Read the directory entry count. */
2611 /* -------------------------------------------------------------------- */
2612 if (!SeekOK(tif
, tif
->tif_diroff
)) {
2613 TIFFErrorExt(tif
->tif_clientdata
, module
,
2614 "%s: Seek error accessing TIFF directory",
2619 read_offset
= tif
->tif_diroff
;
2621 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2623 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) {
2624 TIFFErrorExt(tif
->tif_clientdata
, module
,
2625 "%s: Can not read TIFF directory count",
2629 if (tif
->tif_flags
& TIFF_SWAB
)
2630 TIFFSwabShort(&dircount
);
2635 if (!ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
2636 TIFFErrorExt(tif
->tif_clientdata
, module
,
2637 "%s: Can not read TIFF directory count",
2641 if (tif
->tif_flags
& TIFF_SWAB
)
2642 TIFFSwabLong8(&dircount64
);
2643 dircount
= (uint16
)dircount64
;
2648 /* -------------------------------------------------------------------- */
2649 /* Read through directory to find target tag. */
2650 /* -------------------------------------------------------------------- */
2651 while( dircount
> 0 )
2653 if (!ReadOK(tif
, direntry_raw
, dirsize
)) {
2654 TIFFErrorExt(tif
->tif_clientdata
, module
,
2655 "%s: Can not read TIFF directory entry.",
2660 memcpy( &entry_tag
, direntry_raw
+ 0, sizeof(uint16
) );
2661 if (tif
->tif_flags
&TIFF_SWAB
)
2662 TIFFSwabShort( &entry_tag
);
2664 if( entry_tag
== tag
)
2667 read_offset
+= dirsize
;
2670 if( entry_tag
!= tag
)
2672 TIFFErrorExt(tif
->tif_clientdata
, module
,
2673 "%s: Could not find tag %d.",
2674 tif
->tif_name
, tag
);
2678 /* -------------------------------------------------------------------- */
2679 /* Extract the type, count and offset for this entry. */
2680 /* -------------------------------------------------------------------- */
2681 memcpy( &entry_type
, direntry_raw
+ 2, sizeof(uint16
) );
2682 if (tif
->tif_flags
&TIFF_SWAB
)
2683 TIFFSwabShort( &entry_type
);
2685 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2689 memcpy( &value
, direntry_raw
+ 4, sizeof(uint32
) );
2690 if (tif
->tif_flags
&TIFF_SWAB
)
2691 TIFFSwabLong( &value
);
2692 entry_count
= value
;
2694 memcpy( &value
, direntry_raw
+ 8, sizeof(uint32
) );
2695 if (tif
->tif_flags
&TIFF_SWAB
)
2696 TIFFSwabLong( &value
);
2697 entry_offset
= value
;
2701 memcpy( &entry_count
, direntry_raw
+ 4, sizeof(uint64
) );
2702 if (tif
->tif_flags
&TIFF_SWAB
)
2703 TIFFSwabLong8( &entry_count
);
2705 memcpy( &entry_offset
, direntry_raw
+ 12, sizeof(uint64
) );
2706 if (tif
->tif_flags
&TIFF_SWAB
)
2707 TIFFSwabLong8( &entry_offset
);
2710 /* -------------------------------------------------------------------- */
2711 /* What data type do we want to write this as? */
2712 /* -------------------------------------------------------------------- */
2713 if( TIFFDataWidth(in_datatype
) == 8 && !(tif
->tif_flags
&TIFF_BIGTIFF
) )
2715 if( in_datatype
== TIFF_LONG8
)
2716 datatype
= TIFF_LONG
;
2717 else if( in_datatype
== TIFF_SLONG8
)
2718 datatype
= TIFF_SLONG
;
2719 else if( in_datatype
== TIFF_IFD8
)
2720 datatype
= TIFF_IFD
;
2722 datatype
= in_datatype
;
2725 datatype
= in_datatype
;
2727 /* -------------------------------------------------------------------- */
2728 /* Prepare buffer of actual data to write. This includes */
2729 /* swabbing as needed. */
2730 /* -------------------------------------------------------------------- */
2732 (uint8
*)_TIFFCheckMalloc(tif
, count
, TIFFDataWidth(datatype
),
2733 "for field buffer.");
2737 if( datatype
== in_datatype
)
2738 memcpy( buf_to_write
, data
, count
* TIFFDataWidth(datatype
) );
2739 else if( datatype
== TIFF_SLONG
&& in_datatype
== TIFF_SLONG8
)
2743 for( i
= 0; i
< count
; i
++ )
2745 ((int32
*) buf_to_write
)[i
] =
2746 (int32
) ((int64
*) data
)[i
];
2747 if( (int64
) ((int32
*) buf_to_write
)[i
] != ((int64
*) data
)[i
] )
2749 _TIFFfree( buf_to_write
);
2750 TIFFErrorExt( tif
->tif_clientdata
, module
,
2751 "Value exceeds 32bit range of output type." );
2756 else if( (datatype
== TIFF_LONG
&& in_datatype
== TIFF_LONG8
)
2757 || (datatype
== TIFF_IFD
&& in_datatype
== TIFF_IFD8
) )
2761 for( i
= 0; i
< count
; i
++ )
2763 ((uint32
*) buf_to_write
)[i
] =
2764 (uint32
) ((uint64
*) data
)[i
];
2765 if( (uint64
) ((uint32
*) buf_to_write
)[i
] != ((uint64
*) data
)[i
] )
2767 _TIFFfree( buf_to_write
);
2768 TIFFErrorExt( tif
->tif_clientdata
, module
,
2769 "Value exceeds 32bit range of output type." );
2775 if( TIFFDataWidth(datatype
) > 1 && (tif
->tif_flags
&TIFF_SWAB
) )
2777 if( TIFFDataWidth(datatype
) == 2 )
2778 TIFFSwabArrayOfShort( (uint16
*) buf_to_write
, count
);
2779 else if( TIFFDataWidth(datatype
) == 4 )
2780 TIFFSwabArrayOfLong( (uint32
*) buf_to_write
, count
);
2781 else if( TIFFDataWidth(datatype
) == 8 )
2782 TIFFSwabArrayOfLong8( (uint64
*) buf_to_write
, count
);
2785 /* -------------------------------------------------------------------- */
2786 /* Is this a value that fits into the directory entry? */
2787 /* -------------------------------------------------------------------- */
2788 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2790 if( TIFFDataWidth(datatype
) * count
<= 4 )
2792 entry_offset
= read_offset
+ 8;
2798 if( TIFFDataWidth(datatype
) * count
<= 8 )
2800 entry_offset
= read_offset
+ 12;
2805 /* -------------------------------------------------------------------- */
2806 /* If the tag type, and count match, then we just write it out */
2807 /* over the old values without altering the directory entry at */
2809 /* -------------------------------------------------------------------- */
2810 if( entry_count
== (uint64
)count
&& entry_type
== (uint16
) datatype
)
2812 if (!SeekOK(tif
, entry_offset
)) {
2813 _TIFFfree( buf_to_write
);
2814 TIFFErrorExt(tif
->tif_clientdata
, module
,
2815 "%s: Seek error accessing TIFF directory",
2819 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2820 _TIFFfree( buf_to_write
);
2821 TIFFErrorExt(tif
->tif_clientdata
, module
,
2822 "Error writing directory link");
2826 _TIFFfree( buf_to_write
);
2830 /* -------------------------------------------------------------------- */
2831 /* Otherwise, we write the new tag data at the end of the file. */
2832 /* -------------------------------------------------------------------- */
2833 if( !value_in_entry
)
2835 entry_offset
= TIFFSeekFile(tif
,0,SEEK_END
);
2837 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2838 _TIFFfree( buf_to_write
);
2839 TIFFErrorExt(tif
->tif_clientdata
, module
,
2840 "Error writing directory link");
2844 _TIFFfree( buf_to_write
);
2848 memcpy( &entry_offset
, buf_to_write
, count
*TIFFDataWidth(datatype
));
2851 /* -------------------------------------------------------------------- */
2852 /* Adjust the directory entry. */
2853 /* -------------------------------------------------------------------- */
2854 entry_type
= datatype
;
2855 memcpy( direntry_raw
+ 2, &entry_type
, sizeof(uint16
) );
2856 if (tif
->tif_flags
&TIFF_SWAB
)
2857 TIFFSwabShort( (uint16
*) (direntry_raw
+ 2) );
2859 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2863 value
= (uint32
) entry_count
;
2864 memcpy( direntry_raw
+ 4, &value
, sizeof(uint32
) );
2865 if (tif
->tif_flags
&TIFF_SWAB
)
2866 TIFFSwabLong( (uint32
*) (direntry_raw
+ 4) );
2868 value
= (uint32
) entry_offset
;
2869 memcpy( direntry_raw
+ 8, &value
, sizeof(uint32
) );
2870 if (tif
->tif_flags
&TIFF_SWAB
)
2871 TIFFSwabLong( (uint32
*) (direntry_raw
+ 8) );
2875 memcpy( direntry_raw
+ 4, &entry_count
, sizeof(uint64
) );
2876 if (tif
->tif_flags
&TIFF_SWAB
)
2877 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 4) );
2879 memcpy( direntry_raw
+ 12, &entry_offset
, sizeof(uint64
) );
2880 if (tif
->tif_flags
&TIFF_SWAB
)
2881 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 12) );
2884 /* -------------------------------------------------------------------- */
2885 /* Write the directory entry out to disk. */
2886 /* -------------------------------------------------------------------- */
2887 if (!SeekOK(tif
, read_offset
)) {
2888 TIFFErrorExt(tif
->tif_clientdata
, module
,
2889 "%s: Seek error accessing TIFF directory",
2894 if (!WriteOK(tif
, direntry_raw
,dirsize
))
2896 TIFFErrorExt(tif
->tif_clientdata
, module
,
2897 "%s: Can not write TIFF directory entry.",
2904 /* vim: set ts=8 sts=8 sw=8 noet: */