/* *****************************************************************************
** Initial file written and documented by:
** Kevin Shepherd <kshepherd@php.net> December 2007
** of Scarlet Line http://www.scarletline.com/
** with contributions from Torben Nielsen.
*******************************************************************************/
\brief Object Oriented C++ wrappers around libgd functionality.
Example usage, convert png to gif:
std::ifstream in("image.png", std::ios_base::in | std::ios_base::binary );
GD::Image im(in, GD::Png_tag());
std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
#include "gd_io_stream.h"
/// namespace GD:: contains the C++ wrapper classes for libgd
/** This namespace is primarily to avoid name clashes, and to
contain all of the gd classes within one namespace.
It is not recommended to use the "using namespace" directive with this namespace.
GD::Image im(64, 32, true); // Create a truecolor image 64 pixels wide by 32 pixels high
GD::Point pt(10, 8); // The point at x=10, y=8.
GD::Size sz(16, 8); // The size width=16, height=8.
GD::TrueColor col(0xFF, 0, 0); // The colour red; R=255, G=0, B=0.
im.Rectangle(pt, sz, col.Int()); // Draw a red rectangle with top left corner at pt, of size sz.
/** This class GD::Point stores a point in two dimensions, somewhere
on the plane of an image.
class BGD_EXPORT_DATA_PROT Point
Point & operator=(const Point & p) {
gdPointPtr as_gdPointPtr() {
return (gdPointPtr) this;
typedef Point * PointPtr;
/** This class GD::Size stores length in two dimensions.
Giving the size of an area as width and height.
class BGD_EXPORT_DATA_PROT Size
Size & operator=(const Size & p) {
/** This class GD::TrueColor stores a colour as an RGBA quadruplet.
It can also be read as an integer, and in other colour formats.
class BGD_EXPORT_DATA_PROT TrueColor
unsigned char blue, green, red, alpha;
TrueColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0) {
internal.as_uchar.alpha = a;
internal.as_uchar.red = r;
internal.as_uchar.green = g;
internal.as_uchar.blue = b;
unsigned char Red() const {
return internal.as_uchar.red;
unsigned char Green() const {
return internal.as_uchar.green;
unsigned char Blue() const {
return internal.as_uchar.blue;
unsigned char Alpha() const {
return internal.as_uchar.alpha;
void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0) {
internal.as_uchar.alpha = a;
internal.as_uchar.red = r;
internal.as_uchar.green = g;
internal.as_uchar.blue = b;
void Red(unsigned char c) {
internal.as_uchar.red = c;
void Green(unsigned char c) {
internal.as_uchar.green = c;
void Blue(unsigned char c) {
internal.as_uchar.blue = c;
void Alpha(unsigned char c) {
internal.as_uchar.alpha = c;
/* The following tags are simply empty structures which are used
to tell the compiler which constructor we want when we know
struct BGD_EXPORT_DATA_PROT Png_tag {};
struct BGD_EXPORT_DATA_PROT Gif_tag {};
struct BGD_EXPORT_DATA_PROT WBMP_tag {};
struct BGD_EXPORT_DATA_PROT Jpeg_tag {};
struct BGD_EXPORT_DATA_PROT Gd_tag {};
struct BGD_EXPORT_DATA_PROT Gd2_tag {};
struct BGD_EXPORT_DATA_PROT Xbm_tag {};
/** This class GD::Image wraps all of the 'C' libgd functionality
for the convenience of C++ users. An instance of this class
corresponds to a single image.
class BGD_EXPORT_DATA_PROT Image
/** Construct a null image
/** Construct a blank image, of the given size and colour format type.
\param[in] sx Width of the image
\param[in] sy Height of the image
\param[in] istruecolor Create a true colour image, defaults to false, i.e. create an indexed palette image.
Image(int sx, int sy, bool istruecolor = false)
/** Construct a blank image, of the given size and colour format type.
\param[in] s Width and height of the image
\param[in] istruecolor Create a true colour image, defaults to false, i.e. create an indexed palette image.
Image(const Size & s, bool istruecolor = false)
/** Construct an instance of the GD::Image class, given the internal gdImage poimter.
Note that gdImageDestroy will be called on the image pointer in the destructor.
\param[in] i Pointer to the internal gdImage
/** Copy constructor. Construct an instance of the GD::Image class,
by making a copy of the GD::Image provided.
\param[in] i Reference to the image to be copied
Image(const GD::Image & i)
/** Construct an image by reading from \p in. This constructor
will first attempt to determine the file format.
\param[in] in The stream containing the image data
/** Construct an image by reading from \p in. This constructor
will first attempt to determine the file format.
\param[in] in An opened FILE * handle to a file containing the image data
/** Construct an image by reading from memory block \p data. This constructor
will first attempt to determine the image formatting.
\param[in] size The byte count of the memory block
\param[in] data Pointer to the memory block
Image(int size, void * data)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Png_tag()); // read a png file from input
\param[in] in The stream containing the image data
Image(std::istream & in, Png_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Png_tag()); // read a png file from input
\param[in] in An opened FILE * handle to a file containing the image data
Image(FILE * in, Png_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Png_tag()); // read a png file from input
\param[in] in The io context from which to read the image data
Image(gdIOCtx * in, Png_tag)
/** Construct an image by reading from memory block \p data.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(sz, dat, GD::Png_tag()); // read a png file from dat
\param[in] size The byte count of the memory block
\param[in] data Pointer to the memory block
Image(int size, void * data, Png_tag)
CreateFromPng(size, data);
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Gif_tag()); // read a gif file from input
\param[in] in The stream containing the image data
Image(std::istream & in, Gif_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Gif_tag()); // read a gif file from input
\param[in] in An opened FILE * handle to a file containing the image data
Image(FILE * in, Gif_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Gif_tag()); // read a gif file from input
\param[in] in The io context from which to read the image data
Image(gdIOCtx * in, Gif_tag)
/** Construct an image by reading from memory block \p data.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(sz, dat, GD::Gif_tag()); // read a gif file from dat
\param[in] size The byte count of the memory block
\param[in] data Pointer to the memory block
Image(int size, void * data, Gif_tag)
CreateFromGif(size, data);
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::WBMP_tag()); // read a monchrome WBMP file from input
\param[in] in The stream containing the image data
Image(std::istream & in, WBMP_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::WBMP_tag()); // read a monchrome WBMP file from input
\param[in] in An opened FILE * handle to a file containing the image data
Image(FILE * in, WBMP_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::WBMP_tag()); // read a monchrome WBMP file from input
\param[in] in The io context from which to read the image data
Image(gdIOCtx * in, WBMP_tag)
/** Construct an image by reading from memory block \p data.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(sz, dat, GD::WBMP_tag()); // read a monchrome WBMP file from dat
\param[in] size The byte count of the memory block
\param[in] data Pointer to the memory block
Image(int size, void * data, WBMP_tag)
CreateFromWBMP(size, data);
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Jpeg_tag()); // read a jpeg file from input
\param[in] in The stream containing the image data
Image(std::istream & in, Jpeg_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Jpeg_tag()); // read a jpeg file from input
\param[in] in An opened FILE * handle to a file containing the image data
Image(FILE * in, Jpeg_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Jpeg_tag()); // read a jpeg file from input
\param[in] in The io context from which to read the image data
Image(gdIOCtx * in, Jpeg_tag)
/** Construct an image by reading from memory block \p data.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(sz, dat, GD::Jpeg_tag()); // read a jpeg file from dat
\param[in] size The byte count of the memory block
\param[in] data Pointer to the memory block
Image(int size, void * data, Jpeg_tag)
CreateFromJpeg(size, data);
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Gd_tag()); // read a gd file from input
\param[in] in The stream containing the image data
Image(std::istream & in, Gd_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Gd_tag()); // read a gd file from input
\param[in] in An opened FILE * handle to a file containing the image data
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Gd_tag()); // read a gd file from input
\param[in] in The io context from which to read the image data
Image(gdIOCtx * in, Gd_tag)
/** Construct an image by reading from memory block \p data.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(sz, dat, GD::Gd_tag()); // read a gd file from dat
\param[in] size The byte count of the memory block
\param[in] data Pointer to the memory block
Image(int size, void * data, Gd_tag)
CreateFromGd(size, data);
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Gd2_tag()); // read a gd2 file from input
\param[in] in The stream containing the image data
Image(std::istream & in, Gd2_tag)
/** Construct an image by reading from \p in.
The tag is an empty struct which simply tells the compiler which image read function to use.
e.g. GD::Image img(input, GD::Png_tag()); // read a png file from input