GraphicsMagick: Image Processing Add-In as Easy as Abra Cadabra!

Do You Believe in Magick?

Sooner or later, virtually every app needs to do some on-the-fly image processing. You know, those annoying chores that you could theoretically do by hand in Adobe Photoshop or other equivalent app: Scale an input file so it will look nice on the web, recompress a set of JPEGs that is just too fat, or convert from some obscure file format to TIFF or PNG. When it’s time to do real image processing, get a really useful library like GraphicsMagick to do the work for you.

GraphicsMagick aims to provide one-stop-shopping with a robust collection of tools and libraries that support reading, writing, and manipulating an image in over 88 major formats, including important formats such as DPX, GIF, JPEG, JPEG-2000, PNG, PDF, SVG, and TIFF. GraphicsMagick supports huge images on systems that support files over 2GB and images well into the gigapixel range. GraphicsMagick can create new images from scratch or take an existing image and resize, rotate, sharpen, color reduce, or add special effects. When you’re done, it will save it out to any supported format. GraphicsMagick is accessible from C/C++, Perl, Tcl, Ruby, or .NET interfaces. If you’re a batchfile wiz, you can set up most operations thru the command line interface on any version of Windows or Unix.

If GraphicsMagick sounds vaguely familiar, you may have seen its ancestor ImageMagick 5.5.2. The good news is that GraphicsMagick is available free for use in both open and proprietary applications, and may be redistributed without fee. Its X11-style (“MIT”) license is approved by the Open Source Initiative and declared by the FSF to be compatible with the GPL Version 2.

I could probably spend all day enumerating the features, but here’s the short list that I call the “top ten reasons to use GraphicsMagick”:

  • Convert an image from one format to another (for example, from TIFF to JPEG)
  • Resize, rotate, sharpen, color reduce, or add special effects to an image
  • Create a montage of image thumbnails
  • Deep pixel support with 24-bit, 32-bit, 48-bit, and 64-bit color images
  • Turn a group of images into a GIF animation sequence
  • Create a composite image by combining several separate images
  • Draw shapes or text on an image
  • Decorate an image with a border or frame
  • Describe the format and characteristics of an image
  • Best TIFF variant support in the industry

Getting Started with GraphicsMagick

GraphicsMagick lets you choose your own level of integration for C/C++ developers: both static library and DLL (import library) options are readily available at the Windows binary FTP site. Additionally, you can choose pre-compiled binaries for 24-bit/32-bit color or 48-bit/64-bit color support. If you’re not doing scientific, medical, or cinematic image processing, you’ll find the 24-bit/32-bit color is plenty good enough. The precompiled binaries don’t contain any source code, programming API documentation, or header files, so be sure to pick up the full source ZIP as well and unzip it somewhere handy.

GraphicsMagick for Windows v1.2.5 has a professional installer with options to associate file extensions, install the Perl modules for ActiveState Perl, and the GraphicsMagickObject OLE control for .NET access thru VBscript, VB, WSH, and other scripting systems. If you are using any of these non-C/C++ interfaces, you can skip downloading the source tree unless you are curious.

Before you can use ImageMagic with C/C++ , you will need to rebuild the configure.exe program that builds the magick_config.h file. This file provides specific information on what packages are integrated with your VisualMagick system as well as handling anything specific to which version of Visual Studio that you are going to be using. Windows users will want to leave in support for all optional modules, I think (for example, PNG file support, and so forth).

The configure.exe file also produces a Solution (.SLN) file for building GraphicsMagick using the style of library that you specifically want: static library, dynamic library, and threading options. Unfortunately, configure.exe crashed when I ran it but I was able to comment out a debugging “cout” and then it finished making the SLN files.

Figure 1: Configuring GraphicsMagick for the build you want to use

Then of course, you need to actually build the actual demo program.

A Simple Demo Program

Last, look at a small demo program that will read in a .GIF file and crop it to 100×100 pixels at (100,100) from the origin. Then, it will write out a new GIF file with the cropped image. In the documentation, using the C++ object-oriented interface is called Magick++ for some reason.

#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
   // Construct the image object. Separating image construction
   // from the read operation ensures that a failure to read the
   // image file doesn't render the image object useless.

Image image; try { // Read a file into image object image.read( "girl.gif" ); // Crop the image to specified size (width, height, // xOffset, yOffset) image.crop( Geometry(100,100, 100, 100) ); // Write the image to a file image.write( "x.gif" ); } catch( Exception &error_ ) { cout << "Caught exception: " << error_.what() << endl; return 1; } return 0; }

The basic idea is that an Image type object has lots of operators to do image processing with. This example only used the read(), crop(), and write() methods but of course there are dozens more. These methods mimic what you can do in Adobe Photoshop and most popular photo/image manipulation programs including shear(), solarize(), swirl(), transform(), unsharpmask(), rotate(), scale(), quantize(), magnify(), zoom(), and everything else you could possibly need. You can even burn text into the images with annotate().

Here’s another example that creates a 100×100 image on the fly, plunks a red pixel in the center, and dumps it out to a PNG file.

#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
   Image image( "100x100", "white" );
   image.pixelColor( 49, 49, "red" );
   image.write( "red_pixel.png" );
   return 0;
}

Magick++ provides integrated support for the Standard Template Library (STL) so that the powerful containers available (such as deque, vector, list, and map) can be used to write programs similar to those possible with PERL & PerlMagick. STL-compatible template versions of GraphicsMagick’s list-style operations are provided so that operations may be performed on multiple images stored in STL containers.

Conclusion

Magick++ includes everything you can possibly want to automate image processing whether you are writing a program that produces web graphics on-the-fly or the next great application to clean and process digital photos. Your imagination is the only limit to what you can do with GraphicsMagick.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for the Washtenaw Community College CIS department. Volkman is the editor of numerous books including, C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an email to sysop@HAL9K.com.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read