4 use Test::More tests => 18;
7 use Imager::Test qw(is_image is_color3);
9 sub PI () { 3.14159265358979323846 }
11 -d "testout" or mkdir "testout";
13 Imager::init_log("testout/t75aapolyaa.log",1);
15 my $red = Imager::Color->new(255,0,0);
16 my $green = Imager::Color->new(0,255,0);
17 my $blue = Imager::Color->new(0,0,255);
18 my $white = Imager::Color->new(255,255,255);
20 { # artifacts with multiple vertical lobes
21 # https://rt.cpan.org/Ticket/Display.html?id=43518
22 # previously this would have a full coverage pixel at (0,0) caused
23 # by the (20,0.5) point in the right lobe
37 my $im = Imager->new(xsize => 10, ysize => 2);
38 ok($im->polygon(points => \@pts,
40 "draw with inside point");
41 ok($im->write(file => "testout/t75inside.ppm"), "save to file");
42 # both scanlines should be the same
43 my $line0 = $im->crop(top => 0, height => 1);
44 my $line1 = $im->crop(top => 1, height => 1);
45 is_image($line0, $line1, "both scanlines should be the same");
48 { # check vertical edges are consistent
49 my $im = Imager->new(xsize => 10, ysize => 10);
50 ok($im->polygon(points => [ [ 0.5, 0 ], [ 9.25, 0 ],
51 [ 9.25, 10 ], [ 0.5, 10 ] ],
54 "draw polygon with mid pixel vertical edges")
56 my @line0 = $im->getscanline(y => 0);
57 my $im2 = Imager->new(xsize => 10, ysize => 10);
59 $im2->setscanline(y => $y, pixels => \@line0);
61 is_image($im, $im2, "all scan lines should be the same");
62 is_color3($line0[0], 128, 128, 128, "(0,0) should be 50% coverage");
63 is_color3($line0[9], 64, 64, 64, "(9,0) should be 25% coverage");
66 { # check horizontal edges are consistent
67 my $im = Imager->new(xsize => 10, ysize => 10);
68 ok($im->polygon(points => [ [ 0, 0.5 ], [ 0, 9.25 ],
69 [ 10, 9.25 ], [ 10, 0.5 ] ],
72 "draw polygon with mid-pixel horizontal edges");
73 is_deeply([ $im->getsamples(y => 0, channels => [ 0 ]) ],
75 "all of line 0 should be 50% coverage");
76 is_deeply([ $im->getsamples(y => 9, channels => [ 0 ]) ],
78 "all of line 9 should be 25% coverage");
82 my $img = Imager->new(xsize=>20, ysize=>10);
83 my @data = translate(5.5,5,
86 get_polygon(n_gon => 5)
92 my ($x, $y) = array_to_refpair(@data);
93 ok(Imager::i_poly_aa($img->{IMG}, $x, $y, $white), "primitive poly");
95 ok($img->write(file=>"testout/t75.ppm"), "write to file")
98 my $zoom = make_zoom($img, 8, \@data, $red);
99 ok($zoom, "make zoom of primitive");
100 $zoom->write(file=>"testout/t75zoom.ppm") or die $zoom->errstr;
104 my $img = Imager->new(xsize=>300, ysize=>100);
108 my @data = translate(20+20*($n%14),18+20*int($n/14),
115 my ($x, $y) = array_to_refpair(@data);
116 Imager::i_poly_aa($img->{IMG}, $x, $y, NC(rand(255), rand(255), rand(255)))
120 $img->write(file=>"testout/t75big.ppm") or die $img->errstr;
122 ok($good, "primitive squares");
126 my $img = Imager->new(xsize => 300, ysize => 300);
127 ok($img -> polygon(color=>$white,
132 get_polygon('wavycircle', 32*8, sub { 1.2+1*cos(4*$_) }))))
135 or diag $img->errstr();
137 $img->write(file=>"testout/t75wave.ppm") or die $img->errstr;
141 my $img = Imager->new(xsize=>10,ysize=>6);
142 my @data = translate(165,5,
144 get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })));
146 ok($img -> polygon(color=>$white,
150 get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })))
153 or diag $img->errstr();
155 make_zoom($img,20,\@data, $blue)->write(file=>"testout/t75wavebug.ppm") or die $img->errstr;
160 my $img = Imager->new(xsize=>300, ysize=>300);
161 ok($img->polygon(fill=>{ hatch=>'cross1', fg=>'00FF00', bg=>'0000FF', dx=>3 },
165 get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })))
167 ), "poly filled with hatch")
168 or diag $img->errstr();
169 $img->write(file=>"testout/t75wave_fill.ppm") or die $img->errstr;
173 my $img = Imager->new(xsize=>300, ysize=>300, bits=>16);
174 ok($img->polygon(fill=>{ hatch=>'cross1', fg=>'00FF00', bg=>'0000FF' },
178 get_polygon('wavycircle', 32*8, sub { 1+1*cos(5*$_) })))
180 ), "hatched to 16-bit image")
181 or diag $img->errstr();
182 $img->write(file=>"testout/t75wave_fill16.ppm") or die $img->errstr;
185 Imager::malloc_state();
188 #initialized in a BEGIN, later
194 if (exists $primitives{$name}) {
195 return @{$primitives{$name}};
198 if (exists $polygens{$name}) {
199 return $polygens{$name}->(@_);
202 die "polygon spec: $name unknown\n";
207 my ($img, $sc, $polydata, $linecolor) = @_;
209 # scale with nearest neighboor sampling
210 my $timg = $img->scale(scalefactor=>$sc, qtype=>'preview');
213 for(my $lx=0; $lx<$timg->getwidth(); $lx+=$sc) {
214 $timg->line(color=>$green, x1=>$lx, x2=>$lx, y1=>0, y2=>$timg->getheight(), antialias=>0);
217 for(my $ly=0; $ly<$timg->getheight(); $ly+=$sc) {
218 $timg->line(color=>$green, y1=>$ly, y2=>$ly, x1=>0, x2=>$timg->getwidth(), antialias=>0);
220 my @data = scale($sc, $sc, @$polydata);
221 push(@data, $data[0]);
222 my ($x, $y) = array_to_refpair(@data);
224 $timg->polyline(color=>$linecolor, 'x'=>$x, 'y'=>$y, antialias=>0);
228 # utility functions to manipulate point data
231 my ($x, $y, @data) = @_;
232 return map { [ $_->[0]*$x , $_->[1]*$y ] } @data;
236 my ($x, $y, @data) = @_;
237 map { [ $_->[0]+$x , $_->[1]+$y ] } @data;
241 my ($rad, @data) = @_;
242 map { [ $_->[0]*cos($rad)+$_->[1]*sin($rad) , $_->[1]*cos($rad)-$_->[0]*sin($rad) ] } @data;
245 sub array_to_refpair {
258 box => [ [-0.5,-0.5], [0.5,-0.5], [0.5,0.5], [-0.5,0.5] ],
259 triangle => [ [0,0], [1,0], [1,1] ],
266 my @radians = map { $_*2*PI/$numv } 0..$numv-1;
267 my @radius = map { $radfunc->($_) } @radians;
269 [ $radius[$_] * cos($radians[$_]), $radius[$_] * sin($radians[$_]) ]
275 [ cos($_*2*PI/$N), sin($_*2*PI/$N) ]