[9f824a]: src / foreign / vigra / vigra_impex / tiff.cxx  Maximize  Restore  History

Download this file

993 lines (855 with data), 33.8 kB

  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
/************************************************************************/
/* */
/* Copyright 2001-2002 by Gunnar Kedenburg */
/* Cognitive Systems Group, University of Hamburg, Germany */
/* */
/* This file is part of the VIGRA computer vision library. */
/* ( Version 1.5.0, Dec 07 2006 ) */
/* The VIGRA Website is */
/* http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* koethe@informatik.uni-hamburg.de or */
/* vigra@kogs1.informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
/* Modifications by Pablo d'Angelo
* updated to vigra 1.4 by Douglas Wilkins
* as of 18 Febuary 2006:
* - Added UINT16 and UINT32 pixel types.
* - Added support for obtaining extra bands beyond RGB.
* - Added support for a position field that indicates the start of this
* image relative to some global origin.
* - Added support for x and y resolution fields.
*
* Modification by Andrew Mihal, 27 October 2004:
* updated to vigra 1.4 by Douglas Wilkins
* as of 18 Febuary 2006:
* - Modified encoder to better estimate the number of rows per strip.
* - Modified decoder to use the scanline interface - the strip-based
* interface hogs memory when the rows/strip value is large.
* - Added support for ICC profiles
* Andrew Mihal's modifications are covered by the VIGRA license.
*/
#include <hugin_config.h>
#ifdef HasTIFF
#ifdef _MSC_VER
// NB (jbeda): tiffio.h is going to include this anyway. Let's include
// it now so that we can control how it comes in. Namely, we want
// to get our version that doesn't set the evil min/max macros.
#include "vigra/windows.h"
#endif
#include "vigra/sized_int.hxx"
#include "error.hxx"
#include "tiff.hxx"
#include <iostream>
#include <iomanip>
#include <sstream>
extern "C"
{
#include <tiff.h>
#include <tiffio.h>
#include <tiffvers.h>
}
namespace vigra {
CodecDesc TIFFCodecFactory::getCodecDesc() const
{
CodecDesc desc;
// init file type
desc.fileType = "TIFF";
// init pixel types
desc.pixelTypes.resize(9);
desc.pixelTypes[0] = "BILEVEL";
desc.pixelTypes[1] = "UINT8";
desc.pixelTypes[2] = "INT8";
desc.pixelTypes[3] = "UINT16";
desc.pixelTypes[4] = "INT16";
desc.pixelTypes[5] = "UINT32";
desc.pixelTypes[6] = "INT32";
desc.pixelTypes[7] = "FLOAT";
desc.pixelTypes[8] = "DOUBLE";
// init compression types
desc.compressionTypes.resize(6);
desc.compressionTypes[0] = "NONE";
desc.compressionTypes[1] = "RLE";
desc.compressionTypes[2] = "PACKBITS";
desc.compressionTypes[3] = "JPEG";
desc.compressionTypes[4] = "LZW";
desc.compressionTypes[5] = "DEFLATE";
// init magic strings
#if TIFFLIB_VERSION > 20070712
desc.magicStrings.resize(3);
#else
desc.magicStrings.resize(2);
#endif
desc.magicStrings[0].resize(4);
desc.magicStrings[0][0] = '\115';
desc.magicStrings[0][1] = '\115';
desc.magicStrings[0][2] = '\000';
desc.magicStrings[0][3] = '\052';
desc.magicStrings[1].resize(4);
desc.magicStrings[1][0] = '\111';
desc.magicStrings[1][1] = '\111';
desc.magicStrings[1][2] = '\052';
desc.magicStrings[1][3] = '\000';
#if TIFFLIB_VERSION > 20070712
// magic for bigtiff
desc.magicStrings[2].resize(4);
desc.magicStrings[2][0] = '\111';
desc.magicStrings[2][1] = '\111';
desc.magicStrings[2][2] = '\053';
desc.magicStrings[2][3] = '\000';
#endif
// init file extensions
desc.fileExtensions.resize(2);
desc.fileExtensions[0] = "tif";
desc.fileExtensions[1] = "tiff";
desc.bandNumbers.resize(4);
desc.bandNumbers[0] = 1;
desc.bandNumbers[1] = 2;
desc.bandNumbers[2] = 3;
desc.bandNumbers[3] = 4;
return desc;
}
std::auto_ptr<Decoder> TIFFCodecFactory::getDecoder() const
{
return std::auto_ptr<Decoder>( new TIFFDecoder() );
}
std::auto_ptr<Encoder> TIFFCodecFactory::getEncoder() const
{
return std::auto_ptr<Encoder>( new TIFFEncoder() );
}
class TIFFCodecImpl
{
protected:
std::string pixeltype;
TIFF * tiff;
tdata_t * stripbuffer;
tstrip_t strip;
// mihal 27-10-2004: use scanline interface
unsigned int scanline;
uint32 stripindex, stripheight;
uint32 width, height;
uint16 samples_per_pixel, bits_per_sample,
photometric, planarconfig, fillorder, extra_samples_per_pixel;
float x_resolution, y_resolution;
Diff2D position;
Size2D canvasSize;
Decoder::ICCProfile iccProfile;
public:
TIFFCodecImpl();
~TIFFCodecImpl();
};
TIFFCodecImpl::TIFFCodecImpl()
: pixeltype("undefined")
{
tiff = 0;
stripbuffer = 0;
strip = 0;
// mihal 27-10-2004: use scanline interface
scanline = 0;
stripindex = 0;
planarconfig = PLANARCONFIG_CONTIG;
x_resolution = 0;
y_resolution = 0;
extra_samples_per_pixel = 0;
}
TIFFCodecImpl::~TIFFCodecImpl()
{
if ( planarconfig == PLANARCONFIG_SEPARATE ) {
if ( stripbuffer != 0 ) {
for( unsigned int i = 0; i < samples_per_pixel; ++i )
if ( stripbuffer[i] != 0 )
_TIFFfree(stripbuffer[i]);
delete[] stripbuffer;
}
} else {
if ( stripbuffer != 0 ) {
if ( stripbuffer[0] != 0 )
_TIFFfree(stripbuffer[0]);
delete[] stripbuffer;
}
}
if ( tiff != 0 )
TIFFClose(tiff);
}
class TIFFDecoderImpl : public TIFFCodecImpl
{
friend class TIFFDecoder;
std::string get_pixeltype_by_sampleformat() const;
std::string get_pixeltype_by_datatype() const;
public:
TIFFDecoderImpl( const std::string & filename );
void init();
const void * currentScanlineOfBand( unsigned int band ) const;
void nextScanline();
};
TIFFDecoderImpl::TIFFDecoderImpl( const std::string & filename )
{
tiff = TIFFOpen( filename.c_str(), "r" );
if ( !tiff ) {
std::string msg("Unable to open file '");
msg += filename;
msg += "'.";
vigra_precondition(0, msg.c_str());
}
}
std::string TIFFDecoderImpl::get_pixeltype_by_sampleformat() const
{
uint16 sampleformat;
if ( TIFFGetField( tiff, TIFFTAG_SAMPLEFORMAT, &sampleformat ) ) {
switch (sampleformat) {
case SAMPLEFORMAT_UINT:
// added by dangelo, support for UINT 16 & 32 bit
switch (bits_per_sample) {
case 8:
return "UINT8";
case 16:
return "UINT16";
case 32:
return "UINT32";
}
break;
case SAMPLEFORMAT_INT:
switch (bits_per_sample) {
case 8:
return "INT8";
case 16:
return "INT16";
case 32:
return "INT32";
}
break;
case SAMPLEFORMAT_IEEEFP:
switch (bits_per_sample) {
case 32:
return "FLOAT";
case 64:
return "DOUBLE";
}
break;
}
}
return "undefined";
}
std::string TIFFDecoderImpl::get_pixeltype_by_datatype() const
{
uint16 datatype;
if ( TIFFGetField( tiff, TIFFTAG_DATATYPE, &datatype ) ) {
// dangelo: correct parsing of INT/UINT (given in tiff.h)
switch (datatype) {
case TIFF_BYTE:
return "UINT8";
case TIFF_SBYTE:
return "INT8";
case TIFF_SHORT:
return "UINT16";
case TIFF_SSHORT:
return "INT16";
case TIFF_LONG:
return "UINT32";
case TIFF_SLONG:
return "INT32";
case TIFF_FLOAT:
return "FLOAT";
case TIFF_DOUBLE:
return "DOUBLE";
}
}
return "undefined";
}
void TIFFDecoderImpl::init()
{
// read width and height
TIFFGetField( tiff, TIFFTAG_IMAGEWIDTH, &width );
TIFFGetField( tiff, TIFFTAG_IMAGELENGTH, &height );
// find out strip heights
// mihal 27-10-2004: use scanline interface instead of strip interface
//if ( !TIFFGetField( tiff, TIFFTAG_ROWSPERSTRIP, &stripheight ) )
// stripheight = height;
stripheight = 1;
// get samples_per_pixel
samples_per_pixel = 0;
extra_samples_per_pixel = 0;
if ( !TIFFGetFieldDefaulted( tiff, TIFFTAG_SAMPLESPERPIXEL,
&samples_per_pixel ) )
vigra_fail( "TIFFDecoderImpl::init(): Samples per pixel not set."
" A suitable default was not found." );
// read extra samples (# of alpha channels)
uint16 *extra_sample_types=0;
if (TIFFGetField( tiff, TIFFTAG_EXTRASAMPLES,
&extra_samples_per_pixel, &extra_sample_types )!=1)
{
extra_samples_per_pixel=0;
}
if (extra_samples_per_pixel > 0) {
for (int i=0; i< extra_samples_per_pixel; i++) {
if (extra_sample_types[i] == EXTRASAMPLE_ASSOCALPHA)
{
std::cerr << "WARNING: TIFFDecoderImpl::init(): associated alpha treated"
" as unassociated alpha!" << std::endl;
}
}
}
// get photometric
if ( !TIFFGetFieldDefaulted( tiff, TIFFTAG_PHOTOMETRIC,
&photometric ) )
vigra_fail( "TIFFDecoderImpl::init(): Photometric tag is not set."
" A suitable default was not found." );
// check photometric preconditions
if ( photometric == PHOTOMETRIC_MINISWHITE ||
photometric == PHOTOMETRIC_MINISBLACK ||
photometric == PHOTOMETRIC_PALETTE )
{
if ( samples_per_pixel - extra_samples_per_pixel != 1 )
vigra_fail("TIFFDecoderImpl::init():"
" Photometric tag does not fit the number of"
" samples per pixel." );
}
if ( photometric == PHOTOMETRIC_RGB )
{
if ( samples_per_pixel > 3 && extra_samples_per_pixel == 0 ) {
// file probably lacks the extra_samples tag
extra_samples_per_pixel = samples_per_pixel - 3;
}
if ( samples_per_pixel - extra_samples_per_pixel != 3 )
vigra_fail("TIFFDecoderImpl::init():"
" Photometric tag does not fit the number of"
" samples per pixel." );
}
// get planarconfig
if ( samples_per_pixel > 1 ) {
if ( !TIFFGetFieldDefaulted( tiff, TIFFTAG_PLANARCONFIG,
&planarconfig ) )
vigra_fail( "TIFFDecoderImpl::init(): Planarconfig is not"
" set. A suitable default was not found." );
}
// get bits per pixel
if ( !TIFFGetField( tiff, TIFFTAG_BITSPERSAMPLE, &bits_per_sample ) )
{
// dangelo: commented out
//std::cerr << "Warning: no TIFFTAG_BITSPERSAMPLE, using 8 bits per sample.\n";
bits_per_sample = 8;
}
// get pixeltype
if ( bits_per_sample != 1 ) {
// try the sampleformat tag
pixeltype = get_pixeltype_by_sampleformat();
if ( pixeltype == "undefined" ) {
// try the (obsolete) datatype tag
pixeltype = get_pixeltype_by_datatype();
if ( pixeltype == "undefined" ) {
// ERROR: no useable pixeltype found..
// imagemagick can write files without it..
// try to guess a suitable one here.
switch(bits_per_sample)
{
case 8:
pixeltype = "UINT8";
break;
case 16:
pixeltype = "UINT16";
break;
case 32:
pixeltype = "UINT32"; // prefer int over float
break;
case 64:
pixeltype = "DOUBLE";
break;
default:
vigra_fail( "TIFFDecoderImpl::init(): Sampleformat or Datatype tag undefined and guessing sampletype from Bits per Sample failed." );
break;
}
// dangelo commented out warnings
// std::cerr << "Warning: no TIFFTAG_SAMPLEFORMAT or TIFFTAG_DATATYPE, "
// "guessing pixeltype '" << pixeltype << "'.\n";
}
}
} else {
// if each sample is 1 bit long
pixeltype = "BILEVEL";
// get fillorder
if ( !TIFFGetField( tiff, TIFFTAG_FILLORDER, &fillorder ) )
fillorder = FILLORDER_MSB2LSB;
}
// other fields
uint16 u16value;
uint32 u32value;
float unitLength = 1;
if (TIFFGetField( tiff, TIFFTAG_RESOLUTIONUNIT, &u16value )) {
switch (u16value) {
case RESUNIT_NONE:
unitLength = 1;
break;
case RESUNIT_INCH:
unitLength = 1;
break;
case RESUNIT_CENTIMETER:
unitLength = 1/2.54;
break;
default:
vigra_fail("Unkown resolution unit");
}
}
float fvalue;
if (TIFFGetField( tiff, TIFFTAG_XRESOLUTION, &fvalue )) {
x_resolution = fvalue / unitLength;
}
if (TIFFGetField( tiff, TIFFTAG_YRESOLUTION, &fvalue )) {
y_resolution = fvalue / unitLength;
}
// XPosition
if (TIFFGetField( tiff, TIFFTAG_XPOSITION, &fvalue )) {
fvalue = fvalue * x_resolution;
position.x = (int)floor(fvalue + 0.5);
}
// YPosition
if (TIFFGetField( tiff, TIFFTAG_YPOSITION, &fvalue )) {
fvalue = fvalue * y_resolution;
position.y = (int)floor(fvalue + 0.5);
}
// canvas size
if (TIFFGetField( tiff, TIFFTAG_PIXAR_IMAGEFULLWIDTH, &u32value )) {
canvasSize.x = u32value;
}
if (TIFFGetField( tiff, TIFFTAG_PIXAR_IMAGEFULLLENGTH, &u32value )) {
canvasSize.y = u32value;
}
if ((uint32)canvasSize.x < position.x + width || (uint32)canvasSize.y < position.y + height)
{
//std::cerr << "Warning: invalid TIFFTAG_PIXAR_IMAGEFULLWIDTH/LENGTH tags" << std::endl;
canvasSize.x = canvasSize.y = 0;
}
// ICC Profile
UInt32 iccProfileLength = 0;
const unsigned char *iccProfilePtr = NULL;
if(TIFFGetField(tiff, TIFFTAG_ICCPROFILE,
&iccProfileLength, &iccProfilePtr)
&& iccProfileLength)
{
Decoder::ICCProfile iccData(
iccProfilePtr, iccProfilePtr + iccProfileLength);
iccProfile.swap(iccData);
}
// allocate data buffers
// mihal 27-10-2004: use scanline interface instead of strip interface
//const unsigned int stripsize = TIFFStripSize(tiff);
const unsigned int stripsize = TIFFScanlineSize(tiff);
if ( planarconfig == PLANARCONFIG_SEPARATE ) {
stripbuffer = new tdata_t[samples_per_pixel];
for( unsigned int i = 0; i < samples_per_pixel; ++i ) {
stripbuffer[i] = 0;
}
for( unsigned int i = 0; i < samples_per_pixel; ++i ) {
stripbuffer[i] = _TIFFmalloc(stripsize);
if(stripbuffer[i] == 0)
throw std::bad_alloc();
}
} else {
stripbuffer = new tdata_t[1];
stripbuffer[0] = 0;
stripbuffer[0] = _TIFFmalloc(stripsize);
if(stripbuffer[0] == 0)
throw std::bad_alloc();
}
// let the codec read a new strip
stripindex = stripheight;
}
const void *
TIFFDecoderImpl::currentScanlineOfBand( unsigned int band ) const
{
if ( bits_per_sample == 1 ) {
UInt8 * const buf
= static_cast< UInt8 * >(stripbuffer[0]);
// XXX probably wrong
return buf + ( stripindex * width ) / 8;
} else {
if ( planarconfig == PLANARCONFIG_SEPARATE ) {
UInt8 * const buf
= static_cast< UInt8 * >(stripbuffer[band]);
return buf + ( stripindex * width ) * ( bits_per_sample / 8 );
} else {
UInt8 * const buf
= static_cast< UInt8 * >(stripbuffer[0]);
return buf + ( band + stripindex * width * samples_per_pixel )
* ( bits_per_sample / 8 );
}
}
}
void TIFFDecoderImpl::nextScanline()
{
// eventually read a new strip
if ( ++stripindex >= stripheight ) {
stripindex = 0;
if ( planarconfig == PLANARCONFIG_SEPARATE ) {
// mihal 27-10-2004: modified to use scanline interface
//const tsize_t size = TIFFStripSize(tiff);
const tsize_t size = TIFFScanlineSize(tiff);
for( unsigned int i = 0; i < samples_per_pixel; ++i )
// mihal 27-10-2004: use scanline interface
//TIFFReadEncodedStrip( tiff, strip++, stripbuffer[i],
// size );
TIFFReadScanline(tiff, stripbuffer[i], scanline++, size);
} else {
// mihal 27-10-2004: modified to use scanline interface
//const tsize_t size = TIFFStripSize(tiff);
const tsize_t size = TIFFScanlineSize(tiff);
// mihal 27-10-2004: modified to use scanline interface
//TIFFReadEncodedStrip( tiff, strip++, stripbuffer[0], size );
TIFFReadScanline( tiff, stripbuffer[0], scanline++, size);
}
// XXX handle bilevel images
// invert grayscale images that interpret 0 as white
if ( samples_per_pixel == 1 && pixeltype == "UINT8" &&
photometric == PHOTOMETRIC_MINISWHITE ) {
UInt8 * buf = static_cast< UInt8 * >
(stripbuffer[0]);
// mihal 27-10-2004: modified to use scanline interface
//const unsigned int n = TIFFStripSize(tiff);
const unsigned int n = TIFFScanlineSize(tiff);
// invert every pixel
for ( unsigned int i = 0; i < n; ++i ) {
int x = *buf;
x = 0xff - x;
*buf++ = x;
}
}
}
}
void TIFFDecoder::init( const std::string & filename )
{
pimpl = new TIFFDecoderImpl(filename);
pimpl->init();
iccProfile_ = pimpl->iccProfile;
}
TIFFDecoder::~TIFFDecoder()
{
delete pimpl;
}
std::string TIFFDecoder::getFileType() const
{
return "TIFF";
}
unsigned int TIFFDecoder::getWidth() const
{
return pimpl->width;
}
unsigned int TIFFDecoder::getHeight() const
{
return pimpl->height;
}
unsigned int TIFFDecoder::getNumBands() const
{
return pimpl->samples_per_pixel;
}
unsigned int TIFFDecoder::getNumExtraBands() const
{
return pimpl->extra_samples_per_pixel;
}
vigra::Diff2D TIFFDecoder::getPosition() const
{
return pimpl->position;
}
vigra::Size2D TIFFDecoder::getCanvasSize() const
{
return pimpl->canvasSize;
}
std::string TIFFDecoder::getPixelType() const
{
return pimpl->pixeltype;
}
unsigned int TIFFDecoder::getOffset() const
{
return pimpl->planarconfig == PLANARCONFIG_SEPARATE ?
1 : pimpl->samples_per_pixel;
}
const void * TIFFDecoder::currentScanlineOfBand( unsigned int band ) const
{
return pimpl->currentScanlineOfBand(band);
}
void TIFFDecoder::nextScanline()
{
pimpl->nextScanline();
}
void TIFFDecoder::close() {}
void TIFFDecoder::abort() {}
// this encoder always writes interleaved tiff files
class TIFFEncoderImpl : public TIFFCodecImpl
{
friend class TIFFEncoder;
// attributes
unsigned short tiffcomp;
bool finalized;
public:
// ctor, dtor
TIFFEncoderImpl( const std::string & filename )
: tiffcomp(COMPRESSION_NONE), finalized(false)
{
tiff = TIFFOpen( filename.c_str(), "w" );
if (!tiff)
{
std::string msg("Unable to open file '");
msg += filename;
msg += "'.";
vigra_precondition( false, msg.c_str() );
}
planarconfig = PLANARCONFIG_CONTIG;
}
// methods
void setCompressionType( const std::string &, int );
void finalizeSettings();
void * currentScanlineOfBand( unsigned int band ) const
{
const unsigned int atomicbytes = bits_per_sample >> 3;
UInt8 * buf = ( UInt8 * ) stripbuffer[0];
return buf + atomicbytes *
( width * samples_per_pixel * stripindex + band );
}
void nextScanline()
{
// compute the number of rows in the current strip
unsigned int rows = ( strip + 1 ) * stripheight > height ?
height - strip * stripheight : stripheight;
if ( ++stripindex >= rows ) {
// write next strip
stripindex = 0;
int success = TIFFWriteEncodedStrip( tiff, strip++, stripbuffer[0],
TIFFVStripSize( tiff, rows ) );
if(success == -1 && tiffcomp != COMPRESSION_NONE)
{
throw Encoder::TIFFCompressionException(); // retry without compression
}
vigra_postcondition(success != -1,
"exportImage(): Unable to write TIFF data.");
}
}
};
void TIFFEncoderImpl::setCompressionType( const std::string & comp,
int quality = -1 )
{
// if any compression type is set that we do not support,
// the expected behavior is to do nothing
if ( comp == "NONE" )
tiffcomp = COMPRESSION_NONE;
else if ( ( comp == "JPEG" ) && ( quality != -1 ) )
tiffcomp = COMPRESSION_OJPEG;
else if ( comp == "RLE" || comp == "RunLength")
tiffcomp = COMPRESSION_CCITTRLE;
else if ( comp == "PACKBITS")
tiffcomp = COMPRESSION_PACKBITS;
else if ( comp == "LZW" )
tiffcomp = COMPRESSION_LZW;
else if ( comp == "DEFLATE" )
tiffcomp = COMPRESSION_DEFLATE;
}
void TIFFEncoderImpl::finalizeSettings()
{
// decide if we should write Grey, or RGB files
// all additional channels are treated as extra samples
if (samples_per_pixel < 3) {
extra_samples_per_pixel = samples_per_pixel - 1;
} else {
extra_samples_per_pixel = samples_per_pixel - 3;
}
// set some fields
TIFFSetField( tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
TIFFSetField( tiff, TIFFTAG_IMAGEWIDTH, width );
TIFFSetField( tiff, TIFFTAG_IMAGELENGTH, height );
// TIFFDefaultStripSize tries for 8kb strips! Laughable!
// This will do a 1MB strip for 8-bit images,
// 2MB strip for 16-bit, and so forth.
unsigned int estimate =
std::max(static_cast<UIntBiggest>(1),
(static_cast<UIntBiggest>(1)<<20) / (width * samples_per_pixel));
TIFFSetField( tiff, TIFFTAG_ROWSPERSTRIP,
stripheight = TIFFDefaultStripSize( tiff, estimate ) );
TIFFSetField( tiff, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel );
TIFFSetField( tiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT );
TIFFSetField( tiff, TIFFTAG_COMPRESSION, tiffcomp );
// subfile descriptor
TIFFSetField( tiff, TIFFTAG_SUBFILETYPE, 0);
// set pixel type
if ( pixeltype == "BILEVEL" ) {
// no sampleformat applies for bilevel
bits_per_sample = 1;
} else if ( pixeltype == "UINT8" ) {
TIFFSetField( tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT );
bits_per_sample = 8;
} else if ( pixeltype == "INT16" ) {
TIFFSetField( tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT );
bits_per_sample = 16;
} else if ( pixeltype == "UINT16" ) {
TIFFSetField( tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT );
bits_per_sample = 16;
} else if ( pixeltype == "INT32" ) {
TIFFSetField( tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT );
bits_per_sample = 32;
} else if ( pixeltype == "UINT32" ) {
TIFFSetField( tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT );
bits_per_sample = 32;
} else if ( pixeltype == "FLOAT" ) {
TIFFSetField( tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP );
bits_per_sample = 32;
} else if ( pixeltype == "DOUBLE" ) {
TIFFSetField( tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP );
bits_per_sample = 64;
}
TIFFSetField( tiff, TIFFTAG_BITSPERSAMPLE, bits_per_sample );
if (extra_samples_per_pixel > 0) {
uint16 * types = new uint16[extra_samples_per_pixel];
for ( int i=0; i < extra_samples_per_pixel; i++ ) {
types[i] = EXTRASAMPLE_UNASSALPHA;
}
TIFFSetField( tiff, TIFFTAG_EXTRASAMPLES, extra_samples_per_pixel,
types );
delete[] types;
}
// set photometric
if ( samples_per_pixel - extra_samples_per_pixel == 1 )
TIFFSetField( tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK );
else if ( samples_per_pixel - extra_samples_per_pixel == 3 )
TIFFSetField( tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB );
// set resolution
if ( x_resolution > 0) {
TIFFSetField( tiff, TIFFTAG_XRESOLUTION, x_resolution );
}
if ( y_resolution > 0 ) {
TIFFSetField( tiff, TIFFTAG_YRESOLUTION, y_resolution );
}
if (x_resolution > 0 || y_resolution > 0) {
TIFFSetField( tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH );
}
// save position, if available
if (position.x >= 0 && position.y >= 0 &&
x_resolution > 0 && y_resolution > 0)
{
TIFFSetField( tiff, TIFFTAG_XPOSITION, position.x / x_resolution);
TIFFSetField( tiff, TIFFTAG_YPOSITION, position.y / y_resolution);
}
if ((uint32)canvasSize.x >= position.x + width
&& (uint32)canvasSize.y >= position.y + height)
{
//std::cerr << "Setting canvas size: " << canvasSize << std::endl;
TIFFSetField( tiff, TIFFTAG_PIXAR_IMAGEFULLWIDTH, canvasSize.x);
TIFFSetField( tiff, TIFFTAG_PIXAR_IMAGEFULLLENGTH, canvasSize.y);
} else {
//std::cerr << "Invalid canvas size: " << canvasSize << std::endl;
}
// Set ICC profile, if available.
if (iccProfile.size()) {
TIFFSetField(tiff, TIFFTAG_ICCPROFILE,
iccProfile.size(), iccProfile.begin());
}
// alloc memory
stripbuffer = new tdata_t[1];
stripbuffer[0] = 0;
stripbuffer[0] = _TIFFmalloc( TIFFStripSize(tiff) );
if(stripbuffer[0] == 0)
throw std::bad_alloc();
finalized = true;
}
void TIFFEncoder::init( const std::string & filename )
{
pimpl = new TIFFEncoderImpl(filename);
}
TIFFEncoder::~TIFFEncoder()
{
delete pimpl;
}
std::string TIFFEncoder::getFileType() const
{
return "TIFF";
}
void TIFFEncoder::setWidth( unsigned int width )
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->width = width;
}
void TIFFEncoder::setHeight( unsigned int height )
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->height = height;
}
void TIFFEncoder::setNumBands( unsigned int bands )
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->samples_per_pixel = bands;
}
void TIFFEncoder::setCompressionType( const std::string & comp,
int quality )
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->setCompressionType( comp, quality );
}
void TIFFEncoder::setPixelType( const std::string & pixeltype )
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->pixeltype = pixeltype;
}
void TIFFEncoder::setPosition( const vigra::Diff2D & pos )
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->position = pos;
}
void TIFFEncoder::setCanvasSize( const vigra::Size2D & size )
{
//std::cerr << "TIFF: setting canvas size: " << size << std::endl;
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->canvasSize = size;
}
void TIFFEncoder::setXResolution( float xres )
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->x_resolution = xres;
}
void TIFFEncoder::setYResolution( float yres )
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->y_resolution = yres;
}
unsigned int TIFFEncoder::getOffset() const
{
return pimpl->samples_per_pixel;
}
void TIFFEncoder::finalizeSettings()
{
VIGRA_IMPEX_FINALIZED(pimpl->finalized);
pimpl->finalizeSettings();
}
void * TIFFEncoder::currentScanlineOfBand( unsigned int band )
{
return pimpl->currentScanlineOfBand(band);
}
void TIFFEncoder::nextScanline()
{
pimpl->nextScanline();
}
void TIFFEncoder::setICCProfile(const ICCProfile & data)
{
pimpl->iccProfile = data;
}
void TIFFEncoder::close() {}
void TIFFEncoder::abort() {}
}
#endif // HasTIFF

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks