Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 71 lines (57 sloc) 2.416 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#!perl -w
use strict;
use Test::More tests => 16;

use Imager;

-d "testout" or mkdir "testout";

Imager::init('log'=>'testout/t90cc.log');

{
  my $img=Imager->new();
  ok($img->open(file=>'testimg/scale.ppm'), 'load test image')
    or print "failed: ",$img->{ERRSTR},"\n";
  
  ok(defined($img->getcolorcount(maxcolors=>10000)), 'check color count is small enough');
  print "# color count: ".$img->getcolorcount()."\n";
  is($img->getcolorcount(), 86, 'expected number of colors');
  is($img->getcolorcount(maxcolors => 50), undef, 'check overflow handling');
}

{
  my $black = Imager::Color->new(0, 0, 0);
  my $blue = Imager::Color->new(0, 0, 255);
  my $red = Imager::Color->new(255, 0, 0);
  
  my $im = Imager->new(xsize=>50, ysize=>50);
  
  my $count = $im->getcolorcount();
  is ($count, 1, "getcolorcount is 1");
  my @colour_usage = $im->getcolorusage();
  is_deeply (\@colour_usage, [2500], "2500 are in black");
  
  $im->box(filled=>1, color=>$blue, xmin=>25);
  
  $count = $im->getcolorcount();
  is ($count, 2, "getcolorcount is 2");
  @colour_usage = $im->getcolorusage();
  is_deeply(\@colour_usage, [1250, 1250] , "1250, 1250: Black and blue");
  
  $im->box(filled=>1, color=>$red, ymin=>25);
  
  $count = $im->getcolorcount();
  is ($count, 3, "getcolorcount is 3");
  @colour_usage = $im->getcolorusage();
  is_deeply(\@colour_usage, [625, 625, 1250] ,
"625, 625, 1250: Black blue and red");
  @colour_usage = $im->getcolorusage(maxcolors => 2);
  is(@colour_usage, 0, 'test overflow check');
  
  my $colour_usage = $im->getcolorusagehash();
  my $red_pack = pack("CCC", 255, 0, 0);
  my $blue_pack = pack("CCC", 0, 0, 255);
  my $black_pack = pack("CCC", 0, 0, 0);
  is_deeply( $colour_usage,
{ $black_pack => 625, $blue_pack => 625, $red_pack => 1250 },
"625, 625, 1250: Black blue and red (hash)");
  is($im->getcolorusagehash(maxcolors => 2), undef,
     'test overflow check');

  # test with a greyscale image
  my $im_g = $im->convert(preset => 'grey');
  # since the grey preset scales each source channel differently
  # each of the original colors will be converted to different colors
  is($im_g->getcolorcount, 3, '3 colors (grey)');
  is_deeply([ $im_g->getcolorusage ], [ 625, 625, 1250 ],
'color counts (grey)');
  is_deeply({ "\x00" => 625, "\x12" => 625, "\x38" => 1250 },
$im_g->getcolorusagehash,
'color usage hash (grey)');
}
Something went wrong with that request. Please try again.