3 use Test::More tests => 77;
5 use Imager::Test qw(is_color3 is_image is_imaged test_image_double test_image isnt_image);
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_image($img, $nimg, "copy matches source");
22 # test if ->flip(dir=>'h')->flip(dir=>'h') doesn't alter the image
23 $nimg->flip(dir=>"h")->flip(dir=>"h");
24 is_image($nimg, $img, "double horiz flipped matches original");
26 # test if ->flip(dir=>'v')->flip(dir=>'v') doesn't alter the image
27 $nimg->flip(dir=>"v")->flip(dir=>"v");
28 is_image($nimg, $img, "double vertically flipped image matches original");
31 # test if ->flip(dir=>'h')->flip(dir=>'v') is same as ->flip(dir=>'hv')
32 $nimg->flip(dir=>"v")->flip(dir=>"h")->flip(dir=>"hv");;
33 is_image($img, $nimg, "check flip with hv matches flip v then flip h");
36 my $imsrc = test_image_double;
37 my $imcp = $imsrc->copy;
38 is_imaged($imsrc, $imcp, "copy double image");
39 $imcp->flip(dir=>"v")->flip(dir=>"v");
40 is_imaged($imsrc, $imcp, "flip v twice");
41 $imcp->flip(dir=>"h")->flip(dir=>"h");
42 is_imaged($imsrc, $imcp, "flip h twice");
43 $imcp->flip(dir=>"h")->flip(dir=>"v")->flip(dir=>"hv");
44 is_imaged($imsrc, $imcp, "flip h,v,hv twice");
48 my $impal = test_image()->to_paletted;
49 my $imcp = $impal->copy;
50 is($impal->type, "paletted", "check paletted test image is");
51 is($imcp->type, "paletted", "check copy test image is paletted");
52 ok($impal->flip(dir => "h"), "flip paletted h");
53 isnt_image($impal, $imcp, "check it changed");
54 ok($impal->flip(dir => "v"), "flip paletted v");
55 ok($impal->flip(dir => "hv"), "flip paletted hv");
56 is_image($impal, $imcp, "should be back to original image");
57 is($impal->type, "paletted", "and still paletted");
60 rot_test($img, 90, 4);
61 rot_test($img, 180, 2);
62 rot_test($img, 270, 4);
65 my $pimg = $img->to_paletted();
66 rot_test($pimg, 90, 4);
67 rot_test($pimg, 180, 2);
68 rot_test($pimg, 270, 4);
69 rot_test($pimg, 0, 1);
71 my $timg = $img->rotate(right=>90)->rotate(right=>270);
72 is(Imager::i_img_diff($img->{IMG}, $timg->{IMG}), 0,
73 "check rotate 90 then 270 matches original");
74 $timg = $img->rotate(right=>90)->rotate(right=>180)->rotate(right=>90);
75 is(Imager::i_img_diff($img->{IMG}, $timg->{IMG}), 0,
76 "check rotate 90 then 180 then 90 matches original");
78 # this could use more tests
79 my $rimg = $img->rotate(degrees=>10);
80 ok($rimg, "rotation by 10 degrees gave us an image");
81 if (!$rimg->write(file=>"testout/t64_rot10.ppm")) {
82 print "# Cannot save: ",$rimg->errstr,"\n";
85 # rotate with background
86 $rimg = $img->rotate(degrees=>10, back=>Imager::Color->new(builtin=>'red'));
87 ok($rimg, "rotate with background gave us an image");
88 if (!$rimg->write(file=>"testout/t64_rot10_back.ppm")) {
89 print "# Cannot save: ",$rimg->errstr,"\n";
93 # rotate with text background
94 my $rimg = $img->rotate(degrees => 45, back => '#FF00FF');
95 ok($rimg, "rotate with background as text gave us an image");
97 # check the color set correctly
98 my $c = $rimg->getpixel(x => 0, 'y' => 0);
99 is_deeply([ 255, 0, 255 ], [ ($c->rgba)[0, 1, 2] ],
100 "check background set correctly");
102 # check error handling for background color
103 $rimg = $img->rotate(degrees => 45, back => "some really unknown color");
104 ok(!$rimg, "should fail due to bad back color");
105 cmp_ok($img->errstr, '=~', "^No color named ", "check error message");
108 my $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
111 ok($trimg, "matrix_transform() returned an image");
112 $trimg->write(file=>"testout/t64_trans.ppm")
113 or print "# Cannot save: ",$trimg->errstr,"\n";
115 $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
118 back=>Imager::Color->new(builtin=>'blue'));
119 ok($trimg, "matrix_transform() with back returned an image");
121 $trimg->write(file=>"testout/t64_trans_back.ppm")
122 or print "# Cannot save: ",$trimg->errstr,"\n";
125 my ($src, $degrees, $count) = @_;
127 my $cimg = $src->copy();
131 $cimg = $cimg->rotate(right=>$degrees)
136 ok($cimg, "got a rotated image")
137 or skip("no image to check", 4);
138 my $diff = Imager::i_img_diff($src->{IMG}, $cimg->{IMG});
139 is($diff, 0, "check it matches source")
140 or skip("didn't match", 3);
142 # check that other parameters match
143 is($src->type, $cimg->type, "type check");
144 is($src->bits, $cimg->bits, "bits check");
145 is($src->getchannels, $cimg->getchannels, "channels check");
149 { # http://rt.cpan.org/NoAuth/Bug.html?id=9672
151 local $SIG{__WARN__} =
154 my $printed = $warning;
156 $printed =~ s/\n/\n\#/g;
157 print "# ",$printed, "\n";
159 my $img = Imager->new(xsize=>10, ysize=>10);
161 cmp_ok($warning, '=~', 'void', "correct warning");
162 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
164 $img->rotate(degrees=>5);
165 cmp_ok($warning, '=~', 'void', "correct warning");
166 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
168 $img->matrix_transform(matrix=>[1, 1, 1]);
169 cmp_ok($warning, '=~', 'void', "correct warning");
170 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
174 # 29936 - matrix_transform() should use fabs() instead of abs()
177 # this meant that when sz was < 1 (which it often is for these
178 # transformations), it treated the values out of range, producing a
181 my $src = Imager->new(xsize => 20, ysize => 20);
182 $src->box(filled => 1, color => 'FF0000');
183 my $out = $src->matrix_transform(matrix => [ 1, 0, 0,
186 or print "# ", $src->errstr, "\n";
187 my $blank = Imager->new(xsize => 20, ysize => 20);
188 # they have to be different, surely that would be easy
189 my $diff = Imager::i_img_diff($out->{IMG}, $blank->{IMG});
190 ok($diff, "RT#29936 - check non-blank output");
194 my $im = Imager->new(xsize => 10, ysize => 10, channels => 4);
195 $im->box(filled => 1, color => 'FF0000');
196 my $back = Imager::Color->new(0, 0, 0, 0);
197 my $rot = $im->rotate(degrees => 10, back => $back);
198 # drop the alpha and make sure there's only 2 colors used
199 my $work = $rot->convert(preset => 'noalpha');
200 my $im_pal = $work->to_paletted(make_colors => 'mediancut');
201 my @colors = $im_pal->getcolors;
202 is(@colors, 2, "should be only 2 colors");
203 @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
204 is_color3($colors[0], 0, 0, 0, "check we got black");
205 is_color3($colors[1], 255, 0, 0, "and red");