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

Hello all. I am not sure if this is the correct place to post this, but I am having a problem with some code and I have absolutely no idea why. It is intended to generate four different images, each a different quarter of a circle. It was mostly just for experimentation. When I run it, using perl 5.005 or MacPerl, I get a segmentation fault. There is probably a more efficient way to do this, but could anyone help fix it?
#!/usr/bin/perl -w use strict; use GD; my (%arcData,$orientation,$white,$black,$red,@insertData,$img); %arcData=(topRight=>'0,25,50,50,270,360', topLeft=>'25,25,50,50,180,270', bottomRight=>'0,0,50,50,0,90', bottomLeft=>'25,0,50,50,90,180'); foreach $orientation (keys(%arcData)) { @insertData=split(/,/,$arcData{$orientation}); $img=new GD::Image(25,25); $white=$img->colorAllocate(255,255,255); $img->transparent($white); $black=$img->colorAllocate(0,0,0); $img->arc(@insertData,$black); $red=$img->colorAllocate(255,0,0); $img->fill(@insertData[0,1],$red); open (IMAGEOUT,">$orientation.gif")||die("can't write to $orientat +ion.gif: $!"); binmode (IMAGEOUT); print IMAGEOUT $$orientation->gif; close (IMAGEOUT); print "wrote $orientation.gif.\n"; }
That's all. Thanks.

Replies are listed 'Best First'.
(ar0n) Re: GD segfault
by ar0n (Priest) on May 12, 2001 at 04:04 UTC
    The segfault is caused by the fill function. Your image is 25 x 25 pixels; the coordinate system starts at 0 and ends at 24, so 25 is just one position too much. A fence-post error, if you will.

    Here's the new version of your code, cleaned up slightly (I had nothing better to do. Sue me):
    #!/usr/local/bin/perl -w use strict; use GD; my %arcdata = ( topRight => [ 0, 24, 50, 50, 270, 360 ], topLeft => [ 24, 24, 50, 50, 180, 270 ], bottomRight => [ 0, 0, 50, 50, 0, 90 ], bottomLeft => [ 24, 0, 50, 50, 90, 180 ] ); for my $orientation ( keys %arcdata ) { my @insertdata = @{ $arcdata{$orientation} }; my $img = new GD::Image (25, 25); my $white = $img->colorAllocate(255,255,255); $img->transparent($white); my $black = $img->colorAllocate(0,0,0); $img->arc(@insertdata, $black); my $red = $img->colorAllocate(255,0,0); $img->fill( @insertdata[0,1], $red); open (IMAGEOUT,">$orientation.png") || die "Can't write to $or +ientation.png: $!\n"; binmode (IMAGEOUT); print IMAGEOUT $img->png(); close (IMAGEOUT); print "wrote $orientation.png\n"; }


    ar0n ]

Re: GD segfault
by dws (Chancellor) on May 12, 2001 at 03:22 UTC
    print IMAGEOUT $$orientation->gif; ^^^^^^^^^^^^^
    Perhaps that should be   print IMAGEOUT $img->gif;
Re: GD segfault
by YakitoriSD (Initiate) on May 12, 2001 at 03:34 UTC
    Thanks for pointing that out. That was left over from when I was using a different object for each orientation. Unfortunately, even after changing it, it doesn't work.
Re: GD segfault
by YakitoriSD (Initiate) on May 12, 2001 at 04:22 UTC
    Thanks very much. (It works. ^^)