Rand Stats

GD::Raw

zef:raku-community-modules

Actions Status Actions Status

NAME

GD::Raw - Low level language bindings to GD Graphics Library

SYNOPSIS

use GD::Raw;

my $fh = fopen("my-image.png", "rb");
my $img = gdImageCreateFromPng($fh);
LEAVE gdImageDestroy($_) with $img;

say "Image resolution is ", gdImageSX($img), "x", gdImageSX($img);

DESCRIPTION

GD::Raw is a low level language bindings to LibGD. It does not attempt to provide you with an rakuish interface, but tries to stay as close to its C 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!

FUNCTIONS PORTED TO RAKU

Not all libgd functions are ported to Raku module GD::Raw. Here is the list of these functions, in the same order as the libgd 2.3.3 documentation

Image Formats

gdImageBmpPtr

gdImageBmp

gdImageCreateFromBmp

gdImageCreateFromGif

gdImageGdPtr

gdImageGifPtr

gdImageGif

gdImageJpeg

gdImageJpegPtr

gdImageCreateFromJpeg

gdImageCreateFromPng

gdImagePng

gdImagePngPtr

gdImagePngPtrEx

gdImageTiffPtr

gdImageWebpPtr

gdImageWepbPtrEx

gd.c

Creation and Destruction

gdImageCreate

gdImageCreateTrueColor

gdImageDestroy

Color

gdImageColorExactAlpha

gdImageColorAllocate

gdImageColorResolve

gdImageColorResolveAlpha

Pixels

gdImageSetPixel

gdImageGetPixel

gdImageGetTrueColorPixel

Primitives

gdImageLine

gdImageArc

gdImageEllipse

gdImageFilledEllipse

gdImageRectangle

gdImageFilledRectangle

Cloning and Copying

gdImageCopyResized

gdImageCopyResampled

Polygons

gdImagePolygon

gdImageOpenPolygon

gdImageFilledPolygon

Other

gdImageSetStyle

gdImageSetThickness

gdImageSetAntiAliased

gd.h

Colour Decomposition

gdTrueColorGetAlpha

gdTrueColorGetRed

gdTrueColorGetGreen

gdTrueColorGetBlue

Color Composition

gdTrueColorAlpha

Accessor Macros

gdImageSX

gdImageSY

gdImageColorsTotal

[gdImageRed](https://libgd.github.io/manuals/2.3.3/files/gd-h.html#gdImageRed

L<C<gdImageGreen)|https://libgd.github.io/manuals/2.3.3/files/gd-h.html#gdImageGreen>

gdImageBlue

gdImageAlpha

gd_interpolation.c

gdImageScale

gdImageRotateInterpolated

gdImageSetInterpolationMethod

gd_version.c

gdMajorVersion

gdMinorVersion

gdReleaseVersion

gdExtraVersion

gdVersionString

gdFree

gdFree

Image Filters

gdImageScatter

gdImagePixelate

gdImageNegate

gdImageGrayScale

gdImageBrightness

gdImageContrast

gdImageColor

gdImageSelectiveBlur

gdImageEdgeDetectQuick

gdImageGaussianBlur

gdImageEmboss

gdImageMeanRemoval

gdImageSmooth

gdImageCopyGaussianBlurred

Additional Functions

fopen file management for graphic files

fclose file management for graphic files

gdImageCreatePalette is an alias of gdImageCreate.

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 LEAVE phaser and call function gdImageDestroy like this

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

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

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

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

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

When using a function gdImageXXXPtr to fill a blob with graphic data, the memory management function is gdFree. Usually, the pointer has a very short lifespan. Once the blob has been generated by blob-from-pointer, the pointer is useless and can be fred immediately. A typical chunk of code would be:

use GD::Raw;
use NativeHelpers::Blob;

[...]

my int32 $size;
my $ptr  = gdImagePngPtr($im, $size);
my $blob = blob-from-pointer($ptr, elems => $size, type => Blob[int8]);
gdFree($ptr);

SEE ALSO

Raku Module GD: https://github.com/raku-community-modules/GD

C library: https://libgd.github.io/

AUTHORS

COPYRIGHT AND LICENSE

Copyright 2013 - 2018 Dagur Valberg Johannsson

Copyright 2024, 2026 Raku Community

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