Daniel@0: Daniel@0: Daniel@0: gd 2.0.34 Daniel@0: Daniel@0: Daniel@0: Daniel@0:

gd 2.0.33

Daniel@0:

A graphics library for fast image creation

Daniel@0:

Follow this link to the Daniel@0: latest version Daniel@0: of this document.

Daniel@0:
Daniel@0: UPGRADING UNIX USERS: READ THIS FIRST! Daniel@0: Modern versions of gd install by default to /usr/local/lib and Daniel@0: /usr/local/include. If you already have an older version of gd Daniel@0: in /usr/lib and /usr/include, you may wish to use: Daniel@0:
Daniel@0: ./configure --prefix=/usr
Daniel@0: To ensure that your Daniel@0: new installation overwrites the old. Daniel@0:

Daniel@0: GIF support has been restored in gd 2.0.28 and above. Daniel@0: The well-known patents on LZW compression held by Unisys Daniel@0: have expired in all countries. British Telecom and IBM may hold related Daniel@0: patents but have never chosen to require royalties for GIF applications, Daniel@0: to the best of my knowledge. I am not a lawyer and cannot give Daniel@0: legal advice regarding this issue. PNG remains a superior format especially Daniel@0: if lossless truecolor images are needed. Daniel@0:

Daniel@0: When building from soruce, gd 2.0.33 requires that the Daniel@0: following libraries also be installed, in order to produce the related Daniel@0: image formats. The win32 binary release (bgd) already contains the Daniel@0: appropriate libraries. Daniel@0: You may skip libraries associated with formats you do not use: Daniel@0:

Daniel@0: libpng (see the libpng home page), if you want PNG Daniel@0:

Daniel@0: zlib (see the info-zip home page), if you want PNG Daniel@0:

Daniel@0: jpeg-6b or later, if desired (see the Independent JPEG Group home page), if you want JPEG Daniel@0:

Daniel@0: If you want to use the TrueType font support, you must also Daniel@0: install the FreeType 2.x library, including Daniel@0: the header files. See the Freetype Daniel@0: Home Page, or SourceForge. Daniel@0: No, I cannot explain why that site is down on a particular day, and no, I Daniel@0: can't send you a copy. Daniel@0:

Daniel@0: If you want to use the Xpm color bitmap loading support, you must also Daniel@0: have the X Window System and the Xpm library installed (Xpm is often Daniel@0: included in modern X distributions). Most of the time you won't Daniel@0: need Xpm. Daniel@0:

Daniel@0: Please read the documentation and install the required libraries. Daniel@0: Do not send email asking why png.h is not found. Daniel@0: Do not send email asking why libgd.so is not found, either. Daniel@0: See the requirements section for more Daniel@0: information. Thank you! Daniel@0:

Daniel@0:

Table of Contents

Daniel@0: Daniel@0:

Daniel@0: Up to the LibGD Homepage Daniel@0:

Credits and license terms

Daniel@0:

Daniel@0: In order to resolve any possible confusion regarding the authorship Daniel@0: of gd, the following copyright statement covers all of the authors Daniel@0: who have required such a statement. If you are aware of any oversights Daniel@0: in this copyright notice, please contact Pierre-A. Joye who will be Daniel@0: pleased to correct them. Daniel@0:

Daniel@0: COPYRIGHT STATEMENT FOLLOWS THIS LINE
Daniel@0: 
Daniel@0:
Daniel@0: Daniel@0: Portions copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 by Cold Spring Daniel@0: Harbor Laboratory. Funded under Grant P41-RR02188 by the National Daniel@0: Institutes of Health. Daniel@0:

Daniel@0: Portions copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 by Boutell.Com, Inc. Daniel@0:

Daniel@0: Portions relating to GD2 format copyright 1999, 2000, 2001, 2002, 2003, 2004 Philip Warner. Daniel@0:

Daniel@0: Portions relating to PNG copyright 1999, 2000, 2001, 2002, 2003, 2004 Greg Roelofs. Daniel@0:

Daniel@0: Portions relating to gdttf.c copyright 1999, 2000, 2001, 2002, 2003, 2004 John Ellson (ellson@graphviz.org). Daniel@0:

Daniel@0: Portions relating to gdft.c copyright 2001, 2002, 2003, 2004 John Ellson (ellson@graphviz.org). Daniel@0:

Daniel@0: Portions copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Pierre-Alain Joye (pierre@libgd.org). Daniel@0: Daniel@0:

Daniel@0: 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 Daniel@0: in part on the work of the Independent JPEG Group. See the file Daniel@0: README-JPEG.TXT for more information. Daniel@0:

Daniel@0: Portions relating to GIF compression copyright 1989 by Jef Daniel@0: Poskanzer and David Rowley, with modifications for thread safety Daniel@0: by Thomas Boutell. Daniel@0:

Daniel@0: Portions relating to GIF decompression copyright 1990, 1991, 1993 Daniel@0: by David Koblas, with modifications for thread safety by Daniel@0: Thomas Boutell. Daniel@0:

Daniel@0: Portions relating to WBMP copyright 2000, 2001, 2002, 2003, 2004 Maurice Szmurlo and Johan Van Daniel@0: den Brande. Daniel@0:

Daniel@0: Portions relating to GIF animations copyright 2004 Jaakko Hyvätti (jaakko.hyvatti@iki.fi) Daniel@0:

Daniel@0: Permission has been granted to copy, distribute and modify gd in any Daniel@0: context without fee, including a commercial application, provided that this notice Daniel@0: is present in user-accessible supporting documentation. Daniel@0:

Daniel@0: This does not affect your ownership of the derived work itself, and the intent Daniel@0: is to assure proper credit for the authors of gd, not to interfere Daniel@0: with your productive use of gd. If you have questions, ask. Daniel@0: "Derived works" includes all programs that utilize the library. Daniel@0: Credit must be given in user-accessible documentation. Daniel@0:

Daniel@0: This software is provided "AS IS." Daniel@0: The copyright holders disclaim all warranties, either express or implied, Daniel@0: including but not limited to implied warranties of merchantability and Daniel@0: fitness for a particular purpose, with respect to this code and accompanying Daniel@0: documentation. Daniel@0:

Daniel@0: Although their code does not appear in the current release, the authors Daniel@0: also wish to thank Hutchison Avenue Software Corporation for their Daniel@0: prior contributions. Daniel@0:

Daniel@0:
Daniel@0: END OF COPYRIGHT STATEMENT
Daniel@0: 
Daniel@0:

What is gd?

Daniel@0:

Daniel@0: gd is a graphics library. It allows your code to quickly Daniel@0: draw images complete with lines, arcs, text, multiple Daniel@0: colors, cut and paste from other images, and flood fills, and Daniel@0: write out the result as a PNG or JPEG file. This is particularly Daniel@0: useful in World Wide Web applications, where PNG and JPEG are two Daniel@0: of the formats accepted for inline images by most browsers. Daniel@0:

Daniel@0: gd is not a paint program. Daniel@0: If you are looking for a paint program, you are looking in Daniel@0: the wrong place. If you are not a programmer, you are looking Daniel@0: in the wrong place, unless you are installing a required Daniel@0: library in order to run an application. Daniel@0:

Daniel@0: gd does not provide for every possible desirable graphics Daniel@0: operation. It is not necessary or desirable for gd to become Daniel@0: a kitchen-sink graphics package, but version 2.0 does include Daniel@0: most frequently requested features, including both truecolor and Daniel@0: palette images, resampling (smooth resizing of truecolor images) Daniel@0: and so forth. Daniel@0:

Daniel@0:

What if I want to use another programming Daniel@0: language?

Daniel@0: Not all of these tools are necessarily up to date and fully compatible Daniel@0: with 2.0.33. Daniel@0:

PHP

Daniel@0: A variant of gd 2.x is included in PHP 4.3.0. It is also possible Daniel@0: to patch PHP 4.2.3 for use with gd 2.0.33; see the Daniel@0: gd home page for a link to Daniel@0: that information. It would be a Good Idea to merge all of the things Daniel@0: that are better in mainstream gd and all of the things that are Daniel@0: better in PHP gd at some point in the near future. Daniel@0:

Perl

Daniel@0: gd can also be used from Perl, courtesy of Daniel@0: Lincoln Stein's Daniel@0: Daniel@0: GD.pm library, which uses gd as the basis for a set of Daniel@0: Perl 5.x classes. Highly recommended. Daniel@0:

OCaml

Daniel@0: gd can be used from OCaml, thanks to Daniel@0: Matt Gushee's GD4O project. Daniel@0:

Tcl

Daniel@0: gd can be used from Tcl with John Ellson's Daniel@0: Gdtclft Daniel@0: dynamically loaded extension package. Daniel@0:

Pascal

Daniel@0: Pascal enthusiasts should look into the Daniel@0: freepascal project, a Daniel@0: free Pascal compiler that includes gd support. Daniel@0:

REXX

Daniel@0: A Daniel@0: gd interface Daniel@0: for the REXX language is available. Daniel@0:

Any Language

Daniel@0: The "fly" interpreter performs gd operations specified in a text file. Daniel@0: You can output the desired commands to a simple Daniel@0: text file from whatever scripting language you prefer to use, then Daniel@0: invoke the interpreter. Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.34?

Daniel@0:

Daniel@0: From 2.0.34 and later, please check the ISSUES and ChangeLog as well as Daniel@0: the releases announcements. Daniel@0:

Daniel@0:

What's new in version 2.0.33?

Daniel@0:

Daniel@0: Version 2.0.33 restores compatibility with older releases Daniel@0: of Freetype 2.x in addition to the latest release. Thanks to Daniel@0: John Ellson and the graphviz project. Daniel@0:

Daniel@0:

What's new in version 2.0.32?

Daniel@0:

Daniel@0: Version 2.0.32 restores correct detection of Unicode character sets Daniel@0: for freetype fonts, which repairs a bug that prevented umlauts from Daniel@0: displaying properly. Thanks to John Ellson and the graphviz project. Daniel@0: Also, version 2.0.32 builds all test programs Daniel@0: smoothly in the absence of libpng. Daniel@0:

Daniel@0:

What's new in version 2.0.31?

Daniel@0:

Daniel@0: A minor type naming conflict prevented bgd.dll from compiling, and it Daniel@0: was left out of the distribution as a result. This has been corrected. Daniel@0:

Daniel@0:

What's new in version 2.0.30?

Daniel@0:

Daniel@0: 2.0.29 did not compile correctly when freetype was not available. Daniel@0: This has been corrected. Thanks to Alessandro Ranellucci. Daniel@0:

Daniel@0:

What's new in version 2.0.29?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.28?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.27?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.26?

Daniel@0:

Daniel@0: The following enhancements and fixes: Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.25?

Daniel@0:

Daniel@0: Owing to an oversight while making changes to better accommodate the use Daniel@0: of gd as a DLL, the extern qualifier was dropped from the Daniel@0: declarations of font pointers in 2.0.24. This has been corrected. Daniel@0: Thanks to Richard ("OpenMacNews"). Daniel@0:

Daniel@0:

What's new in version 2.0.24?

Daniel@0:

Daniel@0: Windows DLL now uses __stdcall calling convention. Existing Daniel@0: applications will require a recompile, using the new version of gd.h, Daniel@0: in order to use this version of the DLL. However, Visual BASIC and other Daniel@0: non-C programmers will now be able to use the DLL, which is an enormous Daniel@0: benefit and justifies the one-time inconvenience to existing DLL users. Daniel@0:

Daniel@0: The elaborate #ifdef test for older versions of Freetype without Daniel@0: FT_ENCODING_MS_SYMBOL was needed in a second place also. Thanks to Daniel@0: David R. Morrison. Daniel@0:

Daniel@0: An off-by-one error in gdImageToPalette caused transparency to be applied Daniel@0: to the wrong pixels. Thanks to "Super Pikeman." Daniel@0:

Daniel@0:

What's new in version 2.0.23?

Daniel@0:

Daniel@0: Output dpi specification option added to the Daniel@0: gdFTStringExtra structure, thanks to Daniel@0: Mark Shackelford. See gdImageStringFTEx. Daniel@0:

Daniel@0:

What's new in version 2.0.22?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.21?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.20?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.19?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.18?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.17?

Daniel@0:

Daniel@0: Minor compilation and packaging problems with 2.0.16 were corrected. Daniel@0: If 2.0.16 compiled without errors for you, then you don't need Daniel@0: to upgrade to 2.0.17. Daniel@0:

Daniel@0:

What's new in version 2.0.16?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.15?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.14?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.13?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.12?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.11?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.10?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.9?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.8?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.7?

Daniel@0:

Daniel@0: Version 2.0.7 corrects a problem which caused 'configure' to complain Daniel@0: that the directory NONE was not found, in various places, causing Daniel@0: the configuration process to stop. There are no code changes. Daniel@0:

Daniel@0:

What's new in version 2.0.6?

Daniel@0:

Daniel@0:

Daniel@0:

Daniel@0:

What's new in version 2.0.5?

Daniel@0: Daniel@0:

Daniel@0:

What's new in version 2.0.4?

Daniel@0: The following contributions from John Ellson: Daniel@0: Daniel@0: And the following additional fixes: Daniel@0: Daniel@0:

What's new in version 2.0.3?

Daniel@0: Daniel@0:

Daniel@0:

What's new in version 2.0.2?

Daniel@0: Daniel@0:

What's new in version 2.0.1?

Daniel@0: Daniel@0:

What's new in version 2.0?

Daniel@0: Daniel@0:

What's new in version 1.8.4?

Daniel@0: Daniel@0:

What's new in version 1.8.3?

Daniel@0: Daniel@0:

Daniel@0:

What's new in version 1.8.2?

Daniel@0: Daniel@0:

What's new in version 1.8.1?

Daniel@0: Daniel@0:

What's new in version 1.8?

Daniel@0: Daniel@0:

Additional JPEG Information

Daniel@0: Support for reading and writing JPEG-format images is courtesy Daniel@0: of Doug Becker and the Independent JPEG Group / Thomas G. Lane. You Daniel@0: can get the latest version of the IJG JPEG software from ftp://ftp.uu.net/graphics/jpeg/ Daniel@0: (e.g., the jpegsrc.v6b.tar.gz Daniel@0: file). You must use Daniel@0: version 6b or later of the IJG JPEG software. You might also consult Daniel@0: the JPEG FAQ at Daniel@0: http://www.faqs.org/faqs/jpeg-faq/. Daniel@0:

What's new in version 1.7.3?

Daniel@0: Another attempt at Makefile fixes to permit Daniel@0: linking with all libraries required on platforms with order- Daniel@0: dependent linkers. Perhaps it will work this time. Daniel@0:

What's new in version 1.7.2?

Daniel@0: An uninitialized-pointer bug in gdtestttf.c was corrected. Daniel@0: This bug caused crashes at the end of each call to gdImageStringTTF on Daniel@0: some platforms. Thanks to Wolfgang Haefelinger. Daniel@0:

Daniel@0: Documentation fixes. Thanks to Dohn Arms. Daniel@0:

Daniel@0: Makefile fixes to permit Daniel@0: linking with all libraries required on platforms with order- Daniel@0: dependent linkers. Daniel@0:

What's new in version 1.7.1?

Daniel@0: A minor buglet in the Makefile was corrected, as well as an inaccurate Daniel@0: error message in gdtestttf.c. Thanks to Masahito Yamaga. Daniel@0:

What's new in version 1.7?

Daniel@0: Version 1.7 contains the following changes: Daniel@0: Daniel@0:

What's new in version 1.6.3?

Daniel@0: Version 1.6.3 corrects a memory leak in gd_png.c. This leak caused Daniel@0: a significant amount of memory to be allocated and not freed when Daniel@0: writing a PNG image. Daniel@0:

What's new in version 1.6.2?

Daniel@0: Version 1.6.2 from John Ellson adds two new functions: Daniel@0: Daniel@0:

Daniel@0: Also in this release the build process has been converted to Daniel@0: GNU autoconf/automake/libtool conventions so that both (or either) Daniel@0: static and shared libraries can be built. Daniel@0:

What's new in version 1.6.1?

Daniel@0: Version 1.6.1 incorporates superior PNG reading and writing code Daniel@0: from Greg Roelofs, with minor modifications by Tom Boutell. Daniel@0: Specifically, I altered his code to read non-palette images Daniel@0: (converting them to palette images badly, by dithering them), Daniel@0: and to tolerate palette images with types of transparency that Daniel@0: gd doesn't actually support (it just ignores the advanced Daniel@0: transparency features). Any bugs in this area are therefore my Daniel@0: fault, not Greg's. Daniel@0:

Daniel@0: Unlike gd 1.6, users should have no trouble linking with Daniel@0: gd 1.6.1 if they follow the instructions and install all of Daniel@0: the pieces. However, If you get undefined symbol errors, Daniel@0: be sure to check for older versions of libpng in your Daniel@0: library directories! Daniel@0:

What's new in version 1.6?

Daniel@0: Version 1.6 features the following changes: Daniel@0:

Daniel@0: Support for 8-bit palette PNG images has been added. Daniel@0: Support for GIF has been removed. This step was taken Daniel@0: to completely avoid the legal controversy regarding the LZW Daniel@0: compression algorithm used in GIF. Unisys holds a patent which Daniel@0: is relevant to LZW compression. PNG is a superior image format Daniel@0: in any case. Now that PNG is supported by both Microsoft Daniel@0: Internet Explorer and Netscape (in their recent releases), Daniel@0: we highly recommend that GD users upgrade in order to get Daniel@0: well-compressed images in a format which is legally unemcumbered. Daniel@0: Daniel@0:

What's new in version 1.5?

Daniel@0: Daniel@0: Version 1.5 featured the following changes: Daniel@0: Daniel@0:
Daniel@0:
New GD2 format Daniel@0:
An improvement over the GD format, the GD2 format uses the zlib Daniel@0: compression library to compress the image in chunks. This results Daniel@0: in file sizes comparable to GIFs, with the ability to access parts Daniel@0: of large images without having to read the entire image into memory. Daniel@0:

Daniel@0: This format also supports version numbers and rudimentary validity Daniel@0: checks, so it should be more 'supportable' than the previous GD format. Daniel@0:

Daniel@0:

Re-arranged source files Daniel@0:
gd.c has been broken into constituant parts: io, gif, gd, gd2 and Daniel@0: graphics functions are now in separate files. Daniel@0:

Daniel@0:

Extended I/O capabilities. Daniel@0:
The source/sink feature has been extended to support GD2 file formats (which Daniel@0: require seek/tell functions; seek must return 1 for success, 0 for failure), and to allow more general non-file I/O. Daniel@0:

Daniel@0:

Better support for Lincoln Stein's Perl Module Daniel@0:
The new gdImage*Ptr function returns the chosen format stored in a block of memory. Daniel@0: This can be directly used by the GD perl module. Daniel@0:

Daniel@0:

Added functions Daniel@0:
gdImageCreateFromGd2Part - allows retrieval of part of an image (good for huge images, like maps), Daniel@0:
gdImagePaletteCopy - Copies a palette from one image to another, doing it's best to match the colors in the target image to the colors in the source palette. Daniel@0:
gdImageGd2, gdImageCreateFromGd2 - Support for new format Daniel@0:
gdImageCopyMerge - Merges two images (useful to highlight part of an image) Daniel@0:
gdImageCopyMergeGray - Similar to gdImageCopyMerge, but tries to preserve source image hue. Daniel@0:
gdImagePngPtr, gdImageJpegPtr, gdImageWBMPPtr, gdImageGdPtr, gdImageGd2Ptr - return memory blocks for each type of image. Daniel@0:
gdImageCreateFromPngCtx, gdImageCreateFromGdCtx, gdImageCreateFromGd2Ctx, gdImageCreateFromGd2PartCtx - Support for new I/O context. Daniel@0: Daniel@0:
Daniel@0: Daniel@0: NOTE: In fairness to Thomas Boutell, any bug/problems with any of the above features should Daniel@0: probably be reported to Philip Warner. Daniel@0: Daniel@0:

What's new in version 1.4?

Daniel@0: Daniel@0: Version 1.4 features the following changes: Daniel@0:
Daniel@0:
Fixed polygon fill routine (again) Daniel@0:
Thanks to Kirsten Schulz, version 1.4 is able to fill Daniel@0: numerous types of polygons that caused problems with Daniel@0: previous releases, including version 1.3. Daniel@0:
Support for alternate data sources Daniel@0:
Programmers who wish to load a GIF from something other Daniel@0: than a stdio FILE * stream can use the new Daniel@0: gdImageCreateFromPngSource function. Daniel@0:
Support for alternate data destinations Daniel@0:
Programmers who wish to write a GIF to something other Daniel@0: than a stdio FILE * stream can use the new Daniel@0: gdImagePngToSink function. Daniel@0:
More tolerant when reading GIFs Daniel@0:
Daniel@0: Version 1.4 does not crash when reading certain animated GIFs, Daniel@0: although it still only reads the first frame. Version 1.4 also has Daniel@0: overflow testing code to prevent crashes when reading Daniel@0: damaged GIFs. Daniel@0:
Daniel@0:

What's new in version 1.3?

Daniel@0: Version 1.3 features the following changes: Daniel@0:
Daniel@0:
Non-LZW-based GIF compression code Daniel@0:
Daniel@0: Version 1.3 contained GIF compression code that uses simple Run Length Daniel@0: Encoding instead of LZW compression, while still retaining compatibility Daniel@0: with normal LZW-based GIF decoders (your browser will still like your GIFs). Daniel@0: LZW compression is patented by Unisys. We are currently reevaluating Daniel@0: the approach taken by gd 1.3. The current release of gd does not support Daniel@0: this approach. We recommend that you use the current release, and generate Daniel@0: PNG images. Thanks to Daniel@0: Hutchison Avenue Software Corporation for contributing Daniel@0: the RLE GIF code. Daniel@0:
8-bit fonts, and 8-bit font support Daniel@0:
This improves support for European languages. Thanks are due Daniel@0: to Honza Pazdziora and also to Daniel@0: Jan Pazdziora . Also see the provided bdftogd Daniel@0: Perl script if you wish to convert fixed-width X11 fonts Daniel@0: to gd fonts. Daniel@0:
16-bit font support (no fonts provided) Daniel@0:
Although no such fonts are provided in the distribution, Daniel@0: fonts containing more than 256 characters should work if the Daniel@0: gdImageString16 and gdImageStringUp16 routines are used. Daniel@0:
Improvements to the "webpng" example/utility Daniel@0:
The "webpng" utility is now a slightly more useful application. Thanks to Daniel@0: Brian Dowling for this code. Daniel@0:
Corrections to the color resolution field of GIF output Daniel@0:
Thanks to Bruno Aureli. Daniel@0:
Fixed polygon fills Daniel@0:
A one-line patch for the infamous polygon fill bug, courtesy Daniel@0: of Jim Mason. I believe this fix is sufficient. However, if you Daniel@0: find a situation where polygon fills still fail to behave properly, Daniel@0: please send code that demonstrates the problem, and a fix if Daniel@0: you have one. Verifying the fix is important. Daniel@0:
Row-major, not column-major Daniel@0:
Internally, gd now represents the array of pixels as Daniel@0: an array of rows of pixels, rather than an array of columns Daniel@0: of pixels. This improves the performance of compression and Daniel@0: decompression routines slightly, because horizontally adjacent Daniel@0: pixels are now next to each other in memory. This should Daniel@0: not affect properly written gd applications, but applications that Daniel@0: directly manipulate the pixels array will require Daniel@0: changes. Daniel@0:
Daniel@0:

What else do I need to use gd?

Daniel@0:

Daniel@0: To use gd, you will need an ANSI C compiler. All popular Daniel@0: Windows 95 and NT C compilers are ANSI C compliant. Any Daniel@0: full-ANSI-standard C compiler should be adequate. The cc Daniel@0: compiler released with SunOS 4.1.3 is not an ANSI C compiler. Daniel@0: Most Unix users who do not already have gcc should get it. Daniel@0: gcc is free, ANSI compliant and a de facto industry standard. Daniel@0: Ask your ISP why it is missing. Daniel@0:

Daniel@0: As of version 1.6, you also need the zlib compression library, Daniel@0: and the libpng library. As of version 1.6.2, you can draw text Daniel@0: using antialiased TrueType fonts if you also have the libttf Daniel@0: library installed, but this is not mandatory. Daniel@0: zlib is available for a variety of platforms from Daniel@0: the zlib web site. Daniel@0: libpng is available for a variety of platforms from Daniel@0: the PNG web site. Daniel@0: Daniel@0:

Daniel@0: You will also want a PNG viewer, if you do not already have Daniel@0: one for your system, since you will need a good way to check the Daniel@0: results of your work. Netscape 4.04 and higher, and Microsoft Daniel@0: Internet Explorer 4.0 or higher, both support PNG. Daniel@0: Not every PNG-compatible viewer supports alpha channel Daniel@0: transparency, which is why gd 2.0.2 and above do alpha Daniel@0: blending in the library by default; it is possible to turn on the Daniel@0: saving of alpha channel information to the file instead. Daniel@0:

Daniel@0:

How do I get gd?

