| 1 |
#!perl -w |
| 2 |
use strict; |
| 3 |
use Test::More tests => 22; |
| 4 |
|
| 5 |
use Imager; |
| 6 |
|
| 7 |
-d "testout" or mkdir "testout"; |
| 8 |
|
| 9 |
Imager::init('log'=>'testout/t90cc.log'); |
| 10 |
|
| 11 |
{ |
| 12 |
my $img=Imager->new(); |
| 13 |
ok($img->open(file=>'testimg/scale.ppm'), 'load test image') |
| 14 |
or print "failed: ",$img->{ERRSTR},"\n"; |
| 15 |
|
| 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'); |
| 20 |
} |
| 21 |
|
| 22 |
{ |
| 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); |
| 26 |
|
| 27 |
my $im = Imager->new(xsize=>50, ysize=>50); |
| 28 |
|
| 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"); |
| 33 |
|
| 34 |
$im->box(filled=>1, color=>$blue, xmin=>25); |
| 35 |
|
| 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"); |
| 40 |
|
| 41 |
$im->box(filled=>1, color=>$red, ymin=>25); |
| 42 |
|
| 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'); |
| 50 |
|
| 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'); |
| 60 |
|
| 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)'); |
| 71 |
} |
| 72 |
|
| 73 |
{ |
| 74 |
my $empty = Imager->new; |
| 75 |
is($empty->getcolorcount, undef, "can't getcolorcount an empty image"); |
| 76 |
is($empty->errstr, "getcolorcount: empty input image", |
| 77 |
"check error message"); |
| 78 |
is($empty->getcolorusagehash, undef, "can't getcolorusagehash an empty image"); |
| 79 |
is($empty->errstr, "getcolorusagehash: empty input image", |
| 80 |
"check error message"); |
| 81 |
is($empty->getcolorusage, undef, "can't getcolorusage an empty image"); |
| 82 |
is($empty->errstr, "getcolorusage: empty input image", |
| 83 |
"check error message"); |
| 84 |
} |