GraphicsMagick

- Home
- Project
- Download
- Install
- Source
- News
- Utilities
- Programming
- Reference

# Magick++ API for GraphicsMagick

Contents

- Introduction

- API

- Obtaining Magick++

- Installation

- Usage

- Reporting Bugs

# Introduction

Magick++ is the object-oriented C++ API to the GraphicsMagick image-processing library, the most comprehensive open-source image processing package available. Read the ChangeLog for Magick++.

Magick++ supports an object model which is inspired by PerlMagick. Images support implicit reference counting so that copy constructors and assignment incur almost no cost. The cost of actually copying an image (if necessary) is done just before modification and this copy is managed automatically by Magick++. De-referenced copies are automatically deleted. The image objects support value (rather than pointer) semantics so it is trivial to support multiple generations of an image in memory at one time.

Magick++ provides integrated support for the Standard Template Library (STL) which is part of the standard C++ language so that the powerful containers available (e.g. 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.

# API

Magick++ provides a simple C++ API to the GraphicsMagick image processing library which supports reading and writing a huge number of image formats as well as supporting a broad spectrum of traditional image processing operations. The GraphicsMagick C API is complex and the data structures are not well documented. Magick++ provides access to most of the features available from the C API but in a simple object-oriented and well-documented framework.

Magick++ is intended to support commercial-grade application development. In order to avoid possible conflicts with the user's application, all symbols contained in Magick++ (included by the header <Magick++.h>) are scoped to the namespace Magick. Symbols from the GraphicsMagick C library are imported under the MagickLib namespace to avoid possible conflicts and GraphicsMagick macros are only included within the Magick++ implementation so they won't impact the user's application.

The InitializeMagick() function MUST be invoked before constructing any Magick++ objects. This used to be optional, but now it is absolutely required. This function initalizes semaphores and configuration information necessary for the software to work correctly. Failing to invoke InitializeMagick() is likely to lead to a program crash or thrown assertion. If the program resides in the same directory as the GraphicsMagick files, then argv[0] may be passed as an argument so that GraphicsMagick knows where its files reside, otherwise NULL may be passed and GraphicsMagick will try to use other means (if necessary).

The core class in Magick++ is the Image class. The Image class provides methods to manipulate a single image frame (e.g. a JPEG image). Standard Template Library (STL) compatible algorithms and function objects are provided in order to manipulate multiple image frames or to read and write file formats which support multiple image frames (e.g. GIF animations, MPEG animations, and Postscript files).

The Image class supports reference-counted memory management which supports the semantics of an intrinsic variable type (e.g. 'int') with an extremely efficient operator = and copy constructor (only a pointer is assigned) while ensuring that the image data is replicated as required so that it the image may be modified without impacting earlier generations. Since the Image class manages heap memory internally, images are best allocated via C++ automatic (stack-based) memory allocation. This support allows most programs using Magick++ to be written without using any pointers, simplifying the implementation and avoiding the risks of using pointers. When a program uses automatic memory allocation to allocate Magick++ images, that aspect of the program becomes naturally exception-safe and thread-safe.

The image class uses a number of supportive classes in order to specify arguments. Colors are specified via the Color class. Colors specified in X11-style string form are implicitly converted to the Color class. Geometry arguments (those specifying width, height, and/or x and y offset) are specified via the Geometry class. Similar to the Color class, geometries specified as an X11-style string are implicitly converted to the Geometry class. Two dimensional drawable objects are specified via the Drawable class. Drawable objects may be provided as a single object or as a list of objects to be rendered using the current image options. Montage options (a montage is a rendered grid of thumbnails in one image) are specified via the Montage class.

Errors are reported using C++ exceptions derived from the Exception class, which is itself derived from the standard C++ exception class. Exceptions are reported synchronous with the operation and are caught by the first matching try block as the stack is unraveled. This allows a clean coding style in which multiple related Magick++ commands may be executed with errors handled as a unit rather than line-by-line. Since the Image object provides reference-counted memory management, unreferenced images on the stack are automatically cleaned up, avoiding the potential for memory leaks.

For ease of access, the documentation for the available user-level classes is available via the following table:

Magick++ User-Level Classes
Blob | Binary Large OBject container.
CoderInfo | Report information about supported image formats (use with coderInfoList())
Color | Color specification.
Drawable | Drawable shape (for input to 'draw').
Exception | C++ exception objects.
Geometry | Geometry specification.
Image | Image frame. This is the primary object in Magick++.
Montage | Montage options for montageImages().
Pixels | Low-level access to image pixels.
STL | STL algorithms and function objects for operating on containers of image frames.
TypeMetric | Container for font type metrics (use with Image::fontTypeMetrics).

# Obtaining Magick++

Magick++ is included as part of GraphicsMagick source releases and may be retrieved via ftp or Mercurial.

# Installation

Installation is very easy since Magick++ is part of GraphicsMagick and is built by default. Once GraphicsMagick is built, then Magick++ is available for use.

# Usage

A helper script named GraphicsMagick++-config is installed in the same directory as the GraphicsMagick gm program under Unix which assists with recalling compilation options required to compile and link programs which depend on Magick++. For example, the following command will compile and link the source file example.cpp to produce the executable example (notice that quotes are backward quotes):

    c++ -o example example.cpp `GraphicsMagick++-config --cppflags --cxxflags
      --ldflags --libs`

Windows users may get started by manually editing a project file for one of the Magick++ demo programs.

It is necessary to initialize the GraphicsMagick library prior to using the Magick++ library. This initialization is performed by passing the path to the GraphicsMagick DLLs (assumed to be in the same directory as your program) to the InitializeMagick() function call. This is commonly performed by providing the path to your program (argv[0]) as shown in the following example:

    int main( int /*argc*/, char ** argv)
    {
      InitializeMagick(*argv);

If you don't have the path to your executable, then pass NULL and usually the library will be found anyway. Besides helping to find the GraphicsMagick DLL/library and configuration files, InitializeMagick() initializes all of the semaphores and data necessary for a multi-threaded program to be completely thread safe. This step used to be optional, but it is now absolutely required. Failure to initialize GraphicsMagick will result in an application crash.

# Reporting Bugs

Please report any bugs via the GraphicsMagick Bug Tracker. Questions regarding usage should be directed to Bob Friesenhahn.

Copyright © Bob Friesenhahn 1999 - 2022

Copyright © GraphicsMagick Group 2002 - 2024