Daniel@0:

Binaries (DLL for Windows programmers):

Daniel@0: Daniel@0:

Source Code:

Daniel@0: Daniel@0:

Daniel@0:

How do I build gd?

Daniel@0:
Daniel@0: Win32 DLL users: if you are using MSVC, use the provided batch file Daniel@0: makemsvcimport.bat to make a bgd.lib import library Daniel@0: corresponding to the provided bgd.dll. Copy bgd.dll to your Daniel@0: application directory, or to your Windows sytem directory. In the Daniel@0: settings of your MSVC project, you MUST choose the Daniel@0: "multithreaded DLL" library option under "code generation." Daniel@0: mingw32 and cygwin users can simply link with the provided libbgd.a Daniel@0: stub library in order to use the DLL. Daniel@0:
Daniel@0: Building gd From the Source Daniel@0:

Daniel@0: In order to build gd, you must first unpack the archive you have Daniel@0: downloaded. If you are not familiar with tar and Daniel@0: gunzip (Unix) or ZIP (Windows), please Daniel@0: consult with an experienced user of your system. Sorry, we cannot Daniel@0: answer questions about basic Internet skills. Daniel@0:

Daniel@0: Unpacking the archive will produce a directory called "gd-2.0.33". Daniel@0:

Daniel@0:

For Unix

Daniel@0: cd to the 2.0.33 directory and type: Daniel@0:

Daniel@0: ./configure Daniel@0:

Daniel@0:

Daniel@0: NOTE: BY DEFAULT, THE LIBRARY IS INSTALLED IN Daniel@0: /usr/local/lib and the include files are Daniel@0: installed in /usr/local/include. IF YOU ARE Daniel@0: UPGRADING, you may wish to use: Daniel@0:
Daniel@0: ./configure --prefix=/usr
Daniel@0: 
Daniel@0: Rather than just ./configure, before typing Daniel@0: make and make install. Daniel@0:
Daniel@0:

Daniel@0: If all goes well, this will create a Makefile. If all does not go well -- Daniel@0: for instance, if neither the the JPEG nor the PNG and ZLIB libraries Daniel@0: are found -- you will need to install those libraries, then come back Daniel@0: and run configure again. Daniel@0:

Daniel@0: If necessary, make changes to the resulting Makefile. Then, Daniel@0: type "make". If there are no errors, follow this with "make install". Daniel@0: Because gd 2.0 and above installs as a shared library, it is necessary to Daniel@0: install the library properly before running gd-based programs. Daniel@0:

Daniel@0: If you get errors, type ./configure --help for more Daniel@0: information about the available options. In the unlikely event Daniel@0: that the GNU autoconf-produced configure script does not work well Daniel@0: for you, you may wish to try configure.pl, a Daniel@0: simple Perl script with similar but less complete capabilities. Daniel@0: If all else fails, try renaming makefile.sample Daniel@0: to Makefile. However, ./configure is Daniel@0: almost always your best bet. Daniel@0:

Daniel@0:

For Windows

Daniel@0: Use the DLL version! See the paragraph at the beginning of this sectino. Daniel@0: If you really want to compile it yourself for some strange reason, read on. Daniel@0:

Daniel@0: Create a project using your favorite programming environment. Daniel@0: Copy all of the gd files to the project directory. Add gd.c Daniel@0: to your project. Add other source files as appropriate. Learning the Daniel@0: basic skills of creating projects with your chosen C environment Daniel@0: is up to you. Alternatively, use the free mingw32 Daniel@0: or cygwin tools, which may prove to be compatible Daniel@0: with the provided configure script. Daniel@0:

Daniel@0:

Daniel@0: If you wish to test the library, type "make test" AFTER you have Daniel@0: successfully executed "make install". This will build Daniel@0: several test programs, including "gddemo". (Not all of these Daniel@0: programs are expected to print completely successful messages, Daniel@0: depending on the nature of the image formats with which some of Daniel@0: the tests are tried; for instance, WBMP is a black and white Daniel@0: format, so loss of color information is expected there.) Daniel@0: Run gddemo to see some of the capabilities of gd. Run Daniel@0: gdtestft to play with the freetype support, if you have built Daniel@0: gd with it and have access to truetype fonts. Daniel@0:

Daniel@0: gddemo should execute without incident, creating the file Daniel@0: demoout.png. (Note there is also a file named demoin.png, Daniel@0: which is provided in the package as part of the demonstration.) Daniel@0:

Daniel@0: Display demoout.png in your PNG viewer. The image should Daniel@0: be 128x128 pixels and should contain an image of the Daniel@0: space shuttle with quite a lot of graphical elements drawn Daniel@0: on top of it. Daniel@0:

Daniel@0: (If you are missing the demoin.png file, the other items Daniel@0: should appear anyway.) Daniel@0:

Daniel@0: Look at demoin.png to see the original space shuttle Daniel@0: image which was scaled and copied into the output image. Daniel@0:

Daniel@0:

gd basics: using gd in your program

Daniel@0: gd lets you create PNG or JPEG images on the fly. To use gd in your Daniel@0: program, include the file gd.h, and link with the gd Daniel@0: library and the other required libraries; the syntax for Daniel@0: most Unix flavors is: Daniel@0:
Daniel@0: -lgd -lpng -lz -ljpeg -lfreetype -lm
Daniel@0: 
Daniel@0: Assuming that all of these libraries are available. Daniel@0:

Daniel@0: If you want to use the provided simple fonts, include Daniel@0: gdfontt.h, gdfonts.h, gdfontmb.h, gdfontl.h and/or gdfontg.h. For Daniel@0: more impressive results, install FreeType 2.x and use the Daniel@0: gdImageStringFT Daniel@0: function. If you are not using the provided Makefile and/or a Daniel@0: library-based approach, be sure to include the source modules as well in your Daniel@0: project. (They may be too large for 16-bit memory models, Daniel@0: that is, 16-bit DOS and Windows.) Daniel@0:

Daniel@0: Here is a short example program. (For a more advanced example, Daniel@0: see gddemo.c, included in the distribution. gddemo.c is NOT the same program; Daniel@0: it demonstrates additional features!) Daniel@0:

Daniel@0:

Daniel@0: /* Bring in gd library functions */
Daniel@0: #include "gd.h"
Daniel@0: 
Daniel@0: /* Bring in standard I/O so we can output the PNG to a file */
Daniel@0: #include <stdio.h>
Daniel@0: 
Daniel@0: int main() {
Daniel@0:   /* Declare the image */
Daniel@0:   gdImagePtr im;
Daniel@0:   /* Declare output files */
Daniel@0:   FILE *pngout, *jpegout;
Daniel@0:   /* Declare color indexes */
Daniel@0:   int black;
Daniel@0:   int white;
Daniel@0: 
Daniel@0:   /* Allocate the image: 64 pixels across by 64 pixels tall */
Daniel@0:   im = gdImageCreate(64, 64);
Daniel@0: 
Daniel@0:   /* Allocate the color black (red, green and blue all minimum).
Daniel@0:     Since this is the first color in a new image, it will
Daniel@0:     be the background color. */
Daniel@0:   black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: 
Daniel@0:   /* Allocate the color white (red, green and blue all maximum). */
Daniel@0:   white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0:   
Daniel@0:   /* Draw a line from the upper left to the lower right,
Daniel@0:     using white color index. */
Daniel@0:   gdImageLine(im, 0, 0, 63, 63, white);  
Daniel@0: 
Daniel@0:   /* Open a file for writing. "wb" means "write binary", important
Daniel@0:     under MSDOS, harmless under Unix. */
Daniel@0:   pngout = fopen("test.png", "wb");
Daniel@0: 
Daniel@0:   /* Do the same for a JPEG-format file. */
Daniel@0:   jpegout = fopen("test.jpg", "wb");
Daniel@0: 
Daniel@0:   /* Output the image to the disk file in PNG format. */
Daniel@0:   gdImagePng(im, pngout);
Daniel@0: 
Daniel@0:   /* Output the same image in JPEG format, using the default
Daniel@0:     JPEG quality setting. */
Daniel@0:   gdImageJpeg(im, jpegout, -1);
Daniel@0: 
Daniel@0:   /* Close the files. */
Daniel@0:   fclose(pngout);
Daniel@0:   fclose(jpegout);
Daniel@0: 
Daniel@0:   /* Destroy the image in memory. */
Daniel@0:   gdImageDestroy(im);
Daniel@0: }
Daniel@0: 
Daniel@0: When executed, this program creates an image, allocates Daniel@0: two colors (the first color allocated becomes the background Daniel@0: color), draws a diagonal line (note that 0, 0 is the upper Daniel@0: left corner), writes the image to PNG and JPEG files, and Daniel@0: destroys the image. Daniel@0:

Daniel@0: The above example program should Daniel@0: give you an idea of how the package works. Daniel@0: gd provides many additional functions, which are listed Daniel@0: in the following reference chapters, complete with code Daniel@0: snippets demonstrating each. There is also an Daniel@0: alphabetical index. Daniel@0:

Webpng: a more powerful gd example

Daniel@0: Webpng is a simple utility program to manipulate PNGs from the Daniel@0: command line. It is written for Unix and similar command-line Daniel@0: systems, but should be easily adapted for other environments. Daniel@0: Webpng allows you to set transparency and interlacing and Daniel@0: output interesting information about the PNG in question. Daniel@0:

Daniel@0: webpng.c is provided in the distribution. Unix users can Daniel@0: simply type "make webpng" to compile the program. Type Daniel@0: "webpng" with no arguments to see the available options. Daniel@0:

Function and type reference

Daniel@0: Daniel@0:

Types

Daniel@0:
Daniel@0:
gdImage(TYPE) Daniel@0:
Daniel@0: The data structure in which gd stores images. Daniel@0: gdImageCreate, gdImageCreateTrueColor Daniel@0: and the various image file-loading functions return Daniel@0: a pointer to this type, and the other functions expect to receive Daniel@0: a pointer to this type as their first argument. It is reasonably safe to Daniel@0: examine any of the members of this structure. It is also reasonably Daniel@0: safe to modify individual pixels within the pixels Daniel@0: or tpixels arrays. If the trueColor flag Daniel@0: is set, the tpixels array is valid; otherwise the Daniel@0: pixels array is valid. Daniel@0:

Daniel@0: The colorsTotal, red, green, Daniel@0: blue, alpha and open arrays Daniel@0: manage the palette. They are valid only when the trueColor Daniel@0: flag is not set. Daniel@0: The transparent value contains the palette index of the first Daniel@0: transparent color as read-only information for backwards compatibility; Daniel@0: gd 2.0 stores this information in the alpha array so that Daniel@0: variable transparency can be supported for each palette entry. However, Daniel@0: for truecolor images, transparent represents a single Daniel@0: RGB color which is always 100% transparent, and this Daniel@0: feature is generally supported by browsers which do not support Daniel@0: full alpha channels. Daniel@0:

Daniel@0: typedef struct {
Daniel@0:   /* Palette-based image pixels */
Daniel@0:   unsigned char ** pixels;
Daniel@0:   int sx;
Daniel@0:   int sy;
Daniel@0:   /* These are valid in palette images only. See also
Daniel@0:   /* 'alpha', which appears later in the structure to
Daniel@0:     preserve binary backwards compatibility */
Daniel@0:   int colorsTotal;
Daniel@0:   int red[gdMaxColors];
Daniel@0:   int green[gdMaxColors];
Daniel@0:   int blue[gdMaxColors]; 
Daniel@0:   int open[gdMaxColors];
Daniel@0:   /* For backwards compatibility, this is set to the
Daniel@0:     first palette entry with 100% transparency,
Daniel@0:     and is also set and reset by the 
Daniel@0:     gdImageColorTransparent function. Newer
Daniel@0:     applications can allocate palette entries
Daniel@0:     with any desired level of transparency; however,
Daniel@0:     bear in mind that many viewers, notably
Daniel@0:     many web browsers, fail to implement
Daniel@0:     full alpha channel for PNG and provide
Daniel@0:     support for full opacity or transparency only. */
Daniel@0:   int transparent;
Daniel@0:   int *polyInts;
Daniel@0:   int polyAllocated;
Daniel@0:   struct gdImageStruct *brush;
Daniel@0:   struct gdImageStruct *tile;  
Daniel@0:   int brushColorMap[gdMaxColors];
Daniel@0:   int tileColorMap[gdMaxColors];
Daniel@0:   int styleLength;
Daniel@0:   int stylePos;
Daniel@0:   int *style;
Daniel@0:   int interlace;
Daniel@0:   /* New in 2.0: alpha channel for palettes. Note that only
Daniel@0:     Macintosh Internet Explorer and (possibly) Netscape 6
Daniel@0:     really support multiple levels of transparency in
Daniel@0:     palettes, to my knowledge, as of 2/15/01. Most
Daniel@0:     common browsers will display 100% opaque and
Daniel@0:     100% transparent correctly, and do something 
Daniel@0:     unpredictable and/or undesirable for levels
Daniel@0:     in between. TBB */
Daniel@0:   int alpha[gdMaxColors]; 
Daniel@0:   /* Truecolor flag and pixels. New 2.0 fields appear here at the
Daniel@0:     end to minimize breakage of existing object code. */
Daniel@0:   int trueColor;
Daniel@0:   int ** tpixels;
Daniel@0:   /* Should alpha channel be copied, or applied, each time a
Daniel@0:     pixel is drawn? This applies to truecolor images only.
Daniel@0:     No attempt is made to alpha-blend in palette images,
Daniel@0:     even if semitransparent palette entries exist. 
Daniel@0:     To do that, build your image as a truecolor image,
Daniel@0:     then quantize down to 8 bits. */
Daniel@0:   int alphaBlendingFlag;
Daniel@0:   /* Should the alpha channel of the image be saved? This affects
Daniel@0:     PNG at the moment; other future formats may also
Daniel@0:     have that capability. JPEG doesn't. */
Daniel@0:   int saveAlphaFlag;
Daniel@0: } gdImage;
Daniel@0: 
Daniel@0:

Daniel@0: The order of the structure members may appear confusing, but was chosen Daniel@0: deliberately to increase backwards compatibility with existing gd 1.x-based Daniel@0: binary code that references particular structure members. Daniel@0:

gdImagePtr (TYPE) Daniel@0:
Daniel@0: A pointer to an image structure. gdImageCreate Daniel@0: returns this type, and the other functions expect it as the first Daniel@0: argument. Daniel@0:
gdIOCtx (TYPE) Daniel@0:
Daniel@0: Most of the gd functions that read and write files, such as Daniel@0: gdImagePng and , Daniel@0: also have variants that accept a gdIOCtx structure; see Daniel@0: gdImagePngCtx and Daniel@0: gdImageCreateFromJpegCtx. Those who wish to provide Daniel@0: their own custom routines to read and write images can populate a Daniel@0: gdIOCtx structure with functions of their own devising to Daniel@0: to read and write data. For image reading, the only mandatory Daniel@0: functions are getC and getBuf, which must return the number of Daniel@0: characters actually read, or a negative value on error or EOF. Daniel@0: These functions must read the number of characters requested Daniel@0: unless at the end of the file. For image writing, the only mandatory Daniel@0: functions are putC and putBuf, which return the number of Daniel@0: characters written; these functions must write the number of Daniel@0: characters requested except in the event of an error. The seek Daniel@0: and tell functions are only required in conjunction with the Daniel@0: gd2 file format, which supports quick loading of Daniel@0: partial images. The gd_free function will not be invoked when Daniel@0: calling the standard Ctx functions; it is an implementation Daniel@0: convenience when adding new data types to gd. For examples, Daniel@0: see gd_png.c, gd_gd2.c, gd_jpeg.c, etc., all of which rely Daniel@0: on gdIOCtx to implement the standard image read and write functions. Daniel@0: Daniel@0:
Daniel@0: typedef struct gdIOCtx
Daniel@0: {
Daniel@0:   int (*getC) (struct gdIOCtx *);
Daniel@0:   int (*getBuf) (struct gdIOCtx *, void *, int wanted);
Daniel@0: 
Daniel@0:   void (*putC) (struct gdIOCtx *, int);
Daniel@0:   int (*putBuf) (struct gdIOCtx *, const void *, int wanted);
Daniel@0: 
Daniel@0:   /* seek must return 1 on SUCCESS, 0 on FAILURE. Unlike fseek! */
Daniel@0:   int (*seek) (struct gdIOCtx *, const int);
Daniel@0: 
Daniel@0:   long (*tell) (struct gdIOCtx *);
Daniel@0: 
Daniel@0:   void (*gd_free) (struct gdIOCtx *);
Daniel@0: 
Daniel@0: } gdIOCtx;
Daniel@0: 
Daniel@0: Daniel@0: Daniel@0:
gdFont (TYPE) Daniel@0:
Daniel@0: A font structure. Used to declare the characteristics of a font. Daniel@0: Please see the files gdfontl.c and gdfontl.h for an example of the Daniel@0: proper declaration of this structure. You can provide your Daniel@0: own font data by providing such a structure and the associated Daniel@0: pixel array. You can determine the width and height of a single Daniel@0: character in a font by examining the w and h members of the Daniel@0: structure. If you will not be creating your own fonts, you will Daniel@0: not need to concern yourself with the rest of the components of this Daniel@0: structure. Daniel@0:
Daniel@0: typedef struct {
Daniel@0:   /* # of characters in font */
Daniel@0:   int nchars;
Daniel@0:   /* First character is numbered... (usually 32 = space) */
Daniel@0:   int offset;
Daniel@0:   /* Character width and height */
Daniel@0:   int w;
Daniel@0:   int h;
Daniel@0:   /* Font data; array of characters, one row after another.
Daniel@0:     Easily included in code, also easily loaded from
Daniel@0:     data files. */
Daniel@0:   char *data;
Daniel@0: } gdFont;
Daniel@0: 
Daniel@0:
gdFontPtr (TYPE) Daniel@0:
Daniel@0: A pointer to a font structure. Text-output functions expect these Daniel@0: as their second argument, following the Daniel@0: gdImagePtr argument. Two such pointers are declared in the Daniel@0: provided include files gdfonts.h and gdfontl.h. Daniel@0:
gdPoint (TYPE) Daniel@0:
Daniel@0: Represents a point in the coordinate space of the image; used Daniel@0: by gdImagePolygon, Daniel@0: gdImageOpenPolygon and Daniel@0: gdImageFilledPolygon. Daniel@0:
Daniel@0: typedef struct {
Daniel@0:         int x, y;
Daniel@0: } gdPoint, *gdPointPtr;
Daniel@0: 
Daniel@0:
gdPointPtr (TYPE) Daniel@0:
Daniel@0: A pointer to a gdPoint structure; passed Daniel@0: as an argument to gdImagePolygon, Daniel@0: gdImageOpenPolygon Daniel@0: and gdImageFilledPolygon. Daniel@0:
Daniel@0:
gdFTStringExtra (TYPE) Daniel@0:
Daniel@0: A structure used to pass additional parameters to the Daniel@0: gdImageStringFTEx function. See Daniel@0: gdImageStringFTEx for the Daniel@0: structure definition. Daniel@0:
Daniel@0:
gdFTStringExtraPtr (TYPE) Daniel@0:
Daniel@0: A pointer to a structure used to pass additional parameters to the Daniel@0: gdImageStringFTEx function. See Daniel@0: gdImageStringFTEx for the Daniel@0: structure definition. Daniel@0:
Daniel@0:
gdSource (TYPE) Daniel@0:
Daniel@0:
Daniel@0: typedef struct {
Daniel@0:         int (*source) (void *context, char *buffer, int len);
Daniel@0:         void *context;
Daniel@0: } gdSource, *gdSourcePtr;
Daniel@0: 
Daniel@0: Represents a source from which a PNG can be read. Daniel@0: Programmers who do not wish to read PNGs from a file can provide Daniel@0: their own alternate input mechanism, using the Daniel@0: gdImageCreateFromPngSource function. Daniel@0: See the documentation of that function for an example of the Daniel@0: proper use of this type. Daniel@0:
gdSink (TYPE) Daniel@0:
Daniel@0:
Daniel@0: typedef struct {
Daniel@0:         int (*sink) (void *context, char *buffer, int len);
Daniel@0:         void *context;
Daniel@0: } gdSink, *gdSinkPtr;
Daniel@0: 
Daniel@0: Represents a "sink" (destination) to which a PNG can be written. Daniel@0: Programmers who do not wish to write PNGs to a file can provide Daniel@0: their own alternate output mechanism, using the Daniel@0: gdImagePngToSink function. Daniel@0: See the documentation of that function for an example of the Daniel@0: proper use of this type. Daniel@0:

Image creation, destruction, loading and saving

Daniel@0:
Daniel@0:
gdImageCreate(sx, sy) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCreate is called to create palette-based images, with no Daniel@0: more than 256 colors. Invoke gdImageCreate Daniel@0: with the x and y dimensions of the desired image. gdImageCreate Daniel@0: returns a gdImagePtr to the new image, or Daniel@0: NULL if unable to Daniel@0: allocate the image. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: im = gdImageCreate(64, 64);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
gdImageCreateTrueColor(sx, sy) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCreateTrueColor is called to create truecolor images, with Daniel@0: an essentially unlimited number of colors. Invoke gdImageCreateTrueColor Daniel@0: with the x and y dimensions of the desired image. gdImageCreateTrueColor Daniel@0: returns a gdImagePtr to the new image, or Daniel@0: NULL if unable to Daniel@0: allocate the image. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:

Daniel@0: Truecolor images are always filled with black at creation time. Daniel@0: There is no concept of a "background" color index. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: im = gdImageCreateTrueColor(64, 64);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
gdImageCreateFromJpeg(FILE *in) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCreateFromJpegPtr(int size, void *data) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCreateFromJpegCtx(gdIOCtx *in) Daniel@0: (FUNCTION) Daniel@0:

Daniel@0:

Daniel@0: gdImageCreateFromJpeg is called to load truecolor images from JPEG format files. Daniel@0: Invoke gdImageCreateFromJpeg with an already opened pointer to a file Daniel@0: containing the desired image. Daniel@0: gdImageCreateFromJpeg Daniel@0: returns a gdImagePtr to the new Daniel@0: truecolor image, or NULL Daniel@0: if unable to load the image (most often because the file is corrupt or Daniel@0: does not contain a JPEG image). gdImageCreateFromJpeg does not Daniel@0: close the file. You can inspect the sx and sy members of the Daniel@0: image to determine its size. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). The Daniel@0: returned image is always a truecolor image. Daniel@0:

Daniel@0: If you already have the Daniel@0: image file in memory, pass the size of the file and a pointer to the Daniel@0: file's data to gdImageCreateFromJpegPtr, which is otherwise identical Daniel@0: to gdImageCreateFromJpeg. Daniel@0:

Daniel@0:

Daniel@0: gdImagePtr im;
Daniel@0: ... inside a function ...
Daniel@0: FILE *in;
Daniel@0: in = fopen("myjpeg.jpg", "rb");
Daniel@0: im = gdImageCreateFromJpeg(in);
Daniel@0: fclose(in);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
gdImageCreateFromPng(FILE *in) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromPngPtr(int size, void *data) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromPngCtx(gdIOCtx *in) Daniel@0: (FUNCTION) Daniel@0:

Daniel@0:

Daniel@0: gdImageCreateFromPng is called to load images from PNG format files. Daniel@0: Invoke gdImageCreateFromPng with an already opened pointer to a file Daniel@0: containing the desired image. Daniel@0: gdImageCreateFromPng Daniel@0: returns a gdImagePtr to the new image, or NULL Daniel@0: if unable to load the image (most often because the file is corrupt or Daniel@0: does not contain a PNG image). gdImageCreateFromPng does not Daniel@0: close the file. You can inspect the sx and sy members of the Daniel@0: image to determine its size. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:

Daniel@0: If you already have the Daniel@0: image file in memory, pass the size of the file and a pointer to the Daniel@0: file's data to gdImageCreateFromPngPtr, which is otherwise identical Daniel@0: to gdImageCreateFromPng. Daniel@0:

Daniel@0: If the PNG image being loaded is a truecolor image, the resulting Daniel@0: gdImagePtr will refer to a truecolor image. If the PNG image Daniel@0: being loaded is a palette or grayscale image, the resulting Daniel@0: gdImagePtr will refer to a palette image. gd retains only 8 bits Daniel@0: of resolution for each of the red, green and blue channels, and Daniel@0: only 7 bits of resolution for the alpha channel. The former Daniel@0: restriction affects only a handful of very rare 48-bit color Daniel@0: and 16-bit grayscale PNG images. The second restriction affects Daniel@0: all semitransparent PNG images, but the difference is essentially Daniel@0: invisible to the eye. 7 bits of alpha channel resolution is, Daniel@0: in practice, quite a lot. Daniel@0:

