ImageMagick: MagickWand, C API for ImageMagick
Author: ImageMagick Studio LLC
Description: ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (about 100) including GIF, JPEG, JPEG-2000, PNG, PDF, PhotoCD, TIFF, and DPX. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves. ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you can freely use, copy, modify, and distribute. Its license is compatible with the GPL. It runs on all major operating systems. The functionality of ImageMagick is typically utilized from the command line or you can use the features from programs written in your favorite programming language. Choose from these interfaces: MagickCore (C), MagickWand (C), ChMagick (Ch), Magick++ (C++), JMagick (Java), L-Magick (Lisp), PascalMagick (Pascal), PerlMagick (Perl), MagickWand for PHP (P [cell cut: 1167 characters, first 1000 kept]
Keywords: magickwc, api, for, imagemagick, ImageMagick, ImageMagic, MagickCore, MagickWand, PerlMagick, Magick++, RMagick, PythonMagick, JMagick, TclMagick, Image, Magick, Magic, Wand, ImageMagickObject

The MagickWand API is the recommended interface between the C programming language and the ImageMagick image processing libraries. Unlike the MagickCore C API, MagickWand uses only a few opaque types. Accessors are available to set or get important wand properties. A description of the MagickWand public methods are found here: Magick Wand Methods Set or Get Magick Wand Properties Magick Wand Image Methods Pixel Iterator Methods Pixel View Methods Pixel Wand Methods Image Vector Drawing Deprecated Methods After you write your MagickWand program, compile it like this:
      cc `MagickWand-config --cflags --cppflags` -o wand wand.c `MagickWand-config --ldflags --libs`
Here is a example program that utilizes the MagickWand API to get you started, wand.c. It reads an image, creates a thumbnail, and writes the result to disk.
    #include <stdio.h>
    #include <stdlib.h>
    #include <wand/MagickWand.h>

    int main(int argc,char **argv)
    {
    #define ThrowWandException(wand) \
    { \
      char \
        *description; \
     \
      ExceptionType \
        severity; \
     \
      description=MagickGetException(wand,&severity); \
      (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
      description=(char *) MagickRelinquishMemory(description); \
      exit(-1); \
    }

      MagickBooleanType
        status;

      MagickWand
        *magick_wand;

      if (argc != 3)
        {
          (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
          exit(0);
        }
      /*
        Read an image.
      */
      MagickWandGenesis();
      magick_wand=NewMagickWand();
      status=MagickReadImage(magick_wand,argv[1]);
      if (status == MagickFalse)
        ThrowWandException(magick_wand);
      /*
        Turn the images into a thumbnail sequence.
      */
      MagickResetIterator(magick_wand);
      while (MagickNextImage(magick_wand) != MagickFalse)
        MagickResizeImage(magick_wand,106,80,LanczosFilter,1.0);
      /*
        Write the image then destroy it.
      */
      status=MagickWriteImages(magick_wand,argv[2],MagickTrue);
      if (status == MagickFalse)
        ThrowWandException(magick_wand);
      magick_wand=DestroyMagickWand(magick_wand);
      MagickWandTerminus();
      return(0);
    }
Here is another program that shows one way to get and set image pixels with the MagickWand API, contrast.c. It reads an image, applies sigmoidal non-linearity contrast control, and writes the result to disk.
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <wand/MagickWand.h>

    int main(int argc,char **argv)
    {
    #define QuantumScale  ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
    #define SigmoidalContrast(x) \
      (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
    #define ThrowWandException(wand) \
    { \
      char \
        *description; \
     \
      ExceptionType \
        severity; \
     \
      description=MagickGetException(wand,&severity); \
      (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
      description=(char *) MagickRelinquishMemory(description); \
      exit(-1); \
    }

      long
        y;

      MagickBooleanType
        status;

      MagickPixelPacket
        pixel;

      MagickWand
        *contrast_wand,
        *image_wand;

      PixelIterator
        *contrast_iterator,
        *iterator;

      PixelWand
        **contrast_pixels,
        **pixels;

      register long
        x;

      unsigned long
        width;

      if (argc != 3)
        {
          (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
          exit(0);
        }
      /*
        Read an image.
      */
      MagickWandGenesis();
      image_wand=NewMagickWand();
      status=MagickReadImage(image_wand,argv[1]);
      if (status == MagickFalse)
        ThrowWandException(image_wand);
      contrast_wand=CloneMagickWand(image_wand);
      /*
        Sigmoidal non-linearity contrast control.
      */
      iterator=NewPixelIterator(image_wand);
      contrast_iterator=NewPixelIterator(contrast_wand);
      if ((iterator == (PixelIterator *) NULL) ||
          (contrast_iterator == (PixelIterator *) NULL))
        ThrowWandException(image_wand);
      for (y=0; y < (long) MagickGetImageHeight(image_wand); y++)
      {
        pixels=PixelGetNextIteratorRow(iterator,&width);
        contrast_pixels=PixelGetNextIteratorRow(contrast_iterator,&width);
        if ((pixels == (PixelWand **) NULL) ||
            (contrast_pixels == (PixelWand **) NULL))
          break;
        for (x=0; x < (long) width; x++)
        {
          PixelGetMagickColor(pixels[x],&pixel);
          pixel.red=SigmoidalContrast(pixel.red);
          pixel.green=SigmoidalContrast(pixel.green);
          pixel.blue=SigmoidalContrast(pixel.blue);
          pixel.index=SigmoidalContrast(pixel.index);
          PixelSetMagickColor(contrast_pixels[x],&pixel);
        }
        (void) PixelSyncIterator(contrast_iterator);
      }
      if (y < (long) MagickGetImageHeight(image_wand))
        ThrowWandException(image_wand);
      contrast_iterator=DestroyPixelIterator(contrast_iterator);
      iterator=DestroyPixelIterator(iterator);
      image_wand=DestroyMagickWand(image_wand);
      /*
        Write the image then destroy it.
      */
      status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
      if (status == MagickFalse)
        ThrowWandException(image_wand);
      contrast_wand=DestroyMagickWand(contrast_wand);
      MagickWandTerminus();
      return(0);
    }
Now lets perform the same contrast enhancement while taking advantage of our dual or quad-core processing system by running the algorithm in parallel utilizing pixel views. The sigmoidal-contrast.c module reads an image, applies sigmoidal non-linearity contrast control, and writes the result to disk just like the previous contrast enhancement program, but now it does its work in parallel (assumes ImageMagick is built with OpenMP support).
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <wand/MagickWand.h>

    static MagickBooleanType SigmoidalContrast(PixelView *pixel_view,
      void *context)
    {
    #define QuantumScale  ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
    #define SigmoidalContrast(x) \
      (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)

      MagickPixelPacket
        pixel;

      PixelWand
        **pixels;

      register long
        x;

      pixels=GetPixelViewPixels(contrast_view);
      for (x=0; x < (long) GetPixelViewWidth(contrast_view); x++)
      {
        PixelGetMagickColor(pixels[x],&pixel);
        pixel.red=SigmoidalContrast(pixel.red);
        pixel.green=SigmoidalContrast(pixel.green);
        pixel.blue=SigmoidalContrast(pixel.blue);
        pixel.index=SigmoidalContrast(pixel.index);
        PixelSetMagickColor(contrast_pixels[x],&pixel);
      }
      return(MagickTrue);
    }

    int main(int argc,char **argv)
    {
    #define ThrowWandException(wand) \
    { \
      char \
        *description; \
     \
      ExceptionType \
        severity; \
     \
      description=MagickGetException(wand,&severity); \
      (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
      description=(char *) MagickRelinquishMemory(description); \
      exit(-1); \
    }

      MagickBooleanType
        status;

      MagickPixelPacket
        pixel;

      MagickWand
        *contrast_wand;

      PixelView
        *contrast_view;

      if (argc != 3)
        {
          (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
          exit(0);
        }
      /*
        Read an image.
      */
      MagickWandGenesis();
      contrast_wand=NewMagickWand();
      status=MagickReadImage(contrast_wand,argv[1]);
      if (status == MagickFalse)
        ThrowWandException(contrast_wand);
      /*
        Sigmoidal non-linearity contrast control.
      */
      contrast_view=NewPixelView(contrast_wand);
      if (contrast_view == (PixelView *) NULL)
        ThrowWandException(contrast_wand);
      status=UpdatePixelViewIterator(contrast_view,SigmoidalContrast,(void *) NULL);
      if (status == MagickFalse)
        ThrowWandException(contrast_wand);
      contrast_view=DestroyPixelView(contrast_view);
      /*
        Write the image then destroy it.
      */
      status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
      if (status == MagickFalse)
        ThrowWandException(contrast_wand);
      contrast_wand=DestroyMagickWand(contrast_wand);
      MagickWandTerminus();
      return(0);
    }

Discourse Server • Mailing Lists • Cafe • Studio