Test ImageMagik-development

Source: Internet
Author: User
Tags imagemagick

ImageMagick usage and Development articles:

Test ImageMagik.

Test ImageMagik-development

======================================


This article describes how to use ImageMagick to develop a program. After ImageMagick is installed, it supports C/C ++ program development and provides three interfaces. First, we will introduce three ImageMagick interfaces.

MagickCore:

The underlying C language interface. Complex, but many parameters can be modified, which is only suitable for high-end users.

MagickWand:

Recommended C language interface. Compared with the MagickCore interface, it is much simpler. Suitable for common users.

Magick ++:

Provides object-oriented C ++ interfaces.

 

Next we will review the ImageMagick directory after installation:


Several development-related files:

Lib Folder:Static library files required for development. It contains four libraries and the first three corresponding ImageMagick APIs:

CORE_RL_magick _. lib; CORE_RL_Magick ++ _. lib; CORE_RL_wand _. lib; X11.lib;

 

Include Folder:Header files required for development. It contains three folders and corresponds to three ImageMagick interfaces:

Magick; Magick ++; wand;

 

*. Dll:Dynamic link library files to be used during development and use.

 

In development, we need three types of files: header files (*. h), static library files (*. lib), and dynamic library files (*. dll ). Therefore, create a new project in VC, and copy the Lib folder, Include folder, and dll to the project directory, and configure the path of the header file and static library.

 

The following are examples of the three ImageMagick interfaces.

MagickCore(Underlying C language interface. Complicated, but many parameters can be modified, which is only suitable for high-end users)

Function: Read files, create thumbnails, and save them as files.

/** Lei Xiaohua * leixiaohua1020@126.com * http://blog.csdn.net/leixiaohua1020 * China Communications University/Digital TV technology */# include <stdio. h> # include <stdlib. h> # include <string. h> # include <time. h> # include <magick/MagickCore. h> int main (int argc, char ** argv) {ExceptionInfo * exception; Image * image, * images, * resize_image, * thumbnails; ImageInfo * image_info; if (argc! = 3) {(void) fprintf (stdout, "Usage: % s image thumbnail \ n", argv [0]); exit (0 );} /* Initialize the image info structure and read an image. */MagickCoreGenesis (* argv, MagickTrue); exception = AcquireExceptionInfo (); image_info = CloneImageInfo (ImageInfo *) NULL); (void) strcpy (image_info-> filename, argv [1]); images = ReadImage (image_info, exception); if (exception-> severity! = UndefinedException) CatchException (exception); if (images = (Image *) NULL) exit (1);/* Convert the image to a thumbnail. */thumbnails = NewImageList (); while (image = RemoveFirstImageFromList (& images ))! = (Image *) NULL) {resize_image = ResizeImage (image, 1.0, LanczosFilter, exception); if (resize_image = (Image *) NULL) magickError (exception-> severity, exception-> reason, exception-> description); (void) AppendImageToList (& thumbnails, resize_image); DestroyImage (image );} /* Write the image thumbnail. */(void) strcpy (thumbnails-> filename, argv [2]); WriteImage (image_info, thumbnails);/* Destroy the image thumbnail and exit. */thumbnails = DestroyImageList (thumbnails); image_info = DestroyImageInfo (image_info); exception = DestroyExceptionInfo (exception); MagickCoreTerminus (); return (0 );}


MagickWand(Recommended C language interface. Compared with the MagickCore interface, it is much simpler. Suitable for common Users)

Function: Read files, create thumbnails, and save them as files.

/** Lei Xiaohua * leixiaohua1020@126.com * http://blog.csdn.net/leixiaohua1020 * China Communications University/Digital TV technology */# include <stdio. h> # include <stdlib. h> # include <wand/MagickWand. h> int main (int argc, char ** argv) {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]);/* Turn the images into a thumbnail sequence. */MagickResetIterator (magick_wand); while (MagickNextImage (magick_wand )! = MagickFalse) MagickResizeImage (magick_wand, 6,80, LanczosFilter, 1.0);/* Write the image then destroy it. */status = MagickWriteImages (magick_wand, argv [2], MagickTrue); magick_wand = DestroyMagickWand (magick_wand); MagickWandTerminus (); return (0 );}


Magick ++(Provides object-oriented C ++ interfaces)

