File: 1.t

package info (click to toggle)
libgraphics-tiff-perl 7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 320 kB
  • sloc: perl: 3,777; ansic: 4; makefile: 3
file content (208 lines) | stat: -rw-r--r-- 6,408 bytes parent folder | download
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
use warnings;
use strict;
use Graphics::TIFF ':all';
use Test::More tests => 49;
use Test::Deep;
BEGIN { use_ok('Graphics::TIFF') }

#########################

like( Graphics::TIFF->GetVersion, qr/LIBTIFF, Version/, 'version string' );

my $version = Graphics::TIFF->get_version_scalar;
isnt $version, undef, 'version';

if ( $version < 4.000003 ) {
    plan skip_all => 'libtiff 4.0.3 or better required';
    exit;
}

ok( Graphics::TIFF->IsCODECConfigured(COMPRESSION_DEFLATE),
    'IsCODECConfigured' );

eval "use Image::Magick";
SKIP: {
    skip 'Image::Magick not installed', 36 if $@;

    my $image = Image::Magick->new;
    $image->Read('rose:');
    $image->Set( density => '72x72' );
    $image->Write('test.tif');

    my $tif = Graphics::TIFF->Open( 'test.tif', 'r' );
    is( $tif->FileName, 'test.tif', 'FileName' );
    isa_ok $tif, 'Graphics::TIFF';
    can_ok $tif, qw(Close ReadDirectory GetField);

    is( $tif->ReadDirectory, 0, 'ReadDirectory' );

    is( $tif->ReadEXIFDirectory(0), 0, 'ReadEXIFDirectory' );

    is( $tif->NumberOfDirectories, 1, 'NumberOfDirectories' );

    is( $tif->SetDirectory(0), 1, 'SetDirectory' );

    is( $tif->SetSubDirectory(0), 0, 'SetSubDirectory' );

    is( $tif->GetField(TIFFTAG_FILLORDER),
        FILLORDER_MSB2LSB, 'GetField uint16' );
    is( $tif->GetField(TIFFTAG_XRESOLUTION), 72, 'GetField float' );
    my @counts = $tif->GetField(TIFFTAG_PAGENUMBER);
    is_deeply( \@counts, [ 0, 1 ], 'GetField 2 uint16' );
    @counts = $tif->GetField(TIFFTAG_STRIPBYTECOUNTS);
    is_deeply( \@counts, [ 8190, 1470 ], 'GetField array of uint64' );
    is( $tif->GetField(TIFFTAG_IMAGEWIDTH), 70, 'GetField uint32' );

    @counts = $tif->GetField(TIFFTAG_PRIMARYCHROMATICITIES);
    my @expected = (
        0.639999985694885, 0.330000013113022,
        0.300000011920929, 0.600000023841858,
        0.150000005960464, 0.0599999986588955
    );
    for my $i ( 0 .. $#expected ) {
        cmp_deeply(
            $counts[$i],
            num( $expected[$i], 0.0001 ),
            'GetField TIFFTAG_PRIMARYCHROMATICITIES (array of float)'
        );
    }

    @counts = $tif->GetField(TIFFTAG_WHITEPOINT);
    @expected = ( 0.312700003385544, 0.328999996185303 );
    for my $i ( 0 .. $#expected ) {
        cmp_deeply(
            $counts[$i],
            num( $expected[$i], 0.0001 ),
            'GetField TIFFTAG_WHITEPOINT (array of float)'
        );
    }

    is( $tif->GetFieldDefaulted(TIFFTAG_FILLORDER),
        FILLORDER_MSB2LSB, 'GetFieldDefaulted uint16' );

    is( $tif->SetField( TIFFTAG_FILLORDER, FILLORDER_LSB2MSB ),
        1, 'SetField status' );
    is( $tif->GetField(TIFFTAG_FILLORDER),
        FILLORDER_LSB2MSB, 'SetField result' );
    $tif->SetField( TIFFTAG_FILLORDER, FILLORDER_MSB2LSB );    # reset

    is( $tif->IsTiled, 0, 'IsTiled' );

    is( $tif->ScanlineSize, 210, 'ScanlineSize' );

    is( $tif->StripSize, 8190, 'StripSize' );

    is( $tif->NumberOfStrips, 2, 'NumberOfStrips' );

    is( $tif->TileSize, 8190, 'TileSize' );

    is( $tif->TileRowSize, 210, 'TileRowSize' );

    is( $tif->ComputeStrip( 16, 0 ), 0, 'ComputeStrip' );

    is( length( $tif->ReadEncodedStrip( 0, 8190 ) ),
        8190, 'ReadEncodedStrip full strip' );

    is( length( $tif->ReadEncodedStrip( 1, 8190 ) ),
        1470, 'ReadEncodedStrip part strip' );

    is( length( $tif->ReadRawStrip( 1, 20 ) ), 20, 'ReadRawStrip' );

    is( $tif->ReadTile( 0, 0, 0, 0 ), undef, 'ReadTile' );

    my $filename = 'out.txt';
    open my $fh, '>', $filename;
    $tif->PrintDirectory( $fh, 0 );
    $tif->Close;
    close $fh;
    is( -s $filename, 449, 'PrintDirectory' );
    unlink $filename;
}