Daniel@0: gdImagePtr im;
Daniel@0: ... inside a function ...
Daniel@0: FILE *in;
Daniel@0: in = fopen("mypng.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
gdImageCreateFromPngSource(gdSourcePtr in) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Deprecated in favor of Daniel@0: gdImageCreateFromPngCtx. Should Daniel@0: not be used in new applications. Daniel@0:

Daniel@0: gdImageCreateFromPngSource is called to load a PNG from Daniel@0: a data source other than a file. Usage is very similar to Daniel@0: the gdImageCreateFromPng function, Daniel@0: except that the programmer provides a custom data source. Daniel@0:

Daniel@0: The programmer must write an input function which accepts Daniel@0: a context pointer, a buffer, and a number of bytes to be Daniel@0: read as arguments. This function must read the number of Daniel@0: bytes requested, unless the end of the file has been reached, Daniel@0: in which case the function should return zero, or an error Daniel@0: has occurred, in which case the function should return Daniel@0: -1. The programmer then creates a Daniel@0: gdSource structure and sets Daniel@0: the source pointer to the input function and Daniel@0: the context pointer to any value which is useful to the Daniel@0: programmer. Daniel@0:

Daniel@0: The example below Daniel@0: implements gdImageCreateFromPng Daniel@0: by creating a custom data source and invoking gdImageCreateFromPngSource. Daniel@0:

Daniel@0: static int freadWrapper(void *context, char *buf, int len);
Daniel@0: 
Daniel@0: gdImagePtr gdImageCreateFromPng(FILE *in)
Daniel@0: {
Daniel@0:         gdSource s;
Daniel@0:         s.source = freadWrapper;
Daniel@0:         s.context = in;
Daniel@0:         return gdImageCreateFromPngSource(&s);
Daniel@0: }
Daniel@0: 
Daniel@0: static int freadWrapper(void *context, char *buf, int len)
Daniel@0: {
Daniel@0:         int got = fread(buf, 1, len, (FILE *) context);
Daniel@0:         return got;
Daniel@0: }
Daniel@0: 
Daniel@0:
gdImageCreateFromGif(FILE *in) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromGifPtr(int size, void *data) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromGifCtx(gdIOCtx *in) Daniel@0: (FUNCTION) Daniel@0:

Daniel@0:

Daniel@0: gdImageCreateFromGif is called to load images from GIF format files. Daniel@0: Invoke gdImageCreateFromGif with an already opened pointer to a file Daniel@0: containing the desired image. Daniel@0: gdImageCreateFromGif Daniel@0: returns a gdImagePtr to the new image, or NULL Daniel@0: if unable to load the image (most often because the file is corrupt or Daniel@0: does not contain a GIF image). gdImageCreateFromGif does not Daniel@0: close the file. You can inspect the sx and sy members of the Daniel@0: image to determine its size. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:

Daniel@0: If you already have the Daniel@0: image file in memory, pass the size of the file and a pointer to the Daniel@0: file's data to gdImageCreateFromGifPtr, which is otherwise identical Daniel@0: to gdImageCreateFromGif. Daniel@0:

Daniel@0: gdImagePtr im;
Daniel@0: ... inside a function ...
Daniel@0: FILE *in;
Daniel@0: in = fopen("mygif.gif", "rb");
Daniel@0: im = gdImageCreateFromGif(in);
Daniel@0: fclose(in);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
gdImageCreateFromGd(FILE *in) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromGdPtr(int size, void *data) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromGdCtx(gdIOCtx *in) Daniel@0: (FUNCTION) Daniel@0:

Daniel@0:

Daniel@0: gdImageCreateFromGd is called to load images from gd format files. Daniel@0: Invoke gdImageCreateFromGd Daniel@0: with an already opened pointer to a file containing the desired image Daniel@0: in the gd file format, which is specific to Daniel@0: gd and intended for very fast loading. (It is not intended for Daniel@0: compression; for compression, use PNG or JPEG.) Daniel@0:

Daniel@0: If you already have the Daniel@0: image file in memory, pass the size of the file and a pointer to the Daniel@0: file's data to gdImageCreateFromGdPtr, which is otherwise identical Daniel@0: to gdImageCreateFromGd. Daniel@0:

Daniel@0: gdImageCreateFromGd Daniel@0: returns a gdImagePtr to the new image, or NULL Daniel@0: if unable to load the image (most often because the file is corrupt or Daniel@0: does not contain a gd format image). gdImageCreateFromGd does not Daniel@0: close the file. You can inspect the sx and sy members of the Daniel@0: image to determine its size. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: FILE *in;
Daniel@0: in = fopen("mygd.gd", "rb");
Daniel@0: im = gdImageCreateFromGd(in);
Daniel@0: fclose(in);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0: Daniel@0:
gdImageCreateFromGd2(FILE *in) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromGd2Ptr(int size, void *data) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromGd2Ctx(gdIOCtx *in) Daniel@0: (FUNCTION) Daniel@0:

Daniel@0: Daniel@0:

Daniel@0: gdImageCreateFromGd2 is called to load images from gd2 format files. Daniel@0: Invoke gdImageCreateFromGd2 Daniel@0: with an already opened pointer to a file containing the desired image Daniel@0: in the gd2 file format, which is specific to Daniel@0: gd2 and intended for fast loading of parts of large images. Daniel@0: (It is a compressed format, but generally not as good as maximum Daniel@0: compression of the entire image would be.) Daniel@0:

Daniel@0: If you already have the Daniel@0: image file in memory, pass the size of the file and a pointer to the Daniel@0: file's data to gdImageCreateFromGd2Ptr, which is otherwise identical Daniel@0: to gdImageCreateFromGd2. Daniel@0:

Daniel@0: gdImageCreateFromGd2 Daniel@0: returns a gdImagePtr to the new image, or NULL Daniel@0: if unable to load the image (most often because the file is corrupt or Daniel@0: does not contain a gd format image). gdImageCreateFromGd2 does not Daniel@0: close the file. You can inspect the sx and sy members of the Daniel@0: image to determine its size. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: FILE *in;
Daniel@0: in = fopen("mygd.gd2", "rb");
Daniel@0: im = gdImageCreateFromGd2(in);
Daniel@0: fclose(in);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0: Daniel@0:
gdImageCreateFromGd2Part(FILE *in, int srcX, int srcY, int w, int h) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromGd2PartPtr(int size, void *data, int srcX, int srcY, int w, int h) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromGd2PartCtx(gdIOCtx *in) Daniel@0: (FUNCTION) Daniel@0:

Daniel@0: Daniel@0:

Daniel@0: gdImageCreateFromGd2Part is called to load parts of images from gd2 format files. Daniel@0: Invoked in the same way as gdImageCreateFromGd2, Daniel@0: but with extra parameters Daniel@0: indicating the source (x, y) and width/height of the desired image. Daniel@0: gdImageCreateFromGd2Part returns a gdImagePtr to the Daniel@0: new image, or NULL if unable to load the image. Daniel@0: The image must eventually be destroyed using gdImageDestroy(). Daniel@0:

Daniel@0: If you already have the image file in memory, you may use Daniel@0: gdImageCreateFromGd2PartPtr. Pass the size of the image file, Daniel@0: in bytes, as the first argument and the pointer to the image file data Daniel@0: as the second argument. Daniel@0:

Daniel@0:

gdImageCreateFromWBMP(FILE *in) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromWBMPPtr(int size, void *data) Daniel@0: (FUNCTION) Daniel@0:
gdImageCreateFromWBMPCtx(gdIOCtx *in) Daniel@0: (FUNCTION) Daniel@0:

Daniel@0:

Daniel@0: gdImageCreateFromWBMP is called to load images from WBMP format files. Daniel@0: Invoke gdImageCreateFromWBMP with an already opened pointer to a file Daniel@0: containing the desired image. Daniel@0: gdImageCreateFromWBMP Daniel@0: returns a gdImagePtr to the new image, or NULL Daniel@0: if unable to load the image (most often because the file is corrupt or Daniel@0: does not contain a PNG image). gdImageCreateFromWBMP does not Daniel@0: close the file. You can inspect the sx and sy members of the Daniel@0: image to determine its size. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:

Daniel@0: If you already have the Daniel@0: image file in memory, pass the size of the file and a pointer to the Daniel@0: file's data to gdImageCreateFromWBMPPtr, which is otherwise identical Daniel@0: to gdImageCreateFromWBMP. Daniel@0:

Daniel@0: gdImagePtr im;
Daniel@0: ... inside a function ...
Daniel@0: FILE *in;
Daniel@0: in = fopen("mywbmp.wbmp", "rb");
Daniel@0: im = gdImageCreateFromWBMP(in);
Daniel@0: fclose(in);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:

Daniel@0:

gdImageCreateFromXbm(FILE *in) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCreateFromXbm is called to load images from X bitmap format Daniel@0: files. Invoke gdImageCreateFromXbm Daniel@0: with an already opened pointer to a file containing the desired image. Daniel@0: gdImageCreateFromXbm Daniel@0: returns a gdImagePtr to the new image, or NULL Daniel@0: if unable to load the image (most often because the file is corrupt or Daniel@0: does not contain an X bitmap format image). gdImageCreateFromXbm does Daniel@0: not close the file. You can inspect the sx and sy members of the Daniel@0: image to determine its size. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: FILE *in;
Daniel@0: in = fopen("myxbm.xbm", "rb");
Daniel@0: im = gdImageCreateFromXbm(in);
Daniel@0: fclose(in);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
gdImageCreateFromXpm(char *filename) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCreateFromXbm is called to load images from XPM X Window System Daniel@0: color bitmap format files. This function is available only if HAVE_XPM Daniel@0: is selected in the Makefile and the Xpm library is linked with the Daniel@0: application. Unlike most gd file functions, the Xpm functions require Daniel@0: filenames, not file pointers. Daniel@0: gdImageCreateFromXpm Daniel@0: returns a gdImagePtr to the new image, or NULL Daniel@0: if unable to load the image (most often because the file is corrupt or Daniel@0: does not contain an XPM bitmap format image). You can inspect the sx and sy members of the Daniel@0: image to determine its size. The image must eventually be destroyed Daniel@0: using gdImageDestroy(). Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: FILE *in;
Daniel@0: in = fopen("myxpm.xpm", "rb");
Daniel@0: im = gdImageCreateFromXpm(in);
Daniel@0: fclose(in);
Daniel@0: /* ... Use the image ... */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
gdImageDestroy(gdImagePtr im) (FUNCTION) Daniel@0:
gdImageDestroy is used to free the memory associated with Daniel@0: an image. It is important to invoke gdImageDestroy before Daniel@0: exiting your program or assigning a new image to Daniel@0: a gdImagePtr variable. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: im = gdImageCreate(10, 10);
Daniel@0: /* ... Use the image ... */
Daniel@0: /* Now destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageJpeg(gdImagePtr im, FILE *out, int quality) Daniel@0: (FUNCTION)
Daniel@0: void gdImageJpegCtx(gdImagePtr im, gdIOCtx *out, int quality) Daniel@0: (FUNCTION)
Daniel@0:
Daniel@0: gdImageJpeg outputs the specified image to the specified Daniel@0: file in JPEG format. The file must be open for writing. Under MSDOS Daniel@0: and all versions of Windows, it is important to use "wb" as opposed Daniel@0: to simply "w" as the mode when opening the file, and under Unix there Daniel@0: is no penalty for doing so. gdImageJpeg does not Daniel@0: close the file; your code must do so. Daniel@0:

Daniel@0: If quality is negative, the default IJG JPEG quality value (which Daniel@0: should yield a good general quality / size tradeoff for most Daniel@0: situations) is used. Otherwise, for practical purposes, quality Daniel@0: should be a value in the range 0-95, higher quality values usually Daniel@0: implying both higher quality and larger image sizes. Daniel@0:

Daniel@0: If you have set image interlacing using Daniel@0: gdImageInterlace, this function will Daniel@0: interpret that to mean you wish to output a progressive JPEG. Some Daniel@0: programs (e.g., Web browsers) can display progressive JPEGs Daniel@0: incrementally; this can be useful when browsing over a relatively slow Daniel@0: communications link, for example. Progressive JPEGs can also be Daniel@0: slightly smaller than sequential (non-progressive) JPEGs. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black, white;
Daniel@0: FILE *out;
Daniel@0: /* Create the image */
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Allocate background */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);
Daniel@0: /* Allocate drawing color */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);
Daniel@0: /* Draw rectangle */
Daniel@0: gdImageRectangle(im, 0, 0, 99, 99, black);
Daniel@0: /* Open output file in binary mode */
Daniel@0: out = fopen("rect.jpg", "wb");
Daniel@0: /* Write JPEG using default quality */
Daniel@0: gdImageJpeg(im, out, -1);
Daniel@0: /* Close file */
Daniel@0: fclose(out);
Daniel@0: /* Destroy image */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void* gdImageJpegPtr(gdImagePtr im, int *size, int quality) Daniel@0: (FUNCTION) Daniel@0:
Identical to gdImageJpeg except that it returns a pointer to a memory Daniel@0: area with the JPEG data. This memory must be freed by the caller when it is Daniel@0: no longer needed. The caller must invoke gdFree(), not free(), Daniel@0: unless the caller is absolutely certain that the same implementations of Daniel@0: malloc, free, etc. are used both at library build time and at application Daniel@0: build time. The 'size' parameter receives the total size of the block Daniel@0: of memory. Daniel@0:
Daniel@0: void gdImageGif(gdImagePtr im, FILE *out) Daniel@0:
Daniel@0: Daniel@0: void gdImageGifCtx(gdImagePtr im, gdIOCtx *out) Daniel@0: Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageGif outputs the specified image to the specified Daniel@0: file in GIF format. The file must be open for writing. Under MSDOS Daniel@0: and all versions of Windows, it is important to use "wb" as opposed Daniel@0: to simply "w" as the mode when opening the file, and under Unix there Daniel@0: is no penalty for doing so. gdImageGif does not Daniel@0: close the file; your code must do so. Daniel@0:

Daniel@0: GIF does not support true color; GIF images can contain a maximum Daniel@0: of 256 colors. If the image to be written is a Daniel@0: truecolor image, such as those created with Daniel@0: gdImageCreateTrueColor or loaded Daniel@0: from a JPEG or a truecolor PNG image file, a palette-based Daniel@0: temporary image will automatically be created internally using the Daniel@0: gdImageCreatePaletteFromTrueColor function. The original image pixels are not modified. This conversion Daniel@0: produces high quality palettes but does require some CPU time. If you are Daniel@0: regularly converting truecolor to palette in this way, you should consider Daniel@0: creating your image as a palette-based image in the first place. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black, white;
Daniel@0: FILE *out;
Daniel@0: /* Create the image */
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Allocate background */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);
Daniel@0: /* Allocate drawing color */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);
Daniel@0: /* Draw rectangle */
Daniel@0: gdImageRectangle(im, 0, 0, 99, 99, black);
Daniel@0: /* Open output file in binary mode */
Daniel@0: out = fopen("rect.gif", "wb");
Daniel@0: /* Write GIF */
Daniel@0: gdImageGif(im, out);
Daniel@0: /* Close file */
Daniel@0: fclose(out);
Daniel@0: /* Destroy image */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void* gdImageGifPtr(gdImagePtr im, int *size) Daniel@0: (FUNCTION) Daniel@0:
Identical to gdImageGif except that it returns a pointer to a memory Daniel@0: area with the GIF data. This memory must be freed by the caller when it is Daniel@0: no longer needed. The caller must invoke gdFree(), not free(), Daniel@0: unless the caller is absolutely certain that the same implementations of Daniel@0: malloc, free, etc. are used both at library build time and at application Daniel@0: build time. The 'size' parameter receives the total size of the block Daniel@0: of memory. Daniel@0: Daniel@0:
Daniel@0: void gdImageGifAnimBegin(gdImagePtr im, FILE *out, int GlobalCM, int Loops) Daniel@0:
Daniel@0: Daniel@0: void gdImageGifAnimBeginCtx(gdImagePtr im, gdIOCtx *out, int GlobalCM, int Loops) Daniel@0: Daniel@0: (FUNCTION) Daniel@0: Daniel@0:
This function must be called as the first function when creating a Daniel@0: GIF animation. It writes the correct GIF file headers to selected Daniel@0: file output, and prepares for frames to be added for the animation. Daniel@0: The image argument is not used to produce an image frame to the file, Daniel@0: it is only used to establish the GIF animation frame size, interlacing Daniel@0: options and the color palette. gdImageGifAnimAdd is used to Daniel@0: add the first and subsequent frames to the animation, and the animation Daniel@0: must be terminated by writing a semicolon character (;) to it or by using Daniel@0: gdImageGifAnimEnd to do that. Daniel@0:

Daniel@0: Daniel@0: The GlobalCM flag indicates if a global color map (or palette) is used Daniel@0: in the GIF89A header. A nonzero value specifies that a global color Daniel@0: map should be used to reduce the size of the animation. Daniel@0: Of course, if the color maps of Daniel@0: individual frames differ greatly, a global color map may not be a good idea. Daniel@0: GlobalCM=1 means write global color map, GlobalCM=0 means do not, and Daniel@0: GlobalCM=-1 means to do the default, which currently is to use a global Daniel@0: color map. Daniel@0: Daniel@0:

Daniel@0: Daniel@0: If Loops is 0 or greater, the Netscape 2.0 extension for animation Daniel@0: loop count is written. 0 means infinite loop count. -1 means that Daniel@0: the extension is not added which results in no looping. -1 is the Daniel@0: default. Daniel@0: Daniel@0:

Daniel@0: void* gdImageGifAnimBeginPtr(gdImagePtr im, int *size, int GlobalCM, int Loops) Daniel@0: (FUNCTION) Daniel@0:
Identical to gdImageGifAnimBegin except that it returns a pointer Daniel@0: to a memory area with the GIF data. This memory must be freed by the Daniel@0: caller when it is no longer needed. The caller must invoke Daniel@0: gdFree(), not free(), unless the caller is absolutely certain that the Daniel@0: same implementations of malloc, free, etc. are used both at library Daniel@0: build time and at application build time. The 'size' Daniel@0: parameter receives the total size of the block of memory. Daniel@0: Daniel@0:
Daniel@0: void gdImageGifAnimAdd(gdImagePtr im, FILE *out, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) Daniel@0:
Daniel@0: Daniel@0: void gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtx *out, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) Daniel@0: Daniel@0: (FUNCTION) Daniel@0: Daniel@0:
This function writes GIF animation frames to GIF animation, which Daniel@0: was initialized with gdImageGifAnimBegin. With LeftOfs and Daniel@0: TopOfs you can place this frame in different offset than (0,0) inside Daniel@0: the image screen as defined in gdImageGifAnimBegin. Delay between the Daniel@0: previous frame and this frame is in 1/100s units. Disposal is usually Daniel@0: gdDisposalNone, meaning that the pixels changed by this Daniel@0: frame should remain on the display when the next frame begins to render, but Daniel@0: can also be gdDisposalUnknown (not recommended), Daniel@0: gdDisposalRestoreBackground (restores the first Daniel@0: allocated color of the global palette), or Daniel@0: gdDisposalRestorePrevious (restores the appearance of the Daniel@0: affected area before the frame was rendered). Only Daniel@0: gdDisposalNone is a sensible choice for the first frame. Daniel@0: If previm is Daniel@0: passed, the built-in GIF optimizer will always use gdDisposalNone Daniel@0: regardless of the Disposal parameter. Daniel@0:

Daniel@0: Setting the LocalCM flag to 1 adds a local palette for this image to the Daniel@0: animation. Otherwise the global palette is assumed and the user must make Daniel@0: sure the palettes match. Use gdImagePaletteCopy to do that. Daniel@0: Daniel@0:

Daniel@0: Daniel@0: Automatic optimization is activated by giving the previous image as a Daniel@0: parameter. This function then compares the images and only writes the changed Daniel@0: pixels to the new frame in animation. The Disposal parameter for Daniel@0: optimized animations must be set to 1, also for the first frame. Daniel@0: LeftOfs and TopOfs parameters are ignored for optimized frames. To Daniel@0: achieve good optimization, it is usually best to use a single global Daniel@0: color map. To allow gdImageGifAnimAdd to compress unchanged pixels via Daniel@0: the use of a transparent color, the image must include a transparent color. Daniel@0: Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im, im2, im3;
Daniel@0: int black, white, trans;
Daniel@0: FILE *out;
Daniel@0: /* Create the image */
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Allocate background */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);
Daniel@0: /* Allocate drawing color */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);
Daniel@0: /* Allocate transparent color for animation compression */
Daniel@0: trans = gdImageColorAllocate(im, 1, 1, 1);
Daniel@0: /* Draw rectangle */
Daniel@0: gdImageRectangle(im, 0, 0, 10, 10, black);
Daniel@0: /* Open output file in binary mode */
Daniel@0: out = fopen("anim.gif", "wb");
Daniel@0: /* Write GIF header.  Use global color map.  Loop a few times */
Daniel@0: gdImageGifAnimBegin(im, out, 1, 3);
Daniel@0: /* Write the first frame.  No local color map.  Delay = 1s */
Daniel@0: gdImageGifAnimAdd(im, out, 0, 0, 0, 100, 1, NULL);
Daniel@0: /* construct the second frame */
Daniel@0: im2 = gdImageCreate(100, 100);
Daniel@0: /* Allocate background to make it white */
Daniel@0: (void)gdImageColorAllocate(im2, 255, 255, 255);
Daniel@0: /* Make sure the palette is identical */
Daniel@0: gdImagePaletteCopy (im2, im);
Daniel@0: /* Draw something */
Daniel@0: gdImageRectangle(im2, 0, 0, 15, 15, black);
Daniel@0: /* Allow animation compression with transparent pixels */
Daniel@0: gdImageColorTransparent (im2, trans);
Daniel@0: /* Add the second frame */
Daniel@0: gdImageGifAnimAdd(im2, out, 0, 0, 0, 100, 1, im);
Daniel@0: /* construct the second frame */
Daniel@0: im3 = gdImageCreate(100, 100);
Daniel@0: /* Allocate background to make it white */
Daniel@0: (void)gdImageColorAllocate(im3, 255, 255, 255);
Daniel@0: /* Make sure the palette is identical */
Daniel@0: gdImagePaletteCopy (im3, im);
Daniel@0: /* Draw something */
Daniel@0: gdImageRectangle(im3, 0, 0, 15, 20, black);
Daniel@0: /* Allow animation compression with transparent pixels */
Daniel@0: gdImageColorTransparent (im3, trans);
Daniel@0: /* Add the third frame, compressing against the second one */
Daniel@0: gdImageGifAnimAdd(im3, out, 0, 0, 0, 100, 1, im2);
Daniel@0: /* Write the end marker */
Daniel@0: /* gdImageGifAnimEnd(out); is the same as the following: */
Daniel@0: putc (';', out);
Daniel@0: /* Close file */
Daniel@0: fclose(out);
Daniel@0: /* Destroy images */
Daniel@0: gdImageDestroy(im);
Daniel@0: gdImageDestroy(im2);
Daniel@0: gdImageDestroy(im3);
Daniel@0: 
Daniel@0: Daniel@0:
Daniel@0: void* gdImageGifAnimAddPtr(gdImagePtr im, int *size, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm) Daniel@0: (FUNCTION) Daniel@0:
Identical to gdImageGifAnimAdd except that it returns a pointer Daniel@0: to a memory area with the GIF data. This memory must be freed by the Daniel@0: caller when it is no longer needed. The caller must invoke Daniel@0: gdFree(), not free(), unless the caller is absolutely certain that the Daniel@0: same implementations of malloc, free, etc. are used both at library Daniel@0: build time and at application build time. The 'size' Daniel@0: parameter receives the total size of the block of memory. Daniel@0: Daniel@0:
Daniel@0: void gdImageGifAnimEnd(FILE *out) Daniel@0:
Daniel@0: Daniel@0: void gdImageGifAnimEndCtx(gdIOCtx *out) Daniel@0: Daniel@0: (FUNCTION) Daniel@0: Daniel@0:
Writes semicolon character (;) to the output file. This Daniel@0: terminates the GIF file properly. You can omit the call to Daniel@0: gdImageGifAnimEnd and just print out the semicolon. Daniel@0: Daniel@0:
Daniel@0: void* gdImageGifAnimEndPtr(int *size) Daniel@0: (FUNCTION) Daniel@0: Daniel@0:
Returns a one byte string containing the semicolon character (;). Daniel@0: Returns a pointer to a memory area with that string. This memory must Daniel@0: be freed by the caller when it is no longer needed. The caller Daniel@0: must invoke gdFree(), not free(), unless the caller is absolutely Daniel@0: certain that the same implementations of malloc, free, etc. are used Daniel@0: both at library build time and at application build time. The Daniel@0: 'size' parameter receives the total size of the block of memory. The Daniel@0: string ";" can be used in place of this function. Daniel@0: Daniel@0:
Daniel@0: void gdImagePng(gdImagePtr im, FILE *out) Daniel@0:
Daniel@0: Daniel@0: void gdImagePngCtx(gdImagePtr im, gdIOCtx *out) Daniel@0: Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImagePng outputs the specified image to the specified Daniel@0: file in PNG format. The file must be open for writing. Under MSDOS Daniel@0: and all versions of Windows, it is important to use "wb" as opposed Daniel@0: to simply "w" as the mode when opening the file, and under Unix there Daniel@0: is no penalty for doing so. gdImagePng does not Daniel@0: close the file; your code must do so. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black, white;
Daniel@0: FILE *out;
Daniel@0: /* Create the image */
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Allocate background */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);
Daniel@0: /* Allocate drawing color */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);
Daniel@0: /* Draw rectangle */
Daniel@0: gdImageRectangle(im, 0, 0, 99, 99, black);
Daniel@0: /* Open output file in binary mode */
Daniel@0: out = fopen("rect.png", "wb");
Daniel@0: /* Write PNG */
Daniel@0: gdImagePng(im, out);
Daniel@0: /* Close file */
Daniel@0: fclose(out);
Daniel@0: /* Destroy image */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void gdImagePngEx(gdImagePtr im, FILE *out, int level) Daniel@0:
Daniel@0: Daniel@0: void gdImagePngCtxEx(gdImagePtr im, gdIOCtx *out, int level) Daniel@0: Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Like gdImagePng, gdImagePngEx outputs the Daniel@0: specified image to the specified file in PNG format. In addition, Daniel@0: gdImagePngEx allows the level of compression to be specified. A compression Daniel@0: level of 0 means "no compression." A compression level of 1 means Daniel@0: "compressed, but as quickly as possible." A compression level of 9 Daniel@0: means "compressed as much as possible to produce the smallest possible Daniel@0: file." A compression level of -1 will use the default compression level Daniel@0: at the time zlib was compiled on your system. Daniel@0:

