Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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.

Comment on GD Anti Aliasing
Download Code
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 (Canon) 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.)

    Cheers,
    Rob
        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 ?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://736466]
Approved by Corion
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-09-02 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls