ImageMagick: Porting to ImageMagick Version 7

Unix
Mac OS X
iOS
Windows
Processing
Options
Usage
MagickWand
MagickCore
PerlMagick
Magick++
Unix
Windows
Links

# ImageMagick Version 7 Porting Guide

The design of ImageMagick is an evolutionary process, with the design and implementation efforts serving to influence and guide further progress in the other. With ImageMagick version 7 we aim to improve the design based on lessons learned from the version 6 implementation. ImageMagick was originally designed to display RGB images to an X Windows server. Over time we extended support to RGBA images and then to the CMYK and CMYKA image format. With ImageMagick version 7, we extend support to arbitrary colorspaces with an arbitrary number of pixel channels. Other design changes are in the works and we will document them here so be sure to revisit periodically.

To support variable pixel channels in the MagickCore API, pixel handling has changed and now requires accessors to get or set the pixel components. There are some modest changes to the MagickWand API. Magick++ and PerlMagick should behave exactly as it does for ImageMagick version 6.

We intend to make ImageMagick version 7 available as an Alpha release by the end-of-year 2011. Look for a Beta release sometime in 2012. An official ImageMagick version 7 release will depend on how smoothly the Beta cycle progresses. During the Beta cycle, version 6 developers can attempt to port their software to version 7.

During the ImageMagick version 7 development cycle and release, we will continue to support and enhance version 6 for a minimum of 10 years.

## Header Files

Prior versions of ImageMagick (4-6) references the ImageMagick header files as `magick/` and `wand/`. ImageMagick 7 instead uses `MagickCore/` and `MagickWand/` respectively. For example,

    #include <MagickCore/MagickCore.h>
    #include <MagickWand/MagickWand.h>

## Pixel Channels

Prior versions of ImageMagick (4-6), supports 4 to 5 pixel channels (RGBA or CMYKA). The first 4 channels are accessed with the PixelPacket data structure. The structure includes 4 members of type Quantum (typically 16-bits) of red, green, blue, and opacity. The black channel or colormap indexes are supported by a separate method and structure, IndexPacket. As an example, here is a code snippet from ImageMagick version 6 that negates image pixels:

      for (y=0; y < (ssize_t) image->rows; y++)
      {
        register IndexPacket
          *indexes;

        register PixelPacket
          *q;

        q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
        if (q == (PixelPacket *) NULL)
          {
            status=MagickFalse;
            continue;
          }
        indexes=GetCacheViewAuthenticIndexQueue(image_view);
        for (x=0; x < (ssize_t) image->columns; x++)
        {
          if ((channels & RedChannel) != 0)
            q->red=(Quantum) QuantumRange-q->red;
          if ((channels & GreenChannel) != 0)
            q->green=(Quantum) QuantumRange-q->green;
          if ((channels & BlueChannel) != 0)
            q->blue=(Quantum) QuantumRange-q->blue;
          if ((channels & OpacityChannel) != 0)
            q->opacity=(Quantum) QuantumRange-q->opacity;
          if (((channels & IndexChannel) != 0) &&
              (image->colorspace == CMYKColorspace))
            indexes[x]=(IndexPacket) QuantumRange-indexes[x];
          q++;
        }
        if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
          status=MagickFalse;
      }

ImageMagick version 7 supports any number of channels from 1 to 100 (and beyond) and simplifies access with a single method that returns an array of pixel channels of type Quantum. Source code that compiles against prior versions of ImageMagick will require refactoring to work with ImageMagick version 7. We illustrate with an example. Let's naively refactor the version 6 code snippet from above so it works with the ImageMagick version 7 API:

      for (y=0; y < (ssize_t) image->rows; y++)
      {
        register Quantum
          *q;

        q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
        if (q == (Quantum *) NULL)
          {
            status=MagickFalse;
            continue;
          }
        for (x=0; x < (ssize_t) image->columns; x++)
        {
          if ((channels & RedChannel) != 0)
            SetPixelRed(image,QuantumRange-GetPixelRed(image,q),q);
          if ((channels & GreenChannel) != 0)
            SetPixelGreen(image,QuantumRange-GetPixelGreen(image,q),q);
          if ((channels & BlueChannel) != 0)
            SetPixelBlue(image,QuantumRange-GetPixelBlue(image,q),q);
          if (((channels & IndexChannel) != 0) &&
              (image->colorspace == CMYKColorspace))
            SetPixelBlack(image,QuantumRange-GetPixelBlack(image,q),q);
          if ((channels & OpacityChannel) != 0)
            SetPixelAlpha(image,QuantumRange-GetPixelAlpha(image,q),q);
          q+=GetImagePixelChannels(image);
        }
        if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
          status=MagickFalse;
      }

