Scroll to navigation

GD(3pm) User Contributed Perl Documentation GD(3pm)

NAME

PDL::IO::GD - Interface to the GD image library.

SYNOPSIS

 my $pdl = sequence(byte, 30, 30);
 write_png($pdl, load_lut($lutfile), "test.png");
 write_true_png(sequence(100, 100, 3), "test_true.png");
 my $image = read_png("test.png");
 my $image = read_true_png("test_true_read.png");
 write_true_png($image, "test_true_read.out.png");
 my $lut = read_png_lut("test.png");
 $pdl = sequence(byte, 30, 30);
 write_png_ex($pdl, load_lut($lutfile), "test_nocomp.png", 0);
 write_png_ex($pdl, load_lut($lutfile), "test_bestcomp1.png", 9);
 write_png_best($pdl, load_lut($lutfile), "test_bestcomp2.png");
 $pdl = sequence(100, 100, 3);
 write_true_png_ex($pdl, "test_true_nocomp.png", 0);
 write_true_png_ex($pdl, "test_true_bestcomp1.png", 9);
 write_true_png_best($pdl, "test_true_bestcomp2.png");
 recompress_png_best("test_recomp_best.png");

DESCRIPTION

This is the "General Interface" for the PDL::IO::GD library, and is actually several years old at this point (read: stable). If you're feeling frisky, try the new OO interface described below.

The general version just provides several image IO utility functions you can use with ndarray variables. It's deceptively useful, however.

FUNCTIONS

write_png

  Signature: (byte img(x,y); byte lut(i,j); char* filename)

Writes a 2-d PDL variable out to a PNG file, using the supplied color look-up-table ndarray (hereafter referred to as a LUT).

The LUT contains a line for each value 0-255 with a corresponding R, G, and B value.

write_png does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

write_png_ex

  Signature: (img(x,y); lut(i,j); char* filename; int level)

Same as write_png(), except you can specify the compression level (0-9) as the last argument.

write_png_ex does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

write_true_png

  Signature: (img(x,y,z); char* filename)

Writes an (x, y, z(3)) PDL variable out to a PNG file, using a true color format.

This means a larger file on disk, but can contain more than 256 colors.

write_true_png does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

write_true_png_ex

  Signature: (img(x,y,z); char* filename; int level)

Same as write_true_png(), except you can specify the compression level (0-9) as the last argument.

write_true_png_ex does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

write_png_best

Like write_png(), but it assumes the best PNG compression (9).

  write_png_best( $img(ndarray), $lut(ndarray), $filename )

write_true_png_best

Like write_true_png(), but it assumes the best PNG compression (9).

  write_true_png_best( $img(ndarray), $filename )

load_lut( $filename )

Loads a color look up table from an ASCII file. returns an ndarray

read_png( $filename )

Reads a (palette) PNG image into a (new) PDL variable.

read_png_true( $filename )

Reads a true color PNG image into a (new) PDL variable.

read_png_lut( $filename )

Reads a color LUT from an already-existing palette PNG file.

OO INTERFACE

Object Oriented interface to the GD image library.

SYNOPSIS

 # Open an existing file:
 # 
 my $gd = PDL::IO::GD->new( { filename => "test.png" } );
 
 # Query the x and y sizes:
 my $x = $gd->SX();
 my $y = $gd->SY();
 # Grab the PDL of the data:
 my $pdl = $gd->to_pdl();
 # Kill this thing:
 $gd->DESTROY();
 # Create a new object:
 # 
 my $im = PDL::IO::GD->new( { x => 300, y => 300 } );
 # Allocate some colors:
 #
 my $black = $im->ColorAllocate( 0, 0, 0 );
 my $red = $im->ColorAllocate( 255, 0, 0 );
 my $green = $im->ColorAllocate( 0, 255, 0 );
 my $blue = $im->ColorAllocate( 0, 0, 255 );
 # Draw a rectangle:
 $im->Rectangle( 10, 10, 290, 290, $red );
 # Add some text:
 $im->String( gdFontGetLarge(), 20, 20, "Test Large Font!", $green );
 # Write the output file:
 $im->write_Png( "test2.png" );

