]> git.imager.perl.org - imager.git/blob - t/t64copyflip.t
add format probes for SGI RGB, ILBM, XPM, PCX, FITS, Photoshop, EPS
[imager.git] / t / t64copyflip.t
1 #!perl -w
2 use strict;
3 use lib 't';
4 use Test::More tests => 61;
5 use Imager;
6
7 #$Imager::DEBUG=1;
8
9 Imager::init('log'=>'testout/t64copyflip.log');
10
11 my $img=Imager->new() or die "unable to create image object\n";
12
13 $img->open(file=>'testimg/scale.ppm',type=>'pnm');
14 my $nimg = $img->copy();
15 ok($nimg, "copy returned something");
16
17 # test if ->copy() works
18
19 my $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
20 is($diff, 0, "copy matches source");
21
22
23 # test if ->flip(dir=>'h')->flip(dir=>'h') doesn't alter the image
24
25 $nimg->flip(dir=>"h")->flip(dir=>"h");
26 $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
27 is($diff, 0, "double horiz flipped matches original");
28
29 # test if ->flip(dir=>'v')->flip(dir=>'v') doesn't alter the image
30
31 $nimg->flip(dir=>"v")->flip(dir=>"v");
32 $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
33 is($diff, 0, "double vertically flipped image matches original");
34
35
36 # test if ->flip(dir=>'h')->flip(dir=>'v') is same as ->flip(dir=>'hv')
37
38 $nimg->flip(dir=>"v")->flip(dir=>"h")->flip(dir=>"hv");;
39 $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
40 is($diff, 0, "check flip with hv matches flip v then flip h");
41
42 rot_test($img, 90, 4);
43 rot_test($img, 180, 2);
44 rot_test($img, 270, 4);
45 rot_test($img, 0, 1);
46
47 my $pimg = $img->to_paletted();
48 rot_test($pimg, 90, 4);
49 rot_test($pimg, 180, 2);
50 rot_test($pimg, 270, 4);
51 rot_test($pimg, 0, 1);
52
53 my $timg = $img->rotate(right=>90)->rotate(right=>270);
54 is(Imager::i_img_diff($img->{IMG}, $timg->{IMG}), 0,
55    "check rotate 90 then 270 matches original");
56 $timg = $img->rotate(right=>90)->rotate(right=>180)->rotate(right=>90);
57 is(Imager::i_img_diff($img->{IMG}, $timg->{IMG}), 0,
58      "check rotate 90 then 180 then 90 matches original");
59
60 # this could use more tests
61 my $rimg = $img->rotate(degrees=>10);
62 ok($rimg, "rotation by 10 degrees gave us an image");
63 if (!$rimg->write(file=>"testout/t64_rot10.ppm")) {
64   print "# Cannot save: ",$rimg->errstr,"\n";
65 }
66
67 # rotate with background
68 $rimg = $img->rotate(degrees=>10, back=>Imager::Color->new(builtin=>'red'));
69 ok($rimg, "rotate with background gave us an image");
70 if (!$rimg->write(file=>"testout/t64_rot10_back.ppm")) {
71   print "# Cannot save: ",$rimg->errstr,"\n";
72 }
73
74 {
75   # rotate with text background
76   my $rimg = $img->rotate(degrees => 45, back => '#FF00FF');
77   ok($rimg, "rotate with background as text gave us an image");
78   
79   # check the color set correctly
80   my $c = $rimg->getpixel(x => 0, 'y' => 0);
81   is_deeply([ 255, 0, 255 ], [ ($c->rgba)[0, 1, 2] ],
82             "check background set correctly");
83
84   # check error handling for background color
85   $rimg = $img->rotate(degrees => 45, back => "some really unknown color");
86   ok(!$rimg, "should fail due to bad back color");
87   cmp_ok($img->errstr, '=~', "^No color named ", "check error message");
88 }
89
90 my $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
91                                              0,   1, 0,
92                                              0,   0, 1]);
93 ok($trimg, "matrix_transform() returned an image");
94 $trimg->write(file=>"testout/t64_trans.ppm")
95   or print "# Cannot save: ",$trimg->errstr,"\n";
96
97 $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
98                                              0,   1, 0,
99                                              0,   0, 1],
100                                    back=>Imager::Color->new(builtin=>'blue'));
101 ok($trimg, "matrix_transform() with back returned an image");
102
103 $trimg->write(file=>"testout/t64_trans_back.ppm")
104   or print "# Cannot save: ",$trimg->errstr,"\n";
105
106 sub rot_test {
107   my ($src, $degrees, $count) = @_;
108
109   my $cimg = $src->copy();
110   my $in;
111   for (1..$count) {
112     $in = $cimg;
113     $cimg = $cimg->rotate(right=>$degrees)
114       or last;
115   }
116  SKIP:
117   {
118     ok($cimg, "got a rotated image")
119       or skip("no image to check", 4);
120     my $diff = Imager::i_img_diff($src->{IMG}, $cimg->{IMG});
121     is($diff, 0, "check it matches source")
122       or skip("didn't match", 3);
123
124     # check that other parameters match
125     is($src->type, $cimg->type, "type check");
126     is($src->bits, $cimg->bits, "bits check");
127     is($src->getchannels, $cimg->getchannels, "channels check");
128   }
129 }
130
131 { # http://rt.cpan.org/NoAuth/Bug.html?id=9672
132   my $warning;
133   local $SIG{__WARN__} = 
134     sub { 
135       $warning = "@_";
136       my $printed = $warning;
137       $printed =~ s/\n$//;
138       $printed =~ s/\n/\n\#/g; 
139       print "# ",$printed, "\n";
140     };
141   my $img = Imager->new(xsize=>10, ysize=>10);
142   $img->copy();
143   cmp_ok($warning, '=~', 'void', "correct warning");
144   cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
145   $warning = '';
146   $img->rotate(degrees=>5);
147   cmp_ok($warning, '=~', 'void', "correct warning");
148   cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
149   $warning = '';
150   $img->matrix_transform(matrix=>[1, 1, 1]);
151   cmp_ok($warning, '=~', 'void', "correct warning");
152   cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
153 }
154