]> git.imager.perl.org - imager.git/blobdiff - t/t64copyflip.t
avoid a probably unwarranted compiler warning
[imager.git] / t / t64copyflip.t
index bc9a65d031ed738ae2c9c5231020951d822c9f50..38561cc70cd9a9412729cccd68d0502c9a39aef8 100644 (file)
@@ -1,11 +1,13 @@
 #!perl -w
 use strict;
-use Test::More tests => 65;
+use Test::More tests => 95;
 use Imager;
-use Imager::Test qw(is_color3);
+use Imager::Test qw(is_color3 is_image is_imaged test_image_double test_image isnt_image is_image_similar);
 
 #$Imager::DEBUG=1;
 
+-d "testout" or mkdir "testout";
+
 Imager::init('log'=>'testout/t64copyflip.log');
 
 my $img=Imager->new() or die "unable to create image object\n";
@@ -17,27 +19,57 @@ ok($nimg, "copy returned something");
 # test if ->copy() works
 
 my $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
-is($diff, 0, "copy matches source");
+is_image($img, $nimg, "copy matches source");
 
+{
+  my $empty = Imager->new;
+  ok(!$empty->copy, "fail to copy an empty image");
+  is($empty->errstr, "copy: empty input image", "check error message");
+}
 
 # test if ->flip(dir=>'h')->flip(dir=>'h') doesn't alter the image
-
 $nimg->flip(dir=>"h")->flip(dir=>"h");
-$diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
-is($diff, 0, "double horiz flipped matches original");
+is_image($nimg, $img, "double horiz flipped matches original");
 
 # test if ->flip(dir=>'v')->flip(dir=>'v') doesn't alter the image
-
 $nimg->flip(dir=>"v")->flip(dir=>"v");
-$diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
-is($diff, 0, "double vertically flipped image matches original");
+is_image($nimg, $img, "double vertically flipped image matches original");
 
 
 # test if ->flip(dir=>'h')->flip(dir=>'v') is same as ->flip(dir=>'hv')
-
 $nimg->flip(dir=>"v")->flip(dir=>"h")->flip(dir=>"hv");;
-$diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
-is($diff, 0, "check flip with hv matches flip v then flip h");
+is_image($img, $nimg, "check flip with hv matches flip v then flip h");
+
+{
+  my $empty = Imager->new;
+  ok(!$empty->flip(dir => "v"), "fail to flip an empty image");
+  is($empty->errstr, "flip: empty input image", "check error message");
+}
+
+{
+  my $imsrc = test_image_double;
+  my $imcp = $imsrc->copy;
+  is_imaged($imsrc, $imcp, "copy double image");
+  $imcp->flip(dir=>"v")->flip(dir=>"v");
+  is_imaged($imsrc, $imcp, "flip v twice");
+  $imcp->flip(dir=>"h")->flip(dir=>"h");
+  is_imaged($imsrc, $imcp, "flip h twice");
+  $imcp->flip(dir=>"h")->flip(dir=>"v")->flip(dir=>"hv");
+  is_imaged($imsrc, $imcp, "flip h,v,hv twice");
+}
+
+{
+  my $impal = test_image()->to_paletted;
+  my $imcp = $impal->copy;
+  is($impal->type, "paletted", "check paletted test image is");
+  is($imcp->type, "paletted", "check copy test image is paletted");
+  ok($impal->flip(dir => "h"), "flip paletted h");
+  isnt_image($impal, $imcp, "check it changed");
+  ok($impal->flip(dir => "v"), "flip paletted v");
+  ok($impal->flip(dir => "hv"), "flip paletted hv");
+  is_image($impal, $imcp, "should be back to original image");
+  is($impal->type, "paletted", "and still paletted");
+}
 
 rot_test($img, 90, 4);
 rot_test($img, 180, 2);