DESCRIPTION

This is the Object-Oriented interface from PDL to the GD image library.

See <http://www.boutell.com/gd/> for more information on the GD library and how it works.

IMPLEMENTATION NOTES

Surprisingly enough, this interface has nothing to do with the other Perl->GD interface module, aka 'GD' (as in 'use GD;'). This is done from scratch over the years.

Requires at least version 2.0.22 of the GD library, but it's only been thoroughly tested with gd-2.0.33, so it would be best to use that. The 2.0.22 requirement has to do with a change in GD's font handling functions, so if you don't use those, then don't worry about it.

I should also add, the statement about "thoroughly tested" above is mostly a joke. This OO interface is very young, and it has barely been tested at all, so if something breaks, email me and I'll get it fixed ASAP (for me).

Functions that manipulate and query the image objects generally have a 'gdImage' prefix on the function names (ex: gdImageString()). I've created aliases here for all of those member functions so you don't have to keep typing 'gdImage' in your code, but the long version are in there as well.

METHODS

new

Creates a new PDL::IO::GD object.

Accepts a hash describing how to create the object. Accepts a single hash ( with curly braces ), an inline hash (the same, but without the braces) or a single string interpreted as a filename. Thus the following are all equivalent:

 PDL::IO::GD->new( {filename => 'image.png'} );
 PDL::IO::GD->new( filename => 'image.png' );
 PDL::IO::GD->new( 'image.png' );

If the hash has:

 pdl => $pdl_var (lut => $lut_ndarray)
    Then a new GD is created from that PDL variable.
 filename => $file
    Then a new GD is created from the image file.
    
 x => $num, y => $num
    Then a new GD is created as a palette image, with size x, y
    
 x => $num, y => $num, true_color => 1
    Then a new GD is created as a true color image, with size x, y
 data => $scalar (type => $typename)
    Then a new GD is created from the file data stored in $scalar. 
    If no type is given, then it will try to guess the type of the data, but 
        this will not work for WBMP and gd image types. For those types, you 
        _must_ specify the type of the data, or the operation will fail.
    Valid types are: 'jpg', 'png', 'gif', 'gd', 'gd2', 'wbmp'.

Example:

 my $gd = PDL::IO::GD->new({ pdl => $pdl_var });
    
 my $gd = PDL::IO::GD->new({ pdl => $pdl_var, lut => $lut_ndarray });
 
 my $gd = PDL::IO::GD->new({ filename => "image.png" });
 
 my $gd = PDL::IO::GD->new({ x => 100, y => 100 });
 
 my $gd = PDL::IO::GD->new({ x => 100, y => 100, true_color => 1 });
 
 my $gd = PDL::IO::GD->new({ data => $imageData });
 
 my $gd = PDL::IO::GD->new({ data => $imageData, type => 'wbmp' });

to_pdl

When you're done playing with your GDImage and want an ndarray back, use this function to return one.

apply_lut( $lut(ndarray) )

Does a $im->ColorAllocate() for and entire LUT ndarray at once.

The LUT ndarray format is the same as for the general interface above.

WARNING:

All of the docs below this point are auto-generated (not to mention the actual code), so read with a grain of salt, and always check the main GD documentation about how that function works and what it does.

write_Png

$image->write_Png( $filename )

write_PngEx

$image->write_PngEx( $filename, $level )

write_WBMP

$image->write_WBMP( $fg, $filename )

write_Jpeg

$image->write_Jpeg( $filename, $quality )

write_Gd

$image->write_Gd( $filename )

write_Gd2

$image->write_Gd2( $filename, $cs, $fmt )

write_Gif

$image->write_Gif( $filename )

get_Png_data

$image->get_Png_data( )

get_PngEx_data

$image->get_PngEx_data( $level )

get_WBMP_data

$image->get_WBMP_data( $fg )

get_Jpeg_data

$image->get_Jpeg_data( $quality )

get_Gd_data

