gravity in operations

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

gravity in operations

Post by mkoppanen »


regarding:, is MagickSetImageGravity supposed to affect MagickCompositeImage?
Mikko Koppanen
My blog:
User avatar
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: gravity in operations

Post by magick »

Gravity is not used by the composite method. Instead the command-line parser utilizes gravity with the GravityAdjustGeometry() method:
  • GravityAdjustGeometry(image->columns,image->rows,image->gravity,&geometry);
    (void) CompositeImageChannel(image,channel,image->compose,composite_image,geometry.x,geometry.y);

Re: gravity in operations

Post by portante »

Can someone explain why only the GravityAdjustGeometry() method is called from the composite command-line parser?

This seems like a natural flow for the API (see the code below).

Thanks, -peter

Code: Select all

/* cc setb.c -I /opt/local/include/ImageMagick -L/opt/local/lib -l MagickWand -o setb */

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

#define LOG_MESSAGE(__type, __format, ...) do { fprintf(stderr, "[%06d] %s: " __format, getpid(), __type, ## __VA_ARGS__); fflush(stderr); } while (0)

#define WARN(__format, ...) LOG_MESSAGE("WARNING", __format, ## __VA_ARGS__)

#define ThrowWandException(wand)										\
  {																		\
    char *description;													\
    ExceptionType severity;												\
    description = MagickGetException(wand,&severity);					\
    WARN("MagickWandException: %s %s %ld %s\n", GetMagickModule(), description); \
    description = (char *) MagickRelinquishMemory(description);			\
  } while (0)

#define COND_WAND_EX(status, wand) \
  if (status == MagickFalse) { ThrowWandException(wand); return 1; }

#define ThrowPixelWandException(wand)									\
  do {																	\
    char *description;													\
    ExceptionType severity;												\
    description = PixelGetException(wand,&severity);					\
    WARN("PixelWandException: %s %s %ld %s\n", GetMagickModule(), description); \
    description = (char *) MagickRelinquishMemory(description);			\
  } while (0)

#define COND_PIXEL_EX(status, wand) \
  if (status == MagickFalse) { ThrowPixelWandException(wand); }

int main(int argc, char *argv[]) {

  PixelWand *background = NewPixelWand();
  MagickBooleanType status = PixelSetColor(background, "#ff00ff");
  COND_PIXEL_EX(status, background);

  MagickWand *magick_wand = NewMagickWand();
  if (magick_wand == NULL) {
	fprintf(stderr, "Failed to allocate a magick wand\n");
	return 1;

  status = MagickSetBackgroundColor(magick_wand, background);
  COND_WAND_EX(status, magick_wand);

  status = MagickReadImage(magick_wand, argv[1]);
  COND_WAND_EX(status, magick_wand);

  status = MagickSetImageGravity(magick_wand, CenterGravity);
  COND_WAND_EX(status, magick_wand);

  status = MagickResizeImage(magick_wand, 1650, 2150, BesselFilter, 1.0);
  COND_WAND_EX(status, magick_wand);

  unsigned long iw = MagickGetImageWidth(magick_wand);
  unsigned long ih = MagickGetImageHeight(magick_wand);

  printf("New size: %lu, %lu\n", iw, ih);

  # Here, the X,Y parameters could theoretically be dropped off if ExtentImage obeyed the image gravity.
  status = MagickExtentImage(magick_wand, 1750, 2250, 0, 0);
  COND_WAND_EX(status, magick_wand);

  status = MagickTransformImageColorspace(magick_wand, RGBColorspace);
  COND_WAND_EX(status, magick_wand);

  status = MagickStripImage(magick_wand);
  COND_WAND_EX(status, magick_wand);

  char *pidded_real_path = "./tmp-rgb-stripped.jpg";
  status = MagickWriteImage(magick_wand, pidded_real_path);
  COND_WAND_EX(status, magick_wand);

  return 0;
Post Reply