File: data.t

package info (click to toggle)
libimage-imlib2-perl 1.12-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 192 kB
  • ctags: 39
  • sloc: perl: 289; ansic: 112; makefile: 70
file content (96 lines) | stat: -rw-r--r-- 3,053 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -w
use strict;
use Test::More;

# This line comes from perlport.pod
my $AM_BIG_ENDIAN = unpack( 'h*', pack( 's', 1 ) ) =~ /01/ ? 1 : 0;

# $h must be divisible by 3
my $w = 4;
my $h = 6;
plan tests => 4 + 3 * $w * $h;    # ntests * dimensions

use_ok('Image::Imlib2');

ok( !Image::Imlib2->new_using_data( 16, 16 ), 'no data arg' );
ok( !Image::Imlib2->new_using_data( 16, 16, "0" x 16 ),
    'wrong length data arg' );
ok( Image::Imlib2->new_using_data( 16, 16, "0" x ( 4 * 16 * 16 ) ),
    'right length data arg' );

# Create two images with the same data.
# One is created with an array of packed pixels
# The other has a rectangle filled on it

# The images are three horizontal bands of different color, to test
# that the pixel order is right.

# Note: if any of the colors has a non-255 alpha, then this test fails
# unless the control image also uses new_using_data to clear itself
# first (all pixels to 0,0,0,0).  Reason: new_using_data overwrites the image while
# fill_rectangle blends with (255,0,0,0), giving a different result.

my $null = pack 'CCCC', 0, 0, 0, 0;

# First test has just opaque pixels.  Second has a translucent pixel
for my $test (
    {   blend  => 1,
        pixels => [
            [ 255, 255, 127, 0 ],     #ARGB
            [ 255, 127, 127, 127 ],
            [ 255, 0,   127, 255 ]
        ]
    },

    {   blend  => 1,
        pixels => [
            [ 255, 255, 127, 0 ],     #ARGB
            [ 127, 127, 127, 127 ],
            [ 255, 0,   127, 255 ]
        ]
    },

    {   blend  => 0,
        pixels => [
            [ 255, 255, 127, 0 ],     #ARGB
            [ 127, 127, 127, 127 ],
            [ 255, 0,   127, 255 ]
        ]
    },
    )
{
    Image::Imlib2->will_blend( $test->{blend} );

    my $pixels = $test->{pixels};
    my $alpha  = grep { $_->[0] != 255 } @$pixels;
    my @packed = map { pack 'CCCC', ($AM_BIG_ENDIAN ? @$_ : reverse @$_) } @$pixels;
    my $rect   = ( $packed[0] x ( $w * $h / 3 ) )
        . ( $packed[1] x ( $w * $h / 3 ) )
        . ( $packed[2] x ( $w * $h / 3 ) );
    my $data_image = Image::Imlib2->new_using_data( $w, $h, $rect );

    # If we have a non-opaque pixel, need to create a transparent image

    my $image = $alpha
        && $test->{blend}
        ? Image::Imlib2->new_using_data( $w, $h, $null x ( $w * $h ) )
        : Image::Imlib2->new( $w, $h );

    $image->set_color( @{ $pixels->[0] }[ 1 .. 3 ], $pixels->[0]->[0] )
        ;    # RGBA
    $image->fill_rectangle( 0, 0, $w, $h / 3 );
    $image->set_color( @{ $pixels->[1] }[ 1 .. 3 ], $pixels->[1]->[0] )
        ;    # RGBA
    $image->fill_rectangle( 0, $h / 3, $w, $h / 3 );
    $image->set_color( @{ $pixels->[2] }[ 1 .. 3 ], $pixels->[2]->[0] )
        ;    # RGBA
    $image->fill_rectangle( 0, 2 * $h / 3, $w, $h / 3 );

    for my $x ( 0 .. $w - 1 ) {
        for my $y ( 0 .. $h - 1 ) {
            my @p1 = $data_image->query_pixel( $x, $y );
            my @p2 = $image->query_pixel( $x,      $y );
            is_deeply( \@p1, \@p2, "$x,$y" );
        }
    }
}