UPGRADING UNIX USERS: READ THIS FIRST! Modern versions of gd install by default to /usr/local/lib and /usr/local/include. If you already have an older version of gd in /usr/lib and /usr/include, you may wish to use:./configure --prefix=/usrTo ensure that your new installation overwrites the old.GIF support has been restored in gd 2.0.28 and above. The well-known patents on LZW compression held by Unisys have expired in all countries. British Telecom and IBM may hold related patents but have never chosen to require royalties for GIF applications, to the best of my knowledge. I am not a lawyer and cannot give legal advice regarding this issue. PNG remains a superior format especially if lossless truecolor images are needed.
When building from soruce, gd 2.0.36 requires that the following libraries also be installed, in order to produce the related image formats. The win32 binary release (bgd) already contains the appropriate libraries. You may skip libraries associated with formats you do not use:
libpng (see the libpng home page), if you want PNG
zlib (see the info-zip home page), if you want PNG
jpeg-6b or later, if desired (see the Independent JPEG Group home page), if you want JPEG
If you want to use the TrueType font support, you must also install the FreeType 2.x library, including the header files. See the Freetype Home Page, or SourceForge. No, I cannot explain why that site is down on a particular day, and no, I can't send you a copy.
If you want to use the Xpm color bitmap loading support, you must also have the X Window System and the Xpm library installed (Xpm is often included in modern X distributions). Most of the time you won't need Xpm.
Please read the documentation and install the required libraries. Do not send email asking why
png.his not found. Do not send email asking whylibgd.sois not found, either. See the requirements section for more information. Thank you!
In order to resolve any possible confusion regarding the authorship of gd, the following copyright statement covers all of the authors who have required such a statement. If you are aware of any oversights in this copyright notice, please contact Pierre-A. Joye who will be pleased to correct them.
COPYRIGHT STATEMENT FOLLOWS THIS LINE
Portions copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 by Cold Spring Harbor Laboratory. Funded under Grant P41-RR02188 by the National Institutes of Health.Portions copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 by Boutell.Com, Inc.
Portions relating to GD2 format copyright 1999, 2000, 2001, 2002, 2003, 2004 Philip Warner.
Portions relating to PNG copyright 1999, 2000, 2001, 2002, 2003, 2004 Greg Roelofs.
Portions relating to gdttf.c copyright 1999, 2000, 2001, 2002, 2003, 2004 John Ellson (ellson@graphviz.org).
Portions relating to gdft.c copyright 2001, 2002, 2003, 2004 John Ellson (ellson@graphviz.org).
Portions copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Pierre-Alain Joye (pierre@libgd.org).
Portions relating to JPEG and to color quantization copyright 2000, 2001, 2002, 2003, 2004, Doug Becker and copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Thomas G. Lane. This software is based in part on the work of the Independent JPEG Group. See the file README-JPEG.TXT for more information.
Portions relating to GIF compression copyright 1989 by Jef Poskanzer and David Rowley, with modifications for thread safety by Thomas Boutell.
Portions relating to GIF decompression copyright 1990, 1991, 1993 by David Koblas, with modifications for thread safety by Thomas Boutell.
Portions relating to WBMP copyright 2000, 2001, 2002, 2003, 2004 Maurice Szmurlo and Johan Van den Brande.
Portions relating to GIF animations copyright 2004 Jaakko Hyvätti (jaakko.hyvatti@iki.fi)
Permission has been granted to copy, distribute and modify gd in any context without fee, including a commercial application, provided that this notice is present in user-accessible supporting documentation.
This does not affect your ownership of the derived work itself, and the intent is to assure proper credit for the authors of gd, not to interfere with your productive use of gd. If you have questions, ask. "Derived works" includes all programs that utilize the library. Credit must be given in user-accessible documentation.
This software is provided "AS IS." The copyright holders disclaim all warranties, either express or implied, including but not limited to implied warranties of merchantability and fitness for a particular purpose, with respect to this code and accompanying documentation.
Although their code does not appear in the current release, the authors also wish to thank Hutchison Avenue Software Corporation for their prior contributions.
END OF COPYRIGHT STATEMENT
gd is a graphics library. It allows your code to quickly draw images complete with lines, arcs, text, multiple colors, cut and paste from other images, and flood fills, and write out the result as a PNG or JPEG file. This is particularly useful in World Wide Web applications, where PNG and JPEG are two of the formats accepted for inline images by most browsers.
gd is not a paint program. If you are looking for a paint program, you are looking in the wrong place. If you are not a programmer, you are looking in the wrong place, unless you are installing a required library in order to run an application.
gd does not provide for every possible desirable graphics operation. It is not necessary or desirable for gd to become a kitchen-sink graphics package, but version 2.0 does include most frequently requested features, including both truecolor and palette images, resampling (smooth resizing of truecolor images) and so forth.
Please see the NEWS file, the releases announcements and the ChangeLog for
the changes in the releases after 2.0.34.
From 2.0.34 and later, please check the ISSUES and ChangeLog as well as
the releases announcements.
Version 2.0.33 restores compatibility with older releases
of Freetype 2.x in addition to the latest release. Thanks to
John Ellson and the graphviz project.
Version 2.0.32 restores correct detection of Unicode character sets
for freetype fonts, which repairs a bug that prevented umlauts from
displaying properly. Thanks to John Ellson and the graphviz project.
Also, version 2.0.32 builds all test programs
smoothly in the absence of libpng.
A minor type naming conflict prevented bgd.dll from compiling, and it
was left out of the distribution as a result. This has been corrected.
2.0.29 did not compile correctly when freetype was not available.
This has been corrected. Thanks to Alessandro Ranellucci.
The actual resolved font filename can be returned in the gdFTStringExtra
structure as the fontpath element if the gdFTEX_RETURNFONTPATHNAME flag
is set. Also, a
vector of character position advances can be retrieved if gdFTEX_XSHOW is set
in the flags element. .afm files (font metrics) are now used to adjust size
calculations
when available. When fontconfig is not available, gd falls back to its
usual behavior and requires a specific font file name. One can
still fetch fonts by filename when gdFTUseFontConfig(1) is in effect, by
setting the gdFTEX_FONTPATHNAME flag
in the flag element of the gdFTStringExtra structure. Thanks to
Dag Lem and John Ellson.
The following enhancements and fixes:
Thanks to Lars Hecking and Lincoln Stein for their advice on
implementing this feature. Any blame for the actual implementation
is entirely due to me (TBB).
Owing to an oversight while making changes to better accommodate the use
of gd as a DLL, the extern qualifier was dropped from the
declarations of font pointers in 2.0.24. This has been corrected.
Thanks to Richard ("OpenMacNews").
Windows DLL now uses __stdcall calling convention. Existing
applications will require a recompile, using the new version of gd.h,
in order to use this version of the DLL. However, Visual BASIC and other
non-C programmers will now be able to use the DLL, which is an enormous
benefit and justifies the one-time inconvenience to existing DLL users.
The elaborate #ifdef test for older versions of Freetype without
FT_ENCODING_MS_SYMBOL was needed in a second place also. Thanks to
David R. Morrison.
An off-by-one error in gdImageToPalette caused transparency to be applied
to the wrong pixels. Thanks to "Super Pikeman."
Output dpi specification option added to the
Minor compilation and packaging problems with 2.0.16 were corrected.
If 2.0.16 compiled without errors for you, then you don't need
to upgrade to 2.0.17.
Version 2.0.7 corrects a problem which caused 'configure' to complain
that the directory NONE was not found, in various places, causing
the configuration process to stop. There are no code changes.
What's new in version 2.0.34?
What's new in version 2.0.33?
What's new in version 2.0.32?
What's new in version 2.0.31?
What's new in version 2.0.30?
What's new in version 2.0.29?
What's new in version 2.0.28?
What's new in version 2.0.27?
What's new in version 2.0.26?
gdlib-config, which has been installed by
make install for some time now, has gained
a --features option. This option produces a space-separated
list of optional features with which the gd library was compiled.
Typical usage looks like this:
% gdlib-config --features
GD_XPM GD_JPEG GD_FREETYPE GD_PNG GD_GIF
Other configure scripts can conveniently define
preprocessor symbols based on this list in order to conditionally
compile code. For instance, if
GD_PNG is not reported by --features, then gdImagePng is not
included in the library.
What's new in version 2.0.25?
What's new in version 2.0.24?
What's new in version 2.0.23?
gdFTStringExtra structure, thanks to
Mark Shackelford. See gdImageStringFTEx.
What's new in version 2.0.22?
gdFontPtr.
Direct use of gdFontLarge, etc. is strongly deprecated
for users of bgd.dll; use these new functions instead.
circletexttest if
PNG support is missing.
What's new in version 2.0.21?
gdImageCreateFrom*Ptr family
of functions which make it convenient to load an image in any
GD-supported format directly from memory.
gdNewDynamicCtxEx function was added to
support the easy implementation of the above functions and to
correct a design problem which made life unpleasant for those passing
in memory not originally allocated by gd to the
gdNewDynamicCtx function by provoding a way to specify
that gd should never free or reallocate a particular block of memory.
The gdNewDynamicCtx function and its relatives, although
still exported for ABI compatibility, are now deprecated except
for internal use, in favor of gdImageCreateFromPngPtr
and its relatives.
gdFreeFontCache function,
an undocumented function added in an earlier release which now simply
calls gdFontCacheShutdown for backwards compatibility.
This repairs build problems when compiling PHP against the latest gd.
What's new in version 2.0.20?
gdFreeFontCache function,
an undocumented function added in an earlier release which now simply
calls gdFontCacheShutdown for backwards compatibility.
This repairs build problems when compiling PHP against the latest gd.
What's new in version 2.0.19?
extern declarations for the
gd font pointers inadvertently removed in 2.0.18.
What's new in version 2.0.18?
What's new in version 2.0.17?
What's new in version 2.0.16?
What's new in version 2.0.15?
What's new in version 2.0.14?
What's new in version 2.0.13?
main() function of one of the test programs
was accidentally included in the gd shared library, causing problems
on some platforms. This has been corrected. Thanks to many people
who pointed this out.
What's new in version 2.0.12?
gd2 file format routines
were fixed by Steven Brown. These problems were due to the
incorrect use of a signed integer.
gd file format correctly
for truecolor images. Truecolor gd files created with
earlier releases in the 2.0 series must be written again. The gd
file format is used to quickly load an entire uncompressed image, typically
an existing background to which additional material will be added; it is not a
general purpose file format. More advanced capabilities are also available
via the gd2 format. Thanks to Andreas Pfaller for reporting
the problem.
What's new in version 2.0.11?
configure improvements by Lars Hecking.
What's new in version 2.0.10?
What's new in version 2.0.9?
configure fixes from Lars Hecking. Thanks, Lars.
What's new in version 2.0.8?
What's new in version 2.0.7?
What's new in version 2.0.6?
-Wall, -ansi
and -pedantic options enabled.
What's new in version 2.0.5?
configure time using this syntax:
./configure --prefix=/usr
configure script should be compatible with all standard
GNU configure options and will figure out the correct settings for a
much wider range of operating systems. Many, many thanks to
Lars Hecking for taking care of this.
What's new in version 2.0.4?
The following contributions from John Ellson:
And the following additional fixes:
configure now correctly detects and provides
support for the Xpm library and its dependencies (Len Makin)
What's new in version 2.0.3?
configure script has been extensively modified
to work properly in tests with both Solaris and Linux. Other platforms
should also work based on feedback received and integrated to date.
--prefix option to configure
works properly.
annotate utility has been added. This is a
very handy tool for adding freetype text to existing JPEGs. After
make install, type annotate -h for more
information. Thanks to Joel Dubiner.
What's new in version 2.0.2?
What's new in version 2.0.1?
readme.jpn file.
What's new in version 2.0?
pixels array
will fail only if it encounters an existing truecolor image, which may
happen if the code attempts to open and modify an existing JPEG or
truecolor PNG. Such code should be modified to check the
trueColor flag of the gdImage structure, and
refer to the tpixels array instead when it is set.
What's new in version 1.8.4?
What's new in version 1.8.3?
#include <gd.h> corrected to #include "gd.h" in gd_wbmp.c
What's new in version 1.8.2?
What's new in version 1.8.1?
What's new in version 1.8?
Additional JPEG Information
Support for reading and writing JPEG-format images is courtesy
of Doug Becker and the Independent JPEG Group / Thomas G. Lane. You
can get the latest version of the IJG JPEG software from ftp://ftp.uu.net/graphics/jpeg/
(e.g., the jpegsrc.v6b.tar.gz
file). You must use
version 6b or later of the IJG JPEG software. You might also consult
the JPEG FAQ at
http://www.faqs.org/faqs/jpeg-faq/.
gdtestttf.c was corrected.
This bug caused crashes at the end of each call to gdImageStringTTF on
some platforms. Thanks to Wolfgang Haefelinger.
Documentation fixes. Thanks to Dohn Arms.
Makefile fixes to permit linking with all libraries required on platforms with order- dependent linkers.
gdtestttf.c. Thanks to Masahito Yamaga.
autoconf and configure have been removed, in favor of a
carefully designed Makefile which produces and properly installs
the library and the binaries. System-dependent variables are
at the top of the Makefile for easy modification. I'm sorry,
folks, but autoconf generated many, many confused email
messages from people who didn't have things where autoconf
expected to find them. I am not an autoconf/automake wizard, and
gd is a simple, very compact library which does not need to
be a shared library. I did make many improvements
over the old gd 1.3 Makefile, which were directly inspired by the
autoconf version found in the 1.6 series (thanks to John Ellson).
-pedantic-errors
flag of gcc. Several pieces of not-quite-ANSI-C code were causing problems
for those with non-gcc compilers.
gdttf.c patched to allow the use of Windows symbol
fonts, when present (thanks to Joseph Peppin).
extern "C" wrappers added to gd.h and the
font header files for the convenience of C++ programmers.
bdftogd was also modified to automatically insert these
wrappers into future font header files. Thanks to John Lindal.
SEEK_SET.
Thanks to Robert Bonomi.
gdImageCreateFromXpm
function, if the Xpm library is available. Thanks to Caolan McNamara.
if ((color=gdImageColorExact(im,R,G,B)) < 0)
if ((color=gdImageColorAllocate(im,R,G,B)) < 0)
color=gdImageColorClosest(im,R,G,B);
Also in this release the build process has been converted to GNU autoconf/automake/libtool conventions so that both (or either) static and shared libraries can be built.
Unlike gd 1.6, users should have no trouble linking with gd 1.6.1 if they follow the instructions and install all of the pieces. However, If you get undefined symbol errors, be sure to check for older versions of libpng in your library directories!
Support for 8-bit palette PNG images has been added. Support for GIF has been removed. This step was taken to completely avoid the legal controversy regarding the LZW compression algorithm used in GIF. Unisys holds a patent which is relevant to LZW compression. PNG is a superior image format in any case. Now that PNG is supported by both Microsoft Internet Explorer and Netscape (in their recent releases), we highly recommend that GD users upgrade in order to get well-compressed images in a format which is legally unemcumbered.
This format also supports version numbers and rudimentary validity checks, so it should be more 'supportable' than the previous GD format.
pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors, type ./configure --help for more
information about the available options. In the unlikely event
that the GNU autoconf-produced configure script does not work well
for you, you may wish to try configure.pl, a
simple Perl script with similar but less complete capabilities.
If all else fails, try renaming makefile.sample
to Makefile. However, ./configure is
almost always your best bet.
For Windows
Use the DLL version! See the paragraph at the beginning of this sectino. If you really want to compile it yourself for some strange reason, read on.Create a project using your favorite programming environment. Copy all of the gd files to the project directory. Add
gd.cto your project. Add other source files as appropriate. Learning the basic skills of creating projects with your chosen C environment is up to you. Alternatively, use the freemingw32orcygwintools, which may prove to be compatible with the providedconfigurescript.
If you wish to test the library, type "make test" AFTER you have successfully executed "make install". This will build several test programs, including "gddemo". (Not all of these programs are expected to print completely successful messages, depending on the nature of the image formats with which some of the tests are tried; for instance, WBMP is a black and white format, so loss of color information is expected there.) Run gddemo to see some of the capabilities of gd. Run gdtestft to play with the freetype support, if you have built gd with it and have access to truetype fonts.
gddemo should execute without incident, creating the file demoout.png. (Note there is also a file named demoin.png, which is provided in the package as part of the demonstration.)
Display demoout.png in your PNG viewer. The image should be 128x128 pixels and should contain an image of the space shuttle with quite a lot of graphical elements drawn on top of it.
(If you are missing the demoin.png file, the other items should appear anyway.)
Look at demoin.png to see the original space shuttle image which was scaled and copied into the output image.
-lgd -lpng -lz -ljpeg -lfreetype -lmAssuming that all of these libraries are available.
If you want to use the provided simple fonts, include gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For more impressive results, install FreeType 2.x and use the gdImageStringFT function. If you are not using the provided Makefile and/or a library-based approach, be sure to include the source modules as well in your project. (They may be too large for 16-bit memory models, that is, 16-bit DOS and Windows.)
Here is a short example program. (For a more advanced example, see gddemo.c, included in the distribution. gddemo.c is NOT the same program; it demonstrates additional features!)
/* Bring in gd library functions */
#include "gd.h"
/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>
int main() {
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *pngout, *jpegout;
/* Declare color indexes */
int black;
int white;
/* Allocate the image: 64 pixels across by 64 pixels tall */
im = gdImageCreate(64, 64);
/* Allocate the color black (red, green and blue all minimum).
Since this is the first color in a new image, it will
be the background color. */
black = gdImageColorAllocate(im, 0, 0, 0);
/* Allocate the color white (red, green and blue all maximum). */
white = gdImageColorAllocate(im, 255, 255, 255);
/* Draw a line from the upper left to the lower right,
using white color index. */
gdImageLine(im, 0, 0, 63, 63, white);
/* Open a file for writing. "wb" means "write binary", important
under MSDOS, harmless under Unix. */
pngout = fopen("test.png", "wb");
/* Do the same for a JPEG-format file. */
jpegout = fopen("test.jpg", "wb");
/* Output the image to the disk file in PNG format. */
gdImagePng(im, pngout);
/* Output the same image in JPEG format, using the default
JPEG quality setting. */
gdImageJpeg(im, jpegout, -1);
/* Close the files. */
fclose(pngout);
fclose(jpegout);
/* Destroy the image in memory. */
gdImageDestroy(im);
}
When executed, this program creates an image, allocates
two colors (the first color allocated becomes the background
color), draws a diagonal line (note that 0, 0 is the upper
left corner), writes the image to PNG and JPEG files, and
destroys the image.
The above example program should give you an idea of how the package works. gd provides many additional functions, which are listed in the following reference chapters, complete with code snippets demonstrating each. There is also an alphabetical index.
webpng.c is provided in the distribution. Unix users can simply type "make webpng" to compile the program. Type "webpng" with no arguments to see the available options.
gdImage(TYPE)
pixels
or tpixels arrays. If the trueColor flag
is set, the tpixels array is valid; otherwise the
pixels array is valid.
The colorsTotal, red, green,
blue, alpha and open arrays
manage the palette. They are valid only when the trueColor
flag is not set.
The transparent value contains the palette index of the first
transparent color as read-only information for backwards compatibility;
gd 2.0 stores this information in the alpha array so that
variable transparency can be supported for each palette entry. However,
for truecolor images, transparent represents a single
RGB color which is always 100% transparent, and this
feature is generally supported by browsers which do not support
full alpha channels.
typedef struct {
/* Palette-based image pixels */
unsigned char ** pixels;
int sx;
int sy;
/* These are valid in palette images only. See also
/* 'alpha', which appears later in the structure to
preserve binary backwards compatibility */
int colorsTotal;
int red[gdMaxColors];
int green[gdMaxColors];
int blue[gdMaxColors];
int open[gdMaxColors];
/* For backwards compatibility, this is set to the
first palette entry with 100% transparency,
and is also set and reset by the
gdImageColorTransparent function. Newer
applications can allocate palette entries
with any desired level of transparency; however,
bear in mind that many viewers, notably
many web browsers, fail to implement
full alpha channel for PNG and provide
support for full opacity or transparency only. */
int transparent;
int *polyInts;
int polyAllocated;
struct gdImageStruct *brush;
struct gdImageStruct *tile;
int brushColorMap[gdMaxColors];
int tileColorMap[gdMaxColors];
int styleLength;
int stylePos;
int *style;
int interlace;
/* New in 2.0: alpha channel for palettes. Note that only
Macintosh Internet Explorer and (possibly) Netscape 6
really support multiple levels of transparency in
palettes, to my knowledge, as of 2/15/01. Most
common browsers will display 100% opaque and
100% transparent correctly, and do something
unpredictable and/or undesirable for levels
in between. TBB */
int alpha[gdMaxColors];
/* Truecolor flag and pixels. New 2.0 fields appear here at the
end to minimize breakage of existing object code. */
int trueColor;
int ** tpixels;
/* Should alpha channel be copied, or applied, each time a
pixel is drawn? This applies to truecolor images only.
No attempt is made to alpha-blend in palette images,
even if semitransparent palette entries exist.
To do that, build your image as a truecolor image,
then quantize down to 8 bits. */
int alphaBlendingFlag;
/* Should the alpha channel of the image be saved? This affects
PNG at the moment; other future formats may also
have that capability. JPEG doesn't. */
int saveAlphaFlag;
} gdImage;
The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
gd2 file format, which supports quick loading of
partial images. The gd_free function will not be invoked when
calling the standard Ctx functions; it is an implementation
convenience when adding new data types to gd. For examples,
see gd_png.c, gd_gd2locate ompression and
decompression routines slightly, because horizontally adjacent
pixels are now next to each other in memory. This should
not affect properly written gd applications, but applications that
directly manipulate the pixels array will require
changes.
To use gd, you will need an ANSI C compiler. All popular Windows 95 and NT C compilers are ANSI C compliant. Any full-ANSI-standard C compiler should be adequate. The cc compiler released with SunOS 4.1.3 is not an ANSI C compiler. Most Unix users who do not already have gcc should get it. gcc is free, ANSI compliant and a de facto industry standard. Ask your ISP why it is missing.
As of version 1.6, you also need the zlib compression library, and the libpng library. As of version 1.6.2, you can draw text using antialiased TrueType fonts if you also have the libttf library installed, but this is not mandatory. zlib is available for a variety of platforms from the zlib web site. libpng is available for a variety of platforms from the PNG web site.
You will also want a PNG viewer, if you do not already have one for your system, since you will need a good way to check the results of your work. Netscape 4.04 and higher, and Microsoft Internet Explorer 4.0 or higher, both support PNG. Not every PNG-compatible viewer supports alpha channel transparency, which is why gd 2.0.2 and above do alpha blending in the library by default; it is possible to turn on the saving of alpha channel information to the file instead.
Win32 DLL users: if you are using MSVC, use the provided batch file
makemsvcimport.bat to make a bgd.lib import library
corresponding to the provided bgd.dll. Copy bgd.dll to your
application directory, or to your Windows sytem directory. In the
settings of your MSVC project, you MUST choose the
"multithreaded DLL" library option under "code generation."
mingw32 and cygwin users can simply link with the provided libbgd.a
stub library in order to use the DLL.
Building gd From the Source
In order to build gd, you must first unpack the archive you have
downloaded. If you are not familiar with tar and
gunzip (Unix) or ZIP (Windows), please
consult with an experienced user of your system. Sorry, we cannot
answer questions about basic Internet skills.
Unpacking the archive will produce a directory called "gd-2.0.33".
cd to the 2.0.33 directory and type:
./configure
NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN/usr/local/liband the include files are installed in/usr/local/include. IF YOU ARE UPGRADING, you may wish to use:./configure --prefix=/usrRather than just./configure, before typingmakeandmake install.
If all goes well, this will create a Makefile. If all does not go well --
for instance, if neither the the JPEG nor the PNG and ZLIB libraries
are found -- you will need to install those libraries, then come back
and run configure again.
If necessary, make changes to the resulting Makefile. Then, type "make". If there are no errors, follow this with "make install". Because gd 2.0 and above installs as a shared library, it is necessary to install the library properly before running gd-based programs.
If you get errors