$image->get_Gd_data( )

get_Gd2_data

$image->get_Gd2_data( $cs, $fmt )

SetPixel

$image->SetPixel( $x, $y, $color )

Alias for gdImageSetPixel.

gdImageSetPixel

$image->gdImageSetPixel( $x, $y, $color )

GetPixel

$image->GetPixel( $x, $y )

Alias for gdImageGetPixel.

gdImageGetPixel

$image->gdImageGetPixel( $x, $y )

AABlend

$image->AABlend( )

Alias for gdImageAABlend.

gdImageAABlend

$image->gdImageAABlend( )

Line

$image->Line( $x1, $y1, $x2, $y2, $color )

Alias for gdImageLine.

gdImageLine

$image->gdImageLine( $x1, $y1, $x2, $y2, $color )

DashedLine

$image->DashedLine( $x1, $y1, $x2, $y2, $color )

Alias for gdImageDashedLine.

gdImageDashedLine

$image->gdImageDashedLine( $x1, $y1, $x2, $y2, $color )

Rectangle

$image->Rectangle( $x1, $y1, $x2, $y2, $color )

Alias for gdImageRectangle.

gdImageRectangle

$image->gdImageRectangle( $x1, $y1, $x2, $y2, $color )

FilledRectangle

$image->FilledRectangle( $x1, $y1, $x2, $y2, $color )

Alias for gdImageFilledRectangle.

gdImageFilledRectangle

$image->gdImageFilledRectangle( $x1, $y1, $x2, $y2, $color )

SetClip

$image->SetClip( $x1, $y1, $x2, $y2 )

Alias for gdImageSetClip.

gdImageSetClip

$image->gdImageSetClip( $x1, $y1, $x2, $y2 )

GetClip

$image->GetClip( $x1P, $y1P, $x2P, $y2P )

Alias for gdImageGetClip.

gdImageGetClip

$image->gdImageGetClip( $x1P, $y1P, $x2P, $y2P )

BoundsSafe

$image->BoundsSafe( $x, $y )

Alias for gdImageBoundsSafe.

gdImageBoundsSafe

$image->gdImageBoundsSafe( $x, $y )

Char

$image->Char( $f, $x, $y, $c, $color )

Alias for gdImageChar.

gdImageChar

$image->gdImageChar( $f, $x, $y, $c, $color )

CharUp

$image->CharUp( $f, $x, $y, $c, $color )

Alias for gdImageCharUp.

gdImageCharUp

$image->gdImageCharUp( $f, $x, $y, $c, $color )

String

$image->String( $f, $x, $y, $s, $color )

Alias for gdImageString.

gdImageString

$image->gdImageString( $f, $x, $y, $s, $color )

StringUp

$image->StringUp( $f, $x, $y, $s, $color )

Alias for gdImageStringUp.

gdImageStringUp

$image->gdImageStringUp( $f, $x, $y, $s, $color )

String16

$image->String16( $f, $x, $y, $s, $color )

Alias for gdImageString16.

gdImageString16

$image->gdImageString16( $f, $x, $y, $s, $color )

StringUp16

$image->StringUp16( $f, $x, $y, $s, $color )

Alias for gdImageStringUp16.

gdImageStringUp16

$image->gdImageStringUp16( $f, $x, $y, $s, $color )

Polygon

$image->Polygon( $p, $n, $c )

Alias for gdImagePolygon.

gdImagePolygon

$image->gdImagePolygon( $p, $n, $c )

FilledPolygon

$image->FilledPolygon( $p, $n, $c )

Alias for gdImageFilledPolygon.

gdImageFilledPolygon

$image->gdImageFilledPolygon( $p, $n, $c )

ColorAllocate

$image->ColorAllocate( $r, $g, $b )

Alias for gdImageColorAllocate.

gdImageColorAllocate

$image->gdImageColorAllocate( $r, $g, $b )

ColorAllocateAlpha

$image->ColorAllocateAlpha( $r, $g, $b, $a )

Alias for gdImageColorAllocateAlpha.

gdImageColorAllocateAlpha

$image->gdImageColorAllocateAlpha( $r, $g, $b, $a )

ColorClosest

$image->ColorClosest( $r, $g, $b )

Alias for gdImageColorClosest.

gdImageColorClosest

$image->gdImageColorClosest( $r, $g, $b )

ColorClosestAlpha

$image->ColorClosestAlpha( $r, $g, $b, $a )

Alias for gdImageColorClosestAlpha.

gdImageColorClosestAlpha

$image->gdImageColorClosestAlpha( $r, $g, $b, $a )

ColorClosestHWB

$image->ColorClosestHWB( $r, $g, $b )

Alias for gdImageColorClosestHWB.

gdImageColorClosestHWB

$image->gdImageColorClosestHWB( $r, $g, $b )

ColorExact

$image->ColorExact( $r, $g, $b )

Alias for gdImageColorExact.

gdImageColorExact

$image->gdImageColorExact( $r, $g, $b )

ColorExactAlpha

$image->ColorExactAlpha( $r, $g, $b, $a )

Alias for gdImageColorExactAlpha.

gdImageColorExactAlpha

$image->gdImageColorExactAlpha( $r, $g, $b, $a )

ColorResolve

$image->ColorResolve( $r, $g, $b )

Alias for gdImageColorResolve.

gdImageColorResolve

$image->gdImageColorResolve( $r, $g, $b )

ColorResolveAlpha

$image->ColorResolveAlpha( $r, $g, $b, $a )

Alias for gdImageColorResolveAlpha.

gdImageColorResolveAlpha

$image->gdImageColorResolveAlpha( $r, $g, $b, $a )

ColorDeallocate

$image->ColorDeallocate( $color )

Alias for gdImageColorDeallocate.

gdImageColorDeallocate

$image->gdImageColorDeallocate( $color )

TrueColorToPalette

$image->TrueColorToPalette( $ditherFlag, $colorsWanted )

Alias for gdImageTrueColorToPalette.

gdImageTrueColorToPalette

$image->gdImageTrueColorToPalette( $ditherFlag, $colorsWanted )

ColorTransparent

$image->ColorTransparent( $color )

Alias for gdImageColorTransparent.

gdImageColorTransparent

$image->gdImageColorTransparent( $color )

FilledArc

$image->FilledArc( $cx, $cy, $w, $h, $s, $e, $color, $style )

Alias for gdImageFilledArc.

gdImageFilledArc

$image->gdImageFilledArc( $cx, $cy, $w, $h, $s, $e, $color, $style )

Arc

$image->Arc( $cx, $cy, $w, $h, $s, $e, $color )

Alias for gdImageArc.

gdImageArc

$image->gdImageArc( $cx, $cy, $w, $h, $s, $e, $color )

FilledEllipse

$image->FilledEllipse( $cx, $cy, $w, $h, $color )

Alias for gdImageFilledEllipse.

gdImageFilledEllipse

$image->gdImageFilledEllipse( $cx, $cy, $w, $h, $color )

FillToBorder

$image->FillToBorder( $x, $y, $border, $color )

Alias for gdImageFillToBorder.

gdImageFillToBorder

$image->gdImageFillToBorder( $x, $y, $border, $color )

Fill

$image->Fill( $x, $y, $color )

Alias for gdImageFill.

gdImageFill

$image->gdImageFill( $x, $y, $color )

CopyRotated

$image->CopyRotated( $dstX, $dstY, $srcX, $srcY, $srcWidth, $srcHeight, $angle )

Alias for gdImageCopyRotated.

gdImageCopyRotated

$image->gdImageCopyRotated( $dstX, $dstY, $srcX, $srcY, $srcWidth, $srcHeight, $angle )

SetBrush

$image->SetBrush( )

Alias for gdImageSetBrush.

gdImageSetBrush

$image->gdImageSetBrush( )

SetTile

$image->SetTile( )

Alias for gdImageSetTile.

gdImageSetTile

$image->gdImageSetTile( )

SetAntiAliased

$image->SetAntiAliased( $c )

