<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Imager::Test;
use strict;
use Test::Builder;
require Exporter;
use vars qw(@ISA @EXPORT_OK $VERSION);

$VERSION = "1.000";

@ISA = qw(Exporter);
@EXPORT_OK = 
  qw(
     diff_text_with_nul 
     test_image_raw
     test_image_16
     test_image
     test_image_double 
     is_color1
     is_color3
     is_color4
     is_color_close3
     is_fcolor1
     is_fcolor3
     is_fcolor4
     color_cmp
     is_image
     is_imaged
     is_image_similar
     isnt_image
     image_bounds_checks
     mask_tests
     test_colorf_gpix
     test_color_gpix
     test_colorf_glin);

sub diff_text_with_nul {
  my ($desc, $text1, $text2, @params) = @_;

  my $builder = Test::Builder-&gt;new;

  print "# $desc\n";
  my $imbase = Imager-&gt;new(xsize =&gt; 100, ysize =&gt; 100);
  my $imcopy = Imager-&gt;new(xsize =&gt; 100, ysize =&gt; 100);

  $builder-&gt;ok($imbase-&gt;string(x =&gt; 5, 'y' =&gt; 50, size =&gt; 20,
			       string =&gt; $text1,
			       @params), "$desc - draw text1");
  $builder-&gt;ok($imcopy-&gt;string(x =&gt; 5, 'y' =&gt; 50, size =&gt; 20,
			       string =&gt; $text2,
			       @params), "$desc - draw text2");
  $builder-&gt;isnt_num(Imager::i_img_diff($imbase-&gt;{IMG}, $imcopy-&gt;{IMG}), 0,
		     "$desc - check result different");
}

sub is_color3($$$$$) {
  my ($color, $red, $green, $blue, $comment) = @_;

  my $builder = Test::Builder-&gt;new;

  unless (defined $color) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is undef");
    return;
  }
  unless ($color-&gt;can('rgba')) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is not a color object");
    return;
  }

  my ($cr, $cg, $cb) = $color-&gt;rgba;
  unless ($builder-&gt;ok($cr == $red &amp;&amp; $cg == $green &amp;&amp; $cb == $blue, $comment)) {
    print &lt;&lt;END_DIAG;
Color mismatch:
  Red: $red vs $cr
Green: $green vs $cg
 Blue: $blue vs $cb
END_DIAG
    return;
  }

  return 1;
}

sub is_color_close3($$$$$$) {
  my ($color, $red, $green, $blue, $tolerance, $comment) = @_;

  my $builder = Test::Builder-&gt;new;

  unless (defined $color) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is undef");
    return;
  }
  unless ($color-&gt;can('rgba')) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is not a color object");
    return;
  }

  my ($cr, $cg, $cb) = $color-&gt;rgba;
  unless ($builder-&gt;ok(abs($cr - $red) &lt;= $tolerance
		       &amp;&amp; abs($cg - $green) &lt;= $tolerance
		       &amp;&amp; abs($cb - $blue) &lt;= $tolerance, $comment)) {
    $builder-&gt;diag(&lt;&lt;END_DIAG);
Color out of tolerance ($tolerance):
  Red: expected $red vs received $cr
Green: expected $green vs received $cg
 Blue: expected $blue vs received $cb
END_DIAG
    return;
  }

  return 1;
}

sub is_color4($$$$$$) {
  my ($color, $red, $green, $blue, $alpha, $comment) = @_;

  my $builder = Test::Builder-&gt;new;

  unless (defined $color) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is undef");
    return;
  }
  unless ($color-&gt;can('rgba')) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is not a color object");
    return;
  }

  my ($cr, $cg, $cb, $ca) = $color-&gt;rgba;
  unless ($builder-&gt;ok($cr == $red &amp;&amp; $cg == $green &amp;&amp; $cb == $blue 
		       &amp;&amp; $ca == $alpha, $comment)) {
    $builder-&gt;diag(&lt;&lt;END_DIAG);
Color mismatch:
  Red: $cr vs $red
Green: $cg vs $green
 Blue: $cb vs $blue
Alpha: $ca vs $alpha
END_DIAG
    return;
  }

  return 1;
}

sub is_fcolor4($$$$$$;$) {
  my ($color, $red, $green, $blue, $alpha, $comment_or_diff, $comment_or_undef) = @_;
  my ($comment, $mindiff);
  if (defined $comment_or_undef) {
    ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
  }
  else {
    ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
  }

  my $builder = Test::Builder-&gt;new;

  unless (defined $color) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is undef");
    return;
  }
  unless ($color-&gt;can('rgba')) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is not a color object");
    return;
  }

  my ($cr, $cg, $cb, $ca) = $color-&gt;rgba;
  unless ($builder-&gt;ok(abs($cr - $red) &lt;= $mindiff
		       &amp;&amp; abs($cg - $green) &lt;= $mindiff
		       &amp;&amp; abs($cb - $blue) &lt;= $mindiff
		       &amp;&amp; abs($ca - $alpha) &lt;= $mindiff, $comment)) {
    $builder-&gt;diag(&lt;&lt;END_DIAG);
Color mismatch:
  Red: $cr vs $red
Green: $cg vs $green
 Blue: $cb vs $blue
Alpha: $ca vs $alpha
END_DIAG
    return;
  }

  return 1;
}

sub is_fcolor1($$$;$) {
  my ($color, $grey, $comment_or_diff, $comment_or_undef) = @_;
  my ($comment, $mindiff);
  if (defined $comment_or_undef) {
    ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
  }
  else {
    ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
  }

  my $builder = Test::Builder-&gt;new;

  unless (defined $color) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is undef");
    return;
  }
  unless ($color-&gt;can('rgba')) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is not a color object");
    return;
  }

  my ($cgrey) = $color-&gt;rgba;
  unless ($builder-&gt;ok(abs($cgrey - $grey) &lt;= $mindiff, $comment)) {
    print &lt;&lt;END_DIAG;
Color mismatch:
  Gray: $cgrey vs $grey
END_DIAG
    return;
  }

  return 1;
}

sub is_fcolor3($$$$$;$) {
  my ($color, $red, $green, $blue, $comment_or_diff, $comment_or_undef) = @_;
  my ($comment, $mindiff);
  if (defined $comment_or_undef) {
    ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
  }
  else {
    ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
  }

  my $builder = Test::Builder-&gt;new;

  unless (defined $color) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is undef");
    return;
  }
  unless ($color-&gt;can('rgba')) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is not a color object");
    return;
  }

  my ($cr, $cg, $cb) = $color-&gt;rgba;
  unless ($builder-&gt;ok(abs($cr - $red) &lt;= $mindiff
		       &amp;&amp; abs($cg - $green) &lt;= $mindiff
		       &amp;&amp; abs($cb - $blue) &lt;= $mindiff, $comment)) {
    $builder-&gt;diag(&lt;&lt;END_DIAG);
Color mismatch:
  Red: $cr vs $red
Green: $cg vs $green
 Blue: $cb vs $blue
END_DIAG
    return;
  }

  return 1;
}

sub is_color1($$$) {
  my ($color, $grey, $comment) = @_;

  my $builder = Test::Builder-&gt;new;

  unless (defined $color) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is undef");
    return;
  }
  unless ($color-&gt;can('rgba')) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("color is not a color object");
    return;
  }

  my ($cgrey) = $color-&gt;rgba;
  unless ($builder-&gt;ok($cgrey == $grey, $comment)) {
    $builder-&gt;diag(&lt;&lt;END_DIAG);
Color mismatch:
  Grey: $grey vs $cgrey
END_DIAG
    return;
  }

  return 1;
}

sub test_image_raw {
  my $green=Imager::i_color_new(0,255,0,255);
  my $blue=Imager::i_color_new(0,0,255,255);
  my $red=Imager::i_color_new(255,0,0,255);
  
  my $img=Imager::ImgRaw::new(150,150,3);
  
  Imager::i_box_filled($img,70,25,130,125,$green);
  Imager::i_box_filled($img,20,25,80,125,$blue);
  Imager::i_arc($img,75,75,30,0,361,$red);
  Imager::i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);

  $img;
}

