]> git.imager.perl.org - imager.git/blob - t/t64copyflip.t
- correctly blend a rotated (or matrix_transformed()) image when
[imager.git] / t / t64copyflip.t
1 #!perl -w
2 use strict;
3 use Test::More tests => 65;
4 use Imager;
5 use Imager::Test qw(is_color3);
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
155 {
156   # 29936 - matrix_transform() should use fabs() instead of abs()
157   # range checking sz 
158
159   # this meant that when sz was < 1 (which it often is for these
160   # transformations), it treated the values out of range, producing a
161   # blank output image
162
163   my $src = Imager->new(xsize => 20, ysize => 20);
164   $src->box(filled => 1, color => 'FF0000');
165   my $out = $src->matrix_transform(matrix => [ 1, 0, 0,
166                                                0, 1, 0,
167                                                0, 0, 0.9999 ])
168     or print "# ", $src->errstr, "\n";
169   my $blank = Imager->new(xsize => 20, ysize => 20);
170   # they have to be different, surely that would be easy
171   my $diff = Imager::i_img_diff($out->{IMG}, $blank->{IMG});
172   ok($diff, "RT#29936 - check non-blank output");
173 }
174
175 {
176   my $im = Imager->new(xsize => 10, ysize => 10, channels => 4);
177   $im->box(filled => 1, color => 'FF0000');
178   my $back = Imager::Color->new(0, 0, 0, 0);
179   my $rot = $im->rotate(degrees => 10, back => $back);
180   # drop the alpha and make sure there's only 2 colors used
181   my $work = $rot->convert(preset => 'noalpha');
182   my $im_pal = $work->to_paletted(make_colors => 'mediancut');
183   my @colors = $im_pal->getcolors;
184   is(@colors, 2, "should be only 2 colors");
185   @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
186   is_color3($colors[0], 0, 0, 0, "check we got black");
187   is_color3($colors[1], 255, 0, 0, "and red");
188 }