Alias for gdImageSetAntiAliased.

gdImageSetAntiAliased

$image->gdImageSetAntiAliased( $c )

SetAntiAliasedDontBlend

$image->SetAntiAliasedDontBlend( $c, $dont_blend )

Alias for gdImageSetAntiAliasedDontBlend.

gdImageSetAntiAliasedDontBlend

$image->gdImageSetAntiAliasedDontBlend( $c, $dont_blend )

SetStyle

$image->SetStyle( $style, $noOfPixels )

Alias for gdImageSetStyle.

gdImageSetStyle

$image->gdImageSetStyle( $style, $noOfPixels )

SetThickness

$image->SetThickness( $thickness )

Alias for gdImageSetThickness.

gdImageSetThickness

$image->gdImageSetThickness( $thickness )

Interlace

$image->Interlace( $interlaceArg )

Alias for gdImageInterlace.

gdImageInterlace

$image->gdImageInterlace( $interlaceArg )

AlphaBlending

$image->AlphaBlending( $alphaBlendingArg )

Alias for gdImageAlphaBlending.

gdImageAlphaBlending

$image->gdImageAlphaBlending( $alphaBlendingArg )

SaveAlpha

$image->SaveAlpha( $saveAlphaArg )

Alias for gdImageSaveAlpha.

gdImageSaveAlpha

$image->gdImageSaveAlpha( $saveAlphaArg )

TrueColor

$image->TrueColor( )

Alias for gdImageTrueColor.

gdImageTrueColor

$image->gdImageTrueColor( )

ColorsTotal

$image->ColorsTotal( )

Alias for gdImageColorsTotal.

gdImageColorsTotal

$image->gdImageColorsTotal( )

Red

$image->Red( $c )

Alias for gdImageRed.

gdImageRed

$image->gdImageRed( $c )

Green

$image->Green( $c )

Alias for gdImageGreen.

gdImageGreen

$image->gdImageGreen( $c )

Blue

$image->Blue( $c )

Alias for gdImageBlue.

gdImageBlue

$image->gdImageBlue( $c )

Alpha

$image->Alpha( $c )

Alias for gdImageAlpha.

gdImageAlpha

$image->gdImageAlpha( $c )

GetTransparent

$image->GetTransparent( )

Alias for gdImageGetTransparent.

gdImageGetTransparent

$image->gdImageGetTransparent( )

GetInterlaced

$image->GetInterlaced( )

Alias for gdImageGetInterlaced.

gdImageGetInterlaced

$image->gdImageGetInterlaced( )

SX

$image->SX( )

Alias for gdImageSX.

gdImageSX

$image->gdImageSX( )

SY

$image->SY( )

Alias for gdImageSY.

gdImageSY

$image->gdImageSY( )

ColorAllocates

$image->ColorAllocates( $r(pdl), $g(pdl), $b(pdl) )

Alias for gdImageColorAllocates.

gdImageColorAllocates

$image->gdImageColorAllocates( $r(pdl), $g(pdl), $b(pdl) )

ColorAllocateAlphas

$image->ColorAllocateAlphas( $r(pdl), $g(pdl), $b(pdl), $a(pdl) )

Alias for gdImageColorAllocateAlphas.

gdImageColorAllocateAlphas

$image->gdImageColorAllocateAlphas( $r(pdl), $g(pdl), $b(pdl), $a(pdl) )

SetPixels

$image->SetPixels( $x(pdl), $y(pdl), $color(pdl) )

Alias for gdImageSetPixels.

gdImageSetPixels

$image->gdImageSetPixels( $x(pdl), $y(pdl), $color(pdl) )

Lines

$image->Lines( $x1(pdl), $y1(pdl), $x2(pdl), $y2(pdl), $color(pdl) )

Alias for gdImageLines.

gdImageLines

$image->gdImageLines( $x1(pdl), $y1(pdl), $x2(pdl), $y2(pdl), $color(pdl) )

DashedLines

$image->DashedLines( $x1(pdl), $y1(pdl), $x2(pdl), $y2(pdl), $color(pdl) )

