Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Filling polygons: Imager vs GD

by HASM (Initiate)
on Sep 26, 2013 at 00:57 UTC ( #1055764=perlquestion: print w/replies, xml ) Need Help??
HASM has asked for the wisdom of the Perl Monks concerning the following question:

At a given point of some processing I have a list of polygons (an array of arrays of x y coordinates).
I want to generate a .png image of the filled polygons. There are a few modules I can use, GD, Imager, ImageMagick, PDL, maybe more.
I've tried GD vs Imager as in:

use GD; my $img = new GD::Image ($width, $height); my $black = $img->colorAllocate (0,0,0); my $white = $img->colorAllocate (255,255,255); map { my $polygon = GD::Polygon->new; map { $polygon->addPt (round ($_->[0] * $scale - $minx), round ($maxy - $_->[1] * $scale)); } @{$_}; $img->filledPolygon ($polygon,$white); } @polygons; print $img->png ();
use Imager; my $img = Imager->new (xsize=>$width, ysize=>$height, channels=>1, bits=>8); map { my @polygon = (); map { push (@polygon, [round ($_->[0] * $scale - $minx), round ($maxy - $_->[1] * $scale)]) } @{$_}; $img->polygon (points=>\@polygon, color=>'white'); } @polygons; $img->write (fd => fileno(STDOUT), type=>'png');

For a sample set size of 12690 polygons, generating a 7917x38558 image, the GD approach runs in about 7 seconds, the Imager approach runs in about 209 seconds, a very large difference.

Cursory inspection with top reveals very similar memory and CPU requirements

What am I overlooking, or is this the way it is?

Replies are listed 'Best First'.
Re: Filling polygons: Imager vs GD
by BrowserUk (Pope) on Sep 26, 2013 at 05:05 UTC
    the GD approach runs in about 7 seconds, the Imager approach runs in about 209 seconds,

    Best guess: GD uses a faster polygon fill algorithm; or is better optimised than Imager

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Filling polygons: Imager vs GD
by stonecolddevin (Vicar) on Sep 26, 2013 at 21:01 UTC

    At any rate, we have an image service that scales full size images (like, the size of a yearbook image, for instance) that uses GD and has no trouble with speed (in terms of scaling said images), so I'd be inclined to use GD just for its battle tested-ness.

    Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past

Re: Filling polygons: Imager vs GD
by tonyc (Pilgrim) on Oct 01, 2013 at 23:04 UTC

    Not that I expect it accounts for all of the speed differences, but Imager draws anti-aliased filled polygons, while GD doesn't.

    Pre-calculating the 'white' with something like:

    my $white = Imager::Color->new("white"); ... $img->polygon (points=>\@polygon, color=>$white);

    may also help a little, though it's likely to drown in the worked needed to actually draw the polygons.

    Imager has other inefficiencies in filling areas that probably make some difference too.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055764]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-02-22 11:34 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (291 votes). Check out past polls.