1
2
3
4
5<!DOCTYPE html>
6<html lang="en">
7<head>
8    <title>ImageMagick: MagickWand, C API for ImageMagick</title>
9  <meta charset="utf-8" />
10  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
11  <meta name="viewport" content="width=device-width, initial-scale=1" />
12  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
13  <meta name="application-name" content="ImageMagick"/>
14  <meta name="description" content="ImageMagick® is a software suite to create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, JPEG-2000, GIF, WebP, Postscript, PDF, and SVG. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves."/>
15  <meta name="application-url" content="http://www.imagemagick.org"/>
16  <meta name="generator" content="PHP"/>
17  <meta name="keywords" content="magickwc, api, for, imagemagick, ImageMagick, PerlMagick, image processing, image, photo, software, Magick++, OpenMP, convert"/>
18  <meta name="rating" content="GENERAL"/>
19  <meta name="robots" content="INDEX, FOLLOW"/>
20  <meta name="generator" content="ImageMagick Studio LLC"/>
21  <meta name="author" content="ImageMagick Studio LLC"/>
22  <meta name="revisit-after" content="2 DAYS"/>
23  <meta name="resource-type" content="document"/>
24  <meta name="copyright" content="Copyright (c) 1999-2016 ImageMagick Studio LLC"/>
25  <meta name="distribution" content="Global"/>
26  <meta name="magick-serial" content="P131-S030410-R485315270133-P82224-A6668-G1245-1"/>
27  <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" />
28  <link rel="icon" href="../images/wand.png"/>
29  <link rel="shortcut icon" href="../images/wand.ico"/>
30  <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Roboto:900,400,400italic,700,700italic,300,300italic|Open+Sans:300italic,400italic,700italic,300,400,600,700">
31  <link rel="stylesheet" href="css/magick.css"/>
32</head>
33<body>
34<div class="main">
35<div class="magick-masthead">
36  <div class="container">
37    <script async="async" src="http://localhost/pagead/js/adsbygoogle.js"></script>    <ins class="adsbygoogle"
38         style="display:block"
39         data-ad-client="ca-pub-3129977114552745"
40         data-ad-slot="6345125851"
41         data-ad-format="auto"></ins>
42    <script>
43      (adsbygoogle = window.adsbygoogle || []).push({});
44    </script>
45    <nav class="magick-nav">
46      <a class="magick-nav-item " href="../index.html">Home</a>
47      <a class="magick-nav-item " href="binary-releases.html">Download</a>
48      <a class="magick-nav-item " href="command-line-tools.html">Tools</a>
49      <a class="magick-nav-item " href="command-line-processing.html">Command-line</a>
50      <a class="magick-nav-item " href="resources.html">Resources</a>
51      <a class="magick-nav-item " href="api.html">Develop</a>
52      <a class="magick-nav-item " href="http://www.imagemagick.org/script/search.php">Search</a>
53      <a class="magick-nav-item pull-right" href="https://www.imagemagick.org/discourse-server/">Community</a>
54    </nav>
55  </div>
56</div>
57<div class="container">
58<div class="magick-header">
59
60<p class="lead magick-description">The <a href="http://www.imagemagick.org/api/MagickWand/index.html">MagickWand API</a> is the recommended interface between the C programming language and the ImageMagick image processing libraries.  Unlike the <a href="magick-core.html">MagickCore</a> 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:</p>
61
62<ul>
63  <li><a href="api/magick-wand.html">Magick Wand Methods</a></li>
64  <li><a href="api/magick-property.html">Set or Get Magick Wand Properties</a></li>
65  <li><a href="api/magick-image.html">Magick Wand Image Methods</a></li>
66  <li><a href="api/pixel-iterator.html">Pixel Iterator Methods</a></li>
67  <li><a href="api/pixel-wand.html">Pixel Wand Methods</a></li>
68  <li><a href="api/drawing-wand.html">Image Vector Drawing</a></li>
69  <li><a href="api/mogrify.html">Command-line Interface</a></li>
70  <li><a href="api/wand-view.html">Wand View Methods</a></li>
71  <li><a href="api/magick-deprecate.html">Deprecated Methods</a></li>
72  <li><a href="exception.html">Error and Warning Codes</a></li>
73</ul>
74
75<p>After you write your MagickWand program, compile it like this:</p>
76
77<pre>
78cc -o wand wand.c `pkg-config --cflags --libs MagickWand`
79</pre>
80
81<p>Set the <code>PKG_CONFIG_PATH</code> environment variable if ImageMagick is not in your default system path:</p>
82
83<pre>
84export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
85</pre>
86
87<p>Here is a example program that utilizes the MagickWand API to get you started, <a href="../source/wand.c">wand.c</a>. It reads an image, creates a thumbnail, and writes the result to disk.</p>
88
89<pre class="pre-scrollable">#include &lt;stdio.h>
90#include &lt;stdlib.h>
91#include &lt;wand/MagickWand.h>
92
93int main(int argc,char **argv)
94{
95#define ThrowWandException(wand) \
96{ \
97  char \
98    *description; \
99 \
100  ExceptionType \
101    severity; \
102 \
103  description=MagickGetException(wand,&amp;severity); \
104  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
105  description=(char *) MagickRelinquishMemory(description); \
106  exit(-1); \
107}
108
109  MagickBooleanType
110    status;
111
112  MagickWand
113    *magick_wand;
114
115  if (argc != 3)
116    {
117      (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
118      exit(0);
119    }
120  /*
121    Read an image.
122  */
123  MagickWandGenesis();
124  magick_wand=NewMagickWand();
125  status=MagickReadImage(magick_wand,argv[1]);
126  if (status == MagickFalse)
127    ThrowWandException(magick_wand);
128  /*
129    Turn the images into a thumbnail sequence.
130  */
131  MagickResetIterator(magick_wand);
132  while (MagickNextImage(magick_wand) != MagickFalse)
133    MagickResizeImage(magick_wand,106,80,LanczosFilter,1.0);
134  /*
135    Write the image then destroy it.
136  */
137  status=MagickWriteImages(magick_wand,argv[2],MagickTrue);
138  if (status == MagickFalse)
139    ThrowWandException(magick_wand);
140  magick_wand=DestroyMagickWand(magick_wand);
141  MagickWandTerminus();
142  return(0);
143}
144</pre>
145
146<p>Here is another program that shows one way to get and set image pixels with the MagickWand API, <a href="../source/contrast.c">contrast.c</a>. It reads an image, applies sigmoidal non-linearity contrast control, and writes the result to disk.</p>
147
148<pre class="pre-scrollable">#include &lt;stdio.h>
149#include &lt;stdlib.h>
150#include &lt;math.h>
151#include &lt;wand/MagickWand.h>
152
153int main(int argc,char **argv)
154{
155#define QuantumScale  ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
156#define SigmoidalContrast(x) \
157  (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
158#define ThrowWandException(wand) \
159{ \
160  char \
161    *description; \
162 \
163  ExceptionType \
164    severity; \
165 \
166  description=MagickGetException(wand,&amp;severity); \
167  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
168  description=(char *) MagickRelinquishMemory(description); \
169  exit(-1); \
170}
171
172  long
173    y;
174
175  MagickBooleanType
176    status;
177
178  MagickPixelPacket
179    pixel;
180
181  MagickWand
182    *contrast_wand,
183    *image_wand;
184
185  PixelIterator
186    *contrast_iterator,
187    *iterator;
188
189  PixelWand
190    **contrast_pixels,
191    **pixels;
192
193  register long
194    x;
195
196  unsigned long
197    width;
198
199  if (argc != 3)
200    {
201      (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
202      exit(0);
203    }
204  /*
205    Read an image.
206  */
207  MagickWandGenesis();
208  image_wand=NewMagickWand();
209  status=MagickReadImage(image_wand,argv[1]);
210  if (status == MagickFalse)
211    ThrowWandException(image_wand);
212  contrast_wand=CloneMagickWand(image_wand);
213  /*
214    Sigmoidal non-linearity contrast control.
215  */
216  iterator=NewPixelIterator(image_wand);
217  contrast_iterator=NewPixelIterator(contrast_wand);
218  if ((iterator == (PixelIterator *) NULL) ||
219      (contrast_iterator == (PixelIterator *) NULL))
220    ThrowWandException(image_wand);
221  for (y=0; y &lt; (long) MagickGetImageHeight(image_wand); y++)
222  {
223    pixels=PixelGetNextIteratorRow(iterator,&amp;width);
224    contrast_pixels=PixelGetNextIteratorRow(contrast_iterator,&amp;width);
225    if ((pixels == (PixelWand **) NULL) ||
226        (contrast_pixels == (PixelWand **) NULL))
227      break;
228    for (x=0; x &lt; (long) width; x++)
229    {
230      PixelGetMagickColor(pixels[x],&amp;pixel);
231      pixel.red=SigmoidalContrast(pixel.red);
232      pixel.green=SigmoidalContrast(pixel.green);
233      pixel.blue=SigmoidalContrast(pixel.blue);
234      pixel.index=SigmoidalContrast(pixel.index);
235      PixelSetMagickColor(contrast_pixels[x],&amp;pixel);
236    }
237    (void) PixelSyncIterator(contrast_iterator);
238  }
239  if (y &lt; (long) MagickGetImageHeight(image_wand))
240    ThrowWandException(image_wand);
241  contrast_iterator=DestroyPixelIterator(contrast_iterator);
242  iterator=DestroyPixelIterator(iterator);
243  image_wand=DestroyMagickWand(image_wand);
244  /*
245    Write the image then destroy it.
246  */
247  status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
248  if (status == MagickFalse)
249    ThrowWandException(image_wand);
250  contrast_wand=DestroyMagickWand(contrast_wand);
251  MagickWandTerminus();
252  return(0);
253}
254</pre>
255<p><a id="wand-view"></a>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 wand views.  The <a href="../source/wand/sigmoidal-contrast.c">sigmoidal-contrast.c</a> 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).</p>
256
257<pre class="pre-scrollable">#include &lt;stdio.h>
258#include &lt;stdlib.h>
259#include &lt;math.h>
260#include &lt;wand/MagickWand.h>
261
262static MagickBooleanType SigmoidalContrast(WandView *pixel_view,
263  const ssize_t y,const int id,void *context)
264{
265#define QuantumScale  ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
266#define SigmoidalContrast(x) \
267  (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
268
269  RectangleInfo
270    extent;
271
272  MagickPixelPacket
273    pixel;
274
275  PixelWand
276    **pixels;
277
278  register long
279    x;
280
281  extent=GetWandViewExtent(contrast_view);
282  pixels=GetWandViewPixels(contrast_view);
283  for (x=0; x &lt; (long) (extent.width-extent.height); x++)
284  {
285    PixelGetMagickColor(pixels[x],&amp;pixel);
286    pixel.red=SigmoidalContrast(pixel.red);
287    pixel.green=SigmoidalContrast(pixel.green);
288    pixel.blue=SigmoidalContrast(pixel.blue);
289    pixel.index=SigmoidalContrast(pixel.index);
290    PixelSetMagickColor(contrast_pixels[x],&amp;pixel);
291  }
292  return(MagickTrue);
293}
294
295int main(int argc,char **argv)
296{
297#define ThrowViewException(view) \
298{ \
299  description=GetWandViewException(view,&amp;severity); \
300  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
301  description=(char *) MagickRelinquishMemory(description); \
302  exit(-1); \
303}
304#define ThrowWandException(wand) \
305{ \
306  description=MagickGetException(wand,&amp;severity); \
307  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
308  description=(char *) MagickRelinquishMemory(description); \
309  exit(-1); \
310}
311
312  char
313    *description;
314
315  ExceptionType
316    severity;
317
318  MagickBooleanType
319    status;
320
321  MagickPixelPacket
322    pixel;
323
324  MagickWand
325    *contrast_wand;
326
327  WandView
328    *contrast_view;
329
330  if (argc != 3)
331    {
332      (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
333      exit(0);
334    }
335  /*
336    Read an image.
337  */
338  MagickWandGenesis();
339  contrast_wand=NewMagickWand();
340  status=MagickReadImage(contrast_wand,argv[1]);
341  if (status == MagickFalse)
342    ThrowWandException(contrast_wand);
343  /*
344    Sigmoidal non-linearity contrast control.
345  */
346  contrast_view=NewWandView(contrast_wand);
347  if (contrast_view == (WandView *) NULL)
348    ThrowWandException(contrast_wand);
349  status=UpdateWandViewIterator(contrast_view,SigmoidalContrast,(void *) NULL);
350  if (status == MagickFalse)
351    ThrowWandException(contrast_wand);
352  contrast_view=DestroyWandView(contrast_view);
353  /*
354    Write the image then destroy it.
355  */
356  status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
357  if (status == MagickFalse)
358    ThrowWandException(contrast_wand);
359  contrast_wand=DestroyMagickWand(contrast_wand);
360  MagickWandTerminus();
361  return(0);
362}
363</pre>
364<p><a href="http://members.shaw.ca/el.supremo/MagickWand/">MagickWand Examples in C</a> illustrates how to use the ImageMagick MagickWand API. Each example is presented as a C function, complete with headers, so that it can be copied to a file and then included in your own C project.</p>
365</div>
366  <footer class="magick-footer">
367    <p><a href="support.html">Donate</a> •
368     <a href="sitemap.html">Sitemap</a> •
369    <a href="links.html">Related</a> •
370    <a href="architecture.html">Architecture</a>
371</p>
372    <p><a href="magick-wand.html#">Back to top</a> •
373    <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a> •
374    <a href="http://www.imagemagick.org/script/contact.php">Contact Us</a></p>
375        <p><small>©  1999-2016 ImageMagick Studio LLC</small></p>
376  </footer>
377</div><!-- /.container -->
378
379  <script src="https://localhost/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
380  <script src="../js/magick.html"></script>
381</div>
382</body>
383</html>
384