hardburn has asked for the wisdom of the Perl Monks concerning the following question:

I have a PNG that I want to put on an SDL::Surface. Before placing it, it needs to be rotated an arbitrary number of degrees. What I have so far:

sub _make_surface { my $self = shift; my $img = Image::Magick->new(); $img->Read( $self->{sprite} ); $img->AffineTransform( rotate => $self->{move_angle}, ); my $img_data = ''; open( my $sh, '>', \$img_data ) or die "Can't open filehandle to scalar: $!\n"; $img->Write( file => $sh, filename => 'img.png', ); close $sh; return SDL::Surface->new( -from => $img_data, ); }

The problem is that the resulting image doesn't show up. You just see a single black pixel where the center of the picture is supposed to be.

I'm not stuck on using Image::Magick. GD or any other image processor will work. GD would be nicer because you can get the image data directly (no mucking about with IO::Scalar), but it doesn't appear to have a method for rotating an arbitrary number of degrees. I would prefer to keep everything in memory rather than doing file IO.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Replies are listed 'Best First'.
Re: Rotating an Image for an SDL::Surface
by BrowserUk (Patriarch) on Aug 19, 2005 at 00:26 UTC

    Something like this ought to do it.

    Update: Fixed a couple of typos. And, having defined VERSION_33 in an arbitrary place in GD.xs and re-built GD, it now works.

    #! perl -slw use strict; use GD; our $A ||= 45; my $imgIn = GD::Image->new( $ARGV[ 0 ] ) or die $!; my $cx = int $imgIn->width / 2; my $cy = int $imgIn->height /2; my $imgOut = GD::Image->new( $imgIn->getBounds, 1 ); $imgOut->copyRotated( $imgIn, $cx, $cy, 0, 0, $imgIn->getBounds, $A ); (my $ofile = $ARGV[0]) =~s[(\..*?$)][.r$A$1]; open OUT, '>:raw', $ofile or die $!; print OUT $imgOut->png; close OUT; system $ofile;

    However, despite having installed the latest versions of GD(2.28) and libgd (2.0.33), running this dies with

    libgd 2.0.33 or higher required for copyRotated support at ...

    which is coming from gd.xs

    void gdcopyRotated(dst,src,dstX,dstY,srcX,srcY,srcW,srcH,angle) GD::Image dst GD::Imagesrc double dstX double dstY int srcX int srcY int srcW int srcH int angle PROTOTYPE: $$$$$$$$$ CODE: { #ifdef VERSION_33 gdImageCopyRotated(dst,src,dstX,dstY,srcX,srcY,srcW,srcH,angle +); #else die("libgd 2.0.33 or higher required for copyRotated support") +; #endif }

    but I can't work out where VERSION_33 should be defined?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      However, despite having installed the latest versions of GD(2.28) and libgd (2.0.33) . . .

      Ahh, that was my problem. Had older versions that didn't have copyRotate().

      I ran into the same problem with VERSION_33, except I was naughtier in my solution--I put the define in gd.h (which gd.xs #include's).

      Thanks for the help.

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Rotating an Image for an SDL::Surface
by zentara (Cardinal) on Aug 19, 2005 at 11:56 UTC
    Imager can do this quite easily, it has the "data" option instead of "file".
    #!/usr/bin/perl use warnings; use strict; use Imager; my $file = shift || die "Need File $!\n"; my $pngdata; open (FH,"< $file"); read( FH, $pngdata, -s FH ); close FH; # Convert image my $img = Imager->new; $img->read(data=>$pngdata, type=>'png') or die "Cannot read: ", $img->errstr; #See perldoc Imager::Transformations my $rot20 = $img->rotate(degrees=>20); $rot20->write(file=>"$file-rot20.png", type=>'png') or die "Cannot write: ",$rot20->errstr; # of course you can write to "data" instead of file here. # just use "data=>$output_png"

    I'm not really a human, but I play one on earth. flash japh