-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtif_dir.cs
executable file
·1666 lines (1526 loc) · 53 KB
/
tif_dir.cs
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
// tif_dir.cs
//
// Based on LIBTIFF, Version 3.9.4 - 15-Jun-2010
// Copyright (c) 2006-2010 by the Authors
// 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 Tag Get & Set Routines.
// (and also some miscellaneous stuff)
using System;
using System.Collections.Generic;
namespace Free.Ports.LibTiff
{
// "Library-private" Directory-related Definitions.
// Internal format of a TIFF directory entry.
class TIFFDirectory
{
// bit vector of fields that are set
internal uint[] td_fieldsset=new uint[libtiff.FIELD_SETLONGS];
internal uint td_imagewidth, td_imagelength, td_imagedepth;
internal uint td_tilewidth, td_tilelength, td_tiledepth;
internal FILETYPE td_subfiletype;
internal ushort td_bitspersample;
internal SAMPLEFORMAT td_sampleformat;
internal COMPRESSION td_compression;
internal PHOTOMETRIC td_photometric;
internal THRESHHOLD td_threshholding;
internal FILLORDER td_fillorder;
internal ORIENTATION td_orientation;
internal ushort td_samplesperpixel;
internal uint td_rowsperstrip;
internal ushort td_minsamplevalue, td_maxsamplevalue;
internal double td_sminsamplevalue, td_smaxsamplevalue;
internal double td_xresolution, td_yresolution;
internal RESUNIT td_resolutionunit;
internal PLANARCONFIG td_planarconfig;
internal double td_xposition, td_yposition;
internal ushort[] td_pagenumber=new ushort[2];
internal ushort[][] td_colormap=new ushort[3][];
internal ushort[] td_halftonehints=new ushort[2];
internal ushort td_extrasamples;
internal ushort[] td_sampleinfo;
// even though the name is misleading, td_stripsperimage is the number
// of striles (=strips or tiles) per plane, and td_nstrips the total
// number of striles
internal uint td_stripsperimage;
internal uint td_nstrips; // size of offset & bytecount arrays
internal uint[] td_stripoffset;
internal uint[] td_stripbytecount; // FIXME: it should be tsize_t array
internal int td_stripbytecountsorted; // is the bytecount array sorted ascending?
internal ushort td_nsubifd;
internal uint[] td_subifd;
// YCbCr parameters
internal ushort[] td_ycbcrsubsampling=new ushort[2];
internal YCBCRPOSITION td_ycbcrpositioning;
// Colorimetry parameters
internal double[] td_refblackwhite;
internal ushort[][] td_transferfunction=new ushort[3][];
// CMYK parameters
internal int td_inknameslen;
internal string td_inknames;
internal int td_customValueCount;
internal List<TIFFTagValue> td_customValues;
public void Clear()
{
td_fieldsset=new uint[libtiff.FIELD_SETLONGS];
td_imagewidth=td_imagelength=td_imagedepth=0;
td_tilewidth=td_tilelength=td_tiledepth=0;
td_subfiletype=0;
td_bitspersample=0;
td_sampleformat=0;
td_compression=0;
td_photometric=0;
td_threshholding=0;
td_fillorder=0;
td_orientation=0;
td_samplesperpixel=0;
td_rowsperstrip=0;
td_minsamplevalue=td_maxsamplevalue=0;
td_sminsamplevalue=td_smaxsamplevalue=0;
td_xresolution=td_yresolution=0;
td_resolutionunit=0;
td_planarconfig=0;
td_xposition=td_yposition=0;
td_pagenumber=new ushort[2];
td_colormap=new ushort[3][];
td_halftonehints=new ushort[2];
td_extrasamples=0;
td_sampleinfo=null;
td_stripsperimage=0;
td_nstrips=0; // size of offset & bytecount arrays
td_stripoffset=null;
td_stripbytecount=null;
td_stripbytecountsorted=0; // is the bytecount array sorted ascending?
td_nsubifd=0;
td_subifd=null;
// YCbCr parameters
td_ycbcrsubsampling=new ushort[2];
td_ycbcrpositioning=0;
// Colorimetry parameters
td_refblackwhite=null;
td_transferfunction=new ushort[3][];
// CMYK parameters
td_inknameslen=0;
td_inknames=null;
td_customValueCount=0;
td_customValues.Clear();
}
}
// Field flags used to indicate fields that have
// been set in a directory, and to reference fields
// when manipulating a directory.
// FIELD_IGNORE is used to signify tags that are to
// be processed but otherwise ignored. This permits
// antiquated tags to be quietly read and discarded.
// Note that a bit *is* allocated for ignored tags;
// this is understood by the directory reading logic
// which uses this fact to avoid special-case handling
public enum FIELD : int
{
IGNORE=0,
PSEUDO=0,
// multi-item fields
IMAGEDIMENSIONS=1,
TILEDIMENSIONS=2,
RESOLUTION=3,
POSITION=4,
// single-item fields
SUBFILETYPE=5,
BITSPERSAMPLE=6,
COMPRESSION=7,
PHOTOMETRIC=8,
THRESHHOLDING=9,
FILLORDER=10,
ORIENTATION=15,
SAMPLESPERPIXEL=16,
ROWSPERSTRIP=17,
MINSAMPLEVALUE=18,
MAXSAMPLEVALUE=19,
PLANARCONFIG=20,
RESOLUTIONUNIT=22,
PAGENUMBER=23,
STRIPBYTECOUNTS=24,
STRIPOFFSETS=25,
COLORMAP=26,
EXTRASAMPLES=31,
SAMPLEFORMAT=32,
SMINSAMPLEVALUE=33,
SMAXSAMPLEVALUE=34,
IMAGEDEPTH=35,
TILEDEPTH=36,
HALFTONEHINTS=37,
YCBCRSUBSAMPLING=39,
YCBCRPOSITIONING=40,
REFBLACKWHITE=41,
TRANSFERFUNCTION=44,
INKNAMES=46,
SUBIFD=49,
CUSTOM=65,
// end of support for well-known tags; codec-private tags follow
CODEC=66, // base of codec-private tags
#if JPEG_SUPPORT
JPEG_JPEGTABLES=FIELD.CODEC+0,
JPEG_RECVPARAMS=FIELD.CODEC+1,
JPEG_SUBADDRESS=FIELD.CODEC+2,
JPEG_RECVTIME=FIELD.CODEC+3,
JPEG_FAXDCS=FIELD.CODEC+4,
#endif
#if CCITT_SUPPORT
CCITT_BADFAXLINES=FIELD.CODEC+0,
CCITT_CLEANFAXDATA=FIELD.CODEC+1,
CCITT_BADFAXRUN=FIELD.CODEC+2,
CCITT_RECVPARAMS=FIELD.CODEC+3,
CCITT_SUBADDRESS=FIELD.CODEC+4,
CCITT_RECVTIME=FIELD.CODEC+5,
CCITT_FAXDCS=FIELD.CODEC+6,
CCITT_OPTIONS=FIELD.CODEC+7,
#endif
}
public static partial class libtiff
{
internal const int FIELD_SETLONGS=4;
// Pseudo-tags don't normally need field bits since they
// are not written to an output file (by definition).
// The library also has express logic to always query a
// codec for a pseudo-tag so allocating a field bit for
// one is a waste. If codec wants to promote the notion
// of a pseudo-tag being "set" or "unset" then it can
// do using internal state flags without polluting the
// field bit space defined for real tags.
const int FIELD_PSEUDO=0;
const int FIELD_LAST=(32*FIELD_SETLONGS-1);
static bool TIFFFieldSet(TIFF tif, FIELD field)
{
return (tif.tif_dir.td_fieldsset[((int)field)/32]&(1u<<(((int)field)&0x1f)))!=0;
}
static void TIFFSetFieldBit(TIFF tif, FIELD field)
{
tif.tif_dir.td_fieldsset[((int)field)/32]|=(1u<<(((int)field)&0x1f));
}
static void TIFFClrFieldBit(TIFF tif, FIELD field)
{
tif.tif_dir.td_fieldsset[((int)field)/32]&=~(1u<<(((int)field)&0x1f));
}
static bool FieldSet(uint[] fields, FIELD field)
{
return (fields[((int)field)/32]&(1u<<(((int)field)&0x1f)))!=0;
}
static void ResetFieldBit(uint[] fields, FIELD field)
{
fields[((int)field)/32]&=~(1u<<(((int)field)&0x1f));
}
// These are used in the backwards compatibility code...
const uint DATATYPE_VOID=0; // !untyped data
const uint DATATYPE_INT=1; // !signed integer data
const uint DATATYPE_UINT=2; // !unsigned integer data
const uint DATATYPE_IEEEFP=3; // !IEEE floating point data
static void TIFFsetByteArray(ref byte[] vpp, byte[] vp, uint n)
{
vpp=null;
if(vp!=null)
{
try
{
vpp=new byte[n];
Array.Copy(vp, vpp, n);
}
catch
{
}
}
}
static void TIFFsetShortArray(ref ushort[] wpp, ushort[] wp, uint n)
{
wpp=null;
if(wp!=null)
{
try
{
wpp=new ushort[n];
Array.Copy(wp, wpp, n);
}
catch
{
}
}
}
static void TIFFsetLongArray(ref uint[] lpp, uint[] lp, uint n)
{
lpp=null;
if(lp!=null)
{
try
{
lpp=new uint[n];
Array.Copy(lp, lpp, n);
}
catch
{
}
}
}
static void TIFFsetFloatArray(ref float[] fpp, float[] fp, uint n)
{
fpp=null;
if(fp!=null)
{
try
{
fpp=new float[n];
Array.Copy(fp, fpp, n);
}
catch
{
}
}
}
static void TIFFsetDoubleArray(ref double[] dpp, double[] dp, uint n)
{
dpp=null;
if(dp!=null)
{
try
{
dpp=new double[n];
Array.Copy(dp, dpp, n);
}
catch
{
}
}
}
// Install extra samples information.
static bool setExtraSamples(TIFFDirectory td, object[] ap, out uint v)
{
v=(uint)__GetAsUint(ap, 0);
if((ushort)v>td.td_samplesperpixel) return false;
Array a=ap[1] as Array;
if(v>1&&a==null) return false; // typically missing param
if(a==null&&v==1)
{
if(!(ap[1] is ushort)) // typically wrong param type, maybe EXTRASAMPLE[]
{
if(!(ap[1] is EXTRASAMPLE)) return false;
EXTRASAMPLE va=(EXTRASAMPLE)ap[1];
ushort[] tmp=new ushort[1];
if(va>EXTRASAMPLE.UNASSALPHA)
{
// XXX: Corel Draw is known to produce incorrect
// ExtraSamples tags which must be patched here if we
// want to be able to open some of the damaged TIFF
// files:
if((int)va==999) va=EXTRASAMPLE.UNASSALPHA;
else return false;
}
tmp[0]=(ushort)va;
td.td_extrasamples=(ushort)v;
TIFFsetShortArray(ref td.td_sampleinfo, tmp, td.td_extrasamples);
}
else
{
ushort[] va=new ushort[1];
va[0]=(ushort)ap[1];
if(va[0]>(ushort)EXTRASAMPLE.UNASSALPHA)
{
// XXX: Corel Draw is known to produce incorrect
// ExtraSamples tags which must be patched here if we
// want to be able to open some of the damaged TIFF
// files:
if(va[0]==999) va[0]=(ushort)EXTRASAMPLE.UNASSALPHA;
else return false;
}
td.td_extrasamples=(ushort)v;
TIFFsetShortArray(ref td.td_sampleinfo, va, td.td_extrasamples);
}
}
else
{
if(!(a is ushort[])) // typically wrong param type, maybe EXTRASAMPLE[]
{
if(!(a is EXTRASAMPLE[])) return false;
EXTRASAMPLE[] va=(EXTRASAMPLE[])a;
ushort[] tmp=new ushort[v];
for(int i=0; i<v; i++)
{
if(va[i]>EXTRASAMPLE.UNASSALPHA)
{
// XXX: Corel Draw is known to produce incorrect
// ExtraSamples tags which must be patched here if we
// want to be able to open some of the damaged TIFF
// files:
if((int)va[i]==999) va[i]=EXTRASAMPLE.UNASSALPHA;
else return false;
}
tmp[i]=(ushort)va[i];
}
td.td_extrasamples=(ushort)v;
TIFFsetShortArray(ref td.td_sampleinfo, tmp, td.td_extrasamples);
}
else
{
ushort[] va=(ushort[])a;
for(int i=0; i<v; i++)
{
if(va[i]>(ushort)EXTRASAMPLE.UNASSALPHA)
{
// XXX: Corel Draw is known to produce incorrect
// ExtraSamples tags which must be patched here if we
// want to be able to open some of the damaged TIFF
// files:
if(va[i]==999) va[i]=(ushort)EXTRASAMPLE.UNASSALPHA;
else return false;
}
}
td.td_extrasamples=(ushort)v;
TIFFsetShortArray(ref td.td_sampleinfo, va, td.td_extrasamples);
}
}
return true;
}
static uint checkInkNamesString(TIFF tif, uint slen, string s)
{
TIFFDirectory td=tif.tif_dir;
ushort i=td.td_samplesperpixel;
if(slen>0)
{
string[] inks=s.Split('\0');
if(inks.Length>=i)
{
uint len=(uint)inks[0].Length+1;
for(int a=1; a<i; a++) len+=(uint)inks[a].Length+1;
return len;
}
i-=(ushort)inks.Length;
}
TIFFErrorExt(tif.tif_clientdata, "TIFFSetField", "{0}: Invalid InkNames value; expecting {1} names, found {2}", tif.tif_name, td.td_samplesperpixel, td.td_samplesperpixel-i);
return 0;
}
static bool _TIFFVSetField(TIFF tif, TIFFTAG tag, TIFFDataType dt, object[] ap)
{
string module="_TIFFVSetField";
TIFFDirectory td=tif.tif_dir;
bool status=true;
uint v;
switch(tag)
{
case TIFFTAG.SUBFILETYPE:
td.td_subfiletype=(FILETYPE)__GetAsUint(ap, 0);
break;
case TIFFTAG.IMAGEWIDTH:
td.td_imagewidth=__GetAsUint(ap, 0);
break;
case TIFFTAG.IMAGELENGTH:
td.td_imagelength=__GetAsUint(ap, 0);
break;
case TIFFTAG.BITSPERSAMPLE:
td.td_bitspersample=__GetAsUshort(ap, 0);
// If the data require post-decoding processing to byte-swap
// samples, set it up here. Note that since tags are required
// to be ordered, compression code can override this behaviour
// in the setup method if it wants to roll the post decoding
// work in with its normal work.
if((tif.tif_flags&TIF_FLAGS.TIFF_SWAB)!=0)
{
if(td.td_bitspersample==16) tif.tif_postdecode=TIFFSwab16BitData;
else if(td.td_bitspersample==24) tif.tif_postdecode=TIFFSwab24BitData;
else if(td.td_bitspersample==32) tif.tif_postdecode=TIFFSwab32BitData;
else if(td.td_bitspersample==64) tif.tif_postdecode=TIFFSwab64BitData;
else if(td.td_bitspersample==128) tif.tif_postdecode=TIFFSwab64BitData; // two 64's
}
break;
case TIFFTAG.COMPRESSION:
v=__GetAsUint(ap, 0)&0xffff;
// If we're changing the compression scheme, the notify the
// previous module so that it can cleanup any state it's setup.
if(TIFFFieldSet(tif, FIELD.COMPRESSION))
{
if(td.td_compression==(COMPRESSION)v) break;
tif.tif_cleanup(tif);
tif.tif_flags&=~TIF_FLAGS.TIFF_CODERSETUP;
}
// Setup new compression routine state.
if(status=TIFFSetCompressionScheme(tif, (COMPRESSION)v)) td.td_compression=(COMPRESSION)v;
else status=false;
break;
case TIFFTAG.PHOTOMETRIC:
td.td_photometric=(PHOTOMETRIC)__GetAsUshort(ap, 0);
break;
case TIFFTAG.THRESHHOLDING:
td.td_threshholding=(THRESHHOLD)__GetAsUshort(ap, 0);
break;
case TIFFTAG.FILLORDER:
v=__GetAsUint(ap, 0);
if((FILLORDER)v!=FILLORDER.LSB2MSB&&(FILLORDER)v!=FILLORDER.MSB2LSB) goto badvalue;
td.td_fillorder=(FILLORDER)v;
break;
case TIFFTAG.ORIENTATION:
v=__GetAsUint(ap, 0);
if((ORIENTATION)v<ORIENTATION.TOPLEFT||ORIENTATION.LEFTBOT<(ORIENTATION)v)
goto badvalue;
td.td_orientation=(ORIENTATION)v;
break;
case TIFFTAG.SAMPLESPERPIXEL:
// XXX should cross check -- e.g. if pallette, then 1
v=__GetAsUint(ap, 0);
if(v==0) goto badvalue;
td.td_samplesperpixel=(ushort)v;
break;
case TIFFTAG.ROWSPERSTRIP:
v=__GetAsUint(ap, 0);
if(v==0) goto badvalue;
td.td_rowsperstrip=v;
if(!TIFFFieldSet(tif, FIELD.TILEDIMENSIONS))
{
td.td_tilelength=v;
td.td_tilewidth=td.td_imagewidth;
}
break;
case TIFFTAG.MINSAMPLEVALUE:
td.td_minsamplevalue=__GetAsUshort(ap, 0);
break;
case TIFFTAG.MAXSAMPLEVALUE:
td.td_maxsamplevalue=__GetAsUshort(ap, 0);
break;
case TIFFTAG.SMINSAMPLEVALUE:
td.td_sminsamplevalue=__GetAsDouble(ap, 0);
break;
case TIFFTAG.SMAXSAMPLEVALUE:
td.td_smaxsamplevalue=__GetAsDouble(ap, 0);
break;
case TIFFTAG.XRESOLUTION:
td.td_xresolution=__GetAsDouble(ap, 0);
break;
case TIFFTAG.YRESOLUTION:
td.td_yresolution=__GetAsDouble(ap, 0);
break;
case TIFFTAG.PLANARCONFIG:
v=__GetAsUint(ap, 0);
if((PLANARCONFIG)v!=PLANARCONFIG.CONTIG&&(PLANARCONFIG)v!=PLANARCONFIG.SEPARATE) goto badvalue;
td.td_planarconfig=(PLANARCONFIG)v;
break;
case TIFFTAG.XPOSITION:
td.td_xposition=__GetAsDouble(ap, 0);
break;
case TIFFTAG.YPOSITION:
td.td_yposition=__GetAsDouble(ap, 0);
break;
case TIFFTAG.RESOLUTIONUNIT:
v=__GetAsUint(ap, 0);
if((RESUNIT)v<RESUNIT.NONE||RESUNIT.CENTIMETER<(RESUNIT)v) goto badvalue;
td.td_resolutionunit=(RESUNIT)v;
break;
case TIFFTAG.PAGENUMBER:
td.td_pagenumber[0]=__GetAsUshort(ap, 0);
td.td_pagenumber[1]=__GetAsUshort(ap, 1);
break;
case TIFFTAG.HALFTONEHINTS:
td.td_halftonehints[0]=__GetAsUshort(ap, 0);
td.td_halftonehints[1]=__GetAsUshort(ap, 1);
break;
case TIFFTAG.COLORMAP:
v=1u<<td.td_bitspersample;
TIFFsetShortArray(ref td.td_colormap[0], (ushort[])ap[0], v);
TIFFsetShortArray(ref td.td_colormap[1], (ushort[])ap[1], v);
TIFFsetShortArray(ref td.td_colormap[2], (ushort[])ap[2], v);
break;
case TIFFTAG.EXTRASAMPLES:
if(!setExtraSamples(td, ap, out v)) goto badvalue;
break;
case TIFFTAG.MATTEING:
td.td_extrasamples=(ushort)((__GetAsInt(ap, 0)!=0)?1:0);
if(td.td_extrasamples!=0)
{
ushort[] sv=new ushort[] { (ushort)EXTRASAMPLE.ASSOCALPHA };
TIFFsetShortArray(ref td.td_sampleinfo, sv, 1);
}
break;
case TIFFTAG.TILEWIDTH:
v=__GetAsUint(ap, 0);
if((v%16)!=0)
{
if(tif.tif_mode!=O.RDONLY) goto badvalue;
TIFFWarningExt(tif.tif_clientdata, tif.tif_name, "Nonstandard tile width {0}, convert file", v);
}
td.td_tilewidth=v;
tif.tif_flags|=TIF_FLAGS.TIFF_ISTILED;
break;
case TIFFTAG.TILELENGTH:
v=__GetAsUint(ap, 0);
if((v%16)!=0)
{
if(tif.tif_mode!=O.RDONLY) goto badvalue;
TIFFWarningExt(tif.tif_clientdata, tif.tif_name, "Nonstandard tile length {0}, convert file", v);
}
td.td_tilelength=v;
tif.tif_flags|=TIF_FLAGS.TIFF_ISTILED;
break;
case TIFFTAG.TILEDEPTH:
v=__GetAsUint(ap, 0);
if(v==0) goto badvalue;
td.td_tiledepth=v;
break;
case TIFFTAG.DATATYPE:
v=__GetAsUint(ap, 0);
switch(v)
{
case DATATYPE_VOID: td.td_sampleformat=SAMPLEFORMAT.VOID; break;
case DATATYPE_INT: td.td_sampleformat=SAMPLEFORMAT.INT; break;
case DATATYPE_UINT: td.td_sampleformat=SAMPLEFORMAT.UINT; break;
case DATATYPE_IEEEFP: td.td_sampleformat=SAMPLEFORMAT.IEEEFP; break;
default: goto badvalue;
}
break;
case TIFFTAG.SAMPLEFORMAT:
v=__GetAsUint(ap, 0);
if((SAMPLEFORMAT)v<SAMPLEFORMAT.UINT||SAMPLEFORMAT.COMPLEXIEEEFP<(SAMPLEFORMAT)v) goto badvalue;
td.td_sampleformat=(SAMPLEFORMAT)v;
// Try to fix up the SWAB function for complex data.
if(td.td_sampleformat==SAMPLEFORMAT.COMPLEXINT&&td.td_bitspersample==32&&tif.tif_postdecode==TIFFSwab32BitData) tif.tif_postdecode=TIFFSwab16BitData;
else if((td.td_sampleformat==SAMPLEFORMAT.COMPLEXINT||td.td_sampleformat==SAMPLEFORMAT.COMPLEXIEEEFP)&&td.td_bitspersample==64&&
tif.tif_postdecode==TIFFSwab64BitData) tif.tif_postdecode=TIFFSwab32BitData;
break;
case TIFFTAG.IMAGEDEPTH:
td.td_imagedepth=__GetAsUint(ap, 0);
break;
case TIFFTAG.SUBIFD:
if((tif.tif_flags&TIF_FLAGS.TIFF_INSUBIFD)==0)
{
td.td_nsubifd=__GetAsUshort(ap, 0);
uint[] tmp=new uint[1];
tmp[0]=(uint)ap[1];
TIFFsetLongArray(ref td.td_subifd, tmp, td.td_nsubifd);
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Sorry, cannot nest SubIFDs", tif.tif_name);
status=false;
}
break;
case TIFFTAG.YCBCRPOSITIONING:
td.td_ycbcrpositioning=(YCBCRPOSITION)__GetAsUshort(ap, 0);
break;
case TIFFTAG.YCBCRSUBSAMPLING:
td.td_ycbcrsubsampling[0]=__GetAsUshort(ap, 0);
td.td_ycbcrsubsampling[1]=__GetAsUshort(ap, 1);
break;
case TIFFTAG.TRANSFERFUNCTION:
v=(uint)((td.td_samplesperpixel-td.td_extrasamples)>1?3:1);
for(int i=0; i<v; i++)
TIFFsetShortArray(ref td.td_transferfunction[i], (ushort[])ap[i], 1u<<td.td_bitspersample);
break;
case TIFFTAG.REFERENCEBLACKWHITE:
// XXX should check for null range
TIFFsetDoubleArray(ref td.td_refblackwhite, (double[])ap[0], 6);
break;
case TIFFTAG.INKNAMES:
v=__GetAsUint(ap, 0);
string s=(string)ap[1];
v=checkInkNamesString(tif, v, s);
status=v>0;
if(v>0)
{
td.td_inknames=s.Substring(0, (int)v);
td.td_inknameslen=(int)v;
}
break;
default:
{
TIFFFieldInfo fip=TIFFFindFieldInfo(tif, tag, dt); // was TIFFDataType.TIFF_ANY);
TIFFTagValue tv;
int iCustom;
// This can happen if multiple images are open with different
// codecs which have private tags. The global tag information
// table may then have tags that are valid for one file but not
// the other. If the client tries to set a tag that is not valid
// for the image's codec then we'll arrive here. This
// happens, for example, when tiffcp is used to convert between
// compression schemes and codec-specific tags are blindly copied.
if(fip==null||fip.field_bit!=FIELD.CUSTOM)
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Invalid {1}tag \"{2}\" (not supported by codec)", tif.tif_name, isPseudoTag(tag)?"pseudo-":"", fip!=null?fip.field_name:"Unknown");
status=false;
break;
}
// Find the existing entry for this custom value.
tv=null;
for(iCustom=0; iCustom<td.td_customValueCount; iCustom++)
{
if(td.td_customValues[iCustom].info.field_tag==tag)
{
tv=td.td_customValues[iCustom];
tv.value=null;
break;
}
}
// Grow the custom list if the entry was not found.
if(tv==null)
{
try
{
td.td_customValueCount++;
if(td.td_customValues==null) td.td_customValues=new List<TIFFTagValue>();
tv=new TIFFTagValue();
tv.info=fip;
td.td_customValues.Add(tv);
}
catch
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Failed to allocate space for list of custom values", tif.tif_name);
status=false;
goto end;
}
}
// Set custom value ... save a copy of the custom tag value.
if(TIFFDataSize(fip.field_type)==0)
{
status=false;
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad field type {1} for \"{2}\"", tif.tif_name, fip.field_type, fip.field_name);
goto end;
}
int apcount=0;
if(fip.field_passcount) tv.count=__GetAsInt(ap, apcount++);
else if(fip.field_writecount==TIFF_VARIABLE) tv.count=1;
else if(fip.field_writecount==TIFF_SPP) tv.count=td.td_samplesperpixel;
else tv.count=fip.field_writecount;
if(fip.field_type==TIFFDataType.TIFF_ASCII) tv.value=(string)ap[apcount++];
else
{
if(tv.count<1)
{
status=false;
goto end;
}
byte[] byteArray=null;
sbyte[] sbyteArray=null;
ushort[] ushortArray=null;
short[] shortArray=null;
uint[] uintArray=null;
int[] intArray=null;
float[] floatArray=null;
double[] doubleArray=null;
try
{
switch(fip.field_type)
{
case TIFFDataType.TIFF_UNDEFINED:
case TIFFDataType.TIFF_BYTE:
tv.value=byteArray=new byte[tv.count];
break;
case TIFFDataType.TIFF_SBYTE:
tv.value=sbyteArray=new sbyte[tv.count];
break;
case TIFFDataType.TIFF_SHORT:
tv.value=ushortArray=new ushort[tv.count];
break;
case TIFFDataType.TIFF_SSHORT:
tv.value=shortArray=new short[tv.count];
break;
case TIFFDataType.TIFF_IFD:
case TIFFDataType.TIFF_LONG:
tv.value=uintArray=new uint[tv.count];
break;
case TIFFDataType.TIFF_SLONG:
tv.value=intArray=new int[tv.count];
break;
case TIFFDataType.TIFF_FLOAT:
tv.value=floatArray=new float[tv.count];
break;
case TIFFDataType.TIFF_RATIONAL:
case TIFFDataType.TIFF_SRATIONAL:
case TIFFDataType.TIFF_DOUBLE:
tv.value=doubleArray=new double[tv.count];
break;
default:
tv.value=null;
break;
}
}
catch
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Out of memory.", tif.tif_name);
status=false;
goto end;
}
bool done=false;
if((fip.field_passcount||fip.field_writecount==TIFF_VARIABLE||fip.field_writecount==TIFF_SPP))
{
Array a=ap[apcount] as Array;
if(a!=null&&a.Length>=tv.count)
{
switch(fip.field_type)
{
case TIFFDataType.TIFF_UNDEFINED:
case TIFFDataType.TIFF_BYTE:
if(a is byte[])
{
Array.Copy(a, byteArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a byte[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
case TIFFDataType.TIFF_SBYTE:
if(a is sbyte[])
{
Array.Copy(a, sbyteArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a sbyte[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
case TIFFDataType.TIFF_SHORT:
if(a is ushort[])
{
Array.Copy(a, ushortArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a ushort[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
case TIFFDataType.TIFF_SSHORT:
if(a is short[])
{
Array.Copy(a, shortArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a short[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
case TIFFDataType.TIFF_IFD:
case TIFFDataType.TIFF_LONG:
if(a is uint[])
{
Array.Copy(a, uintArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a ulong[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
case TIFFDataType.TIFF_SLONG:
if(a is int[])
{
Array.Copy(a, intArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a long[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
case TIFFDataType.TIFF_FLOAT:
if(a is float[])
{
Array.Copy(a, floatArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a float[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
case TIFFDataType.TIFF_RATIONAL:
case TIFFDataType.TIFF_SRATIONAL:
case TIFFDataType.TIFF_DOUBLE:
if(a is double[])
{
Array.Copy(a, doubleArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a double[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
default:
tv.value=null;
break;
} // switch
} // if(a!=null&&a.Length>=tv.count)
else if(a!=null)
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Array has too few elements.", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
}
if(!done)
{
try
{
Array a=ap[apcount] as Array;
if(a!=null&&a.Length>=tv.count)
{
switch(fip.field_type)
{
case TIFFDataType.TIFF_UNDEFINED:
case TIFFDataType.TIFF_BYTE:
if(a is byte[])
{
Array.Copy(a, byteArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a byte[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;
goto end;
}
break;
case TIFFDataType.TIFF_SBYTE:
if(a is sbyte[])
{
Array.Copy(a, sbyteArray, tv.count);
done=true;
}
else
{
TIFFErrorExt(tif.tif_clientdata, module, "{0}: Bad argument type for \"{2}\" ({1}). Should be a sbyte[].", tif.tif_name, fip.field_type, fip.field_name);
status=false;