I'm using GD to create some images that will blend with a transparent background but things aren't working out right. I'm hoping someone can point out what is wrong.

Here is the code; as you can see there's no anti-aliasing at all.

#!/usr/bin/env perl use 5.010; use strict; use warnings; # -------------------------------------- use GD; my @white = ( 0xFF, 0xFF, 0xFF ); my @black = ( 0x00, 0x00, 0x00 ); my $size = shift @ARGV || 400; my $offset = $size / 2; my $height = 0.9 * $offset; my $width = 0.1 * $offset; my $img = GD::Image->new( $size, $size, 1 ); $img->saveAlpha( 1 ); $img->alphaBlending( 1 ); # background color: black & transparent my $bg = $img->colorAllocateAlpha( @black, gdAlphaTransparent ); $img->fill( 0, 0, $bg ); # a simple polygon my $p = GD::Polygon->new(); $p->addPt( $offset + $width, $offset + 0 ); $p->addPt( $offset + 0, $offset - $height ); $p->addPt( $offset - $width, $offset + 0 ); $p->addPt( $offset + 0, $offset + $width ); # polygon color: black & opaque my $clr = $img->colorAllocateAlpha( @black, gdAlphaOpaque ); $img->setAntiAliased( $clr ); $img->filledPolygon( $p, gdAntiAliased ); # for testing, use script name for PNG file my $png_file = "$0.png"; open my $fh, '>:raw', $png_file or die "could not open $png_file: $!\n +"; print {$fh} $img->png() or die "could not print to $png_file: $!\n"; close $fh or die "could not close $png_file: $!\n"; __END__

I changed the background colour to transparent white and it seems that the transparency is anti-aliasing in the complete opposite of what it should be. That is, opaque where it should be transparent and vice versa.

#!/usr/bin/env perl use 5.010; use strict; use warnings; # -------------------------------------- use GD; my @white = ( 0xFF, 0xFF, 0xFF ); my @black = ( 0x00, 0x00, 0x00 ); my $size = shift @ARGV || 400; my $offset = $size / 2; my $height = 0.9 * $offset; my $width = 0.1 * $offset; my $img = GD::Image->new( $size, $size, 1 ); $img->saveAlpha( 1 ); $img->alphaBlending( 1 ); # background color: white & transparent my $bg = $img->colorAllocateAlpha( @white, gdAlphaTransparent ); $img->fill( 0, 0, $bg ); # a simple polygon my $p = GD::Polygon->new(); $p->addPt( $offset + $width, $offset + 0 ); $p->addPt( $offset + 0, $offset - $height ); $p->addPt( $offset - $width, $offset + 0 ); $p->addPt( $offset + 0, $offset + $width ); # polygon color: black & opaque my $clr = $img->colorAllocateAlpha( @black, gdAlphaOpaque ); $img->setAntiAliased( $clr ); $img->filledPolygon( $p, gdAntiAliased ); # for testing, use script name for PNG file my $png_file = "$0.png"; open my $fh, '>:raw', $png_file or die "could not open $png_file: $!\n +"; print {$fh} $img->png() or die "could not print to $png_file: $!\n"; close $fh or die "could not close $png_file: $!\n"; __END__

How can I fix this?


In reply to Anti-aliasing Alpha Channel in GD by shawnhcorey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.