• No results found

The Chart::* modules

In document Graphics Programming With Perl (Page 51-55)

tools and modules

3.2.1 The Chart::* modules

These modules provide an object-oriented interface to create many different types of charts. The interface originally borrowed a lot of the ideas from GIFgraph but it has since gone its own way, and provides different sets of functionality. The modules were originally created by David Bonner, and are maintained by Peter Clark.

A full set of documentation comes with the modules, as a single manual page.

Examples of the use of these modules can be found in section 5.2, “The Chart distri-bution,” on page 74.

3.2.2 Gnuplot

Gnuplot is a program to create charts and graphs. It supports a wide variety of chart and data types, including three-dimensional surface plots. The program can be used interactively, and comes with a reasonable on-line help system. The documentation may be installed separately, or downloaded from the gnuplot web page (see [18]). Note that the gnu in gnuplot has nothing to do with the GNU Free Software Foundation.

Chart::GnuPlot provides an interface to gnuplot, but is still in a very early stage, and not ready for production use. Section 5.4, “Interfacing with gnuplot,” on page 85 shows some examples of the use of gnuplot from Perl. These examples have been tested with gnuplot version 3.7.

3.2.3 GD

GD is written and maintained by Lincoln Stein and provides an object-oriented interface to Thomas Boutell’s C library libgd. It offers a simple, but reasonably pow-erful and fast, drawing interface for bitmaps. Recent versions of GD can work with PNG, JPEG, XBM, WBMP (Windows bitmap), and a native file format. Older ver-sions (before 1.20) used to work with the GIF format, which explains why the inter-nal GD image format has much in common with GIF.

You will find several references in this book to the shortcomings of GD where color is concerned; as such, GD only supports palettes of up to 256 colors. However, since it is an interface to libgd, and a new version of libgd that supports true color images currently is in beta, I suspect that the next major release of GD will do this as well.

This new version most likely will be released with a version number of 2.0.

GD comes with an excellent manual page, and examples of its use can be found in many places in this book. The code there has been tested with GD version 1.33.

3.2.4 GD::Graph, GIFgraph and Chart::PNGgraph

These modules all use GD to create charts of various kinds and require the GDText-Utils to work. GIFgraph and GD::Graph were written and are maintained by the author. Chart::PNGgraph was created by Steve Bonds, based on the code base of GIF-graph, when GIF support was dropped from GD. Both GIFgraph and Chart::PNG-graph exist nowadays only to provide backward compatible name space wrappers around GD::Graph directly.

GD::Graph comes with a set of reasonably complete documentation, and examples of its use can be found in section 5.1, “GD::Graph and friends,” on page 62, section 6.3,

“CGI and dynamically generated graphics,” on page 94 and 7.6, “Animated charts,” on page 132. The code in this book has been tested with GD::Graph version 1.33.

3.2.5 GD::Text

The GD::Text module and its cousins GD::Text::Align and GD::Text::Wrap provide a generic interface to string handling and font management for the GD module. They were created during the writing of chapter 11 of this book by the author, and made available from CPAN under the name GDTextUtils. A full set of documentation is included in the modules.

Code that uses the GD::Text modules can be found in section 11.2.1, “Aligning text with GD,” on page 194, and has been tested with GD::Text version 0.80.

3.2.6 The Gimp

The GNU Image Manipulation Program [19] has almost the same application domain as Adobe Photoshop,2 almost the same capabilities, but a much more attrac-tive price; it’s free. The Gimp itself is an interacattrac-tive program, but has a readily accessi-ble extension interface, and tasks can be automated in the Gimp by using the Gimp and Gimp::Fu Perl modules. These modules can be used to write command line pro-grams, as well as plug-in filters and programs for the Gimp. The Gimp Perl modules can be complex, and require a working knowledge of the program and the way it stores and manipulates images, layers and selections.

The Gimp is available from http://www.gimp.org/ and the distribution includes the Perl modules. The module is only sparsely used in this book, but the program is definitely a must for anyone serious about working with image files on Unix platforms.

3.2.7 Image::Magick

Image::Magick is an object-oriented interface to ImageMagick, which is an extremely versatile and powerful image manipulation tool that works with a wide range of

2 Photoshop is generally better suited for images that are destined for print.

image formats and offers an extensive set of operations. ImageMagick and its com-panion Perl module are written and maintained by John Cristy with the assistance of other developers.

The module is available as part of the ImageMagick distribution under the name PerlMagick, as well as separately from CPAN. If you download the module from CPAN, be sure its version number matches the installation of the ImageMagick library.

Binary distributions for several platforms (including Microsoft Windows) are available from the ImageMagick web pages (see [17]); however, if you can, compile it yourself.

ImageMagick uses a multitude of external libraries and programs, and the more it is tuned to your system, the better.

Appendix A contains an introduction to and a reference for Image::Magick. Exam-ples using the modules are presented throughout the book.

The code in this book should run with Image::Magick version 5.4.4. Older versions might lack some of the presented functionality, and some support for old methods or arguments might have been dropped or changed (see also section A.3 on page 245).

Wherever possible, notes have been provided to warn the reader if this is the case. John Cristy is aware of this book, and has promised to keep new versions of Image::Magick as compatible with the presented code as possible.

