Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

GD Anti Aliasing

by KianTern (Acolyte)
on Jan 15, 2009 at 06:35 UTC ( #736466=perlquestion: print w/replies, xml ) Need Help??
KianTern has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I'm trying to draw with GD and can't figure out how to use Anti Aliasing.
This is the sample code.
use strict; use GD; my $gd = GD::Image->new(400,400); my $black = $gd->colorAllocate(0,0,0); my $white = $gd->colorAllocate(255,255,255); $gd->filledRectangle(0,0,399,399,$white); $gd->setAntiAliased($black); $gd->line(0,0,399,399,gdAntiAliased); open(IMG,">","out.png"); binmode IMG; print IMG $gd->png;
This code supposed to draw an Anti Aliased line but it doesn't Anti Alias. I'm using perl 5.10 on Ubuntu 8.10.

Replies are listed 'Best First'.
Re: GD Anti Aliasing
by BrowserUk (Pope) on Jan 15, 2009 at 10:35 UTC

    Two problems with what you have.

    1. You're creating a palettised image, and anti-aliasing dooesn't work too well with them.
    2. The line you've drawn is at exactly 45 degrees, and doesn't get anti-aliased.

    Try this:

    use strict; use GD; my $gd = GD::Image->new(400,400, 1); ## truecolor my $black = $gd->colorAllocate(0,0,0); my $white = $gd->colorAllocate(255,255,255); $gd->filledRectangle( 0,0, 399,399, $white); $gd->setAntiAliased($black); for my $y ( map 10*$_, 0 .. 39 ) { ## All these will be anti-aliased $gd->line( 0,0, 399,$y, gdAntiAliased); } for my $x ( map 10*$_, 0 .. 39 ) { ## And these $gd->line( 0,0, $x,399, gdAntiAliased); } $gd->line( 0,0, 399,399, gdAntiAliased); ## But not this open(IMG,">","out.png"); binmode IMG; print IMG $gd->png;

    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: GD Anti Aliasing
by syphilis (Chancellor) on Jan 15, 2009 at 10:35 UTC
    I think the key to getting Anti Alias to work is (from the docs):
    When using palette-based images, be sure to allocate a broad spectrum +of colors in order to have sufficient colors for the antialiasing to +use
    That is, if you also allocate:
    my $grey = $gd->colorAllocate(130,130,130); my $grey1 = $gd->colorAllocate(32,32,32); my $grey2 = $gd->colorAllocate(64,64,64); my $grey3 = $gd->colorAllocate(190,190,190);
    then you'll find that you get some Anti Aliasing. I'm not sure that you'll be able to actually *see* the difference, but a Digest::MD5 of the output files should reveal that the output has in fact changed. (At least that's what I'm finding on perl-5.8 on Win32.)

        All that will have changed is that you've added 4 entries to the palette.

        Oh ... why does the MD5 of the output png file then change ?

Re: GD Anti Aliasing
by spx2 (Deacon) on Apr 03, 2010 at 21:28 UTC
    I get
    perl Can't locate object method "png" via package "GD::Image" at li +ne 23
    on your code. I've installed the latest GD

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://736466]
Approved by Corion
Front-paged by grinder
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2017-10-20 09:45 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (260 votes). Check out past polls.