#########################

eval "use Image::Magick";
SKIP: {
    skip 'Image::Magick or tiffcmp not installed', 1
      if ( $@ or system("which tiffinfo > /dev/null 2> /dev/null") != 0 );

    my $tif = Graphics::TIFF->Open( 'test.tif',  'r' );
    my $out = Graphics::TIFF->Open( 'test2.tif', 'w' );
    for my $tag (
        ( TIFFTAG_IMAGEWIDTH, TIFFTAG_IMAGELENGTH,
            TIFFTAG_SAMPLESPERPIXEL, TIFFTAG_BITSPERSAMPLE,
            TIFFTAG_ORIENTATION,     TIFFTAG_PLANARCONFIG,
            TIFFTAG_PAGENUMBER,      TIFFTAG_PHOTOMETRIC,
            TIFFTAG_ROWSPERSTRIP,    TIFFTAG_FILLORDER,
            TIFFTAG_RESOLUTIONUNIT,  TIFFTAG_XRESOLUTION,
            TIFFTAG_YRESOLUTION
        )
      )
    {
        my @values = $tif->GetField($tag);
        $out->SetField( $tag, @values );
    }

    my $stripsize = $tif->StripSize;
    for my $stripnum ( 0 .. $tif->NumberOfStrips - 1 ) {
        my $buffer = $tif->ReadEncodedStrip( $stripnum, $stripsize );
        $out->WriteEncodedStrip( $stripnum, $buffer, length($buffer) );
    }
    $out->WriteDirectory;
    $tif->Close;
    $out->Close;

    is( `tiffcmp test.tif test2.tif`, '', 'tiffcmp' );
}

#########################

eval "use Image::Magick";
SKIP: {
    skip 'Image::Magick not installed', 2 if $@;

    my $image = Image::Magick->new;
    $image->Read('rose:');
    $image->Set( density => '72x72', alpha => 'Set' );
    $image->Write('test.tif');
    my $tif = Graphics::TIFF->Open( 'test.tif', 'r' );

    my @values = $tif->GetField(TIFFTAG_EXTRASAMPLES);
    is_deeply( \@values, [EXTRASAMPLE_UNASSALPHA],
        'GetField TIFFTAG_EXTRASAMPLES' );

    @values = $tif->GetFieldDefaulted(TIFFTAG_EXTRASAMPLES);
    is_deeply( \@values, [EXTRASAMPLE_UNASSALPHA],
        'GetFieldDefaulted TIFFTAG_EXTRASAMPLES' );

    $tif->Close;
}

#########################

eval "use Image::Magick";
SKIP: {
    skip 'Image::Magick not installed', 6 if $@;

    my $image = Image::Magick->new;
    $image->Read('rose:');
    $image->Set( density => '72x72', type => 'palette', depth => 2 );
    $image->Write('test.tif');
    my $tif = Graphics::TIFF->Open( 'test.tif', 'r' );

    my @values = $tif->GetField(TIFFTAG_COLORMAP);
    is $#{ $values[0] }, 255, 'GetField TIFFTAG_COLORMAP r';
    is $#{ $values[1] }, 255, 'GetField TIFFTAG_COLORMAP g';
    is $#{ $values[2] }, 255, 'GetField TIFFTAG_COLORMAP b';

    @values = $tif->GetFieldDefaulted(TIFFTAG_COLORMAP);
    is $#{ $values[0] }, 255, 'GetFieldDefaulted TIFFTAG_COLORMAP r';
    is $#{ $values[1] }, 255, 'GetFieldDefaulted TIFFTAG_COLORMAP g';
    is $#{ $values[2] }, 255, 'GetFieldDefaulted TIFFTAG_COLORMAP b';

    $tif->Close;
}

#########################

unlink 'test.tif', 'test2.tif';