Daniel@0: For more information, see gdImagePng. Daniel@0:

Daniel@0: void* gdImagePngPtr(gdImagePtr im, int *size) Daniel@0: (FUNCTION) Daniel@0:
Identical to gdImagePng except that it returns a pointer to a memory Daniel@0: area with the PNG data. This memory must be freed by the caller when it is Daniel@0: no longer needed. The caller must invoke gdFree(), not free(), Daniel@0: unless the caller is absolutely certain that the same implementations of Daniel@0: malloc, free, etc. are used both at library build time and at application Daniel@0: build time. The 'size' parameter receives the total size of the block Daniel@0: of memory. Daniel@0:
Daniel@0: void* gdImagePngPtrEx(gdImagePtr im, int *size, int level) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Like gdImagePngPtr, gdImagePngPtrEx returns a Daniel@0: pointer to a PNG image in allocated memory. Daniel@0: In addition, gdImagePngPtrEx allows the level of compression to be Daniel@0: specified. A compression level of 0 means "no compression." A compression level of 1 means Daniel@0: "compressed, but as quickly as possible." A compression level of 9 Daniel@0: means "compressed as much as possible to produce the smallest possible Daniel@0: file." A compression level of -1 will use the default compression level Daniel@0: at the time zlib was compiled on your system. Daniel@0:

Daniel@0: For more information, see gdImagePngPtr. Daniel@0:

gdImagePngToSink(gdImagePtr im, gdSinkPtr out) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImagePngToSink is called to write a PNG to Daniel@0: a data "sink" (destination) other than a file. Usage is very similar to Daniel@0: the gdImagePng function, Daniel@0: except that the programmer provides a custom data sink. Daniel@0:

Daniel@0: The programmer must write an output function which accepts Daniel@0: a context pointer, a buffer, and a number of bytes to be Daniel@0: written as arguments. This function must write the number of Daniel@0: bytes requested and return that number, unless an error Daniel@0: has occurred, in which case the function should return Daniel@0: -1. The programmer then creates a Daniel@0: gdSink structure and sets Daniel@0: the sink pointer to the output function and Daniel@0: the context pointer to any value which is useful to the Daniel@0: programmer. Daniel@0:

Daniel@0: The example below Daniel@0: implements gdImagePng Daniel@0: by creating a custom data source and invoking gdImagePngFromSink. Daniel@0:

Daniel@0: static int stdioSink(void *context, char *buffer, int len)
Daniel@0: {
Daniel@0:   return fwrite(buffer, 1, len, (FILE *) context);
Daniel@0: }
Daniel@0: 
Daniel@0: void gdImagePng(gdImagePtr im, FILE *out)
Daniel@0: {
Daniel@0:   gdSink mySink;
Daniel@0:   mySink.context = (void *) out;
Daniel@0:   mySink.sink = stdioSink;
Daniel@0:   gdImagePngToSink(im, &mySink);
Daniel@0: }
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageWBMP(gdImagePtr im, int fg, FILE *out) Daniel@0:
gdImageWBMPCtx(gdIOCtx *out) Daniel@0: (FUNCTION)(FUNCTION) Daniel@0:
Daniel@0: gdImageWBMP outputs the specified image to the specified Daniel@0: file in WBMP format. The file must be open for writing. Under MSDOS Daniel@0: and all versions of Windows, it is important to use "wb" as opposed Daniel@0: to simply "w" as the mode when opening the file, and under Unix there Daniel@0: is no penalty for doing so. gdImageWBMP does not Daniel@0: close the file; your code must do so. Daniel@0:

Daniel@0: WBMP file support is black and white only. The color index Daniel@0: specified by the fg argument is the "foreground," and only pixels Daniel@0: of this color will be set in the WBMP file. All other pixels Daniel@0: will be considered "background." Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black, white;
Daniel@0: FILE *out;
Daniel@0: /* Create the image */
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Allocate background */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);
Daniel@0: /* Allocate drawing color */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);
Daniel@0: /* Draw rectangle */
Daniel@0: gdImageRectangle(im, 0, 0, 99, 99, black);
Daniel@0: /* Open output file in binary mode */
Daniel@0: out = fopen("rect.wbmp", "wb");
Daniel@0: /* Write WBMP, with black as foreground */
Daniel@0: gdImageWBMP(im, black, out);
Daniel@0: /* Close file */
Daniel@0: fclose(out);
Daniel@0: /* Destroy image */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void* gdImageWBMPPtr(gdImagePtr im, int *size) Daniel@0: (FUNCTION) Daniel@0:
Identical to gdImageWBMP except that it returns a pointer to a memory Daniel@0: area with the WBMP data. This memory must be freed by the caller when it is Daniel@0: no longer needed. The caller must invoke gdFree(), not free(), Daniel@0: unless the caller is absolutely certain that the same implementations of Daniel@0: malloc, free, etc. are used both at library build time and at application Daniel@0: build time. The 'size' parameter receives the total size of the block Daniel@0: of memory. Daniel@0:
Daniel@0: void gdImageGd(gdImagePtr im, FILE *out) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageGd outputs the specified image to the specified Daniel@0: file in the gd image format. The file must Daniel@0: be open for writing. Under MSDOS and all versions of Windows, it is Daniel@0: important to use "wb" as Daniel@0: opposed to simply "w" as the mode when opening the file, and under Daniel@0: Unix there is no penalty for doing so. gdImagePng does not Daniel@0: close the file; your code must do so. Daniel@0:

Daniel@0: The gd image format is intended for fast reads and writes of Daniel@0: images your program will need frequently to build other Daniel@0: images. It is not a compressed format, and is not intended Daniel@0: for general use. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black, white;
Daniel@0: FILE *out;
Daniel@0: /* Create the image */
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Allocate background */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);
Daniel@0: /* Allocate drawing color */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);
Daniel@0: /* Draw rectangle */
Daniel@0: gdImageRectangle(im, 0, 0, 99, 99, black);
Daniel@0: /* Open output file in binary mode */
Daniel@0: out = fopen("rect.gd", "wb");
Daniel@0: /* Write gd format file */
Daniel@0: gdImageGd(im, out);
Daniel@0: /* Close file */
Daniel@0: fclose(out);
Daniel@0: /* Destroy image */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0: Daniel@0:
Daniel@0: void* gdImageGdPtr(gdImagePtr im, int *size) Daniel@0: (FUNCTION) Daniel@0:
Identical to gdImageGd except that it returns a pointer to a memory Daniel@0: area with the GD data. This memory must be freed by the caller when it is Daniel@0: no longer needed. The caller must invoke gdFree(), not free(), Daniel@0: unless the caller is absolutely certain that the same implementations of Daniel@0: malloc, free, etc. are used both at library build time and at application Daniel@0: build time. The 'size' parameter receives the total size of the block Daniel@0: of memory. Daniel@0: Daniel@0:
Daniel@0: void gdImageGd2(gdImagePtr im, FILE *out, int chunkSize, int fmt) Daniel@0:
Daniel@0: Daniel@0: void gdImageGd2Ctx(gdImagePtr im, gdIOCtx *out, int chunkSize, int fmt) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageGd2 outputs the specified image to the specified Daniel@0: file in the gd2 image format. The file must Daniel@0: be open for writing. Under MSDOS and all versions of Windows, it is Daniel@0: important to use "wb" as Daniel@0: opposed to simply "w" as the mode when opening the file, and under Daniel@0: Unix there is no penalty for doing so. gdImageGd2 does not Daniel@0: close the file; your code must do so. Daniel@0:

Daniel@0: The gd2 image format is intended for fast reads and writes of Daniel@0: parts of images. Daniel@0: It is a compressed format, and well suited to retrieving smll sections of Daniel@0: much larger images. Daniel@0: Daniel@0: The third and fourth parameters are the 'chunk size' and format resposectively. Daniel@0:

Daniel@0: The file is stored as a series of compressed subimages, and the Daniel@0: Chunk Size determines the sub-image size - a value of Daniel@0: zero causes the GD library to use the default. Daniel@0:

Daniel@0: It is also possible to store GD2 files in an uncompressed format, in which case the Daniel@0: fourth parameter should be GD2_FMT_RAW. Daniel@0: Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black, white;
Daniel@0: FILE *out;
Daniel@0: /* Create the image */
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Allocate background */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);
Daniel@0: /* Allocate drawing color */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);
Daniel@0: /* Draw rectangle */
Daniel@0: gdImageRectangle(im, 0, 0, 99, 99, black);
Daniel@0: /* Open output file in binary mode */
Daniel@0: out = fopen("rect.gd", "wb");
Daniel@0: /* Write gd2 format file */
Daniel@0: gdImageGd2(im, out, 0, GD2_FMT_COMPRESSED);
Daniel@0: /* Close file */
Daniel@0: fclose(out);
Daniel@0: /* Destroy image */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0: Daniel@0:
Daniel@0: void* gdImageGd2Ptr(gdImagePtr im, int chunkSize, int fmt, int *size) Daniel@0: (FUNCTION) Daniel@0:
Identical to gdImageGd2 except that it returns a pointer to a memory Daniel@0: area with the GD2 data. This memory must be freed by the caller when it is Daniel@0: no longer needed. The caller must invoke gdFree(), not free(), Daniel@0: unless the caller is absolutely certain that the same implementations of Daniel@0: malloc, free, etc. are used both at library build time and at application Daniel@0: build time. The 'size' parameter receives the total size of the block Daniel@0: of memory. Daniel@0:
Daniel@0: void gdImageTrueColorToPalette(gdImagePtr im, int ditherFlag, int colorsWanted) Daniel@0:
Daniel@0: Daniel@0: gdImagePtr gdImageCreatePaletteFromTrueColor(gdImagePtr im, int ditherFlag, int colorsWanted) Daniel@0: Daniel@0: (FUNCTION) Daniel@0:
Daniel@0:
Daniel@0: gdImageCreatePaletteFromTrueColor returns a new Daniel@0: image. gdImageTrueColorToPalette permanently converts the Daniel@0: existing image. The two functions are otherwise identical. Daniel@0:
Daniel@0:

Daniel@0: The function converts a truecolor image to a palette-based image, Daniel@0: using a high-quality two-pass quantization routine. Daniel@0: If ditherFlag is set, the image will be Daniel@0: dithered to approximate colors better, at the expense Daniel@0: of some obvious "speckling." colorsWanted can be Daniel@0: anything up to 256. If the original source image Daniel@0: includes photographic information or anything that Daniel@0: came out of a JPEG, 256 is strongly recommended. Daniel@0: 100% transparency of a single transparent color in the Daniel@0: original truecolor image will be preserved. There is no other Daniel@0: support for preservation of alpha channel or transparency in Daniel@0: the destination image. Daniel@0:

Daniel@0: For best results, don't use this function -- write real Daniel@0: truecolor PNGs and JPEGs. The disk space gain of Daniel@0: conversion to palette is not great (for small images Daniel@0: it can be negative) and the quality loss is ugly. However, Daniel@0: the version of this function included in version 2.0.12 and later does Daniel@0: do a better job than the version included prior to 2.0.12. Daniel@0:

Daniel@0:

Drawing Functions

