2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * 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 * This is an advanced writing function that must be used in a particular
187 * sequence, and generally together with TIFFForceStrileArrayWriting(),
188 * to make its intended effect. Its aim is to modify the location
189 * where the [Strip/Tile][Offsets/ByteCounts] arrays are located in the file.
190 * More precisely, when TIFFWriteCheck() will be called, the tag entries for
191 * those arrays will be written with type = count = offset = 0 as a temporary
194 * Its effect is only valid for the current directory, and before
195 * TIFFWriteDirectory() is first called, and will be reset when
196 * changing directory.
198 * The typical sequence of calls is:
200 * [ TIFFCreateDirectory(tif) ]
201 * Set fields with calls to TIFFSetField(tif, ...)
202 * TIFFDeferStrileArrayWriting(tif)
203 * TIFFWriteCheck(tif, ...)
204 * TIFFWriteDirectory(tif)
205 * ... potentially create other directories and come back to the above directory
206 * TIFFForceStrileArrayWriting(tif): emit the arrays at the end of file
208 * Returns 1 in case of success, 0 otherwise.
210 int TIFFDeferStrileArrayWriting(TIFF
* tif
)
212 static const char module
[] = "TIFFDeferStrileArrayWriting";
213 if (tif
->tif_mode
== O_RDONLY
)
215 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
216 "File opened in read-only mode");
219 if( tif
->tif_diroff
!= 0 )
221 TIFFErrorExt(tif
->tif_clientdata
, module
,
222 "Directory has already been written");
226 tif
->tif_dir
.td_deferstrilearraywriting
= TRUE
;
231 * Similar to TIFFWriteDirectory(), writes the directory out
232 * but leaves all data structures in memory so that it can be
233 * written again. This will make a partially written TIFF file
234 * readable before it is successfully completed/closed.
237 TIFFCheckpointDirectory(TIFF
* tif
)
240 /* Setup the strips arrays, if they haven't already been. */
241 if (tif
->tif_dir
.td_stripoffset_p
== NULL
)
242 (void) TIFFSetupStrips(tif
);
243 rc
= TIFFWriteDirectorySec(tif
,TRUE
,FALSE
,NULL
);
244 (void) TIFFSetWriteOffset(tif
, TIFFSeekFile(tif
, 0, SEEK_END
));
249 TIFFWriteCustomDirectory(TIFF
* tif
, uint64
* pdiroff
)
251 return TIFFWriteDirectorySec(tif
,FALSE
,FALSE
,pdiroff
);
255 * Similar to TIFFWriteDirectory(), but if the directory has already
256 * been written once, it is relocated to the end of the file, in case it
257 * has changed in size. Note that this will result in the loss of the
258 * previously used directory space.
261 TIFFRewriteDirectory( TIFF
*tif
)
263 static const char module
[] = "TIFFRewriteDirectory";
265 /* We don't need to do anything special if it hasn't been written. */
266 if( tif
->tif_diroff
== 0 )
267 return TIFFWriteDirectory( tif
);
270 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
271 * will cause it to be added after this directories current pre-link.
274 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
276 if (tif
->tif_header
.classic
.tiff_diroff
== tif
->tif_diroff
)
278 tif
->tif_header
.classic
.tiff_diroff
= 0;
281 TIFFSeekFile(tif
,4,SEEK_SET
);
282 if (!WriteOK(tif
, &(tif
->tif_header
.classic
.tiff_diroff
),4))
284 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
285 "Error updating TIFF header");
292 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
297 if (!SeekOK(tif
, nextdir
) ||
298 !ReadOK(tif
, &dircount
, 2)) {
299 TIFFErrorExt(tif
->tif_clientdata
, module
,
300 "Error fetching directory count");
303 if (tif
->tif_flags
& TIFF_SWAB
)
304 TIFFSwabShort(&dircount
);
305 (void) TIFFSeekFile(tif
,
306 nextdir
+2+dircount
*12, SEEK_SET
);
307 if (!ReadOK(tif
, &nextnextdir
, 4)) {
308 TIFFErrorExt(tif
->tif_clientdata
, module
,
309 "Error fetching directory link");
312 if (tif
->tif_flags
& TIFF_SWAB
)
313 TIFFSwabLong(&nextnextdir
);
314 if (nextnextdir
==tif
->tif_diroff
)
318 (void) TIFFSeekFile(tif
,
319 nextdir
+2+dircount
*12, SEEK_SET
);
320 if (!WriteOK(tif
, &m
, 4)) {
321 TIFFErrorExt(tif
->tif_clientdata
, module
,
322 "Error writing directory link");
334 if (tif
->tif_header
.big
.tiff_diroff
== tif
->tif_diroff
)
336 tif
->tif_header
.big
.tiff_diroff
= 0;
339 TIFFSeekFile(tif
,8,SEEK_SET
);
340 if (!WriteOK(tif
, &(tif
->tif_header
.big
.tiff_diroff
),8))
342 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
343 "Error updating TIFF header");
350 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
356 if (!SeekOK(tif
, nextdir
) ||
357 !ReadOK(tif
, &dircount64
, 8)) {
358 TIFFErrorExt(tif
->tif_clientdata
, module
,
359 "Error fetching directory count");
362 if (tif
->tif_flags
& TIFF_SWAB
)
363 TIFFSwabLong8(&dircount64
);
364 if (dircount64
>0xFFFF)
366 TIFFErrorExt(tif
->tif_clientdata
, module
,
367 "Sanity check on tag count failed, likely corrupt TIFF");
370 dircount
=(uint16
)dircount64
;
371 (void) TIFFSeekFile(tif
,
372 nextdir
+8+dircount
*20, SEEK_SET
);
373 if (!ReadOK(tif
, &nextnextdir
, 8)) {
374 TIFFErrorExt(tif
->tif_clientdata
, module
,
375 "Error fetching directory link");
378 if (tif
->tif_flags
& TIFF_SWAB
)
379 TIFFSwabLong8(&nextnextdir
);
380 if (nextnextdir
==tif
->tif_diroff
)
384 (void) TIFFSeekFile(tif
,
385 nextdir
+8+dircount
*20, SEEK_SET
);
386 if (!WriteOK(tif
, &m
, 8)) {
387 TIFFErrorExt(tif
->tif_clientdata
, module
,
388 "Error writing directory link");
400 * Now use TIFFWriteDirectory() normally.
403 return TIFFWriteDirectory( tif
);
407 TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
)
409 static const char module
[] = "TIFFWriteDirectorySec";
415 if (tif
->tif_mode
== O_RDONLY
)
418 _TIFFFillStriles( tif
);
421 * Clear write state so that subsequent images with
422 * different characteristics get the right buffers
427 if (tif
->tif_flags
& TIFF_POSTENCODE
)
429 tif
->tif_flags
&= ~TIFF_POSTENCODE
;
430 if (!(*tif
->tif_postencode
)(tif
))
432 TIFFErrorExt(tif
->tif_clientdata
,module
,
433 "Error post-encoding before directory write");
437 (*tif
->tif_close
)(tif
); /* shutdown encoder */
439 * Flush any data that might have been written
440 * by the compression close+cleanup routines. But
441 * be careful not to write stuff if we didn't add data
442 * in the previous steps as the "rawcc" data may well be
443 * a previously read tile/strip in mixed read/write mode.
445 if (tif
->tif_rawcc
> 0
446 && (tif
->tif_flags
& TIFF_BEENWRITING
) != 0 )
448 if( !TIFFFlushData1(tif
) )
450 TIFFErrorExt(tif
->tif_clientdata
, module
,
451 "Error flushing data before directory write");
455 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
)
457 _TIFFfree(tif
->tif_rawdata
);
458 tif
->tif_rawdata
= NULL
;
460 tif
->tif_rawdatasize
= 0;
461 tif
->tif_rawdataoff
= 0;
462 tif
->tif_rawdataloaded
= 0;
464 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
);
474 if (TIFFFieldSet(tif
,FIELD_IMAGEDIMENSIONS
))
476 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEWIDTH
,tif
->tif_dir
.td_imagewidth
))
478 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGELENGTH
,tif
->tif_dir
.td_imagelength
))
481 if (TIFFFieldSet(tif
,FIELD_TILEDIMENSIONS
))
483 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILEWIDTH
,tif
->tif_dir
.td_tilewidth
))
485 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILELENGTH
,tif
->tif_dir
.td_tilelength
))
488 if (TIFFFieldSet(tif
,FIELD_RESOLUTION
))
490 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XRESOLUTION
,tif
->tif_dir
.td_xresolution
))
492 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YRESOLUTION
,tif
->tif_dir
.td_yresolution
))
495 if (TIFFFieldSet(tif
,FIELD_POSITION
))
497 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XPOSITION
,tif
->tif_dir
.td_xposition
))
499 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YPOSITION
,tif
->tif_dir
.td_yposition
))
502 if (TIFFFieldSet(tif
,FIELD_SUBFILETYPE
))
504 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_SUBFILETYPE
,tif
->tif_dir
.td_subfiletype
))
507 if (TIFFFieldSet(tif
,FIELD_BITSPERSAMPLE
))
509 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_BITSPERSAMPLE
,tif
->tif_dir
.td_bitspersample
))
512 if (TIFFFieldSet(tif
,FIELD_COMPRESSION
))
514 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_COMPRESSION
,tif
->tif_dir
.td_compression
))
517 if (TIFFFieldSet(tif
,FIELD_PHOTOMETRIC
))
519 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PHOTOMETRIC
,tif
->tif_dir
.td_photometric
))
522 if (TIFFFieldSet(tif
,FIELD_THRESHHOLDING
))
524 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_THRESHHOLDING
,tif
->tif_dir
.td_threshholding
))
527 if (TIFFFieldSet(tif
,FIELD_FILLORDER
))
529 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_FILLORDER
,tif
->tif_dir
.td_fillorder
))
532 if (TIFFFieldSet(tif
,FIELD_ORIENTATION
))
534 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_ORIENTATION
,tif
->tif_dir
.td_orientation
))
537 if (TIFFFieldSet(tif
,FIELD_SAMPLESPERPIXEL
))
539 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_SAMPLESPERPIXEL
,tif
->tif_dir
.td_samplesperpixel
))
542 if (TIFFFieldSet(tif
,FIELD_ROWSPERSTRIP
))
544 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_ROWSPERSTRIP
,tif
->tif_dir
.td_rowsperstrip
))
547 if (TIFFFieldSet(tif
,FIELD_MINSAMPLEVALUE
))
549 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MINSAMPLEVALUE
,tif
->tif_dir
.td_minsamplevalue
))
552 if (TIFFFieldSet(tif
,FIELD_MAXSAMPLEVALUE
))
554 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MAXSAMPLEVALUE
,tif
->tif_dir
.td_maxsamplevalue
))
557 if (TIFFFieldSet(tif
,FIELD_PLANARCONFIG
))
559 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PLANARCONFIG
,tif
->tif_dir
.td_planarconfig
))
562 if (TIFFFieldSet(tif
,FIELD_RESOLUTIONUNIT
))
564 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_RESOLUTIONUNIT
,tif
->tif_dir
.td_resolutionunit
))
567 if (TIFFFieldSet(tif
,FIELD_PAGENUMBER
))
569 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_PAGENUMBER
,2,&tif
->tif_dir
.td_pagenumber
[0]))
572 if (TIFFFieldSet(tif
,FIELD_STRIPBYTECOUNTS
))
576 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount_p
))
581 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount_p
))
585 if (TIFFFieldSet(tif
,FIELD_STRIPOFFSETS
))
589 /* td_stripoffset_p might be NULL in an odd OJPEG case. See
590 * tif_dirread.c around line 3634.
592 * If a) compression is OJPEG, b) it's not a tiled TIFF,
593 * and c) the number of strips is 1,
594 * then we tolerate the absence of stripoffsets tag,
595 * because, presumably, all required data is in the
596 * JpegInterchangeFormat stream.
597 * We can get here when using tiffset on such a file.
598 * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
600 if (tif
->tif_dir
.td_stripoffset_p
!= NULL
&&
601 !TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset_p
))
606 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset_p
))
610 if (TIFFFieldSet(tif
,FIELD_COLORMAP
))
612 if (!TIFFWriteDirectoryTagColormap(tif
,&ndir
,dir
))
615 if (TIFFFieldSet(tif
,FIELD_EXTRASAMPLES
))
617 if (tif
->tif_dir
.td_extrasamples
)
621 TIFFGetFieldDefaulted(tif
,TIFFTAG_EXTRASAMPLES
,&na
,&nb
);
622 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_EXTRASAMPLES
,na
,nb
))
626 if (TIFFFieldSet(tif
,FIELD_SAMPLEFORMAT
))
628 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_SAMPLEFORMAT
,tif
->tif_dir
.td_sampleformat
))
631 if (TIFFFieldSet(tif
,FIELD_SMINSAMPLEVALUE
))
633 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMINSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_sminsamplevalue
))
636 if (TIFFFieldSet(tif
,FIELD_SMAXSAMPLEVALUE
))
638 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMAXSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_smaxsamplevalue
))
641 if (TIFFFieldSet(tif
,FIELD_IMAGEDEPTH
))
643 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEDEPTH
,tif
->tif_dir
.td_imagedepth
))
646 if (TIFFFieldSet(tif
,FIELD_TILEDEPTH
))
648 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_TILEDEPTH
,tif
->tif_dir
.td_tiledepth
))
651 if (TIFFFieldSet(tif
,FIELD_HALFTONEHINTS
))
653 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_HALFTONEHINTS
,2,&tif
->tif_dir
.td_halftonehints
[0]))
656 if (TIFFFieldSet(tif
,FIELD_YCBCRSUBSAMPLING
))
658 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_YCBCRSUBSAMPLING
,2,&tif
->tif_dir
.td_ycbcrsubsampling
[0]))
661 if (TIFFFieldSet(tif
,FIELD_YCBCRPOSITIONING
))
663 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_YCBCRPOSITIONING
,tif
->tif_dir
.td_ycbcrpositioning
))
666 if (TIFFFieldSet(tif
,FIELD_REFBLACKWHITE
))
668 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,TIFFTAG_REFERENCEBLACKWHITE
,6,tif
->tif_dir
.td_refblackwhite
))
671 if (TIFFFieldSet(tif
,FIELD_TRANSFERFUNCTION
))
673 if (!TIFFWriteDirectoryTagTransferfunction(tif
,&ndir
,dir
))
676 if (TIFFFieldSet(tif
,FIELD_INKNAMES
))
678 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,TIFFTAG_INKNAMES
,tif
->tif_dir
.td_inknameslen
,tif
->tif_dir
.td_inknames
))
681 if (TIFFFieldSet(tif
,FIELD_SUBIFD
))
683 if (!TIFFWriteDirectoryTagSubifd(tif
,&ndir
,dir
))
688 for (n
=0; n
<tif
->tif_nfields
; n
++) {
690 o
= tif
->tif_fields
[n
];
691 if ((o
->field_bit
>=FIELD_CODEC
)&&(TIFFFieldSet(tif
,o
->field_bit
)))
693 switch (o
->get_field_type
)
695 case TIFF_SETGET_ASCII
:
699 assert(o
->field_type
==TIFF_ASCII
);
700 assert(o
->field_readcount
==TIFF_VARIABLE
);
701 assert(o
->field_passcount
==0);
702 TIFFGetField(tif
,o
->field_tag
,&pb
);
703 pa
=(uint32
)(strlen(pb
));
704 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,pa
,pb
))
708 case TIFF_SETGET_UINT16
:
711 assert(o
->field_type
==TIFF_SHORT
);
712 assert(o
->field_readcount
==1);
713 assert(o
->field_passcount
==0);
714 TIFFGetField(tif
,o
->field_tag
,&p
);
715 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,p
))
719 case TIFF_SETGET_UINT32
:
722 assert(o
->field_type
==TIFF_LONG
);
723 assert(o
->field_readcount
==1);
724 assert(o
->field_passcount
==0);
725 TIFFGetField(tif
,o
->field_tag
,&p
);
726 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,p
))
730 case TIFF_SETGET_C32_UINT8
:
734 assert(o
->field_type
==TIFF_UNDEFINED
);
735 assert(o
->field_readcount
==TIFF_VARIABLE2
);
736 assert(o
->field_passcount
==1);
737 TIFFGetField(tif
,o
->field_tag
,&pa
,&pb
);
738 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,(uint16
)o
->field_tag
,pa
,pb
))
743 TIFFErrorExt(tif
->tif_clientdata
,module
,
744 "Cannot write tag %d (%s)",
746 o
->field_name
? o
->field_name
: "unknown");
753 for (m
=0; m
<(uint32
)(tif
->tif_dir
.td_customValueCount
); m
++)
755 uint16 tag
= (uint16
)tif
->tif_dir
.td_customValues
[m
].info
->field_tag
;
756 uint32 count
= tif
->tif_dir
.td_customValues
[m
].count
;
757 switch (tif
->tif_dir
.td_customValues
[m
].info
->field_type
)
760 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
764 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
768 if (!TIFFWriteDirectoryTagByteArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
772 if (!TIFFWriteDirectoryTagSbyteArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
776 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
780 if (!TIFFWriteDirectoryTagSshortArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
784 if (!TIFFWriteDirectoryTagLongArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
788 if (!TIFFWriteDirectoryTagSlongArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
792 if (!TIFFWriteDirectoryTagLong8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
796 if (!TIFFWriteDirectoryTagSlong8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
800 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
804 if (!TIFFWriteDirectoryTagSrationalArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
808 if (!TIFFWriteDirectoryTagFloatArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
812 if (!TIFFWriteDirectoryTagDoubleArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
816 if (!TIFFWriteDirectoryTagIfdArray(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
820 if (!TIFFWriteDirectoryTagIfdIfd8Array(tif
,&ndir
,dir
,tag
,count
,tif
->tif_dir
.td_customValues
[m
].value
))
824 assert(0); /* we should never get here */
830 dir
=_TIFFmalloc(ndir
*sizeof(TIFFDirEntry
));
833 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
838 if ((tif
->tif_diroff
==0)&&(!TIFFLinkDirectory(tif
)))
842 tif
->tif_diroff
=(TIFFSeekFile(tif
,0,SEEK_END
)+1)&(~((toff_t
)1));
844 *pdiroff
=tif
->tif_diroff
;
845 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
849 tif
->tif_dataoff
=tif
->tif_diroff
+dirsize
;
850 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
851 tif
->tif_dataoff
=(uint32
)tif
->tif_dataoff
;
852 if ((tif
->tif_dataoff
<tif
->tif_diroff
)||(tif
->tif_dataoff
<(uint64
)dirsize
))
854 TIFFErrorExt(tif
->tif_clientdata
,module
,"Maximum TIFF file size exceeded");
857 if (tif
->tif_dataoff
&1)
864 if (TIFFFieldSet(tif
,FIELD_SUBIFD
)&&(tif
->tif_subifdoff
==0))
868 for (na
=0, nb
=dir
; ; na
++, nb
++)
872 TIFFErrorExt(tif
->tif_clientdata
,module
,
873 "Cannot find SubIFD tag");
876 if (nb
->tdir_tag
==TIFFTAG_SUBIFD
)
879 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
880 tif
->tif_subifdoff
=tif
->tif_diroff
+2+na
*12+8;
882 tif
->tif_subifdoff
=tif
->tif_diroff
+8+na
*20+12;
885 dirmem
=_TIFFmalloc(dirsize
);
888 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
891 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
897 *(uint16
*)n
=(uint16
)ndir
;
898 if (tif
->tif_flags
&TIFF_SWAB
)
899 TIFFSwabShort((uint16
*)n
);
902 for (m
=0; m
<ndir
; m
++)
904 *(uint16
*)n
=o
->tdir_tag
;
905 if (tif
->tif_flags
&TIFF_SWAB
)
906 TIFFSwabShort((uint16
*)n
);
908 *(uint16
*)n
=o
->tdir_type
;
909 if (tif
->tif_flags
&TIFF_SWAB
)
910 TIFFSwabShort((uint16
*)n
);
912 nTmp
= (uint32
)o
->tdir_count
;
913 _TIFFmemcpy(n
,&nTmp
,4);
914 if (tif
->tif_flags
&TIFF_SWAB
)
915 TIFFSwabLong((uint32
*)n
);
917 /* This is correct. The data has been */
918 /* swabbed previously in TIFFWriteDirectoryTagData */
919 _TIFFmemcpy(n
,&o
->tdir_offset
,4);
923 nTmp
= (uint32
)tif
->tif_nextdiroff
;
924 if (tif
->tif_flags
&TIFF_SWAB
)
926 _TIFFmemcpy(n
,&nTmp
,4);
934 if (tif
->tif_flags
&TIFF_SWAB
)
935 TIFFSwabLong8((uint64
*)n
);
938 for (m
=0; m
<ndir
; m
++)
940 *(uint16
*)n
=o
->tdir_tag
;
941 if (tif
->tif_flags
&TIFF_SWAB
)
942 TIFFSwabShort((uint16
*)n
);
944 *(uint16
*)n
=o
->tdir_type
;
945 if (tif
->tif_flags
&TIFF_SWAB
)
946 TIFFSwabShort((uint16
*)n
);
948 _TIFFmemcpy(n
,&o
->tdir_count
,8);
949 if (tif
->tif_flags
&TIFF_SWAB
)
950 TIFFSwabLong8((uint64
*)n
);
952 _TIFFmemcpy(n
,&o
->tdir_offset
,8);
956 _TIFFmemcpy(n
,&tif
->tif_nextdiroff
,8);
957 if (tif
->tif_flags
&TIFF_SWAB
)
958 TIFFSwabLong8((uint64
*)n
);
962 if (!SeekOK(tif
,tif
->tif_diroff
))
964 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing directory");
967 if (!WriteOK(tif
,dirmem
,(tmsize_t
)dirsize
))
969 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing directory");
975 TIFFFreeDirectory(tif
);
976 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
977 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
978 (*tif
->tif_cleanup
)(tif
);
980 * Reset directory-related state for subsequent
983 TIFFCreateDirectory(tif
);
994 static int8
TIFFClampDoubleToInt8( double val
)
998 if( val
< -128 || val
!= val
)
1003 static int16
TIFFClampDoubleToInt16( double val
)
1007 if( val
< -32768 || val
!= val
)
1012 static int32
TIFFClampDoubleToInt32( double val
)
1014 if( val
> 0x7FFFFFFF )
1016 if( val
< -0x7FFFFFFF-1 || val
!= val
)
1017 return -0x7FFFFFFF-1;
1021 static uint8
TIFFClampDoubleToUInt8( double val
)
1025 if( val
> 255 || val
!= val
)
1030 static uint16
TIFFClampDoubleToUInt16( double val
)
1034 if( val
> 65535 || val
!= val
)
1039 static uint32
TIFFClampDoubleToUInt32( double val
)
1043 if( val
> 0xFFFFFFFFU
|| val
!= val
)
1049 TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1051 static const char module
[] = "TIFFWriteDirectoryTagSampleformatArray";
1055 conv
= _TIFFmalloc(count
*sizeof(double));
1058 TIFFErrorExt(tif
->tif_clientdata
, module
, "Out of memory");
1062 switch (tif
->tif_dir
.td_sampleformat
)
1064 case SAMPLEFORMAT_IEEEFP
:
1065 if (tif
->tif_dir
.td_bitspersample
<=32)
1067 for (i
= 0; i
< count
; ++i
)
1068 ((float*)conv
)[i
] = _TIFFClampDoubleToFloat(value
[i
]);
1069 ok
= TIFFWriteDirectoryTagFloatArray(tif
,ndir
,dir
,tag
,count
,(float*)conv
);
1073 ok
= TIFFWriteDirectoryTagDoubleArray(tif
,ndir
,dir
,tag
,count
,value
);
1076 case SAMPLEFORMAT_INT
:
1077 if (tif
->tif_dir
.td_bitspersample
<=8)
1079 for (i
= 0; i
< count
; ++i
)
1080 ((int8
*)conv
)[i
] = TIFFClampDoubleToInt8(value
[i
]);
1081 ok
= TIFFWriteDirectoryTagSbyteArray(tif
,ndir
,dir
,tag
,count
,(int8
*)conv
);
1083 else if (tif
->tif_dir
.td_bitspersample
<=16)
1085 for (i
= 0; i
< count
; ++i
)
1086 ((int16
*)conv
)[i
] = TIFFClampDoubleToInt16(value
[i
]);
1087 ok
= TIFFWriteDirectoryTagSshortArray(tif
,ndir
,dir
,tag
,count
,(int16
*)conv
);
1091 for (i
= 0; i
< count
; ++i
)
1092 ((int32
*)conv
)[i
] = TIFFClampDoubleToInt32(value
[i
]);
1093 ok
= TIFFWriteDirectoryTagSlongArray(tif
,ndir
,dir
,tag
,count
,(int32
*)conv
);
1096 case SAMPLEFORMAT_UINT
:
1097 if (tif
->tif_dir
.td_bitspersample
<=8)
1099 for (i
= 0; i
< count
; ++i
)
1100 ((uint8
*)conv
)[i
] = TIFFClampDoubleToUInt8(value
[i
]);
1101 ok
= TIFFWriteDirectoryTagByteArray(tif
,ndir
,dir
,tag
,count
,(uint8
*)conv
);
1103 else if (tif
->tif_dir
.td_bitspersample
<=16)
1105 for (i
= 0; i
< count
; ++i
)
1106 ((uint16
*)conv
)[i
] = TIFFClampDoubleToUInt16(value
[i
]);
1107 ok
= TIFFWriteDirectoryTagShortArray(tif
,ndir
,dir
,tag
,count
,(uint16
*)conv
);
1111 for (i
= 0; i
< count
; ++i
)
1112 ((uint32
*)conv
)[i
] = TIFFClampDoubleToUInt32(value
[i
]);
1113 ok
= TIFFWriteDirectoryTagLongArray(tif
,ndir
,dir
,tag
,count
,(uint32
*)conv
);
1126 TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1128 switch (tif
->tif_dir
.td_sampleformat
)
1130 case SAMPLEFORMAT_IEEEFP
:
1131 if (tif
->tif_dir
.td_bitspersample
<=32)
1132 return(TIFFWriteDirectoryTagFloatPerSample(tif
,ndir
,dir
,tag
,(float)value
));
1134 return(TIFFWriteDirectoryTagDoublePerSample(tif
,ndir
,dir
,tag
,value
));
1135 case SAMPLEFORMAT_INT
:
1136 if (tif
->tif_dir
.td_bitspersample
<=8)
1137 return(TIFFWriteDirectoryTagSbytePerSample(tif
,ndir
,dir
,tag
,(int8
)value
));
1138 else if (tif
->tif_dir
.td_bitspersample
<=16)
1139 return(TIFFWriteDirectoryTagSshortPerSample(tif
,ndir
,dir
,tag
,(int16
)value
));
1141 return(TIFFWriteDirectoryTagSlongPerSample(tif
,ndir
,dir
,tag
,(int32
)value
));
1142 case SAMPLEFORMAT_UINT
:
1143 if (tif
->tif_dir
.td_bitspersample
<=8)
1144 return(TIFFWriteDirectoryTagBytePerSample(tif
,ndir
,dir
,tag
,(uint8
)value
));
1145 else if (tif
->tif_dir
.td_bitspersample
<=16)
1146 return(TIFFWriteDirectoryTagShortPerSample(tif
,ndir
,dir
,tag
,(uint16
)value
));
1148 return(TIFFWriteDirectoryTagLongPerSample(tif
,ndir
,dir
,tag
,(uint32
)value
));
1156 TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1163 return(TIFFWriteDirectoryTagCheckedAscii(tif
,ndir
,dir
,tag
,count
,value
));
1167 TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1174 return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif
,ndir
,dir
,tag
,count
,value
));
1179 TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1186 return(TIFFWriteDirectoryTagCheckedByte(tif
,ndir
,dir
,tag
,value
));
1191 TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1198 return(TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,count
,value
));
1203 TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1205 static const char module
[] = "TIFFWriteDirectoryTagBytePerSample";
1215 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint8
));
1218 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1221 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1223 o
=TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1231 TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1238 return(TIFFWriteDirectoryTagCheckedSbyte(tif
,ndir
,dir
,tag
,value
));
1243 TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1250 return(TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,count
,value
));
1255 TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1257 static const char module
[] = "TIFFWriteDirectoryTagSbytePerSample";
1267 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int8
));
1270 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1273 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1275 o
=TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1282 TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1289 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,value
));
1293 TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1300 return(TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,value
));
1304 TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1306 static const char module
[] = "TIFFWriteDirectoryTagShortPerSample";
1316 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint16
));
1319 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1322 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1324 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1331 TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1338 return(TIFFWriteDirectoryTagCheckedSshort(tif
,ndir
,dir
,tag
,value
));
1343 TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1350 return(TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,count
,value
));
1355 TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1357 static const char module
[] = "TIFFWriteDirectoryTagSshortPerSample";
1367 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int16
));
1370 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1373 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1375 o
=TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1382 TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1389 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1393 TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1400 return(TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,value
));
1405 TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1407 static const char module
[] = "TIFFWriteDirectoryTagLongPerSample";
1417 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint32
));
1420 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1423 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1425 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1433 TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1440 return(TIFFWriteDirectoryTagCheckedSlong(tif
,ndir
,dir
,tag
,value
));
1445 TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
1452 return(TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,count
,value
));
1457 TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1459 static const char module
[] = "TIFFWriteDirectoryTagSlongPerSample";
1469 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int32
));
1472 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1475 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1477 o
=TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1485 TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
1492 return(TIFFWriteDirectoryTagCheckedLong8(tif
,ndir
,dir
,tag
,value
));
1497 TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1504 return(TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1509 TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
1516 return(TIFFWriteDirectoryTagCheckedSlong8(tif
,ndir
,dir
,tag
,value
));
1521 TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
1528 return(TIFFWriteDirectoryTagCheckedSlong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1532 TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1539 return(TIFFWriteDirectoryTagCheckedRational(tif
,ndir
,dir
,tag
,value
));
1543 TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1550 return(TIFFWriteDirectoryTagCheckedRationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1554 TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1561 return(TIFFWriteDirectoryTagCheckedSrationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1565 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1572 return(TIFFWriteDirectoryTagCheckedFloat(tif
,ndir
,dir
,tag
,value
));
1576 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1583 return(TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,count
,value
));
1587 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1589 static const char module
[] = "TIFFWriteDirectoryTagFloatPerSample";
1599 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(float));
1602 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1605 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1607 o
=TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1614 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1621 return(TIFFWriteDirectoryTagCheckedDouble(tif
,ndir
,dir
,tag
,value
));
1625 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1632 return(TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,count
,value
));
1636 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1638 static const char module
[] = "TIFFWriteDirectoryTagDoublePerSample";
1648 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(double));
1651 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1654 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1656 o
=TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1663 TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1670 return(TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,value
));
1675 TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1682 return(TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,tag
,count
,value
));
1687 TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1695 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,(uint16
)value
));
1697 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1700 static int _WriteAsType(TIFF
* tif
, uint64 strile_size
, uint64 uncompressed_threshold
)
1702 const uint16 compression
= tif
->tif_dir
.td_compression
;
1703 if ( compression
== COMPRESSION_NONE
)
1705 return strile_size
> uncompressed_threshold
;
1707 else if ( compression
== COMPRESSION_JPEG
||
1708 compression
== COMPRESSION_LZW
||
1709 compression
== COMPRESSION_ADOBE_DEFLATE
||
1710 compression
== COMPRESSION_LZMA
||
1711 compression
== COMPRESSION_LERC
||
1712 compression
== COMPRESSION_ZSTD
||
1713 compression
== COMPRESSION_WEBP
)
1715 /* For a few select compression types, we assume that in the worst */
1716 /* case the compressed size will be 10 times the uncompressed size */
1717 /* This is overly pessismistic ! */
1718 return strile_size
>= uncompressed_threshold
/ 10;
1723 static int WriteAsLong8(TIFF
* tif
, uint64 strile_size
)
1725 return _WriteAsType(tif
, strile_size
, 0xFFFFFFFFU
);
1728 static int WriteAsLong4(TIFF
* tif
, uint64 strile_size
)
1730 return _WriteAsType(tif
, strile_size
, 0xFFFFU
);
1733 /************************************************************************/
1734 /* TIFFWriteDirectoryTagLongLong8Array() */
1736 /* Write out LONG8 array and write a SHORT/LONG/LONG8 depending */
1737 /* on strile size and Classic/BigTIFF mode. */
1738 /************************************************************************/
1741 TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1743 static const char module
[] = "TIFFWriteDirectoryTagLongLong8Array";
1747 /* is this just a counting pass? */
1754 if( tif
->tif_dir
.td_deferstrilearraywriting
)
1756 return TIFFWriteDirectoryTagData(tif
, ndir
, dir
, tag
, TIFF_NOTYPE
, 0, 0, NULL
);
1759 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1761 int write_aslong8
= 1;
1762 /* In the case of ByteCounts array, we may be able to write them on */
1763 /* LONG if the strip/tilesize is not too big. */
1764 /* Also do that for count > 1 in the case someone would want to create */
1765 /* a single-strip file with a growing height, in which case using */
1766 /* LONG8 will be safer. */
1767 if( count
> 1 && tag
== TIFFTAG_STRIPBYTECOUNTS
)
1769 write_aslong8
= WriteAsLong8(tif
, TIFFStripSize64(tif
));
1771 else if( count
> 1 && tag
== TIFFTAG_TILEBYTECOUNTS
)
1773 write_aslong8
= WriteAsLong8(tif
, TIFFTileSize64(tif
));
1777 return TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,
1783 if( count
> 1 && tag
== TIFFTAG_STRIPBYTECOUNTS
)
1785 write_aslong4
= WriteAsLong4(tif
, TIFFStripSize64(tif
));
1787 else if( count
> 1 && tag
== TIFFTAG_TILEBYTECOUNTS
)
1789 write_aslong4
= WriteAsLong4(tif
, TIFFTileSize64(tif
));
1794 ** For classic tiff we want to verify everything is in range for LONG
1795 ** and convert to long format.
1798 uint32
* p
= _TIFFmalloc(count
*sizeof(uint32
));
1805 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1809 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1813 TIFFErrorExt(tif
->tif_clientdata
,module
,
1814 "Attempt to write value larger than 0xFFFFFFFF in LONG array.");
1821 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1826 uint16
* p
= _TIFFmalloc(count
*sizeof(uint16
));
1833 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1837 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1841 /* Should not happen normally given the check we did before */
1842 TIFFErrorExt(tif
->tif_clientdata
,module
,
1843 "Attempt to write value larger than 0xFFFF in SHORT array.");
1850 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,p
);
1857 /************************************************************************/
1858 /* TIFFWriteDirectoryTagIfdIfd8Array() */
1860 /* Write either IFD8 or IFD array depending on file type. */
1861 /************************************************************************/
1864 TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1866 static const char module
[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1873 /* is this just a counting pass? */
1880 /* We always write IFD8 for BigTIFF, no checking needed. */
1881 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1882 return TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,
1886 ** For classic tiff we want to verify everything is in range for IFD
1887 ** and convert to long format.
1890 p
= _TIFFmalloc(count
*sizeof(uint32
));
1893 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1897 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1901 TIFFErrorExt(tif
->tif_clientdata
,module
,
1902 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1909 o
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,p
);
1917 TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1919 static const char module
[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1930 for (ma
=value
, mb
=0; mb
<count
; ma
++, mb
++)
1932 if ((n
==0)&&(*ma
>0xFFFF))
1934 if ((n
==1)&&(*ma
>0xFFFFFFFF))
1944 p
=_TIFFmalloc(count
*sizeof(uint16
));
1947 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1950 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1952 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,p
);
1959 p
=_TIFFmalloc(count
*sizeof(uint32
));
1962 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1965 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1967 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1973 o
=TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
);
1979 TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1981 static const char module
[] = "TIFFWriteDirectoryTagColormap";
1990 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1991 n
=_TIFFmalloc(3*m
*sizeof(uint16
));
1994 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
1997 _TIFFmemcpy(&n
[0],tif
->tif_dir
.td_colormap
[0],m
*sizeof(uint16
));
1998 _TIFFmemcpy(&n
[m
],tif
->tif_dir
.td_colormap
[1],m
*sizeof(uint16
));
1999 _TIFFmemcpy(&n
[2*m
],tif
->tif_dir
.td_colormap
[2],m
*sizeof(uint16
));
2000 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_COLORMAP
,3*m
,n
);
2006 TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
2008 static const char module
[] = "TIFFWriteDirectoryTagTransferfunction";
2018 m
=(1<<tif
->tif_dir
.td_bitspersample
);
2019 n
=tif
->tif_dir
.td_samplesperpixel
-tif
->tif_dir
.td_extrasamples
;
2021 * Check if the table can be written as a single column,
2022 * or if it must be written as 3 columns. Note that we
2023 * write a 3-column tag if there are 2 samples/pixel and
2024 * a single column of data won't suffice--hmm.
2030 if (tif
->tif_dir
.td_transferfunction
[2] == NULL
||
2031 !_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
)))
2036 if (tif
->tif_dir
.td_transferfunction
[1] == NULL
||
2037 !_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
)))
2042 o
=_TIFFmalloc(n
*m
*sizeof(uint16
));
2045 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2048 _TIFFmemcpy(&o
[0],tif
->tif_dir
.td_transferfunction
[0],m
*sizeof(uint16
));
2050 _TIFFmemcpy(&o
[m
],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
));
2052 _TIFFmemcpy(&o
[2*m
],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
));
2053 p
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_TRANSFERFUNCTION
,n
*m
,o
);
2059 TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
2061 static const char module
[] = "TIFFWriteDirectoryTagSubifd";
2064 if (tif
->tif_dir
.td_nsubifd
==0)
2072 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2078 o
=_TIFFmalloc(tif
->tif_dir
.td_nsubifd
*sizeof(uint32
));
2081 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2084 pa
=tif
->tif_dir
.td_subifd
;
2086 for (p
=0; p
< tif
->tif_dir
.td_nsubifd
; p
++)
2090 /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which is illegal) */
2091 if( *pa
> 0xFFFFFFFFUL
)
2093 TIFFErrorExt(tif
->tif_clientdata
,module
,"Illegal value for SubIFD tag");
2097 *pb
++=(uint32
)(*pa
++);
2099 n
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,o
);
2103 n
=TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,tif
->tif_dir
.td_subifd
);
2107 * Total hack: if this directory includes a SubIFD
2108 * tag then force the next <n> directories to be
2109 * written as ``sub directories'' of this one. This
2110 * is used to write things like thumbnails and
2111 * image masks that one wants to keep out of the
2112 * normal directory linkage access mechanism.
2114 tif
->tif_flags
|=TIFF_INSUBIFD
;
2115 tif
->tif_nsubifd
=tif
->tif_dir
.td_nsubifd
;
2116 if (tif
->tif_dir
.td_nsubifd
==1)
2117 tif
->tif_subifdoff
=0;
2119 tif
->tif_subifdoff
=m
;
2124 TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
2126 assert(sizeof(char)==1);
2127 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_ASCII
,count
,count
,value
));
2131 TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
2133 assert(sizeof(uint8
)==1);
2134 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_UNDEFINED
,count
,count
,value
));
2139 TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
2141 assert(sizeof(uint8
)==1);
2142 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,1,1,&value
));
2147 TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
2149 assert(sizeof(uint8
)==1);
2150 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,count
,count
,value
));
2155 TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
2157 assert(sizeof(int8
)==1);
2158 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,1,1,&value
));
2163 TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
2165 assert(sizeof(int8
)==1);
2166 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,count
,count
,value
));
2170 TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
2173 assert(sizeof(uint16
)==2);
2175 if (tif
->tif_flags
&TIFF_SWAB
)
2177 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,1,2,&m
));
2181 TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
2183 assert(count
<0x80000000);
2184 assert(sizeof(uint16
)==2);
2185 if (tif
->tif_flags
&TIFF_SWAB
)
2186 TIFFSwabArrayOfShort(value
,count
);
2187 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,count
,count
*2,value
));
2192 TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
2195 assert(sizeof(int16
)==2);
2197 if (tif
->tif_flags
&TIFF_SWAB
)
2198 TIFFSwabShort((uint16
*)(&m
));
2199 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,1,2,&m
));
2204 TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
2206 assert(count
<0x80000000);
2207 assert(sizeof(int16
)==2);
2208 if (tif
->tif_flags
&TIFF_SWAB
)
2209 TIFFSwabArrayOfShort((uint16
*)value
,count
);
2210 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,count
,count
*2,value
));
2214 TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
2217 assert(sizeof(uint32
)==4);
2219 if (tif
->tif_flags
&TIFF_SWAB
)
2221 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,1,4,&m
));
2225 TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2227 assert(count
<0x40000000);
2228 assert(sizeof(uint32
)==4);
2229 if (tif
->tif_flags
&TIFF_SWAB
)
2230 TIFFSwabArrayOfLong(value
,count
);
2231 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,count
,count
*4,value
));
2236 TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
2239 assert(sizeof(int32
)==4);
2241 if (tif
->tif_flags
&TIFF_SWAB
)
2242 TIFFSwabLong((uint32
*)(&m
));
2243 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,1,4,&m
));
2248 TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
2250 assert(count
<0x40000000);
2251 assert(sizeof(int32
)==4);
2252 if (tif
->tif_flags
&TIFF_SWAB
)
2253 TIFFSwabArrayOfLong((uint32
*)value
,count
);
2254 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,count
,count
*4,value
));
2259 TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
2262 assert(sizeof(uint64
)==8);
2263 if( !(tif
->tif_flags
&TIFF_BIGTIFF
) ) {
2264 TIFFErrorExt(tif
->tif_clientdata
,"TIFFWriteDirectoryTagCheckedLong8","LONG8 not allowed for ClassicTIFF");
2268 if (tif
->tif_flags
&TIFF_SWAB
)
2270 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,1,8,&m
));
2275 TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2277 assert(count
<0x20000000);
2278 assert(sizeof(uint64
)==8);
2279 if( !(tif
->tif_flags
&TIFF_BIGTIFF
) ) {
2280 TIFFErrorExt(tif
->tif_clientdata
,"TIFFWriteDirectoryTagCheckedLong8Array","LONG8 not allowed for ClassicTIFF");
2283 if (tif
->tif_flags
&TIFF_SWAB
)
2284 TIFFSwabArrayOfLong8(value
,count
);
2285 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,count
,count
*8,value
));
2290 TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
2293 assert(sizeof(int64
)==8);
2294 if( !(tif
->tif_flags
&TIFF_BIGTIFF
) ) {
2295 TIFFErrorExt(tif
->tif_clientdata
,"TIFFWriteDirectoryTagCheckedSlong8","SLONG8 not allowed for ClassicTIFF");
2299 if (tif
->tif_flags
&TIFF_SWAB
)
2300 TIFFSwabLong8((uint64
*)(&m
));
2301 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,1,8,&m
));
2306 TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
2308 assert(count
<0x20000000);
2309 assert(sizeof(int64
)==8);
2310 if( !(tif
->tif_flags
&TIFF_BIGTIFF
) ) {
2311 TIFFErrorExt(tif
->tif_clientdata
,"TIFFWriteDirectoryTagCheckedSlong8Array","SLONG8 not allowed for ClassicTIFF");
2314 if (tif
->tif_flags
&TIFF_SWAB
)
2315 TIFFSwabArrayOfLong8((uint64
*)value
,count
);
2316 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,count
,count
*8,value
));
2320 TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2322 static const char module
[] = "TIFFWriteDirectoryTagCheckedRational";
2324 assert(sizeof(uint32
)==4);
2327 TIFFErrorExt(tif
->tif_clientdata
,module
,"Negative value is illegal");
2330 else if( value
!= value
)
2332 TIFFErrorExt(tif
->tif_clientdata
,module
,"Not-a-number value is illegal");
2335 else if (value
==0.0)
2340 else if (value
<= 0xFFFFFFFFU
&& value
==(double)(uint32
)value
)
2347 m
[0]=(uint32
)(value
*0xFFFFFFFF);
2353 m
[1]=(uint32
)(0xFFFFFFFF/value
);
2355 if (tif
->tif_flags
&TIFF_SWAB
)
2357 TIFFSwabLong(&m
[0]);
2358 TIFFSwabLong(&m
[1]);
2360 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,1,8,&m
[0]));
2364 TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2366 static const char module
[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2372 assert(sizeof(uint32
)==4);
2373 m
=_TIFFmalloc(count
*2*sizeof(uint32
));
2376 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2379 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2381 if (*na
<=0.0 || *na
!= *na
)
2386 else if (*na
>= 0 && *na
<= (float)0xFFFFFFFFU
&&
2387 *na
==(float)(uint32
)(*na
))
2389 nb
[0]=(uint32
)(*na
);
2394 nb
[0]=(uint32
)((double)(*na
)*0xFFFFFFFF);
2400 nb
[1]=(uint32
)((double)0xFFFFFFFF/(*na
));
2403 if (tif
->tif_flags
&TIFF_SWAB
)
2404 TIFFSwabArrayOfLong(m
,count
*2);
2405 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,count
,count
*8,&m
[0]);
2411 TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2413 static const char module
[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2419 assert(sizeof(int32
)==4);
2420 m
=_TIFFmalloc(count
*2*sizeof(int32
));
2423 TIFFErrorExt(tif
->tif_clientdata
,module
,"Out of memory");
2426 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2430 if (*na
==(int32
)(*na
))
2437 nb
[0]=-(int32
)((double)(-*na
)*0x7FFFFFFF);
2443 nb
[1]=(int32
)((double)0x7FFFFFFF/(-*na
));
2448 if (*na
==(int32
)(*na
))
2455 nb
[0]=(int32
)((double)(*na
)*0x7FFFFFFF);
2461 nb
[1]=(int32
)((double)0x7FFFFFFF/(*na
));
2465 if (tif
->tif_flags
&TIFF_SWAB
)
2466 TIFFSwabArrayOfLong((uint32
*)m
,count
*2);
2467 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SRATIONAL
,count
,count
*8,&m
[0]);
2474 TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
2477 assert(sizeof(float)==4);
2479 TIFFCvtNativeToIEEEFloat(tif
,1,&m
);
2480 if (tif
->tif_flags
&TIFF_SWAB
)
2482 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,1,4,&m
));
2487 TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2489 assert(count
<0x40000000);
2490 assert(sizeof(float)==4);
2491 TIFFCvtNativeToIEEEFloat(tif
,count
,&value
);
2492 if (tif
->tif_flags
&TIFF_SWAB
)
2493 TIFFSwabArrayOfFloat(value
,count
);
2494 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,count
,count
*4,value
));
2499 TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2502 assert(sizeof(double)==8);
2504 TIFFCvtNativeToIEEEDouble(tif
,1,&m
);
2505 if (tif
->tif_flags
&TIFF_SWAB
)
2507 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,1,8,&m
));
2512 TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
2514 assert(count
<0x20000000);
2515 assert(sizeof(double)==8);
2516 TIFFCvtNativeToIEEEDouble(tif
,count
,&value
);
2517 if (tif
->tif_flags
&TIFF_SWAB
)
2518 TIFFSwabArrayOfDouble(value
,count
);
2519 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,count
,count
*8,value
));
2523 TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2525 assert(count
<0x40000000);
2526 assert(sizeof(uint32
)==4);
2527 if (tif
->tif_flags
&TIFF_SWAB
)
2528 TIFFSwabArrayOfLong(value
,count
);
2529 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD
,count
,count
*4,value
));
2533 TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2535 assert(count
<0x20000000);
2536 assert(sizeof(uint64
)==8);
2537 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2538 if (tif
->tif_flags
&TIFF_SWAB
)
2539 TIFFSwabArrayOfLong8(value
,count
);
2540 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD8
,count
,count
*8,value
));
2544 TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
)
2546 static const char module
[] = "TIFFWriteDirectoryTagData";
2551 assert(dir
[m
].tdir_tag
!=tag
);
2552 if (dir
[m
].tdir_tag
>tag
)
2559 for (n
=*ndir
; n
>m
; n
--)
2562 dir
[m
].tdir_tag
=tag
;
2563 dir
[m
].tdir_type
=datatype
;
2564 dir
[m
].tdir_count
=count
;
2565 dir
[m
].tdir_offset
.toff_long8
= 0;
2566 if (datalength
<=((tif
->tif_flags
&TIFF_BIGTIFF
)?0x8U
:0x4U
))
2568 if( data
&& datalength
)
2570 _TIFFmemcpy(&dir
[m
].tdir_offset
,data
,datalength
);
2576 na
=tif
->tif_dataoff
;
2578 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2580 if ((nb
<na
)||(nb
<datalength
))
2582 TIFFErrorExt(tif
->tif_clientdata
,module
,"Maximum TIFF file size exceeded");
2585 if (!SeekOK(tif
,na
))
2587 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing tag data");
2590 assert(datalength
<0x80000000UL
);
2591 if (!WriteOK(tif
,data
,(tmsize_t
)datalength
))
2593 TIFFErrorExt(tif
->tif_clientdata
,module
,"IO error writing tag data");
2596 tif
->tif_dataoff
=nb
;
2597 if (tif
->tif_dataoff
&1)
2599 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2603 if (tif
->tif_flags
&TIFF_SWAB
)
2605 _TIFFmemcpy(&dir
[m
].tdir_offset
,&o
,4);
2609 dir
[m
].tdir_offset
.toff_long8
= na
;
2610 if (tif
->tif_flags
&TIFF_SWAB
)
2611 TIFFSwabLong8(&dir
[m
].tdir_offset
.toff_long8
);
2619 * Link the current directory into the directory chain for the file.
2622 TIFFLinkDirectory(TIFF
* tif
)
2624 static const char module
[] = "TIFFLinkDirectory";
2626 tif
->tif_diroff
= (TIFFSeekFile(tif
,0,SEEK_END
)+1) & (~((toff_t
)1));
2631 if (tif
->tif_flags
& TIFF_INSUBIFD
)
2633 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2636 m
= (uint32
)tif
->tif_diroff
;
2637 if (tif
->tif_flags
& TIFF_SWAB
)
2639 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2640 if (!WriteOK(tif
, &m
, 4)) {
2641 TIFFErrorExt(tif
->tif_clientdata
, module
,
2642 "Error writing SubIFD directory link");
2646 * Advance to the next SubIFD or, if this is
2647 * the last one configured, revert back to the
2648 * normal directory linkage.
2650 if (--tif
->tif_nsubifd
)
2651 tif
->tif_subifdoff
+= 4;
2653 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2659 m
= tif
->tif_diroff
;
2660 if (tif
->tif_flags
& TIFF_SWAB
)
2662 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2663 if (!WriteOK(tif
, &m
, 8)) {
2664 TIFFErrorExt(tif
->tif_clientdata
, module
,
2665 "Error writing SubIFD directory link");
2669 * Advance to the next SubIFD or, if this is
2670 * the last one configured, revert back to the
2671 * normal directory linkage.
2673 if (--tif
->tif_nsubifd
)
2674 tif
->tif_subifdoff
+= 8;
2676 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2681 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2685 m
= (uint32
)(tif
->tif_diroff
);
2686 if (tif
->tif_flags
& TIFF_SWAB
)
2688 if (tif
->tif_header
.classic
.tiff_diroff
== 0) {
2690 * First directory, overwrite offset in header.
2692 tif
->tif_header
.classic
.tiff_diroff
= (uint32
) tif
->tif_diroff
;
2693 (void) TIFFSeekFile(tif
,4, SEEK_SET
);
2694 if (!WriteOK(tif
, &m
, 4)) {
2695 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2696 "Error writing TIFF header");
2702 * Not the first directory, search to the last and append.
2704 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
2709 if (!SeekOK(tif
, nextdir
) ||
2710 !ReadOK(tif
, &dircount
, 2)) {
2711 TIFFErrorExt(tif
->tif_clientdata
, module
,
2712 "Error fetching directory count");
2715 if (tif
->tif_flags
& TIFF_SWAB
)
2716 TIFFSwabShort(&dircount
);
2717 (void) TIFFSeekFile(tif
,
2718 nextdir
+2+dircount
*12, SEEK_SET
);
2719 if (!ReadOK(tif
, &nextnextdir
, 4)) {
2720 TIFFErrorExt(tif
->tif_clientdata
, module
,
2721 "Error fetching directory link");
2724 if (tif
->tif_flags
& TIFF_SWAB
)
2725 TIFFSwabLong(&nextnextdir
);
2728 (void) TIFFSeekFile(tif
,
2729 nextdir
+2+dircount
*12, SEEK_SET
);
2730 if (!WriteOK(tif
, &m
, 4)) {
2731 TIFFErrorExt(tif
->tif_clientdata
, module
,
2732 "Error writing directory link");
2737 nextdir
=nextnextdir
;
2744 m
= tif
->tif_diroff
;
2745 if (tif
->tif_flags
& TIFF_SWAB
)
2747 if (tif
->tif_header
.big
.tiff_diroff
== 0) {
2749 * First directory, overwrite offset in header.
2751 tif
->tif_header
.big
.tiff_diroff
= tif
->tif_diroff
;
2752 (void) TIFFSeekFile(tif
,8, SEEK_SET
);
2753 if (!WriteOK(tif
, &m
, 8)) {
2754 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2755 "Error writing TIFF header");
2761 * Not the first directory, search to the last and append.
2763 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
2769 if (!SeekOK(tif
, nextdir
) ||
2770 !ReadOK(tif
, &dircount64
, 8)) {
2771 TIFFErrorExt(tif
->tif_clientdata
, module
,
2772 "Error fetching directory count");
2775 if (tif
->tif_flags
& TIFF_SWAB
)
2776 TIFFSwabLong8(&dircount64
);
2777 if (dircount64
>0xFFFF)
2779 TIFFErrorExt(tif
->tif_clientdata
, module
,
2780 "Sanity check on tag count failed, likely corrupt TIFF");
2783 dircount
=(uint16
)dircount64
;
2784 (void) TIFFSeekFile(tif
,
2785 nextdir
+8+dircount
*20, SEEK_SET
);
2786 if (!ReadOK(tif
, &nextnextdir
, 8)) {
2787 TIFFErrorExt(tif
->tif_clientdata
, module
,
2788 "Error fetching directory link");
2791 if (tif
->tif_flags
& TIFF_SWAB
)
2792 TIFFSwabLong8(&nextnextdir
);
2795 (void) TIFFSeekFile(tif
,
2796 nextdir
+8+dircount
*20, SEEK_SET
);
2797 if (!WriteOK(tif
, &m
, 8)) {
2798 TIFFErrorExt(tif
->tif_clientdata
, module
,
2799 "Error writing directory link");
2804 nextdir
=nextnextdir
;
2810 /************************************************************************/
2811 /* TIFFRewriteField() */
2813 /* Rewrite a field in the directory on disk without regard to */
2814 /* updating the TIFF directory structure in memory. Currently */
2815 /* only supported for field that already exist in the on-disk */
2816 /* directory. Mainly used for updating stripoffset / */
2817 /* stripbytecount values after the directory is already on */
2820 /* Returns zero on failure, and one on success. */
2821 /************************************************************************/
2824 _TIFFRewriteField(TIFF
* tif
, uint16 tag
, TIFFDataType in_datatype
,
2825 tmsize_t count
, void* data
)
2827 static const char module
[] = "TIFFResetField";
2828 /* const TIFFField* fip = NULL; */
2831 uint8 direntry_raw
[20];
2832 uint16 entry_tag
= 0;
2833 uint16 entry_type
= 0;
2834 uint64 entry_count
= 0;
2835 uint64 entry_offset
= 0;
2836 int value_in_entry
= 0;
2838 uint8
*buf_to_write
= NULL
;
2839 TIFFDataType datatype
;
2841 /* -------------------------------------------------------------------- */
2842 /* Find field definition. */
2843 /* -------------------------------------------------------------------- */
2844 /*fip =*/ TIFFFindField(tif
, tag
, TIFF_ANY
);
2846 /* -------------------------------------------------------------------- */
2847 /* Do some checking this is a straight forward case. */
2848 /* -------------------------------------------------------------------- */
2851 TIFFErrorExt( tif
->tif_clientdata
, module
,
2852 "Memory mapped files not currently supported for this operation." );
2856 if( tif
->tif_diroff
== 0 )
2858 TIFFErrorExt( tif
->tif_clientdata
, module
,
2859 "Attempt to reset field on directory not already on disk." );
2863 /* -------------------------------------------------------------------- */
2864 /* Read the directory entry count. */
2865 /* -------------------------------------------------------------------- */
2866 if (!SeekOK(tif
, tif
->tif_diroff
)) {
2867 TIFFErrorExt(tif
->tif_clientdata
, module
,
2868 "%s: Seek error accessing TIFF directory",
2873 read_offset
= tif
->tif_diroff
;
2875 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2877 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) {
2878 TIFFErrorExt(tif
->tif_clientdata
, module
,
2879 "%s: Can not read TIFF directory count",
2883 if (tif
->tif_flags
& TIFF_SWAB
)
2884 TIFFSwabShort(&dircount
);
2889 if (!ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
2890 TIFFErrorExt(tif
->tif_clientdata
, module
,
2891 "%s: Can not read TIFF directory count",
2895 if (tif
->tif_flags
& TIFF_SWAB
)
2896 TIFFSwabLong8(&dircount64
);
2897 dircount
= (uint16
)dircount64
;
2902 /* -------------------------------------------------------------------- */
2903 /* Read through directory to find target tag. */
2904 /* -------------------------------------------------------------------- */
2905 while( dircount
> 0 )
2907 if (!ReadOK(tif
, direntry_raw
, dirsize
)) {
2908 TIFFErrorExt(tif
->tif_clientdata
, module
,
2909 "%s: Can not read TIFF directory entry.",
2914 memcpy( &entry_tag
, direntry_raw
+ 0, sizeof(uint16
) );
2915 if (tif
->tif_flags
&TIFF_SWAB
)
2916 TIFFSwabShort( &entry_tag
);
2918 if( entry_tag
== tag
)
2921 read_offset
+= dirsize
;
2924 if( entry_tag
!= tag
)
2926 TIFFErrorExt(tif
->tif_clientdata
, module
,
2927 "%s: Could not find tag %d.",
2928 tif
->tif_name
, tag
);
2932 /* -------------------------------------------------------------------- */
2933 /* Extract the type, count and offset for this entry. */
2934 /* -------------------------------------------------------------------- */
2935 memcpy( &entry_type
, direntry_raw
+ 2, sizeof(uint16
) );
2936 if (tif
->tif_flags
&TIFF_SWAB
)
2937 TIFFSwabShort( &entry_type
);
2939 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2943 memcpy( &value
, direntry_raw
+ 4, sizeof(uint32
) );
2944 if (tif
->tif_flags
&TIFF_SWAB
)
2945 TIFFSwabLong( &value
);
2946 entry_count
= value
;
2948 memcpy( &value
, direntry_raw
+ 8, sizeof(uint32
) );
2949 if (tif
->tif_flags
&TIFF_SWAB
)
2950 TIFFSwabLong( &value
);
2951 entry_offset
= value
;
2955 memcpy( &entry_count
, direntry_raw
+ 4, sizeof(uint64
) );
2956 if (tif
->tif_flags
&TIFF_SWAB
)
2957 TIFFSwabLong8( &entry_count
);
2959 memcpy( &entry_offset
, direntry_raw
+ 12, sizeof(uint64
) );
2960 if (tif
->tif_flags
&TIFF_SWAB
)
2961 TIFFSwabLong8( &entry_offset
);
2964 /* -------------------------------------------------------------------- */
2965 /* When a dummy tag was written due to TIFFDeferStrileArrayWriting() */
2966 /* -------------------------------------------------------------------- */
2967 if( entry_offset
== 0 && entry_count
== 0 && entry_type
== 0 )
2969 if( tag
== TIFFTAG_TILEOFFSETS
|| tag
== TIFFTAG_STRIPOFFSETS
)
2971 entry_type
= (tif
->tif_flags
&TIFF_BIGTIFF
) ? TIFF_LONG8
: TIFF_LONG
;
2975 int write_aslong8
= 1;
2976 if( count
> 1 && tag
== TIFFTAG_STRIPBYTECOUNTS
)
2978 write_aslong8
= WriteAsLong8(tif
, TIFFStripSize64(tif
));
2980 else if( count
> 1 && tag
== TIFFTAG_TILEBYTECOUNTS
)
2982 write_aslong8
= WriteAsLong8(tif
, TIFFTileSize64(tif
));
2986 entry_type
= TIFF_LONG8
;
2990 int write_aslong4
= 1;
2991 if( count
> 1 && tag
== TIFFTAG_STRIPBYTECOUNTS
)
2993 write_aslong4
= WriteAsLong4(tif
, TIFFStripSize64(tif
));
2995 else if( count
> 1 && tag
== TIFFTAG_TILEBYTECOUNTS
)
2997 write_aslong4
= WriteAsLong4(tif
, TIFFTileSize64(tif
));
3001 entry_type
= TIFF_LONG
;
3005 entry_type
= TIFF_SHORT
;
3011 /* -------------------------------------------------------------------- */
3012 /* What data type do we want to write this as? */
3013 /* -------------------------------------------------------------------- */
3014 if( TIFFDataWidth(in_datatype
) == 8 && !(tif
->tif_flags
&TIFF_BIGTIFF
) )
3016 if( in_datatype
== TIFF_LONG8
)
3017 datatype
= entry_type
== TIFF_SHORT
? TIFF_SHORT
: TIFF_LONG
;
3018 else if( in_datatype
== TIFF_SLONG8
)
3019 datatype
= TIFF_SLONG
;
3020 else if( in_datatype
== TIFF_IFD8
)
3021 datatype
= TIFF_IFD
;
3023 datatype
= in_datatype
;
3027 if( in_datatype
== TIFF_LONG8
&&
3028 (entry_type
== TIFF_SHORT
|| entry_type
== TIFF_LONG
||
3029 entry_type
== TIFF_LONG8
) )
3030 datatype
= entry_type
;
3031 else if( in_datatype
== TIFF_SLONG8
&&
3032 (entry_type
== TIFF_SLONG
|| entry_type
== TIFF_SLONG8
) )
3033 datatype
= entry_type
;
3034 else if( in_datatype
== TIFF_IFD8
&&
3035 (entry_type
== TIFF_IFD
|| entry_type
== TIFF_IFD8
) )
3036 datatype
= entry_type
;
3038 datatype
= in_datatype
;
3041 /* -------------------------------------------------------------------- */
3042 /* Prepare buffer of actual data to write. This includes */
3043 /* swabbing as needed. */
3044 /* -------------------------------------------------------------------- */
3046 (uint8
*)_TIFFCheckMalloc(tif
, count
, TIFFDataWidth(datatype
),
3047 "for field buffer.");
3051 if( datatype
== in_datatype
)
3052 memcpy( buf_to_write
, data
, count
* TIFFDataWidth(datatype
) );
3053 else if( datatype
== TIFF_SLONG
&& in_datatype
== TIFF_SLONG8
)
3057 for( i
= 0; i
< count
; i
++ )
3059 ((int32
*) buf_to_write
)[i
] =
3060 (int32
) ((int64
*) data
)[i
];
3061 if( (int64
) ((int32
*) buf_to_write
)[i
] != ((int64
*) data
)[i
] )
3063 _TIFFfree( buf_to_write
);
3064 TIFFErrorExt( tif
->tif_clientdata
, module
,
3065 "Value exceeds 32bit range of output type." );
3070 else if( (datatype
== TIFF_LONG
&& in_datatype
== TIFF_LONG8
)
3071 || (datatype
== TIFF_IFD
&& in_datatype
== TIFF_IFD8
) )
3075 for( i
= 0; i
< count
; i
++ )
3077 ((uint32
*) buf_to_write
)[i
] =
3078 (uint32
) ((uint64
*) data
)[i
];
3079 if( (uint64
) ((uint32
*) buf_to_write
)[i
] != ((uint64
*) data
)[i
] )
3081 _TIFFfree( buf_to_write
);
3082 TIFFErrorExt( tif
->tif_clientdata
, module
,
3083 "Value exceeds 32bit range of output type." );
3088 else if( datatype
== TIFF_SHORT
&& in_datatype
== TIFF_LONG8
)
3092 for( i
= 0; i
< count
; i
++ )
3094 ((uint16
*) buf_to_write
)[i
] =
3095 (uint16
) ((uint64
*) data
)[i
];
3096 if( (uint64
) ((uint16
*) buf_to_write
)[i
] != ((uint64
*) data
)[i
] )
3098 _TIFFfree( buf_to_write
);
3099 TIFFErrorExt( tif
->tif_clientdata
, module
,
3100 "Value exceeds 16bit range of output type." );
3107 TIFFErrorExt( tif
->tif_clientdata
, module
,
3108 "Unhandled type conversion." );
3112 if( TIFFDataWidth(datatype
) > 1 && (tif
->tif_flags
&TIFF_SWAB
) )
3114 if( TIFFDataWidth(datatype
) == 2 )
3115 TIFFSwabArrayOfShort( (uint16
*) buf_to_write
, count
);
3116 else if( TIFFDataWidth(datatype
) == 4 )
3117 TIFFSwabArrayOfLong( (uint32
*) buf_to_write
, count
);
3118 else if( TIFFDataWidth(datatype
) == 8 )
3119 TIFFSwabArrayOfLong8( (uint64
*) buf_to_write
, count
);
3122 /* -------------------------------------------------------------------- */
3123 /* Is this a value that fits into the directory entry? */
3124 /* -------------------------------------------------------------------- */
3125 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
3127 if( TIFFDataWidth(datatype
) * count
<= 4 )
3129 entry_offset
= read_offset
+ 8;
3135 if( TIFFDataWidth(datatype
) * count
<= 8 )
3137 entry_offset
= read_offset
+ 12;
3142 if( (tag
== TIFFTAG_TILEOFFSETS
|| tag
== TIFFTAG_STRIPOFFSETS
) &&
3143 tif
->tif_dir
.td_stripoffset_entry
.tdir_count
== 0 &&
3144 tif
->tif_dir
.td_stripoffset_entry
.tdir_type
== 0 &&
3145 tif
->tif_dir
.td_stripoffset_entry
.tdir_offset
.toff_long8
== 0 )
3147 tif
->tif_dir
.td_stripoffset_entry
.tdir_type
= datatype
;
3148 tif
->tif_dir
.td_stripoffset_entry
.tdir_count
= count
;
3150 else if( (tag
== TIFFTAG_TILEBYTECOUNTS
|| tag
== TIFFTAG_STRIPBYTECOUNTS
) &&
3151 tif
->tif_dir
.td_stripbytecount_entry
.tdir_count
== 0 &&
3152 tif
->tif_dir
.td_stripbytecount_entry
.tdir_type
== 0 &&
3153 tif
->tif_dir
.td_stripbytecount_entry
.tdir_offset
.toff_long8
== 0 )
3155 tif
->tif_dir
.td_stripbytecount_entry
.tdir_type
= datatype
;
3156 tif
->tif_dir
.td_stripbytecount_entry
.tdir_count
= count
;
3159 /* -------------------------------------------------------------------- */
3160 /* If the tag type, and count match, then we just write it out */
3161 /* over the old values without altering the directory entry at */
3163 /* -------------------------------------------------------------------- */
3164 if( entry_count
== (uint64
)count
&& entry_type
== (uint16
) datatype
)
3166 if (!SeekOK(tif
, entry_offset
)) {
3167 _TIFFfree( buf_to_write
);
3168 TIFFErrorExt(tif
->tif_clientdata
, module
,
3169 "%s: Seek error accessing TIFF directory",
3173 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
3174 _TIFFfree( buf_to_write
);
3175 TIFFErrorExt(tif
->tif_clientdata
, module
,
3176 "Error writing directory link");
3180 _TIFFfree( buf_to_write
);
3184 /* -------------------------------------------------------------------- */
3185 /* Otherwise, we write the new tag data at the end of the file. */
3186 /* -------------------------------------------------------------------- */
3187 if( !value_in_entry
)
3189 entry_offset
= TIFFSeekFile(tif
,0,SEEK_END
);
3191 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
3192 _TIFFfree( buf_to_write
);
3193 TIFFErrorExt(tif
->tif_clientdata
, module
,
3194 "Error writing directory link");
3200 memcpy( &entry_offset
, buf_to_write
, count
*TIFFDataWidth(datatype
));
3203 _TIFFfree( buf_to_write
);
3206 /* -------------------------------------------------------------------- */
3207 /* Adjust the directory entry. */
3208 /* -------------------------------------------------------------------- */
3209 entry_type
= datatype
;
3210 entry_count
= (uint64
)count
;
3211 memcpy( direntry_raw
+ 2, &entry_type
, sizeof(uint16
) );
3212 if (tif
->tif_flags
&TIFF_SWAB
)
3213 TIFFSwabShort( (uint16
*) (direntry_raw
+ 2) );
3215 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
3219 value
= (uint32
) entry_count
;
3220 memcpy( direntry_raw
+ 4, &value
, sizeof(uint32
) );
3221 if (tif
->tif_flags
&TIFF_SWAB
)
3222 TIFFSwabLong( (uint32
*) (direntry_raw
+ 4) );
3224 value
= (uint32
) entry_offset
;
3225 memcpy( direntry_raw
+ 8, &value
, sizeof(uint32
) );
3226 if (tif
->tif_flags
&TIFF_SWAB
)
3227 TIFFSwabLong( (uint32
*) (direntry_raw
+ 8) );
3231 memcpy( direntry_raw
+ 4, &entry_count
, sizeof(uint64
) );
3232 if (tif
->tif_flags
&TIFF_SWAB
)
3233 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 4) );
3235 memcpy( direntry_raw
+ 12, &entry_offset
, sizeof(uint64
) );
3236 if (tif
->tif_flags
&TIFF_SWAB
)
3237 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 12) );
3240 /* -------------------------------------------------------------------- */
3241 /* Write the directory entry out to disk. */
3242 /* -------------------------------------------------------------------- */
3243 if (!SeekOK(tif
, read_offset
)) {
3244 TIFFErrorExt(tif
->tif_clientdata
, module
,
3245 "%s: Seek error accessing TIFF directory",
3250 if (!WriteOK(tif
, direntry_raw
,dirsize
))
3252 TIFFErrorExt(tif
->tif_clientdata
, module
,
3253 "%s: Can not write TIFF directory entry.",
3260 /* vim: set ts=8 sts=8 sw=8 noet: */