copyrights, we depend on more recent Imager now
[imager-graph.git] / t / t10pie.t
CommitLineData
35574351
TC
1#!perl -w
2use strict;
3use Imager::Graph::Pie;
bfcf9414 4use Test::More;
35574351
TC
5
6-d 'testout'
7 or mkdir "testout", 0700
8 or die "Could not create output directory: $!";
9
10++$|;
35574351
TC
11
12my $testnum = 1;
13
14use Imager qw(:handy);
15
bfcf9414 16my $fontfile = 'ImUgly.ttf';
d0c5f540 17my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
bfcf9414 18 or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
35574351
TC
19
20my @data = ( 100, 180, 80, 20, 2, 1, 0.5 );
21my @labels = qw(alpha beta gamma delta epsilon phi gi);
22
bfcf9414
TC
23plan tests => 11;
24
35574351
TC
25my $pie = Imager::Graph::Pie->new;
26ok($pie, "creating pie chart object");
27
28# this may change output quality too
35574351
TC
29
30print "# Imager version: $Imager::VERSION\n";
31print "# Font type: ",ref $font,"\n";
32
33my $img1 = $pie->draw(data=>\@data, labels=>\@labels, font=>$font,
34 title=>{ text=>'Imager::Graph::Pie', size=>32 },
35 features=>{ outline=>1, labels=>1, pieblur=>0, },
36 outline=>{ line => '404040' },
bfcf9414 37 );
35574351 38
bfcf9414
TC
39ok($img1, "drawing first pie chart")
40 or print "# ",$pie->error,"\n";
35574351 41cmpimg($img1, "testimg/t10_pie1.png", 196880977);
bfcf9414 42$img1->write(file=>'testout/t10_pie1.ppm')
35574351
TC
43 or die "Cannot save pie1: ",$img1->errstr,"\n";
44
45my $img2 = $pie->draw(data=>\@data,
46 labels=>\@labels,
47 font=>$font,
48 title=>{ text=>'Imager::Graph::Pie', size=>36 },
49 features=>{ labelspconly=>1, _debugblur=>1,
50 legend=>1 },
51 legend=>{ border=>'000000', fill=>'FF8080', },
52 fills=>[ qw(404040 606060 808080 A0A0A0 C0C0C0 E0E0E0) ],
bfcf9414 53 );
35574351 54
bfcf9414
TC
55ok($img2, "drawing second pie chart")
56 or print "# ",$pie->error,"\n";
35574351 57cmpimg($img2, "testimg/t10_pie2.png", 255956289);
bfcf9414 58$img2->write(file=>'testout/t10_pie2.ppm')
35574351
TC
59 or die "Cannot save pie2: ",$img2->errstr,"\n";
60
61my ($im_version) = $Imager::VERSION =~ /(\d\.[\d_]+)/;
bfcf9414
TC
62{
63 $im_version > 0.38
64 or skip("very old Imager", 6);
35574351
TC
65 my $img3 = $pie->draw(data=>\@data, labels=>\@labels,
66 font=>$font, style=>'fount_lin',
67 features=>[ 'legend', 'labelspconly', ],
68 legend=>{ valign=>'center' });
bfcf9414
TC
69 ok($img3, "third chart")
70 or print "# ",$pie->error,"\n";
71 $img3->write(file=>'testout/t10_lin_fount.ppm')
35574351
TC
72 or die "Cannot save pie3: ",$img3->errstr,"\n";
73 cmpimg($img3, "testimg/t10_lin_fount.png", 180_000);
74
75 my $img4 = $pie->draw(data=>\@data, labels=>\@labels,
76 font=>$font, style=>'fount_rad',
77 features=>[ 'legend', 'labelspc', ],
78 legend=>{ valign=>'bottom',
79 halign=>'left',
80 border=>'000080' });
bfcf9414
TC
81 ok($img4, "fourth chart")
82 or print "# ",$pie->error,"\n";
83 $img4->write(file=>'testout/t10_rad_fount.ppm')
35574351
TC
84 or die "Cannot save pie3: ",$img4->errstr,"\n";
85 cmpimg($img4, "testimg/t10_rad_fount.png", 120_000);
86
87 my $img5 = $pie->draw(data=>\@data, labels=>\@labels,
88 font=>$font, style=>'mono',
89 features=>[ 'allcallouts', 'labelspc' ],
90 legend=>{ valign=>'bottom',
91 halign=>'right' });
bfcf9414
TC
92 ok($img5, "fifth chart")
93 or print "# ",$pie->error,"\n";
94 $img5->write(file=>'testout/t10_mono.ppm')
35574351
TC
95 or die "Cannot save pie3: ",$img5->errstr,"\n";
96 cmpimg($img5, "testimg/t10_mono.png", 550_000);
97}
35574351
TC
98
99sub cmpimg {
100 my ($img, $file, $limit) = @_;
101
102 $limit ||= 10000;
103
bfcf9414
TC
104 SKIP:
105 {
106 $Imager::formats{png}
107 or skip("No PNG support", 1);
108
35574351
TC
109 my $cmpimg = Imager->new;
110 $cmpimg->read(file=>$file)
111 or return ok(0, "Cannot read $file: ".$cmpimg->errstr);
112 my $diff = Imager::i_img_diff($img->{IMG}, $cmpimg->{IMG});
bfcf9414 113 cmp_ok($diff, '<', $limit, "Comparison to $file ($diff)");
35574351
TC
114 }
115}