]> git.imager.perl.org - imager.git/blob - t/t64copyflip.t
- added t/t91pod.t
[imager.git] / t / t64copyflip.t
1 #!perl -w
2 use strict;
3 use lib 't';
4 use Test::More tests=>57;
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 my $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
76                                              0,   1, 0,
77                                              0,   0, 1]);
78 ok($trimg, "matrix_transform() returned an image");
79 $trimg->write(file=>"testout/t64_trans.ppm")
80   or print "# Cannot save: ",$trimg->errstr,"\n";
81
82 $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
83                                              0,   1, 0,
84                                              0,   0, 1],
85                                    back=>Imager::Color->new(builtin=>'blue'));
86 ok($trimg, "matrix_transform() with back returned an image");
87
88 $trimg->write(file=>"testout/t64_trans_back.ppm")
89   or print "# Cannot save: ",$trimg->errstr,"\n";
90
91 sub rot_test {
92   my ($src, $degrees, $count) = @_;
93
94   my $cimg = $src->copy();
95   my $in;
96   for (1..$count) {
97     $in = $cimg;
98     $cimg = $cimg->rotate(right=>$degrees)
99       or last;
100   }
101  SKIP:
102   {
103     ok($cimg, "got a rotated image")
104       or skip("no image to check", 4);
105     my $diff = Imager::i_img_diff($src->{IMG}, $cimg->{IMG});
106     is($diff, 0, "check it matches source")
107       or skip("didn't match", 3);
108
109     # check that other parameters match
110     is($src->type, $cimg->type, "type check");
111     is($src->bits, $cimg->bits, "bits check");
112     is($src->getchannels, $cimg->getchannels, "channels check");
113   }
114 }
115
116 { # http://rt.cpan.org/NoAuth/Bug.html?id=9672
117   my $warning;
118   local $SIG{__WARN__} = 
119     sub { 
120       $warning = "@_";
121       my $printed = $warning;
122       $printed =~ s/\n$//;
123       $printed =~ s/\n/\n\#/g; 
124       print "# ",$printed, "\n";
125     };
126   my $img = Imager->new(xsize=>10, ysize=>10);
127   $img->copy();
128   cmp_ok($warning, '=~', 'void', "correct warning");
129   cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
130   $warning = '';
131   $img->rotate(degrees=>5);
132   cmp_ok($warning, '=~', 'void', "correct warning");
133   cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
134   $warning = '';
135   $img->matrix_transform(matrix=>[1, 1, 1]);
136   cmp_ok($warning, '=~', 'void', "correct warning");
137   cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
138 }
139