Alias for gdImageDashedLines.

gdImageDashedLines

$image->gdImageDashedLines( $x1(pdl), $y1(pdl), $x2(pdl), $y2(pdl), $color(pdl) )

Rectangles

$image->Rectangles( $x1(pdl), $y1(pdl), $x2(pdl), $y2(pdl), $color(pdl) )

Alias for gdImageRectangles.

gdImageRectangles

$image->gdImageRectangles( $x1(pdl), $y1(pdl), $x2(pdl), $y2(pdl), $color(pdl) )

FilledRectangles

$image->FilledRectangles( $x1(pdl), $y1(pdl), $x2(pdl), $y2(pdl), $color(pdl) )

Alias for gdImageFilledRectangles.

gdImageFilledRectangles

$image->gdImageFilledRectangles( $x1(pdl), $y1(pdl), $x2(pdl), $y2(pdl), $color(pdl) )

FilledArcs

$image->FilledArcs( $cx(pdl), $cy(pdl), $w(pdl), $h(pdl), $s(pdl), $e(pdl), $color(pdl), $style(pdl) )

Alias for gdImageFilledArcs.

gdImageFilledArcs

$image->gdImageFilledArcs( $cx(pdl), $cy(pdl), $w(pdl), $h(pdl), $s(pdl), $e(pdl), $color(pdl), $style(pdl) )

Arcs

$image->Arcs( $cx(pdl), $cy(pdl), $w(pdl), $h(pdl), $s(pdl), $e(pdl), $color(pdl) )

Alias for gdImageArcs.

gdImageArcs

$image->gdImageArcs( $cx(pdl), $cy(pdl), $w(pdl), $h(pdl), $s(pdl), $e(pdl), $color(pdl) )

FilledEllipses

$image->FilledEllipses( $cx(pdl), $cy(pdl), $w(pdl), $h(pdl), $color(pdl) )

Alias for gdImageFilledEllipses.

gdImageFilledEllipses

$image->gdImageFilledEllipses( $cx(pdl), $cy(pdl), $w(pdl), $h(pdl), $color(pdl) )

CLASS FUNCTIONS

gdImageCopy

gdImageCopy ( $dst(PDL::IO::GD), $src(PDL::IO::GD), $dstX, $dstY, $srcX, $srcY, $w, $h )

gdImageCopyMerge

gdImageCopyMerge ( $dst(PDL::IO::GD), $src(PDL::IO::GD), $dstX, $dstY, $srcX, $srcY, $w, $h, $pct )

gdImageCopyMergeGray

gdImageCopyMergeGray ( $dst(PDL::IO::GD), $src(PDL::IO::GD), $dstX, $dstY, $srcX, $srcY, $w, $h, $pct )

gdImageCopyResized

gdImageCopyResized ( $dst(PDL::IO::GD), $src(PDL::IO::GD), $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH )

gdImageCopyResampled

gdImageCopyResampled ( $dst(PDL::IO::GD), $src(PDL::IO::GD), $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH )

gdImageCompare

gdImageCompare ( $im1(PDL::IO::GD), $im2(PDL::IO::GD) )

gdImagePaletteCopy

gdImagePaletteCopy ( $dst(PDL::IO::GD), $src(PDL::IO::GD) )

StringTTF

$image->StringTTF( $brect, $fg, $fontlist, $ptsize, $angle, $x, $y, $string )

Alias for gdImageStringTTF.

gdImageStringTTF

$image->gdImageStringTTF( $brect, $fg, $fontlist, $ptsize, $angle, $x, $y, $string )

StringFT

$image->StringFT( $brect, $fg, $fontlist, $ptsize, $angle, $x, $y, $string )

Alias for gdImageStringFT.

gdImageStringFT

$image->gdImageStringFT( $brect, $fg, $fontlist, $ptsize, $angle, $x, $y, $string )

AUTHOR

Judd Taylor, Orbital Systems, Ltd. judd dot t at orbitalsystems dot com

2023-06-17 perl v5.36.0