sub test_image {
  my $green = Imager::Color-&gt;new(0, 255, 0, 255);
  my $blue  = Imager::Color-&gt;new(0, 0, 255, 255);
  my $red   = Imager::Color-&gt;new(255, 0, 0, 255);
  my $img = Imager-&gt;new(xsize =&gt; 150, ysize =&gt; 150);
  $img-&gt;box(filled =&gt; 1, color =&gt; $green, box =&gt; [ 70, 24, 130, 124 ]);
  $img-&gt;box(filled =&gt; 1, color =&gt; $blue,  box =&gt; [ 20, 26, 80, 126 ]);
  $img-&gt;arc(x =&gt; 75, y =&gt; 75, r =&gt; 30, color =&gt; $red);
  $img-&gt;filter(type =&gt; 'conv', coef =&gt; [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);

  $img;
}

sub test_image_16 {
  my $green = Imager::Color-&gt;new(0, 255, 0, 255);
  my $blue  = Imager::Color-&gt;new(0, 0, 255, 255);
  my $red   = Imager::Color-&gt;new(255, 0, 0, 255);
  my $img = Imager-&gt;new(xsize =&gt; 150, ysize =&gt; 150, bits =&gt; 16);
  $img-&gt;box(filled =&gt; 1, color =&gt; $green, box =&gt; [ 70, 25, 130, 125 ]);
  $img-&gt;box(filled =&gt; 1, color =&gt; $blue,  box =&gt; [ 20, 25, 80, 125 ]);
  $img-&gt;arc(x =&gt; 75, y =&gt; 75, r =&gt; 30, color =&gt; $red);
  $img-&gt;filter(type =&gt; 'conv', coef =&gt; [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);

  $img;
}

sub test_image_double {
  my $green = Imager::Color-&gt;new(0, 255, 0, 255);
  my $blue  = Imager::Color-&gt;new(0, 0, 255, 255);
  my $red   = Imager::Color-&gt;new(255, 0, 0, 255);
  my $img = Imager-&gt;new(xsize =&gt; 150, ysize =&gt; 150, bits =&gt; 'double');
  $img-&gt;box(filled =&gt; 1, color =&gt; $green, box =&gt; [ 70, 25, 130, 125 ]);
  $img-&gt;box(filled =&gt; 1, color =&gt; $blue,  box =&gt; [ 20, 25, 80, 125 ]);
  $img-&gt;arc(x =&gt; 75, y =&gt; 75, r =&gt; 30, color =&gt; $red);
  $img-&gt;filter(type =&gt; 'conv', coef =&gt; [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);

  $img;
}

sub _low_image_diff_check {
  my ($left, $right, $comment) = @_;

  my $builder = Test::Builder-&gt;new;

  unless (defined $left) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("left is undef");
    return;
  } 
  unless (defined $right) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("right is undef");
    return;
  }
  unless ($left-&gt;{IMG}) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("left image has no low level object");
    return;
  }
  unless ($right-&gt;{IMG}) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("right image has no low level object");
    return;
  }
  unless ($left-&gt;getwidth == $right-&gt;getwidth) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("left width " . $left-&gt;getwidth . " vs right width " 
                   . $right-&gt;getwidth);
    return;
  }
  unless ($left-&gt;getheight == $right-&gt;getheight) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("left height " . $left-&gt;getheight . " vs right height " 
                   . $right-&gt;getheight);
    return;
  }
  unless ($left-&gt;getchannels == $right-&gt;getchannels) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("left channels " . $left-&gt;getchannels . " vs right channels " 
                   . $right-&gt;getchannels);
    return;
  }

  return 1;
}

sub is_image_similar($$$$) {
  my ($left, $right, $limit, $comment) = @_;

  {
    local $Test::Builder::Level = $Test::Builder::Level + 1;

    _low_image_diff_check($left, $right, $comment)
      or return;
  }

  my $builder = Test::Builder-&gt;new;

  my $diff = Imager::i_img_diff($left-&gt;{IMG}, $right-&gt;{IMG});
  if ($diff &gt; $limit) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("image data difference &gt; $limit - $diff");
   
    if ($limit == 0) {
      # find the first mismatch
      PIXELS:
      for my $y (0 .. $left-&gt;getheight()-1) {
	for my $x (0.. $left-&gt;getwidth()-1) {
	  my @lsamples = $left-&gt;getsamples(x =&gt; $x, y =&gt; $y, width =&gt; 1);
	  my @rsamples = $right-&gt;getsamples(x =&gt; $x, y =&gt; $y, width =&gt; 1);
          if ("@lsamples" ne "@rsamples") {
            $builder-&gt;diag("first mismatch at ($x, $y) - @lsamples vs @rsamples");
            last PIXELS;
          }
	}
      }
    }

    return;
  }
  
  return $builder-&gt;ok(1, $comment);
}

sub is_image($$$) {
  my ($left, $right, $comment) = @_;

  local $Test::Builder::Level = $Test::Builder::Level + 1;

  return is_image_similar($left, $right, 0, $comment);
}

sub is_imaged($$$) {
  my ($left, $right, $comment) = @_;

  {
    local $Test::Builder::Level = $Test::Builder::Level + 1;

    _low_image_diff_check($left, $right, $comment)
      or return;
  }

  my $builder = Test::Builder-&gt;new;

  my $diff = Imager::i_img_diffd($left-&gt;{IMG}, $right-&gt;{IMG});
  if ($diff &gt; 0) {
    $builder-&gt;ok(0, $comment);
    $builder-&gt;diag("image data difference: $diff");
   
    # find the first mismatch
  PIXELS:
    for my $y (0 .. $left-&gt;getheight()-1) {
      for my $x (0.. $left-&gt;getwidth()-1) {
	my @lsamples = $left-&gt;getsamples(x =&gt; $x, y =&gt; $y, width =&gt; 1);
	my @rsamples = $right-&gt;getsamples(x =&gt; $x, y =&gt; $y, width =&gt; 1);
	if ("@lsamples" ne "@rsamples") {
	  $builder-&gt;diag("first mismatch at ($x, $y) - @lsamples vs @rsamples");
	  last PIXELS;
	}
      }
    }

    return;
  }
  
  return $builder-&gt;ok(1, $comment);
}

sub isnt_image {
  my ($left, $right, $comment) = @_;

  my $builder = Test::Builder-&gt;new;

  my $diff = Imager::i_img_diff($left-&gt;{IMG}, $right-&gt;{IMG});

  return $builder-&gt;ok($diff, "$comment");
}

sub image_bounds_checks {
  my $im = shift;

  my $builder = Test::Builder-&gt;new;

  $builder-&gt;ok(!$im-&gt;getpixel(x =&gt; -1, y =&gt; 0), 'bounds check get (-1, 0)');
  $builder-&gt;ok(!$im-&gt;getpixel(x =&gt; 10, y =&gt; 0), 'bounds check get (10, 0)');
  $builder-&gt;ok(!$im-&gt;getpixel(x =&gt; 0, y =&gt; -1), 'bounds check get (0, -1)');
  $builder-&gt;ok(!$im-&gt;getpixel(x =&gt; 0, y =&gt; 10), 'bounds check get (0, 10)');
  $builder-&gt;ok(!$im-&gt;getpixel(x =&gt; -1, y =&gt; 0), 'bounds check get (-1, 0) float');
  $builder-&gt;ok(!$im-&gt;getpixel(x =&gt; 10, y =&gt; 0), 'bounds check get (10, 0) float');
  $builder-&gt;ok(!$im-&gt;getpixel(x =&gt; 0, y =&gt; -1), 'bounds check get (0, -1) float');
  $builder-&gt;ok(!$im-&gt;getpixel(x =&gt; 0, y =&gt; 10), 'bounds check get (0, 10) float');
  my $black = Imager::Color-&gt;new(0, 0, 0);
  require Imager::Color::Float;
  my $blackf = Imager::Color::Float-&gt;new(0, 0, 0);
  $builder-&gt;ok(!$im-&gt;setpixel(x =&gt; -1, y =&gt; 0, color =&gt; $black), 'bounds check set (-1, 0)');
  $builder-&gt;ok(!$im-&gt;setpixel(x =&gt; 10, y =&gt; 0, color =&gt; $black), 'bounds check set (10, 0)');
  $builder-&gt;ok(!$im-&gt;setpixel(x =&gt; 0, y =&gt; -1, color =&gt; $black), 'bounds check set (0, -1)');
  $builder-&gt;ok(!$im-&gt;setpixel(x =&gt; 0, y =&gt; 10, color =&gt; $black), 'bounds check set (0, 10)');
  $builder-&gt;ok(!$im-&gt;setpixel(x =&gt; -1, y =&gt; 0, color =&gt; $blackf), 'bounds check set (-1, 0) float');
  $builder-&gt;ok(!$im-&gt;setpixel(x =&gt; 10, y =&gt; 0, color =&gt; $blackf), 'bounds check set (10, 0) float');
  $builder-&gt;ok(!$im-&gt;setpixel(x =&gt; 0, y =&gt; -1, color =&gt; $blackf), 'bounds check set (0, -1) float');
  $builder-&gt;ok(!$im-&gt;setpixel(x =&gt; 0, y =&gt; 10, color =&gt; $blackf), 'bounds check set (0, 10) float');
}

