Skip to content

Commit 3dc922d

Browse files
authored
Functions gdImageEllipse and gdImageRectangle (#3)
* Implementing gdImageEllipse and gdImageRectangle. * Some POD documentation about memory management.
1 parent 399cb5b commit 3dc922d

5 files changed

Lines changed: 108 additions & 2 deletions

File tree

‎Changes‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Revision history for GD::Raw
22

33
{{$NEXT}}
44

5+
0.5 2026-xx
6+
- Add functions `gdImageEllipse` and `gdImageRectangle` to draw outline ellipses and outline rectangles.
7+
58
0.4 2024-10-23T11:02:05+02:00
69
- Add some in-place filters: 0racle++
710

‎META6.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
],
2525
"test-depends": [
2626
],
27-
"version": "0.4"
27+
"version": "0.5"
2828
}

‎lib/GD/Raw.rakumod‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ sub gdImageFilledEllipse (gdImagePtr $im, int32 $cx, int32 $cy, int32 $w, int32
191191
# returns void
192192
is native(LIB) is export {*}
193193

194+
sub gdImageEllipse (gdImagePtr $im, int32 $cx, int32 $cy, int32 $w, int32 $h,
195+
int32 $color)
196+
# returns void
197+
is native(LIB) is export {*}
198+
194199
sub gdImageCopyResized(gdImageStruct $dst, gdImageStruct $src,
195200
int32 $dstX, int32 $dstY,
196201
int32 $srcX, int32 $srcY,
@@ -301,6 +306,10 @@ sub gdImageFilledRectangle(gdImagePtr $im, int32 $x1, int32 $y1, int32 $x2, int3
301306
#returns void
302307
is native(LIB) is export {*}
303308

309+
sub gdImageRectangle(gdImagePtr $im, int32 $x1, int32 $y1, int32 $x2, int32 $y2, int32 $color)
310+
#returns void
311+
is native(LIB) is export {*}
312+
304313
class gdPoint is repr('CStruct') is export {
305314
has int32 $.x is rw = 0;
306315
has int32 $.y is rw = 0;
@@ -541,6 +550,64 @@ origin as possible.
541550
LibGD is large and this module far from covers it all. Feel free to add anything
542551
your missing and submit a pull request!
543552
553+
=head1 MEMORY MANAGEMENT
554+
555+
When creating an in-memory image, some memory is allocated in GD. This
556+
memory is not automatically deallocated when the variable which refers
557+
to the image goes out of scope. To counter this possible memory leak,
558+
the simplest way is to use the C<LEAVE> phaser and call function
559+
C<gdImageDestroy> like this
560+
561+
=begin code :lang<raku>
562+
563+
my $img = gdImageCreateFromPng($fh);
564+
LEAVE gdImageDestroy($_) with $img;
565+
566+
=end code
567+
568+
If a program creates several images, there will be a problem if the
569+
program reuses the C<$img> variable. In this case, you cannot use the
570+
C<LEAVE> phaser, you must call C<gdImageDestroy> before creating the
571+
second image (and the third, and...)
572+
573+
=begin code :lang<raku>
574+
575+
my $img = gdImageCreateFromPng($fh1);
576+
[...]
577+
gdImageDestroy($img);
578+
$img = gdImageCreateFromPng($fh2);
579+
[...]
580+
gdImageDestroy($img);
581+
$img = gdImageCreateFromPng($fh3);
582+
[...]
583+
gdImageDestroy($img);
584+
585+
=end code
586+
587+
Or a simpler solution is to use different variables C<$img1>,
588+
C<$img2>, C<$img3> and so on, and calling C<gdImageDestroy> each time
589+
with the C<LEAVE> phaser.
590+
591+
=begin code :lang<raku>
592+
593+
my $img1 = gdImageCreateFromPng($fh1);
594+
LEAVE gdImageDestroy($_) with $img1;
595+
[...]
596+
my $img2 = gdImageCreateFromPng($fh2);
597+
LEAVE gdImageDestroy($_) with $img2;
598+
[...]
599+
my $img3 = gdImageCreateFromPng($fh3);
600+
LEAVE gdImageDestroy($_) with $img3;
601+
[...]
602+
603+
=end code
604+
605+
=head1 SEE ALSO
606+
607+
Raku Module C<GD>: L<https://github.com/raku-community-modules/GD>
608+
609+
C library: L<https://libgd.github.io/>
610+
544611
=head1 AUTHORS
545612
546613
=item Dagur Valberg Johannsson
@@ -550,7 +617,7 @@ your missing and submit a pull request!
550617
551618
Copyright 2013 - 2018 Dagur Valberg Johannsson
552619
553-
Copyright 2024 Raku Community
620+
Copyright 2024, 2026 Raku Community
554621
555622
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
556623

‎xt/gdimagerectangle0.rakutest‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- encoding: utf-8; indent-tabs-mode: nil -*-
2+
use Test;
3+
use GD::Raw;
4+
5+
use lib $*PROGRAM.parent;
6+
use gdtest;
7+
8+
plan 1;
9+
10+
my $im = gdImageCreate(200, 50) or die;
11+
LEAVE gdImageDestroy($im) if $im;
12+
13+
my $white = gdImageColorAllocate($im, 0xff, 0xff, 0xff);
14+
my $black = gdImageColorAllocate($im, 0, 0, 0);
15+
my $red = gdImageColorAllocate($im, 255, 0, 0);
16+
my $blue = gdImageColorAllocate($im, 0, 0, 255);
17+
18+
gdImageLine($im, 0, 10, 190, 30, $black);
19+
gdImageFilledEllipse( $im, 10, 20, 20, 20, $white);
20+
gdImageEllipse( $im, 10, 20, 20, 20, $blue);
21+
gdImageFilledRectangle($im, 30, 10, 50, 30, $white);
22+
gdImageRectangle( $im, 30, 10, 50, 30, $blue);
23+
gdImageFilledEllipse( $im, 70, 20, 20, 20, $red);
24+
gdImageFilledRectangle($im, 90, 10, 110, 30, $red);
25+
gdImageSetThickness($im, 3);
26+
gdImageFilledEllipse( $im, 130, 20, 20, 20, $white);
27+
gdImageEllipse( $im, 130, 20, 20, 20, $blue);
28+
gdImageFilledRectangle($im, 150, 10, 170, 30, $white);
29+
gdImageRectangle( $im, 150, 10, 170, 30, $blue);
30+
31+
ok gdAssertImageEqualsToFile
32+
$*PROGRAM.sibling("ported-gdimagerectangle/gdimagerectangle0.png"),
33+
$im
34+
;
35+
36+
# vim: expandtab shiftwidth=4
339 Bytes
Loading

0 commit comments

Comments
 (0)