3 use Test::More tests => 65;
5 use Imager::Test qw(is_color3);
9 Imager::init('log'=>'testout/t64copyflip.log');
11 my $img=Imager->new() or die "unable to create image object\n";
13 $img->open(file=>'testimg/scale.ppm',type=>'pnm');
14 my $nimg = $img->copy();
15 ok($nimg, "copy returned something");
17 # test if ->copy() works
19 my $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
20 is($diff, 0, "copy matches source");
23 # test if ->flip(dir=>'h')->flip(dir=>'h') doesn't alter the image
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");
29 # test if ->flip(dir=>'v')->flip(dir=>'v') doesn't alter the image
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");
36 # test if ->flip(dir=>'h')->flip(dir=>'v') is same as ->flip(dir=>'hv')
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");
42 rot_test($img, 90, 4);
43 rot_test($img, 180, 2);
44 rot_test($img, 270, 4);
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);
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");
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";
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";
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");
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");
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");
90 my $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
93 ok($trimg, "matrix_transform() returned an image");
94 $trimg->write(file=>"testout/t64_trans.ppm")
95 or print "# Cannot save: ",$trimg->errstr,"\n";
97 $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
100 back=>Imager::Color->new(builtin=>'blue'));
101 ok($trimg, "matrix_transform() with back returned an image");
103 $trimg->write(file=>"testout/t64_trans_back.ppm")
104 or print "# Cannot save: ",$trimg->errstr,"\n";
107 my ($src, $degrees, $count) = @_;
109 my $cimg = $src->copy();
113 $cimg = $cimg->rotate(right=>$degrees)
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);
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");
131 { # http://rt.cpan.org/NoAuth/Bug.html?id=9672
133 local $SIG{__WARN__} =
136 my $printed = $warning;
138 $printed =~ s/\n/\n\#/g;
139 print "# ",$printed, "\n";
141 my $img = Imager->new(xsize=>10, ysize=>10);
143 cmp_ok($warning, '=~', 'void', "correct warning");
144 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
146 $img->rotate(degrees=>5);
147 cmp_ok($warning, '=~', 'void', "correct warning");
148 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
150 $img->matrix_transform(matrix=>[1, 1, 1]);
151 cmp_ok($warning, '=~', 'void', "correct warning");
152 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
156 # 29936 - matrix_transform() should use fabs() instead of abs()
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
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,
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");
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");