![]() |
![]() |
![]() |
![]() |
ImageMagick has a number of methods that allow you to read, manipulate, write, or display an image. These methods are accessible through the various tools or the object-oriented Perl interface, PerlMagick. However, you can also access the methods directly from your program through the Magick Application Programmer Interface. To invoke the methods, write your program in your favorite language while making calls to the Magick image methods and link with libMagick.a, libMagick.so, or Magick.dll depending on your system. MagickWand -- C API for ImageMagickThe Magick Wand API is a new high level interface to ImageMagick. The Wand API is recommended over the ImageMagick Core API below. A description of the methods in the MagickWand API are found here: Here is a example of a program that utilizes the MagickWand API, demo.c, that reads a GIF image, creates a thumbnail, and writes it to disk in the MIFF image format. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <wand/magick_wand.h> int main(int argc,char **argv) { #define ThrowWandException(wand) \ { \ char \ *description; \ \ ExceptionType \ severity; \ \ description=MagickGetException(wand,&severity); \ (void) fprintf(stderr,"%s %s %ld %s\n",GetMagickModule(),description); \ description=(char *) MagickRelinquishMemory(description); \ exit(-1); \ } MagickBooleanType status; MagickWand *magick_wand; /* Read an image. */ magick_wand=NewMagickWand(); status=MagickReadImage(magick_wand,"image.gif"); 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 as MIFF and destroy it. */ status=MagickWriteImages(magick_wand,"image.miff",True); if (status == MagickFalse) ThrowWandException(magick_wand); magick_wand=DestroyMagickWand(magick_wand); return(0); } To compile, type: cc `Wand-config --cflags --cppflags` demo.c `Wand-config --ldflags --libs` ImageMagick Core APIThe recommended MagickWand API is a wrapper around the lower level ImageMagick Core API. This API is only recommended for Wizards. A description of the methods in the ImageMagick Core API are found here:
Here is a example of a program that utilizes the ImageMagick Core API, demo.c, that reads a GIF image, creates a thumbnail, and writes it to disk in the MIFF image format. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <magick/api.h> int main(int argc,char **argv) { ExceptionInfo exception; Image *image, *images, *resize_image, *thumbnails; ImageInfo *image_info; /* Initialize the image info structure and read an image. */ InitializeMagick(*argv); GetExceptionInfo(&exception); image_info=CloneImageInfo((ImageInfo *) NULL); (void) strcpy(image_info->filename,"image.gif"); images=ReadImage(image_info,&exception); if (exception.severity != UndefinedException) CatchException(&exception); if (images == (Image *) NULL) exit(1); /* Turn the images into a thumbnail sequence. */ thumbnails=NewImageList(); while ((image=RemoveFirstImageFromList(&images)) != (Image *) NULL) { resize_image=ResizeImage(image,106,80,LanczosFilter,1.0,&exception); if (resize_image == (Image *) NULL) MagickError(exception.severity,exception.reason,exception.description); (void) AppendImageToList(&thumbnails,resize_image); DestroyImage(image); } /* Write the image as MIFF and destroy it. */ (void) strcpy(thumbnails->filename,"image.miff"); WriteImage(image_info,thumbnails); thumbnails=DestroyImageList(thumbnails); image_info=DestroyImageInfo(image_info); DestroyExceptionInfo(&exception); DestroyMagick(); return(0); } Now we need to compile. On Unix, the command would look something like this: setenv LD_LIBRARY_PATH /usr/local/lib gcc `Magick-config --cflags --cppflags` demo.c `Magick-config --ldflags --libs`Another example is smile.c. Compile and excute it to display a smiley face on your X server. Magick++Magick++ is the C++ API for ImageMagick. To compile a Magick++ program, use: c++ `Magick++-config --cxxflags --cppflags` demo.cpp `Magick++-config --ldflags --libs`
![]() |