3.2.8 Image::Size

This lightweight Perl module is intended mainly to be used in determining the size of image files. It will read various file formats, such as GIF, JPEG, XBM, XPM, PPM/ PGM, PBM, PNG, TIFF, SWF, PSD and BMP. Its main subroutine, imgsize() returns the width, height, and image type as a list. You can pass it either a string con-taining a file name, a reference to a file handle, or a reference to a scalar concon-taining the image data. The other two subroutines are merely convenience wrappers around imgsize(). The module is maintained by Randy Ray, and is based on code by Alex Knowles and Andrew Tong.

The module comes with a helpful documentation page, available through the perldoc command, and examples of its use can be found in section 2.2, “Finding the size and type of an image,” on page 20. Image::Size version 2.93 was used to test this code.

3.2.9 Inline

The Inline modules allow a programmer to enclose non-Perl source code in a Perl program. The modules make sure that code gets compiled and loaded when needed.

When the code is compiled, a cached copy of the object file is written to a (config-urable) place, which means that it is compiled only once (unless you change the code). This helps to dramatically speed up subsequent executions. The most obvi-ous place to use the Inline modules is wherever Perl’s execution model is too slow to be useful.

The standard Inline distribution, written and maintained by Brian Ingerson, comes with the Inline::C, which lets you include C code in your Perl programs. CPAN

also has Inline modules for C++, Python and CPR. According to the documentation, modules for Java, Fortran, Ruby and Bash are in the making.

An example of the use of Inline::C is available in section 12.4, “Alpha channels and transparency,” on page 229. The code in this book was run with Inline version 0.43.

3.2.10 OpenGL

The OpenGL module is a Perl interface to the OpenGL programming interface, which in turn is a software interface to graphics hardware. OpenGL was developed by Silicon Graphics Inc. for their hardware and OS, but is now available for a wide range of platforms and is implemented on a large number of hardware graphics cards.

To use this module you must first have OpenGL installed on your system. Some operating systems come with the required libraries installed and available; with others you need to install them yourself. You can either buy a commercial implementation of the library, or download and install Mesa 3D (http://www.mesa3d.org/). This freely available, open source implementation of the OpenGL library is fully func-tional, and some unofficial support is even available from Silicon Graphics.

The OpenGL modules are available from CPAN. The latest version on CPAN is 0.5, which was placed there by Kenneth Albanowski, the original developer for the mod-ule. Ilya Zakharevich has added some portability code, fixed some problems and removed some bugs from this version; but these changes haven’t made it to CPAN yet.

At the time of this writing, the latest version of OpenGL is available from ftp://

ftp.math.ohio-state.edu/pub/users/ilya/perl/modules. At some point in the not too distant future these updated versions will probably be made accessible by CPAN. (See the note on page 156.)

Examples of the use of OpenGL modules can be found in section 9.1, “OpenGL,”

on page 154. Version 0.54 of the OpenGL module was used to test this code, running on the Mesa 3D libraries, version 3.4.

3.2.11 PGPLOT

This module provides an interface to the PGPLOT graphics library, which is very popular with astronomers and many other scientists. It creates charts and plots for all kinds of data, but with a certain emphasis on data sets that would very likely be found on an astronomer’s hard disk. The original PGPLOT library was written in FORTRAN by Tim Pearson but also comes with a C interface. The Perl module, writ-ten by Karl Glazebrook, is an almost direct mapping to this C interface, which in turn is an almost direct mapping to the FORTRAN library calls.

The documentation for the PGPLOT module from Perl explains only the calling conventions for all these functions, and refers the user to the original FORTRAN-based documentation available from the PGPLOT web pages (see [20]). In addition to the documentation, which is quite good, it is also advisable to browse through the exam-ples that come with the PGPLOT library distribution and the PGPLOT module.

More information on and examples of the use of PGPLOT can be found in section 5.3, “PGPLOT,” on page 76. The version used for these examples was 2.18, running on version 5.2 of the PGPLOT library.

3.2.12 RenderMan

The RenderMan module is an interface to Blue Moon Rendering Tools (http://

www.bmrt.org), and can be used to directly interact with BMRT programs, or create RenderMan-compliant output files. The BMRT were originally written and devel-oped by Larry Gritz, and development is now in the hands of Exluna.

The RenderMan API was developed by Pixar Animation Studios to provide a stan-dard, portable communication layer between 3D modeling programs and rendering programs. The API doesn’t concern itself with how the rendering is achieved, but only describes what gets rendered, and what its properties are. The rendering engine can choose any method it pleases to create computer graphics from the described scene.

To work with the output of this module, you will need a RenderMan-compliant renderer. The standard commercial one is Photorealistic RenderMan, Pixar’s own engine. If you don’t have a Hollywood-sized budget, the already mentioned BMRT is available for free for personal use, and for a small fee for commercial use.

The RenderMan module is further described in section 9.2 on page 164. The code in this section was tested against RenderMan 0.04 compiled with BMRT 2.5. The RenderMan module cannot be compiled against version 2.6 of BMRT without con-siderable work.

In document Graphics Programming With Perl (Page 51-55)