/** Lei Xiaohua * leixiaohua1020@126.com * http://blog.csdn.net/leixiaohua1020 * China Communications University/Digital TV technology * // create an Image object, // create a blank image canvas with 640x480 size and 'white' color as background: Image blk_image (Geometry (640,480), Color (MaxRGB, 0 )); // or also, by using the automatic C ++ type conversions for the arguments: Image blank_image ("640x480", "white "); // create an image from URL Image url_image ("http://www.serverName.com/image.gif"); Image local_file_image ("my_image.gif "); // here the URL points to the local filesystem // get/Set Properties // Canvas geometry // returns an unsigned int representing the my_image widthunsigned int Image: columns (); // returns an unsigned int representing the my_image heigthunsigned int Image: rows (); // sets the my_image format; the format string can be "GIF", etcvoid Image :: magick ("png"); // returns a string value representing the image format (e.g. "GIF", "JPEG", etc) string Image: magick (); // read/Save the image file // Reading the contents of a disk file into an image object can be saved medimage my_image (); // create an * empty * image using the default Image constructor // read a GIF image file from disk; the image format is automatically set to GIFmy_image.read ("aGIFImageFile.gif "); // Writing an Image object to a disk file. set the "format" attribute of my_image to PNGmy_image.magick ("png"); // write to disk an image filemy_image.write ("file_name_explicit_extension.gif ");

MagickWand is generally used. Two MagickWand development examples are recorded below.

For more examples, refer to: http://members.shaw.ca/el.supremo/MagickWand/

Function: changes the image width to 50% of the source image.

/** Lei Xiaohua * leixiaohua1020@126.com * http://blog.csdn.net/leixiaohua1020 * China Communications University/Digital TV technology */# include <windows. h ># include <wand/magick_wand.h> void test_wand (void) {MagickWand * m_wand = NULL; int width, height; MagickWandGenesis (); m_wand = NewMagickWand (); // Read the image-all you need to do is change "logo:" to some other // filename to have this resize and, if necessary, convert a different file MagickReadImage (m_wand, "logo:"); // Get the image's width and height width = MagickGetImageWidth (m_wand); height = MagickGetImageHeight (m_wand ); // Cut them in half but make sure they don't underflow if (width/= 2) <1) width = 1; if (height/= 2) <1) height = 1; // Resize the image using the Lanczos filter // The blur factor is a "double", where> 1 is blurry, <1 is sharp // I haven't figured out how you wocould change the blur parameter of MagickResizeImage // on the command line so I have set it to its default of one. magickResizeImage (m_wand, width, height, LanczosFilter, 1); // Set the compression quality to 95 (high quality = low compression) MagickSetImageCompressionQuality (m_wand, 95 ); /* Write the new image */MagickWriteImage (m_wand, "logo_resize.jpg");/* Clean up */if (m_wand) m_wand = DestroyMagickWand (m_wand); MagickWandTerminus ();}

Function: Add a border around the image

/** Lei Xiaohua * leixiaohua1020@126.com * http://blog.csdn.net/leixiaohua1020 * China Communications University/Digital TV technology */# include <windows. h ># include <wand/magick_wand.h> void test_wand (void) {MagickWand * m_wand = NULL; PixelWand * p_wand; int w, h; MagickWandGenesis (); /* Create a wand */m_wand = NewMagickWand (); p_wand = NewPixelWand (); // Change this to whatever color you like-e.g. "none" PixelSetColor (p_wand, "blue");/* Read the input image */MagickReadImage (m_wand, "logo:"); w = MagickGetImageWidth (m_wand ); h = MagickGetImageHeight (m_wand); MagickSetImageBackgroundColor (m_wand, p_wand); // This centres the original image on the new canvas. // Note that the extent's offset is relative to the // top left corner of the * original * image, so adding an extent // around it means that the offset will be negative MagickExtentImage (m_wand, 1024,768,-(1024-w)/2,-(768-h)/2); MagickWriteImage (m_wand, "logo_extent.jpg");/* Tidy up */m_wand = DestroyMagickWand (m_wand); p_wand = DestroyPixelWand (p_wand); MagickWandTerminus ();}

 

Additional: detailed tutorials can be viewed from the left-side directory of ImageMagick's official website (http://www.imagemagick.org. In Program Interfaces, there are detailed development instructions for several Interfaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.