Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/3rdparty/libtiff/tif_dirwrite.c
16337 views
1
/* $Id: tif_dirwrite.c,v 1.89 2017-08-23 13:33:42 erouault Exp $ */
2
3
/*
4
* Copyright (c) 1988-1997 Sam Leffler
5
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
6
*
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.
14
*
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.
18
*
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
24
* OF THIS SOFTWARE.
25
*/
26
27
/*
28
* TIFF Library.
29
*
30
* Directory Write Support Routines.
31
*/
32
#include "tiffiop.h"
33
#include <float.h>
34
35
#ifdef HAVE_IEEEFP
36
#define TIFFCvtNativeToIEEEFloat(tif, n, fp)
37
#define TIFFCvtNativeToIEEEDouble(tif, n, dp)
38
#else
39
extern void TIFFCvtNativeToIEEEFloat(TIFF* tif, uint32 n, float* fp);
40
extern void TIFFCvtNativeToIEEEDouble(TIFF* tif, uint32 n, double* dp);
41
#endif
42
43
static int TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff);
44
45
static int TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
46
#if 0
47
static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
48
#endif
49
50
static int TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
51
static int TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
52
#ifdef notdef
53
static int TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
54
#endif
55
static int TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
56
#if 0
57
static int TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
58
#endif
59
#ifdef notdef
60
static int TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
61
#endif
62
static int TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
63
#if 0
64
static int TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
65
#endif
66
static int TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
67
static int TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
68
static int TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
69
#ifdef notdef
70
static int TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
71
#endif
72
static int TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
73
#if 0
74
static int TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
75
#endif
76
static int TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
77
static int TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
78
#if 0
79
static int TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
80
#endif
81
#ifdef notdef
82
static int TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
83
#endif
84
static int TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
85
#if 0
86
static int TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
87
#endif
88
#ifdef notdef
89
static int TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
90
#endif
91
static int TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
92
#ifdef notdef
93
static int TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
94
#endif
95
static int TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
96
static int TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
97
static int TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
98
static int TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
99
#ifdef notdef
100
static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
101
#endif
102
static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
103
#if 0
104
static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
105
#endif
106
#ifdef notdef
107
static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
108
#endif
109
static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
110
#if 0
111
static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
112
#endif
113
static int TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
114
#ifdef notdef
115
static int TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
116
#endif
117
static int TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
118
static int TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
119
static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
120
#ifdef notdef
121
static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
122
#endif
123
static int TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
124
static int TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
125
static int TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
126
127
static int TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
128
static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
129
#ifdef notdef
130
static int TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
131
#endif
132
static int TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
133
#ifdef notdef
134
static int TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
135
#endif
136
static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
137
static int TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
138
static int TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
139
#ifdef notdef
140
static int TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
141
#endif
142
static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
143
static int TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
144
static int TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
145
#ifdef notdef
146
static int TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
147
#endif
148
static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
149
#ifdef notdef
150
static int TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
151
#endif
152
static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
153
#ifdef notdef
154
static int TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
155
#endif
156
static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
157
static int TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
158
static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
159
static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
160
#ifdef notdef
161
static int TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
162
#endif
163
static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
164
#ifdef notdef
165
static int TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
166
#endif
167
static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
168
static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
169
static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
170
171
static int TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data);
172
173
static int TIFFLinkDirectory(TIFF*);
174
175
/*
176
* Write the contents of the current directory
177
* to the specified file. This routine doesn't
178
* handle overwriting a directory with auxiliary
179
* storage that's been changed.
180
*/
181
int
182
TIFFWriteDirectory(TIFF* tif)
183
{
184
return TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);
185
}
186
187
/*
188
* Similar to TIFFWriteDirectory(), writes the directory out
189
* but leaves all data structures in memory so that it can be
190
* written again. This will make a partially written TIFF file
191
* readable before it is successfully completed/closed.
192
*/
193
int
194
TIFFCheckpointDirectory(TIFF* tif)
195
{
196
int rc;
197
/* Setup the strips arrays, if they haven't already been. */
198
if (tif->tif_dir.td_stripoffset == NULL)
199
(void) TIFFSetupStrips(tif);
200
rc = TIFFWriteDirectorySec(tif,TRUE,FALSE,NULL);
201
(void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
202
return rc;
203
}
204
205
int
206
TIFFWriteCustomDirectory(TIFF* tif, uint64* pdiroff)
207
{
208
return TIFFWriteDirectorySec(tif,FALSE,FALSE,pdiroff);
209
}
210
211
/*
212
* Similar to TIFFWriteDirectory(), but if the directory has already
213
* been written once, it is relocated to the end of the file, in case it
214
* has changed in size. Note that this will result in the loss of the
215
* previously used directory space.
216
*/
217
int
218
TIFFRewriteDirectory( TIFF *tif )
219
{
220
static const char module[] = "TIFFRewriteDirectory";
221
222
/* We don't need to do anything special if it hasn't been written. */
223
if( tif->tif_diroff == 0 )
224
return TIFFWriteDirectory( tif );
225
226
/*
227
* Find and zero the pointer to this directory, so that TIFFLinkDirectory
228
* will cause it to be added after this directories current pre-link.
229
*/
230
231
if (!(tif->tif_flags&TIFF_BIGTIFF))
232
{
233
if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
234
{
235
tif->tif_header.classic.tiff_diroff = 0;
236
tif->tif_diroff = 0;
237
238
TIFFSeekFile(tif,4,SEEK_SET);
239
if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))
240
{
241
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
242
"Error updating TIFF header");
243
return (0);
244
}
245
}
246
else
247
{
248
uint32 nextdir;
249
nextdir = tif->tif_header.classic.tiff_diroff;
250
while(1) {
251
uint16 dircount;
252
uint32 nextnextdir;
253
254
if (!SeekOK(tif, nextdir) ||
255
!ReadOK(tif, &dircount, 2)) {
256
TIFFErrorExt(tif->tif_clientdata, module,
257
"Error fetching directory count");
258
return (0);
259
}
260
if (tif->tif_flags & TIFF_SWAB)
261
TIFFSwabShort(&dircount);
262
(void) TIFFSeekFile(tif,
263
nextdir+2+dircount*12, SEEK_SET);
264
if (!ReadOK(tif, &nextnextdir, 4)) {
265
TIFFErrorExt(tif->tif_clientdata, module,
266
"Error fetching directory link");
267
return (0);
268
}
269
if (tif->tif_flags & TIFF_SWAB)
270
TIFFSwabLong(&nextnextdir);
271
if (nextnextdir==tif->tif_diroff)
272
{
273
uint32 m;
274
m=0;
275
(void) TIFFSeekFile(tif,
276
nextdir+2+dircount*12, SEEK_SET);
277
if (!WriteOK(tif, &m, 4)) {
278
TIFFErrorExt(tif->tif_clientdata, module,
279
"Error writing directory link");
280
return (0);
281
}
282
tif->tif_diroff=0;
283
break;
284
}
285
nextdir=nextnextdir;
286
}
287
}
288
}
289
else
290
{
291
if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
292
{
293
tif->tif_header.big.tiff_diroff = 0;
294
tif->tif_diroff = 0;
295
296
TIFFSeekFile(tif,8,SEEK_SET);
297
if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))
298
{
299
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
300
"Error updating TIFF header");
301
return (0);
302
}
303
}
304
else
305
{
306
uint64 nextdir;
307
nextdir = tif->tif_header.big.tiff_diroff;
308
while(1) {
309
uint64 dircount64;
310
uint16 dircount;
311
uint64 nextnextdir;
312
313
if (!SeekOK(tif, nextdir) ||
314
!ReadOK(tif, &dircount64, 8)) {
315
TIFFErrorExt(tif->tif_clientdata, module,
316
"Error fetching directory count");
317
return (0);
318
}
319
if (tif->tif_flags & TIFF_SWAB)
320
TIFFSwabLong8(&dircount64);
321
if (dircount64>0xFFFF)
322
{
323
TIFFErrorExt(tif->tif_clientdata, module,
324
"Sanity check on tag count failed, likely corrupt TIFF");
325
return (0);
326
}
327
dircount=(uint16)dircount64;
328
(void) TIFFSeekFile(tif,
329
nextdir+8+dircount*20, SEEK_SET);
330
if (!ReadOK(tif, &nextnextdir, 8)) {
331
TIFFErrorExt(tif->tif_clientdata, module,
332
"Error fetching directory link");
333
return (0);
334
}
335
if (tif->tif_flags & TIFF_SWAB)
336
TIFFSwabLong8(&nextnextdir);
337
if (nextnextdir==tif->tif_diroff)
338
{
339
uint64 m;
340
m=0;
341
(void) TIFFSeekFile(tif,
342
nextdir+8+dircount*20, SEEK_SET);
343
if (!WriteOK(tif, &m, 8)) {
344
TIFFErrorExt(tif->tif_clientdata, module,
345
"Error writing directory link");
346
return (0);
347
}
348
tif->tif_diroff=0;
349
break;
350
}
351
nextdir=nextnextdir;
352
}
353
}
354
}
355
356
/*
357
* Now use TIFFWriteDirectory() normally.
358
*/
359
360
return TIFFWriteDirectory( tif );
361
}
362
363
static int
364
TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
365
{
366
static const char module[] = "TIFFWriteDirectorySec";
367
uint32 ndir;
368
TIFFDirEntry* dir;
369
uint32 dirsize;
370
void* dirmem;
371
uint32 m;
372
if (tif->tif_mode == O_RDONLY)
373
return (1);
374
375
_TIFFFillStriles( tif );
376
377
/*
378
* Clear write state so that subsequent images with
379
* different characteristics get the right buffers
380
* setup for them.
381
*/
382
if (imagedone)
383
{
384
if (tif->tif_flags & TIFF_POSTENCODE)
385
{
386
tif->tif_flags &= ~TIFF_POSTENCODE;
387
if (!(*tif->tif_postencode)(tif))
388
{
389
TIFFErrorExt(tif->tif_clientdata,module,
390
"Error post-encoding before directory write");
391
return (0);
392
}
393
}
394
(*tif->tif_close)(tif); /* shutdown encoder */
395
/*
396
* Flush any data that might have been written
397
* by the compression close+cleanup routines. But
398
* be careful not to write stuff if we didn't add data
399
* in the previous steps as the "rawcc" data may well be
400
* a previously read tile/strip in mixed read/write mode.
401
*/
402
if (tif->tif_rawcc > 0
403
&& (tif->tif_flags & TIFF_BEENWRITING) != 0 )
404
{
405
if( !TIFFFlushData1(tif) )
406
{
407
TIFFErrorExt(tif->tif_clientdata, module,
408
"Error flushing data before directory write");
409
return (0);
410
}
411
}
412
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
413
{
414
_TIFFfree(tif->tif_rawdata);
415
tif->tif_rawdata = NULL;
416
tif->tif_rawcc = 0;
417
tif->tif_rawdatasize = 0;
418
tif->tif_rawdataoff = 0;
419
tif->tif_rawdataloaded = 0;
420
}
421
tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
422
}
423
dir=NULL;
424
dirmem=NULL;
425
dirsize=0;
426
while (1)
427
{
428
ndir=0;
429
if (isimage)
430
{
431
if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
432
{
433
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))
434
goto bad;
435
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))
436
goto bad;
437
}
438
if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))
439
{
440
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))
441
goto bad;
442
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))
443
goto bad;
444
}
445
if (TIFFFieldSet(tif,FIELD_RESOLUTION))
446
{
447
if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))
448
goto bad;
449
if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))
450
goto bad;
451
}
452
if (TIFFFieldSet(tif,FIELD_POSITION))
453
{
454
if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))
455
goto bad;
456
if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))
457
goto bad;
458
}
459
if (TIFFFieldSet(tif,FIELD_SUBFILETYPE))
460
{
461
if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))
462
goto bad;
463
}
464
if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
465
{
466
if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))
467
goto bad;
468
}
469
if (TIFFFieldSet(tif,FIELD_COMPRESSION))
470
{
471
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))
472
goto bad;
473
}
474
if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
475
{
476
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))
477
goto bad;
478
}
479
if (TIFFFieldSet(tif,FIELD_THRESHHOLDING))
480
{
481
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))
482
goto bad;
483
}
484
if (TIFFFieldSet(tif,FIELD_FILLORDER))
485
{
486
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))
487
goto bad;
488
}
489
if (TIFFFieldSet(tif,FIELD_ORIENTATION))
490
{
491
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))
492
goto bad;
493
}
494
if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
495
{
496
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))
497
goto bad;
498
}
499
if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))
500
{
501
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))
502
goto bad;
503
}
504
if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
505
{
506
if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))
507
goto bad;
508
}
509
if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
510
{
511
if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))
512
goto bad;
513
}
514
if (TIFFFieldSet(tif,FIELD_PLANARCONFIG))
515
{
516
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))
517
goto bad;
518
}
519
if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))
520
{
521
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))
522
goto bad;
523
}
524
if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
525
{
526
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))
527
goto bad;
528
}
529
if (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))
530
{
531
if (!isTiled(tif))
532
{
533
if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
534
goto bad;
535
}
536
else
537
{
538
if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
539
goto bad;
540
}
541
}
542
if (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))
543
{
544
if (!isTiled(tif))
545
{
546
/* td_stripoffset might be NULL in an odd OJPEG case. See
547
* tif_dirread.c around line 3634.
548
* XXX: OJPEG hack.
549
* If a) compression is OJPEG, b) it's not a tiled TIFF,
550
* and c) the number of strips is 1,
551
* then we tolerate the absence of stripoffsets tag,
552
* because, presumably, all required data is in the
553
* JpegInterchangeFormat stream.
554
* We can get here when using tiffset on such a file.
555
* See http://bugzilla.maptools.org/show_bug.cgi?id=2500
556
*/
557
if (tif->tif_dir.td_stripoffset != NULL &&
558
!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
559
goto bad;
560
}
561
else
562
{
563
if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
564
goto bad;
565
}
566
}
567
if (TIFFFieldSet(tif,FIELD_COLORMAP))
568
{
569
if (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))
570
goto bad;
571
}
572
if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))
573
{
574
if (tif->tif_dir.td_extrasamples)
575
{
576
uint16 na;
577
uint16* nb;
578
TIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);
579
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))
580
goto bad;
581
}
582
}
583
if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))
584
{
585
if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))
586
goto bad;
587
}
588
if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))
589
{
590
if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))
591
goto bad;
592
}
593
if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))
594
{
595
if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))
596
goto bad;
597
}
598
if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
599
{
600
if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))
601
goto bad;
602
}
603
if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
604
{
605
if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))
606
goto bad;
607
}
608
if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
609
{
610
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))
611
goto bad;
612
}
613
if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
614
{
615
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))
616
goto bad;
617
}
618
if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))
619
{
620
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))
621
goto bad;
622
}
623
if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
624
{
625
if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))
626
goto bad;
627
}
628
if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))
629
{
630
if (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))
631
goto bad;
632
}
633
if (TIFFFieldSet(tif,FIELD_INKNAMES))
634
{
635
if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))
636
goto bad;
637
}
638
if (TIFFFieldSet(tif,FIELD_SUBIFD))
639
{
640
if (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))
641
goto bad;
642
}
643
{
644
uint32 n;
645
for (n=0; n<tif->tif_nfields; n++) {
646
const TIFFField* o;
647
o = tif->tif_fields[n];
648
if ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))
649
{
650
switch (o->get_field_type)
651
{
652
case TIFF_SETGET_ASCII:
653
{
654
uint32 pa;
655
char* pb;
656
assert(o->field_type==TIFF_ASCII);
657
assert(o->field_readcount==TIFF_VARIABLE);
658
assert(o->field_passcount==0);
659
TIFFGetField(tif,o->field_tag,&pb);
660
pa=(uint32)(strlen(pb));
661
if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
662
goto bad;
663
}
664
break;
665
case TIFF_SETGET_UINT16:
666
{
667
uint16 p;
668
assert(o->field_type==TIFF_SHORT);
669
assert(o->field_readcount==1);
670
assert(o->field_passcount==0);
671
TIFFGetField(tif,o->field_tag,&p);
672
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,(uint16)o->field_tag,p))
673
goto bad;
674
}
675
break;
676
case TIFF_SETGET_UINT32:
677
{
678
uint32 p;
679
assert(o->field_type==TIFF_LONG);
680
assert(o->field_readcount==1);
681
assert(o->field_passcount==0);
682
TIFFGetField(tif,o->field_tag,&p);
683
if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,(uint16)o->field_tag,p))
684
goto bad;
685
}
686
break;
687
case TIFF_SETGET_C32_UINT8:
688
{
689
uint32 pa;
690
void* pb;
691
assert(o->field_type==TIFF_UNDEFINED);
692
assert(o->field_readcount==TIFF_VARIABLE2);
693
assert(o->field_passcount==1);
694
TIFFGetField(tif,o->field_tag,&pa,&pb);
695
if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
696
goto bad;
697
}
698
break;
699
default:
700
assert(0); /* we should never get here */
701
break;
702
}
703
}
704
}
705
}
706
}
707
for (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)
708
{
709
uint16 tag = (uint16)tif->tif_dir.td_customValues[m].info->field_tag;
710
uint32 count = tif->tif_dir.td_customValues[m].count;
711
switch (tif->tif_dir.td_customValues[m].info->field_type)
712
{
713
case TIFF_ASCII:
714
if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
715
goto bad;
716
break;
717
case TIFF_UNDEFINED:
718
if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
719
goto bad;
720
break;
721
case TIFF_BYTE:
722
if (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
723
goto bad;
724
break;
725
case TIFF_SBYTE:
726
if (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
727
goto bad;
728
break;
729
case TIFF_SHORT:
730
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
731
goto bad;
732
break;
733
case TIFF_SSHORT:
734
if (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
735
goto bad;
736
break;
737
case TIFF_LONG:
738
if (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
739
goto bad;
740
break;
741
case TIFF_SLONG:
742
if (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
743
goto bad;
744
break;
745
case TIFF_LONG8:
746
if (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
747
goto bad;
748
break;
749
case TIFF_SLONG8:
750
if (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
751
goto bad;
752
break;
753
case TIFF_RATIONAL:
754
if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
755
goto bad;
756
break;
757
case TIFF_SRATIONAL:
758
if (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
759
goto bad;
760
break;
761
case TIFF_FLOAT:
762
if (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
763
goto bad;
764
break;
765
case TIFF_DOUBLE:
766
if (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
767
goto bad;
768
break;
769
case TIFF_IFD:
770
if (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
771
goto bad;
772
break;
773
case TIFF_IFD8:
774
if (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
775
goto bad;
776
break;
777
default:
778
assert(0); /* we should never get here */
779
break;
780
}
781
}
782
if (dir!=NULL)
783
break;
784
dir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));
785
if (dir==NULL)
786
{
787
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
788
goto bad;
789
}
790
if (isimage)
791
{
792
if ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))
793
goto bad;
794
}
795
else
796
tif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~((toff_t)1));
797
if (pdiroff!=NULL)
798
*pdiroff=tif->tif_diroff;
799
if (!(tif->tif_flags&TIFF_BIGTIFF))
800
dirsize=2+ndir*12+4;
801
else
802
dirsize=8+ndir*20+8;
803
tif->tif_dataoff=tif->tif_diroff+dirsize;
804
if (!(tif->tif_flags&TIFF_BIGTIFF))
805
tif->tif_dataoff=(uint32)tif->tif_dataoff;
806
if ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))
807
{
808
TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
809
goto bad;
810
}
811
if (tif->tif_dataoff&1)
812
tif->tif_dataoff++;
813
if (isimage)
814
tif->tif_curdir++;
815
}
816
if (isimage)
817
{
818
if (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))
819
{
820
uint32 na;
821
TIFFDirEntry* nb;
822
for (na=0, nb=dir; ; na++, nb++)
823
{
824
if( na == ndir )
825
{
826
TIFFErrorExt(tif->tif_clientdata,module,
827
"Cannot find SubIFD tag");
828
goto bad;
829
}
830
if (nb->tdir_tag==TIFFTAG_SUBIFD)
831
break;
832
}
833
if (!(tif->tif_flags&TIFF_BIGTIFF))
834
tif->tif_subifdoff=tif->tif_diroff+2+na*12+8;
835
else
836
tif->tif_subifdoff=tif->tif_diroff+8+na*20+12;
837
}
838
}
839
dirmem=_TIFFmalloc(dirsize);
840
if (dirmem==NULL)
841
{
842
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
843
goto bad;
844
}
845
if (!(tif->tif_flags&TIFF_BIGTIFF))
846
{
847
uint8* n;
848
uint32 nTmp;
849
TIFFDirEntry* o;
850
n=dirmem;
851
*(uint16*)n=(uint16)ndir;
852
if (tif->tif_flags&TIFF_SWAB)
853
TIFFSwabShort((uint16*)n);
854
n+=2;
855
o=dir;
856
for (m=0; m<ndir; m++)
857
{
858
*(uint16*)n=o->tdir_tag;
859
if (tif->tif_flags&TIFF_SWAB)
860
TIFFSwabShort((uint16*)n);
861
n+=2;
862
*(uint16*)n=o->tdir_type;
863
if (tif->tif_flags&TIFF_SWAB)
864
TIFFSwabShort((uint16*)n);
865
n+=2;
866
nTmp = (uint32)o->tdir_count;
867
_TIFFmemcpy(n,&nTmp,4);
868
if (tif->tif_flags&TIFF_SWAB)
869
TIFFSwabLong((uint32*)n);
870
n+=4;
871
/* This is correct. The data has been */
872
/* swabbed previously in TIFFWriteDirectoryTagData */
873
_TIFFmemcpy(n,&o->tdir_offset,4);
874
n+=4;
875
o++;
876
}
877
nTmp = (uint32)tif->tif_nextdiroff;
878
if (tif->tif_flags&TIFF_SWAB)
879
TIFFSwabLong(&nTmp);
880
_TIFFmemcpy(n,&nTmp,4);
881
}
882
else
883
{
884
uint8* n;
885
TIFFDirEntry* o;
886
n=dirmem;
887
*(uint64*)n=ndir;
888
if (tif->tif_flags&TIFF_SWAB)
889
TIFFSwabLong8((uint64*)n);
890
n+=8;
891
o=dir;
892
for (m=0; m<ndir; m++)
893
{
894
*(uint16*)n=o->tdir_tag;
895
if (tif->tif_flags&TIFF_SWAB)
896
TIFFSwabShort((uint16*)n);
897
n+=2;
898
*(uint16*)n=o->tdir_type;
899
if (tif->tif_flags&TIFF_SWAB)
900
TIFFSwabShort((uint16*)n);
901
n+=2;
902
_TIFFmemcpy(n,&o->tdir_count,8);
903
if (tif->tif_flags&TIFF_SWAB)
904
TIFFSwabLong8((uint64*)n);
905
n+=8;
906
_TIFFmemcpy(n,&o->tdir_offset,8);
907
n+=8;
908
o++;
909
}
910
_TIFFmemcpy(n,&tif->tif_nextdiroff,8);
911
if (tif->tif_flags&TIFF_SWAB)
912
TIFFSwabLong8((uint64*)n);
913
}
914
_TIFFfree(dir);
915
dir=NULL;
916
if (!SeekOK(tif,tif->tif_diroff))
917
{
918
TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
919
goto bad;
920
}
921
if (!WriteOK(tif,dirmem,(tmsize_t)dirsize))
922
{
923
TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
924
goto bad;
925
}
926
_TIFFfree(dirmem);
927
if (imagedone)
928
{
929
TIFFFreeDirectory(tif);
930
tif->tif_flags &= ~TIFF_DIRTYDIRECT;
931
tif->tif_flags &= ~TIFF_DIRTYSTRIP;
932
(*tif->tif_cleanup)(tif);
933
/*
934
* Reset directory-related state for subsequent
935
* directories.
936
*/
937
TIFFCreateDirectory(tif);
938
}
939
return(1);
940
bad:
941
if (dir!=NULL)
942
_TIFFfree(dir);
943
if (dirmem!=NULL)
944
_TIFFfree(dirmem);
945
return(0);
946
}
947
948
static float TIFFClampDoubleToFloat( double val )
949
{
950
if( val > FLT_MAX )
951
return FLT_MAX;
952
if( val < -FLT_MAX )
953
return -FLT_MAX;
954
return (float)val;
955
}
956
957
static int8 TIFFClampDoubleToInt8( double val )
958
{
959
if( val > 127 )
960
return 127;
961
if( val < -128 || val != val )
962
return -128;
963
return (int8)val;
964
}
965
966
static int16 TIFFClampDoubleToInt16( double val )
967
{
968
if( val > 32767 )
969
return 32767;
970
if( val < -32768 || val != val )
971
return -32768;
972
return (int16)val;
973
}
974
975
static int32 TIFFClampDoubleToInt32( double val )
976
{
977
if( val > 0x7FFFFFFF )
978
return 0x7FFFFFFF;
979
if( val < -0x7FFFFFFF-1 || val != val )
980
return -0x7FFFFFFF-1;
981
return (int32)val;
982
}
983
984
static uint8 TIFFClampDoubleToUInt8( double val )
985
{
986
if( val < 0 )
987
return 0;
988
if( val > 255 || val != val )
989
return 255;
990
return (uint8)val;
991
}
992
993
static uint16 TIFFClampDoubleToUInt16( double val )
994
{
995
if( val < 0 )
996
return 0;
997
if( val > 65535 || val != val )
998
return 65535;
999
return (uint16)val;
1000
}
1001
1002
static uint32 TIFFClampDoubleToUInt32( double val )
1003
{
1004
if( val < 0 )
1005
return 0;
1006
if( val > 0xFFFFFFFFU || val != val )
1007
return 0xFFFFFFFFU;
1008
return (uint32)val;
1009
}
1010
1011
static int
1012
TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
1013
{
1014
static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
1015
void* conv;
1016
uint32 i;
1017
int ok;
1018
conv = _TIFFmalloc(count*sizeof(double));
1019
if (conv == NULL)
1020
{
1021
TIFFErrorExt(tif->tif_clientdata, module, "Out of memory");
1022
return (0);
1023
}
1024
1025
switch (tif->tif_dir.td_sampleformat)
1026
{
1027
case SAMPLEFORMAT_IEEEFP:
1028
if (tif->tif_dir.td_bitspersample<=32)
1029
{
1030
for (i = 0; i < count; ++i)
1031
((float*)conv)[i] = TIFFClampDoubleToFloat(value[i]);
1032
ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
1033
}
1034
else
1035
{
1036
ok = TIFFWriteDirectoryTagDoubleArray(tif,ndir,dir,tag,count,value);
1037
}
1038
break;
1039
case SAMPLEFORMAT_INT:
1040
if (tif->tif_dir.td_bitspersample<=8)
1041
{
1042
for (i = 0; i < count; ++i)
1043
((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]);
1044
ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);
1045
}
1046
else if (tif->tif_dir.td_bitspersample<=16)
1047
{
1048
for (i = 0; i < count; ++i)
1049
((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]);
1050
ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);
1051
}
1052
else
1053
{
1054
for (i = 0; i < count; ++i)
1055
((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
1056
ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
1057
}
1058
break;
1059
case SAMPLEFORMAT_UINT:
1060
if (tif->tif_dir.td_bitspersample<=8)
1061
{
1062
for (i = 0; i < count; ++i)
1063
((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
1064
ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);
1065
}
1066
else if (tif->tif_dir.td_bitspersample<=16)
1067
{
1068
for (i = 0; i < count; ++i)
1069
((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
1070
ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);
1071
}
1072
else
1073
{
1074
for (i = 0; i < count; ++i)
1075
((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
1076
ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);
1077
}
1078
break;
1079
default:
1080
ok = 0;
1081
}
1082
1083
_TIFFfree(conv);
1084
return (ok);
1085
}
1086
1087
#if 0
1088
static int
1089
TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1090
{
1091
switch (tif->tif_dir.td_sampleformat)
1092
{
1093
case SAMPLEFORMAT_IEEEFP:
1094
if (tif->tif_dir.td_bitspersample<=32)
1095
return(TIFFWriteDirectoryTagFloatPerSample(tif,ndir,dir,tag,(float)value));
1096
else
1097
return(TIFFWriteDirectoryTagDoublePerSample(tif,ndir,dir,tag,value));
1098
case SAMPLEFORMAT_INT:
1099
if (tif->tif_dir.td_bitspersample<=8)
1100
return(TIFFWriteDirectoryTagSbytePerSample(tif,ndir,dir,tag,(int8)value));
1101
else if (tif->tif_dir.td_bitspersample<=16)
1102
return(TIFFWriteDirectoryTagSshortPerSample(tif,ndir,dir,tag,(int16)value));
1103
else
1104
return(TIFFWriteDirectoryTagSlongPerSample(tif,ndir,dir,tag,(int32)value));
1105
case SAMPLEFORMAT_UINT:
1106
if (tif->tif_dir.td_bitspersample<=8)
1107
return(TIFFWriteDirectoryTagBytePerSample(tif,ndir,dir,tag,(uint8)value));
1108
else if (tif->tif_dir.td_bitspersample<=16)
1109
return(TIFFWriteDirectoryTagShortPerSample(tif,ndir,dir,tag,(uint16)value));
1110
else
1111
return(TIFFWriteDirectoryTagLongPerSample(tif,ndir,dir,tag,(uint32)value));
1112
default:
1113
return(1);
1114
}
1115
}
1116
#endif
1117
1118
static int
1119
TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
1120
{
1121
if (dir==NULL)
1122
{
1123
(*ndir)++;
1124
return(1);
1125
}
1126
return(TIFFWriteDirectoryTagCheckedAscii(tif,ndir,dir,tag,count,value));
1127
}
1128
1129
static int
1130
TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
1131
{
1132
if (dir==NULL)
1133
{
1134
(*ndir)++;
1135
return(1);
1136
}
1137
return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif,ndir,dir,tag,count,value));
1138
}
1139
1140
#ifdef notdef
1141
static int
1142
TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
1143
{
1144
if (dir==NULL)
1145
{
1146
(*ndir)++;
1147
return(1);
1148
}
1149
return(TIFFWriteDirectoryTagCheckedByte(tif,ndir,dir,tag,value));
1150
}
1151
#endif
1152
1153
static int
1154
TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
1155
{
1156
if (dir==NULL)
1157
{
1158
(*ndir)++;
1159
return(1);
1160
}
1161
return(TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,count,value));
1162
}
1163
1164
#if 0
1165
static int
1166
TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
1167
{
1168
static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
1169
uint8* m;
1170
uint8* na;
1171
uint16 nb;
1172
int o;
1173
if (dir==NULL)
1174
{
1175
(*ndir)++;
1176
return(1);
1177
}
1178
m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint8));
1179
if (m==NULL)
1180
{
1181
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1182
return(0);
1183
}
1184
for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1185
*na=value;
1186
o=TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1187
_TIFFfree(m);
1188
return(o);
1189
}
1190
#endif
1191
1192
#ifdef notdef
1193
static int
1194
TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
1195
{
1196
if (dir==NULL)
1197
{
1198
(*ndir)++;
1199
return(1);
1200
}
1201
return(TIFFWriteDirectoryTagCheckedSbyte(tif,ndir,dir,tag,value));
1202
}
1203
#endif
1204
1205
static int
1206
TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
1207
{
1208
if (dir==NULL)
1209
{
1210
(*ndir)++;
1211
return(1);
1212
}
1213
return(TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,count,value));
1214
}
1215
1216
#if 0
1217
static int
1218
TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
1219
{
1220
static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
1221
int8* m;
1222
int8* na;
1223
uint16 nb;
1224
int o;
1225
if (dir==NULL)
1226
{
1227
(*ndir)++;
1228
return(1);
1229
}
1230
m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int8));
1231
if (m==NULL)
1232
{
1233
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1234
return(0);
1235
}
1236
for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1237
*na=value;
1238
o=TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1239
_TIFFfree(m);
1240
return(o);
1241
}
1242
#endif
1243
1244
static int
1245
TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
1246
{
1247
if (dir==NULL)
1248
{
1249
(*ndir)++;
1250
return(1);
1251
}
1252
return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,value));
1253
}
1254
1255
static int
1256
TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
1257
{
1258
if (dir==NULL)
1259
{
1260
(*ndir)++;
1261
return(1);
1262
}
1263
return(TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,value));
1264
}
1265
1266
static int
1267
TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
1268
{
1269
static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
1270
uint16* m;
1271
uint16* na;
1272
uint16 nb;
1273
int o;
1274
if (dir==NULL)
1275
{
1276
(*ndir)++;
1277
return(1);
1278
}
1279
m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16));
1280
if (m==NULL)
1281
{
1282
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1283
return(0);
1284
}
1285
for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1286
*na=value;
1287
o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1288
_TIFFfree(m);
1289
return(o);
1290
}
1291
1292
#ifdef notdef
1293
static int
1294
TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
1295
{
1296
if (dir==NULL)
1297
{
1298
(*ndir)++;
1299
return(1);
1300
}
1301
return(TIFFWriteDirectoryTagCheckedSshort(tif,ndir,dir,tag,value));
1302
}
1303
#endif
1304
1305
static int
1306
TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
1307
{
1308
if (dir==NULL)
1309
{
1310
(*ndir)++;
1311
return(1);
1312
}
1313
return(TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,count,value));
1314
}
1315
1316
#if 0
1317
static int
1318
TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
1319
{
1320
static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
1321
int16* m;
1322
int16* na;
1323
uint16 nb;
1324
int o;
1325
if (dir==NULL)
1326
{
1327
(*ndir)++;
1328
return(1);
1329
}
1330
m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int16));
1331
if (m==NULL)
1332
{
1333
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1334
return(0);
1335
}
1336
for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1337
*na=value;
1338
o=TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1339
_TIFFfree(m);
1340
return(o);
1341
}
1342
#endif
1343
1344
static int
1345
TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
1346
{
1347
if (dir==NULL)
1348
{
1349
(*ndir)++;
1350
return(1);
1351
}
1352
return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
1353
}
1354
1355
static int
1356
TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
1357
{
1358
if (dir==NULL)
1359
{
1360
(*ndir)++;
1361
return(1);
1362
}
1363
return(TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,value));
1364
}
1365
1366
#if 0
1367
static int
1368
TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
1369
{
1370
static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
1371
uint32* m;
1372
uint32* na;
1373
uint16 nb;
1374
int o;
1375
if (dir==NULL)
1376
{
1377
(*ndir)++;
1378
return(1);
1379
}
1380
m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint32));
1381
if (m==NULL)
1382
{
1383
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1384
return(0);
1385
}
1386
for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1387
*na=value;
1388
o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1389
_TIFFfree(m);
1390
return(o);
1391
}
1392
#endif
1393
1394
#ifdef notdef
1395
static int
1396
TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
1397
{
1398
if (dir==NULL)
1399
{
1400
(*ndir)++;
1401
return(1);
1402
}
1403
return(TIFFWriteDirectoryTagCheckedSlong(tif,ndir,dir,tag,value));
1404
}
1405
#endif
1406
1407
static int
1408
TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
1409
{
1410
if (dir==NULL)
1411
{
1412
(*ndir)++;
1413
return(1);
1414
}
1415
return(TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,count,value));
1416
}
1417
1418
#if 0
1419
static int
1420
TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
1421
{
1422
static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
1423
int32* m;
1424
int32* na;
1425
uint16 nb;
1426
int o;
1427
if (dir==NULL)
1428
{
1429
(*ndir)++;
1430
return(1);
1431
}
1432
m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int32));
1433
if (m==NULL)
1434
{
1435
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1436
return(0);
1437
}
1438
for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1439
*na=value;
1440
o=TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1441
_TIFFfree(m);
1442
return(o);
1443
}
1444
#endif
1445
1446
#ifdef notdef
1447
static int
1448
TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
1449
{
1450
if (dir==NULL)
1451
{
1452
(*ndir)++;
1453
return(1);
1454
}
1455
return(TIFFWriteDirectoryTagCheckedLong8(tif,ndir,dir,tag,value));
1456
}
1457
#endif
1458
1459
static int
1460
TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1461
{
1462
if (dir==NULL)
1463
{
1464
(*ndir)++;
1465
return(1);
1466
}
1467
return(TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value));
1468
}
1469
1470
#ifdef notdef
1471
static int
1472
TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
1473
{
1474
if (dir==NULL)
1475
{
1476
(*ndir)++;
1477
return(1);
1478
}
1479
return(TIFFWriteDirectoryTagCheckedSlong8(tif,ndir,dir,tag,value));
1480
}
1481
#endif
1482
1483
static int
1484
TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
1485
{
1486
if (dir==NULL)
1487
{
1488
(*ndir)++;
1489
return(1);
1490
}
1491
return(TIFFWriteDirectoryTagCheckedSlong8Array(tif,ndir,dir,tag,count,value));
1492
}
1493
1494
static int
1495
TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1496
{
1497
if (dir==NULL)
1498
{
1499
(*ndir)++;
1500
return(1);
1501
}
1502
return(TIFFWriteDirectoryTagCheckedRational(tif,ndir,dir,tag,value));
1503
}
1504
1505
static int
1506
TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
1507
{
1508
if (dir==NULL)
1509
{
1510
(*ndir)++;
1511
return(1);
1512
}
1513
return(TIFFWriteDirectoryTagCheckedRationalArray(tif,ndir,dir,tag,count,value));
1514
}
1515
1516
static int
1517
TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
1518
{
1519
if (dir==NULL)
1520
{
1521
(*ndir)++;
1522
return(1);
1523
}
1524
return(TIFFWriteDirectoryTagCheckedSrationalArray(tif,ndir,dir,tag,count,value));
1525
}
1526
1527
#ifdef notdef
1528
static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
1529
{
1530
if (dir==NULL)
1531
{
1532
(*ndir)++;
1533
return(1);
1534
}
1535
return(TIFFWriteDirectoryTagCheckedFloat(tif,ndir,dir,tag,value));
1536
}
1537
#endif
1538
1539
static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
1540
{
1541
if (dir==NULL)
1542
{
1543
(*ndir)++;
1544
return(1);
1545
}
1546
return(TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,count,value));
1547
}
1548
1549
#if 0
1550
static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
1551
{
1552
static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
1553
float* m;
1554
float* na;
1555
uint16 nb;
1556
int o;
1557
if (dir==NULL)
1558
{
1559
(*ndir)++;
1560
return(1);
1561
}
1562
m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(float));
1563
if (m==NULL)
1564
{
1565
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1566
return(0);
1567
}
1568
for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1569
*na=value;
1570
o=TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1571
_TIFFfree(m);
1572
return(o);
1573
}
1574
#endif
1575
1576
#ifdef notdef
1577
static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1578
{
1579
if (dir==NULL)
1580
{
1581
(*ndir)++;
1582
return(1);
1583
}
1584
return(TIFFWriteDirectoryTagCheckedDouble(tif,ndir,dir,tag,value));
1585
}
1586
#endif
1587
1588
static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
1589
{
1590
if (dir==NULL)
1591
{
1592
(*ndir)++;
1593
return(1);
1594
}
1595
return(TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,count,value));
1596
}
1597
1598
#if 0
1599
static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1600
{
1601
static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
1602
double* m;
1603
double* na;
1604
uint16 nb;
1605
int o;
1606
if (dir==NULL)
1607
{
1608
(*ndir)++;
1609
return(1);
1610
}
1611
m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(double));
1612
if (m==NULL)
1613
{
1614
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1615
return(0);
1616
}
1617
for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1618
*na=value;
1619
o=TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
1620
_TIFFfree(m);
1621
return(o);
1622
}
1623
#endif
1624
1625
static int
1626
TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
1627
{
1628
if (dir==NULL)
1629
{
1630
(*ndir)++;
1631
return(1);
1632
}
1633
return(TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,value));
1634
}
1635
1636
#ifdef notdef
1637
static int
1638
TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1639
{
1640
if (dir==NULL)
1641
{
1642
(*ndir)++;
1643
return(1);
1644
}
1645
return(TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,tag,count,value));
1646
}
1647
#endif
1648
1649
static int
1650
TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
1651
{
1652
if (dir==NULL)
1653
{
1654
(*ndir)++;
1655
return(1);
1656
}
1657
if (value<=0xFFFF)
1658
return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,(uint16)value));
1659
else
1660
return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
1661
}
1662
1663
/************************************************************************/
1664
/* TIFFWriteDirectoryTagLongLong8Array() */
1665
/* */
1666
/* Write out LONG8 array as LONG8 for BigTIFF or LONG for */
1667
/* Classic TIFF with some checking. */
1668
/************************************************************************/
1669
1670
static int
1671
TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1672
{
1673
static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
1674
uint64* ma;
1675
uint32 mb;
1676
uint32* p;
1677
uint32* q;
1678
int o;
1679
1680
/* is this just a counting pass? */
1681
if (dir==NULL)
1682
{
1683
(*ndir)++;
1684
return(1);
1685
}
1686
1687
/* We always write LONG8 for BigTIFF, no checking needed. */
1688
if( tif->tif_flags&TIFF_BIGTIFF )
1689
return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,
1690
tag,count,value);
1691
1692
/*
1693
** For classic tiff we want to verify everything is in range for LONG
1694
** and convert to long format.
1695
*/
1696
1697
p = _TIFFmalloc(count*sizeof(uint32));
1698
if (p==NULL)
1699
{
1700
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1701
return(0);
1702
}
1703
1704
for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
1705
{
1706
if (*ma>0xFFFFFFFF)
1707
{
1708
TIFFErrorExt(tif->tif_clientdata,module,
1709
"Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1710
_TIFFfree(p);
1711
return(0);
1712
}
1713
*q= (uint32)(*ma);
1714
}
1715
1716
o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
1717
_TIFFfree(p);
1718
1719
return(o);
1720
}
1721
1722
/************************************************************************/
1723
/* TIFFWriteDirectoryTagIfdIfd8Array() */
1724
/* */
1725
/* Write either IFD8 or IFD array depending on file type. */
1726
/************************************************************************/
1727
1728
static int
1729
TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1730
{
1731
static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1732
uint64* ma;
1733
uint32 mb;
1734
uint32* p;
1735
uint32* q;
1736
int o;
1737
1738
/* is this just a counting pass? */
1739
if (dir==NULL)
1740
{
1741
(*ndir)++;
1742
return(1);
1743
}
1744
1745
/* We always write IFD8 for BigTIFF, no checking needed. */
1746
if( tif->tif_flags&TIFF_BIGTIFF )
1747
return TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,
1748
tag,count,value);
1749
1750
/*
1751
** For classic tiff we want to verify everything is in range for IFD
1752
** and convert to long format.
1753
*/
1754
1755
p = _TIFFmalloc(count*sizeof(uint32));
1756
if (p==NULL)
1757
{
1758
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1759
return(0);
1760
}
1761
1762
for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
1763
{
1764
if (*ma>0xFFFFFFFF)
1765
{
1766
TIFFErrorExt(tif->tif_clientdata,module,
1767
"Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1768
_TIFFfree(p);
1769
return(0);
1770
}
1771
*q= (uint32)(*ma);
1772
}
1773
1774
o=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,p);
1775
_TIFFfree(p);
1776
1777
return(o);
1778
}
1779
1780
#ifdef notdef
1781
static int
1782
TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1783
{
1784
static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1785
uint64* ma;
1786
uint32 mb;
1787
uint8 n;
1788
int o;
1789
if (dir==NULL)
1790
{
1791
(*ndir)++;
1792
return(1);
1793
}
1794
n=0;
1795
for (ma=value, mb=0; mb<count; ma++, mb++)
1796
{
1797
if ((n==0)&&(*ma>0xFFFF))
1798
n=1;
1799
if ((n==1)&&(*ma>0xFFFFFFFF))
1800
{
1801
n=2;
1802
break;
1803
}
1804
}
1805
if (n==0)
1806
{
1807
uint16* p;
1808
uint16* q;
1809
p=_TIFFmalloc(count*sizeof(uint16));
1810
if (p==NULL)
1811
{
1812
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1813
return(0);
1814
}
1815
for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
1816
*q=(uint16)(*ma);
1817
o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);
1818
_TIFFfree(p);
1819
}
1820
else if (n==1)
1821
{
1822
uint32* p;
1823
uint32* q;
1824
p=_TIFFmalloc(count*sizeof(uint32));
1825
if (p==NULL)
1826
{
1827
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1828
return(0);
1829
}
1830
for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
1831
*q=(uint32)(*ma);
1832
o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
1833
_TIFFfree(p);
1834
}
1835
else
1836
{
1837
assert(n==2);
1838
o=TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value);
1839
}
1840
return(o);
1841
}
1842
#endif
1843
static int
1844
TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
1845
{
1846
static const char module[] = "TIFFWriteDirectoryTagColormap";
1847
uint32 m;
1848
uint16* n;
1849
int o;
1850
if (dir==NULL)
1851
{
1852
(*ndir)++;
1853
return(1);
1854
}
1855
m=(1<<tif->tif_dir.td_bitspersample);
1856
n=_TIFFmalloc(3*m*sizeof(uint16));
1857
if (n==NULL)
1858
{
1859
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1860
return(0);
1861
}
1862
_TIFFmemcpy(&n[0],tif->tif_dir.td_colormap[0],m*sizeof(uint16));
1863
_TIFFmemcpy(&n[m],tif->tif_dir.td_colormap[1],m*sizeof(uint16));
1864
_TIFFmemcpy(&n[2*m],tif->tif_dir.td_colormap[2],m*sizeof(uint16));
1865
o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_COLORMAP,3*m,n);
1866
_TIFFfree(n);
1867
return(o);
1868
}
1869
1870
static int
1871
TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
1872
{
1873
static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
1874
uint32 m;
1875
uint16 n;
1876
uint16* o;
1877
int p;
1878
if (dir==NULL)
1879
{
1880
(*ndir)++;
1881
return(1);
1882
}
1883
m=(1<<tif->tif_dir.td_bitspersample);
1884
n=tif->tif_dir.td_samplesperpixel-tif->tif_dir.td_extrasamples;
1885
/*
1886
* Check if the table can be written as a single column,
1887
* or if it must be written as 3 columns. Note that we
1888
* write a 3-column tag if there are 2 samples/pixel and
1889
* a single column of data won't suffice--hmm.
1890
*/
1891
if (n>3)
1892
n=3;
1893
if (n==3)
1894
{
1895
if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16)))
1896
n=2;
1897
}
1898
if (n==2)
1899
{
1900
if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16)))
1901
n=1;
1902
}
1903
if (n==0)
1904
n=1;
1905
o=_TIFFmalloc(n*m*sizeof(uint16));
1906
if (o==NULL)
1907
{
1908
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1909
return(0);
1910
}
1911
_TIFFmemcpy(&o[0],tif->tif_dir.td_transferfunction[0],m*sizeof(uint16));
1912
if (n>1)
1913
_TIFFmemcpy(&o[m],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16));
1914
if (n>2)
1915
_TIFFmemcpy(&o[2*m],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16));
1916
p=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_TRANSFERFUNCTION,n*m,o);
1917
_TIFFfree(o);
1918
return(p);
1919
}
1920
1921
static int
1922
TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
1923
{
1924
static const char module[] = "TIFFWriteDirectoryTagSubifd";
1925
uint64 m;
1926
int n;
1927
if (tif->tif_dir.td_nsubifd==0)
1928
return(1);
1929
if (dir==NULL)
1930
{
1931
(*ndir)++;
1932
return(1);
1933
}
1934
m=tif->tif_dataoff;
1935
if (!(tif->tif_flags&TIFF_BIGTIFF))
1936
{
1937
uint32* o;
1938
uint64* pa;
1939
uint32* pb;
1940
uint16 p;
1941
o=_TIFFmalloc(tif->tif_dir.td_nsubifd*sizeof(uint32));
1942
if (o==NULL)
1943
{
1944
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1945
return(0);
1946
}
1947
pa=tif->tif_dir.td_subifd;
1948
pb=o;
1949
for (p=0; p < tif->tif_dir.td_nsubifd; p++)
1950
{
1951
assert(pa != 0);
1952
1953
/* Could happen if an classicTIFF has a SubIFD of type LONG8 (which is illegal) */
1954
if( *pa > 0xFFFFFFFFUL)
1955
{
1956
TIFFErrorExt(tif->tif_clientdata,module,"Illegal value for SubIFD tag");
1957
_TIFFfree(o);
1958
return(0);
1959
}
1960
*pb++=(uint32)(*pa++);
1961
}
1962
n=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,o);
1963
_TIFFfree(o);
1964
}
1965
else
1966
n=TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,tif->tif_dir.td_subifd);
1967
if (!n)
1968
return(0);
1969
/*
1970
* Total hack: if this directory includes a SubIFD
1971
* tag then force the next <n> directories to be
1972
* written as ``sub directories'' of this one. This
1973
* is used to write things like thumbnails and
1974
* image masks that one wants to keep out of the
1975
* normal directory linkage access mechanism.
1976
*/
1977
tif->tif_flags|=TIFF_INSUBIFD;
1978
tif->tif_nsubifd=tif->tif_dir.td_nsubifd;
1979
if (tif->tif_dir.td_nsubifd==1)
1980
tif->tif_subifdoff=0;
1981
else
1982
tif->tif_subifdoff=m;
1983
return(1);
1984
}
1985
1986
static int
1987
TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
1988
{
1989
assert(sizeof(char)==1);
1990
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_ASCII,count,count,value));
1991
}
1992
1993
static int
1994
TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
1995
{
1996
assert(sizeof(uint8)==1);
1997
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_UNDEFINED,count,count,value));
1998
}
1999
2000
#ifdef notdef
2001
static int
2002
TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
2003
{
2004
assert(sizeof(uint8)==1);
2005
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,1,1,&value));
2006
}
2007
#endif
2008
2009
static int
2010
TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
2011
{
2012
assert(sizeof(uint8)==1);
2013
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,count,count,value));
2014
}
2015
2016
#ifdef notdef
2017
static int
2018
TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
2019
{
2020
assert(sizeof(int8)==1);
2021
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,1,1,&value));
2022
}
2023
#endif
2024
2025
static int
2026
TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
2027
{
2028
assert(sizeof(int8)==1);
2029
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,count,count,value));
2030
}
2031
2032
static int
2033
TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
2034
{
2035
uint16 m;
2036
assert(sizeof(uint16)==2);
2037
m=value;
2038
if (tif->tif_flags&TIFF_SWAB)
2039
TIFFSwabShort(&m);
2040
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,1,2,&m));
2041
}
2042
2043
static int
2044
TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
2045
{
2046
assert(count<0x80000000);
2047
assert(sizeof(uint16)==2);
2048
if (tif->tif_flags&TIFF_SWAB)
2049
TIFFSwabArrayOfShort(value,count);
2050
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,count,count*2,value));
2051
}
2052
2053
#ifdef notdef
2054
static int
2055
TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
2056
{
2057
int16 m;
2058
assert(sizeof(int16)==2);
2059
m=value;
2060
if (tif->tif_flags&TIFF_SWAB)
2061
TIFFSwabShort((uint16*)(&m));
2062
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,1,2,&m));
2063
}
2064
#endif
2065
2066
static int
2067
TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
2068
{
2069
assert(count<0x80000000);
2070
assert(sizeof(int16)==2);
2071
if (tif->tif_flags&TIFF_SWAB)
2072
TIFFSwabArrayOfShort((uint16*)value,count);
2073
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,count,count*2,value));
2074
}
2075
2076
static int
2077
TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
2078
{
2079
uint32 m;
2080
assert(sizeof(uint32)==4);
2081
m=value;
2082
if (tif->tif_flags&TIFF_SWAB)
2083
TIFFSwabLong(&m);
2084
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,1,4,&m));
2085
}
2086
2087
static int
2088
TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
2089
{
2090
assert(count<0x40000000);
2091
assert(sizeof(uint32)==4);
2092
if (tif->tif_flags&TIFF_SWAB)
2093
TIFFSwabArrayOfLong(value,count);
2094
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,count,count*4,value));
2095
}
2096
2097
#ifdef notdef
2098
static int
2099
TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
2100
{
2101
int32 m;
2102
assert(sizeof(int32)==4);
2103
m=value;
2104
if (tif->tif_flags&TIFF_SWAB)
2105
TIFFSwabLong((uint32*)(&m));
2106
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,1,4,&m));
2107
}
2108
#endif
2109
2110
static int
2111
TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
2112
{
2113
assert(count<0x40000000);
2114
assert(sizeof(int32)==4);
2115
if (tif->tif_flags&TIFF_SWAB)
2116
TIFFSwabArrayOfLong((uint32*)value,count);
2117
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,count,count*4,value));
2118
}
2119
2120
#ifdef notdef
2121
static int
2122
TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
2123
{
2124
uint64 m;
2125
assert(sizeof(uint64)==8);
2126
if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2127
TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8","LONG8 not allowed for ClassicTIFF");
2128
return(0);
2129
}
2130
m=value;
2131
if (tif->tif_flags&TIFF_SWAB)
2132
TIFFSwabLong8(&m);
2133
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,1,8,&m));
2134
}
2135
#endif
2136
2137
static int
2138
TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
2139
{
2140
assert(count<0x20000000);
2141
assert(sizeof(uint64)==8);
2142
if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2143
TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8Array","LONG8 not allowed for ClassicTIFF");
2144
return(0);
2145
}
2146
if (tif->tif_flags&TIFF_SWAB)
2147
TIFFSwabArrayOfLong8(value,count);
2148
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,count,count*8,value));
2149
}
2150
2151
#ifdef notdef
2152
static int
2153
TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
2154
{
2155
int64 m;
2156
assert(sizeof(int64)==8);
2157
if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2158
TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8","SLONG8 not allowed for ClassicTIFF");
2159
return(0);
2160
}
2161
m=value;
2162
if (tif->tif_flags&TIFF_SWAB)
2163
TIFFSwabLong8((uint64*)(&m));
2164
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,1,8,&m));
2165
}
2166
#endif
2167
2168
static int
2169
TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
2170
{
2171
assert(count<0x20000000);
2172
assert(sizeof(int64)==8);
2173
if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2174
TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8Array","SLONG8 not allowed for ClassicTIFF");
2175
return(0);
2176
}
2177
if (tif->tif_flags&TIFF_SWAB)
2178
TIFFSwabArrayOfLong8((uint64*)value,count);
2179
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,count,count*8,value));
2180
}
2181
2182
static int
2183
TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
2184
{
2185
static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
2186
uint32 m[2];
2187
assert(sizeof(uint32)==4);
2188
if( value < 0 )
2189
{
2190
TIFFErrorExt(tif->tif_clientdata,module,"Negative value is illegal");
2191
return 0;
2192
}
2193
else if( value != value )
2194
{
2195
TIFFErrorExt(tif->tif_clientdata,module,"Not-a-number value is illegal");
2196
return 0;
2197
}
2198
else if (value==0.0)
2199
{
2200
m[0]=0;
2201
m[1]=1;
2202
}
2203
else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value)
2204
{
2205
m[0]=(uint32)value;
2206
m[1]=1;
2207
}
2208
else if (value<1.0)
2209
{
2210
m[0]=(uint32)(value*0xFFFFFFFF);
2211
m[1]=0xFFFFFFFF;
2212
}
2213
else
2214
{
2215
m[0]=0xFFFFFFFF;
2216
m[1]=(uint32)(0xFFFFFFFF/value);
2217
}
2218
if (tif->tif_flags&TIFF_SWAB)
2219
{
2220
TIFFSwabLong(&m[0]);
2221
TIFFSwabLong(&m[1]);
2222
}
2223
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,1,8,&m[0]));
2224
}
2225
2226
static int
2227
TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
2228
{
2229
static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2230
uint32* m;
2231
float* na;
2232
uint32* nb;
2233
uint32 nc;
2234
int o;
2235
assert(sizeof(uint32)==4);
2236
m=_TIFFmalloc(count*2*sizeof(uint32));
2237
if (m==NULL)
2238
{
2239
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2240
return(0);
2241
}
2242
for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
2243
{
2244
if (*na<=0.0 || *na != *na)
2245
{
2246
nb[0]=0;
2247
nb[1]=1;
2248
}
2249
else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
2250
*na==(float)(uint32)(*na))
2251
{
2252
nb[0]=(uint32)(*na);
2253
nb[1]=1;
2254
}
2255
else if (*na<1.0)
2256
{
2257
nb[0]=(uint32)((double)(*na)*0xFFFFFFFF);
2258
nb[1]=0xFFFFFFFF;
2259
}
2260
else
2261
{
2262
nb[0]=0xFFFFFFFF;
2263
nb[1]=(uint32)((double)0xFFFFFFFF/(*na));
2264
}
2265
}
2266
if (tif->tif_flags&TIFF_SWAB)
2267
TIFFSwabArrayOfLong(m,count*2);
2268
o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
2269
_TIFFfree(m);
2270
return(o);
2271
}
2272
2273
static int
2274
TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
2275
{
2276
static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2277
int32* m;
2278
float* na;
2279
int32* nb;
2280
uint32 nc;
2281
int o;
2282
assert(sizeof(int32)==4);
2283
m=_TIFFmalloc(count*2*sizeof(int32));
2284
if (m==NULL)
2285
{
2286
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2287
return(0);
2288
}
2289
for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
2290
{
2291
if (*na<0.0)
2292
{
2293
if (*na==(int32)(*na))
2294
{
2295
nb[0]=(int32)(*na);
2296
nb[1]=1;
2297
}
2298
else if (*na>-1.0)
2299
{
2300
nb[0]=-(int32)((double)(-*na)*0x7FFFFFFF);
2301
nb[1]=0x7FFFFFFF;
2302
}
2303
else
2304
{
2305
nb[0]=-0x7FFFFFFF;
2306
nb[1]=(int32)((double)0x7FFFFFFF/(-*na));
2307
}
2308
}
2309
else
2310
{
2311
if (*na==(int32)(*na))
2312
{
2313
nb[0]=(int32)(*na);
2314
nb[1]=1;
2315
}
2316
else if (*na<1.0)
2317
{
2318
nb[0]=(int32)((double)(*na)*0x7FFFFFFF);
2319
nb[1]=0x7FFFFFFF;
2320
}
2321
else
2322
{
2323
nb[0]=0x7FFFFFFF;
2324
nb[1]=(int32)((double)0x7FFFFFFF/(*na));
2325
}
2326
}
2327
}
2328
if (tif->tif_flags&TIFF_SWAB)
2329
TIFFSwabArrayOfLong((uint32*)m,count*2);
2330
o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SRATIONAL,count,count*8,&m[0]);
2331
_TIFFfree(m);
2332
return(o);
2333
}
2334
2335
#ifdef notdef
2336
static int
2337
TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
2338
{
2339
float m;
2340
assert(sizeof(float)==4);
2341
m=value;
2342
TIFFCvtNativeToIEEEFloat(tif,1,&m);
2343
if (tif->tif_flags&TIFF_SWAB)
2344
TIFFSwabFloat(&m);
2345
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,1,4,&m));
2346
}
2347
#endif
2348
2349
static int
2350
TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
2351
{
2352
assert(count<0x40000000);
2353
assert(sizeof(float)==4);
2354
TIFFCvtNativeToIEEEFloat(tif,count,&value);
2355
if (tif->tif_flags&TIFF_SWAB)
2356
TIFFSwabArrayOfFloat(value,count);
2357
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,count,count*4,value));
2358
}
2359
2360
#ifdef notdef
2361
static int
2362
TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
2363
{
2364
double m;
2365
assert(sizeof(double)==8);
2366
m=value;
2367
TIFFCvtNativeToIEEEDouble(tif,1,&m);
2368
if (tif->tif_flags&TIFF_SWAB)
2369
TIFFSwabDouble(&m);
2370
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,1,8,&m));
2371
}
2372
#endif
2373
2374
static int
2375
TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
2376
{
2377
assert(count<0x20000000);
2378
assert(sizeof(double)==8);
2379
TIFFCvtNativeToIEEEDouble(tif,count,&value);
2380
if (tif->tif_flags&TIFF_SWAB)
2381
TIFFSwabArrayOfDouble(value,count);
2382
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,count,count*8,value));
2383
}
2384
2385
static int
2386
TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
2387
{
2388
assert(count<0x40000000);
2389
assert(sizeof(uint32)==4);
2390
if (tif->tif_flags&TIFF_SWAB)
2391
TIFFSwabArrayOfLong(value,count);
2392
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD,count,count*4,value));
2393
}
2394
2395
static int
2396
TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
2397
{
2398
assert(count<0x20000000);
2399
assert(sizeof(uint64)==8);
2400
assert(tif->tif_flags&TIFF_BIGTIFF);
2401
if (tif->tif_flags&TIFF_SWAB)
2402
TIFFSwabArrayOfLong8(value,count);
2403
return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD8,count,count*8,value));
2404
}
2405
2406
static int
2407
TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data)
2408
{
2409
static const char module[] = "TIFFWriteDirectoryTagData";
2410
uint32 m;
2411
m=0;
2412
while (m<(*ndir))
2413
{
2414
assert(dir[m].tdir_tag!=tag);
2415
if (dir[m].tdir_tag>tag)
2416
break;
2417
m++;
2418
}
2419
if (m<(*ndir))
2420
{
2421
uint32 n;
2422
for (n=*ndir; n>m; n--)
2423
dir[n]=dir[n-1];
2424
}
2425
dir[m].tdir_tag=tag;
2426
dir[m].tdir_type=datatype;
2427
dir[m].tdir_count=count;
2428
dir[m].tdir_offset.toff_long8 = 0;
2429
if (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))
2430
_TIFFmemcpy(&dir[m].tdir_offset,data,datalength);
2431
else
2432
{
2433
uint64 na,nb;
2434
na=tif->tif_dataoff;
2435
nb=na+datalength;
2436
if (!(tif->tif_flags&TIFF_BIGTIFF))
2437
nb=(uint32)nb;
2438
if ((nb<na)||(nb<datalength))
2439
{
2440
TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
2441
return(0);
2442
}
2443
if (!SeekOK(tif,na))
2444
{
2445
TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
2446
return(0);
2447
}
2448
assert(datalength<0x80000000UL);
2449
if (!WriteOK(tif,data,(tmsize_t)datalength))
2450
{
2451
TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
2452
return(0);
2453
}
2454
tif->tif_dataoff=nb;
2455
if (tif->tif_dataoff&1)
2456
tif->tif_dataoff++;
2457
if (!(tif->tif_flags&TIFF_BIGTIFF))
2458
{
2459
uint32 o;
2460
o=(uint32)na;
2461
if (tif->tif_flags&TIFF_SWAB)
2462
TIFFSwabLong(&o);
2463
_TIFFmemcpy(&dir[m].tdir_offset,&o,4);
2464
}
2465
else
2466
{
2467
dir[m].tdir_offset.toff_long8 = na;
2468
if (tif->tif_flags&TIFF_SWAB)
2469
TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
2470
}
2471
}
2472
(*ndir)++;
2473
return(1);
2474
}
2475
2476
/*
2477
* Link the current directory into the directory chain for the file.
2478
*/
2479
static int
2480
TIFFLinkDirectory(TIFF* tif)
2481
{
2482
static const char module[] = "TIFFLinkDirectory";
2483
2484
tif->tif_diroff = (TIFFSeekFile(tif,0,SEEK_END)+1) & (~((toff_t)1));
2485
2486
/*
2487
* Handle SubIFDs
2488
*/
2489
if (tif->tif_flags & TIFF_INSUBIFD)
2490
{
2491
if (!(tif->tif_flags&TIFF_BIGTIFF))
2492
{
2493
uint32 m;
2494
m = (uint32)tif->tif_diroff;
2495
if (tif->tif_flags & TIFF_SWAB)
2496
TIFFSwabLong(&m);
2497
(void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
2498
if (!WriteOK(tif, &m, 4)) {
2499
TIFFErrorExt(tif->tif_clientdata, module,
2500
"Error writing SubIFD directory link");
2501
return (0);
2502
}
2503
/*
2504
* Advance to the next SubIFD or, if this is
2505
* the last one configured, revert back to the
2506
* normal directory linkage.
2507
*/
2508
if (--tif->tif_nsubifd)
2509
tif->tif_subifdoff += 4;
2510
else
2511
tif->tif_flags &= ~TIFF_INSUBIFD;
2512
return (1);
2513
}
2514
else
2515
{
2516
uint64 m;
2517
m = tif->tif_diroff;
2518
if (tif->tif_flags & TIFF_SWAB)
2519
TIFFSwabLong8(&m);
2520
(void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
2521
if (!WriteOK(tif, &m, 8)) {
2522
TIFFErrorExt(tif->tif_clientdata, module,
2523
"Error writing SubIFD directory link");
2524
return (0);
2525
}
2526
/*
2527
* Advance to the next SubIFD or, if this is
2528
* the last one configured, revert back to the
2529
* normal directory linkage.
2530
*/
2531
if (--tif->tif_nsubifd)
2532
tif->tif_subifdoff += 8;
2533
else
2534
tif->tif_flags &= ~TIFF_INSUBIFD;
2535
return (1);
2536
}
2537
}
2538
2539
if (!(tif->tif_flags&TIFF_BIGTIFF))
2540
{
2541
uint32 m;
2542
uint32 nextdir;
2543
m = (uint32)(tif->tif_diroff);
2544
if (tif->tif_flags & TIFF_SWAB)
2545
TIFFSwabLong(&m);
2546
if (tif->tif_header.classic.tiff_diroff == 0) {
2547
/*
2548
* First directory, overwrite offset in header.
2549
*/
2550
tif->tif_header.classic.tiff_diroff = (uint32) tif->tif_diroff;
2551
(void) TIFFSeekFile(tif,4, SEEK_SET);
2552
if (!WriteOK(tif, &m, 4)) {
2553
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
2554
"Error writing TIFF header");
2555
return (0);
2556
}
2557
return (1);
2558
}
2559
/*
2560
* Not the first directory, search to the last and append.
2561
*/
2562
nextdir = tif->tif_header.classic.tiff_diroff;
2563
while(1) {
2564
uint16 dircount;
2565
uint32 nextnextdir;
2566
2567
if (!SeekOK(tif, nextdir) ||
2568
!ReadOK(tif, &dircount, 2)) {
2569
TIFFErrorExt(tif->tif_clientdata, module,
2570
"Error fetching directory count");
2571
return (0);
2572
}
2573
if (tif->tif_flags & TIFF_SWAB)
2574
TIFFSwabShort(&dircount);
2575
(void) TIFFSeekFile(tif,
2576
nextdir+2+dircount*12, SEEK_SET);
2577
if (!ReadOK(tif, &nextnextdir, 4)) {
2578
TIFFErrorExt(tif->tif_clientdata, module,
2579
"Error fetching directory link");
2580
return (0);
2581
}
2582
if (tif->tif_flags & TIFF_SWAB)
2583
TIFFSwabLong(&nextnextdir);
2584
if (nextnextdir==0)
2585
{
2586
(void) TIFFSeekFile(tif,
2587
nextdir+2+dircount*12, SEEK_SET);
2588
if (!WriteOK(tif, &m, 4)) {
2589
TIFFErrorExt(tif->tif_clientdata, module,
2590
"Error writing directory link");
2591
return (0);
2592
}
2593
break;
2594
}
2595
nextdir=nextnextdir;
2596
}
2597
}
2598
else
2599
{
2600
uint64 m;
2601
uint64 nextdir;
2602
m = tif->tif_diroff;
2603
if (tif->tif_flags & TIFF_SWAB)
2604
TIFFSwabLong8(&m);
2605
if (tif->tif_header.big.tiff_diroff == 0) {
2606
/*
2607
* First directory, overwrite offset in header.
2608
*/
2609
tif->tif_header.big.tiff_diroff = tif->tif_diroff;
2610
(void) TIFFSeekFile(tif,8, SEEK_SET);
2611
if (!WriteOK(tif, &m, 8)) {
2612
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
2613
"Error writing TIFF header");
2614
return (0);
2615
}
2616
return (1);
2617
}
2618
/*
2619
* Not the first directory, search to the last and append.
2620
*/
2621
nextdir = tif->tif_header.big.tiff_diroff;
2622
while(1) {
2623
uint64 dircount64;
2624
uint16 dircount;
2625
uint64 nextnextdir;
2626
2627
if (!SeekOK(tif, nextdir) ||
2628
!ReadOK(tif, &dircount64, 8)) {
2629
TIFFErrorExt(tif->tif_clientdata, module,
2630
"Error fetching directory count");
2631
return (0);
2632
}
2633
if (tif->tif_flags & TIFF_SWAB)
2634
TIFFSwabLong8(&dircount64);
2635
if (dircount64>0xFFFF)
2636
{
2637
TIFFErrorExt(tif->tif_clientdata, module,
2638
"Sanity check on tag count failed, likely corrupt TIFF");
2639
return (0);
2640
}
2641
dircount=(uint16)dircount64;
2642
(void) TIFFSeekFile(tif,
2643
nextdir+8+dircount*20, SEEK_SET);
2644
if (!ReadOK(tif, &nextnextdir, 8)) {
2645
TIFFErrorExt(tif->tif_clientdata, module,
2646
"Error fetching directory link");
2647
return (0);
2648
}
2649
if (tif->tif_flags & TIFF_SWAB)
2650
TIFFSwabLong8(&nextnextdir);
2651
if (nextnextdir==0)
2652
{
2653
(void) TIFFSeekFile(tif,
2654
nextdir+8+dircount*20, SEEK_SET);
2655
if (!WriteOK(tif, &m, 8)) {
2656
TIFFErrorExt(tif->tif_clientdata, module,
2657
"Error writing directory link");
2658
return (0);
2659
}
2660
break;
2661
}
2662
nextdir=nextnextdir;
2663
}
2664
}
2665
return (1);
2666
}
2667
2668
/************************************************************************/
2669
/* TIFFRewriteField() */
2670
/* */
2671
/* Rewrite a field in the directory on disk without regard to */
2672
/* updating the TIFF directory structure in memory. Currently */
2673
/* only supported for field that already exist in the on-disk */
2674
/* directory. Mainly used for updating stripoffset / */
2675
/* stripbytecount values after the directory is already on */
2676
/* disk. */
2677
/* */
2678
/* Returns zero on failure, and one on success. */
2679
/************************************************************************/
2680
2681
int
2682
_TIFFRewriteField(TIFF* tif, uint16 tag, TIFFDataType in_datatype,
2683
tmsize_t count, void* data)
2684
{
2685
static const char module[] = "TIFFResetField";
2686
/* const TIFFField* fip = NULL; */
2687
uint16 dircount;
2688
tmsize_t dirsize;
2689
uint8 direntry_raw[20];
2690
uint16 entry_tag = 0;
2691
uint16 entry_type = 0;
2692
uint64 entry_count = 0;
2693
uint64 entry_offset = 0;
2694
int value_in_entry = 0;
2695
uint64 read_offset;
2696
uint8 *buf_to_write = NULL;
2697
TIFFDataType datatype;
2698
2699
/* -------------------------------------------------------------------- */
2700
/* Find field definition. */
2701
/* -------------------------------------------------------------------- */
2702
/*fip =*/ TIFFFindField(tif, tag, TIFF_ANY);
2703
2704
/* -------------------------------------------------------------------- */
2705
/* Do some checking this is a straight forward case. */
2706
/* -------------------------------------------------------------------- */
2707
if( isMapped(tif) )
2708
{
2709
TIFFErrorExt( tif->tif_clientdata, module,
2710
"Memory mapped files not currently supported for this operation." );
2711
return 0;
2712
}
2713
2714
if( tif->tif_diroff == 0 )
2715
{
2716
TIFFErrorExt( tif->tif_clientdata, module,
2717
"Attempt to reset field on directory not already on disk." );
2718
return 0;
2719
}
2720
2721
/* -------------------------------------------------------------------- */
2722
/* Read the directory entry count. */
2723
/* -------------------------------------------------------------------- */
2724
if (!SeekOK(tif, tif->tif_diroff)) {
2725
TIFFErrorExt(tif->tif_clientdata, module,
2726
"%s: Seek error accessing TIFF directory",
2727
tif->tif_name);
2728
return 0;
2729
}
2730
2731
read_offset = tif->tif_diroff;
2732
2733
if (!(tif->tif_flags&TIFF_BIGTIFF))
2734
{
2735
if (!ReadOK(tif, &dircount, sizeof (uint16))) {
2736
TIFFErrorExt(tif->tif_clientdata, module,
2737
"%s: Can not read TIFF directory count",
2738
tif->tif_name);
2739
return 0;
2740
}
2741
if (tif->tif_flags & TIFF_SWAB)
2742
TIFFSwabShort(&dircount);
2743
dirsize = 12;
2744
read_offset += 2;
2745
} else {
2746
uint64 dircount64;
2747
if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
2748
TIFFErrorExt(tif->tif_clientdata, module,
2749
"%s: Can not read TIFF directory count",
2750
tif->tif_name);
2751
return 0;
2752
}
2753
if (tif->tif_flags & TIFF_SWAB)
2754
TIFFSwabLong8(&dircount64);
2755
dircount = (uint16)dircount64;
2756
dirsize = 20;
2757
read_offset += 8;
2758
}
2759
2760
/* -------------------------------------------------------------------- */
2761
/* Read through directory to find target tag. */
2762
/* -------------------------------------------------------------------- */
2763
while( dircount > 0 )
2764
{
2765
if (!ReadOK(tif, direntry_raw, dirsize)) {
2766
TIFFErrorExt(tif->tif_clientdata, module,
2767
"%s: Can not read TIFF directory entry.",
2768
tif->tif_name);
2769
return 0;
2770
}
2771
2772
memcpy( &entry_tag, direntry_raw + 0, sizeof(uint16) );
2773
if (tif->tif_flags&TIFF_SWAB)
2774
TIFFSwabShort( &entry_tag );
2775
2776
if( entry_tag == tag )
2777
break;
2778
2779
read_offset += dirsize;
2780
}
2781
2782
if( entry_tag != tag )
2783
{
2784
TIFFErrorExt(tif->tif_clientdata, module,
2785
"%s: Could not find tag %d.",
2786
tif->tif_name, tag );
2787
return 0;
2788
}
2789
2790
/* -------------------------------------------------------------------- */
2791
/* Extract the type, count and offset for this entry. */
2792
/* -------------------------------------------------------------------- */
2793
memcpy( &entry_type, direntry_raw + 2, sizeof(uint16) );
2794
if (tif->tif_flags&TIFF_SWAB)
2795
TIFFSwabShort( &entry_type );
2796
2797
if (!(tif->tif_flags&TIFF_BIGTIFF))
2798
{
2799
uint32 value;
2800
2801
memcpy( &value, direntry_raw + 4, sizeof(uint32) );
2802
if (tif->tif_flags&TIFF_SWAB)
2803
TIFFSwabLong( &value );
2804
entry_count = value;
2805
2806
memcpy( &value, direntry_raw + 8, sizeof(uint32) );
2807
if (tif->tif_flags&TIFF_SWAB)
2808
TIFFSwabLong( &value );
2809
entry_offset = value;
2810
}
2811
else
2812
{
2813
memcpy( &entry_count, direntry_raw + 4, sizeof(uint64) );
2814
if (tif->tif_flags&TIFF_SWAB)
2815
TIFFSwabLong8( &entry_count );
2816
2817
memcpy( &entry_offset, direntry_raw + 12, sizeof(uint64) );
2818
if (tif->tif_flags&TIFF_SWAB)
2819
TIFFSwabLong8( &entry_offset );
2820
}
2821
2822
/* -------------------------------------------------------------------- */
2823
/* What data type do we want to write this as? */
2824
/* -------------------------------------------------------------------- */
2825
if( TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags&TIFF_BIGTIFF) )
2826
{
2827
if( in_datatype == TIFF_LONG8 )
2828
datatype = TIFF_LONG;
2829
else if( in_datatype == TIFF_SLONG8 )
2830
datatype = TIFF_SLONG;
2831
else if( in_datatype == TIFF_IFD8 )
2832
datatype = TIFF_IFD;
2833
else
2834
datatype = in_datatype;
2835
}
2836
else
2837
datatype = in_datatype;
2838
2839
/* -------------------------------------------------------------------- */
2840
/* Prepare buffer of actual data to write. This includes */
2841
/* swabbing as needed. */
2842
/* -------------------------------------------------------------------- */
2843
buf_to_write =
2844
(uint8 *)_TIFFCheckMalloc(tif, count, TIFFDataWidth(datatype),
2845
"for field buffer.");
2846
if (!buf_to_write)
2847
return 0;
2848
2849
if( datatype == in_datatype )
2850
memcpy( buf_to_write, data, count * TIFFDataWidth(datatype) );
2851
else if( datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8 )
2852
{
2853
tmsize_t i;
2854
2855
for( i = 0; i < count; i++ )
2856
{
2857
((int32 *) buf_to_write)[i] =
2858
(int32) ((int64 *) data)[i];
2859
if( (int64) ((int32 *) buf_to_write)[i] != ((int64 *) data)[i] )
2860
{
2861
_TIFFfree( buf_to_write );
2862
TIFFErrorExt( tif->tif_clientdata, module,
2863
"Value exceeds 32bit range of output type." );
2864
return 0;
2865
}
2866
}
2867
}
2868
else if( (datatype == TIFF_LONG && in_datatype == TIFF_LONG8)
2869
|| (datatype == TIFF_IFD && in_datatype == TIFF_IFD8) )
2870
{
2871
tmsize_t i;
2872
2873
for( i = 0; i < count; i++ )
2874
{
2875
((uint32 *) buf_to_write)[i] =
2876
(uint32) ((uint64 *) data)[i];
2877
if( (uint64) ((uint32 *) buf_to_write)[i] != ((uint64 *) data)[i] )
2878
{
2879
_TIFFfree( buf_to_write );
2880
TIFFErrorExt( tif->tif_clientdata, module,
2881
"Value exceeds 32bit range of output type." );
2882
return 0;
2883
}
2884
}
2885
}
2886
2887
if( TIFFDataWidth(datatype) > 1 && (tif->tif_flags&TIFF_SWAB) )
2888
{
2889
if( TIFFDataWidth(datatype) == 2 )
2890
TIFFSwabArrayOfShort( (uint16 *) buf_to_write, count );
2891
else if( TIFFDataWidth(datatype) == 4 )
2892
TIFFSwabArrayOfLong( (uint32 *) buf_to_write, count );
2893
else if( TIFFDataWidth(datatype) == 8 )
2894
TIFFSwabArrayOfLong8( (uint64 *) buf_to_write, count );
2895
}
2896
2897
/* -------------------------------------------------------------------- */
2898
/* Is this a value that fits into the directory entry? */
2899
/* -------------------------------------------------------------------- */
2900
if (!(tif->tif_flags&TIFF_BIGTIFF))
2901
{
2902
if( TIFFDataWidth(datatype) * count <= 4 )
2903
{
2904
entry_offset = read_offset + 8;
2905
value_in_entry = 1;
2906
}
2907
}
2908
else
2909
{
2910
if( TIFFDataWidth(datatype) * count <= 8 )
2911
{
2912
entry_offset = read_offset + 12;
2913
value_in_entry = 1;
2914
}
2915
}
2916
2917
/* -------------------------------------------------------------------- */
2918
/* If the tag type, and count match, then we just write it out */
2919
/* over the old values without altering the directory entry at */
2920
/* all. */
2921
/* -------------------------------------------------------------------- */
2922
if( entry_count == (uint64)count && entry_type == (uint16) datatype )
2923
{
2924
if (!SeekOK(tif, entry_offset)) {
2925
_TIFFfree( buf_to_write );
2926
TIFFErrorExt(tif->tif_clientdata, module,
2927
"%s: Seek error accessing TIFF directory",
2928
tif->tif_name);
2929
return 0;
2930
}
2931
if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
2932
_TIFFfree( buf_to_write );
2933
TIFFErrorExt(tif->tif_clientdata, module,
2934
"Error writing directory link");
2935
return (0);
2936
}
2937
2938
_TIFFfree( buf_to_write );
2939
return 1;
2940
}
2941
2942
/* -------------------------------------------------------------------- */
2943
/* Otherwise, we write the new tag data at the end of the file. */
2944
/* -------------------------------------------------------------------- */
2945
if( !value_in_entry )
2946
{
2947
entry_offset = TIFFSeekFile(tif,0,SEEK_END);
2948
2949
if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
2950
_TIFFfree( buf_to_write );
2951
TIFFErrorExt(tif->tif_clientdata, module,
2952
"Error writing directory link");
2953
return (0);
2954
}
2955
}
2956
else
2957
{
2958
memcpy( &entry_offset, buf_to_write, count*TIFFDataWidth(datatype));
2959
}
2960
2961
_TIFFfree( buf_to_write );
2962
buf_to_write = 0;
2963
2964
/* -------------------------------------------------------------------- */
2965
/* Adjust the directory entry. */
2966
/* -------------------------------------------------------------------- */
2967
entry_type = datatype;
2968
memcpy( direntry_raw + 2, &entry_type, sizeof(uint16) );
2969
if (tif->tif_flags&TIFF_SWAB)
2970
TIFFSwabShort( (uint16 *) (direntry_raw + 2) );
2971
2972
if (!(tif->tif_flags&TIFF_BIGTIFF))
2973
{
2974
uint32 value;
2975
2976
value = (uint32) entry_count;
2977
memcpy( direntry_raw + 4, &value, sizeof(uint32) );
2978
if (tif->tif_flags&TIFF_SWAB)
2979
TIFFSwabLong( (uint32 *) (direntry_raw + 4) );
2980
2981
value = (uint32) entry_offset;
2982
memcpy( direntry_raw + 8, &value, sizeof(uint32) );
2983
if (tif->tif_flags&TIFF_SWAB)
2984
TIFFSwabLong( (uint32 *) (direntry_raw + 8) );
2985
}
2986
else
2987
{
2988
memcpy( direntry_raw + 4, &entry_count, sizeof(uint64) );
2989
if (tif->tif_flags&TIFF_SWAB)
2990
TIFFSwabLong8( (uint64 *) (direntry_raw + 4) );
2991
2992
memcpy( direntry_raw + 12, &entry_offset, sizeof(uint64) );
2993
if (tif->tif_flags&TIFF_SWAB)
2994
TIFFSwabLong8( (uint64 *) (direntry_raw + 12) );
2995
}
2996
2997
/* -------------------------------------------------------------------- */
2998
/* Write the directory entry out to disk. */
2999
/* -------------------------------------------------------------------- */
3000
if (!SeekOK(tif, read_offset )) {
3001
TIFFErrorExt(tif->tif_clientdata, module,
3002
"%s: Seek error accessing TIFF directory",
3003
tif->tif_name);
3004
return 0;
3005
}
3006
3007
if (!WriteOK(tif, direntry_raw,dirsize))
3008
{
3009
TIFFErrorExt(tif->tif_clientdata, module,
3010
"%s: Can not write TIFF directory entry.",
3011
tif->tif_name);
3012
return 0;
3013
}
3014
3015
return 1;
3016
}
3017
/* vim: set ts=8 sts=8 sw=8 noet: */
3018
/*
3019
* Local Variables:
3020
* mode: c
3021
* c-basic-offset: 8
3022
* fill-column: 78
3023
* End:
3024
*/
3025
3026