sub test_colorf_gpix {
  my ($im, $x, $y, $expected, $epsilon, $comment) = @_;

  my $builder = Test::Builder-&gt;new;
  
  defined $comment or $comment = '';

  my $c = Imager::i_gpixf($im, $x, $y);
  unless ($c) {
    $builder-&gt;ok(0, "$comment - retrieve color at ($x,$y)");
    return;
  }
  unless ($builder-&gt;ok(colorf_cmp($c, $expected, $epsilon) == 0,
	     "$comment - got right color ($x, $y)")) {
    my @c = $c-&gt;rgba;
    my @exp = $expected-&gt;rgba;
    $builder-&gt;diag(&lt;&lt;EOS);
# got: ($c[0], $c[1], $c[2])
# expected: ($exp[0], $exp[1], $exp[2])
EOS
  }
  1;
}

sub test_color_gpix {
  my ($im, $x, $y, $expected, $comment) = @_;

  my $builder = Test::Builder-&gt;new;
  
  defined $comment or $comment = '';
  my $c = Imager::i_get_pixel($im, $x, $y);
  unless ($c) {
    $builder-&gt;ok(0, "$comment - retrieve color at ($x,$y)");
    return;
  }
  unless ($builder-&gt;ok(color_cmp($c, $expected) == 0,
     "got right color ($x, $y)")) {
    my @c = $c-&gt;rgba;
    my @exp = $expected-&gt;rgba;
    $builder-&gt;diag(&lt;&lt;EOS);
# got: ($c[0], $c[1], $c[2])
# expected: ($exp[0], $exp[1], $exp[2])
EOS
    return;
  }

  return 1;
}

