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.
35 #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
36 #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
38 extern void TIFFCvtNativeToIEEEFloat(TIFF
* tif
, uint32 n
, float* fp
);
39 extern void TIFFCvtNativeToIEEEDouble(TIFF
* tif
, uint32 n
, double* dp
);
42 static int TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
);
44 static int TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
46 static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
49 static int TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
);
50 static int TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
52 static int TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
54 static int TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
56 static int TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
59 static int TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
61 static int TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
);
63 static int TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
65 static int TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
66 static int TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
);
67 static int TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
69 static int TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
71 static int TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
);
73 static int TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
75 static int TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
76 static int TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
78 static int TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
81 static int TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
83 static int TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
);
85 static int TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
88 static int TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
);
90 static int TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
92 static int TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
);
94 static int TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
);
95 static int TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
96 static int TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
97 static int TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
99 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
101 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
103 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
106 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
108 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
110 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
112 static int TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
114 static int TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
116 static int TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
117 static int TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
118 static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
120 static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
122 static int TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
123 static int TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
124 static int TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
);
126 static int TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
);
127 static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
129 static int TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
);
131 static int TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
);
133 static int TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
);
135 static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
);
136 static int TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
);
137 static int TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
);
139 static int TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
);
141 static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
);
142 static int TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
);
143 static int TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
145 static int TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
);
147 static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
);
149 static int TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
);
151 static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
153 static int TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
);
155 static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
);
156 static int TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
157 static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
158 static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
160 static int TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
);
162 static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
);
164 static int TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
);
166 static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
);
167 static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
);
168 static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
);
170 static int TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
);
172 static int TIFFLinkDirectory(TIFF
*);
175 * Write the contents of the current directory
176 * to the specified file. This routine doesn't
177 * handle overwriting a directory with auxiliary
178 * storage that's been changed.
181 TIFFWriteDirectory(TIFF
* tif
)
183 return TIFFWriteDirectorySec(tif
,TRUE
,TRUE
,NULL
);
187 * Similar to TIFFWriteDirectory(), writes the directory out
188 * but leaves all data structures in memory so that it can be
189 * written again. This will make a partially written TIFF file
190 * readable before it is successfully completed/closed.
193 TIFFCheckpointDirectory(TIFF
* tif
)
196 /* Setup the strips arrays, if they haven't already been. */
197 if (tif
->tif_dir
.td_stripoffset
== NULL
)
198 (void) TIFFSetupStrips(tif
);
199 rc
= TIFFWriteDirectorySec(tif
,TRUE
,FALSE
,NULL
);
200 (void) TIFFSetWriteOffset(tif
, TIFFSeekFile(tif
, 0, SEEK_END
));
205 TIFFWriteCustomDirectory(TIFF
* tif
, uint64
* pdiroff
)
207 return TIFFWriteDirectorySec(tif
,FALSE
,FALSE
,pdiroff
);
211 * Similar to TIFFWriteDirectory(), but if the directory has already
212 * been written once, it is relocated to the end of the file, in case it
213 * has changed in size. Note that this will result in the loss of the
214 * previously used directory space.
217 TIFFRewriteDirectory( TIFF
*tif
)
219 static const char module[] = "TIFFRewriteDirectory";
221 /* We don't need to do anything special if it hasn't been written. */
222 if( tif
->tif_diroff
== 0 )
223 return TIFFWriteDirectory( tif
);
226 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
227 * will cause it to be added after this directories current pre-link.
230 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
232 if (tif
->tif_header
.classic
.tiff_diroff
== tif
->tif_diroff
)
234 tif
->tif_header
.classic
.tiff_diroff
= 0;
237 TIFFSeekFile(tif
,4,SEEK_SET
);
238 if (!WriteOK(tif
, &(tif
->tif_header
.classic
.tiff_diroff
),4))
240 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
241 "Error updating TIFF header");
248 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
253 if (!SeekOK(tif
, nextdir
) ||
254 !ReadOK(tif
, &dircount
, 2)) {
255 TIFFErrorExt(tif
->tif_clientdata
, module,
256 "Error fetching directory count");
259 if (tif
->tif_flags
& TIFF_SWAB
)
260 TIFFSwabShort(&dircount
);
261 (void) TIFFSeekFile(tif
,
262 nextdir
+2+dircount
*12, SEEK_SET
);
263 if (!ReadOK(tif
, &nextnextdir
, 4)) {
264 TIFFErrorExt(tif
->tif_clientdata
, module,
265 "Error fetching directory link");
268 if (tif
->tif_flags
& TIFF_SWAB
)
269 TIFFSwabLong(&nextnextdir
);
270 if (nextnextdir
==tif
->tif_diroff
)
274 (void) TIFFSeekFile(tif
,
275 nextdir
+2+dircount
*12, SEEK_SET
);
276 if (!WriteOK(tif
, &m
, 4)) {
277 TIFFErrorExt(tif
->tif_clientdata
, module,
278 "Error writing directory link");
290 if (tif
->tif_header
.big
.tiff_diroff
== tif
->tif_diroff
)
292 tif
->tif_header
.big
.tiff_diroff
= 0;
295 TIFFSeekFile(tif
,8,SEEK_SET
);
296 if (!WriteOK(tif
, &(tif
->tif_header
.big
.tiff_diroff
),8))
298 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
299 "Error updating TIFF header");
306 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
312 if (!SeekOK(tif
, nextdir
) ||
313 !ReadOK(tif
, &dircount64
, 8)) {
314 TIFFErrorExt(tif
->tif_clientdata
, module,
315 "Error fetching directory count");
318 if (tif
->tif_flags
& TIFF_SWAB
)
319 TIFFSwabLong8(&dircount64
);
320 if (dircount64
>0xFFFF)
322 TIFFErrorExt(tif
->tif_clientdata
, module,
323 "Sanity check on tag count failed, likely corrupt TIFF");
326 dircount
=(uint16
)dircount64
;
327 (void) TIFFSeekFile(tif
,
328 nextdir
+8+dircount
*20, SEEK_SET
);
329 if (!ReadOK(tif
, &nextnextdir
, 8)) {
330 TIFFErrorExt(tif
->tif_clientdata
, module,
331 "Error fetching directory link");
334 if (tif
->tif_flags
& TIFF_SWAB
)
335 TIFFSwabLong8(&nextnextdir
);
336 if (nextnextdir
==tif
->tif_diroff
)
340 (void) TIFFSeekFile(tif
,
341 nextdir
+8+dircount
*20, SEEK_SET
);
342 if (!WriteOK(tif
, &m
, 8)) {
343 TIFFErrorExt(tif
->tif_clientdata
, module,
344 "Error writing directory link");
356 * Now use TIFFWriteDirectory() normally.
359 return TIFFWriteDirectory( tif
);
363 TIFFWriteDirectorySec(TIFF
* tif
, int isimage
, int imagedone
, uint64
* pdiroff
)
365 static const char module[] = "TIFFWriteDirectorySec";
371 if (tif
->tif_mode
== O_RDONLY
)
374 _TIFFFillStriles( tif
);
377 * Clear write state so that subsequent images with
378 * different characteristics get the right buffers
383 if (tif
->tif_flags
& TIFF_POSTENCODE
)
385 tif
->tif_flags
&= ~TIFF_POSTENCODE
;
386 if (!(*tif
->tif_postencode
)(tif
))
388 TIFFErrorExt(tif
->tif_clientdata
,module,
389 "Error post-encoding before directory write");
393 (*tif
->tif_close
)(tif
); /* shutdown encoder */
395 * Flush any data that might have been written
396 * by the compression close+cleanup routines. But
397 * be careful not to write stuff if we didn't add data
398 * in the previous steps as the "rawcc" data may well be
399 * a previously read tile/strip in mixed read/write mode.
401 if (tif
->tif_rawcc
> 0
402 && (tif
->tif_flags
& TIFF_BEENWRITING
) != 0 )
404 if( !TIFFFlushData1(tif
) )
406 TIFFErrorExt(tif
->tif_clientdata
, module,
407 "Error flushing data before directory write");
411 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
)
413 _TIFFfree(tif
->tif_rawdata
);
414 tif
->tif_rawdata
= NULL
;
416 tif
->tif_rawdatasize
= 0;
417 tif
->tif_rawdataoff
= 0;
418 tif
->tif_rawdataloaded
= 0;
420 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
);
430 if (TIFFFieldSet(tif
,FIELD_IMAGEDIMENSIONS
))
432 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEWIDTH
,tif
->tif_dir
.td_imagewidth
))
434 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_IMAGELENGTH
,tif
->tif_dir
.td_imagelength
))
437 if (TIFFFieldSet(tif
,FIELD_TILEDIMENSIONS
))
439 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILEWIDTH
,tif
->tif_dir
.td_tilewidth
))
441 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_TILELENGTH
,tif
->tif_dir
.td_tilelength
))
444 if (TIFFFieldSet(tif
,FIELD_RESOLUTION
))
446 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XRESOLUTION
,tif
->tif_dir
.td_xresolution
))
448 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YRESOLUTION
,tif
->tif_dir
.td_yresolution
))
451 if (TIFFFieldSet(tif
,FIELD_POSITION
))
453 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_XPOSITION
,tif
->tif_dir
.td_xposition
))
455 if (!TIFFWriteDirectoryTagRational(tif
,&ndir
,dir
,TIFFTAG_YPOSITION
,tif
->tif_dir
.td_yposition
))
458 if (TIFFFieldSet(tif
,FIELD_SUBFILETYPE
))
460 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_SUBFILETYPE
,tif
->tif_dir
.td_subfiletype
))
463 if (TIFFFieldSet(tif
,FIELD_BITSPERSAMPLE
))
465 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_BITSPERSAMPLE
,tif
->tif_dir
.td_bitspersample
))
468 if (TIFFFieldSet(tif
,FIELD_COMPRESSION
))
470 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_COMPRESSION
,tif
->tif_dir
.td_compression
))
473 if (TIFFFieldSet(tif
,FIELD_PHOTOMETRIC
))
475 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PHOTOMETRIC
,tif
->tif_dir
.td_photometric
))
478 if (TIFFFieldSet(tif
,FIELD_THRESHHOLDING
))
480 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_THRESHHOLDING
,tif
->tif_dir
.td_threshholding
))
483 if (TIFFFieldSet(tif
,FIELD_FILLORDER
))
485 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_FILLORDER
,tif
->tif_dir
.td_fillorder
))
488 if (TIFFFieldSet(tif
,FIELD_ORIENTATION
))
490 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_ORIENTATION
,tif
->tif_dir
.td_orientation
))
493 if (TIFFFieldSet(tif
,FIELD_SAMPLESPERPIXEL
))
495 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_SAMPLESPERPIXEL
,tif
->tif_dir
.td_samplesperpixel
))
498 if (TIFFFieldSet(tif
,FIELD_ROWSPERSTRIP
))
500 if (!TIFFWriteDirectoryTagShortLong(tif
,&ndir
,dir
,TIFFTAG_ROWSPERSTRIP
,tif
->tif_dir
.td_rowsperstrip
))
503 if (TIFFFieldSet(tif
,FIELD_MINSAMPLEVALUE
))
505 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MINSAMPLEVALUE
,tif
->tif_dir
.td_minsamplevalue
))
508 if (TIFFFieldSet(tif
,FIELD_MAXSAMPLEVALUE
))
510 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_MAXSAMPLEVALUE
,tif
->tif_dir
.td_maxsamplevalue
))
513 if (TIFFFieldSet(tif
,FIELD_PLANARCONFIG
))
515 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_PLANARCONFIG
,tif
->tif_dir
.td_planarconfig
))
518 if (TIFFFieldSet(tif
,FIELD_RESOLUTIONUNIT
))
520 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_RESOLUTIONUNIT
,tif
->tif_dir
.td_resolutionunit
))
523 if (TIFFFieldSet(tif
,FIELD_PAGENUMBER
))
525 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_PAGENUMBER
,2,&tif
->tif_dir
.td_pagenumber
[0]))
528 if (TIFFFieldSet(tif
,FIELD_STRIPBYTECOUNTS
))
532 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount
))
537 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEBYTECOUNTS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripbytecount
))
541 if (TIFFFieldSet(tif
,FIELD_STRIPOFFSETS
))
545 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_STRIPOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
550 if (!TIFFWriteDirectoryTagLongLong8Array(tif
,&ndir
,dir
,TIFFTAG_TILEOFFSETS
,tif
->tif_dir
.td_nstrips
,tif
->tif_dir
.td_stripoffset
))
554 if (TIFFFieldSet(tif
,FIELD_COLORMAP
))
556 if (!TIFFWriteDirectoryTagColormap(tif
,&ndir
,dir
))
559 if (TIFFFieldSet(tif
,FIELD_EXTRASAMPLES
))
561 if (tif
->tif_dir
.td_extrasamples
)
565 TIFFGetFieldDefaulted(tif
,TIFFTAG_EXTRASAMPLES
,&na
,&nb
);
566 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_EXTRASAMPLES
,na
,nb
))
570 if (TIFFFieldSet(tif
,FIELD_SAMPLEFORMAT
))
572 if (!TIFFWriteDirectoryTagShortPerSample(tif
,&ndir
,dir
,TIFFTAG_SAMPLEFORMAT
,tif
->tif_dir
.td_sampleformat
))
575 if (TIFFFieldSet(tif
,FIELD_SMINSAMPLEVALUE
))
577 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMINSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_sminsamplevalue
))
580 if (TIFFFieldSet(tif
,FIELD_SMAXSAMPLEVALUE
))
582 if (!TIFFWriteDirectoryTagSampleformatArray(tif
,&ndir
,dir
,TIFFTAG_SMAXSAMPLEVALUE
,tif
->tif_dir
.td_samplesperpixel
,tif
->tif_dir
.td_smaxsamplevalue
))
585 if (TIFFFieldSet(tif
,FIELD_IMAGEDEPTH
))
587 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_IMAGEDEPTH
,tif
->tif_dir
.td_imagedepth
))
590 if (TIFFFieldSet(tif
,FIELD_TILEDEPTH
))
592 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,TIFFTAG_TILEDEPTH
,tif
->tif_dir
.td_tiledepth
))
595 if (TIFFFieldSet(tif
,FIELD_HALFTONEHINTS
))
597 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_HALFTONEHINTS
,2,&tif
->tif_dir
.td_halftonehints
[0]))
600 if (TIFFFieldSet(tif
,FIELD_YCBCRSUBSAMPLING
))
602 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,TIFFTAG_YCBCRSUBSAMPLING
,2,&tif
->tif_dir
.td_ycbcrsubsampling
[0]))
605 if (TIFFFieldSet(tif
,FIELD_YCBCRPOSITIONING
))
607 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,TIFFTAG_YCBCRPOSITIONING
,tif
->tif_dir
.td_ycbcrpositioning
))
610 if (TIFFFieldSet(tif
,FIELD_REFBLACKWHITE
))
612 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,TIFFTAG_REFERENCEBLACKWHITE
,6,tif
->tif_dir
.td_refblackwhite
))
615 if (TIFFFieldSet(tif
,FIELD_TRANSFERFUNCTION
))
617 if (!TIFFWriteDirectoryTagTransferfunction(tif
,&ndir
,dir
))
620 if (TIFFFieldSet(tif
,FIELD_INKNAMES
))
622 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,TIFFTAG_INKNAMES
,tif
->tif_dir
.td_inknameslen
,tif
->tif_dir
.td_inknames
))
625 if (TIFFFieldSet(tif
,FIELD_SUBIFD
))
627 if (!TIFFWriteDirectoryTagSubifd(tif
,&ndir
,dir
))
632 for (n
=0; n
<tif
->tif_nfields
; n
++) {
634 o
= tif
->tif_fields
[n
];
635 if ((o
->field_bit
>=FIELD_CODEC
)&&(TIFFFieldSet(tif
,o
->field_bit
)))
637 switch (o
->get_field_type
)
639 case TIFF_SETGET_ASCII
:
643 assert(o
->field_type
==TIFF_ASCII
);
644 assert(o
->field_readcount
==TIFF_VARIABLE
);
645 assert(o
->field_passcount
==0);
646 TIFFGetField(tif
,o
->field_tag
,&pb
);
647 pa
=(uint32
)(strlen(pb
));
648 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,o
->field_tag
,pa
,pb
))
652 case TIFF_SETGET_UINT16
:
655 assert(o
->field_type
==TIFF_SHORT
);
656 assert(o
->field_readcount
==1);
657 assert(o
->field_passcount
==0);
658 TIFFGetField(tif
,o
->field_tag
,&p
);
659 if (!TIFFWriteDirectoryTagShort(tif
,&ndir
,dir
,o
->field_tag
,p
))
663 case TIFF_SETGET_UINT32
:
666 assert(o
->field_type
==TIFF_LONG
);
667 assert(o
->field_readcount
==1);
668 assert(o
->field_passcount
==0);
669 TIFFGetField(tif
,o
->field_tag
,&p
);
670 if (!TIFFWriteDirectoryTagLong(tif
,&ndir
,dir
,o
->field_tag
,p
))
674 case TIFF_SETGET_C32_UINT8
:
678 assert(o
->field_type
==TIFF_UNDEFINED
);
679 assert(o
->field_readcount
==TIFF_VARIABLE2
);
680 assert(o
->field_passcount
==1);
681 TIFFGetField(tif
,o
->field_tag
,&pa
,&pb
);
682 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,o
->field_tag
,pa
,pb
))
687 assert(0); /* we should never get here */
694 for (m
=0; m
<(uint32
)(tif
->tif_dir
.td_customValueCount
); m
++)
696 switch (tif
->tif_dir
.td_customValues
[m
].info
->field_type
)
699 if (!TIFFWriteDirectoryTagAscii(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
703 if (!TIFFWriteDirectoryTagUndefinedArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
707 if (!TIFFWriteDirectoryTagByteArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
711 if (!TIFFWriteDirectoryTagSbyteArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
715 if (!TIFFWriteDirectoryTagShortArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
719 if (!TIFFWriteDirectoryTagSshortArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
723 if (!TIFFWriteDirectoryTagLongArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
727 if (!TIFFWriteDirectoryTagSlongArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
731 if (!TIFFWriteDirectoryTagLong8Array(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
735 if (!TIFFWriteDirectoryTagSlong8Array(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
739 if (!TIFFWriteDirectoryTagRationalArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
743 if (!TIFFWriteDirectoryTagSrationalArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
747 if (!TIFFWriteDirectoryTagFloatArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
751 if (!TIFFWriteDirectoryTagDoubleArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
755 if (!TIFFWriteDirectoryTagIfdArray(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
759 if (!TIFFWriteDirectoryTagIfdIfd8Array(tif
,&ndir
,dir
,tif
->tif_dir
.td_customValues
[m
].info
->field_tag
,tif
->tif_dir
.td_customValues
[m
].count
,tif
->tif_dir
.td_customValues
[m
].value
))
763 assert(0); /* we should never get here */
769 dir
=_TIFFmalloc(ndir
*sizeof(TIFFDirEntry
));
772 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
777 if ((tif
->tif_diroff
==0)&&(!TIFFLinkDirectory(tif
)))
781 tif
->tif_diroff
=(TIFFSeekFile(tif
,0,SEEK_END
)+1)&(~1);
783 *pdiroff
=tif
->tif_diroff
;
784 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
788 tif
->tif_dataoff
=tif
->tif_diroff
+dirsize
;
789 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
790 tif
->tif_dataoff
=(uint32
)tif
->tif_dataoff
;
791 if ((tif
->tif_dataoff
<tif
->tif_diroff
)||(tif
->tif_dataoff
<(uint64
)dirsize
))
793 TIFFErrorExt(tif
->tif_clientdata
,module,"Maximum TIFF file size exceeded");
796 if (tif
->tif_dataoff
&1)
803 if (TIFFFieldSet(tif
,FIELD_SUBIFD
)&&(tif
->tif_subifdoff
==0))
807 for (na
=0, nb
=dir
; ; na
++, nb
++)
810 if (nb
->tdir_tag
==TIFFTAG_SUBIFD
)
813 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
814 tif
->tif_subifdoff
=tif
->tif_diroff
+2+na
*12+8;
816 tif
->tif_subifdoff
=tif
->tif_diroff
+8+na
*20+12;
819 dirmem
=_TIFFmalloc(dirsize
);
822 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
825 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
832 if (tif
->tif_flags
&TIFF_SWAB
)
833 TIFFSwabShort((uint16
*)n
);
836 for (m
=0; m
<ndir
; m
++)
838 *(uint16
*)n
=o
->tdir_tag
;
839 if (tif
->tif_flags
&TIFF_SWAB
)
840 TIFFSwabShort((uint16
*)n
);
842 *(uint16
*)n
=o
->tdir_type
;
843 if (tif
->tif_flags
&TIFF_SWAB
)
844 TIFFSwabShort((uint16
*)n
);
846 nTmp
= (uint32
)o
->tdir_count
;
847 _TIFFmemcpy(n
,&nTmp
,4);
848 if (tif
->tif_flags
&TIFF_SWAB
)
849 TIFFSwabLong((uint32
*)n
);
851 /* This is correct. The data has been */
852 /* swabbed previously in TIFFWriteDirectoryTagData */
853 _TIFFmemcpy(n
,&o
->tdir_offset
,4);
857 nTmp
= (uint32
)tif
->tif_nextdiroff
;
858 if (tif
->tif_flags
&TIFF_SWAB
)
860 _TIFFmemcpy(n
,&nTmp
,4);
868 if (tif
->tif_flags
&TIFF_SWAB
)
869 TIFFSwabLong8((uint64
*)n
);
872 for (m
=0; m
<ndir
; m
++)
874 *(uint16
*)n
=o
->tdir_tag
;
875 if (tif
->tif_flags
&TIFF_SWAB
)
876 TIFFSwabShort((uint16
*)n
);
878 *(uint16
*)n
=o
->tdir_type
;
879 if (tif
->tif_flags
&TIFF_SWAB
)
880 TIFFSwabShort((uint16
*)n
);
882 _TIFFmemcpy(n
,&o
->tdir_count
,8);
883 if (tif
->tif_flags
&TIFF_SWAB
)
884 TIFFSwabLong8((uint64
*)n
);
886 _TIFFmemcpy(n
,&o
->tdir_offset
,8);
890 _TIFFmemcpy(n
,&tif
->tif_nextdiroff
,8);
891 if (tif
->tif_flags
&TIFF_SWAB
)
892 TIFFSwabLong8((uint64
*)n
);
896 if (!SeekOK(tif
,tif
->tif_diroff
))
898 TIFFErrorExt(tif
->tif_clientdata
,module,"IO error writing directory");
901 if (!WriteOK(tif
,dirmem
,(tmsize_t
)dirsize
))
903 TIFFErrorExt(tif
->tif_clientdata
,module,"IO error writing directory");
909 TIFFFreeDirectory(tif
);
910 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
911 tif
->tif_flags
&= ~TIFF_DIRTYSTRIP
;
912 (*tif
->tif_cleanup
)(tif
);
914 * Reset directory-related state for subsequent
917 TIFFCreateDirectory(tif
);
929 TIFFWriteDirectoryTagSampleformatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
931 static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
935 conv
= _TIFFmalloc(count
*sizeof(double));
938 TIFFErrorExt(tif
->tif_clientdata
, module, "Out of memory");
942 switch (tif
->tif_dir
.td_sampleformat
)
944 case SAMPLEFORMAT_IEEEFP
:
945 if (tif
->tif_dir
.td_bitspersample
<=32)
947 for (i
= 0; i
< count
; ++i
)
948 ((float*)conv
)[i
] = (float)value
[i
];
949 ok
= TIFFWriteDirectoryTagFloatArray(tif
,ndir
,dir
,tag
,count
,(float*)conv
);
953 ok
= TIFFWriteDirectoryTagDoubleArray(tif
,ndir
,dir
,tag
,count
,value
);
956 case SAMPLEFORMAT_INT
:
957 if (tif
->tif_dir
.td_bitspersample
<=8)
959 for (i
= 0; i
< count
; ++i
)
960 ((int8
*)conv
)[i
] = (int8
)value
[i
];
961 ok
= TIFFWriteDirectoryTagSbyteArray(tif
,ndir
,dir
,tag
,count
,(int8
*)conv
);
963 else if (tif
->tif_dir
.td_bitspersample
<=16)
965 for (i
= 0; i
< count
; ++i
)
966 ((int16
*)conv
)[i
] = (int16
)value
[i
];
967 ok
= TIFFWriteDirectoryTagSshortArray(tif
,ndir
,dir
,tag
,count
,(int16
*)conv
);
971 for (i
= 0; i
< count
; ++i
)
972 ((int32
*)conv
)[i
] = (int32
)value
[i
];
973 ok
= TIFFWriteDirectoryTagSlongArray(tif
,ndir
,dir
,tag
,count
,(int32
*)conv
);
976 case SAMPLEFORMAT_UINT
:
977 if (tif
->tif_dir
.td_bitspersample
<=8)
979 for (i
= 0; i
< count
; ++i
)
980 ((uint8
*)conv
)[i
] = (uint8
)value
[i
];
981 ok
= TIFFWriteDirectoryTagByteArray(tif
,ndir
,dir
,tag
,count
,(uint8
*)conv
);
983 else if (tif
->tif_dir
.td_bitspersample
<=16)
985 for (i
= 0; i
< count
; ++i
)
986 ((uint16
*)conv
)[i
] = (uint16
)value
[i
];
987 ok
= TIFFWriteDirectoryTagShortArray(tif
,ndir
,dir
,tag
,count
,(uint16
*)conv
);
991 for (i
= 0; i
< count
; ++i
)
992 ((uint32
*)conv
)[i
] = (uint32
)value
[i
];
993 ok
= TIFFWriteDirectoryTagLongArray(tif
,ndir
,dir
,tag
,count
,(uint32
*)conv
);
1006 TIFFWriteDirectoryTagSampleformatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1008 switch (tif
->tif_dir
.td_sampleformat
)
1010 case SAMPLEFORMAT_IEEEFP
:
1011 if (tif
->tif_dir
.td_bitspersample
<=32)
1012 return(TIFFWriteDirectoryTagFloatPerSample(tif
,ndir
,dir
,tag
,(float)value
));
1014 return(TIFFWriteDirectoryTagDoublePerSample(tif
,ndir
,dir
,tag
,value
));
1015 case SAMPLEFORMAT_INT
:
1016 if (tif
->tif_dir
.td_bitspersample
<=8)
1017 return(TIFFWriteDirectoryTagSbytePerSample(tif
,ndir
,dir
,tag
,(int8
)value
));
1018 else if (tif
->tif_dir
.td_bitspersample
<=16)
1019 return(TIFFWriteDirectoryTagSshortPerSample(tif
,ndir
,dir
,tag
,(int16
)value
));
1021 return(TIFFWriteDirectoryTagSlongPerSample(tif
,ndir
,dir
,tag
,(int32
)value
));
1022 case SAMPLEFORMAT_UINT
:
1023 if (tif
->tif_dir
.td_bitspersample
<=8)
1024 return(TIFFWriteDirectoryTagBytePerSample(tif
,ndir
,dir
,tag
,(uint8
)value
));
1025 else if (tif
->tif_dir
.td_bitspersample
<=16)
1026 return(TIFFWriteDirectoryTagShortPerSample(tif
,ndir
,dir
,tag
,(uint16
)value
));
1028 return(TIFFWriteDirectoryTagLongPerSample(tif
,ndir
,dir
,tag
,(uint32
)value
));
1036 TIFFWriteDirectoryTagAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1043 return(TIFFWriteDirectoryTagCheckedAscii(tif
,ndir
,dir
,tag
,count
,value
));
1047 TIFFWriteDirectoryTagUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1054 return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif
,ndir
,dir
,tag
,count
,value
));
1059 TIFFWriteDirectoryTagByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1066 return(TIFFWriteDirectoryTagCheckedByte(tif
,ndir
,dir
,tag
,value
));
1071 TIFFWriteDirectoryTagByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1078 return(TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,count
,value
));
1083 TIFFWriteDirectoryTagBytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1085 static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
1095 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint8
));
1098 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1101 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1103 o
=TIFFWriteDirectoryTagCheckedByteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1111 TIFFWriteDirectoryTagSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1118 return(TIFFWriteDirectoryTagCheckedSbyte(tif
,ndir
,dir
,tag
,value
));
1123 TIFFWriteDirectoryTagSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1130 return(TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,count
,value
));
1135 TIFFWriteDirectoryTagSbytePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1137 static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
1147 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int8
));
1150 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1153 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1155 o
=TIFFWriteDirectoryTagCheckedSbyteArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1162 TIFFWriteDirectoryTagShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1169 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,value
));
1173 TIFFWriteDirectoryTagShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1180 return(TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,value
));
1184 TIFFWriteDirectoryTagShortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1186 static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
1196 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint16
));
1199 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1202 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1204 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1211 TIFFWriteDirectoryTagSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1218 return(TIFFWriteDirectoryTagCheckedSshort(tif
,ndir
,dir
,tag
,value
));
1223 TIFFWriteDirectoryTagSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1230 return(TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,count
,value
));
1235 TIFFWriteDirectoryTagSshortPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1237 static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
1247 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int16
));
1250 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1253 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1255 o
=TIFFWriteDirectoryTagCheckedSshortArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1262 TIFFWriteDirectoryTagLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1269 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1273 TIFFWriteDirectoryTagLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1280 return(TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,value
));
1285 TIFFWriteDirectoryTagLongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1287 static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
1297 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(uint32
));
1300 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1303 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1305 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1313 TIFFWriteDirectoryTagSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1320 return(TIFFWriteDirectoryTagCheckedSlong(tif
,ndir
,dir
,tag
,value
));
1325 TIFFWriteDirectoryTagSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
1332 return(TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,count
,value
));
1337 TIFFWriteDirectoryTagSlongPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
1339 static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
1349 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(int32
));
1352 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1355 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1357 o
=TIFFWriteDirectoryTagCheckedSlongArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1365 TIFFWriteDirectoryTagLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
1372 return(TIFFWriteDirectoryTagCheckedLong8(tif
,ndir
,dir
,tag
,value
));
1377 TIFFWriteDirectoryTagLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1384 return(TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1389 TIFFWriteDirectoryTagSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
1396 return(TIFFWriteDirectoryTagCheckedSlong8(tif
,ndir
,dir
,tag
,value
));
1401 TIFFWriteDirectoryTagSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
1408 return(TIFFWriteDirectoryTagCheckedSlong8Array(tif
,ndir
,dir
,tag
,count
,value
));
1412 TIFFWriteDirectoryTagRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1419 return(TIFFWriteDirectoryTagCheckedRational(tif
,ndir
,dir
,tag
,value
));
1423 TIFFWriteDirectoryTagRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1430 return(TIFFWriteDirectoryTagCheckedRationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1434 TIFFWriteDirectoryTagSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1441 return(TIFFWriteDirectoryTagCheckedSrationalArray(tif
,ndir
,dir
,tag
,count
,value
));
1445 static int TIFFWriteDirectoryTagFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1452 return(TIFFWriteDirectoryTagCheckedFloat(tif
,ndir
,dir
,tag
,value
));
1456 static int TIFFWriteDirectoryTagFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
1463 return(TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,count
,value
));
1467 static int TIFFWriteDirectoryTagFloatPerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
1469 static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
1479 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(float));
1482 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1485 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1487 o
=TIFFWriteDirectoryTagCheckedFloatArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1494 static int TIFFWriteDirectoryTagDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1501 return(TIFFWriteDirectoryTagCheckedDouble(tif
,ndir
,dir
,tag
,value
));
1505 static int TIFFWriteDirectoryTagDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
1512 return(TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,count
,value
));
1516 static int TIFFWriteDirectoryTagDoublePerSample(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
1518 static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
1528 m
=_TIFFmalloc(tif
->tif_dir
.td_samplesperpixel
*sizeof(double));
1531 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1534 for (na
=m
, nb
=0; nb
<tif
->tif_dir
.td_samplesperpixel
; na
++, nb
++)
1536 o
=TIFFWriteDirectoryTagCheckedDoubleArray(tif
,ndir
,dir
,tag
,tif
->tif_dir
.td_samplesperpixel
,m
);
1543 TIFFWriteDirectoryTagIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
1550 return(TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,value
));
1555 TIFFWriteDirectoryTagIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1562 return(TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,tag
,count
,value
));
1567 TIFFWriteDirectoryTagShortLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1575 return(TIFFWriteDirectoryTagCheckedShort(tif
,ndir
,dir
,tag
,(uint16
)value
));
1577 return(TIFFWriteDirectoryTagCheckedLong(tif
,ndir
,dir
,tag
,value
));
1580 /************************************************************************/
1581 /* TIFFWriteDirectoryTagLongLong8Array() */
1583 /* Write out LONG8 array as LONG8 for BigTIFF or LONG for */
1584 /* Classic TIFF with some checking. */
1585 /************************************************************************/
1588 TIFFWriteDirectoryTagLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1590 static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
1597 /* is this just a counting pass? */
1604 /* We always write LONG8 for BigTIFF, no checking needed. */
1605 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1606 return TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,
1610 ** For classic tiff we want to verify everything is in range for LONG
1611 ** and convert to long format.
1614 p
= _TIFFmalloc(count
*sizeof(uint32
));
1617 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1621 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1625 TIFFErrorExt(tif
->tif_clientdata
,module,
1626 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1633 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1639 /************************************************************************/
1640 /* TIFFWriteDirectoryTagIfdIfd8Array() */
1642 /* Write either IFD8 or IFD array depending on file type. */
1643 /************************************************************************/
1646 TIFFWriteDirectoryTagIfdIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1648 static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1655 /* is this just a counting pass? */
1662 /* We always write IFD8 for BigTIFF, no checking needed. */
1663 if( tif
->tif_flags
&TIFF_BIGTIFF
)
1664 return TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,
1668 ** For classic tiff we want to verify everything is in range for IFD
1669 ** and convert to long format.
1672 p
= _TIFFmalloc(count
*sizeof(uint32
));
1675 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1679 for (q
=p
, ma
=value
, mb
=0; mb
<count
; ma
++, mb
++, q
++)
1683 TIFFErrorExt(tif
->tif_clientdata
,module,
1684 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1691 o
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,tag
,count
,p
);
1699 TIFFWriteDirectoryTagShortLongLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
1701 static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1712 for (ma
=value
, mb
=0; mb
<count
; ma
++, mb
++)
1714 if ((n
==0)&&(*ma
>0xFFFF))
1716 if ((n
==1)&&(*ma
>0xFFFFFFFF))
1726 p
=_TIFFmalloc(count
*sizeof(uint16
));
1729 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1732 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1734 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,tag
,count
,p
);
1741 p
=_TIFFmalloc(count
*sizeof(uint32
));
1744 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1747 for (ma
=value
, mb
=0, q
=p
; mb
<count
; ma
++, mb
++, q
++)
1749 o
=TIFFWriteDirectoryTagCheckedLongArray(tif
,ndir
,dir
,tag
,count
,p
);
1755 o
=TIFFWriteDirectoryTagCheckedLong8Array(tif
,ndir
,dir
,tag
,count
,value
);
1761 TIFFWriteDirectoryTagColormap(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1763 static const char module[] = "TIFFWriteDirectoryTagColormap";
1772 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1773 n
=_TIFFmalloc(3*m
*sizeof(uint16
));
1776 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1779 _TIFFmemcpy(&n
[0],tif
->tif_dir
.td_colormap
[0],m
*sizeof(uint16
));
1780 _TIFFmemcpy(&n
[m
],tif
->tif_dir
.td_colormap
[1],m
*sizeof(uint16
));
1781 _TIFFmemcpy(&n
[2*m
],tif
->tif_dir
.td_colormap
[2],m
*sizeof(uint16
));
1782 o
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_COLORMAP
,3*m
,n
);
1788 TIFFWriteDirectoryTagTransferfunction(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1790 static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
1800 m
=(1<<tif
->tif_dir
.td_bitspersample
);
1801 n
=tif
->tif_dir
.td_samplesperpixel
-tif
->tif_dir
.td_extrasamples
;
1803 * Check if the table can be written as a single column,
1804 * or if it must be written as 3 columns. Note that we
1805 * write a 3-column tag if there are 2 samples/pixel and
1806 * a single column of data won't suffice--hmm.
1812 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
)))
1817 if (!_TIFFmemcmp(tif
->tif_dir
.td_transferfunction
[0],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
)))
1822 o
=_TIFFmalloc(n
*m
*sizeof(uint16
));
1825 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1828 _TIFFmemcpy(&o
[0],tif
->tif_dir
.td_transferfunction
[0],m
*sizeof(uint16
));
1830 _TIFFmemcpy(&o
[m
],tif
->tif_dir
.td_transferfunction
[1],m
*sizeof(uint16
));
1832 _TIFFmemcpy(&o
[2*m
],tif
->tif_dir
.td_transferfunction
[2],m
*sizeof(uint16
));
1833 p
=TIFFWriteDirectoryTagCheckedShortArray(tif
,ndir
,dir
,TIFFTAG_TRANSFERFUNCTION
,n
*m
,o
);
1839 TIFFWriteDirectoryTagSubifd(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
)
1841 static const char module[] = "TIFFWriteDirectoryTagSubifd";
1844 if (tif
->tif_dir
.td_nsubifd
==0)
1852 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1858 o
=_TIFFmalloc(tif
->tif_dir
.td_nsubifd
*sizeof(uint32
));
1861 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
1864 pa
=tif
->tif_dir
.td_subifd
;
1866 for (p
=0; p
< tif
->tif_dir
.td_nsubifd
; p
++)
1869 assert(*pa
<= 0xFFFFFFFFUL
);
1870 *pb
++=(uint32
)(*pa
++);
1872 n
=TIFFWriteDirectoryTagCheckedIfdArray(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,o
);
1876 n
=TIFFWriteDirectoryTagCheckedIfd8Array(tif
,ndir
,dir
,TIFFTAG_SUBIFD
,tif
->tif_dir
.td_nsubifd
,tif
->tif_dir
.td_subifd
);
1880 * Total hack: if this directory includes a SubIFD
1881 * tag then force the next <n> directories to be
1882 * written as ``sub directories'' of this one. This
1883 * is used to write things like thumbnails and
1884 * image masks that one wants to keep out of the
1885 * normal directory linkage access mechanism.
1887 tif
->tif_flags
|=TIFF_INSUBIFD
;
1888 tif
->tif_nsubifd
=tif
->tif_dir
.td_nsubifd
;
1889 if (tif
->tif_dir
.td_nsubifd
==1)
1890 tif
->tif_subifdoff
=0;
1892 tif
->tif_subifdoff
=m
;
1897 TIFFWriteDirectoryTagCheckedAscii(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, char* value
)
1899 assert(sizeof(char)==1);
1900 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_ASCII
,count
,count
,value
));
1904 TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1906 assert(sizeof(uint8
)==1);
1907 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_UNDEFINED
,count
,count
,value
));
1912 TIFFWriteDirectoryTagCheckedByte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint8 value
)
1914 assert(sizeof(uint8
)==1);
1915 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,1,1,&value
));
1920 TIFFWriteDirectoryTagCheckedByteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint8
* value
)
1922 assert(sizeof(uint8
)==1);
1923 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_BYTE
,count
,count
,value
));
1928 TIFFWriteDirectoryTagCheckedSbyte(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int8 value
)
1930 assert(sizeof(int8
)==1);
1931 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,1,1,&value
));
1936 TIFFWriteDirectoryTagCheckedSbyteArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int8
* value
)
1938 assert(sizeof(int8
)==1);
1939 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SBYTE
,count
,count
,value
));
1943 TIFFWriteDirectoryTagCheckedShort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 value
)
1946 assert(sizeof(uint16
)==2);
1948 if (tif
->tif_flags
&TIFF_SWAB
)
1950 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,1,2,&m
));
1954 TIFFWriteDirectoryTagCheckedShortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint16
* value
)
1956 assert(count
<0x80000000);
1957 assert(sizeof(uint16
)==2);
1958 if (tif
->tif_flags
&TIFF_SWAB
)
1959 TIFFSwabArrayOfShort(value
,count
);
1960 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SHORT
,count
,count
*2,value
));
1965 TIFFWriteDirectoryTagCheckedSshort(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int16 value
)
1968 assert(sizeof(int16
)==2);
1970 if (tif
->tif_flags
&TIFF_SWAB
)
1971 TIFFSwabShort((uint16
*)(&m
));
1972 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,1,2,&m
));
1977 TIFFWriteDirectoryTagCheckedSshortArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int16
* value
)
1979 assert(count
<0x80000000);
1980 assert(sizeof(int16
)==2);
1981 if (tif
->tif_flags
&TIFF_SWAB
)
1982 TIFFSwabArrayOfShort((uint16
*)value
,count
);
1983 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SSHORT
,count
,count
*2,value
));
1987 TIFFWriteDirectoryTagCheckedLong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 value
)
1990 assert(sizeof(uint32
)==4);
1992 if (tif
->tif_flags
&TIFF_SWAB
)
1994 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,1,4,&m
));
1998 TIFFWriteDirectoryTagCheckedLongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2000 assert(count
<0x40000000);
2001 assert(sizeof(uint32
)==4);
2002 if (tif
->tif_flags
&TIFF_SWAB
)
2003 TIFFSwabArrayOfLong(value
,count
);
2004 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG
,count
,count
*4,value
));
2009 TIFFWriteDirectoryTagCheckedSlong(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int32 value
)
2012 assert(sizeof(int32
)==4);
2014 if (tif
->tif_flags
&TIFF_SWAB
)
2015 TIFFSwabLong((uint32
*)(&m
));
2016 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,1,4,&m
));
2021 TIFFWriteDirectoryTagCheckedSlongArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int32
* value
)
2023 assert(count
<0x40000000);
2024 assert(sizeof(int32
)==4);
2025 if (tif
->tif_flags
&TIFF_SWAB
)
2026 TIFFSwabArrayOfLong((uint32
*)value
,count
);
2027 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG
,count
,count
*4,value
));
2032 TIFFWriteDirectoryTagCheckedLong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint64 value
)
2035 assert(sizeof(uint64
)==8);
2036 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2038 if (tif
->tif_flags
&TIFF_SWAB
)
2040 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,1,8,&m
));
2045 TIFFWriteDirectoryTagCheckedLong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2047 assert(count
<0x20000000);
2048 assert(sizeof(uint64
)==8);
2049 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2050 if (tif
->tif_flags
&TIFF_SWAB
)
2051 TIFFSwabArrayOfLong8(value
,count
);
2052 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_LONG8
,count
,count
*8,value
));
2057 TIFFWriteDirectoryTagCheckedSlong8(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, int64 value
)
2060 assert(sizeof(int64
)==8);
2061 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2063 if (tif
->tif_flags
&TIFF_SWAB
)
2064 TIFFSwabLong8((uint64
*)(&m
));
2065 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,1,8,&m
));
2070 TIFFWriteDirectoryTagCheckedSlong8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, int64
* value
)
2072 assert(count
<0x20000000);
2073 assert(sizeof(int64
)==8);
2074 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2075 if (tif
->tif_flags
&TIFF_SWAB
)
2076 TIFFSwabArrayOfLong8((uint64
*)value
,count
);
2077 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SLONG8
,count
,count
*8,value
));
2081 TIFFWriteDirectoryTagCheckedRational(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2085 assert(sizeof(uint32
)==4);
2091 else if (value
==(double)(uint32
)value
)
2098 m
[0]=(uint32
)(value
*0xFFFFFFFF);
2104 m
[1]=(uint32
)(0xFFFFFFFF/value
);
2106 if (tif
->tif_flags
&TIFF_SWAB
)
2108 TIFFSwabLong(&m
[0]);
2109 TIFFSwabLong(&m
[1]);
2111 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,1,8,&m
[0]));
2115 TIFFWriteDirectoryTagCheckedRationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2117 static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2123 assert(sizeof(uint32
)==4);
2124 m
=_TIFFmalloc(count
*2*sizeof(uint32
));
2127 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
2130 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2137 else if (*na
==(float)(uint32
)(*na
))
2139 nb
[0]=(uint32
)(*na
);
2144 nb
[0]=(uint32
)((*na
)*0xFFFFFFFF);
2150 nb
[1]=(uint32
)(0xFFFFFFFF/(*na
));
2153 if (tif
->tif_flags
&TIFF_SWAB
)
2154 TIFFSwabArrayOfLong(m
,count
*2);
2155 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_RATIONAL
,count
,count
*8,&m
[0]);
2161 TIFFWriteDirectoryTagCheckedSrationalArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2163 static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2169 assert(sizeof(int32
)==4);
2170 m
=_TIFFmalloc(count
*2*sizeof(int32
));
2173 TIFFErrorExt(tif
->tif_clientdata
,module,"Out of memory");
2176 for (na
=value
, nb
=m
, nc
=0; nc
<count
; na
++, nb
+=2, nc
++)
2180 if (*na
==(int32
)(*na
))
2187 nb
[0]=-(int32
)((-*na
)*0x7FFFFFFF);
2193 nb
[1]=(int32
)(0x7FFFFFFF/(-*na
));
2198 if (*na
==(int32
)(*na
))
2205 nb
[0]=(int32
)((*na
)*0x7FFFFFFF);
2211 nb
[1]=(int32
)(0x7FFFFFFF/(*na
));
2215 if (tif
->tif_flags
&TIFF_SWAB
)
2216 TIFFSwabArrayOfLong((uint32
*)m
,count
*2);
2217 o
=TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_SRATIONAL
,count
,count
*8,&m
[0]);
2224 TIFFWriteDirectoryTagCheckedFloat(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, float value
)
2227 assert(sizeof(float)==4);
2229 TIFFCvtNativeToIEEEFloat(tif
,1,&m
);
2230 if (tif
->tif_flags
&TIFF_SWAB
)
2232 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,1,4,&m
));
2237 TIFFWriteDirectoryTagCheckedFloatArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, float* value
)
2239 assert(count
<0x40000000);
2240 assert(sizeof(float)==4);
2241 TIFFCvtNativeToIEEEFloat(tif
,count
,&value
);
2242 if (tif
->tif_flags
&TIFF_SWAB
)
2243 TIFFSwabArrayOfFloat(value
,count
);
2244 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_FLOAT
,count
,count
*4,value
));
2249 TIFFWriteDirectoryTagCheckedDouble(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, double value
)
2252 assert(sizeof(double)==8);
2254 TIFFCvtNativeToIEEEDouble(tif
,1,&m
);
2255 if (tif
->tif_flags
&TIFF_SWAB
)
2257 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,1,8,&m
));
2262 TIFFWriteDirectoryTagCheckedDoubleArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, double* value
)
2264 assert(count
<0x20000000);
2265 assert(sizeof(double)==8);
2266 TIFFCvtNativeToIEEEDouble(tif
,count
,&value
);
2267 if (tif
->tif_flags
&TIFF_SWAB
)
2268 TIFFSwabArrayOfDouble(value
,count
);
2269 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_DOUBLE
,count
,count
*8,value
));
2273 TIFFWriteDirectoryTagCheckedIfdArray(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint32
* value
)
2275 assert(count
<0x40000000);
2276 assert(sizeof(uint32
)==4);
2277 if (tif
->tif_flags
&TIFF_SWAB
)
2278 TIFFSwabArrayOfLong(value
,count
);
2279 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD
,count
,count
*4,value
));
2283 TIFFWriteDirectoryTagCheckedIfd8Array(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint32 count
, uint64
* value
)
2285 assert(count
<0x20000000);
2286 assert(sizeof(uint64
)==8);
2287 assert(tif
->tif_flags
&TIFF_BIGTIFF
);
2288 if (tif
->tif_flags
&TIFF_SWAB
)
2289 TIFFSwabArrayOfLong8(value
,count
);
2290 return(TIFFWriteDirectoryTagData(tif
,ndir
,dir
,tag
,TIFF_IFD8
,count
,count
*8,value
));
2294 TIFFWriteDirectoryTagData(TIFF
* tif
, uint32
* ndir
, TIFFDirEntry
* dir
, uint16 tag
, uint16 datatype
, uint32 count
, uint32 datalength
, void* data
)
2296 static const char module[] = "TIFFWriteDirectoryTagData";
2301 assert(dir
[m
].tdir_tag
!=tag
);
2302 if (dir
[m
].tdir_tag
>tag
)
2309 for (n
=*ndir
; n
>m
; n
--)
2312 dir
[m
].tdir_tag
=tag
;
2313 dir
[m
].tdir_type
=datatype
;
2314 dir
[m
].tdir_count
=count
;
2315 dir
[m
].tdir_offset
.toff_long8
= 0;
2316 if (datalength
<=((tif
->tif_flags
&TIFF_BIGTIFF
)?0x8U
:0x4U
))
2317 _TIFFmemcpy(&dir
[m
].tdir_offset
,data
,datalength
);
2321 na
=tif
->tif_dataoff
;
2323 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2325 if ((nb
<na
)||(nb
<datalength
))
2327 TIFFErrorExt(tif
->tif_clientdata
,module,"Maximum TIFF file size exceeded");
2330 if (!SeekOK(tif
,na
))
2332 TIFFErrorExt(tif
->tif_clientdata
,module,"IO error writing tag data");
2335 assert(datalength
<0x80000000UL
);
2336 if (!WriteOK(tif
,data
,(tmsize_t
)datalength
))
2338 TIFFErrorExt(tif
->tif_clientdata
,module,"IO error writing tag data");
2341 tif
->tif_dataoff
=nb
;
2342 if (tif
->tif_dataoff
&1)
2344 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2348 if (tif
->tif_flags
&TIFF_SWAB
)
2350 _TIFFmemcpy(&dir
[m
].tdir_offset
,&o
,4);
2354 dir
[m
].tdir_offset
.toff_long8
= na
;
2355 if (tif
->tif_flags
&TIFF_SWAB
)
2356 TIFFSwabLong8(&dir
[m
].tdir_offset
.toff_long8
);
2364 * Link the current directory into the directory chain for the file.
2367 TIFFLinkDirectory(TIFF
* tif
)
2369 static const char module[] = "TIFFLinkDirectory";
2371 tif
->tif_diroff
= (TIFFSeekFile(tif
,0,SEEK_END
)+1) &~ 1;
2376 if (tif
->tif_flags
& TIFF_INSUBIFD
)
2378 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2381 m
= (uint32
)tif
->tif_diroff
;
2382 if (tif
->tif_flags
& TIFF_SWAB
)
2384 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2385 if (!WriteOK(tif
, &m
, 4)) {
2386 TIFFErrorExt(tif
->tif_clientdata
, module,
2387 "Error writing SubIFD directory link");
2391 * Advance to the next SubIFD or, if this is
2392 * the last one configured, revert back to the
2393 * normal directory linkage.
2395 if (--tif
->tif_nsubifd
)
2396 tif
->tif_subifdoff
+= 4;
2398 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2404 m
= tif
->tif_diroff
;
2405 if (tif
->tif_flags
& TIFF_SWAB
)
2407 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
);
2408 if (!WriteOK(tif
, &m
, 8)) {
2409 TIFFErrorExt(tif
->tif_clientdata
, module,
2410 "Error writing SubIFD directory link");
2414 * Advance to the next SubIFD or, if this is
2415 * the last one configured, revert back to the
2416 * normal directory linkage.
2418 if (--tif
->tif_nsubifd
)
2419 tif
->tif_subifdoff
+= 8;
2421 tif
->tif_flags
&= ~TIFF_INSUBIFD
;
2426 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2430 m
= (uint32
)(tif
->tif_diroff
);
2431 if (tif
->tif_flags
& TIFF_SWAB
)
2433 if (tif
->tif_header
.classic
.tiff_diroff
== 0) {
2435 * First directory, overwrite offset in header.
2437 tif
->tif_header
.classic
.tiff_diroff
= (uint32
) tif
->tif_diroff
;
2438 (void) TIFFSeekFile(tif
,4, SEEK_SET
);
2439 if (!WriteOK(tif
, &m
, 4)) {
2440 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2441 "Error writing TIFF header");
2447 * Not the first directory, search to the last and append.
2449 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
2454 if (!SeekOK(tif
, nextdir
) ||
2455 !ReadOK(tif
, &dircount
, 2)) {
2456 TIFFErrorExt(tif
->tif_clientdata
, module,
2457 "Error fetching directory count");
2460 if (tif
->tif_flags
& TIFF_SWAB
)
2461 TIFFSwabShort(&dircount
);
2462 (void) TIFFSeekFile(tif
,
2463 nextdir
+2+dircount
*12, SEEK_SET
);
2464 if (!ReadOK(tif
, &nextnextdir
, 4)) {
2465 TIFFErrorExt(tif
->tif_clientdata
, module,
2466 "Error fetching directory link");
2469 if (tif
->tif_flags
& TIFF_SWAB
)
2470 TIFFSwabLong(&nextnextdir
);
2473 (void) TIFFSeekFile(tif
,
2474 nextdir
+2+dircount
*12, SEEK_SET
);
2475 if (!WriteOK(tif
, &m
, 4)) {
2476 TIFFErrorExt(tif
->tif_clientdata
, module,
2477 "Error writing directory link");
2482 nextdir
=nextnextdir
;
2489 m
= tif
->tif_diroff
;
2490 if (tif
->tif_flags
& TIFF_SWAB
)
2492 if (tif
->tif_header
.big
.tiff_diroff
== 0) {
2494 * First directory, overwrite offset in header.
2496 tif
->tif_header
.big
.tiff_diroff
= tif
->tif_diroff
;
2497 (void) TIFFSeekFile(tif
,8, SEEK_SET
);
2498 if (!WriteOK(tif
, &m
, 8)) {
2499 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
2500 "Error writing TIFF header");
2506 * Not the first directory, search to the last and append.
2508 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
2514 if (!SeekOK(tif
, nextdir
) ||
2515 !ReadOK(tif
, &dircount64
, 8)) {
2516 TIFFErrorExt(tif
->tif_clientdata
, module,
2517 "Error fetching directory count");
2520 if (tif
->tif_flags
& TIFF_SWAB
)
2521 TIFFSwabLong8(&dircount64
);
2522 if (dircount64
>0xFFFF)
2524 TIFFErrorExt(tif
->tif_clientdata
, module,
2525 "Sanity check on tag count failed, likely corrupt TIFF");
2528 dircount
=(uint16
)dircount64
;
2529 (void) TIFFSeekFile(tif
,
2530 nextdir
+8+dircount
*20, SEEK_SET
);
2531 if (!ReadOK(tif
, &nextnextdir
, 8)) {
2532 TIFFErrorExt(tif
->tif_clientdata
, module,
2533 "Error fetching directory link");
2536 if (tif
->tif_flags
& TIFF_SWAB
)
2537 TIFFSwabLong8(&nextnextdir
);
2540 (void) TIFFSeekFile(tif
,
2541 nextdir
+8+dircount
*20, SEEK_SET
);
2542 if (!WriteOK(tif
, &m
, 8)) {
2543 TIFFErrorExt(tif
->tif_clientdata
, module,
2544 "Error writing directory link");
2549 nextdir
=nextnextdir
;
2555 /************************************************************************/
2556 /* TIFFRewriteField() */
2558 /* Rewrite a field in the directory on disk without regard to */
2559 /* updating the TIFF directory structure in memory. Currently */
2560 /* only supported for field that already exist in the on-disk */
2561 /* directory. Mainly used for updating stripoffset / */
2562 /* stripbytecount values after the directory is already on */
2565 /* Returns zero on failure, and one on success. */
2566 /************************************************************************/
2569 _TIFFRewriteField(TIFF
* tif
, uint16 tag
, TIFFDataType in_datatype
,
2570 tmsize_t count
, void* data
)
2572 static const char module[] = "TIFFResetField";
2573 /* const TIFFField* fip = NULL; */
2576 uint8 direntry_raw
[20];
2577 uint16 entry_tag
= 0;
2578 uint16 entry_type
= 0;
2579 uint64 entry_count
= 0;
2580 uint64 entry_offset
= 0;
2581 int value_in_entry
= 0;
2583 uint8
*buf_to_write
= NULL
;
2584 TIFFDataType datatype
;
2586 /* -------------------------------------------------------------------- */
2587 /* Find field definition. */
2588 /* -------------------------------------------------------------------- */
2589 /*fip =*/ TIFFFindField(tif
, tag
, TIFF_ANY
);
2591 /* -------------------------------------------------------------------- */
2592 /* Do some checking this is a straight forward case. */
2593 /* -------------------------------------------------------------------- */
2596 TIFFErrorExt( tif
->tif_clientdata
, module,
2597 "Memory mapped files not currently supported for this operation." );
2601 if( tif
->tif_diroff
== 0 )
2603 TIFFErrorExt( tif
->tif_clientdata
, module,
2604 "Attempt to reset field on directory not already on disk." );
2608 /* -------------------------------------------------------------------- */
2609 /* Read the directory entry count. */
2610 /* -------------------------------------------------------------------- */
2611 if (!SeekOK(tif
, tif
->tif_diroff
)) {
2612 TIFFErrorExt(tif
->tif_clientdata
, module,
2613 "%s: Seek error accessing TIFF directory",
2618 read_offset
= tif
->tif_diroff
;
2620 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2622 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) {
2623 TIFFErrorExt(tif
->tif_clientdata
, module,
2624 "%s: Can not read TIFF directory count",
2628 if (tif
->tif_flags
& TIFF_SWAB
)
2629 TIFFSwabShort(&dircount
);
2634 if (!ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
2635 TIFFErrorExt(tif
->tif_clientdata
, module,
2636 "%s: Can not read TIFF directory count",
2640 if (tif
->tif_flags
& TIFF_SWAB
)
2641 TIFFSwabLong8(&dircount64
);
2642 dircount
= (uint16
)dircount64
;
2647 /* -------------------------------------------------------------------- */
2648 /* Read through directory to find target tag. */
2649 /* -------------------------------------------------------------------- */
2650 while( dircount
> 0 )
2652 if (!ReadOK(tif
, direntry_raw
, dirsize
)) {
2653 TIFFErrorExt(tif
->tif_clientdata
, module,
2654 "%s: Can not read TIFF directory entry.",
2659 memcpy( &entry_tag
, direntry_raw
+ 0, sizeof(uint16
) );
2660 if (tif
->tif_flags
&TIFF_SWAB
)
2661 TIFFSwabShort( &entry_tag
);
2663 if( entry_tag
== tag
)
2666 read_offset
+= dirsize
;
2669 if( entry_tag
!= tag
)
2671 TIFFErrorExt(tif
->tif_clientdata
, module,
2672 "%s: Could not find tag %d.",
2673 tif
->tif_name
, tag
);
2677 /* -------------------------------------------------------------------- */
2678 /* Extract the type, count and offset for this entry. */
2679 /* -------------------------------------------------------------------- */
2680 memcpy( &entry_type
, direntry_raw
+ 2, sizeof(uint16
) );
2681 if (tif
->tif_flags
&TIFF_SWAB
)
2682 TIFFSwabShort( &entry_type
);
2684 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2688 memcpy( &value
, direntry_raw
+ 4, sizeof(uint32
) );
2689 if (tif
->tif_flags
&TIFF_SWAB
)
2690 TIFFSwabLong( &value
);
2691 entry_count
= value
;
2693 memcpy( &value
, direntry_raw
+ 8, sizeof(uint32
) );
2694 if (tif
->tif_flags
&TIFF_SWAB
)
2695 TIFFSwabLong( &value
);
2696 entry_offset
= value
;
2700 memcpy( &entry_count
, direntry_raw
+ 4, sizeof(uint64
) );
2701 if (tif
->tif_flags
&TIFF_SWAB
)
2702 TIFFSwabLong8( &entry_count
);
2704 memcpy( &entry_offset
, direntry_raw
+ 12, sizeof(uint64
) );
2705 if (tif
->tif_flags
&TIFF_SWAB
)
2706 TIFFSwabLong8( &entry_offset
);
2709 /* -------------------------------------------------------------------- */
2710 /* What data type do we want to write this as? */
2711 /* -------------------------------------------------------------------- */
2712 if( TIFFDataWidth(in_datatype
) == 8 && !(tif
->tif_flags
&TIFF_BIGTIFF
) )
2714 if( in_datatype
== TIFF_LONG8
)
2715 datatype
= TIFF_LONG
;
2716 else if( in_datatype
== TIFF_SLONG8
)
2717 datatype
= TIFF_SLONG
;
2718 else if( in_datatype
== TIFF_IFD8
)
2719 datatype
= TIFF_IFD
;
2721 datatype
= in_datatype
;
2724 datatype
= in_datatype
;
2726 /* -------------------------------------------------------------------- */
2727 /* Prepare buffer of actual data to write. This includes */
2728 /* swabbing as needed. */
2729 /* -------------------------------------------------------------------- */
2731 (uint8
*)_TIFFCheckMalloc(tif
, count
, TIFFDataWidth(datatype
),
2732 "for field buffer.");
2736 if( datatype
== in_datatype
)
2737 memcpy( buf_to_write
, data
, count
* TIFFDataWidth(datatype
) );
2738 else if( datatype
== TIFF_SLONG
&& in_datatype
== TIFF_SLONG8
)
2742 for( i
= 0; i
< count
; i
++ )
2744 ((int32
*) buf_to_write
)[i
] =
2745 (int32
) ((int64
*) data
)[i
];
2746 if( (int64
) ((int32
*) buf_to_write
)[i
] != ((int64
*) data
)[i
] )
2748 _TIFFfree( buf_to_write
);
2749 TIFFErrorExt( tif
->tif_clientdata
, module,
2750 "Value exceeds 32bit range of output type." );
2755 else if( (datatype
== TIFF_LONG
&& in_datatype
== TIFF_LONG8
)
2756 || (datatype
== TIFF_IFD
&& in_datatype
== TIFF_IFD8
) )
2760 for( i
= 0; i
< count
; i
++ )
2762 ((uint32
*) buf_to_write
)[i
] =
2763 (uint32
) ((uint64
*) data
)[i
];
2764 if( (uint64
) ((uint32
*) buf_to_write
)[i
] != ((uint64
*) data
)[i
] )
2766 _TIFFfree( buf_to_write
);
2767 TIFFErrorExt( tif
->tif_clientdata
, module,
2768 "Value exceeds 32bit range of output type." );
2774 if( TIFFDataWidth(datatype
) > 1 && (tif
->tif_flags
&TIFF_SWAB
) )
2776 if( TIFFDataWidth(datatype
) == 2 )
2777 TIFFSwabArrayOfShort( (uint16
*) buf_to_write
, count
);
2778 else if( TIFFDataWidth(datatype
) == 4 )
2779 TIFFSwabArrayOfLong( (uint32
*) buf_to_write
, count
);
2780 else if( TIFFDataWidth(datatype
) == 8 )
2781 TIFFSwabArrayOfLong8( (uint64
*) buf_to_write
, count
);
2784 /* -------------------------------------------------------------------- */
2785 /* Is this a value that fits into the directory entry? */
2786 /* -------------------------------------------------------------------- */
2787 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2789 if( TIFFDataWidth(datatype
) * count
<= 4 )
2791 entry_offset
= read_offset
+ 8;
2797 if( TIFFDataWidth(datatype
) * count
<= 8 )
2799 entry_offset
= read_offset
+ 12;
2804 /* -------------------------------------------------------------------- */
2805 /* If the tag type, and count match, then we just write it out */
2806 /* over the old values without altering the directory entry at */
2808 /* -------------------------------------------------------------------- */
2809 if( entry_count
== (uint64
)count
&& entry_type
== (uint16
) datatype
)
2811 if (!SeekOK(tif
, entry_offset
)) {
2812 _TIFFfree( buf_to_write
);
2813 TIFFErrorExt(tif
->tif_clientdata
, module,
2814 "%s: Seek error accessing TIFF directory",
2818 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2819 _TIFFfree( buf_to_write
);
2820 TIFFErrorExt(tif
->tif_clientdata
, module,
2821 "Error writing directory link");
2825 _TIFFfree( buf_to_write
);
2829 /* -------------------------------------------------------------------- */
2830 /* Otherwise, we write the new tag data at the end of the file. */
2831 /* -------------------------------------------------------------------- */
2832 if( !value_in_entry
)
2834 entry_offset
= TIFFSeekFile(tif
,0,SEEK_END
);
2836 if (!WriteOK(tif
, buf_to_write
, count
*TIFFDataWidth(datatype
))) {
2837 _TIFFfree( buf_to_write
);
2838 TIFFErrorExt(tif
->tif_clientdata
, module,
2839 "Error writing directory link");
2843 _TIFFfree( buf_to_write
);
2847 memcpy( &entry_offset
, buf_to_write
, count
*TIFFDataWidth(datatype
));
2850 /* -------------------------------------------------------------------- */
2851 /* Adjust the directory entry. */
2852 /* -------------------------------------------------------------------- */
2853 entry_type
= datatype
;
2854 memcpy( direntry_raw
+ 2, &entry_type
, sizeof(uint16
) );
2855 if (tif
->tif_flags
&TIFF_SWAB
)
2856 TIFFSwabShort( (uint16
*) (direntry_raw
+ 2) );
2858 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
2862 value
= (uint32
) entry_count
;
2863 memcpy( direntry_raw
+ 4, &value
, sizeof(uint32
) );
2864 if (tif
->tif_flags
&TIFF_SWAB
)
2865 TIFFSwabLong( (uint32
*) (direntry_raw
+ 4) );
2867 value
= (uint32
) entry_offset
;
2868 memcpy( direntry_raw
+ 8, &value
, sizeof(uint32
) );
2869 if (tif
->tif_flags
&TIFF_SWAB
)
2870 TIFFSwabLong( (uint32
*) (direntry_raw
+ 8) );
2874 memcpy( direntry_raw
+ 4, &entry_count
, sizeof(uint64
) );
2875 if (tif
->tif_flags
&TIFF_SWAB
)
2876 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 4) );
2878 memcpy( direntry_raw
+ 12, &entry_offset
, sizeof(uint64
) );
2879 if (tif
->tif_flags
&TIFF_SWAB
)
2880 TIFFSwabLong8( (uint64
*) (direntry_raw
+ 12) );
2883 /* -------------------------------------------------------------------- */
2884 /* Write the directory entry out to disk. */
2885 /* -------------------------------------------------------------------- */
2886 if (!SeekOK(tif
, read_offset
)) {
2887 TIFFErrorExt(tif
->tif_clientdata
, module,
2888 "%s: Seek error accessing TIFF directory",
2893 if (!WriteOK(tif
, direntry_raw
,dirsize
))
2895 TIFFErrorExt(tif
->tif_clientdata
, module,
2896 "%s: Can not write TIFF directory entry.",
2903 /* vim: set ts=8 sts=8 sw=8 noet: */