forked from JoshDullen/libtiffN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tif_getimage.cs
executable file
·2999 lines (2701 loc) · 91.6 KB
/
tif_getimage.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_getimage.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
//
// Read and return a packed RGBA image.
using System;
using System.Collections.Generic;
using System.Text;
namespace Free.Ports.LibTiff
{
public static partial class libtiff
{
const string photoTag="PhotometricInterpretation";
// Helper constants used in Orientation tag handling
const int FLIP_VERTICALLY=0x01;
const int FLIP_HORIZONTALLY=0x02;
static byte W2B(ushort w) { return (byte)(w>>8); }
// Check the image to see if TIFFReadRGBAImage can deal with it.
// 1/0 is returned according to whether or not the image can
// be handled. If 0 is returned, emsg contains the reason
// why it is being rejected.
public static bool TIFFRGBAImageOK(TIFF tif, out string emsg)
{
TIFFDirectory td=tif.tif_dir;
PHOTOMETRIC photometric;
if(!tif.tif_decodestatus)
{
emsg="Sorry, requested compression method is not configured";
return false;
}
switch(td.td_bitspersample)
{
case 1:
case 2:
case 4:
case 8:
case 16: break;
default: emsg=string.Format("Sorry, can not handle images with {0}-bit samples", td.td_bitspersample);
return false;
}
object[] ap=new object[2];
int colorchannels=td.td_samplesperpixel-td.td_extrasamples;
if(!TIFFGetField(tif, TIFFTAG.PHOTOMETRIC, ap))
{
switch(colorchannels)
{
case 1: photometric=PHOTOMETRIC.MINISBLACK; break;
case 3: photometric=PHOTOMETRIC.RGB; break;
default: emsg=string.Format("Missing needed {0} tag", photoTag); return false;
}
}
else photometric=(PHOTOMETRIC)__GetAsUshort(ap, 0);
switch(photometric)
{
case PHOTOMETRIC.MINISWHITE:
case PHOTOMETRIC.MINISBLACK:
case PHOTOMETRIC.PALETTE:
if(td.td_planarconfig==PLANARCONFIG.CONTIG&&td.td_samplesperpixel!=1&&td.td_bitspersample<8)
{
emsg=string.Format("Sorry, can not handle contiguous data with {0}={1}, and {2}={3} and Bits/Sample={4}",
photoTag, photometric, "Samples/pixel", td.td_samplesperpixel, td.td_bitspersample);
return false;
}
// We should likely validate that any extra samples are either
// to be ignored, or are alpha, and if alpha we should try to use
// them. But for now we won't bother with this.
break;
case PHOTOMETRIC.YCBCR:
// TODO: if at all meaningful and useful, make more complete
// support check here, or better still, refactor to let supporting
// code decide whether there is support and what meaningfull
// error to return
break;
case PHOTOMETRIC.RGB:
if(colorchannels<3)
{
emsg=string.Format("Sorry, can not handle RGB image with {0}={1}", "Color channels", colorchannels);
return false;
}
break;
case PHOTOMETRIC.SEPARATED:
{
TIFFGetFieldDefaulted(tif, TIFFTAG.INKSET, ap);
INKSET inkset=(INKSET)__GetAsUshort(ap, 0);
if(inkset!=INKSET.CMYK)
{
emsg=string.Format("Sorry, can not handle separated image with {0}={1}", "InkSet", inkset);
return false;
}
if(td.td_samplesperpixel<4)
{
emsg=string.Format("Sorry, can not handle separated image with {0}={1}", "Samples/pixel", td.td_samplesperpixel);
return false;
}
break;
}
case PHOTOMETRIC.LOGL:
if(td.td_compression!=COMPRESSION.SGILOG)
{
emsg=string.Format("Sorry, LogL data must have {0}={1}", "Compression", COMPRESSION.SGILOG);
return false;
}
break;
case PHOTOMETRIC.LOGLUV:
if(td.td_compression!=COMPRESSION.SGILOG&&
td.td_compression!=COMPRESSION.SGILOG24)
{
emsg=string.Format("Sorry, LogLuv data must have {0}={1} or {2}", "Compression", COMPRESSION.SGILOG, COMPRESSION.SGILOG24);
return false;
}
if(td.td_planarconfig!=PLANARCONFIG.CONTIG)
{
emsg=string.Format("Sorry, can not handle LogLuv images with {0}={1}", "Planarconfiguration", td.td_planarconfig);
return false;
}
break;
case PHOTOMETRIC.CIELAB: break;
default: emsg=string.Format("Sorry, can not handle image with {0}={1}", photoTag, photometric);
return false;
}
emsg="";
return true;
}
public static void TIFFRGBAImageEnd(TIFFRGBAImage img)
{
img.Map=null;
img.BWmap=img.PALmap=null;
img.ycbcr=null;
img.cielab=null;
img.redcmap=img.greencmap=img.bluecmap=null;
}
static bool isCCITTCompression(TIFF tif)
{
object[] ap=new object[2];
TIFFGetField(tif, TIFFTAG.COMPRESSION, ap);
COMPRESSION compress=(COMPRESSION)__GetAsUshort(ap, 0);
return (compress==COMPRESSION.CCITTFAX3||compress==COMPRESSION.CCITTFAX4||compress==COMPRESSION.CCITTRLE||compress==COMPRESSION.CCITTRLEW);
}
public static bool TIFFRGBAImageBegin(TIFFRGBAImage img, TIFF tif, bool stop, out string emsg)
{
// Initialize to normal values
img.row_offset=0;
img.col_offset=0;
img.redcmap=img.greencmap=img.bluecmap=null;
img.req_orientation=ORIENTATION.BOTLEFT; // It is the default
img.tif=tif;
img.stoponerr=stop;
object[] ap=new object[4];
TIFFGetFieldDefaulted(tif, TIFFTAG.BITSPERSAMPLE, ap);
img.bitspersample=__GetAsUshort(ap, 0);
switch(img.bitspersample)
{
case 1:
case 2:
case 4:
case 8:
case 16: break;
default:
emsg=string.Format("Sorry, can not handle images with {0}-bit samples", img.bitspersample);
return false;
}
img.alpha=0;
TIFFGetFieldDefaulted(tif, TIFFTAG.SAMPLESPERPIXEL, ap);
img.samplesperpixel=__GetAsUshort(ap, 0);
TIFFGetFieldDefaulted(tif, TIFFTAG.EXTRASAMPLES, ap);
ushort extrasamples=__GetAsUshort(ap, 0);
ushort[] sampleinfo=(ushort[])ap[1];
if(extrasamples>=1)
{
switch((EXTRASAMPLE)sampleinfo[0])
{
case EXTRASAMPLE.UNSPECIFIED: // Workaround for some images without
if(img.samplesperpixel>3) // correct info about alpha channel
img.alpha=EXTRASAMPLE.ASSOCALPHA;
break;
case EXTRASAMPLE.ASSOCALPHA: // data is pre-multiplied
case EXTRASAMPLE.UNASSALPHA: // data is not pre-multiplied
img.alpha=(EXTRASAMPLE)sampleinfo[0];
break;
}
}
#if DEFAULT_EXTRASAMPLE_AS_ALPHA
if(!TIFFGetField(tif, TIFFTAG.PHOTOMETRIC, ap)) img.photometric=PHOTOMETRIC.MINISWHITE;
else img.photometric=(PHOTOMETRIC)__GetAsUshort(ap, 0);
if(extrasamples==0&&img.samplesperpixel==4&&img.photometric==PHOTOMETRIC.RGB)
{
img.alpha=EXTRASAMPLE.ASSOCALPHA;
extrasamples=1;
}
#endif
int colorchannels=img.samplesperpixel-extrasamples;
TIFFGetFieldDefaulted(tif, TIFFTAG.COMPRESSION, ap);
COMPRESSION compress=(COMPRESSION)__GetAsUshort(ap, 0);
TIFFGetFieldDefaulted(tif, TIFFTAG.PLANARCONFIG, ap);
PLANARCONFIG planarconfig=(PLANARCONFIG)__GetAsUshort(ap, 0);
if(!TIFFGetField(tif, TIFFTAG.PHOTOMETRIC, ap))
{
switch(colorchannels)
{
case 1:
if(isCCITTCompression(tif)) img.photometric=PHOTOMETRIC.MINISWHITE;
else img.photometric=PHOTOMETRIC.MINISBLACK;
break;
case 3:
img.photometric=PHOTOMETRIC.RGB;
break;
default:
emsg=string.Format("Missing needed {0} tag", photoTag);
return false;
}
}
else img.photometric=(PHOTOMETRIC)__GetAsUshort(ap, 0);
switch(img.photometric)
{
case PHOTOMETRIC.PALETTE:
ushort[] red_orig, green_orig, blue_orig;
if(!TIFFGetField(tif, TIFFTAG.COLORMAP, ap))
{
emsg="Missing required \"Colormap\" tag";
return false;
}
red_orig=ap[0] as ushort[];
green_orig=ap[1] as ushort[];
blue_orig=ap[2] as ushort[];
// copy the colormaps so we can modify them
int n_color=(1<<img.bitspersample);
try
{
img.redcmap=new ushort[n_color];
img.greencmap=new ushort[n_color];
img.bluecmap=new ushort[n_color];
}
catch
{
emsg="Out of memory for colormap copy";
return false;
}
Array.Copy(red_orig, img.redcmap, n_color);
Array.Copy(green_orig, img.greencmap, n_color);
Array.Copy(blue_orig, img.bluecmap, n_color);
goto case PHOTOMETRIC.MINISBLACK; // fall thru...
case PHOTOMETRIC.MINISWHITE:
case PHOTOMETRIC.MINISBLACK:
if(planarconfig==PLANARCONFIG.CONTIG&&img.samplesperpixel!=1&&img.bitspersample<8)
{
emsg=string.Format("Sorry, can not handle contiguous data with {0}={1}, and {2}={3} and Bits/Sample={4}", photoTag, img.photometric, "Samples/pixel", img.samplesperpixel, img.bitspersample);
return false;
}
break;
case PHOTOMETRIC.YCBCR:
// It would probably be nice to have a reality check here.
if(planarconfig==PLANARCONFIG.CONTIG)
{
// can rely on libjpeg to convert to RGB
// XXX should restore current state on exit
switch(compress)
{
case COMPRESSION.JPEG:
// TODO: when complete tests verify complete desubsampling
// and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
// favor of tif_getimage.c native handling
TIFFSetField(tif, TIFFTAG.JPEGCOLORMODE, JPEGCOLORMODE.RGB);
img.photometric=PHOTOMETRIC.RGB;
break;
default: break; // do nothing
}
}
// TODO: if at all meaningful and useful, make more complete
// support check here, or better still, refactor to let supporting
// code decide whether there is support and what meaningfull
// error to return
break;
case PHOTOMETRIC.RGB:
if(colorchannels<3)
{
emsg=string.Format("Sorry, can not handle RGB image with {0}={1}", "Color channels", colorchannels);
return false;
}
break;
case PHOTOMETRIC.SEPARATED:
{
TIFFGetFieldDefaulted(tif, TIFFTAG.INKSET, ap);
INKSET inkset=(INKSET)__GetAsUshort(ap, 0);
if(inkset!=INKSET.CMYK)
{
emsg=string.Format("Sorry, can not handle separated image with {0}={1}", "InkSet", inkset);
return false;
}
if(img.samplesperpixel<4)
{
emsg=string.Format("Sorry, can not handle separated image with {0}={1}", "Samples/pixel", img.samplesperpixel);
return false;
}
}
break;
case PHOTOMETRIC.LOGL:
if(compress!=COMPRESSION.SGILOG)
{
emsg=string.Format("Sorry, LogL data must have{0}={1}", "Compression", COMPRESSION.SGILOG);
return false;
}
TIFFSetField(tif, TIFFTAG.SGILOGDATAFMT, SGILOGDATAFMT._8BIT);
img.photometric=PHOTOMETRIC.MINISBLACK; // little white lie
img.bitspersample=8;
break;
case PHOTOMETRIC.LOGLUV:
if(compress!=COMPRESSION.SGILOG&&compress!=COMPRESSION.SGILOG24)
{
emsg=string.Format("Sorry, LogLuv data must have {0}={1} or {2}", "Compression", COMPRESSION.SGILOG, COMPRESSION.SGILOG24);
return false;
}
if(planarconfig!=PLANARCONFIG.CONTIG)
{
emsg=string.Format("Sorry, can not handle LogLuv images with {0}={1}", "Planarconfiguration", planarconfig);
return false;
}
TIFFSetField(tif, TIFFTAG.SGILOGDATAFMT, SGILOGDATAFMT._8BIT);
img.photometric=PHOTOMETRIC.RGB; // little white lie
img.bitspersample=8;
break;
case PHOTOMETRIC.CIELAB:
break;
default:
emsg=string.Format("Sorry, can not handle image with {0}={1}", photoTag, img.photometric);
return false;
}
img.Map=null;
img.BWmap=null;
img.PALmap=null;
img.ycbcr=null;
img.cielab=null;
TIFFGetField(tif, TIFFTAG.IMAGEWIDTH, ap); img.width=__GetAsUint(ap, 0);
TIFFGetField(tif, TIFFTAG.IMAGELENGTH, ap); img.height=__GetAsUint(ap, 0);
TIFFGetFieldDefaulted(tif, TIFFTAG.ORIENTATION, ap); img.orientation=(ORIENTATION)__GetAsUshort(ap, 0);
img.isContig=!(planarconfig==PLANARCONFIG.SEPARATE&&colorchannels>1);
if(img.isContig)
{
if(!PickContigCase(img))
{
emsg="Sorry, can not handle image";
return false;
}
}
else
{
if(!PickSeparateCase(img))
{
emsg="Sorry, can not handle image";
return false;
}
}
emsg="";
return true;
}
public static bool TIFFRGBAImageGet(TIFFRGBAImage img, uint[] raster, uint raster_offset, uint w, uint h)
{
if(img.get==null)
{
TIFFErrorExt(img.tif.tif_clientdata, TIFFFileName(img.tif), "No \"get\" routine setup");
return false;
}
if(img.contig==null&&img.separate==null)
{
TIFFErrorExt(img.tif.tif_clientdata, TIFFFileName(img.tif), "No \"put\" routine setupl; probably can not handle image format");
return false;
}
return img.get(img, raster, raster_offset, w, h);
}
// Read the specified image into an ABGR-format rastertaking in account
// specified orientation.
public static bool TIFFReadRGBAImageOriented(TIFF tif, uint rwidth, uint rheight, uint[] raster, ORIENTATION orientation, bool stop)
{
string emsg="";
TIFFRGBAImage img=new TIFFRGBAImage();
bool ok;
if(TIFFRGBAImageOK(tif, out emsg)&&TIFFRGBAImageBegin(img, tif, stop, out emsg))
{
img.req_orientation=orientation;
// XXX verify rwidth and rheight against width and height
ok=TIFFRGBAImageGet(img, raster, (rheight-img.height)*rwidth, rwidth, img.height);
TIFFRGBAImageEnd(img);
}
else
{
TIFFErrorExt(tif.tif_clientdata, TIFFFileName(tif), emsg);
ok=false;
}
return ok;
}
// Read the specified image into an ABGR-format raster. Use bottom left
// origin for raster by default.
public static bool TIFFReadRGBAImage(TIFF tif, uint rwidth, uint rheight, uint[] raster, bool stop)
{
return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster, ORIENTATION.BOTLEFT, stop);
}
static int setorientation(TIFFRGBAImage img)
{
switch(img.orientation)
{
case ORIENTATION.TOPLEFT:
case ORIENTATION.LEFTTOP:
if(img.req_orientation==ORIENTATION.TOPRIGHT||img.req_orientation==ORIENTATION.RIGHTTOP) return FLIP_HORIZONTALLY;
else if(img.req_orientation==ORIENTATION.BOTRIGHT||img.req_orientation==ORIENTATION.RIGHTBOT) return FLIP_HORIZONTALLY|FLIP_VERTICALLY;
else if(img.req_orientation==ORIENTATION.BOTLEFT||img.req_orientation==ORIENTATION.LEFTBOT) return FLIP_VERTICALLY;
else return 0;
case ORIENTATION.TOPRIGHT:
case ORIENTATION.RIGHTTOP:
if(img.req_orientation==ORIENTATION.TOPLEFT||img.req_orientation==ORIENTATION.LEFTTOP) return FLIP_HORIZONTALLY;
else if(img.req_orientation==ORIENTATION.BOTRIGHT||img.req_orientation==ORIENTATION.RIGHTBOT) return FLIP_VERTICALLY;
else if(img.req_orientation==ORIENTATION.BOTLEFT||img.req_orientation==ORIENTATION.LEFTBOT) return FLIP_HORIZONTALLY|FLIP_VERTICALLY;
else return 0;
case ORIENTATION.BOTRIGHT:
case ORIENTATION.RIGHTBOT:
if(img.req_orientation==ORIENTATION.TOPLEFT||img.req_orientation==ORIENTATION.LEFTTOP) return FLIP_HORIZONTALLY|FLIP_VERTICALLY;
else if(img.req_orientation==ORIENTATION.TOPRIGHT||img.req_orientation==ORIENTATION.RIGHTTOP) return FLIP_VERTICALLY;
else if(img.req_orientation==ORIENTATION.BOTLEFT||img.req_orientation==ORIENTATION.LEFTBOT) return FLIP_HORIZONTALLY;
else return 0;
case ORIENTATION.BOTLEFT:
case ORIENTATION.LEFTBOT:
if(img.req_orientation==ORIENTATION.TOPLEFT||img.req_orientation==ORIENTATION.LEFTTOP) return FLIP_VERTICALLY;
else if(img.req_orientation==ORIENTATION.TOPRIGHT||img.req_orientation==ORIENTATION.RIGHTTOP) return FLIP_HORIZONTALLY|FLIP_VERTICALLY;
else if(img.req_orientation==ORIENTATION.BOTRIGHT||img.req_orientation==ORIENTATION.RIGHTBOT) return FLIP_HORIZONTALLY;
else return 0;
default: return 0; // NOTREACHED
}
}
// Get an tile-organized image that has
// PlanarConfiguration contiguous if SamplesPerPixel > 1
// or
// SamplesPerPixel == 1
static bool gtTileContig(TIFFRGBAImage img, uint[] raster, uint raster_offset, uint w, uint h)
{
TIFF tif=img.tif;
byte[] buf=null;
try
{
buf=new byte[TIFFTileSize(tif)];
}
catch
{
TIFFErrorExt(tif.tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
return false;
}
object[] ap=new object[2];
TIFFGetField(tif, TIFFTAG.TILEWIDTH, ap); uint tw=__GetAsUint(ap, 0);
TIFFGetField(tif, TIFFTAG.TILELENGTH, ap); uint th=__GetAsUint(ap, 0);
int flip=setorientation(img);
uint y;
int toskew;
if((flip&FLIP_VERTICALLY)!=0)
{
y=h-1;
toskew=-(int)(tw+w);
}
else
{
y=0;
toskew=-(int)(tw-w);
}
bool ret=true;
uint nrow;
tileContigRoutine put=img.contig;
for(uint row=0; row<h; row+=nrow)
{
uint rowstoread=(uint)(th-(row+img.row_offset)%th);
nrow=(row+rowstoread>h?h-row:rowstoread);
for(uint col=0; col<w; col+=tw)
{
if(TIFFReadTile(tif, buf, (uint)(col+img.col_offset), (uint)(row+img.row_offset), 0, 0)<0&&img.stoponerr)
{
ret=false;
break;
}
uint pos=(uint)(((row+img.row_offset)%th)*TIFFTileRowSize(tif));
if(col+tw>w)
{
// Tile is clipped horizontally. Calculate
// visible portion and skewing factors.
uint npix=w-col;
int fromskew=(int)(tw-npix);
put(img, raster, raster_offset+y*w+col, col, y, npix, nrow, fromskew, toskew+fromskew, buf, pos);
}
else
{
put(img, raster, raster_offset+y*w+col, col, y, tw, nrow, 0, toskew, buf, pos);
}
}
if((flip&FLIP_VERTICALLY)!=0) y-=nrow;
else y+=nrow;
}
buf=null;
if((flip&FLIP_HORIZONTALLY)!=0)
{
unsafe
{
fixed(uint* raster_=raster)
{
for(uint line=0; line<h; line++)
{
uint* left=raster_+raster_offset+(line*w);
uint* right=left+w-1;
while(left<right)
{
uint temp=*left;
*left=*right;
*right=temp;
left++; right--;
}
}
}
}
}
return ret;
}
// Get an tile-organized image that has
// SamplesPerPixel > 1
// PlanarConfiguration separated
// We assume that all such images are RGB.
static bool gtTileSeparate(TIFFRGBAImage img, uint[] raster, uint raster_offset, uint w, uint h)
{
TIFF tif=img.tif;
tileSeparateRoutine put=img.separate;
EXTRASAMPLE alpha=img.alpha;
byte[] p0, p1, p2, pa=null;
int tilesize=TIFFTileSize(tif);
try
{
p0=new byte[tilesize];
p1=new byte[tilesize];
p2=new byte[tilesize];
if(alpha!=0) pa=new byte[tilesize];
}
catch
{
TIFFErrorExt(tif.tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
return false;
}
object[] ap=new object[2];
TIFFGetField(tif, TIFFTAG.TILEWIDTH, ap); uint tw=__GetAsUint(ap, 0);
TIFFGetField(tif, TIFFTAG.TILELENGTH, ap); uint th=__GetAsUint(ap, 0);
int flip=setorientation(img);
int toskew;
uint y;
if((flip&FLIP_VERTICALLY)!=0)
{
y=h-1;
toskew=-(int)(tw+w);
}
else
{
y=0;
toskew=-(int)(tw-w);
}
uint pos;
bool ret=true;
uint nrow;
for(uint row=0; row<h; row+=nrow)
{
uint rowstoread=(uint)(th-(row+img.row_offset)%th);
nrow=(row+rowstoread>h?h-row:rowstoread);
for(uint col=0; col<w; col+=tw)
{
if(TIFFReadTile(tif, p0, (uint)(col+img.col_offset), (uint)(row+img.row_offset), 0, 0)<0&&img.stoponerr)
{
ret=false;
break;
}
if(TIFFReadTile(tif, p1, (uint)(col+img.col_offset), (uint)(row+img.row_offset), 0, 1)<0&&img.stoponerr)
{
ret=false;
break;
}
if(TIFFReadTile(tif, p2, (uint)(col+img.col_offset), (uint)(row+img.row_offset), 0, 2)<0&&img.stoponerr)
{
ret=false;
break;
}
if(alpha!=0)
{
if(TIFFReadTile(tif, pa, (uint)(col+img.col_offset), (uint)(row+img.row_offset), 0, 3)<0&&img.stoponerr)
{
ret=false;
break;
}
}
pos=(uint)(((row+img.row_offset)%th)*TIFFTileRowSize(tif));
if(col+tw>w)
{
// Tile is clipped horizontally. Calculate
// visible portion and skewing factors.
uint npix=w-col;
int fromskew=(int)(tw-npix);
put(img, raster, raster_offset+y*w+col, col, y, npix, nrow, fromskew, toskew+fromskew, p0, p1, p2, pa, pos);
}
else put(img, raster, raster_offset+y*w+col, col, y, tw, nrow, 0, toskew, p0, p1, p2, pa, pos);
}
if((flip&FLIP_VERTICALLY)!=0) y-=nrow;
else y+=nrow;
}
if((flip&FLIP_HORIZONTALLY)!=0)
{
unsafe
{
fixed(uint* raster_=raster)
{
for(uint line=0; line<h; line++)
{
uint* left=raster_+raster_offset+(line*w);
uint* right=left+w-1;
while(left<right)
{
uint temp=*left;
*left=*right;
*right=temp;
left++; right--;
}
}
}
}
}
return ret;
}
// Get a strip-organized image that has
// PlanarConfiguration contiguous if SamplesPerPixel > 1
// or
// SamplesPerPixel == 1
static bool gtStripContig(TIFFRGBAImage img, uint[] raster, uint raster_offset, uint w, uint h)
{
TIFF tif=img.tif;
byte[] buf;
try
{
buf=new byte[TIFFStripSize(tif)];
}
catch
{
TIFFErrorExt(tif.tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
return false;
}
int flip=setorientation(img);
int toskew;
uint y;
if((flip&FLIP_VERTICALLY)!=0)
{
y=h-1;
toskew=-(int)(w+w);
}
else
{
y=0;
toskew=-(int)(w-w);
}
object[] ap=new object[2];
TIFFGetFieldDefaulted(tif, TIFFTAG.ROWSPERSTRIP, ap); uint rowsperstrip=__GetAsUint(ap, 0);
TIFFGetFieldDefaulted(tif, TIFFTAG.YCBCRSUBSAMPLING, ap);
ushort subsamplinghor=__GetAsUshort(ap, 0);
ushort subsamplingver=__GetAsUshort(ap, 1);
tileContigRoutine put=img.contig;
uint nrow;
bool ret=true;
int scanline=TIFFNewScanlineSize(tif);
uint imagewidth=img.width;
int fromskew=(int)(w<imagewidth?imagewidth-w:0);
for(uint row=0; row<h; row+=nrow)
{
uint rowstoread=(uint)(rowsperstrip-(row+img.row_offset)%rowsperstrip);
nrow=(row+rowstoread>h?h-row:rowstoread);
uint nrowsub=nrow;
if((nrowsub%subsamplingver)!=0) nrowsub+=subsamplingver-nrowsub%subsamplingver;
if(TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, (uint)(row+img.row_offset), 0), buf, (int)(((row+img.row_offset)%rowsperstrip+nrowsub)*scanline))<0&&img.stoponerr)
{
ret=false;
break;
}
uint pos=(uint)(((row+img.row_offset)%rowsperstrip)*scanline);
put(img, raster, raster_offset+y*w, 0, y, w, nrow, fromskew, toskew, buf, pos);
if((flip&FLIP_VERTICALLY)!=0) y-=nrow;
else y+=nrow;
}
if((flip&FLIP_HORIZONTALLY)!=0)
{
unsafe
{
fixed(uint* raster_=raster)
{
for(uint line=0; line<h; line++)
{
uint* left=raster_+raster_offset+(line*w);
uint* right=left+w-1;
while(left<right)
{
uint temp=*left;
*left=*right;
*right=temp;
left++; right--;
}
}
}
}
}
return ret;
}
// Get a strip-organized image with
// SamplesPerPixel > 1
// PlanarConfiguration separated
// We assume that all such images are RGB.
static bool gtStripSeparate(TIFFRGBAImage img, uint[] raster, uint raster_offset, uint w, uint h)
{
TIFF tif=img.tif;
tileSeparateRoutine put=img.separate;
EXTRASAMPLE alpha=img.alpha;
byte[] p0, p1, p2, pa=null;
int stripsize=TIFFStripSize(tif);
try
{
p0=new byte[stripsize];
p1=new byte[stripsize];
p2=new byte[stripsize];
if(alpha!=0) pa=new byte[stripsize];
}
catch
{
TIFFErrorExt(tif.tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
return false;
}
int flip=setorientation(img);
int toskew;
uint y;
if((flip&FLIP_VERTICALLY)!=0)
{
y=h-1;
toskew=-(int)(w+w);
}
else
{
y=0;
toskew=-(int)(w-w);
}
object[] ap=new object[2];
TIFFGetFieldDefaulted(tif, TIFFTAG.ROWSPERSTRIP, ap); uint rowsperstrip=__GetAsUint(ap, 0);
uint nrow;
uint imagewidth=img.width;
bool ret=true;
int scanline=TIFFScanlineSize(tif);
int fromskew=(int)(w<imagewidth?imagewidth-w:0);
for(uint row=0; row<h; row+=nrow)
{
uint rowstoread=(uint)(rowsperstrip-(row+img.row_offset)%rowsperstrip);
nrow=(row+rowstoread>h?h-row:rowstoread);
uint offset_row=(uint)(row+img.row_offset);
if(TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0), p0, (int)((row+img.row_offset)%rowsperstrip+nrow)*scanline)<0&&img.stoponerr)
{
ret=false;
break;
}
if(TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1, (int)((row+img.row_offset)%rowsperstrip+nrow)*scanline)<0&&img.stoponerr)
{
ret=false;
break;
}
if(TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2, (int)((row+img.row_offset)%rowsperstrip+nrow)*scanline)<0&&img.stoponerr)
{
ret=false;
break;
}
if(alpha!=0)
{
if(TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3), pa, (int)((row+img.row_offset)%rowsperstrip+nrow)*scanline)<0&&img.stoponerr)
{
ret=false;
break;
}
}
uint pos=(uint)(((row+img.row_offset)%rowsperstrip)*scanline);
put(img, raster, raster_offset+y*w, 0, y, w, nrow, fromskew, toskew, p0, p1, p2, pa, pos);
if((flip&FLIP_VERTICALLY)!=0) y-=nrow;
else y+=nrow;
}
if((flip&FLIP_HORIZONTALLY)!=0)
{
unsafe
{
fixed(uint* raster_=raster)
{
for(uint line=0; line<h; line++)
{
uint* left=raster_+raster_offset+(line*w);
uint* right=left+w-1;
while(left<right)
{
uint temp=*left;
*left=*right;
*right=temp;
left++; right--;
}
}
}
}
}
return ret;
}
// The following routines move decoded data returned
// from the TIFF library into rasters filled with packed
// ABGR pixels (i.e. suitable for passing to lrecwrite.)
//
// The routines have been created according to the most
// important cases and optimized. PickContigCase and
// PickSeparateCase analyze the parameters and select
// the appropriate "get" and "put" routine to use.
// 8-bit palette => colormap/RGB
static void put8bitcmaptile(TIFFRGBAImage img, uint[] cp0, uint cp_offset, uint x, uint y, uint w, uint h, int fromskew, int toskew, byte[] pp0, uint pp_offset)
{
uint[][] PALmap=img.PALmap;
int samplesperpixel=img.samplesperpixel;
unsafe
{
fixed(uint* cp_=cp0)
{
uint* cp=cp_+cp_offset;
fixed(byte* pp_=pp0)
{
byte* pp=pp_+pp_offset;
while((h--)>0)
{
for(x=w; (x--)>0; )
{
*cp++=PALmap[*pp][0];
pp+=samplesperpixel;
}
cp+=toskew;
pp+=fromskew;
}
}
}
}
}
// 4-bit palette => colormap/RGB
static void put4bitcmaptile(TIFFRGBAImage img, uint[] cp0, uint cp_offset, uint x, uint y, uint w, uint h, int fromskew, int toskew, byte[] pp0, uint pp_offset)
{
uint[][] PALmap=img.PALmap;
unsafe
{
fixed(uint* cp_=cp0)
{
uint* cp=cp_+cp_offset;
fixed(byte* pp_=pp0)
{
byte* pp=pp_+pp_offset;
fromskew/=2;
while((h--)>0)
{
uint _x;
for(_x=w; _x>=2; _x-=2)
{
uint[] bw=PALmap[*pp++];
*cp++=bw[0];
*cp++=bw[1];
}
if(_x>0)
{
uint[] bw=PALmap[*pp++];
*cp++=bw[0];
}
cp+=toskew;
pp+=fromskew;
}
}
}
}
}
// 2-bit palette => colormap/RGB
static void put2bitcmaptile(TIFFRGBAImage img, uint[] cp0, uint cp_offset, uint x, uint y, uint w, uint h, int fromskew, int toskew, byte[] pp0, uint pp_offset)
{
uint[][] PALmap=img.PALmap;