sub test_colorf_glin {
  my ($im, $x, $y, $pels, $comment) = @_;

  my $builder = Test::Builder-&gt;new;
  
  my @got = Imager::i_glinf($im, $x, $x+@$pels, $y);
  @got == @$pels
    or return $builder-&gt;is_num(scalar(@got), scalar(@$pels), "$comment - pixels retrieved");
  
  return $builder-&gt;ok(!grep(colorf_cmp($pels-&gt;[$_], $got[$_], 0.005), 0..$#got),
     "$comment - check colors ($x, $y)");
}

sub colorf_cmp {
  my ($c1, $c2, $epsilon) = @_;

  defined $epsilon or $epsilon = 0;

  my @s1 = $c1-&gt;rgba;
  my @s2 = $c2-&gt;rgba;

  # print "# (",join(",", @s1[0..2]),") &lt;=&gt; (",join(",", @s2[0..2]),")\n";
  return abs($s1[0]-$s2[0]) &gt;= $epsilon &amp;&amp; $s1[0] &lt;=&gt; $s2[0] 
    || abs($s1[1]-$s2[1]) &gt;= $epsilon &amp;&amp; $s1[1] &lt;=&gt; $s2[1]
      || abs($s1[2]-$s2[2]) &gt;= $epsilon &amp;&amp; $s1[2] &lt;=&gt; $s2[2];
}

sub color_cmp {
  my ($c1, $c2) = @_;

  my @s1 = $c1-&gt;rgba;
  my @s2 = $c2-&gt;rgba;

  return $s1[0] &lt;=&gt; $s2[0] 
    || $s1[1] &lt;=&gt; $s2[1]
      || $s1[2] &lt;=&gt; $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) = @_;

  my $builder = Test::Builder-&gt;new;

  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 = Imager::NC(255, 255, 255);
  my $grey = Imager::NC(128, 128, 128);
  my $white_grey = Imager::NC(128, 255, 128);

  print "# with ppix\n";
  $builder-&gt;ok($im-&gt;setmask(mask=&gt;~0), "set to default mask");
  $builder-&gt;ok($im-&gt;setpixel(x=&gt;0, 'y'=&gt;0, color=&gt;$white), "set to white all channels");
  test_color_gpix($im-&gt;{IMG}, 0, 0, $white, "ppix");
  $builder-&gt;ok($im-&gt;setmask(mask=&gt;0xF-0x2), "set channel to exclude channel1");
  $builder-&gt;ok($im-&gt;setpixel(x=&gt;0, 'y'=&gt;0, color=&gt;$grey), "set to grey, no channel 2");
  test_color_gpix($im-&gt;{IMG}, 0, 0, $white_grey, "ppix masked");

  print "# with plin\n";
  $builder-&gt;ok($im-&gt;setmask(mask=&gt;~0), "set to default mask");
  $builder-&gt;ok($im-&gt;setscanline(x=&gt;0, 'y'=&gt;1, pixels =&gt; [$white]), 
     "set to white all channels");
  test_color_gpix($im-&gt;{IMG}, 0, 1, $white, "plin");
  $builder-&gt;ok($im-&gt;setmask(mask=&gt;0xF-0x2), "set channel to exclude channel1");
  $builder-&gt;ok($im-&gt;setscanline(x=&gt;0, 'y'=&gt;1, pixels=&gt;[$grey]), 
     "set to grey, no channel 2");
  test_color_gpix($im-&gt;{IMG}, 0, 1, $white_grey, "plin masked");

  # float color tests
  my $whitef = Imager::NCF(1.0, 1.0, 1.0);
  my $greyf = Imager::NCF(0.5, 0.5, 0.5);
  my $white_greyf = Imager::NCF(0.5, 1.0, 0.5);

  print "# with ppixf\n";
  $builder-&gt;ok($im-&gt;setmask(mask=&gt;~0), "set to default mask");
  $builder-&gt;ok($im-&gt;setpixel(x=&gt;0, 'y'=&gt;2, color=&gt;$whitef), "set to white all channels");
  test_colorf_gpix($im-&gt;{IMG}, 0, 2, $whitef, $epsilon, "ppixf");
  $builder-&gt;ok($im-&gt;setmask(mask=&gt;0xF-0x2), "set channel to exclude channel1");
  $builder-&gt;ok($im-&gt;setpixel(x=&gt;0, 'y'=&gt;2, color=&gt;$greyf), "set to grey, no channel 2");
  test_colorf_gpix($im-&gt;{IMG}, 0, 2, $white_greyf, $epsilon, "ppixf masked");

  print "# with plinf\n";
  $builder-&gt;ok($im-&gt;setmask(mask=&gt;~0), "set to default mask");
  $builder-&gt;ok($im-&gt;setscanline(x=&gt;0, 'y'=&gt;3, pixels =&gt; [$whitef]), 
     "set to white all channels");
  test_colorf_gpix($im-&gt;{IMG}, 0, 3, $whitef, $epsilon, "plinf");
  $builder-&gt;ok($im-&gt;setmask(mask=&gt;0xF-0x2), "set channel to exclude channel1");
  $builder-&gt;ok($im-&gt;setscanline(x=&gt;0, 'y'=&gt;3, pixels=&gt;[$greyf]), 
     "set to grey, no channel 2");
  test_colorf_gpix($im-&gt;{IMG}, 0, 3, $white_greyf, $epsilon, "plinf masked");

}

1;

__END__

=head1 NAME

Imager::Test - common functions used in testing Imager

=head1 SYNOPSIS

  use Imager::Test 'diff_text_with_nul';
  diff_text_with_nul($test_name, $text1, $text2, @string_options);

=head1 DESCRIPTION

This is a repository of functions used in testing Imager.

Some functions will only be useful in testing Imager itself, while
others should be useful in testing modules that use Imager.

No functions are exported by default.

=head1 FUNCTIONS

=head2 Test functions

=for stopwords OO

=over

=item is_color1($color, $grey, $comment)

Tests if the first channel of $color matches $grey.

=item is_color3($color, $red, $green, $blue, $comment)

Tests if $color matches the given ($red, $green, $blue)

=item is_color4($color, $red, $green, $blue, $alpha, $comment)

Tests if $color matches the given ($red, $green, $blue, $alpha)

=item is_fcolor1($fcolor, $grey, $comment)

=item is_fcolor1($fcolor, $grey, $epsilon, $comment)

Tests if $fcolor's first channel is within $epsilon of ($grey).  For
the first form $epsilon is taken as 0.001.

=item is_fcolor3($fcolor, $red, $green, $blue, $comment)

=item is_fcolor3($fcolor, $red, $green, $blue, $epsilon, $comment)

Tests if $fcolor's channels are within $epsilon of ($red, $green,
$blue).  For the first form $epsilon is taken as 0.001.

=item is_fcolor4($fcolor, $red, $green, $blue, $alpha, $comment)

=item is_fcolor4($fcolor, $red, $green, $blue, $alpha, $epsilon, $comment)

Tests if $fcolor's channels are within $epsilon of ($red, $green,
$blue, $alpha).  For the first form $epsilon is taken as 0.001.

=item is_image($im1, $im2, $comment)

Tests if the 2 images have the same content.  Both images must be
defined, have the same width, height, channels and the same color in
each pixel.  The color comparison is done at 8-bits per pixel.  The
color representation such as direct vs paletted, bits per sample are
not checked.  Equivalent to is_image_similar($im1, $im2, 0, $comment).

=item is_imaged($im, $im2, $comment)

Tests if the two images have the same content at the double/sample
level.

=item is_image_similar($im1, $im2, $maxdiff, $comment)

Tests if the 2 images have similar content.  Both images must be
defined, have the same width, height and channels.  The cum of the
squares of the differences of each sample are calculated and must be
less than or equal to I&lt;$maxdiff&gt; for the test to pass.  The color
comparison is done at 8-bits per pixel.  The color representation such
as direct vs paletted, bits per sample are not checked.

=item isnt_image($im1, $im2, $comment)

Tests that the two images are different.  For regressions tests where
something (like text output of "0") produced no change, but should
have produced a change.

=item test_colorf_gpix($im, $x, $y, $expected, $epsilon, $comment)

Retrieves the pixel ($x,$y) from the low-level image $im and compares
it to the floating point color $expected, with a tolerance of epsilon.

=item test_color_gpix($im, $x, $y, $expected, $comment)

Retrieves the pixel ($x,$y) from the low-level image $im and compares
it to the floating point color $expected.

=item test_colorf_glin($im, $x, $y, $pels, $comment)

Retrieves the floating point pixels ($x, $y)-[$x+@$pels, $y] from the
low level image $im and compares them against @$pels.

=item is_color_close3($color, $red, $green, $blue, $tolerance, $comment)

Tests if $color's first three channels are within $tolerance of ($red,
$green, $blue).

=back

=head2 Test suite functions

Functions that perform one or more tests, typically used to test
various parts of Imager's implementation.

=over

=item image_bounds_checks($im)

Attempts to write to various pixel positions outside the edge of the
image to ensure that it fails in those locations.

Any new image type should pass these tests.  Does 16 separate tests.

=item mask_tests($im, $epsilon)

Perform a standard set of mask tests on the OO image $im.  Does 24
separate tests.

=item diff_text_with_nul($test_name, $text1, $text2, @options)

Creates 2 test images and writes $text1 to the first image and $text2
to the second image with the string() method.  Each call adds 3
C&lt;ok&gt;/C&lt;not ok&gt; to the output of the test script.

Extra options that should be supplied include the font and either a
color or channel parameter.

This was explicitly created for regression tests on #21770.

=back

=head2 Helper functions

=over

=item test_image_raw()

Returns a 150x150x3 Imager::ImgRaw test image.

=item test_image()

Returns a 150x150x3 8-bit/sample OO test image.

=item test_image_16()

Returns a 150x150x3 16-bit/sample OO test image.

=item test_image_double()

Returns a 150x150x3 double/sample OO test image.

=item color_cmp($c1, $c2)

Performs an ordering of 3-channel colors (like &lt;=&gt;).

=item colorf_cmp($c1, $c2)

Performs an ordering of 3-channel floating point colors (like &lt;=&gt;).

=back

=head1 AUTHOR

Tony Cook &lt;tony@develop-help.com&gt;

=cut
</pre></body></html>