Daniel@0:
Daniel@0:
void gdImageSetPixel(gdImagePtr im, int x, int y, int color) (FUNCTION) Daniel@0:
gdImageSetPixel sets a pixel to a particular color index. Always use Daniel@0: this function or one of the other drawing functions to access pixels; Daniel@0: do not access the pixels of the gdImage structure Daniel@0: directly. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Set a pixel near the center. */
Daniel@0: gdImageSetPixel(im, 50, 50, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageLine is used to draw a line between two endpoints (x1,y1 and x2, y2). Daniel@0: The line is drawn using the color index specified. Note that the color Daniel@0: index can be an actual color returned by Daniel@0: gdImageColorAllocate or one of gdStyled, Daniel@0: gdBrushed or Daniel@0: gdStyledBrushed. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green 
Daniel@0:   and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a line from the upper left corner to the 
Daniel@0:   lower right corner. */
Daniel@0: gdImageLine(im, 0, 0, 99, 99, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageDashedLine is provided solely for backwards compatibility Daniel@0: with gd 1.0. New programs should draw dashed lines using Daniel@0: the normal gdImageLine function and the Daniel@0: new gdImageSetStyle function. Daniel@0:

Daniel@0: gdImageDashedLine is used to draw a dashed line between two endpoints Daniel@0: (x1,y1 and x2, y2). Daniel@0: The line is drawn using the color index specified. The portions of the line Daniel@0: that are not drawn are left transparent so the background is visible. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue 
Daniel@0:   all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a dashed line from the upper left corner 
Daniel@0:   to the lower right corner. */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImagePolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImagePolygon is used to draw a polygon with the verticies Daniel@0: (at least 3) specified, using the color index specified. Daniel@0: See also gdImageFilledPolygon. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: /* Points of polygon */
Daniel@0: gdPoint points[3];
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and 
Daniel@0:   blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a triangle. */
Daniel@0: points[0].x = 50;
Daniel@0: points[0].y = 0;
Daniel@0: points[1].x = 99;
Daniel@0: points[1].y = 99;
Daniel@0: points[2].x = 0;
Daniel@0: points[2].y = 99;
Daniel@0: gdImagePolygon(im, points, 3, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageOpenPolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageOpenPolygon is used to draw a sequence of lines with the verticies Daniel@0: (at least 3) specified, using the color index specified. Unlike Daniel@0: gdImagePolygon, the enpoints of the line Daniel@0: sequence are not connected to a closed polygon. Daniel@0:
void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageRectangle is used to draw a rectangle with the two corners Daniel@0: (upper left first, then lower right) specified, using the Daniel@0: color index specified. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a rectangle occupying the central area. */
Daniel@0: gdImageRectangle(im, 25, 25, 74, 74, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageFilledPolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageFilledPolygon is used to fill a polygon with the verticies Daniel@0: (at least 3) specified, using the color index specified. Daniel@0: See also gdImagePolygon. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: int red;
Daniel@0: /* Points of polygon */
Daniel@0: gdPoint points[3];
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Allocate the color red. */
Daniel@0: red = gdImageColorAllocate(im, 255, 0, 0);  
Daniel@0: /* Draw a triangle. */
Daniel@0: points[0].x = 50;
Daniel@0: points[0].y = 0;
Daniel@0: points[1].x = 99;
Daniel@0: points[1].y = 99;
Daniel@0: points[2].x = 0;
Daniel@0: points[2].y = 99;
Daniel@0: /* Paint it in white */
Daniel@0: gdImageFilledPolygon(im, points, 3, white);
Daniel@0: /* Outline it in red; must be done second */
Daniel@0: gdImagePolygon(im, points, 3, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageFilledRectangle is used to draw a solid rectangle with the two corners Daniel@0: (upper left first, then lower right) specified, using the Daniel@0: color index specified. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = int gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a filled rectangle occupying the central area. */
Daniel@0: gdImageFilledRectangle(im, 25, 25, 74, 74, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageArc is used to draw a partial ellipse centered at the given point, Daniel@0: with the specified width and height in pixels. The arc begins at Daniel@0: the position in degrees specified by s and ends at Daniel@0: the position specified by e. The arc is drawn in Daniel@0: the color specified by the last argument. A circle can be drawn Daniel@0: by beginning from 0 degrees and ending at 360 degrees, with Daniel@0: width and height being equal. e must be greater than s. Values greater Daniel@0: than 360 are interpreted modulo 360. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 50);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Inscribe an ellipse in the image. */
Daniel@0: gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageFilledArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color, int style) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageFilledArc is used to draw a partial ellipse centered at the given point, Daniel@0: with the specified width and height in pixels. The arc begins at Daniel@0: the position in degrees specified by s and ends at Daniel@0: the position specified by e. The arc is filled in Daniel@0: the color specified by the second to last argument. A circle can be drawn Daniel@0: by beginning from 0 degrees and ending at 360 degrees, with Daniel@0: width and height being equal. e must be greater than s. Values greater Daniel@0: than 360 are interpreted modulo 360. The last argument is a bitwise Daniel@0: OR of the following possibilities: Daniel@0:
    Daniel@0:
  • gdArc Daniel@0:
  • gdChord Daniel@0:
  • gdPie (synonym for gdChord) Daniel@0:
  • gdNoFill Daniel@0:
  • gdEdged Daniel@0:
Daniel@0: gdArc and gdChord are mutually exclusive; Daniel@0: gdChord just connects the starting and ending Daniel@0: angles with a straight line, while gdArc produces Daniel@0: a rounded edge. gdPie is a synonym for gdArc. Daniel@0: gdNoFill indicates that the arc or chord should be Daniel@0: outlined, not filled. gdEdged, used together with Daniel@0: gdNoFill, indicates that the beginning and ending Daniel@0: angles should be connected to the center; this is Daniel@0: a good way to outline (rather than fill) a Daniel@0: 'pie slice'. Daniel@0: Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 50);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Inscribe a filled pie slice in the image. */
Daniel@0: gdImageFilledArc(im, 50, 25, 98, 48, 0, 45, white, gdArc);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageFilledEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageFilledEllipse is used to draw an ellipse centered at the given point, Daniel@0: with the specified width and height in pixels. The ellipse is filled in Daniel@0: the color specified by the last argument. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 50);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Inscribe a filled ellipse in the image. */
Daniel@0: gdImageFilledEllipse(im, 50, 25, 98, 48, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageFillToBorder(gdImagePtr im, int x, int y, int border, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageFillToBorder floods a portion of the image with the specified Daniel@0: color, beginning at the specified point and stopping at Daniel@0: the specified border color. For a way of flooding an Daniel@0: area defined by the color of the starting point, see Daniel@0: gdImageFill. Daniel@0:

Daniel@0: The border color cannot be a special color Daniel@0: such as gdTiled; it must be a proper Daniel@0: solid color. The fill color can be, however. Daniel@0:

Daniel@0: Note that gdImageFillToBorder is recursive. It is not the most Daniel@0: naive implementation possible, and the implementation is Daniel@0: expected to improve, but there will always be degenerate Daniel@0: cases in which the stack can become very deep. This can be Daniel@0: a problem in MSDOS and MS Windows 3.1 environments. (Of course, Daniel@0: in a Unix or Windows 95/98/NT environment with a proper stack, this is Daniel@0: not a problem at all.) Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: int red;
Daniel@0: im = gdImageCreate(100, 50);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Allocate the color red. */
Daniel@0: red = gdImageColorAllocate(im, 255, 0, 0);  
Daniel@0: /* Inscribe an ellipse in the image. */
Daniel@0: gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
Daniel@0: /* Flood-fill the ellipse. Fill color is red, border color is
Daniel@0:   white (ellipse). */
Daniel@0: gdImageFillToBorder(im, 50, 50, white, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageFill(gdImagePtr im, int x, int y, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageFill floods a portion of the image with the specified Daniel@0: color, beginning at the specified point and flooding the Daniel@0: surrounding region of the same color as the starting point. Daniel@0: For a way of flooding a region defined by a specific border Daniel@0: color rather than by its interior color, see Daniel@0: gdImageFillToBorder. Daniel@0:

Daniel@0: The fill color can be gdTiled, resulting Daniel@0: in a tile fill using another image as the tile. However, Daniel@0: the tile image cannot be transparent. If the image you wish Daniel@0: to fill with has a transparent color index, call Daniel@0: gdImageTransparent on the Daniel@0: tile image and set the transparent color index to -1 Daniel@0: to turn off its transparency. Daniel@0:

Daniel@0: Note that gdImageFill is recursive. It is not the most Daniel@0: naive implementation possible, and the implementation is Daniel@0: expected to improve, but there will always be degenerate Daniel@0: cases in which the stack can become very deep. This can be Daniel@0: a problem in MSDOS and MS Windows environments. (Of course, Daniel@0: in a Unix or Windows 95/98/NT environment with a proper stack, this is Daniel@0: not a problem at all.) Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: int red;
Daniel@0: im = gdImageCreate(100, 50);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Allocate the color red. */
Daniel@0: red = gdImageColorAllocate(im, 255, 0, 0);  
Daniel@0: /* Inscribe an ellipse in the image. */
Daniel@0: gdImageArc(im, 50, 25, 98, 48, 0, 360, white);
Daniel@0: /* Flood-fill the ellipse. Fill color is red, and will replace the
Daniel@0:   black interior of the ellipse. */
Daniel@0: gdImageFill(im, 50, 50, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageSetAntiAliased(gdImagePtr im, int c) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: "Antialiasing" is a process by which jagged edges associated with line Daniel@0: drawing can be reduced by blending the foreground color with an appropriate Daniel@0: percentage of the background, depending on how much of the pixel in question Daniel@0: is actually within the boundaries of the line being drawn. Daniel@0: All line-drawing functions, Daniel@0: such as gdImageLine, Daniel@0: gdImageOpenPolygon and Daniel@0: gdImagePolygon, will draw antialiased lines Daniel@0: if the special "color" Daniel@0: gdAntiAliased is used when calling them. Daniel@0:

Daniel@0: gdImageSetAntiAliased is used to specify the actual foreground color Daniel@0: to be used when drawing antialiased lines. You may set any color to Daniel@0: be the foreground, however as of version 2.0.12 an alpha channel Daniel@0: component is not supported. Daniel@0:

Daniel@0: Antialiased lines can be drawn on both truecolor and palette-based Daniel@0: images. However, attempts to draw antialiased lines on Daniel@0: highly complex palette-based backgrounds may not give satisfactory Daniel@0: results, due to the limited number of colors available in the Daniel@0: palette. Antialiased line-drawing on simple backgrounds should Daniel@0: work well with palette-based images; otherwise create or fetch Daniel@0: a truecolor image instead. Daniel@0:

Daniel@0: You need not take any special action when you are finished Daniel@0: with antialised line drawing. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im, brush;
Daniel@0: int black;
Daniel@0: int blue;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: blue = gdImageColorAllocate(im, 0, 0, 255);
Daniel@0: gdImageSetAntiAliased(im, blue);
Daniel@0: /* Draw a smooth line from the upper left corner to the 
Daniel@0:   lower right corner. */
Daniel@0: gdImageLine(im, 0, 0, 99, 99, gdAntiAliased);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0: See also gdAntiAliased and Daniel@0: gdSetAntiAliasedDontBlend. Daniel@0:
void gdImageSetAntiAliasedDontBlend(gdImagePtr im, int c) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Normally, when drawing lines with the special Daniel@0: gdAntiAliased "color," blending with the Daniel@0: background to reduce jagged edges is the desired behavior. However, when Daniel@0: it is desired that lines not be blended with one particular color when Daniel@0: it is encountered in the background, the Daniel@0: gdImageSetAntiAliasedDontBlend function can be used to indicate the Daniel@0: special color that the foreground should stand out more clearly against. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im, brush;
Daniel@0: int black;
Daniel@0: int blue;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: blue = gdImageColorAllocate(im, 0, 0, 255);
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);
Daniel@0: 
Daniel@0: gdImageSetAntiAliased(im, blue);
Daniel@0: /* The portion of the line that crosses this white rectangle will
Daniel@0:   not be blended smoothly */
Daniel@0: gdImageSetAntiAliasedDontBlend(im, white);
Daniel@0: gdImageFilledRectangle(im, 25, 25, 75, 75, white);
Daniel@0: /* Draw a smooth line from the upper left corner 
Daniel@0:   to the lower right corner. */
Daniel@0: gdImageLine(im, 0, 0, 99, 99, gdAntiAliased);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0: See also gdAntiAliased and Daniel@0: gdSetAntiAliased. Daniel@0:
void gdImageSetBrush(gdImagePtr im, gdImagePtr brush) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: A "brush" is an image used to draw wide, shaped strokes in another image. Just Daniel@0: as a paintbrush is not a single point, a brush image need not be Daniel@0: a single pixel. Any gd image can be used as a brush, and by Daniel@0: setting the transparent color index of the brush image with Daniel@0: gdImageColorTransparent, Daniel@0: a brush of any shape can be created. All line-drawing functions, Daniel@0: such as gdImageLine, Daniel@0: gdImageOpenPolygon and Daniel@0: gdImagePolygon, will use the Daniel@0: current brush if the special "color" Daniel@0: gdBrushed or gdStyledBrushed Daniel@0: is used when calling them. Daniel@0:

Daniel@0: gdImageSetBrush is used to specify the brush to be used in a Daniel@0: particular image. You can set any image to be the brush. Daniel@0: If the brush image does not have the same color map as the Daniel@0: first image, any colors missing from the first image Daniel@0: will be allocated. If not enough colors can be allocated, Daniel@0: the closest colors already available will be used. This Daniel@0: allows arbitrary PNGs to be used as brush images. It also Daniel@0: means, however, that you should not set a brush unless you Daniel@0: will actually use it; if you set a rapid succession of Daniel@0: different brush images, you can quickly fill your color map, Daniel@0: and the results will not be optimal. Daniel@0:

Daniel@0: You need not take any special action when you are finished Daniel@0: with a brush. As for any other image, if you will not Daniel@0: be using the brush image for any further purpose, Daniel@0: you should call gdImageDestroy. Daniel@0: You must not use the color gdBrushed Daniel@0: if the current brush has been destroyed; you can of Daniel@0: course set a new brush to replace it. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im, brush;
Daniel@0: FILE *in;
Daniel@0: int black;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Open the brush PNG. For best results, portions of the
Daniel@0:   brush that should be transparent (ie, not part of the
Daniel@0:   brush shape) should have the transparent color index. */
Daniel@0: in = fopen("star.png", "rb");
Daniel@0: brush = gdImageCreateFromPng(in);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: gdImageSetBrush(im, brush);
Daniel@0: /* Draw a line from the upper left corner to the lower right corner
Daniel@0:   using the brush. */
Daniel@0: gdImageLine(im, 0, 0, 99, 99, gdBrushed);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: /* Destroy the brush image */
Daniel@0: gdImageDestroy(brush);
Daniel@0: 
Daniel@0:
void gdImageSetTile(gdImagePtr im, gdImagePtr tile) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: A "tile" is an image used to fill an area with a repeated pattern. Daniel@0: Any gd image can be used as a tile, and by Daniel@0: setting the transparent color index of the tile image with Daniel@0: gdImageColorTransparent, Daniel@0: a tile that allows certain parts of the underlying area to shine Daniel@0: through can be created. All region-filling functions, Daniel@0: such as gdImageFill and Daniel@0: gdImageFilledPolygon, will use the Daniel@0: current tile if the special "color" Daniel@0: gdTiled is used when calling them. Daniel@0:

Daniel@0: gdImageSetTile is used to specify the tile to be used in a Daniel@0: particular image. You can set any image to be the tile. Daniel@0: If the tile image does not have the same color map as the Daniel@0: first image, any colors missing from the first image Daniel@0: will be allocated. If not enough colors can be allocated, Daniel@0: the closest colors already available will be used. This Daniel@0: allows arbitrary PNGs to be used as tile images. It also Daniel@0: means, however, that you should not set a tile unless you Daniel@0: will actually use it; if you set a rapid succession of Daniel@0: different tile images, you can quickly fill your color map, Daniel@0: and the results will not be optimal. Daniel@0:

Daniel@0: You need not take any special action when you are finished Daniel@0: with a tile. As for any other image, if you will not Daniel@0: be using the tile image for any further purpose, Daniel@0: you should call gdImageDestroy. Daniel@0: You must not use the color gdTiled Daniel@0: if the current tile has been destroyed; you can of Daniel@0: course set a new tile to replace it. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im, tile;
Daniel@0: FILE *in;
Daniel@0: int black;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Open the tile PNG. For best results, portions of the
Daniel@0:   tile that should be transparent (ie, allowing the
Daniel@0:   background to shine through) should have the transparent
Daniel@0:   color index. */
Daniel@0: in = fopen("star.png", "rb");
Daniel@0: tile = gdImageCreateFromPng(in);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: gdImageSetTile(im, tile);
Daniel@0: /* Fill an area using the tile. */
Daniel@0: gdImageFilledRectangle(im, 25, 25, 75, 75, gdTiled);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: /* Destroy the tile image */
Daniel@0: gdImageDestroy(tile);
Daniel@0: 
Daniel@0:
void gdImageSetStyle(gdImagePtr im, int *style, int styleLength) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: It is often desirable to draw dashed lines, dotted lines, and other Daniel@0: variations on a broken line. gdImageSetStyle can be used to set Daniel@0: any desired series of colors, including a special color that Daniel@0: leaves the background intact, to be repeated during the drawing Daniel@0: of a line. Daniel@0:

Daniel@0: To use gdImageSetStyle, create an array of integers and assign Daniel@0: them the desired series of color values to be repeated. Daniel@0: You can assign the special color value Daniel@0: gdTransparent to indicate that the existing color should Daniel@0: be left unchanged for that particular pixel (allowing a dashed Daniel@0: line to be attractively drawn over an existing image). Daniel@0:

Daniel@0: Then, to draw a line using the style, use the normal Daniel@0: gdImageLine function with the Daniel@0: special color value gdStyled. Daniel@0:

Daniel@0: As of version 1.1.1, the style Daniel@0: array is copied when you set the style, so you need not Daniel@0: be concerned with keeping the array around indefinitely. Daniel@0: This should not break existing code that assumes styles Daniel@0: are not copied. Daniel@0:

Daniel@0: You can also combine styles and brushes to draw the brush Daniel@0: image at intervals instead of in a continuous stroke. Daniel@0: When creating a style for use with a brush, the Daniel@0: style values are interpreted differently: zero (0) indicates Daniel@0: pixels at which the brush should not be drawn, while one (1) Daniel@0: indicates pixels at which the brush should be drawn. Daniel@0: To draw a styled, brushed line, you must use the Daniel@0: special color value Daniel@0: gdStyledBrushed. For an example of this feature Daniel@0: in use, see gddemo.c (provided in the distribution). Daniel@0:

Daniel@0: gdImagePtr im;
Daniel@0: int styleDotted[2], styleDashed[6];
Daniel@0: FILE *in;
Daniel@0: int black;
Daniel@0: int red;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: red = gdImageColorAllocate(im, 255, 0, 0);  
Daniel@0: /* Set up dotted style. Leave every other pixel alone. */
Daniel@0: styleDotted[0] = red;
Daniel@0: styleDotted[1] = gdTransparent;
Daniel@0: /* Set up dashed style. Three on, three off. */
Daniel@0: styleDashed[0] = red;
Daniel@0: styleDashed[1] = red;
Daniel@0: styleDashed[2] = red;
Daniel@0: styleDashed[3] = gdTransparent;
Daniel@0: styleDashed[4] = gdTransparent;
Daniel@0: styleDashed[5] = gdTransparent;
Daniel@0: /* Set dotted style. Note that we have to specify how many pixels are
Daniel@0:   in the style! */
Daniel@0: gdImageSetStyle(im, styleDotted, 2);
Daniel@0: /* Draw a line from the upper left corner to the lower right corner. */
Daniel@0: gdImageLine(im, 0, 0, 99, 99, gdStyled);
Daniel@0: /* Now the dashed line. */
Daniel@0: gdImageSetStyle(im, styleDashed, 6);
Daniel@0: gdImageLine(im, 0, 99, 0, 99, gdStyled);
Daniel@0: 
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file ... */
Daniel@0: 
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageSetThickness(gdImagePtr im, int thickness) (FUNCTION) Daniel@0:
gdImageSetThickness determines the width of lines drawn by the Daniel@0: gdImageLine, gdImagePolygon, gdImageOpenPolygon Daniel@0: and related functions, in pixels. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Set thickness. */
Daniel@0: gdImageSetThickness(im, 4);
Daniel@0: /* Draw a fat line from the upper left corner to the lower right corner. */
Daniel@0: gdImageLine(im, 0, 0, 99, 99, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
void gdImageAlphaBlending(gdImagePtr im, int blending) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: The gdImageAlphaBlending Daniel@0: function allows for two different modes of drawing on truecolor Daniel@0: images. In blending mode, which is on by default (gd 2.0.2 Daniel@0: and above), the alpha channel component of the color Daniel@0: supplied to all drawing functions, such as Daniel@0: gdImageSetPixel, determines how much of Daniel@0: the underlying color should be allowed to shine through. As a result, Daniel@0: gd automatically blends the existing color at that point with the Daniel@0: drawing color, and stores the result in the image. The resulting pixel Daniel@0: is opaque. In non-blending mode, the drawing color is copied literally Daniel@0: with its alpha channel information, replacing the destination pixel. Daniel@0: Blending mode is not available when drawing on palette images. Daniel@0:
Daniel@0: gdImagePtr im;
Daniel@0: int red, blue;
Daniel@0: im = gdImageCreateTrueColor(100, 100);
Daniel@0: /* Background color */
Daniel@0: red = gdTrueColor(255, 0, 0);  
Daniel@0: gdImageFilledRectangle(im, 0, 0, 100, 100, red);
Daniel@0: /* Drawing color. Full transparency would be an alpha channel value
Daniel@0:   of 127 (gd has a 7 bit alpha chnanel). 0 is opaque,
Daniel@0:   127 is transparent. So cut gdAlphaTransparent in half to get
Daniel@0:   50% blending. */
Daniel@0: blue = gdTrueColorAlpha(0, 0, 255, gdAlphaTransparent / 2);  
Daniel@0: /* Draw with blending. Result will be 50% red, 50% blue: yellow 
Daniel@0:   (emitted light, remember, not reflected light. What you learned 
Daniel@0:   in Kindergarten is wrong here). */
Daniel@0: gdImageAlphaBlending(im, 1);
Daniel@0: gdImageFilledRectangle(im, 0, 0, 25, 25, blue);
Daniel@0: /* Draw without blending. Result will be 50% blue, 50%
Daniel@0:   the background color of the image viewer or web browser
Daniel@0:   used; results in browsers that don't support
Daniel@0:   semi-transparent pixels are unpredictable! */
Daniel@0: gdImageAlphaBlending(im, 0);
Daniel@0: gdImageFilledRectangle(im, 75, 75, 25, 25, blue);
Daniel@0: /* Write the image to disk, etc. */
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageSaveAlpha(gdImagePtr im, int saveFlag) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: By default, gd 2.0.2 and above do not attempt to save full alpha channel information Daniel@0: (as opposed to single-color transparency) when saving PNG images. (PNG Daniel@0: is currently the only output format supported by gd which can accommodate Daniel@0: alpa channel information.) This saves space in the output file. If you wish Daniel@0: to create an image with alpha channel information for use with tools that Daniel@0: support it, call gdImageSaveAlpha(im, 1) to turn on saving of such Daniel@0: information, and call gdImageAlphaBlending(im, 0) Daniel@0: to turn off alpha blending within the library so that alpha channel Daniel@0: information is actually stored in the image rather than being composited Daniel@0: immediately at the time that drawing functions are invoked. Daniel@0:
Daniel@0: void gdImageSetClip(gdImagePtr im, int x1, int y1, int x2, int y2) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Establishes a clipping rectangle. Once gdImageSetClip has been called, Daniel@0: all future drawing operations will remain within the specified clipping Daniel@0: area, until a new gdImageSetClip call takes place. For instance, Daniel@0: if a clipping rectangle of 25, 25, 75, 75 has been set within a Daniel@0: 100x100 image, a diagonal line from 0,0 to 99,99 will appear only Daniel@0: between 25,25 and 75,75. Daniel@0:

Daniel@0: If gdImageSetClip is never called, the clipping area will be the Daniel@0: entire image. Daniel@0:

Daniel@0: The parameters passed to gdImageSetClip are checked against the dimensions Daniel@0: of the image and limited to "safe" values. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Set the clipping rectangle. */
Daniel@0: gdImageSetClip(im, 25, 25, 75, 75);
Daniel@0: /* Draw a line from the upper left corner to the lower right corner. 
Daniel@0:   Only the part within the clipping rectangle will appear. */
Daniel@0: gdImageLine(im, 0, 0, 99, 99, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file ... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0: See also gdImageGetClip. Daniel@0:
Daniel@0: void gdImageGetClip(gdImagePtr im, int *x1P, int *y1P, int *x2P, int *y2P) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Fetches the boundaries of the current clipping rectangle. Daniel@0:
Daniel@0: ... Inside a function ... 
Daniel@0: gdImagePtr im = gdImageCreateTrueColor(100, 100);
Daniel@0: int x1, y1, x2, y2;
Daniel@0: gdImageSetClip(im, 25, 25, 75, 75);
Daniel@0: gdImageGetClip(im, &x1, &y1, &x2, &y2);
Daniel@0: printf("%d %d %d %d\n", x1, y1, x2, y2);
Daniel@0: 
Daniel@0: The above code would print: Daniel@0:
Daniel@0: 25 25 75 75
Daniel@0: 
Daniel@0: See also gdImageSetClip. Daniel@0:
Daniel@0:

Query Functions

Daniel@0:
Daniel@0:
Daniel@0: int gdImageAlpha(gdImagePtr im, int color) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageAlpha is a macro which returns the alpha channel component of Daniel@0: the specified color index. Alpha channel values vary between Daniel@0: 0 (gdAlphaOpaque), which does not blend at all with the background, Daniel@0: through 127 (gdAlphaTransparent), which allows the background to Daniel@0: shine through 100%. Use this macro rather than accessing the Daniel@0: structure members directly. Daniel@0: int gdImageBlue(gdImagePtr im, int color) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageBlue is a macro which returns the blue component of Daniel@0: the specified color index. Use this macro rather than accessing the Daniel@0: structure members directly. Daniel@0:
int gdImageGetPixel(gdImagePtr im, int x, int y) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageGetPixel() retrieves the color index of a particular Daniel@0: pixel. Always use this function to query pixels; Daniel@0: do not access the pixels of the gdImage structure Daniel@0: directly. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: FILE *in;
Daniel@0: gdImagePtr im;
Daniel@0: int c;
Daniel@0: in = fopen("mypng.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: c = gdImageGetPixel(im, gdImageSX(im) / 2, gdImageSY(im) / 2);
Daniel@0: printf("The value of the center pixel is %d; RGB values are %d,%d,%d\n",
Daniel@0:   c, im->red[c], im->green[c], im->blue[c]);
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageBoundsSafe(gdImagePtr im, int x, int y) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageBoundsSafe returns true (1) if the specified point is within the Daniel@0: current clipping rectangle, false (0) if not. The clipping rectangle is Daniel@0: set by gdImageSetClip and defaults Daniel@0: to the entire image. This function is intended primarily for Daniel@0: use by those who wish to add functions to gd. All of the gd drawing Daniel@0: functions already clip safely using this function or its macro Daniel@0: equivalent in gd.c, gdImageBoundsSafeMacro. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: if (gdImageBoundsSafe(im, 50, 50)) {
Daniel@0:   printf("50, 50 is within the image bounds\n");
Daniel@0: } else {
Daniel@0:   printf("50, 50 is outside the image bounds\n");
Daniel@0: }
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageGreen(gdImagePtr im, int color) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageGreen is a macro which returns the green component of Daniel@0: the specified color index. Use this macro rather than accessing the Daniel@0: structure members directly. Daniel@0:
Daniel@0: int gdImageRed(gdImagePtr im, int color) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageRed is a macro which returns the red component of Daniel@0: the specified color index. Use this macro rather than accessing the Daniel@0: structure members directly. Daniel@0:
Daniel@0: int gdImageSX(gdImagePtr im) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageSX is a macro which returns the width of the image Daniel@0: in pixels. Use this macro rather than accessing the Daniel@0: structure members directly. Daniel@0:
Daniel@0: int gdImageSY(gdImagePtr im) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageSY is a macro which returns the height of the image Daniel@0: in pixels. Use this macro rather than accessing the Daniel@0: structure members directly. Daniel@0:
Daniel@0:

Fonts and text-handling functions

Daniel@0:
Daniel@0:
Daniel@0: gdFontPtr gdFontGetSmall(void) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Returns a font pointer for the "small" gd font. Your code must Daniel@0: include the header file gdfonts.h before Daniel@0: calling this function. Under Windows, due to the nature of DLLs, Daniel@0: the use of this function is strongly recommended rather than attempting Daniel@0: to use the gdFontSmall pointer directly. (You may Daniel@0: safely assign the result to a local gdFontPtr variable Daniel@0: in your own code.) Daniel@0:

Daniel@0: See gdImageString for more information Daniel@0: and examples, or gdImageStringFT for a Daniel@0: freetype-based alternative that supports truetype fonts. Daniel@0:

Daniel@0: gdFontPtr gdFontGetLarge(void) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Returns a font pointer for the "large" gd font. Your code must Daniel@0: include the header file gdfontl.h before Daniel@0: calling this function. Under Windows, due to the nature of DLLs, Daniel@0: the use of this function is strongly recommended rather than attempting Daniel@0: to use the gdFontLarge pointer directly. (You may Daniel@0: safely assign the result to a local gdFontPtr variable Daniel@0: in your own code.) Daniel@0:

Daniel@0: See gdImageString for more information Daniel@0: and examples, or gdImageStringFT for a Daniel@0: freetype-based alternative that supports truetype fonts. Daniel@0:

Daniel@0: gdFontPtr gdFontGetMediumBold(void) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Returns a font pointer for the "medium bold" gd font. Your code must Daniel@0: include the header file gdfontmb.h before Daniel@0: calling this function. Under Windows, due to the nature of DLLs, Daniel@0: the use of this function is strongly recommended rather than attempting Daniel@0: to use the gdFontMediumBold pointer directly. (You may Daniel@0: safely assign the result to a local gdFontPtr variable Daniel@0: in your own code.) Daniel@0:

Daniel@0: See gdImageString for more information Daniel@0: and examples, or gdImageStringFT for a Daniel@0: freetype-based alternative that supports truetype fonts. Daniel@0:

Daniel@0: gdFontPtr gdFontGetGiant(void) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Returns a font pointer for the "giant" gd font. Your code must Daniel@0: include the header file gdfontg.h before Daniel@0: calling this function. Under Windows, due to the nature of DLLs, Daniel@0: the use of this function is strongly recommended rather than attempting Daniel@0: to use the gdFontGiant pointer directly. (You may Daniel@0: safely assign the result to a local gdFontPtr variable Daniel@0: in your own code.) Daniel@0:

Daniel@0: See gdImageString for more information Daniel@0: and examples, or gdImageStringFT for a Daniel@0: freetype-based alternative that supports truetype fonts. Daniel@0:

Daniel@0: gdFontPtr gdFontGetTiny(void) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Returns a font pointer for the "tiny" gd font. Your code must Daniel@0: include the header file gdfontt.h before Daniel@0: calling this function. Under Windows, due to the nature of DLLs, Daniel@0: the use of this function is strongly recommended rather than attempting Daniel@0: to use the gdFontTiny pointer directly. (You may Daniel@0: safely assign the result to a local gdFontPtr variable Daniel@0: in your own code.) Daniel@0:

Daniel@0: See gdImageString for more information Daniel@0: and examples, or gdImageStringFT for a Daniel@0: freetype-based alternative that supports truetype fonts. Daniel@0:

Daniel@0: void gdImageChar(gdImagePtr im, gdFontPtr font, int x, int y, Daniel@0: int c, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageChar is used to draw single characters on the image. Daniel@0: (To draw multiple characters, use Daniel@0: gdImageString or Daniel@0: gdImageString16. Daniel@0: See also gdImageStringFT for a high quality Daniel@0: solution.) Daniel@0: The second argument is a pointer to a font definition structure; five fonts are Daniel@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, Daniel@0: gdFontLarge, and gdFontGiant. Daniel@0:

Daniel@0: You must include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", Daniel@0: "gdfontl.h" and "gdfontg.h" respectively Daniel@0: and (if you are not using a library-based approach) link with the Daniel@0: corresponding .c files to use the provided fonts. Daniel@0:

Daniel@0:

Daniel@0: Windows DLL users: although you can use Daniel@0: these DLL-exported pointers directly, you cannot easily assign them to other Daniel@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you Daniel@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), Daniel@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to Daniel@0: obtain pointers to the fonts under Windows. Daniel@0:
Daniel@0:

Daniel@0: The character specified by the fifth Daniel@0: argument is drawn from left to right in the specified Daniel@0: color. (See gdImageCharUp for a way Daniel@0: of drawing vertical text.) Pixels not Daniel@0: set by a particular character retain their previous color. Daniel@0:

Daniel@0: #include "gd.h"
Daniel@0: #include "gdfontl.h"
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a character. */
Daniel@0: gdImageChar(im, gdFontGetLarge(), 0, 0, 'Q', white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageCharUp(gdImagePtr im, gdFontPtr font, int x, int y, Daniel@0: int c, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCharUp is used to draw single characters on the image, Daniel@0: rotated 90 degrees. Daniel@0: (To draw multiple characters, use Daniel@0: gdImageStringUp or Daniel@0: gdImageStringUp16.) The second argument is a Daniel@0: pointer to a font definition structure; five fonts are Daniel@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, Daniel@0: gdFontLarge, and gdFontGiant. You must Daniel@0: include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", Daniel@0: "gdfontl.h" and "gdfontg.h" respectively Daniel@0: and (if you are not using a library-based approach) link with the Daniel@0: corresponding .c files to use the provided fonts. Daniel@0:

Daniel@0:

Daniel@0: Windows DLL users: although you can use Daniel@0: these DLL-exported pointers directly, you cannot easily assign them to other Daniel@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you Daniel@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), Daniel@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to Daniel@0: obtain pointers to the fonts under Windows. Daniel@0:
Daniel@0:

Daniel@0: The character specified by Daniel@0: the fifth argument is drawn Daniel@0: from bottom to top, rotated at a 90-degree angle, in the specified Daniel@0: color. (See gdImageChar for a way Daniel@0: of drawing horizontal text.) Pixels not Daniel@0: set by a particular character retain their previous color. Daniel@0:

Daniel@0: #include "gd.h"
Daniel@0: #include "gdfontl.h"
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a character upwards so it rests against the top of the image. */
Daniel@0: gdImageCharUp(im, gdFontGetLarge(),
Daniel@0:   0, gdFontGetLarge()->h, 'Q', white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageString(gdImagePtr im, gdFontPtr font, int x, int y, Daniel@0: unsigned char *s, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageString is used to draw multiple characters on the image. Daniel@0: (To draw single characters, use Daniel@0: gdImageChar.) The second argument is a Daniel@0: pointer to a font definition structure; five fonts are Daniel@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, Daniel@0: gdFontLarge, and gdFontGiant. You must Daniel@0: include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", Daniel@0: "gdfontl.h" and "gdfontg.h" respectively Daniel@0: and (if you are not using a library-based approach) link with the Daniel@0: corresponding .c files to use the provided fonts. Daniel@0:

Daniel@0:

Daniel@0: Windows DLL users: although you can use Daniel@0: these DLL-exported pointers directly, you cannot easily assign them to other Daniel@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you Daniel@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), Daniel@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to Daniel@0: obtain pointers to the fonts under Windows. Daniel@0:
Daniel@0: The null-terminated C string specified Daniel@0: by the fifth argument is drawn from left to right in the specified Daniel@0: color. (See gdImageStringUp for a way Daniel@0: of drawing vertical text. Daniel@0: See also gdImageStringFT for a high Daniel@0: quality solution.) Daniel@0: Pixels not set by a particular character retain their previous color. Daniel@0:
Daniel@0: #include "gd.h"
Daniel@0: #include "gdfontl.h"
Daniel@0: #include <string.h>
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: /* String to draw. */
Daniel@0: char *s = "Hello.";
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a centered string. */
Daniel@0: gdImageString(im, gdFontGetLarge(),
Daniel@0:   im->sx / 2 - (strlen(s) * gdFontGetLarge()->w / 2),
Daniel@0:   im->sy / 2 - gdFontGetLarge()->h / 2,
Daniel@0:   s, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageString16(gdImagePtr im, gdFontPtr font, int x, int y, Daniel@0: unsigned short *s, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageString16 is used to draw multiple 16-bit characters on the image. Daniel@0: (To draw single characters, use Daniel@0: gdImageChar16.) The second argument is a Daniel@0: pointer to a font definition structure; no 16-bit fonts are Daniel@0: provided with gd as standard equipment and there does not seem to be Daniel@0: much momentum to create them although the bdftogd script can do so. The Daniel@0: preferred solution is gdImageStringFT, which Daniel@0: uses freetype to provide truetype font support. Daniel@0:

Daniel@0:

Daniel@0: Windows DLL users: although you can use Daniel@0: these DLL-exported pointers directly, you cannot easily assign them to other Daniel@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you Daniel@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), Daniel@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to Daniel@0: obtain pointers to the fonts under Windows. Daniel@0:
Daniel@0: The null-terminated string of characters represented as 16-bit unsigned Daniel@0: short integers specified by the fifth argument is drawn from left to right Daniel@0: in the specified Daniel@0: color. (See gdImageStringUp16 for a way Daniel@0: of drawing vertical text.) Pixels not Daniel@0: set by a particular character retain their previous color. Daniel@0:

Daniel@0: This function was added in gd1.3 to provide a means of rendering Daniel@0: fonts with more than 256 characters for those who have them. A Daniel@0: more frequently used routine is gdImageString. Daniel@0:

Daniel@0: void gdImageStringUp(gdImagePtr im, gdFontPtr font, int x, int y, Daniel@0: unsigned char *s, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageStringUp is used to draw multiple characters on the image, Daniel@0: rotated 90 degrees. Daniel@0: (To draw single characters, use Daniel@0: gdImageCharUp.) The second argument is a Daniel@0: pointer to a font definition structure; five fonts are Daniel@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, Daniel@0: gdFontLarge, and gdFontGiant. You must Daniel@0: include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", Daniel@0: "gdfontl.h" and "gdfontg.h" respectively Daniel@0: and (if you are not using a library-based approach) link with the Daniel@0: corresponding .c files to use the provided fonts. Daniel@0:
Daniel@0: Windows DLL users: although you can use Daniel@0: these DLL-exported pointers directly, you cannot easily assign them to other Daniel@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you Daniel@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), Daniel@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to Daniel@0: obtain pointers to the fonts under Windows. Daniel@0:
Daniel@0: Daniel@0: The null-terminated C string specified Daniel@0: by the fifth argument is drawn from bottom to top (rotated Daniel@0: 90 degrees) in the specified color. (See Daniel@0: gdImageString for a way Daniel@0: of drawing horizontal text.) Pixels not Daniel@0: set by a particular character retain their previous color. Daniel@0:
Daniel@0: #include "gd.h"
Daniel@0: #include "gdfontl.h"
Daniel@0: #include <string.h>
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: /* String to draw. */
Daniel@0: char *s = "Hello.";
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color white (red, green and blue all maximum). */
Daniel@0: white = gdImageColorAllocate(im, 255, 255, 255);  
Daniel@0: /* Draw a centered string going upwards. Axes are reversed,
Daniel@0:   and Y axis is decreasing as the string is drawn. */
Daniel@0: gdImageStringUp(im, gdFontGetLarge(),
Daniel@0:   im->w / 2 - gdFontGetLarge()->h / 2,
Daniel@0:   im->h / 2 + (strlen(s) * gdFontGetLarge()->w / 2),
Daniel@0:   s, white);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageStringUp16(gdImagePtr im, gdFontPtr font, int x, int y, Daniel@0: unsigned short *s, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageString is used to draw multiple 16-bit characters vertically on Daniel@0: the image. (To draw single characters, use Daniel@0: gdImageChar.) The second argument is a Daniel@0: pointer to a font definition structure; five fonts are Daniel@0: provided with gd, gdFontTiny, gdFontSmall, gdFontMediumBold, Daniel@0: gdFontLarge, and gdFontGiant. You must Daniel@0: include the files "gdfontt.h", "gdfonts.h", "gdfontmb.h", Daniel@0: "gdfontl.h" and "gdfontg.h" respectively Daniel@0: and (if you are not using a library-based approach) link with the Daniel@0: corresponding .c files to use the provided fonts. Daniel@0:
Daniel@0: Windows DLL users: although you can use Daniel@0: these DLL-exported pointers directly, you cannot easily assign them to other Daniel@0: pointers. This will cause hard-to-debug problems. To avoid such troubles, you Daniel@0: should call the functions gdFontGetTiny(), gdFontGetSmall(), Daniel@0: gdFontGetMediumBold(), gdFontGetLarge(), and gdFontGetGiant() in order to Daniel@0: obtain pointers to the fonts under Windows. Daniel@0:
Daniel@0: The null-terminated string of characters represented as 16-bit unsigned Daniel@0: short integers specified by the fifth argument is drawn from bottom to top Daniel@0: in the specified color. Daniel@0: (See gdImageStringUp16 for a way Daniel@0: of drawing horizontal text.) Pixels not Daniel@0: set by a particular character retain their previous color. Daniel@0:

Daniel@0: This function was added in gd1.3 to provide a means of rendering Daniel@0: fonts with more than 256 characters for those who have them. A Daniel@0: more frequently used routine is gdImageStringUp. Daniel@0:

int gdFTUseFontConfig(int flag) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: GD 2.0.29 introduced the ability to use Daniel@0: fontconfig patterns Daniel@0: rather than font file names as parameters to Daniel@0: gdImageStringFT, Daniel@0: gdImageStringFTEx and Daniel@0: gdImageStringFTCircle. Daniel@0: For backwards compatibility reasons, the fontlist parameter to those Daniel@0: functions is still expected to be a full or partial font file path name Daniel@0: or list thereof by default. However, as a convenience, a single call Daniel@0: to gdFTUseFontConfig with a nonzero parameter configures gd to expect Daniel@0: the fontlist parameter to be a fontconfig pattern. Regardless of whether Daniel@0: the flag argument is nonzero, this function returns true when the Daniel@0: fontconfig library is available and false when it is not. When fontconfig Daniel@0: is not available, the fontlist parameter always behaves as in previous Daniel@0: versions of GD. Daniel@0:
Daniel@0: #include "gd.h"
Daniel@0: #include <string.h>
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: int brect[8];
Daniel@0: int x, y;
Daniel@0: char *err;
Daniel@0: 
Daniel@0: char *s = "Hello."; /* String to draw. */
Daniel@0: double sz = 40.;
Daniel@0: char *fc = "times:bold:italic"; /* fontconfig pattern */
Daniel@0: 
Daniel@0: /* Signal that all freetype font calls in this program will receive
Daniel@0:   fontconfig patterns rather than filenames of font files */
Daniel@0: gdUseFontConfig(1);
Daniel@0: 
Daniel@0: /* obtain brect so that we can size the image */
Daniel@0: err = gdImageStringFT(NULL,&brect[0],0,fc,sz,0.,0,0,s);
Daniel@0: if (err) {fprintf(stderr,err); return 1;}
Daniel@0: 
Daniel@0: /* create an image big enough for the string plus a little whitespace */
Daniel@0: x = brect[2]-brect[6] + 6;
Daniel@0: y = brect[3]-brect[7] + 6;
Daniel@0: im = gdImageCreate(x,y);
Daniel@0: 
Daniel@0: /* Background color (first allocated) */
Daniel@0: white = gdImageColorResolve(im, 255, 255, 255);
Daniel@0: black = gdImageColorResolve(im, 0, 0, 0);
Daniel@0: 
Daniel@0: /* render the string, offset origin to center string*/
Daniel@0: /* note that we use top-left coordinate for adjustment
Daniel@0:  * since gd origin is in top-left with y increasing downwards. */
Daniel@0: x = 3 - brect[6];
Daniel@0: y = 3 - brect[7];
Daniel@0: err = gdImageStringFT(im,&brect[0],black,fc,sz,0.0,x,y,s);
Daniel@0: if (err) {fprintf(stderr,err); return 1;}
Daniel@0: 
Daniel@0:
Daniel@0: char *gdImageStringFT(gdImagePtr im, int *brect, Daniel@0: int fg, char *fontname, double ptsize, double angle, Daniel@0: int x, int y, char *string) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: RECOMMENDED. New in 1.8.4. gdImageStringFT draws text using the Daniel@0: FreeType 2.x library. Daniel@0:

Daniel@0: gdImageStringFT draws a string of anti-aliased characters on the image using Daniel@0: the FreeType Daniel@0: library to render user-supplied TrueType fonts. We do not provide Daniel@0: TrueType fonts (.ttf and .ttc files). Obtaining them is entirely up to Daniel@0: you. The string is anti-aliased, meaning that there should be Daniel@0: fewer "jaggies" visible. The fontname is the full pathname to a TrueType Daniel@0: font file, or a font face name if the GDFONTPATH environment variable Daniel@0: or the compiled-in DEFAULT_FONTPATH macro of gdft.c have been set intelligently. In the absence of a full path, the font face name may be presented with or without extension (2.0.26). Daniel@0:

Daniel@0: The null-terminated string argument is considered to be encoded via the UTF_8 Daniel@0: standard; also, HTML entities are supported, including decimal, Daniel@0: hexadecimal, and named entities (2.0.26). Those who are passing Daniel@0: ordinary ASCII strings may have difficulty with the & Daniel@0: character unless encoded correctly as & but should have no Daniel@0: other difficulties. Daniel@0:

Daniel@0: The string may be arbitrarily scaled (ptsize) and rotated (angle in radians). Daniel@0: The direction of rotation is counter-clockwise, with 0 radians (0 degrees) Daniel@0: at 3 o'clock and PI/2 radians (90 degrees) at 12 o'clock. Daniel@0: Daniel@0:

Daniel@0: The user-supplied int brect[8] array is filled on return from gdImageStringFT Daniel@0: with the 8 elements representing the 4 corner coordinates of the Daniel@0: bounding rectangle (the smallest rectangle that completely surrounds the Daniel@0: rendered string and does not intersect any pixel of the rendered string). Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0:
0 Daniel@0: lower left corner, X position
1 Daniel@0: lower left corner, Y position
2 Daniel@0: lower right corner, X position
3 Daniel@0: lower right corner, Y position
4 Daniel@0: upper right corner, X position
5 Daniel@0: upper right corner, Y position
6 Daniel@0: upper left corner, X position
7 Daniel@0: upper left corner, Y position
Daniel@0:

Daniel@0: The points are relative to the text regardless of the angle, so "upper left" Daniel@0: means in the top left-hand corner seeing the text horizontally. Daniel@0:

Daniel@0: Use a NULL gdImagePtr to get the bounding rectangle without rendering. Daniel@0: This is a relatively cheap operation if followed by a rendering of the same Daniel@0: string, because of the caching of the partial rendering during bounding Daniel@0: rectangle calculation. Daniel@0:

Daniel@0: The string is rendered in the color indicated by the gf color index. Daniel@0: Use the negative of the desired color index to Daniel@0: disable anti-aliasing. Daniel@0:

Daniel@0: The string may contain UTF-8 sequences like: "&#192;" Daniel@0:

Daniel@0: gdImageStringFT will return a null char* on success, or an error Daniel@0: string on failure. Daniel@0:

Daniel@0: #include "gd.h"
Daniel@0: #include <string.h>
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int white;
Daniel@0: int brect[8];
Daniel@0: int x, y;
Daniel@0: char *err;
Daniel@0: 
Daniel@0: char *s = "Hello."; /* String to draw. */
Daniel@0: double sz = 40.;
Daniel@0: char *f = "/usr/local/share/ttf/Times.ttf";  /* User supplied font */
Daniel@0: 
Daniel@0: /* obtain brect so that we can size the image */
Daniel@0: err = gdImageStringFT(NULL,&brect[0],0,f,sz,0.,0,0,s);
Daniel@0: if (err) {fprintf(stderr,err); return 1;}
Daniel@0: 
Daniel@0: /* create an image big enough for the string plus a little whitespace */
Daniel@0: x = brect[2]-brect[6] + 6;
Daniel@0: y = brect[3]-brect[7] + 6;
Daniel@0: im = gdImageCreate(x,y);
Daniel@0: 
Daniel@0: /* Background color (first allocated) */
Daniel@0: white = gdImageColorResolve(im, 255, 255, 255);
Daniel@0: black = gdImageColorResolve(im, 0, 0, 0);
Daniel@0: 
Daniel@0: /* render the string, offset origin to center string*/
Daniel@0: /* note that we use top-left coordinate for adjustment
Daniel@0:  * since gd origin is in top-left with y increasing downwards. */
Daniel@0: x = 3 - brect[6];
Daniel@0: y = 3 - brect[7];
Daniel@0: err = gdImageStringFT(im,&brect[0],black,f,sz,0.0,x,y,s);
Daniel@0: if (err) {fprintf(stderr,err); return 1;}
Daniel@0: 
Daniel@0: /* Write img to stdout */
Daniel@0: gdImagePng(im, stdout);
Daniel@0: 
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0: See also gdImageStringFTEx. Daniel@0:
Daniel@0: char *gdImageStringFTEx(gdImagePtr im, int *brect, Daniel@0: int fg, char *fontname, double ptsize, double angle, Daniel@0: int x, int y, char *string, gdFTStringExtraPtr strex) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: New in 2.0.5, also found in common third-party versions Daniel@0: of gd. gdImageStringFTEx extends the capabilities of Daniel@0: gdImageStringFT by providing a Daniel@0: way to pass additional parameters. Daniel@0:

Daniel@0: If the strex parameter is not null, it must point to a Daniel@0: gdFTStringExtra structure. As of gd 2.0.5, this structure Daniel@0: is defined as follows: Daniel@0:

Daniel@0: typedef struct {
Daniel@0:        /* logical OR of gdFTEX_ values */
Daniel@0:        int flags; 
Daniel@0:        /* fine tune line spacing for '\n' */
Daniel@0:        double linespacing; 
Daniel@0:        /* Preferred character mapping */
Daniel@0:        int charmap;
Daniel@0:        /* Rendering resolution */
Daniel@0:        int hdpi;
Daniel@0:        int vdpi;
Daniel@0:        char *xshow;
Daniel@0:        char *fontpath;
Daniel@0: } gdFTStringExtra, *gdFTStringExtraPtr;
Daniel@0: 
Daniel@0: To output multiline text with a specific line spacing, Daniel@0: include gdFTEX_LINESPACE in the setting of Daniel@0: flags: Daniel@0:
Daniel@0: flags |= gdFTEX_LINESPACE;
Daniel@0: 
Daniel@0: And also set linespacing to the desired spacing, expressed as a Daniel@0: multiple of the font height. Thus a line spacing of 1.0 is the Daniel@0: minimum to guarantee that lines of text do not collide. Daniel@0:

Daniel@0: If gdFTEX_LINESPACE is not present, or Daniel@0: strex is null, or gdImageStringFT Daniel@0: is called, linespacing defaults to 1.05. Daniel@0:

Daniel@0: To specify a preference for Unicode, Shift_JIS Big5 character encoding, Daniel@0: set or To output multiline text with a specific line spacing, Daniel@0: include gdFTEX_CHARMAP in the setting of Daniel@0: flags: Daniel@0:

Daniel@0: flags |= gdFTEX_CHARMAP;
Daniel@0: 
Daniel@0: And set charmap to the desired value, which can be Daniel@0: any of gdFTEX_Unicode, gdFTEX_Shift_JIS, gdFTEX_Big5, or gdFTEX_Adobe_Custom. Daniel@0: If you do not specify a preference, Unicode will be tried first. If the preferred Daniel@0: character mapping is not found in the font, other character mappings Daniel@0: are attempted. Daniel@0:

Daniel@0: GD operates on the assumption that the output image will be Daniel@0: rendered to a computer screen. By default, gd passes a Daniel@0: resolution of 96 dpi to the freetype text rendering engine. Daniel@0: This influences the "hinting" decisions made by the renderer. To Daniel@0: specify a different resolution, set hdpi and vdpi accordingly Daniel@0: (in dots per inch) and add gdFTEX_RESOLUTION to flags: Daniel@0:

Daniel@0: flags | gdFTEX_RESOLUTION;
Daniel@0: 
Daniel@0: GD 2.0.29 and later will normally attempt to apply kerning tables, if Daniel@0: fontconfig is available, to adjust the relative positions of consecutive Daniel@0: characters more ideally for that pair of characters. This can be turn off by Daniel@0: specifying the gdFTEX_DISABLE_KERNING flag: Daniel@0:
Daniel@0: flags | gdFTEX_DISABLE_KERNING;
Daniel@0: 
Daniel@0: GD 2.0.29 and later can return a vector of individual character Daniel@0: position advances, occasionally useful in applications that must know Daniel@0: exactly where each character begins. This is returned in the xshow Daniel@0: element of the gdFTStringExtra structure if the gdFTEX_XSHOW Daniel@0: flag is set: Daniel@0:
Daniel@0: flags | gdFTEX_XSHOW;
Daniel@0: 
Daniel@0: The caller is responsible for calling gdFree() on the xshow Daniel@0: element after the call if gdFTEX_XSHOW is set. Daniel@0:

Daniel@0: GD 2.0.29 and later can also return the path to the actual font file Daniel@0: used if the gdFTEX_RETURNFONTPATHNAME flag is set. This is useful because Daniel@0: GD 2.0.29 and above are capable of Daniel@0: selecting a font automatically based on a fontconfig font pattern Daniel@0: when fontconfig is available. This information is returned in the Daniel@0: fontpath element of the gdFTStringExtra structure. Daniel@0:

Daniel@0: flags | gdFTEX_RETURNFONTPATHNAME;
Daniel@0: 
Daniel@0: The caller is responsible for calling gdFree() on the fontpath Daniel@0: element after the call if gdFTEX_RETURNFONTPATHNAME is set. Daniel@0:

Daniel@0: GD 2.0.29 and later can use fontconfig to resolve Daniel@0: font names, including fontconfig patterns, if the gdFTEX_FONTCONFIG Daniel@0: flag is set. As a convenience, this behavior can be made the default Daniel@0: by calling gdFTUseFontConfig with Daniel@0: a nonzero value. In that situation it is not necessary to set the Daniel@0: gdFTEX_FONTCONFIG flag on every call; however explicit font path names Daniel@0: can still be used if the gdFTEX_FONTPATHNAME flag is set: Daniel@0:

Daniel@0: flags | gdFTEX_FONTPATHNAME;
Daniel@0: 
Daniel@0:

Daniel@0: Unless gdFTUseFontConfig has been Daniel@0: called with a nonzero value, GD 2.0.29 and later will still expect Daniel@0: the fontlist argument to the freetype text output functions to be Daniel@0: a font file name or list thereof as in previous versions. If you do Daniel@0: not wish to make fontconfig the default, it is Daniel@0: still possible to force the use of fontconfig for a single call to Daniel@0: the freetype text output functions by setting the gdFTEX_FONTCONFIG Daniel@0: flag: Daniel@0:

Daniel@0: flags | gdFTEX_FONTCONFIG;
Daniel@0: 
Daniel@0: GD 2.0.29 and above can use fontconfig to resolve Daniel@0: font names, including fontconfig patterns, if the gdFTEX_FONTCONFIG Daniel@0: flag is set. As a convenience, this behavior can be made the default Daniel@0: by calling gdFTUseFontConfig with Daniel@0: a nonzero value. In that situation it is not necessary to set the Daniel@0: gdFTEX_FONTCONFIG flag on every call; however explicit font path names Daniel@0: can still be used if the gdFTEX_FONTPATHNAME flag is set: Daniel@0:
Daniel@0: flags | gdFTEX_FONTPATHNAME;
Daniel@0: 
Daniel@0: For more information, see gdImageStringFT. Daniel@0:
Daniel@0: char *gdImageStringFTCircle(gdImagePtr im, Daniel@0: int cx, Daniel@0: int cy, Daniel@0: double radius, Daniel@0: double textRadius, Daniel@0: double fillPortion, Daniel@0: char *font, Daniel@0: double points, Daniel@0: char *top, Daniel@0: char *bottom, Daniel@0: int fgcolor) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Draws the text strings specified by top and bottom Daniel@0: on im, curved along the edge of a circle of radius Daniel@0: radius, with its center at cx and cy. Daniel@0: top is written clockwise Daniel@0: along the top; bottom is written counterclockwise Daniel@0: along the bottom. textRadius determines the "height" Daniel@0: of each character; if textRadius is 1/2 of Daniel@0: radius, Daniel@0: characters extend halfway from the edge to the center. Daniel@0: fillPortion varies from 0 to 1.0, with useful values Daniel@0: from about 0.4 to 0.9, and determines how much of the Daniel@0: 180 degrees of arc assigned to each section of text Daniel@0: is actually occupied by text; 0.9 looks better than Daniel@0: 1.0 which is rather crowded. font is a freetype Daniel@0: font; see gdImageStringFT. points is passed to the Daniel@0: freetype engine and has an effect on hinting; although Daniel@0: the size of the text is determined by radius, Daniel@0: textRadius, and fillPortion, you should Daniel@0: pass a point size that Daniel@0: "hints" appropriately -- if you know the text will be Daniel@0: large, pass a large point size such as 24.0 to get the Daniel@0: best results. fgcolor can be any color, and may have Daniel@0: an alpha component, do blending, etc. Daniel@0:

Daniel@0: Returns 0 on success, or an error string otherwise. Daniel@0:

Daniel@0: #include <stdio.h>
Daniel@0: #include <gd.h>
Daniel@0: 
Daniel@0: int main (int argc, char *argv[])
Daniel@0: {
Daniel@0:         FILE *in;
Daniel@0:         FILE *out;
Daniel@0:         gdImagePtr im;
Daniel@0:         int radius;
Daniel@0:         /* Create an image of text on a circle, with an
Daniel@0:                 alpha channel so that we can copy it onto a
Daniel@0:                 background */
Daniel@0:         in = fopen("mypicture.jpg", "rb");
Daniel@0:         if (!in) {
Daniel@0:                 im = gdImageCreateTrueColor(300, 300);
Daniel@0:         } else {
Daniel@0:                 im = gdImageCreateFromJpeg(in);
Daniel@0:                 fclose(in);
Daniel@0:         }
Daniel@0:         if (gdImageSX(im) < gdImageSY(im)) {
Daniel@0:                 radius = gdImageSX(im) / 2;
Daniel@0:         } else {
Daniel@0:                 radius = gdImageSY(im) / 2;
Daniel@0:         }
Daniel@0:         gdStringFTCircle(
Daniel@0:                 im,
Daniel@0:                 gdImageSX(im) / 2,
Daniel@0:                 gdImageSY(im) / 2,
Daniel@0:                 radius,
Daniel@0:                 radius / 2,
Daniel@0:                 0.8,
Daniel@0:                 "arial",
Daniel@0:                 24,
Daniel@0:                 "top text",
Daniel@0:                 "bottom text",
Daniel@0:                 gdTrueColorAlpha(240, 240, 255, 32));
Daniel@0:         out = fopen("gdfx.png", "wb");
Daniel@0:         if (!out) {
Daniel@0:                 fprintf(stderr, "Can't create gdfx.png\n");
Daniel@0:                 return 1;
Daniel@0:         }
Daniel@0:         gdImagePng(im, out);
Daniel@0:         fclose(out);
Daniel@0:         gdImageDestroy(im);
Daniel@0:         return 0;
Daniel@0: }
Daniel@0: 
Daniel@0: Daniel@0:

Daniel@0: For more information, see gdImageStringFTEx Daniel@0: and gdImageSquareToCircle. Daniel@0:

Daniel@0: char *gdImageStringTTF(gdImagePtr im, int *brect, Daniel@0: int fg, char *fontname, double ptsize, double angle, Daniel@0: int x, int y, char *string) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: DEPRECATED. This function simply invokes Daniel@0: gdImageStringFT for backwards Daniel@0: compatibility with old code that was written with FreeType 1.x. Daniel@0:
Daniel@0: int gdFontCacheSetup(void) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: This function initializes the font cache for freetype text output Daniel@0: functions such as gdImageStringFTEx. Daniel@0: If this function is not called by the programmer, it is invoked Daniel@0: automatically on the first truetype text output call, which is Daniel@0: perfectly safe unless the application is multithreaded. Daniel@0: Multithreaded applications should directly invoke this function before Daniel@0: allowing any thread to use freetype text output. Returns 0 on success, Daniel@0: nonzero if the freetype library fails to initialize. Daniel@0:
Daniel@0: void gdFontCacheShutdown(void) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: This function releases the memory used by the freetype font cache Daniel@0: and the text output mutex. Applications that use gd for their Daniel@0: entire lifetime, then exit, need not call this function. Daniel@0:
Daniel@0:

Color-handling functions

Daniel@0:
Daniel@0:
Daniel@0: int gdImageColorAllocate(gdImagePtr im, int r, int g, int b) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorAllocate finds the first available color index in Daniel@0: the image specified, sets its RGB values to those requested Daniel@0: (255 is the maximum for each), Daniel@0: and returns the index of the new color table entry, or an RGBA Daniel@0: value in the case of a truecolor image; in either case you can Daniel@0: then use the returned value as a parameter to drawing functions. When Daniel@0: creating a new palette-based image, the first time you invoke this function, Daniel@0: you are setting the background color for that image. Daniel@0:

Daniel@0: In the event that all gdMaxColors colors Daniel@0: (256) have already been allocated, gdImageColorAllocate will Daniel@0: return -1 to indicate failure. (This is not uncommon when Daniel@0: working with existing PNG files that already use 256 colors.) Daniel@0: Note that gdImageColorAllocate Daniel@0: does not check for existing colors that match your request; Daniel@0: see gdImageColorExact, Daniel@0: gdImageColorClosest and Daniel@0: gdImageColorClosestHWB Daniel@0: for ways to locate existing colors that approximate the Daniel@0: color desired in situations where a new color is not available. Daniel@0: Also see gdImageColorResolve, Daniel@0: new in gd-1.6.2. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int red;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color red. */
Daniel@0: red = gdImageColorAllocate(im, 255, 0, 0);  
Daniel@0: /* Draw a dashed line from the upper left corner 
Daniel@0:   to the lower right corner. */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
Daniel@0: /* ... Do something with the image, such as saving 
Daniel@0:   it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageColorAllocateAlpha(gdImagePtr im, int r, int g, int b, int a) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorAllocateAlpha finds the first available color index in Daniel@0: the image specified, sets its RGBA values to those requested Daniel@0: (255 is the maximum for red, green and blue, and 127 represents Daniel@0: full transparency for alpha), Daniel@0: and returns the index of the new color table entry, or an RGBA Daniel@0: value in the case of a truecolor image; in either case you can Daniel@0: then use the returned value as a parameter to drawing functions. When Daniel@0: creating a new palette-based image, the first time you invoke this function, Daniel@0: you are setting the background color for that image. Daniel@0:

Daniel@0: In the event that all gdMaxColors colors Daniel@0: (256) have already been allocated, gdImageColorAllocate will Daniel@0: return -1 to indicate failure. (This is not uncommon when Daniel@0: working with existing palette-based PNG files that already use 256 colors.) Daniel@0: Note that gdImageColorAllocateAlpha Daniel@0: does not check for existing colors that match your request; Daniel@0: see gdImageColorExactAlpha and Daniel@0: gdImageColorClosestAlpha Daniel@0: for ways to locate existing colors that approximate the Daniel@0: color desired in situations where a new color is not available. Daniel@0: Also see gdImageColorResolveAlpha. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: int red;
Daniel@0: im = gdImageCreate(100, 100);
Daniel@0: /* Background color (first allocated) */
Daniel@0: black = gdImageColorAllocate(im, 0, 0, 0);  
Daniel@0: /* Allocate the color red, 50% transparent. */
Daniel@0: red = gdImageColorAllocateAlpha(im, 255, 0, 0, 64);  
Daniel@0: /* Draw a dashed line from the upper left corner to the lower right corner. */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageColorClosest(gdImagePtr im, int r, int g, int b) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorClosest searches the colors which have been Daniel@0: defined thus far in the image specified and returns the Daniel@0: index of the color with RGB values closest to those of the Daniel@0: request. (Closeness is determined by Euclidian distance, Daniel@0: which is used to determine the distance in three-dimensional color Daniel@0: space between colors.) Daniel@0:

Daniel@0: If no colors have yet been allocated in the image, Daniel@0: gdImageColorClosest returns -1. Daniel@0:

Daniel@0: When applied to a truecolor image, this function always Daniel@0: succeeds in returning the desired color. Daniel@0:

Daniel@0: This function is most useful as a backup method for choosing Daniel@0: a drawing color when an image already contains Daniel@0: gdMaxColors (256) colors and Daniel@0: no more can be allocated. (This is not uncommon when Daniel@0: working with existing PNG files that already use many colors.) Daniel@0: See gdImageColorExact Daniel@0: for a method of locating exact matches only. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: FILE *in;
Daniel@0: int red;
Daniel@0: /* Let's suppose that photo.png is a scanned photograph with
Daniel@0:   many colors. */
Daniel@0: in = fopen("photo.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Try to allocate red directly */
Daniel@0: red = gdImageColorAllocate(im, 255, 0, 0);  
Daniel@0: /* If we fail to allocate red... */
Daniel@0: if (red == (-1)) {
Daniel@0:   /* Find the closest color instead. */
Daniel@0:   red = gdImageColorClosest(im, 255, 0, 0);
Daniel@0: }
Daniel@0: /* Draw a dashed line from the upper left corner to the lower right corner */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageColorClosestAlpha(gdImagePtr im, int r, int g, int b, int a) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorClosest searches the colors which have been Daniel@0: defined thus far in the image specified and returns the Daniel@0: index of the color with RGBA values closest to those of the Daniel@0: request. (Closeness is determined by Euclidian distance, Daniel@0: which is used to determine the distance in four-dimensional color/alpha Daniel@0: space between colors.) Daniel@0:

Daniel@0: If no colors have yet been allocated in the image, Daniel@0: gdImageColorClosestAlpha returns -1. Daniel@0:

Daniel@0: When applied to a truecolor image, this function always Daniel@0: succeeds in returning the desired color. Daniel@0:

Daniel@0: This function is most useful as a backup method for choosing Daniel@0: a drawing color when a palette-based image already contains Daniel@0: gdMaxColors (256) colors and Daniel@0: no more can be allocated. (This is not uncommon when Daniel@0: working with existing palette-based PNG files that already use many colors.) Daniel@0: See gdImageColorExactAlpha Daniel@0: for a method of locating exact matches only. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: FILE *in;
Daniel@0: int red;
Daniel@0: /* Let's suppose that photo.png is a scanned photograph with
Daniel@0:   many colors. */
Daniel@0: in = fopen("photo.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Try to allocate red, 50% transparent, directly */
Daniel@0: red = gdImageColorAllocateAlpha(im, 255, 0, 0, 64);  
Daniel@0: /* If we fail to allocate red... */
Daniel@0: if (red == (-1)) {
Daniel@0:   /* Find the closest color instead. */
Daniel@0:   red = gdImageColorClosestAlpha(im, 255, 0, 0, 64);
Daniel@0: }
Daniel@0: /* Draw a dashed line from the upper left corner to the lower right corner */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageColorClosestHWB(gdImagePtr im, int r, int g, int b) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorClosestHWB searches the colors which have been Daniel@0: defined thus far in the image specified and returns the Daniel@0: index of the color with hue, whiteness and blackness closest to the Daniel@0: requested color. This scheme is typically superior to the Daniel@0: Euclidian distance scheme used by Daniel@0: gdImageColorClosest. Daniel@0:

Daniel@0: If no colors have yet been allocated in the image, Daniel@0: gdImageColorClosestHWB returns -1. Daniel@0:

Daniel@0: When applied to a truecolor image, this function always Daniel@0: succeeds in returning the desired color. Daniel@0:

Daniel@0: This function is most useful as a backup method for choosing Daniel@0: a drawing color when an image already contains Daniel@0: gdMaxColors (256) colors and Daniel@0: no more can be allocated. (This is not uncommon when Daniel@0: working with existing PNG files that already use many colors.) Daniel@0: See gdImageColorExact Daniel@0: for a method of locating exact matches only. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: FILE *in;
Daniel@0: int red;
Daniel@0: /* Let's suppose that photo.png is a scanned photograph with
Daniel@0:   many colors. */
Daniel@0: in = fopen("photo.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Try to allocate red directly */
Daniel@0: red = gdImageColorAllocate(im, 255, 0, 0);  
Daniel@0: /* If we fail to allocate red... */
Daniel@0: if (red == (-1)) {
Daniel@0:   /* Find the closest color instead. */
Daniel@0:   red = gdImageColorClosestHWB(im, 255, 0, 0);
Daniel@0: }
Daniel@0: /* Draw a dashed line from the upper left corner to the lower right corner */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageColorExact(gdImagePtr im, int r, int g, int b) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorExact searches the colors which have been Daniel@0: defined thus far in the image specified and returns the Daniel@0: index of the first color with RGB values which exactly Daniel@0: match those of the request. If no allocated color matches the Daniel@0: request precisely, gdImageColorExact returns -1. Daniel@0: See gdImageColorClosest Daniel@0: for a way to find the color closest to the color requested. Daniel@0:

Daniel@0: When applied to a truecolor image, this function always Daniel@0: succeeds in returning the desired color. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int red;
Daniel@0: in = fopen("photo.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* The image may already contain red; if it does, we'll save a slot
Daniel@0:   in the color table by using that color. */
Daniel@0: /* Try to allocate red directly */
Daniel@0: red = gdImageColorExact(im, 255, 0, 0);
Daniel@0: /* If red isn't already present... */
Daniel@0: if (red == (-1)) {
Daniel@0:   /* Second best: try to allocate it directly. */
Daniel@0:   red = gdImageColorAllocate(im, 255, 0, 0);  
Daniel@0:   /* Out of colors, so find the closest color instead. */
Daniel@0:   red = gdImageColorClosest(im, 255, 0, 0);
Daniel@0: }
Daniel@0: /* Draw a dashed line from the upper left corner to the lower right corner */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageColorResolve(gdImagePtr im, int r, int g, int b) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorResolve searches the colors which have been Daniel@0: defined thus far in the image specified and returns the Daniel@0: index of the first color with RGB values which exactly Daniel@0: match those of the request. If no allocated color matches the Daniel@0: request precisely, then gdImageColorResolve tries to allocate the Daniel@0: exact color. If there is no space left in the color table then Daniel@0: gdImageColorResolve returns the closest color (as in gdImageColorClosest). Daniel@0: This function always returns an index of a color. Daniel@0:

Daniel@0: When applied to a truecolor image, this function always Daniel@0: succeeds in returning the desired color. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int red;
Daniel@0: in = fopen("photo.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* The image may already contain red; if it does, we'll save a slot
Daniel@0:   in the color table by using that color. */
Daniel@0: /* Get index of red, or color closest to red */
Daniel@0: red = gdImageColorResolve(im, 255, 0, 0);
Daniel@0: /* Draw a dashed line from the upper left corner to the lower right corner */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageColorResolveAlpha(gdImagePtr im, int r, int g, int b, int a) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorResolveAlpha searches the colors which have been Daniel@0: defined thus far in the image specified and returns the Daniel@0: index of the first color with RGBA values which exactly Daniel@0: match those of the request. If no allocated color matches the Daniel@0: request precisely, then gdImageColorResolveAlpha tries to allocate the Daniel@0: exact color. If there is no space left in the color table then Daniel@0: gdImageColorResolveAlpha returns the closest color (as in gdImageColorClosestAlpha). Daniel@0: This function always returns an index of a color. Daniel@0:

Daniel@0: When applied to a truecolor image, this function always Daniel@0: succeeds in returning the desired color. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int red;
Daniel@0: in = fopen("photo.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* The image may already contain red; if it does, 
Daniel@0:   we'll save a slot in the color table by using that color. */
Daniel@0: /* Get index of red, 50% transparent, or the next best thing */
Daniel@0: red = gdImageColorResolveAlpha(im, 255, 0, 0, 64);
Daniel@0: /* Draw a dashed line from the upper left corner to the lower right corner */
Daniel@0: gdImageDashedLine(im, 0, 0, 99, 99, red);
Daniel@0: /* ... Do something with the image, such as saving 
Daniel@0:   it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: int gdImageColorsTotal(gdImagePtr im) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageColorsTotal is a macro which returns the number of Daniel@0: colors currently allocated in a palette image. For truecolor Daniel@0: images, the result of this call is undefined and should not Daniel@0: be used. Daniel@0:
Daniel@0: int gdImageRed(gdImagePtr im, int c) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageRed is a macro which returns the red portion Daniel@0: of the specified color in the image. This macro works Daniel@0: for both palette and truecolor images. Daniel@0:
Daniel@0: int gdImageGreen(gdImagePtr im, int c) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageGreen is a macro which returns the green portion Daniel@0: of the specified color in the image. This macro works Daniel@0: for both palette and truecolor images. Daniel@0:
Daniel@0: int gdImageBlue(gdImagePtr im, int c) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageBlue is a macro which returns the blue portion Daniel@0: of the specified color in the image. This macro works Daniel@0: for both palette and truecolor images. Daniel@0:
Daniel@0: int gdImageGetInterlaced(gdImagePtr im) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageGetInterlaced is a macro which returns true (1) Daniel@0: if the image is interlaced, false (0) if not. Daniel@0: Use this macro to obtain this information; do not Daniel@0: access the structure directly. Daniel@0: See gdImageInterlace for Daniel@0: a means of interlacing images. Daniel@0:
Daniel@0: int gdImageGetTransparent(gdImagePtr im) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageGetTransparent is a macro which returns the Daniel@0: current transparent color index in the image. Daniel@0: If there is no transparent color, gdImageGetTransparent Daniel@0: returns -1. Use this macro to obtain this information; do not Daniel@0: access the structure directly. Daniel@0:
Daniel@0: void gdImageColorDeallocate(gdImagePtr im, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorDeallocate marks the specified color as being Daniel@0: available for reuse. It does not attempt to determine whether Daniel@0: the color index is still in use in the image. After a call Daniel@0: to this function, the next call to Daniel@0: gdImageColorAllocate Daniel@0: for the same image will set new RGB values for that Daniel@0: color index, changing the color of any pixels which Daniel@0: have that index as a result. If multiple calls to Daniel@0: gdImageColorDeallocate are made consecutively, the lowest-numbered Daniel@0: index among them will be reused by the next Daniel@0: gdImageColorAllocate call. Daniel@0:
Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int red, blue;
Daniel@0: in = fopen("photo.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Look for red in the color table. */
Daniel@0: red = gdImageColorExact(im, 255, 0, 0);
Daniel@0: /* If red is present... */
Daniel@0: if (red != (-1)) {
Daniel@0:   /* Deallocate it. */
Daniel@0:   gdImageColorDeallocate(im, red);
Daniel@0:   /* Allocate blue, reusing slot in table.
Daniel@0:     Existing red pixels will change color. */
Daniel@0:   blue = gdImageColorAllocate(im, 0, 0, 255);
Daniel@0: }
Daniel@0: /* ... Do something with the image, such as 
Daniel@0:   saving it to a file... */
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageColorTransparent(gdImagePtr im, int color) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageColorTransparent sets the transparent color index Daniel@0: for the specified image to the specified index. To indicate Daniel@0: that there should be no transparent color, invoke Daniel@0: gdImageColorTransparent with a color index of -1. Note that Daniel@0: JPEG images do not support transparency, so this setting has no effect Daniel@0: when writing JPEG images. Daniel@0:

Daniel@0: The color index used should be an index Daniel@0: allocated by gdImageColorAllocate, Daniel@0: whether explicitly invoked by your code or implicitly Daniel@0: invoked by loading an image. Daniel@0: In order to ensure that your image has a reasonable appearance Daniel@0: when viewed by users who do not have transparent background Daniel@0: capabilities (or when you are writing a JPEG-format file, which does Daniel@0: not support transparency), be sure to give reasonable RGB values to the Daniel@0: color you allocate for use as a transparent color, Daniel@0: even though it will be transparent on systems Daniel@0: that support PNG transparency. Daniel@0:

Daniel@0: ... inside a function ...
Daniel@0: gdImagePtr im;
Daniel@0: int black;
Daniel@0: FILE *in, *out;
Daniel@0: in = fopen("photo.png", "rb");
Daniel@0: im = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Look for black in the color table and make it transparent. */
Daniel@0: black = gdImageColorExact(im, 0, 0, 0);
Daniel@0: /* If black is present... */
Daniel@0: if (black != (-1)) {
Daniel@0:   /* Make it transparent */
Daniel@0:   gdImageColorTransparent(im, black);
Daniel@0: }
Daniel@0: /* Save the newly-transparent image back to the file */
Daniel@0: out = fopen("photo.png", "wb");
Daniel@0: gdImagePng(im, out);
Daniel@0: fclose(out);
Daniel@0: /* Destroy it */
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
Daniel@0: void gdImageTrueColor(int red, int green, int blue) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdImageTrueColor returns an RGBA color value for use when Daniel@0: drawing on a truecolor image. Red, green, and blue are all Daniel@0: in the range between 0 (off) and 255 (maximum). This macro should Daniel@0: not be used with palette-based images. If you need to write Daniel@0: code which is compatible with both palette-based and Daniel@0: truecolor images, use gdImageColorResolve. Daniel@0:
Daniel@0: void gdTrueColorAlpha(int red, int green, int blue, int alpha) Daniel@0: (MACRO) Daniel@0:
Daniel@0: gdTrueColorAlpha returns an RGBA color value for use when Daniel@0: drawing on a truecolor image with alpha channel transparency. Red, Daniel@0: green, and blue are all Daniel@0: in the range between 0 (off) and 255 (maximum). Alpha is in the Daniel@0: range between 0 (opaque) and 127 (fully transparent). This macro should Daniel@0: not be used with palette-based images. If you need to write Daniel@0: code which is compatible with both palette-based and Daniel@0: truecolor images, use gdImageColorResolveAlpha.
Daniel@0:

Copying and resizing functions

Daniel@0:
Daniel@0: Daniel@0:
void gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCopy is used to copy a rectangular portion of one image to Daniel@0: another image. (For a way of stretching or shrinking the image Daniel@0: in the process, see Daniel@0: gdImageCopyResized.) Daniel@0:

Daniel@0: The dst argument is the destination image to which the Daniel@0: region will be copied. The src argument is the source Daniel@0: image from which the region is copied. The dstX Daniel@0: and dstY arguments specify the point in the destination Daniel@0: image to which the region will be copied. The srcX Daniel@0: and srcY arguments specify the upper left corner Daniel@0: of the region in the source image. The w Daniel@0: and h arguments specify the width and height Daniel@0: of the region. Daniel@0:

Daniel@0: When you copy a region from one location in an image to another Daniel@0: location in the same image, gdImageCopy will perform as expected Daniel@0: unless the regions overlap, in which case the result is Daniel@0: unpredictable. Daniel@0:

Daniel@0: Important note on copying between images: since Daniel@0: different images do Daniel@0: not necessarily have the same color tables, pixels are not simply set to the Daniel@0: same color index values to copy them. gdImageCopy will attempt Daniel@0: to find an identical RGB value in the destination image for Daniel@0: each pixel in the copied portion of the source image by Daniel@0: invoking gdImageColorExact. If Daniel@0: such a value is not found, gdImageCopy will attempt to Daniel@0: allocate colors as needed using Daniel@0: gdImageColorAllocate. If both of these methods fail, Daniel@0: gdImageCopy will invoke Daniel@0: gdImageColorClosest to find the color in the destination Daniel@0: image which most closely approximates the color of the Daniel@0: pixel being copied. Daniel@0:

Daniel@0: ... Inside a function ...
Daniel@0: gdImagePtr im_in;
Daniel@0: gdImagePtr im_out;
Daniel@0: int x, y;
Daniel@0: FILE *in;
Daniel@0: FILE *out;
Daniel@0: /* Load a small png to tile the larger one with */
Daniel@0: in = fopen("small.png", "rb");
Daniel@0: im_in = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Make the output image four times as large on both axes */
Daniel@0: im_out = gdImageCreate(im_in->sx * 4, im_in->sy * 4);
Daniel@0: /* Now tile the larger image using the smaller one */
Daniel@0: for (y = 0; (y < 4); y++) {
Daniel@0:   for (x = 0; (x < 4); x++) {
Daniel@0:     gdImageCopy(im_out, im_in,
Daniel@0:       x * im_in->sx, y * im_in->sy,
Daniel@0:       0, 0,
Daniel@0:       im_in->sx, im_in->sy);
Daniel@0:   }
Daniel@0: }
Daniel@0: out = fopen("tiled.png", "wb");
Daniel@0: gdImagePng(im_out, out);
Daniel@0: fclose(out);
Daniel@0: gdImageDestroy(im_in);
Daniel@0: gdImageDestroy(im_out);
Daniel@0: 
Daniel@0:
void gdImageCopyResized(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int destW, int destH, int srcW, int srcH) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCopyResized is used to copy a rectangular portion of one image to Daniel@0: another image. The X and Y dimensions of the original region and the Daniel@0: destination region can vary, resulting in stretching or shrinking of Daniel@0: the region as appropriate. (For a simpler version of this function Daniel@0: which does not deal with resizing, see Daniel@0: gdImageCopy.) Daniel@0:

Daniel@0: The dst argument is the destination image to which the Daniel@0: region will be copied. The src argument is the source Daniel@0: image from which the region is copied. The dstX Daniel@0: and dstY arguments specify the point in the destination Daniel@0: image to which the region will be copied. The srcX Daniel@0: and srcY arguments specify the upper left corner Daniel@0: of the region in the source image. The dstW Daniel@0: and dstH arguments specify the width and height Daniel@0: of the destination region. The srcW Daniel@0: and srcH arguments specify the width and height Daniel@0: of the source region and can differ from the destination size, Daniel@0: allowing a region to be scaled during the copying process. Daniel@0:

Daniel@0: When you copy a region from one location in an image to another Daniel@0: location in the same image, gdImageCopy will perform as expected Daniel@0: unless the regions overlap, in which case the result is Daniel@0: unpredictable. If this presents a problem, create a scratch image Daniel@0: in which to keep intermediate results. Daniel@0:

Daniel@0: Important note on copying between images: since images Daniel@0: do not necessarily have the same color tables, pixels are not simply set Daniel@0: to the same color index values to copy them. gdImageCopy will attempt Daniel@0: to find an identical RGB value in the destination image for Daniel@0: each pixel in the copied portion of the source image by Daniel@0: invoking gdImageColorExact. If Daniel@0: such a value is not found, gdImageCopy will attempt to Daniel@0: allocate colors as needed using Daniel@0: gdImageColorAllocate. If both of these methods fail, Daniel@0: gdImageCopy will invoke Daniel@0: gdImageColorClosest to find the color in the destination Daniel@0: image which most closely approximates the color of the Daniel@0: pixel being copied. Daniel@0:

Daniel@0: ... Inside a function ...
Daniel@0: gdImagePtr im_in;
Daniel@0: gdImagePtr im_out;
Daniel@0: int x, y;
Daniel@0: FILE *in;
Daniel@0: FILE *out;
Daniel@0: /* Load a small png to expand in the larger one */
Daniel@0: in = fopen("small.png", "rb");
Daniel@0: im_in = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Make the output image four times as large on both axes */
Daniel@0: im_out = gdImageCreate(im_in->sx * 4, im_in->sy * 4);
Daniel@0: /* Now copy the smaller image, but four times larger */
Daniel@0: gdImageCopyResized(im_out, im_in, 0, 0, 0, 0,
Daniel@0:   im_out->sx, im_out->sy,
Daniel@0:   im_in->sx, im_in->sy);  
Daniel@0: out = fopen("large.png", "wb");
Daniel@0: gdImagePng(im_out, out);
Daniel@0: fclose(out);
Daniel@0: gdImageDestroy(im_in);
Daniel@0: gdImageDestroy(im_out);
Daniel@0: 
Daniel@0:
void gdImageCopyResampled(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int destW, int destH, int srcW, int srcH) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCopyResampled is used to copy a rectangular portion of one image to Daniel@0: another image, smoothly interpolating pixel values so that, in particular, Daniel@0: reducing the size of an image still retains a great deal of clarity. The Daniel@0: X and Y dimensions of the original region and the Daniel@0: destination region can vary, resulting in stretching or shrinking of Daniel@0: the region as appropriate. (For a simpler version of this function Daniel@0: which does not deal with resizing, see Daniel@0: gdImageCopy. For a version which does not interpolate pixel values, Daniel@0: see gdImageCopyResized. Daniel@0:

Daniel@0: Pixel values are only interpolated if the destination image is a Daniel@0: truecolor image. Otherwise, Daniel@0: gdImageCopyResized is Daniel@0: automatically invoked. Daniel@0:

Daniel@0: The dst argument is the destination image to which the Daniel@0: region will be copied. The src argument is the source Daniel@0: image from which the region is copied. The dstX Daniel@0: and dstY arguments specify the point in the destination Daniel@0: image to which the region will be copied. The srcX Daniel@0: and srcY arguments specify the upper left corner Daniel@0: of the region in the source image. The dstW Daniel@0: and dstH arguments specify the width and height Daniel@0: of the destination region. The srcW Daniel@0: and srcH arguments specify the width and height Daniel@0: of the source region and can differ from the destination size, Daniel@0: allowing a region to be scaled during the copying process. Daniel@0:

Daniel@0: When you copy a region from one location in an image to another Daniel@0: location in the same image, gdImageCopy will perform as expected Daniel@0: unless the regions overlap, in which case the result is Daniel@0: unpredictable. If this presents a problem, create a scratch image Daniel@0: in which to keep intermediate results. Daniel@0:

Daniel@0: Important note on copying between images: since images Daniel@0: do not necessarily have the same color tables, pixels are not simply set Daniel@0: to the same color index values to copy them. If the destination image Daniel@0: is a palette image, gd will use the Daniel@0: gdImageColorResolve function to Daniel@0: determine the best color available. Daniel@0:

Daniel@0: ... Inside a function ...
Daniel@0: gdImagePtr im_in;
Daniel@0: gdImagePtr im_out;
Daniel@0: int x, y;
Daniel@0: FILE *in;
Daniel@0: FILE *out;
Daniel@0: /* Load a large png to shrink in the smaller one */
Daniel@0: in = fopen("large.png", "rb");
Daniel@0: im_in = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Make the output image four times as small on both axes. Use
Daniel@0:   a true color image so that we can interpolate colors. */
Daniel@0: im_out = gdImageCreateTrueColor(im_in->sx / 4, im_in->sy / 4);
Daniel@0: /* Now copy the large image, but four times smaller */
Daniel@0: gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0,
Daniel@0:   im_out->sx, im_out->sy,
Daniel@0:   im_in->sx, im_in->sy);  
Daniel@0: out = fopen("large.png", "wb");
Daniel@0: gdImagePng(im_out, out);
Daniel@0: fclose(out);
Daniel@0: gdImageDestroy(im_in);
Daniel@0: gdImageDestroy(im_out);
Daniel@0: 
Daniel@0:
void gdImageCopyRotated(gdImagePtr dst, gdImagePtr src, double dstX, double dstY, int srcX, int srcY, int srcW, int srcH, int angle) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCopyRotated is used to copy a rectangular portion of one image to Daniel@0: another image, or to another region of the same image. The srcX and Daniel@0: srcY coordinates specify the upper left corner of the source area; however, Daniel@0: the dstX and dstY coordinates specify the CENTER of the destination area. Daniel@0: This important distinction is made because the rotated rectangle may Daniel@0: may or may not be parallel to the X and Y axes. The destination coordinates Daniel@0: may be floating point, as the center of the desired destination area may lie Daniel@0: at the center of a pixel (0.5 pixels) rather than its upper left corner. Daniel@0: The angle specified is an integer number of degrees, between 0 and 360, Daniel@0: with 0 degrees causing no change, and counterclockwise rotation as Daniel@0: the angle increases. Daniel@0:

Daniel@0: When you copy a region from one location in an image to another Daniel@0: location in the same image, gdImageCopyRotated will perform as expected Daniel@0: unless the regions overlap, in which case the result is Daniel@0: unpredictable. If this presents a problem, create a scratch image Daniel@0: in which to keep intermediate results. Daniel@0:

Daniel@0: Important note on copying between images: since Daniel@0: palette-based images do not necessarily have the same color tables, pixels Daniel@0: are not simply set to the same color index values to copy them. Daniel@0: If the destination image is not a truecolor image, Daniel@0: gdImageColorResolveAlpha is Daniel@0: used to choose the destination pixel. Daniel@0:

Daniel@0: ... Inside a function ...
Daniel@0: gdImagePtr im_in;
Daniel@0: gdImagePtr im_out;
Daniel@0: int x, y;
Daniel@0: int a;
Daniel@0: FILE *in;
Daniel@0: FILE *out;
Daniel@0: /* Load a small png to rotate in the larger one */
Daniel@0: in = fopen("small.png", "rb");
Daniel@0: im_in = gdImageCreateFromPng(in);
Daniel@0: fclose(in);
Daniel@0: /* Make the output image four times as large on both axes */
Daniel@0: im_out = gdImageCreate(im_in->sx * 4, im_in->sy * 4);
Daniel@0: /* Now rotate the smaller image */
Daniel@0: for (a = 0; (a < 360); a += 45) {
Daniel@0:         double x = cos(a * .0174532925) * gdImageSX(im_out) / 2;
Daniel@0:         double y = -sin(a * .0174532925) * gdImageSY(im_out) / 2;
Daniel@0:   gdImageCopyRotated(im_out, im_in, 
Daniel@0:                 gdImageSX(im_out) / 2 + x, 
Daniel@0:                 gdImageSY(im_out) / 2 + y, 
Daniel@0:     0, 0,
Daniel@0:                 gdImageSX(im_in),
Daniel@0:                 gdImageSY(im_in),
Daniel@0:                 a); 
Daniel@0: }
Daniel@0: out = fopen("large.png", "wb");
Daniel@0: gdImagePng(im_out, out);
Daniel@0: fclose(out);
Daniel@0: gdImageDestroy(im_in);
Daniel@0: gdImageDestroy(im_out);
Daniel@0: 
Daniel@0: Daniel@0:
void gdImageCopyMerge(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h, int pct) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCopyMerge is almost identical to gdImageCopy, except that Daniel@0: it 'merges' the two images by an amount specified in the last parameter. If the last Daniel@0: parameter is 100, then it will function identically to gdImageCopy - the source image replaces Daniel@0: the pixels in the destination. Daniel@0:

Daniel@0: If, however, the pct parameter is less than 100, then the two images are merged. Daniel@0: With pct = 0, no action is taken. Daniel@0:

This feature is most useful to 'highlight' sections of an image by merging a solid color with Daniel@0: pct = 50: Daniel@0:

Daniel@0: ... Inside a function ...
Daniel@0: gdImageCopyMerge(im_out, im_in, 100, 200, 0, 0, 30, 50, 50);
Daniel@0: 
Daniel@0: Daniel@0:
void gdImageCopyMergeGray(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int Daniel@0: w, int h, int pct) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCopyMergeGray is almost identical to gdImageCopyMerge, Daniel@0: except that when merging images it preserves the hue of the source by converting the destination Daniel@0: pixels to grey scale before the copy operation. Daniel@0:
Daniel@0: ... Inside a function ...
Daniel@0: gdImageCopyMergeGray(im_out, im_in, 100, 200, 0, 0, 30, 50, 50);
Daniel@0: 
Daniel@0: Daniel@0:
void gdImagePaletteCopy(gdImagePtr dst, gdImagePtr src) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Copies a palette from one image to another, attempting to match the colors in the target image Daniel@0: to the colors Daniel@0: in the source palette. Daniel@0:
void gdImageSquareToCircle(gdImagePtr im, int radius) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: im MUST be square, but can have any size. Returns a new image Daniel@0: of width and height radius * 2, in which the X axis of Daniel@0: the original has been remapped to theta (angle) and the Y axis Daniel@0: of the original has been remapped to rho (distance from center). Daniel@0: This is known as a "polar coordinate transform." Daniel@0: See also gdImageStringFTCircle, which Daniel@0: uses this function internally. Daniel@0:
void gdImageSharpen(gdImagePtr im, int pct) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: Sharpens the specified image. pct is a sharpening percentage, and Daniel@0: can be greater than 100. Silently does nothing to non-truecolor images. Daniel@0: Silently does nothing for pct<0. Transparency/alpha channel are not Daniel@0: altered. Daniel@0:
Daniel@0:

Miscellaneous Functions

Daniel@0:
Daniel@0: Daniel@0:
int gdImageCompare(gdImagePtr im1, gdImagePtr im2) Daniel@0: (FUNCTION) Daniel@0:
Daniel@0: gdImageCompare returns a bitmap indicating if the two images are different. The members of the Daniel@0: bitmap are defined in gd.h, but the most important is GD_CMP_IMAGE, which indicated that the images Daniel@0: will actually appear different when displayed. Other, less important, differences relate to pallette Daniel@0: entries. Any difference in the transparent colour is assumed to make images display differently, Daniel@0: even if the transparent colour is not used. Daniel@0:
Daniel@0: ... Inside a function ...
Daniel@0: cmpMask = gdImageCompare(im1, im2);
Daniel@0: 
Daniel@0: Daniel@0:
gdImageInterlace(gdImagePtr im, int interlace) (FUNCTION) Daniel@0:
Daniel@0: gdImageInterlace is used to determine whether an image should be stored Daniel@0: in a linear fashion, in which lines will appear on the display from Daniel@0: first to last, or in an interlaced fashion, in which the image Daniel@0: will "fade in" over several passes. By default, images are not Daniel@0: interlaced. (When writing JPEG images, interlacing implies generating Daniel@0: progressive JPEG files, which are represented as a series of scans of Daniel@0: increasing quality. Noninterlaced gd images result in regular Daniel@0: [sequential] JPEG data streams.) Daniel@0:

Daniel@0: A nonzero value for the interlace argument turns on interlace; Daniel@0: a zero value turns it off. Note that interlace has no effect Daniel@0: on other functions, and has no meaning unless you save the Daniel@0: image in PNG or JPEG format; the gd and xbm formats do not support Daniel@0: interlace. Daniel@0:

Daniel@0: When a PNG is loaded with Daniel@0: gdImageCreateFromPng or a JPEG is Daniel@0: loaded with Daniel@0: gdImageCreateFromJpeg, interlace Daniel@0: will be set according to the setting in the PNG or JPEG file. Daniel@0:

Daniel@0: Note that many PNG and JPEG viewers and web browsers do not Daniel@0: support interlace or the incremental display of progressive Daniel@0: JPEGs. However, the interlaced PNG or progressive JPEG should still Daniel@0: display; it will simply appear all at once, just as other images do. Daniel@0:

Daniel@0: gdImagePtr im;
Daniel@0: FILE *out;
Daniel@0: /* ... Create or load the image... */
Daniel@0: 
Daniel@0: /* Now turn on interlace */
Daniel@0: gdImageInterlace(im, 1);
Daniel@0: /* And open an output file */
Daniel@0: out = fopen("test.png", "wb");
Daniel@0: /* And save the image  -- could also use gdImageJpeg */
Daniel@0: gdImagePng(im, out);
Daniel@0: fclose(out);
Daniel@0: gdImageDestroy(im);
Daniel@0: 
Daniel@0:
gdFree(void *ptr) (FUNCTION) Daniel@0:
Daniel@0: gdFree provides a reliable way to free memory allocated by functions Daniel@0: such as gdImagePngPtr which return Daniel@0: blocks of memory. Use of this function guarantees that the Daniel@0: version of free() that is ultimately called will Daniel@0: be intended for use with the version of malloc() that Daniel@0: originally allocated the block. Daniel@0:
Daniel@0:

Constants

Daniel@0:
Daniel@0:
gdAntiAliased (CONSTANT) Daniel@0:
Daniel@0: Used in place of a color when invoking a line-drawing Daniel@0: function such as gdImageLine Daniel@0: or gdImageRectangle. Daniel@0: When gdAntiAliased is used as the color, the foreground color Daniel@0: set with gdImageSetAntiAliased Daniel@0: is used, with antialiasing mechanisms to minimize any Daniel@0: "jagged" appearance. Daniel@0: For more information, see Daniel@0: gdImageSetAntiAliased. Daniel@0:
gdBrushed (CONSTANT) Daniel@0:
Daniel@0: Used in place of a color when invoking a line-drawing Daniel@0: function such as gdImageLine Daniel@0: or gdImageRectangle. Daniel@0: When gdBrushed is used as the color, the brush Daniel@0: image set with gdImageSetBrush Daniel@0: is drawn in place of each pixel of the line (the brush is Daniel@0: usually larger than one pixel, creating the effect Daniel@0: of a wide paintbrush). See also Daniel@0: gdStyledBrushed for a way Daniel@0: to draw broken lines with a series of distinct copies of an image. Daniel@0:
gdMaxColors(CONSTANT) Daniel@0:
Daniel@0: The constant 256. This is the maximum number of colors in a palette-based Daniel@0: PNG file according to the PNG standard, and is also the maximum number of Daniel@0: colors in a palette-based gd image. This of course does not apply to Daniel@0: truecolor images. Daniel@0:
gdStyled (CONSTANT) Daniel@0:
Daniel@0: Used in place of a color when invoking a line-drawing Daniel@0: function such as gdImageLine Daniel@0: or gdImageRectangle. Daniel@0: When gdStyled is used as the color, the colors of the pixels are Daniel@0: drawn successively from the style that has been Daniel@0: set with gdImageSetStyle. Daniel@0: If the color of a pixel is equal to Daniel@0: gdTransparent, that pixel Daniel@0: is not altered. (This mechanism is completely unrelated Daniel@0: to the "transparent color" of the image itself; see Daniel@0: gdImageColorTransparent Daniel@0: gdImageColorTransparent for that mechanism.) See also Daniel@0: gdStyledBrushed. Daniel@0:
gdStyledBrushed (CONSTANT) Daniel@0:
Daniel@0: Used in place of a color when invoking a line-drawing Daniel@0: function such as gdImageLine Daniel@0: or gdImageRectangle. Daniel@0: When gdStyledBrushed is used as the color, the brush Daniel@0: image set with gdImageSetBrush Daniel@0: is drawn at each pixel of the line, providing that the Daniel@0: style set with gdImageSetStyle Daniel@0: contains a nonzero value (OR gdTransparent, which Daniel@0: does not equal zero but is supported for consistency) Daniel@0: for the current pixel. (Pixels are drawn successively from the style as the Daniel@0: line is drawn, returning to the beginning when the Daniel@0: available pixels in the style are exhausted.) Note that Daniel@0: this differs from the behavior of gdStyled, Daniel@0: in which the values in the style are used as actual Daniel@0: pixel colors, except for gdTransparent. Daniel@0:
gdDashSize (CONSTANT) Daniel@0:
Daniel@0: The length of a dash in a dashed line. Defined to be 4 for Daniel@0: backwards compatibility with programs that use Daniel@0: gdImageDashedLine. New Daniel@0: programs should use Daniel@0: gdImageSetStyle and call the standard Daniel@0: gdImageLine function Daniel@0: with the special "color" Daniel@0: gdStyled or gdStyledBrushed. Daniel@0:
gdTiled (CONSTANT) Daniel@0:
Daniel@0: Used in place of a normal color in Daniel@0: gdImageFilledRectangle, Daniel@0: gdImageFilledPolygon, Daniel@0: gdImageFill, and Daniel@0: gdImageFillToBorder. gdTiled selects a pixel from the Daniel@0: tile image set with gdImageSetTile Daniel@0: in such a way as to ensure that the filled area will be Daniel@0: tiled with copies of the tile image. See the discussions of Daniel@0: gdImageFill and Daniel@0: gdImageFillToBorder for special Daniel@0: restrictions regarding those functions. Daniel@0:
gdTransparent (CONSTANT) Daniel@0:
Daniel@0: Used in place of a normal color in a style to be set with Daniel@0: gdImageSetStyle. Daniel@0: gdTransparent is not the transparent Daniel@0: color index of the image; for that functionality please Daniel@0: see gdImageColorTransparent. Daniel@0:
Daniel@0: Daniel@0:

About the additional .gd image file format

Daniel@0: In addition to reading and writing the PNG and JPEG formats and reading the Daniel@0: X Bitmap format, gd has the capability to read and write its Daniel@0: own ".gd" format. This format is not intended for Daniel@0: general purpose use and should never be used to distribute Daniel@0: images. It is not a compressed format. Its purpose is solely to Daniel@0: allow very fast loading of images your program needs often in Daniel@0: order to build other images for output. If you are experiencing Daniel@0: performance problems when loading large, fixed PNG images your Daniel@0: program needs to produce its output images, you may wish Daniel@0: to examine the functions Daniel@0: gdImageCreateFromGd and gdImageGd, Daniel@0: which read and write .gd format images. Daniel@0: Daniel@0:

Daniel@0: The program "pngtogd.c" is provided as a simple way of converting Daniel@0: .png files to .gd format. I emphasize again that you will not Daniel@0: need to use this format unless you have a need for high-speed loading Daniel@0: of a few frequently-used images in your program. Daniel@0: Daniel@0:

About the .gd2 image file format

Daniel@0: In addition to reading and writing the PNG format and reading the Daniel@0: X Bitmap format, gd has the capability to read and write its Daniel@0: own ".gd2" format. This format is not intended for Daniel@0: general purpose use and should never be used to distribute Daniel@0: images. It is a compressed format allowing pseudo-random access Daniel@0: to large image files. Its purpose is solely to Daniel@0: allow very fast loading of parts of images Daniel@0: If you are experiencing Daniel@0: performance problems when loading large, fixed PNG or JPEG images your Daniel@0: program needs to produce its output images, you may wish Daniel@0: to examine the functions Daniel@0: gdImageCreateFromGd2, Daniel@0: gdImageCreateFromGd2Part and gdImageGd2, Daniel@0: which read and write .gd2 format images. Daniel@0: Daniel@0:

Daniel@0: The program "pngtogd2.c" is provided as a simple way of converting Daniel@0: .png files to .gd2 format. Daniel@0: Daniel@0:

About the gdIOCtx structure

Daniel@0: Version 1.5 of GD added a new style of I/O based on an IOCtx Daniel@0: structure (the most up-to-date version can be found in gd_io.h): Daniel@0:
Daniel@0: typedef struct gdIOCtx {
Daniel@0:         int     (*getC)(struct gdIOCtx*);
Daniel@0:         int     (*getBuf)(struct gdIOCtx*, void*, int);
Daniel@0: 
Daniel@0:         void     (*putC)(struct gdIOCtx*, int);
Daniel@0:         int     (*putBuf)(struct gdIOCtx*, const void*, int);
Daniel@0: 
Daniel@0:         int     (*seek)(struct gdIOCtx*, const int); /* Returns 1 on SUCCESS */
Daniel@0:         long    (*tell)(struct gdIOCtx*); 
Daniel@0: 
Daniel@0:         void    (*free)(struct gdIOCtx*);
Daniel@0: 
Daniel@0: } gdIOCtx;
Daniel@0: 
Daniel@0: Daniel@0: Most functions that accepted files in previous versions now also have a Daniel@0: counterpart that accepts an I/O context. These functions have a 'Ctx' Daniel@0: suffix. Daniel@0:

Daniel@0: The Ctx routines use the function pointers in the I/O context pointed to Daniel@0: by gdIOCtx to perform all I/O. Examples of how to implement an I/O context Daniel@0: can be found in io_file.c (which provides a wrapper for file routines), and Daniel@0: io_dp.c (which implements in-memory storage). Daniel@0:

Daniel@0: It is not necessary to implement all functions in an I/O context if you know Daniel@0: that it will only be used in limited cirsumstances. At the time of writing Daniel@0: (Version 1.6.1, July 1999), the known requirements are: Daniel@0:

Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0:
All Must have 'free',
Anything that reads from the contextMust have 'getC' and 'getBuf',
Anything that writes to the contextMust have 'putC' and 'putBuf'.
If gdCreateFromGd2Part is calledMust also have 'seek' and 'tell'. Note: seek must return 1 on SUCCESS and 0 on FAILURE.
If gdImageGd2 is calledMust also have 'seek' and 'tell'.
Daniel@0: Daniel@0: Daniel@0: Daniel@0:

Please tell us you're using gd!

Daniel@0: When you contact us and let us know you are using gd, Daniel@0: you help us justify the time spent in maintaining and improving Daniel@0: it. So please let us know. If the results are publicly Daniel@0: visible on the web, a URL is a wonderful thing to receive, but Daniel@0: if it's not a publicly visible project, a simple note is just Daniel@0: as welcome. Daniel@0: Daniel@0:

How do I get support?

Daniel@0:

Free Support

Daniel@0: Daniel@0: Anyone can mail questions about the gd library using the Daniel@0: LibGD support. However, Daniel@0: we receive a very large volume of email on many subjects, and while we do Daniel@0: our best to respond to all queries this can take some time. Sometimes Daniel@0: the response must take the form of an eventual new release or Daniel@0: an addition to a FAQ or other document, as opposed to an detailed individual response. Daniel@0: Daniel@0:

Hourly Support

Daniel@0: Those requiring support in detail may arrange for direct support Daniel@0: from the maintaines, at the rate of $50/hr, billed Daniel@0: directly by credit card. Purchase orders are also accepted from Daniel@0: Fortune 500 corporations and institutions in good standing. Daniel@0: To make arrangements, contact Pierre-A. Joye. To avoid delay Daniel@0: and/or confusion, be sure to specifically mention that you wish to purchase gd support at the Daniel@0: hourly rate above. Daniel@0: Daniel@0:

How do I report issues, bugs or features request?

Daniel@0: Bugs, feature requests or other issues can be reported using the Daniel@0: libGD.org issues tracker. as well as using Daniel@0: one of our support channels: LibGD support Daniel@0: Daniel@0:

Alphabetical quick index

Daniel@0: gdAntiAliased | Daniel@0: gdBrushed | Daniel@0: gdDashSize | Daniel@0: gdFont | Daniel@0: gdFontGetHuge | Daniel@0: gdFontGetLarge | Daniel@0: gdFontGetMediumBold | Daniel@0: gdFontGetSmall | Daniel@0: gdFontGetTiny | Daniel@0: gdFontCacheSetup | Daniel@0: gdFontCacheShutdown | Daniel@0: gdFontPtr | Daniel@0: gdFree | Daniel@0: gdImage | Daniel@0: gdImageAlphaBlending | Daniel@0: gdImageArc | Daniel@0: gdImageBlue | Daniel@0: gdImageBoundsSafe | Daniel@0: gdImageChar | Daniel@0: gdImageCharUp | Daniel@0: gdImageColorAllocate | Daniel@0: gdImageColorAllocateAlpha | Daniel@0: gdImageColorClosest | Daniel@0: gdImageColorClosestAlpha | Daniel@0: gdImageColorClosestHWB | Daniel@0: gdImageColorDeallocate | Daniel@0: gdImageColorExact | Daniel@0: gdImageColorExactAlpha | Daniel@0: gdImageColorResolve | Daniel@0: gdImageColorResolveAlpha | Daniel@0: gdImageColorTransparent | Daniel@0: gdImageCopy | Daniel@0: gdImageCopyMerge | Daniel@0: gdImageMergeGray | Daniel@0: gdImageCopyResized | Daniel@0: gdImageCopyResampled | Daniel@0: gdImageCopyRotated | Daniel@0: gdImageCreate | Daniel@0: gdImageCreatePalette | Daniel@0: gdImageCreateTrueColor | Daniel@0: gdImageCreateFromGd | Daniel@0: gdImageCreateFromGdCtx | Daniel@0: gdImageCreateFromGdPtr | Daniel@0: gdImageCreateFromGd2 | Daniel@0: gdImageCreateFromGd2Ctx | Daniel@0: gdImageCreateFromGd2Ptr | Daniel@0: gdImageCreateFromGd2Part | Daniel@0: gdImageCreateFromGd2PartCtx | Daniel@0: gdImageCreateFromGd2PartPtr | Daniel@0: gdImageCreateFromJpeg | Daniel@0: gdImageCreateFromJpegCtx | Daniel@0: gdImageCreateFromJpegPtr | Daniel@0: gdImageCreateFromPng | Daniel@0: gdImageCreateFromPngCtx | Daniel@0: gdImageCreateFromPngPtr | Daniel@0: gdImageCreateFromPngSource | Daniel@0: gdImageCreateFromWBMP | Daniel@0: gdImageCreateFromWBMPCtx | Daniel@0: gdImageCreateFromWBMPPtr | Daniel@0: gdImageCreateFromXbm | Daniel@0: gdImageCreateFromXpm | Daniel@0: gdImageDashedLine | Daniel@0: gdImageDestroy | Daniel@0: gdImageFill | Daniel@0: gdImageFilledArc | Daniel@0: gdImageFilledEllipse | Daniel@0: gdImageFillToBorder | Daniel@0: gdImageFilledRectangle | Daniel@0: gdImageGd | Daniel@0: gdImageGd2 | Daniel@0: gdImageGetInterlaced | Daniel@0: gdImageGetPixel | Daniel@0: gdImageGetTransparent | Daniel@0: gdImageGifAnimAdd | Daniel@0: gdImageGifAnimAddCtx | Daniel@0: gdImageGifAnimAddPtr | Daniel@0: gdImageGifAnimBegin | Daniel@0: gdImageGifAnimBeginCtx | Daniel@0: gdImageGifAnimBeginPtr | Daniel@0: gdImageGifAnimEnd | Daniel@0: gdImageGifAnimEndCtx | Daniel@0: gdImageGifAnimEndPtr | Daniel@0: gdImageGreen | Daniel@0: gdImageInterlace | Daniel@0: gdImageJpeg | Daniel@0: gdImageJpegCtx | Daniel@0: gdImageLine | Daniel@0: gdImageFilledPolygon | Daniel@0: gdImageOpenPolygon | Daniel@0: gdImagePaletteCopy | Daniel@0: gdImagePng | Daniel@0: gdImagePngEx | Daniel@0: gdImagePngCtx | Daniel@0: gdImagePngCtxEx | Daniel@0: gdImagePngPtr | Daniel@0: gdImagePngPtrEx | Daniel@0: gdImagePngToSink | Daniel@0: gdImagePolygon | Daniel@0: gdImagePtr | Daniel@0: gdImageWBMP | Daniel@0: gdImageWBMPCtx | Daniel@0: gdImageRectangle | Daniel@0: gdImageRed | Daniel@0: gdImageSaveAlpha | Daniel@0: gdImageSetAntiAliased | Daniel@0: gdImageSetAntiAliasedDontBlend | Daniel@0: gdImageSetBrush | Daniel@0: gdImageSetPixel | Daniel@0: gdImageSetStyle | Daniel@0: gdImageSetThickness | Daniel@0: gdImageSetTile | Daniel@0: gdImageSharpen | Daniel@0: gdImageSquareToCircle | Daniel@0: gdImageString | Daniel@0: gdImageString16 | Daniel@0: gdImageStringFT | Daniel@0: gdImageStringFTCircle | Daniel@0: gdImageStringFTEx | Daniel@0: gdImageStringTTF | Daniel@0: gdImageStringUp | Daniel@0: gdImageStringUp16 | Daniel@0: gdImageToPalette | Daniel@0: gdImageWBMP | Daniel@0: gdMaxColors | Daniel@0: gdPoint | Daniel@0: gdStyled | Daniel@0: gdStyledBrushed | Daniel@0: gdTiled | Daniel@0: gdTransparent Daniel@0:

Daniel@0: Daniel@0: www.libgd.org Daniel@0: Daniel@0: