- converted t/t021sixteen.t to use Test::More
authorTony Cook <tony@develop=help.com>
Fri, 11 Nov 2005 12:08:10 +0000 (12:08 +0000)
committerTony Cook <tony@develop=help.com>
Fri, 11 Nov 2005 12:08:10 +0000 (12:08 +0000)
- 16-bit per sample images were ignoring the channel mask

Changes
img16.c
t/t01introvert.t
t/t021sixteen.t
t/t022double.t
t/testtools.pl

diff --git a/Changes b/Changes
index a5cc4bba87b7c38838b141ee7d9f08704e3562bc..43d43820d89403cb6381de44d4b178d8ad962cb3 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1168,6 +1168,8 @@ Revision history for Perl extension Imager.
 - setmask() now returns true on success and reports a missing mask
   parameter.
 - double per sample images were ignoring the channel mask.
+- converted t/t021sixteen.t to use Test::More
+- 16-bit per sample images were ignoring the channel mask
 
 =================================================================
 
diff --git a/img16.c b/img16.c
index 2d795477fbf172662c0c56112cc7917c9e4916c7..92f998cd8b20e8e874d7b0eeb4c068c62ff5d23f 100644 (file)
--- a/img16.c
+++ b/img16.c
@@ -204,8 +204,15 @@ static int i_ppix_d16(i_img *im, int x, int y, i_color *val) {
     return -1;
 
   off = (x + y * im->xsize) * im->channels;
-  for (ch = 0; ch < im->channels; ++ch)
-    STORE8as16(im->idata, off+ch, val->channel[ch]);
+  if (I_ALL_CHANNELS_WRITABLE(im)) {
+    for (ch = 0; ch < im->channels; ++ch)
+      STORE8as16(im->idata, off+ch, val->channel[ch]);
+  }
+  else {
+    for (ch = 0; ch < im->channels; ++ch)
+      if (im->ch_mask & (1 << ch))
+       STORE8as16(im->idata, off+ch, val->channel[ch]);
+  }
 
   return 0;
 }
@@ -230,8 +237,15 @@ static int i_ppixf_d16(i_img *im, int x, int y, i_fcolor *val) {
     return -1;
 
   off = (x + y * im->xsize) * im->channels;
-  for (ch = 0; ch < im->channels; ++ch)
-    STORE16(im->idata, off+ch, SampleFTo16(val->channel[ch]));
+  if (I_ALL_CHANNELS_WRITABLE(im)) {
+    for (ch = 0; ch < im->channels; ++ch)
+      STORE16(im->idata, off+ch, SampleFTo16(val->channel[ch]));
+  }
+  else {
+    for (ch = 0; ch < im->channels; ++ch)
+      if (im->ch_mask & (1 << ch))
+       STORE16(im->idata, off+ch, SampleFTo16(val->channel[ch]));
+  }
 
   return 0;
 }
@@ -278,10 +292,21 @@ static int i_plin_d16(i_img *im, int l, int r, int y, i_color *vals) {
       r = im->xsize;
     off = (l+y*im->xsize) * im->channels;
     count = r - l;
-    for (i = 0; i < count; ++i) {
-      for (ch = 0; ch < im->channels; ++ch) {
-        STORE8as16(im->idata, off, vals[i].channel[ch]);
-        ++off;
+    if (I_ALL_CHANNELS_WRITABLE(im)) {
+      for (i = 0; i < count; ++i) {
+       for (ch = 0; ch < im->channels; ++ch) {
+         STORE8as16(im->idata, off, vals[i].channel[ch]);
+         ++off;
+       }
+      }
+    }
+    else {
+      for (i = 0; i < count; ++i) {
+       for (ch = 0; ch < im->channels; ++ch) {
+         if (im->ch_mask & (1 << ch))
+           STORE8as16(im->idata, off, vals[i].channel[ch]);
+         ++off;
+       }
       }
     }
     return count;
@@ -320,10 +345,21 @@ static int i_plinf_d16(i_img *im, int l, int r, int y, i_fcolor *vals) {
       r = im->xsize;
     off = (l+y*im->xsize) * im->channels;
     count = r - l;
-    for (i = 0; i < count; ++i) {
-      for (ch = 0; ch < im->channels; ++ch) {
-        STORE16(im->idata, off, SampleFTo16(vals[i].channel[ch]));
-        ++off;
+    if (I_ALL_CHANNELS_WRITABLE(im)) {
+      for (i = 0; i < count; ++i) {
+       for (ch = 0; ch < im->channels; ++ch) {
+         STORE16(im->idata, off, SampleFTo16(vals[i].channel[ch]));
+         ++off;
+       }
+      }
+    }
+    else {
+      for (i = 0; i < count; ++i) {
+       for (ch = 0; ch < im->channels; ++ch) {
+         if (im->ch_mask & (1 << ch))
+           STORE16(im->idata, off, SampleFTo16(vals[i].channel[ch]));
+         ++off;
+       }
       }
     }
     return count;
index 6d963170899dad0838fccc3f1029f855d40ada42..3276926b4d65889a6069dc12d5c5e77a22c27050 100644 (file)
@@ -4,7 +4,7 @@
 
 use strict;
 use lib 't';
-use Test::More tests=>164;
+use Test::More tests=>196;
 
 BEGIN { use_ok(Imager => qw(:handy :all)) }
 
@@ -473,6 +473,13 @@ cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
   print "# end OO level scanline function tests\n";
 }
 
+{ # check the channel mask function
+  
+  my $im = Imager->new(xsize => 10, ysize=>10, bits=>8);
+
+  mask_tests($im, 0.005);
+}
+
 sub check_add {
   my ($im, $color, $expected) = @_;
   my $index = Imager::i_addcolors($im, $color);
@@ -486,15 +493,6 @@ sub check_add {
   $index;
 }
 
-sub color_cmp {
-  my ($l, $r) = @_;
-  my @l = $l->rgba;
-  my @r = $r->rgba;
-  return $l[0] <=> $r[0]
-    || $l[1] <=> $r[1]
-      || $l[2] <=> $r[2];
-}
-
 # sub array_ncmp {
 #   my ($a1, $a2) = @_;
 #   my $len = @$a1 < @$a2 ? @$a1 : @$a2;
index c057b6ebc1469f6aaf9e18f6d549c1e9e85e3be6..e8904e1c7c6c7c254d093a031b778ea2f9404b2b 100644 (file)
@@ -1,48 +1,36 @@
 #!perl -w
 use strict;
-BEGIN { $| = 1; print "1..51\n"; }
-my $loaded;
-END {print "not ok 1\n" unless $loaded;}
-use Imager qw(:all :handy);
-#use Data::Dumper;
-$loaded = 1;
-print "ok 1\n";
+use lib 't';
+use Test::More tests => 83;
+
+BEGIN { use_ok(Imager=>qw(:all :handy)) }
+
 init_log("testout/t021sixteen.log", 1);
+
 require "t/testtools.pl";
 
 use Imager::Color::Float;
 
 my $im_g = Imager::i_img_16_new(100, 101, 1);
 
-print Imager::i_img_getchannels($im_g) == 1 
-  ? "ok 2\n" : "not ok 2 # 1 channel image channel count mismatch\n";
-print Imager::i_img_getmask($im_g) & 1 
-  ? "ok 3\n" : "not ok 3 # 1 channel image bad mask\n";
-print Imager::i_img_virtual($im_g) 
-  ? "not ok 4 # 1 channel image thinks it is virtual\n" : "ok 4\n";
-print Imager::i_img_bits($im_g) == 16
-  ? "ok 5\n" : "not ok 5 # 1 channel image has bits != 16\n";
-print Imager::i_img_type($im_g) == 0 # direct
-  ? "ok 6\n" : "not ok 6 # 1 channel image isn't direct\n";
+is(Imager::i_img_getchannels($im_g), 1, "1 channel image channel count");
+ok(Imager::i_img_getmask($im_g) & 1, "1 channel image mask");
+ok(!Imager::i_img_virtual($im_g), "shouldn't be marked virtual");
+is(Imager::i_img_bits($im_g), 16, "1 channel image has bits == 16");
+is(Imager::i_img_type($im_g), 0, "1 channel image isn't direct");
 
 my @ginfo = i_img_info($im_g);
-print $ginfo[0] == 100 
-  ? "ok 7\n" : "not ok 7 # 1 channel image width incorrect\n";
-print $ginfo[1] == 101
-  ? "ok 8\n" : "not ok 8 # 1 channel image height incorrect\n";
+is($ginfo[0], 100, "1 channel image width");
+is($ginfo[1], 101, "1 channel image height");
 
 undef $im_g;
 
 my $im_rgb = Imager::i_img_16_new(100, 101, 3);
 
-print Imager::i_img_getchannels($im_rgb) == 3
-  ? "ok 9\n" : "not ok 9 # 3 channel image channel count mismatch\n";
-print +(Imager::i_img_getmask($im_rgb) & 7) == 7
-  ? "ok 10\n" : "not ok 10 # 3 channel image bad mask\n";
-print Imager::i_img_bits($im_rgb) == 16
-  ? "ok 11\n" : "not ok 11 # 3 channel image has bits != 16\n";
-print Imager::i_img_type($im_rgb) == 0 # direct
-  ? "ok 12\n" : "not ok 12 # 3 channel image isn't direct\n";
+is(Imager::i_img_getchannels($im_rgb), 3, "3 channel image channel count");
+ok((Imager::i_img_getmask($im_rgb) & 7) == 7, "3 channel image mask");
+is(Imager::i_img_bits($im_rgb), 16, "3 channel image bits");
+is(Imager::i_img_type($im_rgb), 0, "3 channel image type");
 
 my $redf = NCF(1, 0, 0);
 my $greenf = NCF(0, 1, 0);
@@ -52,60 +40,57 @@ my $bluef = NCF(0, 0, 1);
 for my $y (0..101) {
   Imager::i_plinf($im_rgb, 0, $y, ($redf) x 100);
 }
-print "ok 13\n";
+pass("fill with red");
 # basic sanity
-test_colorf_gpix(14, $im_rgb, 0,  0,   $redf);
-test_colorf_gpix(16, $im_rgb, 99, 0,   $redf);
-test_colorf_gpix(18, $im_rgb, 0,  100, $redf);
-test_colorf_gpix(20, $im_rgb, 99, 100, $redf);
-test_colorf_glin(22, $im_rgb, 0,  0,   ($redf) x 100);
-test_colorf_glin(24, $im_rgb, 0,  100, ($redf) x 100);
+test_colorf_gpix($im_rgb, 0,  0,   $redf);
+test_colorf_gpix($im_rgb, 99, 0,   $redf);
+test_colorf_gpix($im_rgb, 0,  100, $redf);
+test_colorf_gpix($im_rgb, 99, 100, $redf);
+test_colorf_glin($im_rgb, 0,  0,   ($redf) x 100);
+test_colorf_glin($im_rgb, 0,  100, ($redf) x 100);
 
 Imager::i_plinf($im_rgb, 20, 1, ($greenf) x 60);
-test_colorf_glin(26, $im_rgb, 0, 1, 
+test_colorf_glin($im_rgb, 0, 1, 
                  ($redf) x 20, ($greenf) x 60, ($redf) x 20);
 
 # basic OO tests
-my $oo16img = Imager->new(xsize=>200, ysize=>201, bits=>16)
-  or print "not ";
-print "ok 28\n";
-$oo16img->bits == 16 or print "not ";
-print "ok 29\n";
+my $oo16img = Imager->new(xsize=>200, ysize=>201, bits=>16);
+ok($oo16img, "make a 16-bit oo image");
+is($oo16img->bits,  16, "test bits");
 
-my $num = 30;
 # make sure of error handling
-okn($num++, !Imager->new(xsize=>0, ysize=>1, bits=>16),
+ok(!Imager->new(xsize=>0, ysize=>1, bits=>16),
     "fail to create a 0 pixel wide image");
-matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
+cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
        "and correct error message");
 
-okn($num++, !Imager->new(xsize=>1, ysize=>0, bits=>16),
+ok(!Imager->new(xsize=>1, ysize=>0, bits=>16),
     "fail to create a 0 pixel high image");
-matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
+cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
        "and correct error message");
 
-okn($num++, !Imager->new(xsize=>-1, ysize=>1, bits=>16),
+ok(!Imager->new(xsize=>-1, ysize=>1, bits=>16),
     "fail to create a negative width image");
-matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
+cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
        "and correct error message");
 
-okn($num++, !Imager->new(xsize=>1, ysize=>-1, bits=>16),
+ok(!Imager->new(xsize=>1, ysize=>-1, bits=>16),
     "fail to create a negative height image");
-matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
+cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
        "and correct error message");
 
-okn($num++, !Imager->new(xsize=>-1, ysize=>-1, bits=>16),
+ok(!Imager->new(xsize=>-1, ysize=>-1, bits=>16),
     "fail to create a negative width/height image");
-matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
+cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
        "and correct error message");
 
-okn($num++, !Imager->new(xsize=>1, ysize=>1, bits=>16, channels=>0),
+ok(!Imager->new(xsize=>1, ysize=>1, bits=>16, channels=>0),
     "fail to create a zero channel image");
-matchn($num++, Imager->errstr, qr/channels must be between 1 and 4/,
+cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
        "and correct error message");
-okn($num++, !Imager->new(xsize=>1, ysize=>1, bits=>16, channels=>5),
+ok(!Imager->new(xsize=>1, ysize=>1, bits=>16, channels=>5),
     "fail to create a five channel image");
-matchn($num++, Imager->errstr, qr/channels must be between 1 and 4/,
+cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
        "and correct error message");
 
 {
@@ -115,20 +100,22 @@ matchn($num++, Imager->errstr, qr/channels must be between 1 and 4/,
   # result in trying to allocate 4Gb of memory, which is unfriendly at
   # least and may result in running out of memory, causing a different
   # type of exit
-  use Config;
-  if ($Config{intsize} == 4) {
+ SKIP: {
+    use Config;
+    $Config{intsize} == 4
+      or skip("don't want to allocate 4Gb", 8);
     my $uint_range = 256 ** $Config{intsize};
     print "# range $uint_range\n";
     my $dim1 = int(sqrt($uint_range/2))+1;
     
     my $im_b = Imager->new(xsize=>$dim1, ysize=>$dim1, channels=>1, bits=>16);
-    isn($num++, $im_b, undef, "integer overflow check - 1 channel");
+    is($im_b, undef, "integer overflow check - 1 channel");
     
     $im_b = Imager->new(xisze=>$dim1, ysize=>1, channels=>1, bits=>16);
-    okn($num++, $im_b, "but same width ok");
+    ok($im_b, "but same width ok");
     $im_b = Imager->new(xisze=>1, ysize=>$dim1, channels=>1, bits=>16);
-    okn($num++, $im_b, "but same height ok");
-    matchn($num++, Imager->errstr, qr/integer overflow/,
+    ok($im_b, "but same height ok");
+    cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
            "check the error message");
 
     # do a similar test with a 3 channel image, so we're sure we catch
@@ -136,52 +123,21 @@ matchn($num++, Imager->errstr, qr/channels must be between 1 and 4/,
     my $dim3 = int(sqrt($uint_range / 3 / 2))+1;
     
     $im_b = Imager->new(xsize=>$dim3, ysize=>$dim3, channels=>3, bits=>16);
-    isn($num++, $im_b, undef, "integer overflow check - 3 channel");
+    is($im_b, undef, "integer overflow check - 3 channel");
     
     $im_b = Imager->new(xisze=>$dim3, ysize=>1, channels=>3, bits=>16);
-    okn($num++, $im_b, "but same width ok");
+    ok($im_b, "but same width ok");
     $im_b = Imager->new(xisze=>1, ysize=>$dim3, channels=>3, bits=>16);
-    okn($num++, $im_b, "but same height ok");
+    ok($im_b, "but same height ok");
 
-    matchn($num++, Imager->errstr, qr/integer overflow/,
+    cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
            "check the error message");
   }
-  else {
-    skipn($num, 8, "don't want to allocate 4Gb");
-    $num += 8;
-  }
-}
-
-sub NCF {
-  return Imager::Color::Float->new(@_);
-}
-
-sub test_colorf_gpix {
-  my ($test_base, $im, $x, $y, $expected) = @_;
-  my $c = Imager::i_gpixf($im, $x, $y);
-  $c or print "not ";
-  print "ok ",$test_base++,"\n";
-  colorf_cmp($c, $expected) == 0 or print "not ";
-  print "ok ",$test_base++,"\n";
-}
-
-sub test_colorf_glin {
-  my ($test_base, $im, $x, $y, @pels) = @_;
-
-  my @got = Imager::i_glinf($im, $x, $x+@pels, $y);
-  @got == @pels or print "not ";
-  print "ok ",$test_base++,"\n";
-  grep(colorf_cmp($pels[$_], $got[$_]), 0..$#got) and print "not ";
-  print "ok ",$test_base++,"\n";
 }
 
-sub colorf_cmp {
-  my ($c1, $c2) = @_;
-  my @s1 = map { int($_*65535.99) } $c1->rgba;
-  my @s2 = map { int($_*65535.99) } $c2->rgba;
+{ # check the channel mask function
+  
+  my $im = Imager->new(xsize => 10, ysize=>10, bits=>16);
 
-  # print "# (",join(",", @s1[0..2]),") <=> (",join(",", @s2[0..2]),")\n";
-  return $s1[0] <=> $s2[0] 
-    || $s1[1] <=> $s2[1]
-      || $s1[2] <=> $s2[2];
+  mask_tests($im, 1.0/65535);
 }
index da7e27a52b9393f90a91d997e4ad1b886ee2cfad..e809c86a13b1a1ec9228c439578b536230a674c2 100644 (file)
@@ -137,116 +137,10 @@ cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
 }
 
 { # check the channel mask function
-  # we want to check all four of ppix() and plin(), ppix() and plinf()
-  # basic test procedure:
-  #   first using default/all 1s mask, set to white
-  #   make sure we got white
-  #   set mask to skip a channel, set to grey
-  #   make sure only the right channels set
   
-  print "# channel mask tests\n";
   my $im = Imager->new(xsize => 10, ysize=>10, bits=>'double');
 
-  # 8-bit color tests
-  my $white = NC(255, 255, 255);
-  my $grey = NC(128, 128, 128);
-  my $white_grey = NC(128, 255, 128);
-
-  print "# with ppix\n";
-  ok($im->setmask(mask=>~0), "set to default mask");
-  ok($im->setpixel(x=>0, 'y'=>0, color=>$white), "set to white all channels");
-  test_color_gpix($im->{IMG}, 0, 0, $white);
-  ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
-  ok($im->setpixel(x=>0, 'y'=>0, color=>$grey), "set to grey, no channel 2");
-  test_color_gpix($im->{IMG}, 0, 0, $white_grey);
-
-  print "# with plin\n";
-  ok($im->setmask(mask=>~0), "set to default mask");
-  ok($im->setscanline(x=>0, 'y'=>1, pixels => [$white]), 
-     "set to white all channels");
-  test_color_gpix($im->{IMG}, 0, 1, $white);
-  ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
-  ok($im->setscanline(x=>0, 'y'=>1, pixels=>[$grey]), 
-     "set to grey, no channel 2");
-  test_color_gpix($im->{IMG}, 0, 1, $white_grey);
-
-  # float color tests
-  my $whitef = NCF(1.0, 1.0, 1.0);
-  my $greyf = NCF(0.5, 0.5, 0.5);
-  my $white_greyf = NCF(0.5, 1.0, 0.5);
-
-  print "# with ppixf\n";
-  ok($im->setmask(mask=>~0), "set to default mask");
-  ok($im->setpixel(x=>0, 'y'=>2, color=>$whitef), "set to white all channels");
-  test_colorf_gpix($im->{IMG}, 0, 2, $whitef);
-  ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
-  ok($im->setpixel(x=>0, 'y'=>2, color=>$greyf), "set to grey, no channel 2");
-  test_colorf_gpix($im->{IMG}, 0, 2, $white_greyf);
-
-  print "# with plinf\n";
-  ok($im->setmask(mask=>~0), "set to default mask");
-  ok($im->setscanline(x=>0, 'y'=>3, pixels => [$whitef]), 
-     "set to white all channels");
-  test_colorf_gpix($im->{IMG}, 0, 3, $whitef);
-  ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
-  ok($im->setscanline(x=>0, 'y'=>3, pixels=>[$greyf]), 
-     "set to grey, no channel 2");
-  test_colorf_gpix($im->{IMG}, 0, 3, $white_greyf);
+  mask_tests($im);
 }
 
-sub NCF {
-  return Imager::Color::Float->new(@_);
-}
-
-sub test_colorf_gpix {
-  my ($im, $x, $y, $expected) = @_;
-  my $c = Imager::i_gpixf($im, $x, $y);
-  ok($c, "got gpix ($x, $y)");
-  unless (ok(colorf_cmp($c, $expected) == 0,
-            "got right color ($x, $y)")) {
-    print "# got: (", join(",", ($c->rgba)[0,1,2]), ")\n";
-    print "# expected: (", join(",", ($expected->rgba)[0,1,2]), ")\n";
-  }
-}
-
-sub test_color_gpix {
-  my ($im, $x, $y, $expected) = @_;
-  my $c = Imager::i_get_pixel($im, $x, $y);
-  ok($c, "got gpix ($x, $y)");
-  unless (ok(color_cmp($c, $expected) == 0,
-     "got right color ($x, $y)")) {
-    print "# got: (", join(",", ($c->rgba)[0,1,2]), ")\n";
-    print "# expected: (", join(",", ($expected->rgba)[0,1,2]), ")\n";
-  }
-}
-
-sub test_colorf_glin {
-  my ($im, $x, $y, @pels) = @_;
 
-  my @got = Imager::i_glinf($im, $x, $x+@pels, $y);
-  is(@got, @pels, "check number of pixels ($x, $y)");
-  ok(!grep(colorf_cmp($pels[$_], $got[$_]), 0..$#got),
-     "check colors ($x, $y)");
-}
-
-sub colorf_cmp {
-  my ($c1, $c2) = @_;
-  my @s1 = map { int($_*65535.99) } $c1->rgba;
-  my @s2 = map { int($_*65535.99) } $c2->rgba;
-
-  # print "# (",join(",", @s1[0..2]),") <=> (",join(",", @s2[0..2]),")\n";
-  return $s1[0] <=> $s2[0] 
-    || $s1[1] <=> $s2[1]
-      || $s1[2] <=> $s2[2];
-}
-
-sub color_cmp {
-  my ($c1, $c2) = @_;
-
-  my @s1 = $c1->rgba;
-  my @s2 = $c2->rgba;
-
-  return $s1[0] <=> $s2[0] 
-    || $s1[1] <=> $s2[1]
-      || $s1[2] <=> $s2[2];
-}
index e36d8073e70bf3e11db7698de5fd60ea30ac3596..df67ad3a78d2615cb6edea7f71203e7d89726590 100644 (file)
@@ -180,5 +180,128 @@ sub _sv_str {
   }
 }
 
+
 1;
 
+sub test_colorf_gpix {
+  my ($im, $x, $y, $expected, $epsilon) = @_;
+  my $c = Imager::i_gpixf($im, $x, $y);
+  ok($c, "got gpix ($x, $y)");
+  unless (ok(colorf_cmp($c, $expected, $epsilon) == 0,
+            "got right color ($x, $y)")) {
+    print "# got: (", join(",", ($c->rgba)[0,1,2]), ")\n";
+    print "# expected: (", join(",", ($expected->rgba)[0,1,2]), ")\n";
+  }
+}
+
+sub test_color_gpix {
+  my ($im, $x, $y, $expected) = @_;
+  my $c = Imager::i_get_pixel($im, $x, $y);
+  ok($c, "got gpix ($x, $y)");
+  unless (ok(color_cmp($c, $expected) == 0,
+     "got right color ($x, $y)")) {
+    print "# got: (", join(",", ($c->rgba)[0,1,2]), ")\n";
+    print "# expected: (", join(",", ($expected->rgba)[0,1,2]), ")\n";
+  }
+}
+
+sub test_colorf_glin {
+  my ($im, $x, $y, @pels) = @_;
+
+  my @got = Imager::i_glinf($im, $x, $x+@pels, $y);
+  is(@got, @pels, "check number of pixels ($x, $y)");
+  ok(!grep(colorf_cmp($pels[$_], $got[$_], 0.005), 0..$#got),
+     "check colors ($x, $y)");
+}
+
+sub colorf_cmp {
+  my ($c1, $c2, $epsilon) = @_;
+
+  defined $epsilon or $epsilon = 0;
+
+  my @s1 = $c1->rgba;
+  my @s2 = $c2->rgba;
+
+  # print "# (",join(",", @s1[0..2]),") <=> (",join(",", @s2[0..2]),")\n";
+  return abs($s1[0]-$s2[0]) >= $epsilon && $s1[0] <=> $s2[0] 
+    || abs($s1[1]-$s2[1]) >= $epsilon && $s1[1] <=> $s2[1]
+      || abs($s1[2]-$s2[2]) >= $epsilon && $s1[2] <=> $s2[2];
+}
+
+sub color_cmp {
+  my ($c1, $c2) = @_;
+
+  my @s1 = $c1->rgba;
+  my @s2 = $c2->rgba;
+
+  return $s1[0] <=> $s2[0] 
+    || $s1[1] <=> $s2[1]
+      || $s1[2] <=> $s2[2];
+}
+
+# these test the action of the channel mask on the image supplied
+# which should be an OO image.
+sub mask_tests {
+  my ($im, $epsilon) = @_;
+
+  defined $epsilon or $epsilon = 0;
+
+  # we want to check all four of ppix() and plin(), ppix() and plinf()
+  # basic test procedure:
+  #   first using default/all 1s mask, set to white
+  #   make sure we got white
+  #   set mask to skip a channel, set to grey
+  #   make sure only the right channels set
+
+  print "# channel mask tests\n";
+  # 8-bit color tests
+  my $white = NC(255, 255, 255);
+  my $grey = NC(128, 128, 128);
+  my $white_grey = NC(128, 255, 128);
+
+  print "# with ppix\n";
+  ok($im->setmask(mask=>~0), "set to default mask");
+  ok($im->setpixel(x=>0, 'y'=>0, color=>$white), "set to white all channels");
+  test_color_gpix($im->{IMG}, 0, 0, $white);
+  ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
+  ok($im->setpixel(x=>0, 'y'=>0, color=>$grey), "set to grey, no channel 2");
+  test_color_gpix($im->{IMG}, 0, 0, $white_grey);
+
+  print "# with plin\n";
+  ok($im->setmask(mask=>~0), "set to default mask");
+  ok($im->setscanline(x=>0, 'y'=>1, pixels => [$white]), 
+     "set to white all channels");
+  test_color_gpix($im->{IMG}, 0, 1, $white);
+  ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
+  ok($im->setscanline(x=>0, 'y'=>1, pixels=>[$grey]), 
+     "set to grey, no channel 2");
+  test_color_gpix($im->{IMG}, 0, 1, $white_grey);
+
+  # float color tests
+  my $whitef = NCF(1.0, 1.0, 1.0);
+  my $greyf = NCF(0.5, 0.5, 0.5);
+  my $white_greyf = NCF(0.5, 1.0, 0.5);
+
+  print "# with ppixf\n";
+  ok($im->setmask(mask=>~0), "set to default mask");
+  ok($im->setpixel(x=>0, 'y'=>2, color=>$whitef), "set to white all channels");
+  test_colorf_gpix($im->{IMG}, 0, 2, $whitef, $epsilon);
+  ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
+  ok($im->setpixel(x=>0, 'y'=>2, color=>$greyf), "set to grey, no channel 2");
+  test_colorf_gpix($im->{IMG}, 0, 2, $white_greyf, $epsilon);
+
+  print "# with plinf\n";
+  ok($im->setmask(mask=>~0), "set to default mask");
+  ok($im->setscanline(x=>0, 'y'=>3, pixels => [$whitef]), 
+     "set to white all channels");
+  test_colorf_gpix($im->{IMG}, 0, 3, $whitef, $epsilon);
+  ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
+  ok($im->setscanline(x=>0, 'y'=>3, pixels=>[$greyf]), 
+     "set to grey, no channel 2");
+  test_colorf_gpix($im->{IMG}, 0, 3, $white_greyf, $epsilon);
+
+}
+
+sub NCF {
+  return Imager::Color::Float->new(@_);
+}