@@ -86,6 +118,31 @@ if (!$rimg->write(file=>"testout/t64_rot10_back.ppm")) {
   ok(!$rimg, "should fail due to bad back color");
   cmp_ok($img->errstr, '=~', "^No color named ", "check error message");
 }
+SKIP:
+{ # rotate in double mode
+  my $dimg = $img->to_rgb16;
+  my $rimg = $dimg->rotate(degrees => 10);
+  ok($rimg, "rotate 16-bit image gave us an image")
+    or skip("could not rotate", 3);
+  ok($rimg->write(file => "testout/t64_rotf10.ppm", pnm_write_wide_data => 1),
+     "save wide data rotated")
+    or diag($rimg->errstr);
+
+  # with a background color
+  my $rimgb = $dimg->rotate(degrees => 10, back => "#FF8000");
+  ok($rimgb, "rotate 16-bit image with back gave us an image")
+    or skip("could not rotate", 1);
+  ok($rimgb->write(file => "testout/t64_rotfb10.ppm", pnm_write_wide_data => 1),
+     "save wide data rotated")
+    or diag($rimgb->errstr);
+}
+{ # rotate in paletted mode
+  my $rimg = $pimg->rotate(degrees => 10);
+  ok($rimg, "rotated paletted image 10 degrees");
+  ok($rimg->write(file => "testout/t64_rotp10.ppm"),
+     "save paletted rotated")
+    or diag($rimg->errstr);
+}
 
 my $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
                                              0,   1, 0,
@@ -103,6 +160,16 @@ ok($trimg, "matrix_transform() with back returned an image");
 $trimg->write(file=>"testout/t64_trans_back.ppm")
   or print "# Cannot save: ",$trimg->errstr,"\n";
 
+{
+  my $empty = Imager->new;
+  ok(!$empty->matrix_transform(matrix => [ 1, 0, 0,
+                                          0, 1, 0,
+                                          0, 0, 1 ]),
+     "can't transform an empty image");
+  is($empty->errstr, "matrix_transform: empty input image",
+     "check error message");
+}
+
 sub rot_test {
   my ($src, $degrees, $count) = @_;
 
@@ -181,8 +248,38 @@ sub rot_test {
   my $work = $rot->convert(preset => 'noalpha');
   my $im_pal = $work->to_paletted(make_colors => 'mediancut');
   my @colors = $im_pal->getcolors;
-  is(@colors, 2, "should be only 2 colors");
+  is(@colors, 2, "should be only 2 colors")
+    or do {
+      print "# ", join(",", $_->rgba), "\n" for @colors;
+    };
   @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
   is_color3($colors[0], 0, 0, 0, "check we got black");
   is_color3($colors[1], 255, 0, 0, "and red");
 }
+
+{ # RT #77063 rotate with degrees => 270 gives a black border
+  # so be a little less strict about rounding up
+  # I've also:
+  #  - improved calculation of the rotation matrix
+  #  - added rounding to interpolation for 1/3 channel images
+  my $im = test_image;
+  $im->box(color => "#00F");
+  my $right = $im->rotate(right => 270);
+  my $deg = $im->rotate(degrees => 270, back => "#FFF");
+  is($deg->getwidth, 150, "check degrees => 270 width");
+  is($deg->getheight, 150, "check degrees => 270 height");
+  ok($deg->write(file => "testout/t64rotdeg270.ppm"), "save it");
+  $right->write(file => "testout/t64rotright270.ppm");
+  is_image($deg, $right, "check right and degrees result the same");
+  #$deg = $deg->convert(preset => "addalpha");
+  # $right = $right->convert(preset => "addalpha");
+  # my $diff = $right->difference(other => $deg, mindist => 1);
+  # $diff->write(file => "testout/t64rotdiff.png");
+}
+
+{
+  my $empty = Imager->new;
+  ok(!$empty->rotate(degrees => 90), "can't rotate an empty image");
+  is($empty->errstr, "rotate: empty input image",
+     "check error message");
+}