File: 5_filter.t

package info (click to toggle)
libgtk3-imageview-perl 12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 1,160; makefile: 4
file content (114 lines) | stat: -rw-r--r-- 2,621 bytes parent folder | download | duplicates (2)
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
use warnings;
use strict;
use File::Temp;
use Image::Magick;
use Test::More tests => 10;
use MIME::Base64;

BEGIN {
    use Glib qw/TRUE FALSE/;
    use Gtk3 -init;
    use_ok('Gtk3::ImageView');
    Glib::Object::Introspection->setup(
        basename => 'GdkX11',
        version  => '3.0',
        package  => 'Gtk3::GdkX11',
    );
}

my $window = Gtk3::Window->new('toplevel');
$window->set_size_request( 300, 200 );
my $view = Gtk3::ImageView->new;
$view->set_pixbuf( Gtk3::Gdk::Pixbuf->new_from_file('t/2color.svg'), TRUE );

$window->add($view);
$window->show_all;
my $xid = $window->get_window->get_xid;
$view->set_zoom(15);

$view->set_interpolation('bilinear');
my $image = Image::Magick->new( magick => 'png' );

Glib::Timeout->add(
    1000,
    sub {
        $image->Read("x:$xid");
        Gtk3::main_quit;
        return FALSE;
    }
);
Gtk3::main;

diag('PNG of the blurred window:');
diag( encode_base64( $image->ImageToBlob ) );

my $x      = $image->Get('width') / 2;
my $y      = $image->Get('height') / 2;
my @middle = $image->GetPixel( x => $x, y => $y );
is_deeply( \@middle, [ 1, 0, 0 ], 'middle pixel should be red' );

my $found;
my @pixel;

$found = 0;
while ( $x > 0 ) {
    @pixel = $image->GetPixel( x => $x, y => $y );
    if ( join( ',', @pixel ) ne '1,0,0' ) {
        $found = 1;
        last;
    }
    $x--;
}
is( $found, 1, 'there is non-red outside' );
my $blurred_x = $x;

$found = 0;
while ( $x > 0 ) {
    @pixel = $image->GetPixel( x => $x, y => $y );
    if ( join( ',', @pixel ) eq '0,0,1' ) {
        $found = 1;
        last;
    }
    $x--;
}
is( $found, 1, 'there is blue outside' );
my $fullblue_x = $x;
cmp_ok( $fullblue_x, '<', $blurred_x );

$view->set_interpolation('nearest');
$image = Image::Magick->new( magick => 'png' );

Glib::Timeout->add(
    1000,
    sub {
        $image->Read("x:$xid");
        Gtk3::main_quit;
        return FALSE;
    }
);
Gtk3::main;

diag('PNG of the crisp window:');
diag( encode_base64( $image->ImageToBlob ) );

@pixel = $image->GetPixel( x => $fullblue_x, y => $y );
is_deeply( \@pixel, [ 0, 0, 1 ], 'blue pixel should still be blue' );

$found = 0;
while ( $x <= $blurred_x ) {
    @pixel = $image->GetPixel( x => $x, y => $y );
    if ( join( ',', @pixel ) ne '0,0,1' ) {
        $found = 1;
        last;
    }
    $x++;
}
is( $found, 1, 'there is non-blue inside' );
is_deeply(
    \@pixel,
    [ 1, 0, 0 ],
    'red pixel should be immediatelly near blue one'
);

cmp_ok( $fullblue_x, '<', $x, 'sharp edge should be within blurred edge (1)' );
cmp_ok( $x, '<', $blurred_x,  'sharp edge should be within blurred edge (2)' );