Let's do that again but take full advantage of the new variable pixel channel support:

      for (y=0; y < (ssize_t) image->rows; y++)
      {
        register Quantum
          *q;

        q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
        if (q == (Quantum *) NULL)
          {
            status=MagickFalse;
            continue;
          }
        for (x=0; x < (ssize_t) image->columns; x++)
          for (channel=0; channel < GetImagePixelChannels(image); channel++)
          {
            if ((channels & (1 << channel)) != 0)
              *q=(Quantum) QuantumRange-(*q);
            q++;
          }
        if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
          status=MagickFalse;
      }

Use GetImagePixelChannels() to advance to the next set of pixel channels.

#### Pixel Accessors

Use accessors to get or set pixel components (e.g. GetPixelRed(), SetPixelRed()). You can find these accessors defined in the header file, `MagickCore/pixel-accessor.h`

- The colormap indexes and black pixel component (for the CMYK colorspace) are no longer stored in the index channel, previously accessed with GetAuthenticIndexQueue() and GetCacheViewAuthenticIndexQueue((). Instead they are now a pixel channel and accessed with the convenience pixel macros GetPixelIndex(), SetPixelIndex(), GetPixelBlack(), and SetPixelBlack().

In addition to supporting any number of channels, version 7 simplifies working with channels and provides opportunity for compiler optimiziations that were previously not possible. Our benchmarking shows version 7 has increased performance for virtually all image operations.

#### Associated Pixel Channels

In version 7, we introduce associated pixel channels. These pixel channels are for your exclusive use and are accessed with these MagickCore API methods:

- SetImageAssociatedPixelChannels()
- GetImageAssociatedPixelChannels()
- GetVirtualAssociatedPixels
- GetAuthenticAssociatedPixels
- GetCacheViewAuthenticAssociatedPixels
- GetCacheViewVirtualAssociatedPixels

## Alpha

We support alpha now, previously opacity. With alpha, a value of 0 means that the pixel does not have any coverage information and is transparent; i.e. there was no color contribution from any geometry because the geometry did not overlap this pixel. A value of QuantumRange means that the pixel is opaque because the geometry completely overlapped the pixel. As a consequence, in version 7, the PixelPacket structure member alpha has replaced the previous opacity member.

## Deprecated Features Removed

All deprecated features from ImageMagick version 6 are removed in version 7. These include the `Magick-config` and `Wand-config` configuration utilities. Instead use:

      MagickCore-config
      MagickWand-config

In addition, all deprecated MagickCore and MagickWand methods are no longer available in version 7.

## Version 7 Change Summary

Changes from ImageMagick version 6 to version 7 are summarized here:

##### Pixels

- Pixels are no longer addressed with PixelPacket structure members (e.g. red, green, blue, opacity) but as an array of channels (e.g. p[PixelRed]).
- Use convenience macros to access pixel components (e.g. GetPixelRed(), SetPixelRed()).
- The black channel for the CMYK colorspace is no longer stored in the index channel, previously accessed with GetAuthenticIndexQueue() and GetCacheViewAuthenticIndexQueue((). Instead its now a pixel channel and accessed with the convenience pixel macros GetPixelBlack() and SetPixelBlack().
- Use the index channel to address colormapped indexes. It no longer hosts the black channel for CMYK and CMYKA images.
- Use GetImagePixelChannels() to advance to the next set of pixel channels.
- Use associated pixel channels for your own use.

##### Alpha

- We support alpha rather than opacity (0 transparent; QuantumRange opaque).
- Use GetPixelAlpha() or SetPixelAlpha() to get or set the alpha pixel component value.

##### Deprecated Methods

- All ImageMagick version 6 MagickCore and MagickWand deprecated methods are removed and no longer available in ImageMagick version 7.

Discourse Server • Studio