1 /* $Id: tif_dirwrite.c,v 1.85 2017-01-11 16:09:02 erouault Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * Directory Write Support Routines.
37 #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
38 #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
40 extern void TIFFCvtNativeToIEEEFloat(TIFF
* tif
, uint32 n
, float* fp
);
41 extern void TIFFCvtNativeToIEEEDouble(TIFF
* tif
, uint32 n
, double* dp
);
44 static int TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
);
46 static int TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
48 static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
51 static int TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
);
52 static int TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
54 static int TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
56 static int TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
58 static int TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
61 static int TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
63 static int TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
);
65 static int TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
67 static int TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
68 static int TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
);
69 static int TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
71 static int TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
73 static int TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
);
75 static int TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
77 static int TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
78 static int TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
80 static int TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
83 static int TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
85 static int TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
);
87 static int TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
90 static int TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
);
92 static int TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
94 static int TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
);
96 static int TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
);
97 static int TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
98 static int TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
99 static int TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
101 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
103 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
105 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
108 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
110 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
112 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
114 static int TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
116 static int TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
118 static int TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
119 static int TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
120 static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
122 static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
124 static int TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
125 static int TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
126 static int TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
128 static int TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
);
129 static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
131 static int TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
133 static int TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
135 static int TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
137 static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
);
138 static int TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
139 static int TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
);
141 static int TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
143 static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
);
144 static int TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
145 static int TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
147 static int TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
149 static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
);
151 static int TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
);
153 static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
155 static int TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
);
157 static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
);
158 static int TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
159 static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
160 static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
162 static int TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
164 static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
166 static int TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
168 static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
169 static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
170 static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
172 static int TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
);
174 static int TIFFLinkDirectory(TIFF
*);
177 * Write the contents of the current directory
178 * to the specified file. This routine doesn't
179 * handle overwriting a directory with auxiliary
180 * storage that's been changed.
183 TIFFWriteDirectory(TIFF
* tif
)
185 return TIFFWriteDirectorySec(tif
,TRUE
,TRUE
,NULL
);
189 * Similar to TIFFWriteDirectory(), writes the directory out
190 * but leaves all data structures in memory so that it can be
191 * written again. This will make a partially written TIFF file
192 * readable before it is successfully completed/closed.
195 TIFFCheckpointDirectory(TIFF
* tif
)
198 /* Setup the strips arrays, if they haven't already been. */
199 if (tif
->tif_dir
.td_stripoffset
== NULL
)
200 (void) TIFFSetupStrips(tif
);
201 rc
= TIFFWriteDirectorySec(tif
,TRUE
,FALSE
,NULL
);
202 (void) TIFFSetWriteOffset(tif
, TIFFSeekFile(tif
, 0, SEEK_END
));
207 TIFFWriteCustomDirectory(TIFF
* tif
, uint64
* pdiroff
)
209 return TIFFWriteDirectorySec(tif
,FALSE
,FALSE
,pdiroff
);
213 * Similar to TIFFWriteDirectory(), but if the directory has already
214 * been written once, it is relocated to the end of the file, in case it
215 * has changed in size. Note that this will result in the loss of the
216 * previously used directory space.
219 TIFFRewriteDirectory( TIFF
*tif
)
221 static const char module
[] = "TIFFRewriteDirectory";
223 /* We don't need to do anything special if it hasn't been written. */
224 if( tif
->tif_diroff
== 0 )
225 return TIFFWriteDirectory( tif
);
228 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
229 * will cause it to be added after this directories current pre-link.
232 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
234 if (tif
->tif_header
.classic
.tiff_diroff
== tif
->tif_diroff
)
236 tif
->tif_header
.classic
.tiff_diroff
= 0;
239 TIFFSeekFile(tif
,4,SEEK_SET
);
240 if (!WriteOK(tif
, &(tif
->tif_header
.classic
.tiff_diroff
),4))
242 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
243 "Error updating TIFF header");
250 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
255 if (!SeekOK(tif
, nextdir
) ||
256 !ReadOK(tif
, &dircount
, 2)) {
257 TIFFErrorExt(tif
->tif_clientdata
, module
,
258 "Error fetching directory count");
261 if (tif
->tif_flags
& TIFF_SWAB
)
262 TIFFSwabShort(&dircount
);
263 (void) TIFFSeekFile(tif
,
264 nextdir
+2+dircount
*12, SEEK_SET
);
265 if (!ReadOK(tif
, &nextnextdir
, 4)) {
266 TIFFErrorExt(tif
->tif_clientdata
, module
,
267 "Error fetching directory link");
270 if (tif
->tif_flags
& TIFF_SWAB
)
271 TIFFSwabLong(&nextnextdir
);
272 if (nextnextdir
==tif
->tif_diroff
)
276 (void) TIFFSeekFile(tif
,
277 nextdir
+2+dircount
*12, SEEK_SET
);
278 if (!WriteOK(tif
, &m
, 4)) {
279 TIFFErrorExt(tif
->tif_clientdata
, module
,
280 "Error writing directory link");
292 if (tif
->tif_header
.big
.tiff_diroff
== tif
->tif_diroff
)
294 tif
->tif_header
.big
.tiff_diroff
= 0;
297 TIFFSeekFile(tif
,8,SEEK_SET
);
298 if (!WriteOK(tif
, &(tif
->tif_header
.big
.tiff_diroff
),8))
300 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
301 "Error updating TIFF header");
308 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
314 if (!SeekOK(tif
, nextdir
) ||
315 !ReadOK(tif
, &dircount64
, 8)) {
316 TIFFErrorExt(tif
->tif_clientdata
, module
,
317 "Error fetching directory count");
320 if (tif
->tif_flags
& TIFF_SWAB
)
321 TIFFSwabLong8(&dircount64
);
322 if (dircount64
>0xFFFF)
324 TIFFErrorExt(tif
->tif_clientdata
, module
,
325 "Sanity check on tag count failed, likely corrupt TIFF");
328 dircount
=(uint16
)dircount64
;
329 (void) TIFFSeekFile(tif
,
330 nextdir
+8+dircount
*20, SEEK_SET
);
331 if (!ReadOK(tif
, &nextnextdir
, 8)) {
332 TIFFErrorExt(tif
->tif_clientdata
, module
,
333 "Error fetching directory link");
336 if (tif
->tif_flags
& TIFF_SWAB
)
337 TIFFSwabLong8(&nextnextdir
);
338 if (nextnextdir
==tif
->tif_diroff
)
342 (void) TIFFSeekFile(tif
,
343 nextdir
+8+dircount
*20, SEEK_SET
);
344 if (!WriteOK(tif
, &m
, 8)) {
345 TIFFErrorExt(tif
->tif_clientdata
, module
,
346 "Error writing directory link");
358 * Now use TIFFWriteDirectory() normally.
361 return TIFFWriteDirectory( tif
);
365 TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
)
367 static const char module
[] = "TIFFWriteDirectorySec";
373 if (tif
->tif_mode
== O_RDONLY
)
376 _TIFFFillStriles( tif
);
379 * Clear write state so that subsequent images with
380 * different characteristics get the right buffers
385 if (tif
->tif_flags
& TIFF_POSTENCODE
)
387 tif
->tif_flags
&= ~TIFF_POSTENCODE
;
388 if (!(*tif
->tif_postencode
)(tif
))
390 TIFFErrorExt(tif
->tif_clientdata
,module
,
391 "Error post-encoding before directory write");
395 (*tif
->tif_close
)(tif
); /* shutdown encoder */
397 * Flush any data that might have been written
398 * by the compression close+cleanup routines. But
399 * be careful not to write stuff if we didn't add data
400 * in the previous steps as the "rawcc" data may well be
401 * a previously read tile/strip in mixed read/write mode.
403 if (tif
->tif_rawcc
> 0
404 && (tif
->tif_flags
& TIFF_BEENWRITING
) != 0 )
406 if( !TIFFFlushData1(tif
) )
408 TIFFErrorExt(tif
->tif_clientdata
, module
,
409 "Error flushing data before directory write");
413 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
)
415 _TIFFfree(tif
->tif_rawdata
);
416 tif
->tif_rawdata
= NULL
;
418 tif
->tif_rawdatasize
= 0;
419 tif
->tif_rawdataoff
= 0;
420 tif
->tif_rawdataloaded
= 0;
422 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
);
432 if (TIFFFieldSet(tif
,FIELD_IMAGEDIMENSIONS
))
434 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEWIDTH
,tif
->tif_dir
.td_imagewidth
))
436 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGELENGTH
,tif
->tif_dir
.td_imagelength
))
439 if (TIFFFieldSet(tif
,FIELD_TILEDIMENSIONS
))
441 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILEWIDTH
,tif
->tif_dir
.td_tilewidth
))
443 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILELENGTH
,tif
->tif_dir
.td_tilelength
))
446 if (TIFFFieldSet(tif
,FIELD_RESOLUTION
))
448 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XRESOLUTION
,tif
->tif_dir
.td_xresolution
))
450 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YRESOLUTION
,tif
->tif_dir
.td_yresolution
))
453 if (TIFFFieldSet(tif
,FIELD_POSITION
))
455 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XPOSITION
,tif
->tif_dir
.td_xposition
))
457 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YPOSITION
,tif
->tif_dir
.td_yposition
))
460 if (TIFFFieldSet(tif
,FIELD_SUBFILETYPE
))
462 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_SUBFILETYPE
,tif
->tif_dir
.td_subfiletype
))
465 if (TIFFFieldSet(tif
,FIELD_BITSPERSAMPLE
))
467 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_BITSPERSAMPLE
,tif
->tif_dir
.td_bitspersample
))
470 if (TIFFFieldSet(tif
,FIELD_COMPRESSION
))
472 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_COMPRESSION
,tif
->tif_dir
.td_compression
))
475 if (TIFFFieldSet(tif
,FIELD_PHOTOMETRIC
))
477 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PHOTOMETRIC
,tif
->tif_dir
.td_photometric
))
480 if (TIFFFieldSet(tif
,FIELD_THRESHHOLDING
))
482 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_THRESHHOLDING
,tif
->tif_dir
.td_threshholding
))
485 if (TIFFFieldSet(tif
,FIELD_FILLORDER
))
487 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_FILLORDER
,tif
->tif_dir
.td_fillorder
))
490 if (TIFFFieldSet(tif
,FIELD_ORIENTATION
))
492 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_ORIENTATION
,tif
->tif_dir
.td_orientation
))
495 if (TIFFFieldSet(tif
,FIELD_SAMPLESPERPIXEL
))
497 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_SAMPLESPERPIXEL
,tif
->tif_dir
.td_samplesperpixel
))
500 if (TIFFFieldSet(tif
,FIELD_ROWSPERSTRIP
))
502 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_ROWSPERSTRIP
,tif
->tif_dir
.td_rowsperstrip
))
505 if (TIFFFieldSet(tif
,FIELD_MINSAMPLEVALUE
))
507 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MINSAMPLEVALUE
,tif
->tif_dir
.td_minsamplevalue
))
510 if (TIFFFieldSet(tif
,FIELD_MAXSAMPLEVALUE
))
512 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MAXSAMPLEVALUE
,tif
->tif_dir
.td_maxsamplevalue
))
515 if (TIFFFieldSet(tif
,FIELD_PLANARCONFIG
))
517 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PLANARCONFIG
,tif
->tif_dir
.td_planarconfig
))
520 if (TIFFFieldSet(tif
,FIELD_RESOLUTIONUNIT
))
522 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_RESOLUTIONUNIT
,tif
->tif_dir
.td_resolutionunit
))
525 if (TIFFFieldSet(tif
,FIELD_PAGENUMBER
))
527 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_PAGENUMBER
,2,&tif
->tif_dir
.td_pagenumber
[0]))
530 if (TIFFFieldSet(tif
,FIELD_STRIPBYTECOUNTS
))
534 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount
))
539 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount
))
543 if (TIFFFieldSet(tif
,FIELD_STRIPOFFSETS
))
547 /* td_stripoffset might be NULL in an odd OJPEG case. See
548 * tif_dirread.c around line 3634.
550 * If a) compression is OJPEG, b) it's not a tiled TIFF,
551 * and c) the number of strips is 1,
552 * then we tolerate the absence of stripoffsets tag,
553 * because, presumably, all required data is in the
554 * JpegInterchangeFormat stream.
555 * We can get here when using tiffset on such a file.
556 * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
558 if (tif
->tif_dir
.td_stripoffset
!= NULL
&&
559 !TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
564 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
568 if (TIFFFieldSet(tif
,FIELD_COLORMAP
))
570 if (!TIFFWriteDirectoryTagColormap(tif
,&ndir
,dir
))
573 if (TIFFFieldSet(tif
,FIELD_EXTRASAMPLES
))
575 if (tif
->tif_dir
.td_extrasamples
)
579 TIFFGetFieldDefaulted(tif
,TIFFTAG_EXTRASAMPLES
,&na
,&nb
);
580 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_EXTRASAMPLES
,na
,nb
))
584 if (TIFFFieldSet(tif
,FIELD_SAMPLEFORMAT
))
586 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_SAMPLEFORMAT
,tif
->tif_dir
.td_sampleformat
))
589 if (TIFFFieldSet(tif
,FIELD_SMINSAMPLEVALUE
))
591 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMINSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_sminsamplevalue
))
594 if (TIFFFieldSet(tif
,FIELD_SMAXSAMPLEVALUE
))
596 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMAXSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_smaxsamplevalue
))
599 if (TIFFFieldSet(tif
,FIELD_IMAGEDEPTH
))
601 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEDEPTH
,tif
->tif_dir
.td_imagedepth
))
604 if (TIFFFieldSet(tif
,FIELD_TILEDEPTH
))
606 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_TILEDEPTH
,tif
->tif_dir
.td_tiledepth
))
609 if (TIFFFieldSet(tif
,FIELD_HALFTONEHINTS
))
611 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_HALFTONEHINTS
,2,&tif
->tif_dir
.td_halftonehints
[0]))
614 if (TIFFFieldSet(tif
,FIELD_YCBCRSUBSAMPLING
))
616 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_YCBCRSUBSAMPLING
,2,&tif
->tif_dir
.td_ycbcrsubsampling
[0]))
619 if (TIFFFieldSet(tif
,FIELD_YCBCRPOSITIONING
))
621 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_YCBCRPOSITIONING
,tif
->tif_dir
.td_ycbcrpositioning
))
624 if (TIFFFieldSet(tif
,FIELD_REFBLACKWHITE
))
626 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,TIFFTAG_REFERENCEBLACKWHITE
,6,tif
->tif_dir
.td_refblackwhite
))
629 if (TIFFFieldSet(tif
,FIELD_TRANSFERFUNCTION
))
631 if (!TIFFWriteDirectoryTagTransferfunction(tif
,&ndir
,dir
))
634 if (TIFFFieldSet(tif
,FIELD_INKNAMES
))
636 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,TIFFTAG_INKNAMES
,tif
->tif_dir
.td_inknameslen
,tif
->tif_dir
.td_inknames
))
639 if (TIFFFieldSet(tif
,FIELD_SUBIFD
))
641 if (!TIFFWriteDirectoryTagSubifd(tif
,&ndir
,dir
))
646 for (n
=0; n
<tif
->tif_nfields
; n
++) {
648 o
= tif
->tif_fields
[n
];
649 if ((o
->field_bit
>=FIELD_CODEC
)&&(TIFFFieldSet(tif
,o
->field_bit
)))
651 switch (o
->get_field_type
)
653 case TIFF_SETGET_ASCII
:
657 assert(o
->field_type
==TIFF_ASCII
);
658 assert(o
->field_readcount
==TIFF_VARIABLE
);
659 assert(o
->field_passcount
==0);
660 TIFFGetField(tif
,o
->field_tag
,&pb
);
661 pa
=(uint32
)(strlen(pb
));
662 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,pa
,pb
))
666 case TIFF_SETGET_UINT16
:
669 assert(o
->field_type
==TIFF_SHORT
);
670 assert(o
->field_readcount
==1);
671 assert(o
->field_passcount
==0);
672 TIFFGetField(tif
,o
->field_tag
,&p
);
673 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,p
))
677 case TIFF_SETGET_UINT32
:
680 assert(o
->field_type
==TIFF_LONG
);
681 assert(o
->field_readcount
==1);
682 assert(o
->field_passcount
==0);
683 TIFFGetField(tif
,o
->field_tag
,&p
);
684 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,p
))
688 case TIFF_SETGET_C32_UINT8
:
692 assert(o
->field_type
==TIFF_UNDEFINED
);
693 assert(o
->field_readcount
==TIFF_VARIABLE2
);
694 assert(o
->field_passcount
==1);
695 TIFFGetField(tif
,o
->field_tag
,&pa
,&pb
);
696 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,pa
,pb
))
701 assert(0); /* we should never get here */
708 for (m
=0; m
<(uint32
)(tif
->tif_dir
.td_customValueCount
); m
++)
710 uint16 tag
= (uint16
)tif
->tif_dir
.td_customValues
[m
].info
->field_tag
;
711 uint32 count
= tif
->tif_dir
.td_customValues
[m
].count
;
712 switch (tif
->tif_dir
.td_customValues
[m
].info
->field_type
)
715 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
719 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
723 if (!TIFFWriteDirectoryTagByteArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
727 if (!TIFFWriteDirectoryTagSbyteArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
731 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
735 if (!TIFFWriteDirectoryTagSshortArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
739 if (!TIFFWriteDirectoryTagLongArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
743 if (!TIFFWriteDirectoryTagSlongArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
747 if (!TIFFWriteDirectoryTagLong8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
751 if (!TIFFWriteDirectoryTagSlong8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
755 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
759 if (!TIFFWriteDirectoryTagSrationalArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
763 if (!TIFFWriteDirectoryTagFloatArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
767 if (!TIFFWriteDirectoryTagDoubleArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
771 if (!TIFFWriteDirectoryTagIfdArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
775 if (!TIFFWriteDirectoryTagIfdIfd8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
779 assert(0); /* we should never get here */
785 dir
=_TIFFmalloc(ndir
*sizeof(TIFFDirEntry
));
788 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
793 if ((tif
->tif_diroff
==0)&&(!TIFFLinkDirectory(tif
)))
797 tif
->tif_diroff
=(TIFFSeekFile(tif
,0,SEEK_END
)+1)&(~((toff_t
)1));
799 *pdiroff
=tif
->tif_diroff
;
800 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
804 tif
->tif_dataoff
=tif
->tif_diroff
+dirsize
;
805 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
806 tif
->tif_dataoff
=(uint32
)tif
->tif_dataoff
;
807 if ((tif
->tif_dataoff
<tif
->tif_diroff
)||(tif
->tif_dataoff
<(uint64
)dirsize
))
809 TIFFErrorExt(tif
->tif_clientdata
,module
,"Maximum TIFF file size exceeded");
812 if (tif
->tif_dataoff
&1)
819 if (TIFFFieldSet(tif
,FIELD_SUBIFD
)&&(tif
->tif_subifdoff
==0))
823 for (na
=0, nb
=dir
; ; na
++, nb
++)
826 if (nb
->tdir_tag
==TIFFTAG_SUBIFD
)
829 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
830 tif
->tif_subifdoff
=tif
->tif_diroff
+2+na
*12+8;
832 tif
->tif_subifdoff
=tif
->tif_diroff
+8+na
*20+12;
835 dirmem
=_TIFFmalloc(dirsize
);
838 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
841 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
847 *(uint16
*)n
=(uint16
)ndir
;
848 if (tif
->tif_flags
&TIFF_SWAB
)
849 TIFFSwabShort((uint16
*)n
);
852 for (m
=0; m
<ndir
; m
++)
854 *(uint16
*)n
=o
->tdir_tag
;
855 if (tif
->tif_flags
&TIFF_SWAB
)
856 TIFFSwabShort((uint16
*)n
);
858 *(uint16
*)n
=o
->tdir_type
;
859 if (tif
->tif_flags
&TIFF_SWAB
)
860 TIFFSwabShort((uint16
*)n
);
862 nTmp
= (uint32
)o
->tdir_count
;
863 _TIFFmemcpy(n
,&nTmp
,4);
864 if (tif
->tif_flags
&TIFF_SWAB
)
865 TIFFSwabLong((uint32
*)n
);
867 /* This is correct. The data has been */
868 /* swabbed previously in TIFFWriteDirectoryTagData */
869 _TIFFmemcpy(n
,&o
->tdir_offset
,4);
873 nTmp
= (uint32
)tif
->tif_nextdiroff
;
874 if (tif
->tif_flags
&TIFF_SWAB
)
876 _TIFFmemcpy(n
,&nTmp
,4);
884 if (tif
->tif_flags
&TIFF_SWAB
)
885 TIFFSwabLong8((uint64
*)n
);
888 for (m
=0; m
<ndir
; m
++)
890 *(uint16
*)n
=o
->tdir_tag
;
891 if (tif
->tif_flags
&TIFF_SWAB
)
892 TIFFSwabShort((uint16
*)n
);
894 *(uint16
*)n
=o
->tdir_type
;
895 if (tif
->tif_flags
&TIFF_SWAB
)
896 TIFFSwabShort((uint16
*)n
);
898 _TIFFmemcpy(n
,&o
->tdir_count
,8);
899 if (tif
->tif_flags
&TIFF_SWAB
)
900 TIFFSwabLong8((uint64
*)n
);
902 _TIFFmemcpy(n
,&o
->tdir_offset
,8);
906 _TIFFmemcpy(n
,&tif
->tif_nextdiroff
,8);
907 if (tif
->tif_flags
&TIFF_SWAB
)
908 TIFFSwabLong8((uint64
*)n
);
912 if (!SeekOK(tif
,tif
->tif_diroff
))
914 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing directory");
917 if (!WriteOK(tif
,dirmem
,(tmsize_t
)dirsize
))
919 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing directory");
925 TIFFFreeDirectory(tif
);
926 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
927 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
928 (*tif
->tif_cleanup
)(tif
);
930 * Reset directory-related state for subsequent
933 TIFFCreateDirectory(tif
);
944 static float TIFFClampDoubleToFloat( double val
)
953 static int8
TIFFClampDoubleToInt8( double val
)
957 if( val
< -128 || val
!= val
)
962 static int16
TIFFClampDoubleToInt16( double val
)
966 if( val
< -32768 || val
!= val
)
971 static int32
TIFFClampDoubleToInt32( double val
)
973 if( val
> 0x7FFFFFFF )
975 if( val
< -0x7FFFFFFF-1 || val
!= val
)
976 return -0x7FFFFFFF-1;
980 static uint8
TIFFClampDoubleToUInt8( double val
)
984 if( val
> 255 || val
!= val
)
989 static uint16
TIFFClampDoubleToUInt16( double val
)
993 if( val
> 65535 || val
!= val
)
998 static uint32
TIFFClampDoubleToUInt32( double val
)
1002 if( val
> 0xFFFFFFFFU
|| val
!= val
)
1008 TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1010 static const char module
[] = "TIFFWriteDirectoryTagSampleformatArray";
1014 conv
= _TIFFmalloc(count
*sizeof(double));
1017 TIFFErrorExt(tif
->tif_clientdata
, module
, "Out of memory");
1021 switch (tif
->tif_dir
.td_sampleformat
)
1023 case SAMPLEFORMAT_IEEEFP
:
1024 if (tif
->tif_dir
.td_bitspersample
<=32)
1026 for (i
= 0; i
< count
; ++i
)
1027 ((float*)conv
)[i
] = TIFFClampDoubleToFloat(value
[i
]);
1028 ok
= TIFFWriteDirectoryTagFloatArray(tif
,ndir
,dir
,tag
,count
,(float*)conv
);
1032 ok
= TIFFWriteDirectoryTagDoubleArray(tif
,ndir
,dir
,tag
,count
,value
);
1035 case SAMPLEFORMAT_INT
:
1036 if (tif
->tif_dir
.td_bitspersample
<=8)
1038 for (i
= 0; i
< count
; ++i
)
1039 ((int8
*)conv
)[i
] = TIFFClampDoubleToInt8(value
[i
]);
1040 ok
= TIFFWriteDirectoryTagSbyteArray(tif
,ndir
,dir
,tag
,count
,(int8
*)conv
);
1042 else if (tif
->tif_dir
.td_bitspersample
<=16)
1044 for (i
= 0; i
< count
; ++i
)
1045 ((int16
*)conv
)[i
] = TIFFClampDoubleToInt16(value
[i
]);
1046 ok
= TIFFWriteDirectoryTagSshortArray(tif
,ndir
,dir
,tag
,count
,(int16
*)conv
);
1050 for (i
= 0; i
< count
; ++i
)
1051 ((int32
*)conv
)[i
] = TIFFClampDoubleToInt32(value
[i
]);
1052 ok
= TIFFWriteDirectoryTagSlongArray(tif
,ndir
,dir
,tag
,count
,(int32
*)conv
);
1055 case SAMPLEFORMAT_UINT
:
1056 if (tif
->tif_dir
.td_bitspersample
<=8)
1058 for (i
= 0; i
< count
; ++i
)
1059 ((uint8
*)conv
)[i
] = TIFFClampDoubleToUInt8(value
[i
]);
1060 ok
= TIFFWriteDirectoryTagByteArray(tif
,ndir
,dir
,tag
,count
,(uint8
*)conv
);
1062 else if (tif
->tif_dir
.td_bitspersample
<=16)
1064 for (i
= 0; i
< count
; ++i
)
1065 ((uint16
*)conv
)[i
] = TIFFClampDoubleToUInt16(value
[i
]);
1066 ok
= TIFFWriteDirectoryTagShortArray(tif
,ndir
,dir
,tag
,count
,(uint16
*)conv
);
1070 for (i
= 0; i
< count
; ++i
)
1071 ((uint32
*)conv
)[i
] = TIFFClampDoubleToUInt32(value
[i
]);
1072 ok
= TIFFWriteDirectoryTagLongArray(tif
,ndir
,dir
,tag
,count
,(uint32
*)conv
);
1085 TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1087 switch (tif
->tif_dir
.td_sampleformat
)
1089 case SAMPLEFORMAT_IEEEFP
:
1090 if (tif
->tif_dir
.td_bitspersample
<=32)
1091 return(TIFFWriteDirectoryTagFloatPerSample(tif
,ndir
,dir
,tag
,(float)value
));
1093 return(TIFFWriteDirectoryTagDoublePerSample(tif
,ndir
,dir
,tag
,value
));
1094 case SAMPLEFORMAT_INT
:
1095 if (tif
->tif_dir
.td_bitspersample
<=8)
1096 return(TIFFWriteDirectoryTagSbytePerSample(tif
,ndir
,dir
,tag
,(int8
)value
));
1097 else if (tif
->tif_dir
.td_bitspersample
<=16)
1098 return(TIFFWriteDirectoryTagSshortPerSample(tif
,ndir
,dir
,tag
,(int16
)value
));
1100 return(TIFFWriteDirectoryTagSlongPerSample(tif
,ndir
,dir
,tag
,(int32
)value
));
1101 case SAMPLEFORMAT_UINT
:
1102 if (tif
->tif_dir
.td_bitspersample
<=8)
1103 return(TIFFWriteDirectoryTagBytePerSample(tif
,ndir
,dir
,tag
,(uint8
)value
));
1104 else if (tif
->tif_dir
.td_bitspersample
<=16)
1105 return(TIFFWriteDirectoryTagShortPerSample(tif
,ndir
,dir
,tag
,(uint16
)value
));
1107 return(TIFFWriteDirectoryTagLongPerSample(tif
,ndir
,dir
,tag
,(uint32
)value
));
1115 TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1122 return(TIFFWriteDirectoryTagCheckedAscii(tif
,ndir
,dir
,tag
,count
,value
));
1126 TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1133 return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif
,ndir
,dir
,tag
,count
,value
));
1138 TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1145 return(TIFFWriteDirectoryTagCheckedByte(tif
,ndir
,dir
,tag
,value
));
1150 TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1157 return(TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,count
,value
));
1162 TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1164 static const char module
[] = "TIFFWriteDirectoryTagBytePerSample";
1174 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint8
));
1177 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1180 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1182 o
=TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1190 TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1197 return(TIFFWriteDirectoryTagCheckedSbyte(tif
,ndir
,dir
,tag
,value
));
1202 TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1209 return(TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,count
,value
));
1214 TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1216 static const char module
[] = "TIFFWriteDirectoryTagSbytePerSample";
1226 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int8
));
1229 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1232 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1234 o
=TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1241 TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1248 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,value
));
1252 TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1259 return(TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,value
));
1263 TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1265 static const char module
[] = "TIFFWriteDirectoryTagShortPerSample";
1275 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint16
));
1278 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1281 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1283 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1290 TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1297 return(TIFFWriteDirectoryTagCheckedSshort(tif
,ndir
,dir
,tag
,value
));
1302 TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1309 return(TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,count
,value
));
1314 TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1316 static const char module
[] = "TIFFWriteDirectoryTagSshortPerSample";
1326 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int16
));
1329 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1332 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1334 o
=TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1341 TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1348 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1352 TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1359 return(TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,value
));
1364 TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1366 static const char module
[] = "TIFFWriteDirectoryTagLongPerSample";
1376 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint32
));
1379 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1382 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1384 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1392 TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1399 return(TIFFWriteDirectoryTagCheckedSlong(tif
,ndir
,dir
,tag
,value
));
1404 TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
1411 return(TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,count
,value
));
1416 TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1418 static const char module
[] = "TIFFWriteDirectoryTagSlongPerSample";
1428 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int32
));
1431 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1434 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1436 o
=TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1444 TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
1451 return(TIFFWriteDirectoryTagCheckedLong8(tif
,ndir
,dir
,tag
,value
));
1456 TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1463 return(TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1468 TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
1475 return(TIFFWriteDirectoryTagCheckedSlong8(tif
,ndir
,dir
,tag
,value
));
1480 TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
1487 return(TIFFWriteDirectoryTagCheckedSlong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1491 TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1498 return(TIFFWriteDirectoryTagCheckedRational(tif
,ndir
,dir
,tag
,value
));
1502 TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1509 return(TIFFWriteDirectoryTagCheckedRationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1513 TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1520 return(TIFFWriteDirectoryTagCheckedSrationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1524 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1531 return(TIFFWriteDirectoryTagCheckedFloat(tif
,ndir
,dir
,tag
,value
));
1535 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1542 return(TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,count
,value
));
1546 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1548 static const char module
[] = "TIFFWriteDirectoryTagFloatPerSample";
1558 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(float));
1561 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1564 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1566 o
=TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1573 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1580 return(TIFFWriteDirectoryTagCheckedDouble(tif
,ndir
,dir
,tag
,value
));
1584 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1591 return(TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,count
,value
));
1595 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1597 static const char module
[] = "TIFFWriteDirectoryTagDoublePerSample";
1607 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(double));
1610 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1613 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1615 o
=TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1622 TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1629 return(TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,value
));
1634 TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1641 return(TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,tag
,count
,value
));
1646 TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1654 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,(uint16
)value
));
1656 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1659 /************************************************************************/
1660 /* TIFFWriteDirectoryTagLongLong8Array() */
1662 /* Write out LONG8 array as LONG8 for BigTIFF or LONG for */
1663 /* Classic TIFF with some checking. */
1664 /************************************************************************/
1667 TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1669 static const char module
[] = "TIFFWriteDirectoryTagLongLong8Array";
1676 /* is this just a counting pass? */
1683 /* We always write LONG8 for BigTIFF, no checking needed. */
1684 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1685 return TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,
1689 ** For classic tiff we want to verify everything is in range for LONG
1690 ** and convert to long format.
1693 p
= _TIFFmalloc(count
*sizeof(uint32
));
1696 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1700 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1704 TIFFErrorExt(tif
->tif_clientdata
,module
,
1705 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1712 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1718 /************************************************************************/
1719 /* TIFFWriteDirectoryTagIfdIfd8Array() */
1721 /* Write either IFD8 or IFD array depending on file type. */
1722 /************************************************************************/
1725 TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1727 static const char module
[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1734 /* is this just a counting pass? */
1741 /* We always write IFD8 for BigTIFF, no checking needed. */
1742 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1743 return TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,
1747 ** For classic tiff we want to verify everything is in range for IFD
1748 ** and convert to long format.
1751 p
= _TIFFmalloc(count
*sizeof(uint32
));
1754 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1758 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1762 TIFFErrorExt(tif
->tif_clientdata
,module
,
1763 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1770 o
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,p
);
1778 TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1780 static const char module
[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1791 for (ma
=value
, mb
=0; mb
<count
; ma
++, mb
++)
1793 if ((n
==0)&&(*ma
>0xFFFF))
1795 if ((n
==1)&&(*ma
>0xFFFFFFFF))
1805 p
=_TIFFmalloc(count
*sizeof(uint16
));
1808 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1811 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1813 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,p
);
1820 p
=_TIFFmalloc(count
*sizeof(uint32
));
1823 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1826 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1828 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1834 o
=TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
);
1840 TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1842 static const char module
[] = "TIFFWriteDirectoryTagColormap";
1851 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1852 n
=_TIFFmalloc(3*m
*sizeof(uint16
));
1855 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1858 _TIFFmemcpy(&n
[0],tif
->tif_dir
.td_colormap
[0],m
*sizeof(uint16
));
1859 _TIFFmemcpy(&n
[m
],tif
->tif_dir
.td_colormap
[1],m
*sizeof(uint16
));
1860 _TIFFmemcpy(&n
[2*m
],tif
->tif_dir
.td_colormap
[2],m
*sizeof(uint16
));
1861 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_COLORMAP
,3*m
,n
);
1867 TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1869 static const char module
[] = "TIFFWriteDirectoryTagTransferfunction";
1879 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1880 n
=tif
->tif_dir
.td_samplesperpixel
-tif
->tif_dir
.td_extrasamples
;
1882 * Check if the table can be written as a single column,
1883 * or if it must be written as 3 columns. Note that we
1884 * write a 3-column tag if there are 2 samples/pixel and
1885 * a single column of data won't suffice--hmm.
1891 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
)))
1896 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
)))
1901 o
=_TIFFmalloc(n
*m
*sizeof(uint16
));
1904 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1907 _TIFFmemcpy(&o
[0],tif
->tif_dir
.td_transferfunction
[0],m
*sizeof(uint16
));
1909 _TIFFmemcpy(&o
[m
],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
));
1911 _TIFFmemcpy(&o
[2*m
],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
));
1912 p
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_TRANSFERFUNCTION
,n
*m
,o
);
1918 TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1920 static const char module
[] = "TIFFWriteDirectoryTagSubifd";
1923 if (tif
->tif_dir
.td_nsubifd
==0)
1931 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1937 o
=_TIFFmalloc(tif
->tif_dir
.td_nsubifd
*sizeof(uint32
));
1940 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1943 pa
=tif
->tif_dir
.td_subifd
;
1945 for (p
=0; p
< tif
->tif_dir
.td_nsubifd
; p
++)
1948 assert(*pa
<= 0xFFFFFFFFUL
);
1949 *pb
++=(uint32
)(*pa
++);
1951 n
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,o
);
1955 n
=TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,tif
->tif_dir
.td_subifd
);
1959 * Total hack: if this directory includes a SubIFD
1960 * tag then force the next <n> directories to be
1961 * written as ``sub directories'' of this one. This
1962 * is used to write things like thumbnails and
1963 * image masks that one wants to keep out of the
1964 * normal directory linkage access mechanism.
1966 tif
->tif_flags
|=TIFF_INSUBIFD
;
1967 tif
->tif_nsubifd
=tif
->tif_dir
.td_nsubifd
;
1968 if (tif
->tif_dir
.td_nsubifd
==1)
1969 tif
->tif_subifdoff
=0;
1971 tif
->tif_subifdoff
=m
;
1976 TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1978 assert(sizeof(char)==1);
1979 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_ASCII
,count
,count
,value
));
1983 TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1985 assert(sizeof(uint8
)==1);
1986 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_UNDEFINED
,count
,count
,value
));
1991 TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1993 assert(sizeof(uint8
)==1);
1994 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,1,1,&value
));
1999 TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
2001 assert(sizeof(uint8
)==1);
2002 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,count
,count
,value
));
2007 TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
2009 assert(sizeof(int8
)==1);
2010 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,1,1,&value
));
2015 TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
2017 assert(sizeof(int8
)==1);
2018 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,count
,count
,value
));
2022 TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
2025 assert(sizeof(uint16
)==2);
2027 if (tif
->tif_flags
&TIFF_SWAB
)
2029 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,1,2,&m
));
2033 TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
2035 assert(count
<0x80000000);
2036 assert(sizeof(uint16
)==2);
2037 if (tif
->tif_flags
&TIFF_SWAB
)
2038 TIFFSwabArrayOfShort(value
,count
);
2039 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,count
,count
*2,value
));
2044 TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
2047 assert(sizeof(int16
)==2);
2049 if (tif
->tif_flags
&TIFF_SWAB
)
2050 TIFFSwabShort((uint16
*)(&m
));
2051 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,1,2,&m
));
2056 TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
2058 assert(count
<0x80000000);
2059 assert(sizeof(int16
)==2);
2060 if (tif
->tif_flags
&TIFF_SWAB
)
2061 TIFFSwabArrayOfShort((uint16
*)value
,count
);
2062 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,count
,count
*2,value
));
2066 TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
2069 assert(sizeof(uint32
)==4);
2071 if (tif
->tif_flags
&TIFF_SWAB
)
2073 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,1,4,&m
));
2077 TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2079 assert(count
<0x40000000);
2080 assert(sizeof(uint32
)==4);
2081 if (tif
->tif_flags
&TIFF_SWAB
)
2082 TIFFSwabArrayOfLong(value
,count
);
2083 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,count
,count
*4,value
));
2088 TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
2091 assert(sizeof(int32
)==4);
2093 if (tif
->tif_flags
&TIFF_SWAB
)
2094 TIFFSwabLong((uint32
*)(&m
));
2095 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,1,4,&m
));
2100 TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
2102 assert(count
<0x40000000);
2103 assert(sizeof(int32
)==4);
2104 if (tif
->tif_flags
&TIFF_SWAB
)
2105 TIFFSwabArrayOfLong((uint32
*)value
,count
);
2106 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,count
,count
*4,value
));
2111 TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
2114 assert(sizeof(uint64
)==8);
2115 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2117 if (tif
->tif_flags
&TIFF_SWAB
)
2119 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,1,8,&m
));
2124 TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2126 assert(count
<0x20000000);
2127 assert(sizeof(uint64
)==8);
2128 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2129 if (tif
->tif_flags
&TIFF_SWAB
)
2130 TIFFSwabArrayOfLong8(value
,count
);
2131 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,count
,count
*8,value
));
2136 TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
2139 assert(sizeof(int64
)==8);
2140 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2142 if (tif
->tif_flags
&TIFF_SWAB
)
2143 TIFFSwabLong8((uint64
*)(&m
));
2144 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,1,8,&m
));
2149 TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
2151 assert(count
<0x20000000);
2152 assert(sizeof(int64
)==8);
2153 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2154 if (tif
->tif_flags
&TIFF_SWAB
)
2155 TIFFSwabArrayOfLong8((uint64
*)value
,count
);
2156 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,count
,count
*8,value
));
2160 TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2162 static const char module
[] = "TIFFWriteDirectoryTagCheckedRational";
2164 assert(sizeof(uint32
)==4);
2167 TIFFErrorExt(tif
->tif_clientdata
,module
,"Negative value is illegal");
2170 else if( value
!= value
)
2172 TIFFErrorExt(tif
->tif_clientdata
,module
,"Not-a-number value is illegal");
2175 else if (value
==0.0)
2180 else if (value
<= 0xFFFFFFFFU
&& value
==(double)(uint32
)value
)
2187 m
[0]=(uint32
)(value
*0xFFFFFFFF);
2193 m
[1]=(uint32
)(0xFFFFFFFF/value
);
2195 if (tif
->tif_flags
&TIFF_SWAB
)
2197 TIFFSwabLong(&m
[0]);
2198 TIFFSwabLong(&m
[1]);
2200 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,1,8,&m
[0]));
2204 TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2206 static const char module
[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2212 assert(sizeof(uint32
)==4);
2213 m
=_TIFFmalloc(count
*2*sizeof(uint32
));
2216 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2219 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2221 if (*na
<=0.0 || *na
!= *na
)
2226 else if (*na
>= 0 && *na
<= (float)0xFFFFFFFFU
&&
2227 *na
==(float)(uint32
)(*na
))
2229 nb
[0]=(uint32
)(*na
);
2234 nb
[0]=(uint32
)((double)(*na
)*0xFFFFFFFF);
2240 nb
[1]=(uint32
)((double)0xFFFFFFFF/(*na
));
2243 if (tif
->tif_flags
&TIFF_SWAB
)
2244 TIFFSwabArrayOfLong(m
,count
*2);
2245 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,count
,count
*8,&m
[0]);
2251 TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2253 static const char module
[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2259 assert(sizeof(int32
)==4);
2260 m
=_TIFFmalloc(count
*2*sizeof(int32
));
2263 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2266 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2270 if (*na
==(int32
)(*na
))
2277 nb
[0]=-(int32
)((double)(-*na
)*0x7FFFFFFF);
2283 nb
[1]=(int32
)((double)0x7FFFFFFF/(-*na
));
2288 if (*na
==(int32
)(*na
))
2295 nb
[0]=(int32
)((double)(*na
)*0x7FFFFFFF);
2301 nb
[1]=(int32
)((double)0x7FFFFFFF/(*na
));
2305 if (tif
->tif_flags
&TIFF_SWAB
)
2306 TIFFSwabArrayOfLong((uint32
*)m
,count
*2);
2307 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SRATIONAL
,count
,count
*8,&m
[0]);
2314 TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
2317 assert(sizeof(float)==4);
2319 TIFFCvtNativeToIEEEFloat(tif
,1,&m
);
2320 if (tif
->tif_flags
&TIFF_SWAB
)
2322 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,1,4,&m
));
2327 TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2329 assert(count
<0x40000000);
2330 assert(sizeof(float)==4);
2331 TIFFCvtNativeToIEEEFloat(tif
,count
,&value
);
2332 if (tif
->tif_flags
&TIFF_SWAB
)
2333 TIFFSwabArrayOfFloat(value
,count
);
2334 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,count
,count
*4,value
));
2339 TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2342 assert(sizeof(double)==8);
2344 TIFFCvtNativeToIEEEDouble(tif
,1,&m
);
2345 if (tif
->tif_flags
&TIFF_SWAB
)
2347 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,1,8,&m
));
2352 TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
2354 assert(count
<0x20000000);
2355 assert(sizeof(double)==8);
2356 TIFFCvtNativeToIEEEDouble(tif
,count
,&value
);
2357 if (tif
->tif_flags
&TIFF_SWAB
)
2358 TIFFSwabArrayOfDouble(value
,count
);
2359 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,count
,count
*8,value
));
2363 TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2365 assert(count
<0x40000000);
2366 assert(sizeof(uint32
)==4);
2367 if (tif
->tif_flags
&TIFF_SWAB
)
2368 TIFFSwabArrayOfLong(value
,count
);
2369 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD
,count
,count
*4,value
));
2373 TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2375 assert(count
<0x20000000);
2376 assert(sizeof(uint64
)==8);
2377 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2378 if (tif
->tif_flags
&TIFF_SWAB
)
2379 TIFFSwabArrayOfLong8(value
,count
);
2380 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD8
,count
,count
*8,value
));
2384 TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
)
2386 static const char module
[] = "TIFFWriteDirectoryTagData";
2391 assert(dir
[m
].tdir_tag
!=tag
);
2392 if (dir
[m
].tdir_tag
>tag
)
2399 for (n
=*ndir
; n
>m
; n
--)
2402 dir
[m
].tdir_tag
=tag
;
2403 dir
[m
].tdir_type
=datatype
;
2404 dir
[m
].tdir_count
=count
;
2405 dir
[m
].tdir_offset
.toff_long8
= 0;
2406 if (datalength
<=((tif
->tif_flags
&TIFF_BIGTIFF
)?0x8U
:0x4U
))
2407 _TIFFmemcpy(&dir
[m
].tdir_offset
,data
,datalength
);
2411 na
=tif
->tif_dataoff
;
2413 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2415 if ((nb
<na
)||(nb
<datalength
))
2417 TIFFErrorExt(tif
->tif_clientdata
,module
,"Maximum TIFF file size exceeded");
2420 if (!SeekOK(tif
,na
))
2422 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing tag data");
2425 assert(datalength
<0x80000000UL
);
2426 if (!WriteOK(tif
,data
,(tmsize_t
)datalength
))
2428 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing tag data");
2431 tif
->tif_dataoff
=nb
;
2432 if (tif
->tif_dataoff
&1)
2434 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2438 if (tif
->tif_flags
&TIFF_SWAB
)
2440 _TIFFmemcpy(&dir
[m
].tdir_offset
,&o
,4);
2444 dir
[m
].tdir_offset
.toff_long8
= na
;
2445 if (tif
->tif_flags
&TIFF_SWAB
)
2446 TIFFSwabLong8(&dir
[m
].tdir_offset
.toff_long8
);
2454 * Link the current directory into the directory chain for the file.
2457 TIFFLinkDirectory(TIFF
* tif
)
2459 static const char module
[] = "TIFFLinkDirectory";
2461 tif
->tif_diroff
= (TIFFSeekFile(tif
,0,SEEK_END
)+1) & (~((toff_t
)1));
2466 if (tif
->tif_flags
& TIFF_INSUBIFD
)
2468 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2471 m
= (uint32
)tif
->tif_diroff
;
2472 if (tif
->tif_flags
& TIFF_SWAB
)
2474 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2475 if (!WriteOK(tif
, &m
, 4)) {
2476 TIFFErrorExt(tif
->tif_clientdata
, module
,
2477 "Error writing SubIFD directory link");
2481 * Advance to the next SubIFD or, if this is
2482 * the last one configured, revert back to the
2483 * normal directory linkage.
2485 if (--tif
->tif_nsubifd
)
2486 tif
->tif_subifdoff
+= 4;
2488 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2494 m
= tif
->tif_diroff
;
2495 if (tif
->tif_flags
& TIFF_SWAB
)
2497 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2498 if (!WriteOK(tif
, &m
, 8)) {
2499 TIFFErrorExt(tif
->tif_clientdata
, module
,
2500 "Error writing SubIFD directory link");
2504 * Advance to the next SubIFD or, if this is
2505 * the last one configured, revert back to the
2506 * normal directory linkage.
2508 if (--tif
->tif_nsubifd
)
2509 tif
->tif_subifdoff
+= 8;
2511 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2516 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2520 m
= (uint32
)(tif
->tif_diroff
);
2521 if (tif
->tif_flags
& TIFF_SWAB
)
2523 if (tif
->tif_header
.classic
.tiff_diroff
== 0) {
2525 * First directory, overwrite offset in header.
2527 tif
->tif_header
.classic
.tiff_diroff
= (uint32
) tif
->tif_diroff
;
2528 (void) TIFFSeekFile(tif
,4, SEEK_SET
);
2529 if (!WriteOK(tif
, &m
, 4)) {
2530 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2531 "Error writing TIFF header");
2537 * Not the first directory, search to the last and append.
2539 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
2544 if (!SeekOK(tif
, nextdir
) ||
2545 !ReadOK(tif
, &dircount
, 2)) {
2546 TIFFErrorExt(tif
->tif_clientdata
, module
,
2547 "Error fetching directory count");
2550 if (tif
->tif_flags
& TIFF_SWAB
)
2551 TIFFSwabShort(&dircount
);
2552 (void) TIFFSeekFile(tif
,
2553 nextdir
+2+dircount
*12, SEEK_SET
);
2554 if (!ReadOK(tif
, &nextnextdir
, 4)) {
2555 TIFFErrorExt(tif
->tif_clientdata
, module
,
2556 "Error fetching directory link");
2559 if (tif
->tif_flags
& TIFF_SWAB
)
2560 TIFFSwabLong(&nextnextdir
);
2563 (void) TIFFSeekFile(tif
,
2564 nextdir
+2+dircount
*12, SEEK_SET
);
2565 if (!WriteOK(tif
, &m
, 4)) {
2566 TIFFErrorExt(tif
->tif_clientdata
, module
,
2567 "Error writing directory link");
2572 nextdir
=nextnextdir
;
2579 m
= tif
->tif_diroff
;
2580 if (tif
->tif_flags
& TIFF_SWAB
)
2582 if (tif
->tif_header
.big
.tiff_diroff
== 0) {
2584 * First directory, overwrite offset in header.
2586 tif
->tif_header
.big
.tiff_diroff
= tif
->tif_diroff
;
2587 (void) TIFFSeekFile(tif
,8, SEEK_SET
);
2588 if (!WriteOK(tif
, &m
, 8)) {
2589 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2590 "Error writing TIFF header");
2596 * Not the first directory, search to the last and append.
2598 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
2604 if (!SeekOK(tif
, nextdir
) ||
2605 !ReadOK(tif
, &dircount64
, 8)) {
2606 TIFFErrorExt(tif
->tif_clientdata
, module
,
2607 "Error fetching directory count");
2610 if (tif
->tif_flags
& TIFF_SWAB
)
2611 TIFFSwabLong8(&dircount64
);
2612 if (dircount64
>0xFFFF)
2614 TIFFErrorExt(tif
->tif_clientdata
, module
,
2615 "Sanity check on tag count failed, likely corrupt TIFF");
2618 dircount
=(uint16
)dircount64
;
2619 (void) TIFFSeekFile(tif
,
2620 nextdir
+8+dircount
*20, SEEK_SET
);
2621 if (!ReadOK(tif
, &nextnextdir
, 8)) {
2622 TIFFErrorExt(tif
->tif_clientdata
, module
,
2623 "Error fetching directory link");
2626 if (tif
->tif_flags
& TIFF_SWAB
)
2627 TIFFSwabLong8(&nextnextdir
);
2630 (void) TIFFSeekFile(tif
,
2631 nextdir
+8+dircount
*20, SEEK_SET
);
2632 if (!WriteOK(tif
, &m
, 8)) {
2633 TIFFErrorExt(tif
->tif_clientdata
, module
,
2634 "Error writing directory link");
2639 nextdir
=nextnextdir
;
2645 /************************************************************************/
2646 /* TIFFRewriteField() */
2648 /* Rewrite a field in the directory on disk without regard to */
2649 /* updating the TIFF directory structure in memory. Currently */
2650 /* only supported for field that already exist in the on-disk */
2651 /* directory. Mainly used for updating stripoffset / */
2652 /* stripbytecount values after the directory is already on */
2655 /* Returns zero on failure, and one on success. */
2656 /************************************************************************/
2659 _TIFFRewriteField(TIFF
* tif
, uint16 tag
, TIFFDataType in_datatype
,
2660 tmsize_t count
, void* data
)
2662 static const char module
[] = "TIFFResetField";
2663 /* const TIFFField* fip = NULL; */
2666 uint8 direntry_raw
[20];
2667 uint16 entry_tag
= 0;
2668 uint16 entry_type
= 0;
2669 uint64 entry_count
= 0;
2670 uint64 entry_offset
= 0;
2671 int value_in_entry
= 0;
2673 uint8
*buf_to_write
= NULL
;
2674 TIFFDataType datatype
;
2676 /* -------------------------------------------------------------------- */
2677 /* Find field definition. */
2678 /* -------------------------------------------------------------------- */
2679 /*fip =*/ TIFFFindField(tif
, tag
, TIFF_ANY
);
2681 /* -------------------------------------------------------------------- */
2682 /* Do some checking this is a straight forward case. */
2683 /* -------------------------------------------------------------------- */
2686 TIFFErrorExt( tif
->tif_clientdata
, module
,
2687 "Memory mapped files not currently supported for this operation." );
2691 if( tif
->tif_diroff
== 0 )
2693 TIFFErrorExt( tif
->tif_clientdata
, module
,
2694 "Attempt to reset field on directory not already on disk." );
2698 /* -------------------------------------------------------------------- */
2699 /* Read the directory entry count. */
2700 /* -------------------------------------------------------------------- */
2701 if (!SeekOK(tif
, tif
->tif_diroff
)) {
2702 TIFFErrorExt(tif
->tif_clientdata
, module
,
2703 "%s: Seek error accessing TIFF directory",
2708 read_offset
= tif
->tif_diroff
;
2710 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2712 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) {
2713 TIFFErrorExt(tif
->tif_clientdata
, module
,
2714 "%s: Can not read TIFF directory count",
2718 if (tif
->tif_flags
& TIFF_SWAB
)
2719 TIFFSwabShort(&dircount
);
2724 if (!ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
2725 TIFFErrorExt(tif
->tif_clientdata
, module
,
2726 "%s: Can not read TIFF directory count",
2730 if (tif
->tif_flags
& TIFF_SWAB
)
2731 TIFFSwabLong8(&dircount64
);
2732 dircount
= (uint16
)dircount64
;
2737 /* -------------------------------------------------------------------- */
2738 /* Read through directory to find target tag. */
2739 /* -------------------------------------------------------------------- */
2740 while( dircount
> 0 )
2742 if (!ReadOK(tif
, direntry_raw
, dirsize
)) {
2743 TIFFErrorExt(tif
->tif_clientdata
, module
,
2744 "%s: Can not read TIFF directory entry.",
2749 memcpy( &entry_tag
, direntry_raw
+ 0, sizeof(uint16
) );
2750 if (tif
->tif_flags
&TIFF_SWAB
)
2751 TIFFSwabShort( &entry_tag
);
2753 if( entry_tag
== tag
)
2756 read_offset
+= dirsize
;
2759 if( entry_tag
!= tag
)
2761 TIFFErrorExt(tif
->tif_clientdata
, module
,
2762 "%s: Could not find tag %d.",
2763 tif
->tif_name
, tag
);
2767 /* -------------------------------------------------------------------- */
2768 /* Extract the type, count and offset for this entry. */
2769 /* -------------------------------------------------------------------- */
2770 memcpy( &entry_type
, direntry_raw
+ 2, sizeof(uint16
) );
2771 if (tif
->tif_flags
&TIFF_SWAB
)
2772 TIFFSwabShort( &entry_type
);
2774 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2778 memcpy( &value
, direntry_raw
+ 4, sizeof(uint32
) );
2779 if (tif
->tif_flags
&TIFF_SWAB
)
2780 TIFFSwabLong( &value
);
2781 entry_count
= value
;
2783 memcpy( &value
, direntry_raw
+ 8, sizeof(uint32
) );
2784 if (tif
->tif_flags
&TIFF_SWAB
)
2785 TIFFSwabLong( &value
);
2786 entry_offset
= value
;
2790 memcpy( &entry_count
, direntry_raw
+ 4, sizeof(uint64
) );
2791 if (tif
->tif_flags
&TIFF_SWAB
)
2792 TIFFSwabLong8( &entry_count
);
2794 memcpy( &entry_offset
, direntry_raw
+ 12, sizeof(uint64
) );
2795 if (tif
->tif_flags
&TIFF_SWAB
)
2796 TIFFSwabLong8( &entry_offset
);
2799 /* -------------------------------------------------------------------- */
2800 /* What data type do we want to write this as? */
2801 /* -------------------------------------------------------------------- */
2802 if( TIFFDataWidth(in_datatype
) == 8 && !(tif
->tif_flags
&TIFF_BIGTIFF
) )
2804 if( in_datatype
== TIFF_LONG8
)
2805 datatype
= TIFF_LONG
;
2806 else if( in_datatype
== TIFF_SLONG8
)
2807 datatype
= TIFF_SLONG
;
2808 else if( in_datatype
== TIFF_IFD8
)
2809 datatype
= TIFF_IFD
;
2811 datatype
= in_datatype
;
2814 datatype
= in_datatype
;
2816 /* -------------------------------------------------------------------- */
2817 /* Prepare buffer of actual data to write. This includes */
2818 /* swabbing as needed. */
2819 /* -------------------------------------------------------------------- */
2821 (uint8
*)_TIFFCheckMalloc(tif
, count
, TIFFDataWidth(datatype
),
2822 "for field buffer.");
2826 if( datatype
== in_datatype
)
2827 memcpy( buf_to_write
, data
, count
* TIFFDataWidth(datatype
) );
2828 else if( datatype
== TIFF_SLONG
&& in_datatype
== TIFF_SLONG8
)
2832 for( i
= 0; i
< count
; i
++ )
2834 ((int32
*) buf_to_write
)[i
] =
2835 (int32
) ((int64
*) data
)[i
];
2836 if( (int64
) ((int32
*) buf_to_write
)[i
] != ((int64
*) data
)[i
] )
2838 _TIFFfree( buf_to_write
);
2839 TIFFErrorExt( tif
->tif_clientdata
, module
,
2840 "Value exceeds 32bit range of output type." );
2845 else if( (datatype
== TIFF_LONG
&& in_datatype
== TIFF_LONG8
)
2846 || (datatype
== TIFF_IFD
&& in_datatype
== TIFF_IFD8
) )
2850 for( i
= 0; i
< count
; i
++ )
2852 ((uint32
*) buf_to_write
)[i
] =
2853 (uint32
) ((uint64
*) data
)[i
];
2854 if( (uint64
) ((uint32
*) buf_to_write
)[i
] != ((uint64
*) data
)[i
] )
2856 _TIFFfree( buf_to_write
);
2857 TIFFErrorExt( tif
->tif_clientdata
, module
,
2858 "Value exceeds 32bit range of output type." );
2864 if( TIFFDataWidth(datatype
) > 1 && (tif
->tif_flags
&TIFF_SWAB
) )
2866 if( TIFFDataWidth(datatype
) == 2 )
2867 TIFFSwabArrayOfShort( (uint16
*) buf_to_write
, count
);
2868 else if( TIFFDataWidth(datatype
) == 4 )
2869 TIFFSwabArrayOfLong( (uint32
*) buf_to_write
, count
);
2870 else if( TIFFDataWidth(datatype
) == 8 )
2871 TIFFSwabArrayOfLong8( (uint64
*) buf_to_write
, count
);
2874 /* -------------------------------------------------------------------- */
2875 /* Is this a value that fits into the directory entry? */
2876 /* -------------------------------------------------------------------- */
2877 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2879 if( TIFFDataWidth(datatype
) * count
<= 4 )
2881 entry_offset
= read_offset
+ 8;
2887 if( TIFFDataWidth(datatype
) * count
<= 8 )
2889 entry_offset
= read_offset
+ 12;
2894 /* -------------------------------------------------------------------- */
2895 /* If the tag type, and count match, then we just write it out */
2896 /* over the old values without altering the directory entry at */
2898 /* -------------------------------------------------------------------- */
2899 if( entry_count
== (uint64
)count
&& entry_type
== (uint16
) datatype
)
2901 if (!SeekOK(tif
, entry_offset
)) {
2902 _TIFFfree( buf_to_write
);
2903 TIFFErrorExt(tif
->tif_clientdata
, module
,
2904 "%s: Seek error accessing TIFF directory",
2908 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2909 _TIFFfree( buf_to_write
);
2910 TIFFErrorExt(tif
->tif_clientdata
, module
,
2911 "Error writing directory link");
2915 _TIFFfree( buf_to_write
);
2919 /* -------------------------------------------------------------------- */
2920 /* Otherwise, we write the new tag data at the end of the file. */
2921 /* -------------------------------------------------------------------- */
2922 if( !value_in_entry
)
2924 entry_offset
= TIFFSeekFile(tif
,0,SEEK_END
);
2926 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2927 _TIFFfree( buf_to_write
);
2928 TIFFErrorExt(tif
->tif_clientdata
, module
,
2929 "Error writing directory link");
2935 memcpy( &entry_offset
, buf_to_write
, count
*TIFFDataWidth(datatype
));
2938 _TIFFfree( buf_to_write
);
2941 /* -------------------------------------------------------------------- */
2942 /* Adjust the directory entry. */
2943 /* -------------------------------------------------------------------- */
2944 entry_type
= datatype
;
2945 memcpy( direntry_raw
+ 2, &entry_type
, sizeof(uint16
) );
2946 if (tif
->tif_flags
&TIFF_SWAB
)
2947 TIFFSwabShort( (uint16
*) (direntry_raw
+ 2) );
2949 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2953 value
= (uint32
) entry_count
;
2954 memcpy( direntry_raw
+ 4, &value
, sizeof(uint32
) );
2955 if (tif
->tif_flags
&TIFF_SWAB
)
2956 TIFFSwabLong( (uint32
*) (direntry_raw
+ 4) );
2958 value
= (uint32
) entry_offset
;
2959 memcpy( direntry_raw
+ 8, &value
, sizeof(uint32
) );
2960 if (tif
->tif_flags
&TIFF_SWAB
)
2961 TIFFSwabLong( (uint32
*) (direntry_raw
+ 8) );
2965 memcpy( direntry_raw
+ 4, &entry_count
, sizeof(uint64
) );
2966 if (tif
->tif_flags
&TIFF_SWAB
)
2967 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 4) );
2969 memcpy( direntry_raw
+ 12, &entry_offset
, sizeof(uint64
) );
2970 if (tif
->tif_flags
&TIFF_SWAB
)
2971 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 12) );
2974 /* -------------------------------------------------------------------- */
2975 /* Write the directory entry out to disk. */
2976 /* -------------------------------------------------------------------- */
2977 if (!SeekOK(tif
, read_offset
)) {
2978 TIFFErrorExt(tif
->tif_clientdata
, module
,
2979 "%s: Seek error accessing TIFF directory",
2984 if (!WriteOK(tif
, direntry_raw
,dirsize
))
2986 TIFFErrorExt(tif
->tif_clientdata
, module
,
2987 "%s: Can not write TIFF directory entry.",
2994 /* vim: set ts=8 sts=8 sw=8 noet: */