Contact: zeng  @  zegraph.com      Last update: September 2016

GD Library

This library uses GD for processing images. For users who use GD for making plot, please refore to the reference to the old version. lt is recommended to use the Cairo library for making plot.

Function Parameter Type Remark
gd(filename) string Loads image from a file and returns the object. The image type is determined by file name extension, i,e., ".gif", ".png", ".jpg", ".tif", ".bmp", "xbm", or "xpm". Refer to example 1.
gd(w, h[, flag]) integer, integer, boolean Creates an image of width w and height h and returns the object. The optional flag may be set to false to create a palette image, e.g., for GIF animation.
.version()   Returns the version numbers. Refer to example 1.
.size()   Returns an array containing the image width and height. Refer to example 1.
.save(filename) string Saves the created image to a file. Refer to example 2.
.show([title]) string Creates a window and displays the image in it. The optional title parameter lets you show with your own title. Refer to example 2.
Image Manipulation
.copy(x, y, w, h) integers Copies the image starting at (x,y) with width w and height h and returns an image object. It always uses SCS. Refer to example 2.
.merge(img, x, y[, p]) user, integer, integer, integer Merges img into the caller starting at (x,y). The optional parameter p (default to100) determines the percentage of img in merging. It always uses SCS. Refer to example 2.
.resize(w, h) user Resizes image to with w and height h. Refer to example 2.
.ptr(type) string Returns an array containing a pointer to encoded image and its size in byte. The image type may be "gif", "png", or "jpg". Refer to example 2.
.get(r, g, b, n) user, user, user, integer Extracts image colors. The parameters r, g, b are unsigned char pointers and their size n must equal the total number of image pixels. Color values range from 0 to 255. Refer to example 2.
.set(r, g, b, n) user, user, user, integer Sets image colors. The parameters r, g, b are unsigned byte pointers and their size n must equal the total number of image pixels. Color values range from 0 to 255. Refer to example 2.
.clip(x, y, w, h) integers Sets clipping area for restricting drawing. It always uses SCS.

Example-1

// load library
load("gd.dll");

// load image from file
gd = gd("data/earth.png");

// print version info
csv(gd.version());

// save to file
gd.save("test.jpg");

Example-2

load("gd.dll", "matrix.dll");
gd = gd("data/earth.png");

// show image
gd.show();

// copy part of the image
gd2 = gd.copy(0, 0, 200, 100);
gd2.show();

// merge gd2 to gd1 half transparently
gd.merge(gd2, 100, 100, 50);
gd.show();

// get pointer to ompressed gif image for web output
[ptr, n] = gd.ptr("gif");
csv(ptr, n);

// get image size
[w, h] = gd.size();
// create matrix for extracting image color
R = uchar(h, w);
[Rptr, n] = R.ptr();
G = uchar(h, w);
[Gptr, n] = G.ptr();
B = uchar(h, w);
[Bptr, n] = B.ptr();
// get image color
gd.get(Rptr, Gptr, Bptr, n);
// remve red component
R[*] = 0;
// set color back
gd.set(Rptr, Gptr, Bptr, n);
gd.show();

// resize image
gd2 = gd.resize(200, 100);
gd2.show();