3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 * Directory Write Support Routines.
34 #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
35 #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
37 extern void TIFFCvtNativeToIEEEFloat(TIFF
* tif
, uint32 n
, float* fp
);
38 extern void TIFFCvtNativeToIEEEDouble(TIFF
* tif
, uint32 n
, double* dp
);
41 static int TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
);
43 static int TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
45 static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
48 static int TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
);
49 static int TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
51 static int TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
53 static int TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
55 static int TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
58 static int TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
60 static int TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
);
62 static int TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
64 static int TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
65 static int TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
);
66 static int TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
68 static int TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
70 static int TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
);
72 static int TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
74 static int TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
75 static int TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
77 static int TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
80 static int TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
82 static int TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
);
84 static int TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
87 static int TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
);
89 static int TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
91 static int TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
);
93 static int TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
);
94 static int TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
95 static int TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
96 static int TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
98 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
100 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
102 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
105 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
107 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
109 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
111 static int TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
113 static int TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
115 static int TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
116 static int TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
117 static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
119 static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
121 static int TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
122 static int TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
123 static int TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
125 static int TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
);
126 static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
128 static int TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
130 static int TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
132 static int TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
134 static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
);
135 static int TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
136 static int TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
);
138 static int TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
140 static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
);
141 static int TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
142 static int TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
144 static int TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
146 static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
);
148 static int TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
);
150 static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
152 static int TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
);
154 static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
);
155 static int TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
156 static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
157 static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
159 static int TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
161 static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
163 static int TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
165 static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
166 static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
167 static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
169 static int TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
);
171 static int TIFFLinkDirectory(TIFF
*);
174 * Write the contents of the current directory
175 * to the specified file. This routine doesn't
176 * handle overwriting a directory with auxiliary
177 * storage that's been changed.
180 TIFFWriteDirectory(TIFF
* tif
)
182 return TIFFWriteDirectorySec(tif
,TRUE
,TRUE
,NULL
);
186 * Similar to TIFFWriteDirectory(), writes the directory out
187 * but leaves all data structures in memory so that it can be
188 * written again. This will make a partially written TIFF file
189 * readable before it is successfully completed/closed.
192 TIFFCheckpointDirectory(TIFF
* tif
)
195 /* Setup the strips arrays, if they haven't already been. */
196 if (tif
->tif_dir
.td_stripoffset
== NULL
)
197 (void) TIFFSetupStrips(tif
);
198 rc
= TIFFWriteDirectorySec(tif
,TRUE
,FALSE
,NULL
);
199 (void) TIFFSetWriteOffset(tif
, TIFFSeekFile(tif
, 0, SEEK_END
));
204 TIFFWriteCustomDirectory(TIFF
* tif
, uint64
* pdiroff
)
206 return TIFFWriteDirectorySec(tif
,FALSE
,FALSE
,pdiroff
);
210 * Similar to TIFFWriteDirectory(), but if the directory has already
211 * been written once, it is relocated to the end of the file, in case it
212 * has changed in size. Note that this will result in the loss of the
213 * previously used directory space.
216 TIFFRewriteDirectory( TIFF
*tif
)
218 static const char module[] = "TIFFRewriteDirectory";
220 /* We don't need to do anything special if it hasn't been written. */
221 if( tif
->tif_diroff
== 0 )
222 return TIFFWriteDirectory( tif
);
225 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
226 * will cause it to be added after this directories current pre-link.
229 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
231 if (tif
->tif_header
.classic
.tiff_diroff
== tif
->tif_diroff
)
233 tif
->tif_header
.classic
.tiff_diroff
= 0;
236 TIFFSeekFile(tif
,4,SEEK_SET
);
237 if (!WriteOK(tif
, &(tif
->tif_header
.classic
.tiff_diroff
),4))
239 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
240 "Error updating TIFF header");
247 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
252 if (!SeekOK(tif
, nextdir
) ||
253 !ReadOK(tif
, &dircount
, 2)) {
254 TIFFErrorExt(tif
->tif_clientdata
, module,
255 "Error fetching directory count");
258 if (tif
->tif_flags
& TIFF_SWAB
)
259 TIFFSwabShort(&dircount
);
260 (void) TIFFSeekFile(tif
,
261 nextdir
+2+dircount
*12, SEEK_SET
);
262 if (!ReadOK(tif
, &nextnextdir
, 4)) {
263 TIFFErrorExt(tif
->tif_clientdata
, module,
264 "Error fetching directory link");
267 if (tif
->tif_flags
& TIFF_SWAB
)
268 TIFFSwabLong(&nextnextdir
);
269 if (nextnextdir
==tif
->tif_diroff
)
273 (void) TIFFSeekFile(tif
,
274 nextdir
+2+dircount
*12, SEEK_SET
);
275 if (!WriteOK(tif
, &m
, 4)) {
276 TIFFErrorExt(tif
->tif_clientdata
, module,
277 "Error writing directory link");
289 if (tif
->tif_header
.big
.tiff_diroff
== tif
->tif_diroff
)
291 tif
->tif_header
.big
.tiff_diroff
= 0;
294 TIFFSeekFile(tif
,8,SEEK_SET
);
295 if (!WriteOK(tif
, &(tif
->tif_header
.big
.tiff_diroff
),8))
297 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
298 "Error updating TIFF header");
305 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
311 if (!SeekOK(tif
, nextdir
) ||
312 !ReadOK(tif
, &dircount64
, 8)) {
313 TIFFErrorExt(tif
->tif_clientdata
, module,
314 "Error fetching directory count");
317 if (tif
->tif_flags
& TIFF_SWAB
)
318 TIFFSwabLong8(&dircount64
);
319 if (dircount64
>0xFFFF)
321 TIFFErrorExt(tif
->tif_clientdata
, module,
322 "Sanity check on tag count failed, likely corrupt TIFF");
325 dircount
=(uint16
)dircount64
;
326 (void) TIFFSeekFile(tif
,
327 nextdir
+8+dircount
*20, SEEK_SET
);
328 if (!ReadOK(tif
, &nextnextdir
, 8)) {
329 TIFFErrorExt(tif
->tif_clientdata
, module,
330 "Error fetching directory link");
333 if (tif
->tif_flags
& TIFF_SWAB
)
334 TIFFSwabLong8(&nextnextdir
);
335 if (nextnextdir
==tif
->tif_diroff
)
339 (void) TIFFSeekFile(tif
,
340 nextdir
+8+dircount
*20, SEEK_SET
);
341 if (!WriteOK(tif
, &m
, 8)) {
342 TIFFErrorExt(tif
->tif_clientdata
, module,
343 "Error writing directory link");
355 * Now use TIFFWriteDirectory() normally.
358 return TIFFWriteDirectory( tif
);
362 TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
)
364 static const char module[] = "TIFFWriteDirectorySec";
370 if (tif
->tif_mode
== O_RDONLY
)
373 _TIFFFillStriles( tif
);
376 * Clear write state so that subsequent images with
377 * different characteristics get the right buffers
382 if (tif
->tif_flags
& TIFF_POSTENCODE
)
384 tif
->tif_flags
&= ~TIFF_POSTENCODE
;
385 if (!(*tif
->tif_postencode
)(tif
))
387 TIFFErrorExt(tif
->tif_clientdata
,module,
388 "Error post-encoding before directory write");
392 (*tif
->tif_close
)(tif
); /* shutdown encoder */
394 * Flush any data that might have been written
395 * by the compression close+cleanup routines. But
396 * be careful not to write stuff if we didn't add data
397 * in the previous steps as the "rawcc" data may well be
398 * a previously read tile/strip in mixed read/write mode.
400 if (tif
->tif_rawcc
> 0
401 && (tif
->tif_flags
& TIFF_BEENWRITING
) != 0 )
403 if( !TIFFFlushData1(tif
) )
405 TIFFErrorExt(tif
->tif_clientdata
, module,
406 "Error flushing data before directory write");
410 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
)
412 _TIFFfree(tif
->tif_rawdata
);
413 tif
->tif_rawdata
= NULL
;
415 tif
->tif_rawdatasize
= 0;
416 tif
->tif_rawdataoff
= 0;
417 tif
->tif_rawdataloaded
= 0;
419 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
);
429 if (TIFFFieldSet(tif
,FIELD_IMAGEDIMENSIONS
))
431 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEWIDTH
,tif
->tif_dir
.td_imagewidth
))
433 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGELENGTH
,tif
->tif_dir
.td_imagelength
))
436 if (TIFFFieldSet(tif
,FIELD_TILEDIMENSIONS
))
438 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILEWIDTH
,tif
->tif_dir
.td_tilewidth
))
440 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILELENGTH
,tif
->tif_dir
.td_tilelength
))
443 if (TIFFFieldSet(tif
,FIELD_RESOLUTION
))
445 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XRESOLUTION
,tif
->tif_dir
.td_xresolution
))
447 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YRESOLUTION
,tif
->tif_dir
.td_yresolution
))
450 if (TIFFFieldSet(tif
,FIELD_POSITION
))
452 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XPOSITION
,tif
->tif_dir
.td_xposition
))
454 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YPOSITION
,tif
->tif_dir
.td_yposition
))
457 if (TIFFFieldSet(tif
,FIELD_SUBFILETYPE
))
459 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_SUBFILETYPE
,tif
->tif_dir
.td_subfiletype
))
462 if (TIFFFieldSet(tif
,FIELD_BITSPERSAMPLE
))
464 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_BITSPERSAMPLE
,tif
->tif_dir
.td_bitspersample
))
467 if (TIFFFieldSet(tif
,FIELD_COMPRESSION
))
469 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_COMPRESSION
,tif
->tif_dir
.td_compression
))
472 if (TIFFFieldSet(tif
,FIELD_PHOTOMETRIC
))
474 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PHOTOMETRIC
,tif
->tif_dir
.td_photometric
))
477 if (TIFFFieldSet(tif
,FIELD_THRESHHOLDING
))
479 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_THRESHHOLDING
,tif
->tif_dir
.td_threshholding
))
482 if (TIFFFieldSet(tif
,FIELD_FILLORDER
))
484 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_FILLORDER
,tif
->tif_dir
.td_fillorder
))
487 if (TIFFFieldSet(tif
,FIELD_ORIENTATION
))
489 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_ORIENTATION
,tif
->tif_dir
.td_orientation
))
492 if (TIFFFieldSet(tif
,FIELD_SAMPLESPERPIXEL
))
494 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_SAMPLESPERPIXEL
,tif
->tif_dir
.td_samplesperpixel
))
497 if (TIFFFieldSet(tif
,FIELD_ROWSPERSTRIP
))
499 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_ROWSPERSTRIP
,tif
->tif_dir
.td_rowsperstrip
))
502 if (TIFFFieldSet(tif
,FIELD_MINSAMPLEVALUE
))
504 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MINSAMPLEVALUE
,tif
->tif_dir
.td_minsamplevalue
))
507 if (TIFFFieldSet(tif
,FIELD_MAXSAMPLEVALUE
))
509 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MAXSAMPLEVALUE
,tif
->tif_dir
.td_maxsamplevalue
))
512 if (TIFFFieldSet(tif
,FIELD_PLANARCONFIG
))
514 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PLANARCONFIG
,tif
->tif_dir
.td_planarconfig
))
517 if (TIFFFieldSet(tif
,FIELD_RESOLUTIONUNIT
))
519 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_RESOLUTIONUNIT
,tif
->tif_dir
.td_resolutionunit
))
522 if (TIFFFieldSet(tif
,FIELD_PAGENUMBER
))
524 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_PAGENUMBER
,2,&tif
->tif_dir
.td_pagenumber
[0]))
527 if (TIFFFieldSet(tif
,FIELD_STRIPBYTECOUNTS
))
531 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount
))
536 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount
))
540 if (TIFFFieldSet(tif
,FIELD_STRIPOFFSETS
))
544 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
549 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
553 if (TIFFFieldSet(tif
,FIELD_COLORMAP
))
555 if (!TIFFWriteDirectoryTagColormap(tif
,&ndir
,dir
))
558 if (TIFFFieldSet(tif
,FIELD_EXTRASAMPLES
))
560 if (tif
->tif_dir
.td_extrasamples
)
564 TIFFGetFieldDefaulted(tif
,TIFFTAG_EXTRASAMPLES
,&na
,&nb
);
565 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_EXTRASAMPLES
,na
,nb
))
569 if (TIFFFieldSet(tif
,FIELD_SAMPLEFORMAT
))
571 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_SAMPLEFORMAT
,tif
->tif_dir
.td_sampleformat
))
574 if (TIFFFieldSet(tif
,FIELD_SMINSAMPLEVALUE
))
576 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMINSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_sminsamplevalue
))
579 if (TIFFFieldSet(tif
,FIELD_SMAXSAMPLEVALUE
))
581 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMAXSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_smaxsamplevalue
))
584 if (TIFFFieldSet(tif
,FIELD_IMAGEDEPTH
))
586 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEDEPTH
,tif
->tif_dir
.td_imagedepth
))
589 if (TIFFFieldSet(tif
,FIELD_TILEDEPTH
))
591 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_TILEDEPTH
,tif
->tif_dir
.td_tiledepth
))
594 if (TIFFFieldSet(tif
,FIELD_HALFTONEHINTS
))
596 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_HALFTONEHINTS
,2,&tif
->tif_dir
.td_halftonehints
[0]))
599 if (TIFFFieldSet(tif
,FIELD_YCBCRSUBSAMPLING
))
601 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_YCBCRSUBSAMPLING
,2,&tif
->tif_dir
.td_ycbcrsubsampling
[0]))
604 if (TIFFFieldSet(tif
,FIELD_YCBCRPOSITIONING
))
606 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_YCBCRPOSITIONING
,tif
->tif_dir
.td_ycbcrpositioning
))
609 if (TIFFFieldSet(tif
,FIELD_REFBLACKWHITE
))
611 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,TIFFTAG_REFERENCEBLACKWHITE
,6,tif
->tif_dir
.td_refblackwhite
))
614 if (TIFFFieldSet(tif
,FIELD_TRANSFERFUNCTION
))
616 if (!TIFFWriteDirectoryTagTransferfunction(tif
,&ndir
,dir
))
619 if (TIFFFieldSet(tif
,FIELD_INKNAMES
))
621 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,TIFFTAG_INKNAMES
,tif
->tif_dir
.td_inknameslen
,tif
->tif_dir
.td_inknames
))
624 if (TIFFFieldSet(tif
,FIELD_SUBIFD
))
626 if (!TIFFWriteDirectoryTagSubifd(tif
,&ndir
,dir
))
631 for (n
=0; n
<tif
->tif_nfields
; n
++) {
633 o
= tif
->tif_fields
[n
];
634 if ((o
->field_bit
>=FIELD_CODEC
)&&(TIFFFieldSet(tif
,o
->field_bit
)))
636 switch (o
->get_field_type
)
638 case TIFF_SETGET_ASCII
:
642 assert(o
->field_type
==TIFF_ASCII
);
643 assert(o
->field_readcount
==TIFF_VARIABLE
);
644 assert(o
->field_passcount
==0);
645 TIFFGetField(tif
,o
->field_tag
,&pb
);
646 pa
=(uint32
)(strlen(pb
));
647 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,o
->field_tag
,pa
,pb
))
651 case TIFF_SETGET_UINT16
:
654 assert(o
->field_type
==TIFF_SHORT
);
655 assert(o
->field_readcount
==1);
656 assert(o
->field_passcount
==0);
657 TIFFGetField(tif
,o
->field_tag
,&p
);
658 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,o
->field_tag
,p
))
662 case TIFF_SETGET_UINT32
:
665 assert(o
->field_type
==TIFF_LONG
);
666 assert(o
->field_readcount
==1);
667 assert(o
->field_passcount
==0);
668 TIFFGetField(tif
,o
->field_tag
,&p
);
669 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,o
->field_tag
,p
))
673 case TIFF_SETGET_C32_UINT8
:
677 assert(o
->field_type
==TIFF_UNDEFINED
);
678 assert(o
->field_readcount
==TIFF_VARIABLE2
);
679 assert(o
->field_passcount
==1);
680 TIFFGetField(tif
,o
->field_tag
,&pa
,&pb
);
681 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,o
->field_tag
,pa
,pb
))
686 assert(0); /* we should never get here */
693 for (m
=0; m
<(uint32
)(tif
->tif_dir
.td_customValueCount
); m
++)
695 switch (tif
->tif_dir
.td_customValues
[m
].info
->field_type
)
698 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
))
702 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
))
706 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
))
710 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
))
714 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
))
718 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
))
722 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
))
726 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
))
730 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
))
734 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
))
738 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
))
742 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
))
746 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
))
750 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
))
754 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
))
758 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
))
762 assert(0); /* we should never get here */
768 dir
=_TIFFmalloc(ndir
*sizeof(TIFFDirEntry
));
771 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
776 if ((tif
->tif_diroff
==0)&&(!TIFFLinkDirectory(tif
)))
780 tif
->tif_diroff
=(TIFFSeekFile(tif
,0,SEEK_END
)+1)&(~1);
782 *pdiroff
=tif
->tif_diroff
;
783 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
787 tif
->tif_dataoff
=tif
->tif_diroff
+dirsize
;
788 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
789 tif
->tif_dataoff
=(uint32
)tif
->tif_dataoff
;
790 if ((tif
->tif_dataoff
<tif
->tif_diroff
)||(tif
->tif_dataoff
<(uint64
)dirsize
))
792 TIFFErrorExt(tif
->tif_clientdata
,module,"Maximum TIFF file size exceeded");
795 if (tif
->tif_dataoff
&1)
802 if (TIFFFieldSet(tif
,FIELD_SUBIFD
)&&(tif
->tif_subifdoff
==0))
806 for (na
=0, nb
=dir
; ; na
++, nb
++)
809 if (nb
->tdir_tag
==TIFFTAG_SUBIFD
)
812 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
813 tif
->tif_subifdoff
=tif
->tif_diroff
+2+na
*12+8;
815 tif
->tif_subifdoff
=tif
->tif_diroff
+8+na
*20+12;
818 dirmem
=_TIFFmalloc(dirsize
);
821 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
824 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
831 if (tif
->tif_flags
&TIFF_SWAB
)
832 TIFFSwabShort((uint16
*)n
);
835 for (m
=0; m
<ndir
; m
++)
837 *(uint16
*)n
=o
->tdir_tag
;
838 if (tif
->tif_flags
&TIFF_SWAB
)
839 TIFFSwabShort((uint16
*)n
);
841 *(uint16
*)n
=o
->tdir_type
;
842 if (tif
->tif_flags
&TIFF_SWAB
)
843 TIFFSwabShort((uint16
*)n
);
845 nTmp
= (uint32
)o
->tdir_count
;
846 _TIFFmemcpy(n
,&nTmp
,4);
847 if (tif
->tif_flags
&TIFF_SWAB
)
848 TIFFSwabLong((uint32
*)n
);
850 /* This is correct. The data has been */
851 /* swabbed previously in TIFFWriteDirectoryTagData */
852 _TIFFmemcpy(n
,&o
->tdir_offset
,4);
856 nTmp
= (uint32
)tif
->tif_nextdiroff
;
857 if (tif
->tif_flags
&TIFF_SWAB
)
859 _TIFFmemcpy(n
,&nTmp
,4);
867 if (tif
->tif_flags
&TIFF_SWAB
)
868 TIFFSwabLong8((uint64
*)n
);
871 for (m
=0; m
<ndir
; m
++)
873 *(uint16
*)n
=o
->tdir_tag
;
874 if (tif
->tif_flags
&TIFF_SWAB
)
875 TIFFSwabShort((uint16
*)n
);
877 *(uint16
*)n
=o
->tdir_type
;
878 if (tif
->tif_flags
&TIFF_SWAB
)
879 TIFFSwabShort((uint16
*)n
);
881 _TIFFmemcpy(n
,&o
->tdir_count
,8);
882 if (tif
->tif_flags
&TIFF_SWAB
)
883 TIFFSwabLong8((uint64
*)n
);
885 _TIFFmemcpy(n
,&o
->tdir_offset
,8);
889 _TIFFmemcpy(n
,&tif
->tif_nextdiroff
,8);
890 if (tif
->tif_flags
&TIFF_SWAB
)
891 TIFFSwabLong8((uint64
*)n
);
895 if (!SeekOK(tif
,tif
->tif_diroff
))
897 TIFFErrorExt(tif
->tif_clientdata
,module,"IO error writing directory");
900 if (!WriteOK(tif
,dirmem
,(tmsize_t
)dirsize
))
902 TIFFErrorExt(tif
->tif_clientdata
,module,"IO error writing directory");
908 TIFFFreeDirectory(tif
);
909 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
910 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
911 (*tif
->tif_cleanup
)(tif
);
913 * Reset directory-related state for subsequent
916 TIFFCreateDirectory(tif
);
928 TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
930 static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
934 conv
= _TIFFmalloc(count
*sizeof(double));
937 TIFFErrorExt(tif
->tif_clientdata
, module, "Out of memory");
941 switch (tif
->tif_dir
.td_sampleformat
)
943 case SAMPLEFORMAT_IEEEFP
:
944 if (tif
->tif_dir
.td_bitspersample
<=32)
946 for (i
= 0; i
< count
; ++i
)
947 ((float*)conv
)[i
] = (float)value
[i
];
948 ok
= TIFFWriteDirectoryTagFloatArray(tif
,ndir
,dir
,tag
,count
,(float*)conv
);
952 ok
= TIFFWriteDirectoryTagDoubleArray(tif
,ndir
,dir
,tag
,count
,value
);
955 case SAMPLEFORMAT_INT
:
956 if (tif
->tif_dir
.td_bitspersample
<=8)
958 for (i
= 0; i
< count
; ++i
)
959 ((int8
*)conv
)[i
] = (int8
)value
[i
];
960 ok
= TIFFWriteDirectoryTagSbyteArray(tif
,ndir
,dir
,tag
,count
,(int8
*)conv
);
962 else if (tif
->tif_dir
.td_bitspersample
<=16)
964 for (i
= 0; i
< count
; ++i
)
965 ((int16
*)conv
)[i
] = (int16
)value
[i
];
966 ok
= TIFFWriteDirectoryTagSshortArray(tif
,ndir
,dir
,tag
,count
,(int16
*)conv
);
970 for (i
= 0; i
< count
; ++i
)
971 ((int32
*)conv
)[i
] = (int32
)value
[i
];
972 ok
= TIFFWriteDirectoryTagSlongArray(tif
,ndir
,dir
,tag
,count
,(int32
*)conv
);
975 case SAMPLEFORMAT_UINT
:
976 if (tif
->tif_dir
.td_bitspersample
<=8)
978 for (i
= 0; i
< count
; ++i
)
979 ((uint8
*)conv
)[i
] = (uint8
)value
[i
];
980 ok
= TIFFWriteDirectoryTagByteArray(tif
,ndir
,dir
,tag
,count
,(uint8
*)conv
);
982 else if (tif
->tif_dir
.td_bitspersample
<=16)
984 for (i
= 0; i
< count
; ++i
)
985 ((uint16
*)conv
)[i
] = (uint16
)value
[i
];
986 ok
= TIFFWriteDirectoryTagShortArray(tif
,ndir
,dir
,tag
,count
,(uint16
*)conv
);
990 for (i
= 0; i
< count
; ++i
)
991 ((uint32
*)conv
)[i
] = (uint32
)value
[i
];
992 ok
= TIFFWriteDirectoryTagLongArray(tif
,ndir
,dir
,tag
,count
,(uint32
*)conv
);
1005 TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1007 switch (tif
->tif_dir
.td_sampleformat
)
1009 case SAMPLEFORMAT_IEEEFP
:
1010 if (tif
->tif_dir
.td_bitspersample
<=32)
1011 return(TIFFWriteDirectoryTagFloatPerSample(tif
,ndir
,dir
,tag
,(float)value
));
1013 return(TIFFWriteDirectoryTagDoublePerSample(tif
,ndir
,dir
,tag
,value
));
1014 case SAMPLEFORMAT_INT
:
1015 if (tif
->tif_dir
.td_bitspersample
<=8)
1016 return(TIFFWriteDirectoryTagSbytePerSample(tif
,ndir
,dir
,tag
,(int8
)value
));
1017 else if (tif
->tif_dir
.td_bitspersample
<=16)
1018 return(TIFFWriteDirectoryTagSshortPerSample(tif
,ndir
,dir
,tag
,(int16
)value
));
1020 return(TIFFWriteDirectoryTagSlongPerSample(tif
,ndir
,dir
,tag
,(int32
)value
));
1021 case SAMPLEFORMAT_UINT
:
1022 if (tif
->tif_dir
.td_bitspersample
<=8)
1023 return(TIFFWriteDirectoryTagBytePerSample(tif
,ndir
,dir
,tag
,(uint8
)value
));
1024 else if (tif
->tif_dir
.td_bitspersample
<=16)
1025 return(TIFFWriteDirectoryTagShortPerSample(tif
,ndir
,dir
,tag
,(uint16
)value
));
1027 return(TIFFWriteDirectoryTagLongPerSample(tif
,ndir
,dir
,tag
,(uint32
)value
));
1035 TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1042 return(TIFFWriteDirectoryTagCheckedAscii(tif
,ndir
,dir
,tag
,count
,value
));
1046 TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1053 return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif
,ndir
,dir
,tag
,count
,value
));
1058 TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1065 return(TIFFWriteDirectoryTagCheckedByte(tif
,ndir
,dir
,tag
,value
));
1070 TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1077 return(TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,count
,value
));
1082 TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1084 static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
1094 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint8
));
1097 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1100 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1102 o
=TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1110 TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1117 return(TIFFWriteDirectoryTagCheckedSbyte(tif
,ndir
,dir
,tag
,value
));
1122 TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1129 return(TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,count
,value
));
1134 TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1136 static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
1146 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int8
));
1149 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1152 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1154 o
=TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1161 TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1168 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,value
));
1172 TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1179 return(TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,value
));
1183 TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1185 static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
1195 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint16
));
1198 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1201 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1203 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1210 TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1217 return(TIFFWriteDirectoryTagCheckedSshort(tif
,ndir
,dir
,tag
,value
));
1222 TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1229 return(TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,count
,value
));
1234 TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1236 static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
1246 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int16
));
1249 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1252 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1254 o
=TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1261 TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1268 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1272 TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1279 return(TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,value
));
1284 TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1286 static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
1296 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint32
));
1299 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1302 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1304 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1312 TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1319 return(TIFFWriteDirectoryTagCheckedSlong(tif
,ndir
,dir
,tag
,value
));
1324 TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
1331 return(TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,count
,value
));
1336 TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1338 static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
1348 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int32
));
1351 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1354 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1356 o
=TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1364 TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
1371 return(TIFFWriteDirectoryTagCheckedLong8(tif
,ndir
,dir
,tag
,value
));
1376 TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1383 return(TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1388 TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
1395 return(TIFFWriteDirectoryTagCheckedSlong8(tif
,ndir
,dir
,tag
,value
));
1400 TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
1407 return(TIFFWriteDirectoryTagCheckedSlong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1411 TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1418 return(TIFFWriteDirectoryTagCheckedRational(tif
,ndir
,dir
,tag
,value
));
1422 TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1429 return(TIFFWriteDirectoryTagCheckedRationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1433 TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1440 return(TIFFWriteDirectoryTagCheckedSrationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1444 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1451 return(TIFFWriteDirectoryTagCheckedFloat(tif
,ndir
,dir
,tag
,value
));
1455 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1462 return(TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,count
,value
));
1466 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1468 static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
1478 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(float));
1481 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1484 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1486 o
=TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1493 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1500 return(TIFFWriteDirectoryTagCheckedDouble(tif
,ndir
,dir
,tag
,value
));
1504 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1511 return(TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,count
,value
));
1515 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1517 static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
1527 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(double));
1530 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1533 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1535 o
=TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1542 TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1549 return(TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,value
));
1554 TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1561 return(TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,tag
,count
,value
));
1566 TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1574 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,(uint16
)value
));
1576 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1579 /************************************************************************/
1580 /* TIFFWriteDirectoryTagLongLong8Array() */
1582 /* Write out LONG8 array as LONG8 for BigTIFF or LONG for */
1583 /* Classic TIFF with some checking. */
1584 /************************************************************************/
1587 TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1589 static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
1596 /* is this just a counting pass? */
1603 /* We always write LONG8 for BigTIFF, no checking needed. */
1604 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1605 return TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,
1609 ** For classic tiff we want to verify everything is in range for LONG
1610 ** and convert to long format.
1613 p
= _TIFFmalloc(count
*sizeof(uint32
));
1616 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1620 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1624 TIFFErrorExt(tif
->tif_clientdata
,module,
1625 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1632 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1638 /************************************************************************/
1639 /* TIFFWriteDirectoryTagIfdIfd8Array() */
1641 /* Write either IFD8 or IFD array depending on file type. */
1642 /************************************************************************/
1645 TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1647 static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1654 /* is this just a counting pass? */
1661 /* We always write IFD8 for BigTIFF, no checking needed. */
1662 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1663 return TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,
1667 ** For classic tiff we want to verify everything is in range for IFD
1668 ** and convert to long format.
1671 p
= _TIFFmalloc(count
*sizeof(uint32
));
1674 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1678 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1682 TIFFErrorExt(tif
->tif_clientdata
,module,
1683 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1690 o
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,p
);
1698 TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1700 static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1711 for (ma
=value
, mb
=0; mb
<count
; ma
++, mb
++)
1713 if ((n
==0)&&(*ma
>0xFFFF))
1715 if ((n
==1)&&(*ma
>0xFFFFFFFF))
1725 p
=_TIFFmalloc(count
*sizeof(uint16
));
1728 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1731 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1733 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,p
);
1740 p
=_TIFFmalloc(count
*sizeof(uint32
));
1743 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1746 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1748 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1754 o
=TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
);
1760 TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1762 static const char module[] = "TIFFWriteDirectoryTagColormap";
1771 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1772 n
=_TIFFmalloc(3*m
*sizeof(uint16
));
1775 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1778 _TIFFmemcpy(&n
[0],tif
->tif_dir
.td_colormap
[0],m
*sizeof(uint16
));
1779 _TIFFmemcpy(&n
[m
],tif
->tif_dir
.td_colormap
[1],m
*sizeof(uint16
));
1780 _TIFFmemcpy(&n
[2*m
],tif
->tif_dir
.td_colormap
[2],m
*sizeof(uint16
));
1781 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_COLORMAP
,3*m
,n
);
1787 TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1789 static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
1799 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1800 n
=tif
->tif_dir
.td_samplesperpixel
-tif
->tif_dir
.td_extrasamples
;
1802 * Check if the table can be written as a single column,
1803 * or if it must be written as 3 columns. Note that we
1804 * write a 3-column tag if there are 2 samples/pixel and
1805 * a single column of data won't suffice--hmm.
1811 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
)))
1816 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
)))
1821 o
=_TIFFmalloc(n
*m
*sizeof(uint16
));
1824 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1827 _TIFFmemcpy(&o
[0],tif
->tif_dir
.td_transferfunction
[0],m
*sizeof(uint16
));
1829 _TIFFmemcpy(&o
[m
],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
));
1831 _TIFFmemcpy(&o
[2*m
],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
));
1832 p
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_TRANSFERFUNCTION
,n
*m
,o
);
1838 TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1840 static const char module[] = "TIFFWriteDirectoryTagSubifd";
1843 if (tif
->tif_dir
.td_nsubifd
==0)
1851 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1857 o
=_TIFFmalloc(tif
->tif_dir
.td_nsubifd
*sizeof(uint32
));
1860 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1863 pa
=tif
->tif_dir
.td_subifd
;
1865 for (p
=0; p
< tif
->tif_dir
.td_nsubifd
; p
++)
1868 assert(*pa
<= 0xFFFFFFFFUL
);
1869 *pb
++=(uint32
)(*pa
++);
1871 n
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,o
);
1875 n
=TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,tif
->tif_dir
.td_subifd
);
1879 * Total hack: if this directory includes a SubIFD
1880 * tag then force the next <n> directories to be
1881 * written as ``sub directories'' of this one. This
1882 * is used to write things like thumbnails and
1883 * image masks that one wants to keep out of the
1884 * normal directory linkage access mechanism.
1886 tif
->tif_flags
|=TIFF_INSUBIFD
;
1887 tif
->tif_nsubifd
=tif
->tif_dir
.td_nsubifd
;
1888 if (tif
->tif_dir
.td_nsubifd
==1)
1889 tif
->tif_subifdoff
=0;
1891 tif
->tif_subifdoff
=m
;
1896 TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1898 assert(sizeof(char)==1);
1899 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_ASCII
,count
,count
,value
));
1903 TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1905 assert(sizeof(uint8
)==1);
1906 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_UNDEFINED
,count
,count
,value
));
1911 TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1913 assert(sizeof(uint8
)==1);
1914 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,1,1,&value
));
1919 TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1921 assert(sizeof(uint8
)==1);
1922 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,count
,count
,value
));
1927 TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1929 assert(sizeof(int8
)==1);
1930 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,1,1,&value
));
1935 TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1937 assert(sizeof(int8
)==1);
1938 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,count
,count
,value
));
1942 TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1945 assert(sizeof(uint16
)==2);
1947 if (tif
->tif_flags
&TIFF_SWAB
)
1949 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,1,2,&m
));
1953 TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1955 assert(count
<0x80000000);
1956 assert(sizeof(uint16
)==2);
1957 if (tif
->tif_flags
&TIFF_SWAB
)
1958 TIFFSwabArrayOfShort(value
,count
);
1959 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,count
,count
*2,value
));
1964 TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1967 assert(sizeof(int16
)==2);
1969 if (tif
->tif_flags
&TIFF_SWAB
)
1970 TIFFSwabShort((uint16
*)(&m
));
1971 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,1,2,&m
));
1976 TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1978 assert(count
<0x80000000);
1979 assert(sizeof(int16
)==2);
1980 if (tif
->tif_flags
&TIFF_SWAB
)
1981 TIFFSwabArrayOfShort((uint16
*)value
,count
);
1982 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,count
,count
*2,value
));
1986 TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1989 assert(sizeof(uint32
)==4);
1991 if (tif
->tif_flags
&TIFF_SWAB
)
1993 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,1,4,&m
));
1997 TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1999 assert(count
<0x40000000);
2000 assert(sizeof(uint32
)==4);
2001 if (tif
->tif_flags
&TIFF_SWAB
)
2002 TIFFSwabArrayOfLong(value
,count
);
2003 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,count
,count
*4,value
));
2008 TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
2011 assert(sizeof(int32
)==4);
2013 if (tif
->tif_flags
&TIFF_SWAB
)
2014 TIFFSwabLong((uint32
*)(&m
));
2015 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,1,4,&m
));
2020 TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
2022 assert(count
<0x40000000);
2023 assert(sizeof(int32
)==4);
2024 if (tif
->tif_flags
&TIFF_SWAB
)
2025 TIFFSwabArrayOfLong((uint32
*)value
,count
);
2026 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,count
,count
*4,value
));
2031 TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
2034 assert(sizeof(uint64
)==8);
2035 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2037 if (tif
->tif_flags
&TIFF_SWAB
)
2039 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,1,8,&m
));
2044 TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2046 assert(count
<0x20000000);
2047 assert(sizeof(uint64
)==8);
2048 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2049 if (tif
->tif_flags
&TIFF_SWAB
)
2050 TIFFSwabArrayOfLong8(value
,count
);
2051 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,count
,count
*8,value
));
2056 TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
2059 assert(sizeof(int64
)==8);
2060 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2062 if (tif
->tif_flags
&TIFF_SWAB
)
2063 TIFFSwabLong8((uint64
*)(&m
));
2064 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,1,8,&m
));
2069 TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
2071 assert(count
<0x20000000);
2072 assert(sizeof(int64
)==8);
2073 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2074 if (tif
->tif_flags
&TIFF_SWAB
)
2075 TIFFSwabArrayOfLong8((uint64
*)value
,count
);
2076 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,count
,count
*8,value
));
2080 TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2084 assert(sizeof(uint32
)==4);
2090 else if (value
==(double)(uint32
)value
)
2097 m
[0]=(uint32
)(value
*0xFFFFFFFF);
2103 m
[1]=(uint32
)(0xFFFFFFFF/value
);
2105 if (tif
->tif_flags
&TIFF_SWAB
)
2107 TIFFSwabLong(&m
[0]);
2108 TIFFSwabLong(&m
[1]);
2110 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,1,8,&m
[0]));
2114 TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2116 static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2122 assert(sizeof(uint32
)==4);
2123 m
=_TIFFmalloc(count
*2*sizeof(uint32
));
2126 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
2129 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2136 else if (*na
==(float)(uint32
)(*na
))
2138 nb
[0]=(uint32
)(*na
);
2143 nb
[0]=(uint32
)((*na
)*0xFFFFFFFF);
2149 nb
[1]=(uint32
)(0xFFFFFFFF/(*na
));
2152 if (tif
->tif_flags
&TIFF_SWAB
)
2153 TIFFSwabArrayOfLong(m
,count
*2);
2154 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,count
,count
*8,&m
[0]);
2160 TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2162 static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2168 assert(sizeof(int32
)==4);
2169 m
=_TIFFmalloc(count
*2*sizeof(int32
));
2172 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
2175 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2179 if (*na
==(int32
)(*na
))
2186 nb
[0]=-(int32
)((-*na
)*0x7FFFFFFF);
2192 nb
[1]=(int32
)(0x7FFFFFFF/(-*na
));
2197 if (*na
==(int32
)(*na
))
2204 nb
[0]=(int32
)((*na
)*0x7FFFFFFF);
2210 nb
[1]=(int32
)(0x7FFFFFFF/(*na
));
2214 if (tif
->tif_flags
&TIFF_SWAB
)
2215 TIFFSwabArrayOfLong((uint32
*)m
,count
*2);
2216 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SRATIONAL
,count
,count
*8,&m
[0]);
2223 TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
2226 assert(sizeof(float)==4);
2228 TIFFCvtNativeToIEEEFloat(tif
,1,&m
);
2229 if (tif
->tif_flags
&TIFF_SWAB
)
2231 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,1,4,&m
));
2236 TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2238 assert(count
<0x40000000);
2239 assert(sizeof(float)==4);
2240 TIFFCvtNativeToIEEEFloat(tif
,count
,&value
);
2241 if (tif
->tif_flags
&TIFF_SWAB
)
2242 TIFFSwabArrayOfFloat(value
,count
);
2243 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,count
,count
*4,value
));
2248 TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2251 assert(sizeof(double)==8);
2253 TIFFCvtNativeToIEEEDouble(tif
,1,&m
);
2254 if (tif
->tif_flags
&TIFF_SWAB
)
2256 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,1,8,&m
));
2261 TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
2263 assert(count
<0x20000000);
2264 assert(sizeof(double)==8);
2265 TIFFCvtNativeToIEEEDouble(tif
,count
,&value
);
2266 if (tif
->tif_flags
&TIFF_SWAB
)
2267 TIFFSwabArrayOfDouble(value
,count
);
2268 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,count
,count
*8,value
));
2272 TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2274 assert(count
<0x40000000);
2275 assert(sizeof(uint32
)==4);
2276 if (tif
->tif_flags
&TIFF_SWAB
)
2277 TIFFSwabArrayOfLong(value
,count
);
2278 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD
,count
,count
*4,value
));
2282 TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2284 assert(count
<0x20000000);
2285 assert(sizeof(uint64
)==8);
2286 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2287 if (tif
->tif_flags
&TIFF_SWAB
)
2288 TIFFSwabArrayOfLong8(value
,count
);
2289 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD8
,count
,count
*8,value
));
2293 TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
)
2295 static const char module[] = "TIFFWriteDirectoryTagData";
2300 assert(dir
[m
].tdir_tag
!=tag
);
2301 if (dir
[m
].tdir_tag
>tag
)
2308 for (n
=*ndir
; n
>m
; n
--)
2311 dir
[m
].tdir_tag
=tag
;
2312 dir
[m
].tdir_type
=datatype
;
2313 dir
[m
].tdir_count
=count
;
2314 dir
[m
].tdir_offset
.toff_long8
= 0;
2315 if (datalength
<=((tif
->tif_flags
&TIFF_BIGTIFF
)?0x8U
:0x4U
))
2316 _TIFFmemcpy(&dir
[m
].tdir_offset
,data
,datalength
);
2320 na
=tif
->tif_dataoff
;
2322 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2324 if ((nb
<na
)||(nb
<datalength
))
2326 TIFFErrorExt(tif
->tif_clientdata
,module,"Maximum TIFF file size exceeded");
2329 if (!SeekOK(tif
,na
))
2331 TIFFErrorExt(tif
->tif_clientdata
,module,"IO error writing tag data");
2334 assert(datalength
<0x80000000UL
);
2335 if (!WriteOK(tif
,data
,(tmsize_t
)datalength
))
2337 TIFFErrorExt(tif
->tif_clientdata
,module,"IO error writing tag data");
2340 tif
->tif_dataoff
=nb
;
2341 if (tif
->tif_dataoff
&1)
2343 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2347 if (tif
->tif_flags
&TIFF_SWAB
)
2349 _TIFFmemcpy(&dir
[m
].tdir_offset
,&o
,4);
2353 dir
[m
].tdir_offset
.toff_long8
= na
;
2354 if (tif
->tif_flags
&TIFF_SWAB
)
2355 TIFFSwabLong8(&dir
[m
].tdir_offset
.toff_long8
);
2363 * Link the current directory into the directory chain for the file.
2366 TIFFLinkDirectory(TIFF
* tif
)
2368 static const char module[] = "TIFFLinkDirectory";
2370 tif
->tif_diroff
= (TIFFSeekFile(tif
,0,SEEK_END
)+1) &~ 1;
2375 if (tif
->tif_flags
& TIFF_INSUBIFD
)
2377 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2380 m
= (uint32
)tif
->tif_diroff
;
2381 if (tif
->tif_flags
& TIFF_SWAB
)
2383 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2384 if (!WriteOK(tif
, &m
, 4)) {
2385 TIFFErrorExt(tif
->tif_clientdata
, module,
2386 "Error writing SubIFD directory link");
2390 * Advance to the next SubIFD or, if this is
2391 * the last one configured, revert back to the
2392 * normal directory linkage.
2394 if (--tif
->tif_nsubifd
)
2395 tif
->tif_subifdoff
+= 4;
2397 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2403 m
= tif
->tif_diroff
;
2404 if (tif
->tif_flags
& TIFF_SWAB
)
2406 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2407 if (!WriteOK(tif
, &m
, 8)) {
2408 TIFFErrorExt(tif
->tif_clientdata
, module,
2409 "Error writing SubIFD directory link");
2413 * Advance to the next SubIFD or, if this is
2414 * the last one configured, revert back to the
2415 * normal directory linkage.
2417 if (--tif
->tif_nsubifd
)
2418 tif
->tif_subifdoff
+= 8;
2420 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2425 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2429 m
= (uint32
)(tif
->tif_diroff
);
2430 if (tif
->tif_flags
& TIFF_SWAB
)
2432 if (tif
->tif_header
.classic
.tiff_diroff
== 0) {
2434 * First directory, overwrite offset in header.
2436 tif
->tif_header
.classic
.tiff_diroff
= (uint32
) tif
->tif_diroff
;
2437 (void) TIFFSeekFile(tif
,4, SEEK_SET
);
2438 if (!WriteOK(tif
, &m
, 4)) {
2439 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2440 "Error writing TIFF header");
2446 * Not the first directory, search to the last and append.
2448 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
2453 if (!SeekOK(tif
, nextdir
) ||
2454 !ReadOK(tif
, &dircount
, 2)) {
2455 TIFFErrorExt(tif
->tif_clientdata
, module,
2456 "Error fetching directory count");
2459 if (tif
->tif_flags
& TIFF_SWAB
)
2460 TIFFSwabShort(&dircount
);
2461 (void) TIFFSeekFile(tif
,
2462 nextdir
+2+dircount
*12, SEEK_SET
);
2463 if (!ReadOK(tif
, &nextnextdir
, 4)) {
2464 TIFFErrorExt(tif
->tif_clientdata
, module,
2465 "Error fetching directory link");
2468 if (tif
->tif_flags
& TIFF_SWAB
)
2469 TIFFSwabLong(&nextnextdir
);
2472 (void) TIFFSeekFile(tif
,
2473 nextdir
+2+dircount
*12, SEEK_SET
);
2474 if (!WriteOK(tif
, &m
, 4)) {
2475 TIFFErrorExt(tif
->tif_clientdata
, module,
2476 "Error writing directory link");
2481 nextdir
=nextnextdir
;
2488 m
= tif
->tif_diroff
;
2489 if (tif
->tif_flags
& TIFF_SWAB
)
2491 if (tif
->tif_header
.big
.tiff_diroff
== 0) {
2493 * First directory, overwrite offset in header.
2495 tif
->tif_header
.big
.tiff_diroff
= tif
->tif_diroff
;
2496 (void) TIFFSeekFile(tif
,8, SEEK_SET
);
2497 if (!WriteOK(tif
, &m
, 8)) {
2498 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2499 "Error writing TIFF header");
2505 * Not the first directory, search to the last and append.
2507 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
2513 if (!SeekOK(tif
, nextdir
) ||
2514 !ReadOK(tif
, &dircount64
, 8)) {
2515 TIFFErrorExt(tif
->tif_clientdata
, module,
2516 "Error fetching directory count");
2519 if (tif
->tif_flags
& TIFF_SWAB
)
2520 TIFFSwabLong8(&dircount64
);
2521 if (dircount64
>0xFFFF)
2523 TIFFErrorExt(tif
->tif_clientdata
, module,
2524 "Sanity check on tag count failed, likely corrupt TIFF");
2527 dircount
=(uint16
)dircount64
;
2528 (void) TIFFSeekFile(tif
,
2529 nextdir
+8+dircount
*20, SEEK_SET
);
2530 if (!ReadOK(tif
, &nextnextdir
, 8)) {
2531 TIFFErrorExt(tif
->tif_clientdata
, module,
2532 "Error fetching directory link");
2535 if (tif
->tif_flags
& TIFF_SWAB
)
2536 TIFFSwabLong8(&nextnextdir
);
2539 (void) TIFFSeekFile(tif
,
2540 nextdir
+8+dircount
*20, SEEK_SET
);
2541 if (!WriteOK(tif
, &m
, 8)) {
2542 TIFFErrorExt(tif
->tif_clientdata
, module,
2543 "Error writing directory link");
2548 nextdir
=nextnextdir
;
2554 /************************************************************************/
2555 /* TIFFRewriteField() */
2557 /* Rewrite a field in the directory on disk without regard to */
2558 /* updating the TIFF directory structure in memory. Currently */
2559 /* only supported for field that already exist in the on-disk */
2560 /* directory. Mainly used for updating stripoffset / */
2561 /* stripbytecount values after the directory is already on */
2564 /* Returns zero on failure, and one on success. */
2565 /************************************************************************/
2568 _TIFFRewriteField(TIFF
* tif
, uint16 tag
, TIFFDataType in_datatype
,
2569 tmsize_t count
, void* data
)
2571 static const char module[] = "TIFFResetField";
2572 /* const TIFFField* fip = NULL; */
2575 uint8 direntry_raw
[20];
2576 uint16 entry_tag
= 0;
2577 uint16 entry_type
= 0;
2578 uint64 entry_count
= 0;
2579 uint64 entry_offset
= 0;
2580 int value_in_entry
= 0;
2582 uint8
*buf_to_write
= NULL
;
2583 TIFFDataType datatype
;
2585 /* -------------------------------------------------------------------- */
2586 /* Find field definition. */
2587 /* -------------------------------------------------------------------- */
2588 /*fip =*/ TIFFFindField(tif
, tag
, TIFF_ANY
);
2590 /* -------------------------------------------------------------------- */
2591 /* Do some checking this is a straight forward case. */
2592 /* -------------------------------------------------------------------- */
2595 TIFFErrorExt( tif
->tif_clientdata
, module,
2596 "Memory mapped files not currently supported for this operation." );
2600 if( tif
->tif_diroff
== 0 )
2602 TIFFErrorExt( tif
->tif_clientdata
, module,
2603 "Attempt to reset field on directory not already on disk." );
2607 /* -------------------------------------------------------------------- */
2608 /* Read the directory entry count. */
2609 /* -------------------------------------------------------------------- */
2610 if (!SeekOK(tif
, tif
->tif_diroff
)) {
2611 TIFFErrorExt(tif
->tif_clientdata
, module,
2612 "%s: Seek error accessing TIFF directory",
2617 read_offset
= tif
->tif_diroff
;
2619 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2621 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) {
2622 TIFFErrorExt(tif
->tif_clientdata
, module,
2623 "%s: Can not read TIFF directory count",
2627 if (tif
->tif_flags
& TIFF_SWAB
)
2628 TIFFSwabShort(&dircount
);
2633 if (!ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
2634 TIFFErrorExt(tif
->tif_clientdata
, module,
2635 "%s: Can not read TIFF directory count",
2639 if (tif
->tif_flags
& TIFF_SWAB
)
2640 TIFFSwabLong8(&dircount64
);
2641 dircount
= (uint16
)dircount64
;
2646 /* -------------------------------------------------------------------- */
2647 /* Read through directory to find target tag. */
2648 /* -------------------------------------------------------------------- */
2649 while( dircount
> 0 )
2651 if (!ReadOK(tif
, direntry_raw
, dirsize
)) {
2652 TIFFErrorExt(tif
->tif_clientdata
, module,
2653 "%s: Can not read TIFF directory entry.",
2658 memcpy( &entry_tag
, direntry_raw
+ 0, sizeof(uint16
) );
2659 if (tif
->tif_flags
&TIFF_SWAB
)
2660 TIFFSwabShort( &entry_tag
);
2662 if( entry_tag
== tag
)
2665 read_offset
+= dirsize
;
2668 if( entry_tag
!= tag
)
2670 TIFFErrorExt(tif
->tif_clientdata
, module,
2671 "%s: Could not find tag %d.",
2672 tif
->tif_name
, tag
);
2676 /* -------------------------------------------------------------------- */
2677 /* Extract the type, count and offset for this entry. */
2678 /* -------------------------------------------------------------------- */
2679 memcpy( &entry_type
, direntry_raw
+ 2, sizeof(uint16
) );
2680 if (tif
->tif_flags
&TIFF_SWAB
)
2681 TIFFSwabShort( &entry_type
);
2683 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2687 memcpy( &value
, direntry_raw
+ 4, sizeof(uint32
) );
2688 if (tif
->tif_flags
&TIFF_SWAB
)
2689 TIFFSwabLong( &value
);
2690 entry_count
= value
;
2692 memcpy( &value
, direntry_raw
+ 8, sizeof(uint32
) );
2693 if (tif
->tif_flags
&TIFF_SWAB
)
2694 TIFFSwabLong( &value
);
2695 entry_offset
= value
;
2699 memcpy( &entry_count
, direntry_raw
+ 4, sizeof(uint64
) );
2700 if (tif
->tif_flags
&TIFF_SWAB
)
2701 TIFFSwabLong8( &entry_count
);
2703 memcpy( &entry_offset
, direntry_raw
+ 12, sizeof(uint64
) );
2704 if (tif
->tif_flags
&TIFF_SWAB
)
2705 TIFFSwabLong8( &entry_offset
);
2708 /* -------------------------------------------------------------------- */
2709 /* What data type do we want to write this as? */
2710 /* -------------------------------------------------------------------- */
2711 if( TIFFDataWidth(in_datatype
) == 8 && !(tif
->tif_flags
&TIFF_BIGTIFF
) )
2713 if( in_datatype
== TIFF_LONG8
)
2714 datatype
= TIFF_LONG
;
2715 else if( in_datatype
== TIFF_SLONG8
)
2716 datatype
= TIFF_SLONG
;
2717 else if( in_datatype
== TIFF_IFD8
)
2718 datatype
= TIFF_IFD
;
2720 datatype
= in_datatype
;
2723 datatype
= in_datatype
;
2725 /* -------------------------------------------------------------------- */
2726 /* Prepare buffer of actual data to write. This includes */
2727 /* swabbing as needed. */
2728 /* -------------------------------------------------------------------- */
2730 (uint8
*)_TIFFCheckMalloc(tif
, count
, TIFFDataWidth(datatype
),
2731 "for field buffer.");
2735 if( datatype
== in_datatype
)
2736 memcpy( buf_to_write
, data
, count
* TIFFDataWidth(datatype
) );
2737 else if( datatype
== TIFF_SLONG
&& in_datatype
== TIFF_SLONG8
)
2741 for( i
= 0; i
< count
; i
++ )
2743 ((int32
*) buf_to_write
)[i
] =
2744 (int32
) ((int64
*) data
)[i
];
2745 if( (int64
) ((int32
*) buf_to_write
)[i
] != ((int64
*) data
)[i
] )
2747 _TIFFfree( buf_to_write
);
2748 TIFFErrorExt( tif
->tif_clientdata
, module,
2749 "Value exceeds 32bit range of output type." );
2754 else if( (datatype
== TIFF_LONG
&& in_datatype
== TIFF_LONG8
)
2755 || (datatype
== TIFF_IFD
&& in_datatype
== TIFF_IFD8
) )
2759 for( i
= 0; i
< count
; i
++ )
2761 ((uint32
*) buf_to_write
)[i
] =
2762 (uint32
) ((uint64
*) data
)[i
];
2763 if( (uint64
) ((uint32
*) buf_to_write
)[i
] != ((uint64
*) data
)[i
] )
2765 _TIFFfree( buf_to_write
);
2766 TIFFErrorExt( tif
->tif_clientdata
, module,
2767 "Value exceeds 32bit range of output type." );
2773 if( TIFFDataWidth(datatype
) > 1 && (tif
->tif_flags
&TIFF_SWAB
) )
2775 if( TIFFDataWidth(datatype
) == 2 )
2776 TIFFSwabArrayOfShort( (uint16
*) buf_to_write
, count
);
2777 else if( TIFFDataWidth(datatype
) == 4 )
2778 TIFFSwabArrayOfLong( (uint32
*) buf_to_write
, count
);
2779 else if( TIFFDataWidth(datatype
) == 8 )
2780 TIFFSwabArrayOfLong8( (uint64
*) buf_to_write
, count
);
2783 /* -------------------------------------------------------------------- */
2784 /* Is this a value that fits into the directory entry? */
2785 /* -------------------------------------------------------------------- */
2786 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2788 if( TIFFDataWidth(datatype
) * count
<= 4 )
2790 entry_offset
= read_offset
+ 8;
2796 if( TIFFDataWidth(datatype
) * count
<= 8 )
2798 entry_offset
= read_offset
+ 12;
2803 /* -------------------------------------------------------------------- */
2804 /* If the tag type, and count match, then we just write it out */
2805 /* over the old values without altering the directory entry at */
2807 /* -------------------------------------------------------------------- */
2808 if( entry_count
== (uint64
)count
&& entry_type
== (uint16
) datatype
)
2810 if (!SeekOK(tif
, entry_offset
)) {
2811 _TIFFfree( buf_to_write
);
2812 TIFFErrorExt(tif
->tif_clientdata
, module,
2813 "%s: Seek error accessing TIFF directory",
2817 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2818 _TIFFfree( buf_to_write
);
2819 TIFFErrorExt(tif
->tif_clientdata
, module,
2820 "Error writing directory link");
2824 _TIFFfree( buf_to_write
);
2828 /* -------------------------------------------------------------------- */
2829 /* Otherwise, we write the new tag data at the end of the file. */
2830 /* -------------------------------------------------------------------- */
2831 if( !value_in_entry
)
2833 entry_offset
= TIFFSeekFile(tif
,0,SEEK_END
);
2835 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2836 _TIFFfree( buf_to_write
);
2837 TIFFErrorExt(tif
->tif_clientdata
, module,
2838 "Error writing directory link");
2842 _TIFFfree( buf_to_write
);
2846 memcpy( &entry_offset
, buf_to_write
, count
*TIFFDataWidth(datatype
));
2849 /* -------------------------------------------------------------------- */
2850 /* Adjust the directory entry. */
2851 /* -------------------------------------------------------------------- */
2852 entry_type
= datatype
;
2853 memcpy( direntry_raw
+ 2, &entry_type
, sizeof(uint16
) );
2854 if (tif
->tif_flags
&TIFF_SWAB
)
2855 TIFFSwabShort( (uint16
*) (direntry_raw
+ 2) );
2857 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2861 value
= (uint32
) entry_count
;
2862 memcpy( direntry_raw
+ 4, &value
, sizeof(uint32
) );
2863 if (tif
->tif_flags
&TIFF_SWAB
)
2864 TIFFSwabLong( (uint32
*) (direntry_raw
+ 4) );
2866 value
= (uint32
) entry_offset
;
2867 memcpy( direntry_raw
+ 8, &value
, sizeof(uint32
) );
2868 if (tif
->tif_flags
&TIFF_SWAB
)
2869 TIFFSwabLong( (uint32
*) (direntry_raw
+ 8) );
2873 memcpy( direntry_raw
+ 4, &entry_count
, sizeof(uint64
) );
2874 if (tif
->tif_flags
&TIFF_SWAB
)
2875 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 4) );
2877 memcpy( direntry_raw
+ 12, &entry_offset
, sizeof(uint64
) );
2878 if (tif
->tif_flags
&TIFF_SWAB
)
2879 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 12) );
2882 /* -------------------------------------------------------------------- */
2883 /* Write the directory entry out to disk. */
2884 /* -------------------------------------------------------------------- */
2885 if (!SeekOK(tif
, read_offset
)) {
2886 TIFFErrorExt(tif
->tif_clientdata
, module,
2887 "%s: Seek error accessing TIFF directory",
2892 if (!WriteOK(tif
, direntry_raw
,dirsize
))
2894 TIFFErrorExt(tif
->tif_clientdata
, module,
2895 "%s: Can not write TIFF directory entry.",
2902 /* vim: set ts=8 sts=8 sw=8 noet: */