#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <wand/magick-wand.h>

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

int main(int argc, char **argv)
{
  MagickBooleanType status;
  MagickWand *wand;
  PixelIterator *iterator;
  PixelWand **pixels;
  unsigned long width, height, x, y;

  /* read an image */
  MagickWandGenesis();
  wand = NewMagickWand();
  status = MagickReadImage(wand, "in.png");
  if (status == MagickFalse)
    ThrowWandException(wand);

  width = 50;
  height = 10;
  x = y = 0;
  MagickCropImage(wand, width, height, x, y);

  /* loop over pixels */
  iterator = NewPixelIterator(wand);
  if ((iterator == (PixelIterator *) NULL))
    ThrowWandException(wand);
  for (y=0; y<MagickGetImageHeight(wand); y++)
    {
      pixels = PixelGetNextIteratorRow(iterator, &width);
      if ((pixels == (PixelWand **) NULL))
	break;
      printf("width=%lu\n", width);
    }
  if (y < MagickGetImageHeight(wand))
    ThrowWandException(wand);

  /* clean up */
  iterator = DestroyPixelIterator(iterator);
  wand = DestroyMagickWand(wand);
  MagickWandTerminus();

  return 0;
}
