Skip to content

Latest commit

 

History

History
2917 lines (2759 loc) · 86.2 KB

tif_dirwrite.c

File metadata and controls

2917 lines (2759 loc) · 86.2 KB
 
Nov 10, 2019
Nov 10, 2019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: tif_dirwrite.c,v 1.77 2012-07-06 19:18:31 bfriesen Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
/*
* TIFF Library.
*
* Directory Write Support Routines.
*/
#include "tiffiop.h"
#ifdef HAVE_IEEEFP
#define TIFFCvtNativeToIEEEFloat(tif, n, fp)
#define TIFFCvtNativeToIEEEDouble(tif, n, dp)
#else
extern void TIFFCvtNativeToIEEEFloat(TIFF* tif, uint32 n, float* fp);
extern void TIFFCvtNativeToIEEEDouble(TIFF* tif, uint32 n, double* dp);
#endif
static int TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff);
static int TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
#if 0
static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
#endif
static int TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
static int TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
#ifdef notdef
static int TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
#endif
static int TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
#if 0
static int TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
#endif
#ifdef notdef
static int TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
#endif
static int TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
#if 0
static int TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
#endif
static int TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
static int TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
static int TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
#ifdef notdef
static int TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
#endif
static int TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
#if 0
static int TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
#endif
static int TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
static int TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
#if 0
static int TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
#endif
#ifdef notdef
static int TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
#endif
static int TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
#if 0
static int TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
#endif
#ifdef notdef
static int TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
#endif
static int TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
#ifdef notdef
static int TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
#endif
static int TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
static int TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
static int TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
static int TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
#ifdef notdef
static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
#endif
static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
#if 0
static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
#endif
#ifdef notdef
static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
#endif
static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
#if 0
static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
#endif
static int TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
#ifdef notdef
static int TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
#endif
static int TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
static int TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
#ifdef notdef
static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
#endif
static int TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
static int TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
static int TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
static int TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
#ifdef notdef
static int TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
#endif
static int TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
#ifdef notdef
static int TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
#endif
static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
static int TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
static int TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
#ifdef notdef
static int TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
#endif
static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
static int TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
static int TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
#ifdef notdef
static int TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
#endif
static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
#ifdef notdef
static int TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
#endif
static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
#ifdef notdef
static int TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
#endif
static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
static int TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
#ifdef notdef
static int TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
#endif
static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
#ifdef notdef
static int TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
#endif
static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
static int TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data);
static int TIFFLinkDirectory(TIFF*);
/*
* Write the contents of the current directory
* to the specified file. This routine doesn't
* handle overwriting a directory with auxiliary
* storage that's been changed.
*/
int
TIFFWriteDirectory(TIFF* tif)
{
return TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);
}
/*
* Similar to TIFFWriteDirectory(), writes the directory out
* but leaves all data structures in memory so that it can be
* written again. This will make a partially written TIFF file
* readable before it is successfully completed/closed.
*/
int
TIFFCheckpointDirectory(TIFF* tif)
{
int rc;
/* Setup the strips arrays, if they haven't already been. */
if (tif->tif_dir.td_stripoffset == NULL)
(void) TIFFSetupStrips(tif);
rc = TIFFWriteDirectorySec(tif,TRUE,FALSE,NULL);
(void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
return rc;
}
int
TIFFWriteCustomDirectory(TIFF* tif, uint64* pdiroff)
{
return TIFFWriteDirectorySec(tif,FALSE,FALSE,pdiroff);
}
/*
* Similar to TIFFWriteDirectory(), but if the directory has already
* been written once, it is relocated to the end of the file, in case it
* has changed in size. Note that this will result in the loss of the
* previously used directory space.
*/
int
TIFFRewriteDirectory( TIFF *tif )
{
static const char module[] = "TIFFRewriteDirectory";
/* We don't need to do anything special if it hasn't been written. */
if( tif->tif_diroff == 0 )
return TIFFWriteDirectory( tif );
/*
* Find and zero the pointer to this directory, so that TIFFLinkDirectory
* will cause it to be added after this directories current pre-link.
*/
if (!(tif->tif_flags&TIFF_BIGTIFF))
{
if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
{
tif->tif_header.classic.tiff_diroff = 0;
tif->tif_diroff = 0;
TIFFSeekFile(tif,4,SEEK_SET);
if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))
{
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
"Error updating TIFF header");
return (0);
}
}
else
{
uint32 nextdir;
nextdir = tif->tif_header.classic.tiff_diroff;
while(1) {
uint16 dircount;
uint32 nextnextdir;
if (!SeekOK(tif, nextdir) ||
!ReadOK(tif, &dircount, 2)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error fetching directory count");
return (0);
}
if (tif->tif_flags & TIFF_SWAB)
TIFFSwabShort(&dircount);
(void) TIFFSeekFile(tif,
nextdir+2+dircount*12, SEEK_SET);
if (!ReadOK(tif, &nextnextdir, 4)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error fetching directory link");
return (0);
}
if (tif->tif_flags & TIFF_SWAB)
TIFFSwabLong(&nextnextdir);
if (nextnextdir==tif->tif_diroff)
{
uint32 m;
m=0;
(void) TIFFSeekFile(tif,
nextdir+2+dircount*12, SEEK_SET);
if (!WriteOK(tif, &m, 4)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error writing directory link");
return (0);
}
tif->tif_diroff=0;
break;
}
nextdir=nextnextdir;
}
}
}
else
{
if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
{
tif->tif_header.big.tiff_diroff = 0;
tif->tif_diroff = 0;
TIFFSeekFile(tif,8,SEEK_SET);
if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))
{
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
"Error updating TIFF header");
return (0);
}
}
else
{
uint64 nextdir;
nextdir = tif->tif_header.big.tiff_diroff;
while(1) {
uint64 dircount64;
uint16 dircount;
uint64 nextnextdir;
if (!SeekOK(tif, nextdir) ||
!ReadOK(tif, &dircount64, 8)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error fetching directory count");
return (0);
}
if (tif->tif_flags & TIFF_SWAB)
TIFFSwabLong8(&dircount64);
if (dircount64>0xFFFF)
{
TIFFErrorExt(tif->tif_clientdata, module,
"Sanity check on tag count failed, likely corrupt TIFF");
return (0);
}
dircount=(uint16)dircount64;
(void) TIFFSeekFile(tif,
nextdir+8+dircount*20, SEEK_SET);
if (!ReadOK(tif, &nextnextdir, 8)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error fetching directory link");
return (0);
}
if (tif->tif_flags & TIFF_SWAB)
TIFFSwabLong8(&nextnextdir);
if (nextnextdir==tif->tif_diroff)
{
uint64 m;
m=0;
(void) TIFFSeekFile(tif,
nextdir+8+dircount*20, SEEK_SET);
if (!WriteOK(tif, &m, 8)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Error writing directory link");
return (0);
}
tif->tif_diroff=0;
break;
}
nextdir=nextnextdir;
}
}
}
/*
* Now use TIFFWriteDirectory() normally.
*/
return TIFFWriteDirectory( tif );
}
static int
TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
{
static const char module[] = "TIFFWriteDirectorySec";
uint32 ndir;
TIFFDirEntry* dir;
uint32 dirsize;
void* dirmem;
uint32 m;
if (tif->tif_mode == O_RDONLY)
return (1);
_TIFFFillStriles( tif );
/*
* Clear write state so that subsequent images with
* different characteristics get the right buffers
* setup for them.
*/
if (imagedone)
{
if (tif->tif_flags & TIFF_POSTENCODE)
{
tif->tif_flags &= ~TIFF_POSTENCODE;
if (!(*tif->tif_postencode)(tif))
{
TIFFErrorExt(tif->tif_clientdata,module,
"Error post-encoding before directory write");
return (0);
}
}
(*tif->tif_close)(tif); /* shutdown encoder */
/*
* Flush any data that might have been written
* by the compression close+cleanup routines. But
* be careful not to write stuff if we didn't add data
* in the previous steps as the "rawcc" data may well be
* a previously read tile/strip in mixed read/write mode.
*/
if (tif->tif_rawcc > 0
&& (tif->tif_flags & TIFF_BEENWRITING) != 0 )
{
if( !TIFFFlushData1(tif) )
{
TIFFErrorExt(tif->tif_clientdata, module,
"Error flushing data before directory write");
return (0);
}
}
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
{
_TIFFfree(tif->tif_rawdata);
tif->tif_rawdata = NULL;
tif->tif_rawcc = 0;
tif->tif_rawdatasize = 0;
tif->tif_rawdataoff = 0;
tif->tif_rawdataloaded = 0;
}
tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
}
dir=NULL;
dirmem=NULL;
dirsize=0;
while (1)
{
ndir=0;
if (isimage)
{
if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
{
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))
goto bad;
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))
{
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))
goto bad;
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_RESOLUTION))
{
if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))
goto bad;
if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_POSITION))
{
if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))
goto bad;
if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_SUBFILETYPE))
{
if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
{
if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_COMPRESSION))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_THRESHHOLDING))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_FILLORDER))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_ORIENTATION))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))
{
if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
{
if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
{
if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_PLANARCONFIG))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
{
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))
{
if (!isTiled(tif))
{
if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
goto bad;
}
else
{
if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
goto bad;
}
}
if (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))
{
if (!isTiled(tif))
{
/* td_stripoffset can be NULL even if td_nstrips == 1 due to OJPEG hack */
if (tif->tif_dir.td_stripoffset)
{
if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
goto bad;
}
}
else
{
if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
goto bad;
}
}
if (TIFFFieldSet(tif,FIELD_COLORMAP))
{
if (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))
{
if (tif->tif_dir.td_extrasamples)
{
uint16 na;
uint16* nb;
TIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))
goto bad;
}
}
if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))
{
if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))
{
if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))
{
if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
{
if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
{
if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
{
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
{
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))
{
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
{
if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))
{
if (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_INKNAMES))
{
if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))
goto bad;
}
if (TIFFFieldSet(tif,FIELD_SUBIFD))
{
if (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))
goto bad;
}
{
uint32 n;
for (n=0; n<tif->tif_nfields; n++) {
const TIFFField* o;
o = tif->tif_fields[n];
if ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))
{
switch (o->get_field_type)
{
case TIFF_SETGET_ASCII:
{
uint32 pa;
char* pb;
assert(o->field_type==TIFF_ASCII);
assert(o->field_readcount==TIFF_VARIABLE);
assert(o->field_passcount==0);
TIFFGetField(tif,o->field_tag,&pb);
pa=(uint32)(strlen(pb));
if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,o->field_tag,pa,pb))
goto bad;
}
break;
case TIFF_SETGET_UINT16:
{
uint16 p;
assert(o->field_type==TIFF_SHORT);
assert(o->field_readcount==1);
assert(o->field_passcount==0);
TIFFGetField(tif,o->field_tag,&p);
if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,o->field_tag,p))
goto bad;
}
break;
case TIFF_SETGET_UINT32:
{
uint32 p;
assert(o->field_type==TIFF_LONG);
assert(o->field_readcount==1);
assert(o->field_passcount==0);
TIFFGetField(tif,o->field_tag,&p);
if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,o->field_tag,p))
goto bad;
}
break;
case TIFF_SETGET_C32_UINT8:
{
uint32 pa;
void* pb;
assert(o->field_type==TIFF_UNDEFINED);
assert(o->field_readcount==TIFF_VARIABLE2);
assert(o->field_passcount==1);
TIFFGetField(tif,o->field_tag,&pa,&pb);
if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,o->field_tag,pa,pb))
goto bad;
}
break;
default:
TIFFErrorExt(tif->tif_clientdata,module,
"Cannot write tag %d (%s)",
TIFFFieldTag(o),
o->field_name ? o->field_name : "unknown");
goto bad;
}
}
}
}
}
for (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)
{
switch (tif->tif_dir.td_customValues[m].info->field_type)
{
case TIFF_ASCII:
if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_UNDEFINED:
if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_BYTE:
if (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_SBYTE:
if (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_SHORT:
if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_SSHORT:
if (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_LONG:
if (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_SLONG:
if (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_LONG8:
if (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_SLONG8:
if (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_RATIONAL:
if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_SRATIONAL:
if (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_FLOAT:
if (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_DOUBLE:
if (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_IFD:
if (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
case TIFF_IFD8:
if (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
goto bad;
break;
default:
assert(0); /* we should never get here */
break;
}
}
if (dir!=NULL)
break;
dir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));
if (dir==NULL)
{
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
goto bad;
}
if (isimage)
{
if ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))
goto bad;
}
else
tif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~1);
if (pdiroff!=NULL)
*pdiroff=tif->tif_diroff;
if (!(tif->tif_flags&TIFF_BIGTIFF))
dirsize=2+ndir*12+4;
else
dirsize=8+ndir*20+8;
tif->tif_dataoff=tif->tif_diroff+dirsize;
if (!(tif->tif_flags&TIFF_BIGTIFF))
tif->tif_dataoff=(uint32)tif->tif_dataoff;
if ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))
{
TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
goto bad;
}
if (tif->tif_dataoff&1)
tif->tif_dataoff++;
if (isimage)
tif->tif_curdir++;
}
if (isimage)
{
if (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))
{
uint32 na;
TIFFDirEntry* nb;
for (na=0, nb=dir; ; na++, nb++)
{
assert(na<ndir);
if (nb->tdir_tag==TIFFTAG_SUBIFD)
break;
}
if (!(tif->tif_flags&TIFF_BIGTIFF))
tif->tif_subifdoff=tif->tif_diroff+2+na*12+8;
else
tif->tif_subifdoff=tif->tif_diroff+8+na*20+12;
}
}
dirmem=_TIFFmalloc(dirsize);
if (dirmem==NULL)
{
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
goto bad;
}
if (!(tif->tif_flags&TIFF_BIGTIFF))
{
uint8* n;
uint32 nTmp;
TIFFDirEntry* o;
n=dirmem;
*(uint16*)n=ndir;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabShort((uint16*)n);
n+=2;
o=dir;
for (m=0; m<ndir; m++)
{
*(uint16*)n=o->tdir_tag;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabShort((uint16*)n);
n+=2;
*(uint16*)n=o->tdir_type;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabShort((uint16*)n);
n+=2;
nTmp = (uint32)o->tdir_count;
_TIFFmemcpy(n,&nTmp,4);
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabLong((uint32*)n);
n+=4;
/* This is correct. The data has been */
/* swabbed previously in TIFFWriteDirectoryTagData */
_TIFFmemcpy(n,&o->tdir_offset,4);
n+=4;
o++;
}
nTmp = (uint32)tif->tif_nextdiroff;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabLong(&nTmp);
_TIFFmemcpy(n,&nTmp,4);
}
else
{
uint8* n;
TIFFDirEntry* o;
n=dirmem;
*(uint64*)n=ndir;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabLong8((uint64*)n);
n+=8;
o=dir;
for (m=0; m<ndir; m++)
{
*(uint16*)n=o->tdir_tag;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabShort((uint16*)n);
n+=2;
*(uint16*)n=o->tdir_type;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabShort((uint16*)n);
n+=2;
_TIFFmemcpy(n,&o->tdir_count,8);
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabLong8((uint64*)n);
n+=8;
_TIFFmemcpy(n,&o->tdir_offset,8);
n+=8;
o++;
}
_TIFFmemcpy(n,&tif->tif_nextdiroff,8);
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabLong8((uint64*)n);
}
_TIFFfree(dir);
dir=NULL;
if (!SeekOK(tif,tif->tif_diroff))
{
TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
goto bad;
}
if (!WriteOK(tif,dirmem,(tmsize_t)dirsize))
{
TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
goto bad;
}
_TIFFfree(dirmem);
if (imagedone)
{
TIFFFreeDirectory(tif);
tif->tif_flags &= ~TIFF_DIRTYDIRECT;
tif->tif_flags &= ~TIFF_DIRTYSTRIP;
(*tif->tif_cleanup)(tif);
/*
* Reset directory-related state for subsequent
* directories.
*/
TIFFCreateDirectory(tif);
}
return(1);
bad:
if (dir!=NULL)
_TIFFfree(dir);
if (dirmem!=NULL)
_TIFFfree(dirmem);
return(0);
}
static int
TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
{
static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
void* conv;
uint32 i;
int ok;
conv = _TIFFmalloc(count*sizeof(double));
if (conv == NULL)
{
TIFFErrorExt(tif->tif_clientdata, module, "Out of memory");
return (0);
}
switch (tif->tif_dir.td_sampleformat)
{
case SAMPLEFORMAT_IEEEFP:
if (tif->tif_dir.td_bitspersample<=32)
{
for (i = 0; i < count; ++i)
((float*)conv)[i] = (float)value[i];
ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
}
else
{
ok = TIFFWriteDirectoryTagDoubleArray(tif,ndir,dir,tag,count,value);
}
break;
case SAMPLEFORMAT_INT:
if (tif->tif_dir.td_bitspersample<=8)
{
for (i = 0; i < count; ++i)
((int8*)conv)[i] = (int8)value[i];
ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);
}
else if (tif->tif_dir.td_bitspersample<=16)
{
for (i = 0; i < count; ++i)
((int16*)conv)[i] = (int16)value[i];
ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);
}
else
{
for (i = 0; i < count; ++i)
((int32*)conv)[i] = (int32)value[i];
ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
}
break;
case SAMPLEFORMAT_UINT:
if (tif->tif_dir.td_bitspersample<=8)
{
for (i = 0; i < count; ++i)
((uint8*)conv)[i] = (uint8)value[i];
ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);
}
else if (tif->tif_dir.td_bitspersample<=16)
{
for (i = 0; i < count; ++i)
((uint16*)conv)[i] = (uint16)value[i];
ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);
}
else
{
for (i = 0; i < count; ++i)
((uint32*)conv)[i] = (uint32)value[i];
ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);