4 use Test::More tests => 24;
7 use Imager::Test qw(is_image is_color3);
9 sub PI () { 3.14159265358979323846 }
11 -d "testout" or mkdir "testout";
16 unless ($ENV{IMAGER_KEEP_FILES}) {
22 Imager->open_log(log => "testout/250-polyaa.log");
23 push @out_files, "testout/250-polyaa.log";
25 my $red = Imager::Color->new(255,0,0);
26 my $green = Imager::Color->new(0,255,0);
27 my $blue = Imager::Color->new(0,0,255);
28 my $white = Imager::Color->new(255,255,255);
29 my $black = Imager::Color->new(0, 0, 0);
31 { # artifacts with multiple vertical lobes
32 # https://rt.cpan.org/Ticket/Display.html?id=43518
33 # previously this would have a full coverage pixel at (0,0) caused
34 # by the (20,0.5) point in the right lobe
48 my $im = Imager->new(xsize => 10, ysize => 2);
49 ok($im->polygon(points => \@pts,
51 "draw with inside point");
52 ok($im->write(file => "testout/250-poly-inside.ppm"), "save to file");
53 push @out_files, "testout/250-poly-inside.ppm";
54 # both scanlines should be the same
55 my $line0 = $im->crop(top => 0, height => 1);
56 my $line1 = $im->crop(top => 1, height => 1);
57 is_image($line0, $line1, "both scanlines should be the same");
60 { # check vertical edges are consistent
61 my $im = Imager->new(xsize => 10, ysize => 10);
62 ok($im->polygon(points => [ [ 0.5, 0 ], [ 9.25, 0 ],
63 [ 9.25, 10 ], [ 0.5, 10 ] ],
66 "draw polygon with mid pixel vertical edges")
68 my @line0 = $im->getscanline(y => 0);
69 my $im2 = Imager->new(xsize => 10, ysize => 10);
71 $im2->setscanline(y => $y, pixels => \@line0);
73 is_image($im, $im2, "all scan lines should be the same");
74 is_color3($line0[0], 128, 128, 128, "(0,0) should be 50% coverage");
75 is_color3($line0[9], 64, 64, 64, "(9,0) should be 25% coverage");
78 { # check horizontal edges are consistent
79 my $im = Imager->new(xsize => 10, ysize => 10);
80 ok($im->polygon(points => [ [ 0, 0.5 ], [ 0, 9.25 ],
81 [ 10, 9.25 ], [ 10, 0.5 ] ],
84 "draw polygon with mid-pixel horizontal edges");
85 is_deeply([ $im->getsamples(y => 0, channels => [ 0 ]) ],
87 "all of line 0 should be 50% coverage");
88 is_deeply([ $im->getsamples(y => 9, channels => [ 0 ]) ],
90 "all of line 9 should be 25% coverage");
94 my $img = Imager->new(xsize=>20, ysize=>10);
95 my @data = translate(5.5,5,
98 get_polygon(n_gon => 5)
104 my ($x, $y) = array_to_refpair(@data);
105 ok(Imager::i_poly_aa_m($img->{IMG}, $x, $y, 0, $white), "primitive poly");
107 ok($img->write(file=>"testout/250-poly.ppm"), "write to file")
108 or diag $img->errstr;
109 push @out_files, "testout/250-poly.ppm";
111 my $zoom = make_zoom($img, 8, \@data, $red);
112 ok($zoom, "make zoom of primitive");
113 $zoom->write(file=>"testout/250-poly-zoom.ppm") or die $zoom->errstr;
114 push @out_files, "testout/250-poly-zoom.ppm";
118 my $img = Imager->new(xsize=>300, ysize=>100);
122 my @data = translate(20+20*($n%14),18+20*int($n/14),
129 my ($x, $y) = array_to_refpair(@data);
130 Imager::i_poly_aa_m($img->{IMG}, $x, $y, 0, NC(rand(255), rand(255), rand(255)))
134 $img->write(file=>"testout/250-poly-big.ppm") or die $img->errstr;
136 ok($good, "primitive squares");
137 push @out_files, "testout/250-poly-big.ppm";
141 my $img = Imager->new(xsize => 300, ysize => 300);
142 ok($img -> polygon(color=>$white,
147 get_polygon('wavycircle', 32*8, sub { 1.2+1*cos(4*$_) }))))
150 or diag $img->errstr();
152 $img->write(file=>"testout/250-poly-wave.ppm") or die $img->errstr;
153 push @out_files, "testout/250-poly-wave.ppm";
157 my $img = Imager->new(xsize=>10,ysize=>6);
158 my @data = translate(165,5,
160 get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })));
162 ok($img -> polygon(color=>$white,
166 get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })))
169 or diag $img->errstr();
171 make_zoom($img,20,\@data, $blue)->write(file=>"testout/250-poly-wavebug.ppm") or die $img->errstr;
173 push @out_files, "testout/250-poly-wavebug.ppm";
177 my $img = Imager->new(xsize=>300, ysize=>300);
178 ok($img->polygon(fill=>{ hatch=>'cross1', fg=>'00FF00', bg=>'0000FF', dx=>3 },
182 get_polygon('wavycircle', 32*8, sub { 1+1*cos(4*$_) })))
184 ), "poly filled with hatch")
185 or diag $img->errstr();
186 $img->write(file=>"testout/250-poly-wave_fill.ppm") or die $img->errstr;
187 push @out_files, "testout/250-poly-wave_fill.ppm";
191 my $img = Imager->new(xsize=>300, ysize=>300, bits=>16);
192 ok($img->polygon(fill=>{ hatch=>'cross1', fg=>'00FF00', bg=>'0000FF' },
196 get_polygon('wavycircle', 32*8, sub { 1+1*cos(5*$_) })))
198 ), "hatched to 16-bit image")
199 or diag $img->errstr();
200 $img->write(file=>"testout/250-poly-wave_fill16.ppm") or die $img->errstr;
201 push @out_files, "testout/250-poly-wave_fill16.ppm";
205 my $img = Imager->new(xsize => 100, ysize => 100);
226 ), "default polypolygon");
227 push @out_files, "testout/250-poly-ppeo.ppm";
228 ok($img->write(file => "testout/250-poly-ppeo.ppm"),
230 my $cmp_eo = Imager->new(xsize => 100, ysize => 100);
231 $cmp_eo->box(filled => 1, color => $white, box => [ 10, 10, 89, 89 ]);
232 $cmp_eo->box(filled => 1, color => $black, box => [ 20, 20, 44, 79 ]);
233 $cmp_eo->box(filled => 1, color => $black, box => [ 55, 20, 79, 79 ]);
234 is_image($img, $cmp_eo, "check even/odd matches");
235 $img = Imager->new(xsize => 100, ysize => 100);
242 ), "default polypolygon");
243 my $cmp_nz = Imager->new(xsize => 100, ysize => 100);
244 $cmp_nz->box(filled => 1, color => $white, box => [ 10, 10, 89, 89 ]);
245 $cmp_nz->box(filled => 1, color => $black, box => [ 55, 20, 79, 79 ]);
246 is_image($img, $cmp_nz, "check non-zero matches");
247 push @out_files, "testout/250-poly-ppnz.ppm";
248 ok($img->write(file => "testout/250-poly-ppnz.ppm"),
254 Imager::malloc_state();
256 #initialized in a BEGIN, later
262 if (exists $primitives{$name}) {
263 return @{$primitives{$name}};
266 if (exists $polygens{$name}) {
267 return $polygens{$name}->(@_);
270 die "polygon spec: $name unknown\n";
275 my ($img, $sc, $polydata, $linecolor) = @_;
277 # scale with nearest neighboor sampling
278 my $timg = $img->scale(scalefactor=>$sc, qtype=>'preview');
281 for(my $lx=0; $lx<$timg->getwidth(); $lx+=$sc) {
282 $timg->line(color=>$green, x1=>$lx, x2=>$lx, y1=>0, y2=>$timg->getheight(), antialias=>0);
285 for(my $ly=0; $ly<$timg->getheight(); $ly+=$sc) {
286 $timg->line(color=>$green, y1=>$ly, y2=>$ly, x1=>0, x2=>$timg->getwidth(), antialias=>0);
288 my @data = scale($sc, $sc, @$polydata);
289 push(@data, $data[0]);
290 my ($x, $y) = array_to_refpair(@data);
292 $timg->polyline(color=>$linecolor, 'x'=>$x, 'y'=>$y, antialias=>0);
296 # utility functions to manipulate point data
299 my ($x, $y, @data) = @_;
300 return map { [ $_->[0]*$x , $_->[1]*$y ] } @data;
304 my ($x, $y, @data) = @_;
305 map { [ $_->[0]+$x , $_->[1]+$y ] } @data;
309 my ($rad, @data) = @_;
310 map { [ $_->[0]*cos($rad)+$_->[1]*sin($rad) , $_->[1]*cos($rad)-$_->[0]*sin($rad) ] } @data;
313 sub array_to_refpair {
326 box => [ [-0.5,-0.5], [0.5,-0.5], [0.5,0.5], [-0.5,0.5] ],
327 triangle => [ [0,0], [1,0], [1,1] ],
334 my @radians = map { $_*2*PI/$numv } 0..$numv-1;
335 my @radius = map { $radfunc->($_) } @radians;
337 [ $radius[$_] * cos($radians[$_]), $radius[$_] * sin($radians[$_]) ]
343 [ cos($_*2*PI/$N), sin($_*2*PI/$N) ]