Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Revision history for GD::Raw

{{$NEXT}}

0.5 2026-xx
- Add functions `gdImageEllipse` and `gdImageRectangle` to draw outline ellipses and outline rectangles.

0.4 2024-10-23T11:02:05+02:00
- Add some in-place filters: 0racle++

Expand Down
2 changes: 1 addition & 1 deletion META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
],
"test-depends": [
],
"version": "0.4"
"version": "0.5"
}
69 changes: 68 additions & 1 deletion lib/GD/Raw.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ sub gdImageFilledEllipse (gdImagePtr $im, int32 $cx, int32 $cy, int32 $w, int32
# returns void
is native(LIB) is export {*}

sub gdImageEllipse (gdImagePtr $im, int32 $cx, int32 $cy, int32 $w, int32 $h,
int32 $color)
# returns void
is native(LIB) is export {*}

sub gdImageCopyResized(gdImageStruct $dst, gdImageStruct $src,
int32 $dstX, int32 $dstY,
int32 $srcX, int32 $srcY,
Expand Down Expand Up @@ -301,6 +306,10 @@ sub gdImageFilledRectangle(gdImagePtr $im, int32 $x1, int32 $y1, int32 $x2, int3
#returns void
is native(LIB) is export {*}

sub gdImageRectangle(gdImagePtr $im, int32 $x1, int32 $y1, int32 $x2, int32 $y2, int32 $color)
#returns void
is native(LIB) is export {*}

class gdPoint is repr('CStruct') is export {
has int32 $.x is rw = 0;
has int32 $.y is rw = 0;
Expand Down Expand Up @@ -541,6 +550,64 @@ origin as possible.
LibGD is large and this module far from covers it all. Feel free to add anything
your missing and submit a pull request!

=head1 MEMORY MANAGEMENT

When creating an in-memory image, some memory is allocated in GD. This
memory is not automatically deallocated when the variable which refers
to the image goes out of scope. To counter this possible memory leak,
the simplest way is to use the C<LEAVE> phaser and call function
C<gdImageDestroy> like this

=begin code :lang<raku>

my $img = gdImageCreateFromPng($fh);
LEAVE gdImageDestroy($_) with $img;

=end code

If a program creates several images, there will be a problem if the
program reuses the C<$img> variable. In this case, you cannot use the
C<LEAVE> phaser, you must call C<gdImageDestroy> before creating the
second image (and the third, and...)

=begin code :lang<raku>

my $img = gdImageCreateFromPng($fh1);
[...]
gdImageDestroy($img);
$img = gdImageCreateFromPng($fh2);
[...]
gdImageDestroy($img);
$img = gdImageCreateFromPng($fh3);
[...]
gdImageDestroy($img);

=end code

Or a simpler solution is to use different variables C<$img1>,
C<$img2>, C<$img3> and so on, and calling C<gdImageDestroy> each time
with the C<LEAVE> phaser.

=begin code :lang<raku>

my $img1 = gdImageCreateFromPng($fh1);
LEAVE gdImageDestroy($_) with $img1;
[...]
my $img2 = gdImageCreateFromPng($fh2);
LEAVE gdImageDestroy($_) with $img2;
[...]
my $img3 = gdImageCreateFromPng($fh3);
LEAVE gdImageDestroy($_) with $img3;
[...]

=end code

=head1 SEE ALSO

Raku Module C<GD>: L<https://github.com/raku-community-modules/GD>

C library: L<https://libgd.github.io/>

=head1 AUTHORS

=item Dagur Valberg Johannsson
Expand All @@ -550,7 +617,7 @@ your missing and submit a pull request!

Copyright 2013 - 2018 Dagur Valberg Johannsson

Copyright 2024 Raku Community
Copyright 2024, 2026 Raku Community

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

Expand Down
36 changes: 36 additions & 0 deletions xt/gdimagerectangle0.rakutest
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- encoding: utf-8; indent-tabs-mode: nil -*-
use Test;
use GD::Raw;

use lib $*PROGRAM.parent;
use gdtest;

plan 1;

my $im = gdImageCreate(200, 50) or die;
LEAVE gdImageDestroy($im) if $im;

my $white = gdImageColorAllocate($im, 0xff, 0xff, 0xff);
my $black = gdImageColorAllocate($im, 0, 0, 0);
my $red = gdImageColorAllocate($im, 255, 0, 0);
my $blue = gdImageColorAllocate($im, 0, 0, 255);

gdImageLine($im, 0, 10, 190, 30, $black);
gdImageFilledEllipse( $im, 10, 20, 20, 20, $white);
gdImageEllipse( $im, 10, 20, 20, 20, $blue);
gdImageFilledRectangle($im, 30, 10, 50, 30, $white);
gdImageRectangle( $im, 30, 10, 50, 30, $blue);
gdImageFilledEllipse( $im, 70, 20, 20, 20, $red);
gdImageFilledRectangle($im, 90, 10, 110, 30, $red);
gdImageSetThickness($im, 3);
gdImageFilledEllipse( $im, 130, 20, 20, 20, $white);
gdImageEllipse( $im, 130, 20, 20, 20, $blue);
gdImageFilledRectangle($im, 150, 10, 170, 30, $white);
gdImageRectangle( $im, 150, 10, 170, 30, $blue);

ok gdAssertImageEqualsToFile
$*PROGRAM.sibling("ported-gdimagerectangle/gdimagerectangle0.png"),
$im
;

# vim: expandtab shiftwidth=4
Binary file added xt/ported-gdimagerectangle/gdimagerectangle0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading