3 use Test::More tests => 16;
7 -d "testout" or mkdir "testout";
9 Imager::init('log'=>'testout/t90cc.log');
12 my $img=Imager->new();
13 ok($img->open(file=>'testimg/scale.ppm'), 'load test image')
14 or print "failed: ",$img->{ERRSTR},"\n";
16 ok(defined($img->getcolorcount(maxcolors=>10000)), 'check color count is small enough');
17 print "# color count: ".$img->getcolorcount()."\n";
18 is($img->getcolorcount(), 86, 'expected number of colors');
19 is($img->getcolorcount(maxcolors => 50), undef, 'check overflow handling');
23 my $black = Imager::Color->new(0, 0, 0);
24 my $blue = Imager::Color->new(0, 0, 255);
25 my $red = Imager::Color->new(255, 0, 0);
27 my $im = Imager->new(xsize=>50, ysize=>50);
29 my $count = $im->getcolorcount();
30 is ($count, 1, "getcolorcount is 1");
31 my @colour_usage = $im->getcolorusage();
32 is_deeply (\@colour_usage, [2500], "2500 are in black");
34 $im->box(filled=>1, color=>$blue, xmin=>25);
36 $count = $im->getcolorcount();
37 is ($count, 2, "getcolorcount is 2");
38 @colour_usage = $im->getcolorusage();
39 is_deeply(\@colour_usage, [1250, 1250] , "1250, 1250: Black and blue");
41 $im->box(filled=>1, color=>$red, ymin=>25);
43 $count = $im->getcolorcount();
44 is ($count, 3, "getcolorcount is 3");
45 @colour_usage = $im->getcolorusage();
46 is_deeply(\@colour_usage, [625, 625, 1250] ,
47 "625, 625, 1250: Black blue and red");
48 @colour_usage = $im->getcolorusage(maxcolors => 2);
49 is(@colour_usage, 0, 'test overflow check');
51 my $colour_usage = $im->getcolorusagehash();
52 my $red_pack = pack("CCC", 255, 0, 0);
53 my $blue_pack = pack("CCC", 0, 0, 255);
54 my $black_pack = pack("CCC", 0, 0, 0);
55 is_deeply( $colour_usage,
56 { $black_pack => 625, $blue_pack => 625, $red_pack => 1250 },
57 "625, 625, 1250: Black blue and red (hash)");
58 is($im->getcolorusagehash(maxcolors => 2), undef,
59 'test overflow check');
61 # test with a greyscale image
62 my $im_g = $im->convert(preset => 'grey');
63 # since the grey preset scales each source channel differently
64 # each of the original colors will be converted to different colors
65 is($im_g->getcolorcount, 3, '3 colors (grey)');
66 is_deeply([ $im_g->getcolorusage ], [ 625, 625, 1250 ],
67 'color counts (grey)');
68 is_deeply({ "\x00" => 625, "\x12" => 625, "\x38" => 1250 },
69 $im_g->getcolorusagehash,
70 'color usage hash (grey)');