]> git.imager.perl.org - imager.git/commitdiff
OO API
authorTony Cook <tony@develop-help.com>
Mon, 13 Feb 2012 09:57:59 +0000 (20:57 +1100)
committerTony Cook <tony@develop-help.com>
Mon, 13 Feb 2012 11:14:33 +0000 (22:14 +1100)
Imager.pm
t/t01introvert.t

index d03638be3dc93f2e3e1d2063995962ef155e419e..fa8578d34dfeca17b6ee79b3f3d242857ff6f16b 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -3388,24 +3388,44 @@ sub setsamples {
     return;
   }
 
     return;
   }
 
-  unless(defined $opts{data} && ref $opts{data}) {
-    $self->_set_error('setsamples: data parameter missing or invalid');
+  my $data = $opts{data};
+  unless(defined $data) {
+    $self->_set_error('setsamples: data parameter missing');
     return;
   }
 
     return;
   }
 
-  unless ($opts{type} && $opts{type} =~ /^(\d+)bit$/) {
-    $self->_set_error('setsamples: type parameter missing or invalid');
-    return;
+  my $type = $opts{type};
+  defined $type or $type = '8bit';
+
+  my $width = defined $opts{width} ? $opts{width}
+    : $self->getwidth() - $opts{x};
+
+  my $count;
+  if ($type eq '8bit') {
+    $count = i_psamp($self->{IMG}, $opts{x}, $opts{y}, $opts{channels},
+                    $data);
+  }
+  elsif ($type eq 'float') {
+    $count = i_psampf($self->{IMG}, $opts{x}, $opts{y}, $opts{channels},
+                     $opts{data});
   }
   }
-  my $bits = $1;
+  elsif ($type =~ /^([0-9]+)bit$/) {
+    my $bits = $1;
 
 
-  unless (defined $opts{width}) {
-    $opts{width} = $self->getwidth() - $opts{x};
+    unless (ref $data) {
+      $self->_set_error("setsamples: data must be an array ref for type not 8bit or float");
+      return;
+    }
+
+    $count = i_psamp_bits($self->{IMG}, $opts{x}, $opts{y}, $bits,
+                         $opts{channels}, $opts{data}, $opts{offset}, 
+                         $width);
+  }
+  else {
+    $self->_set_error('setsamples: type parameter invalid');
+    return;
   }
 
   }
 
-  my $count = i_psamp_bits($self->{IMG}, $opts{x}, $opts{y}, $bits,
-                          $opts{channels}, $opts{data}, $opts{offset}, 
-                          $opts{width});
   unless (defined $count) {
     $self->_set_error(Imager->_error_as_msg);
     return;
   unless (defined $count) {
     $self->_set_error(Imager->_error_as_msg);
     return;
index 77f8c42bb5495744518cee4b700eb66dce6133cb..e8d8bc35e413b7361181d7a87f50f1caeb738c64 100644 (file)
@@ -3,7 +3,7 @@
 # to make sure we get expected values
 
 use strict;
 # to make sure we get expected values
 
 use strict;
-use Test::More tests => 307;
+use Test::More tests => 329;
 
 BEGIN { use_ok(Imager => qw(:handy :all)) }
 
 
 BEGIN { use_ok(Imager => qw(:handy :all)) }
 
@@ -418,6 +418,7 @@ cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
   print "# end low-level scan-line function tests\n";
 }
 
   print "# end low-level scan-line function tests\n";
 }
 
+my $psamp_outside_error = "Image position outside of image";
 { # psamp
   print "# psamp\n";
   my $imraw = Imager::ImgRaw::new(10, 10, 3);
 { # psamp
   print "# psamp\n";
   my $imraw = Imager::ImgRaw::new(10, 10, 3);
@@ -754,6 +755,63 @@ cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
   image_bounds_checks($im);
 }
 
   image_bounds_checks($im);
 }
 
+{ # setsamples() interface to psamp()
+  my $im = Imager->new(xsize => 10, ysize => 10);
+  is($im->setsamples(y => 1, x => 2, data => [ 1 .. 6 ]), 6,
+     "simple put (array), default channels");
+  is_deeply([ $im->getsamples(y => 1, x => 0) ],
+           [ (0) x 6, 1 .. 6, (0) x 18 ], "check they were stored");
+  is($im->setsamples(y => 3, x => 3, data => pack("C*", 2 .. 10 )), 9,
+     "simple put (scalar), default channels")
+    or diag $im->errstr;
+  is_deeply([ $im->getsamples(y => 3, x => 0) ],
+           [ (0) x 9, 2 .. 10, (0) x 12 ], "check they were stored");
+  is($im->setsamples(y => 4, x => 4, data => [ map $_ / 254.5, 1 .. 6 ], type => 'float'),
+     6, "simple put (float array), default channels");
+  is_deeply([ $im->getsamples(y => 4, x => 0) ],
+           [ (0) x 12, 1 .. 6, (0) x 12 ], "check they were stored");
+
+  is($im->setsamples(y => 5, x => 3, data => pack("d*", map $_ / 254.5, 1 .. 6), type => 'float'),
+     6, "simple put (float scalar), default channels");
+  is_deeply([ $im->getsamples(y => 5, x => 0) ],
+           [ (0) x 9, 1 .. 6, (0) x 15 ], "check they were stored");
+
+  is_deeply([ $im->setsamples(y => 6, x => 10, data => [ (0) x 3 ]) ],
+           [], "check out of range result (8bit)");
+  is($im->errstr, $psamp_outside_error, "check error message");
+
+  is_deeply([ $im->setsamples(y => 6, x => 10, data => [ (0) x 3 ], type => "float") ],
+           [], "check out of range result (float)");
+  is($im->errstr, $psamp_outside_error, "check error message");
+
+  is_deeply([ $im->setsamples(y => 6, x => 2, channels => [0, 1, 3 ],
+                             data => [ (0) x 3 ]) ],
+           [], "check bad channels (8bit)");
+  is($im->errstr, "No channel 3 in this image",
+     "check error message");
+  
+  is_deeply([ $im->setsamples(y => 6, x => 2, channels => [0, 1, 3 ], 
+                             data => [ (0) x 3 ], type => "float") ],
+           [], "check bad channels (float)");
+  is($im->errstr, "No channel 3 in this image",
+     "check error message");
+
+  is($im->setsamples(y => 5, data => [ (0) x 3 ], type => "bad"),
+     undef, "setsamples with bad type");
+  is($im->errstr, "setsamples: type parameter invalid",
+     "check error message");
+  is($im->setsamples(y => 5),
+     undef, "setsamples with no data");
+  is($im->errstr, "setsamples: data parameter missing",
+     "check error message");
+
+  my $imempty = Imager->new;
+  is($imempty->setsamples(y => 0, data => [ (0) x 3 ]), undef,
+     "setsamples to empty image");
+  is($imempty->errstr, "setsamples: empty input image",
+     "check error message");
+}
+
 Imager->close_log();
 
 unless ($ENV{IMAGER_KEEP_FILES}) {
 Imager->close_log();
 
 unless ($ENV{IMAGER_KEEP_FILES}) {