blob: fcfb5912bb3d636eed50258bb878b2a69187ed8f [file] [log] [blame]
Cristy594d2772015-11-15 10:47:42 -05001
2
3
4
cristy29dc8652015-05-12 16:26:21 +00005<!DOCTYPE html>
cristyd2432012015-05-04 23:15:15 +00006<html lang="en">
cristyad41c0c2012-10-31 00:49:28 +00007<head>
Cristy912a9222016-06-17 15:37:28 -04008 <meta charset="utf-8" />
Cristy3b0b58d2016-11-06 07:40:23 -05009 <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
10 <meta http-equiv="x-ua-compatible" content="ie=edge" />
Cristybc232422016-11-22 07:46:18 -050011 <title>MagickWand, C API @ ImageMagick</title>
Cristy594d2772015-11-15 10:47:42 -050012 <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."/>
Cristy364a72f2016-11-04 22:38:41 -040015 <meta name="application-url" content="https://www.imagemagick.org"/>
Cristy594d2772015-11-15 10:47:42 -050016 <meta name="generator" content="PHP"/>
Cristybc232422016-11-22 07:46:18 -050017 <meta name="keywords" content="magickwc, api, ImageMagick, PerlMagick, image processing, image, photo, software, Magick++, OpenMP, convert"/>
Cristy594d2772015-11-15 10:47:42 -050018 <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"/>
Cristyfe36f992016-12-29 20:24:13 -050024 <meta name="copyright" content="Copyright (c) 1999-2017 ImageMagick Studio LLC"/>
Cristy594d2772015-11-15 10:47:42 -050025 <meta name="distribution" content="Global"/>
26 <meta name="magick-serial" content="P131-S030410-R485315270133-P82224-A6668-G1245-1"/>
Cristy912a9222016-06-17 15:37:28 -040027 <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" />
Cristy6549abb2016-11-10 08:25:55 -050028 <link rel="canonical" href="https://www.imagemagick.org/script/magick-wand.html"/>
Cristy594d2772015-11-15 10:47:42 -050029 <link rel="icon" href="../images/wand.png"/>
30 <link rel="shortcut icon" href="../images/wand.ico"/>
Cristybc232422016-11-22 07:46:18 -050031 <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" />
Cristyce4a3552015-12-14 13:53:35 -050032 <link rel="stylesheet" href="css/magick.css"/>
cristyad41c0c2012-10-31 00:49:28 +000033</head>
cristyd2432012015-05-04 23:15:15 +000034<body>
cristyad41c0c2012-10-31 00:49:28 +000035<div class="main">
cristyd2432012015-05-04 23:15:15 +000036<div class="magick-masthead">
37 <div class="container">
Cristy6549abb2016-11-10 08:25:55 -050038 <script async="async" src="http://localhost/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle"
Cristy594d2772015-11-15 10:47:42 -050039 style="display:block"
40 data-ad-client="ca-pub-3129977114552745"
41 data-ad-slot="6345125851"
42 data-ad-format="auto"></ins>
cristy29f2c972015-07-28 13:37:46 +000043 <script>
Cristy542697a2015-10-12 20:10:32 -040044 (adsbygoogle = window.adsbygoogle || []).push({});
cristyd2432012015-05-04 23:15:15 +000045 </script>
cristyd2432012015-05-04 23:15:15 +000046 <nav class="magick-nav">
cristy44443b92015-05-05 00:06:07 +000047 <a class="magick-nav-item " href="../index.html">Home</a>
Cristy9017f3a2017-01-21 10:51:11 -050048 <a class="magick-nav-item " href="download.html">Download</a>
cristyd2432012015-05-04 23:15:15 +000049 <a class="magick-nav-item " href="command-line-tools.html">Tools</a>
Cristye93be682015-11-26 09:07:51 -050050 <a class="magick-nav-item " href="command-line-processing.html">Command-line</a>
cristyea2793e2015-05-31 16:52:26 +000051 <a class="magick-nav-item " href="resources.html">Resources</a>
Cristy9017f3a2017-01-21 10:51:11 -050052 <a class="magick-nav-item " href="develop.html">Develop</a>
Cristy6549abb2016-11-10 08:25:55 -050053 <a class="magick-nav-item " href="https://www.imagemagick.org/script/search.php">Search</a>
54 <a class="magick-nav-item pull-right" href="https://www.imagemagick.org/discourse-server/">Community</a>
cristyd2432012015-05-04 23:15:15 +000055 </nav>
56 </div>
57</div>
58<div class="container">
59<div class="magick-header">
cristyad41c0c2012-10-31 00:49:28 +000060
Cristy6549abb2016-11-10 08:25:55 -050061<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>
cristy3ed852e2009-09-05 21:47:34 +000062
63<ul>
Cristy6549abb2016-11-10 08:25:55 -050064 <li><a href="http://www.imagemagick.org/api/magick-wand.html">Magick Wand Methods</a></li>
65 <li><a href="http://www.imagemagick.org/api/magick-property.html">Set or Get Magick Wand Properties</a></li>
66 <li><a href="http://www.imagemagick.org/api/magick-image.html">Magick Wand Image Methods</a></li>
67 <li><a href="http://www.imagemagick.org/api/pixel-iterator.html">Pixel Iterator Methods</a></li>
68 <li><a href="http://www.imagemagick.org/api/pixel-wand.html">Pixel Wand Methods</a></li>
69 <li><a href="http://www.imagemagick.org/api/drawing-wand.html">Image Vector Drawing</a></li>
70 <li><a href="http://www.imagemagick.org/api/mogrify.html">Command-line Interface</a></li>
cristy10d5f992015-05-07 17:52:30 +000071 <li><a href="api/wand-view.html">Wand View Methods</a></li>
Cristy6549abb2016-11-10 08:25:55 -050072 <li><a href="http://www.imagemagick.org/api/magick-deprecate.html">Deprecated Methods</a></li>
cristy751980d2012-06-03 23:18:35 +000073 <li><a href="exception.html">Error and Warning Codes</a></li>
cristy3ed852e2009-09-05 21:47:34 +000074</ul>
75
76<p>After you write your MagickWand program, compile it like this:</p>
77
cristy82a2ff22015-05-12 16:41:32 +000078<pre>
79cc -o wand wand.c `pkg-config --cflags --libs MagickWand`
80</pre>
81
cristy8ee7f242013-06-20 16:08:44 +000082<p>Set the <code>PKG_CONFIG_PATH</code> environment variable if ImageMagick is not in your default system path:</p>
cristy82a2ff22015-05-12 16:41:32 +000083
84<pre>
85export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
86</pre>
87
Cristy6549abb2016-11-10 08:25:55 -050088<p>Here is a example program that utilizes the MagickWand API to get you started, <a href="http://www.imagemagick.org/source/wand.c">wand.c</a>. It reads an image, creates a thumbnail, and writes the result to disk.</p>
cristy3ed852e2009-09-05 21:47:34 +000089
Cristy594d2772015-11-15 10:47:42 -050090<pre class="pre-scrollable">#include &lt;stdio.h>
91#include &lt;stdlib.h>
92#include &lt;wand/MagickWand.h>
cristy3ed852e2009-09-05 21:47:34 +000093
94int main(int argc,char **argv)
95{
96#define ThrowWandException(wand) \
97{ \
98 char \
99 *description; \
100 \
101 ExceptionType \
102 severity; \
103 \
104 description=MagickGetException(wand,&amp;severity); \
cristyacc04882011-05-04 23:03:43 +0000105 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
cristy3ed852e2009-09-05 21:47:34 +0000106 description=(char *) MagickRelinquishMemory(description); \
107 exit(-1); \
108}
109
110 MagickBooleanType
111 status;
112
113 MagickWand
114 *magick_wand;
115
116 if (argc != 3)
117 {
118 (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
119 exit(0);
120 }
121 /*
122 Read an image.
123 */
124 MagickWandGenesis();
125 magick_wand=NewMagickWand();
126 status=MagickReadImage(magick_wand,argv[1]);
127 if (status == MagickFalse)
128 ThrowWandException(magick_wand);
129 /*
130 Turn the images into a thumbnail sequence.
131 */
132 MagickResetIterator(magick_wand);
133 while (MagickNextImage(magick_wand) != MagickFalse)
134 MagickResizeImage(magick_wand,106,80,LanczosFilter,1.0);
135 /*
136 Write the image then destroy it.
137 */
138 status=MagickWriteImages(magick_wand,argv[2],MagickTrue);
139 if (status == MagickFalse)
140 ThrowWandException(magick_wand);
141 magick_wand=DestroyMagickWand(magick_wand);
142 MagickWandTerminus();
143 return(0);
144}
cristyd2432012015-05-04 23:15:15 +0000145</pre>
cristy3ed852e2009-09-05 21:47:34 +0000146
Cristy6549abb2016-11-10 08:25:55 -0500147<p>Here is another program that shows one way to get and set image pixels with the MagickWand API, <a href="http://www.imagemagick.org/source/contrast.c">contrast.c</a>. It reads an image, applies sigmoidal non-linearity contrast control, and writes the result to disk.</p>
cristy3ed852e2009-09-05 21:47:34 +0000148
Cristy594d2772015-11-15 10:47:42 -0500149<pre class="pre-scrollable">#include &lt;stdio.h>
150#include &lt;stdlib.h>
151#include &lt;math.h>
152#include &lt;wand/MagickWand.h>
cristy3ed852e2009-09-05 21:47:34 +0000153
154int main(int argc,char **argv)
155{
156#define QuantumScale ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
157#define SigmoidalContrast(x) \
158 (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
159#define ThrowWandException(wand) \
160{ \
161 char \
162 *description; \
163 \
164 ExceptionType \
165 severity; \
166 \
167 description=MagickGetException(wand,&amp;severity); \
168 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
169 description=(char *) MagickRelinquishMemory(description); \
170 exit(-1); \
171}
172
173 long
174 y;
175
176 MagickBooleanType
177 status;
178
179 MagickPixelPacket
180 pixel;
181
182 MagickWand
183 *contrast_wand,
184 *image_wand;
185
186 PixelIterator
187 *contrast_iterator,
188 *iterator;
189
190 PixelWand
191 **contrast_pixels,
192 **pixels;
193
194 register long
195 x;
196
197 unsigned long
198 width;
199
200 if (argc != 3)
201 {
202 (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
203 exit(0);
204 }
205 /*
206 Read an image.
207 */
208 MagickWandGenesis();
209 image_wand=NewMagickWand();
210 status=MagickReadImage(image_wand,argv[1]);
211 if (status == MagickFalse)
212 ThrowWandException(image_wand);
213 contrast_wand=CloneMagickWand(image_wand);
214 /*
215 Sigmoidal non-linearity contrast control.
216 */
217 iterator=NewPixelIterator(image_wand);
218 contrast_iterator=NewPixelIterator(contrast_wand);
219 if ((iterator == (PixelIterator *) NULL) ||
220 (contrast_iterator == (PixelIterator *) NULL))
221 ThrowWandException(image_wand);
222 for (y=0; y &lt; (long) MagickGetImageHeight(image_wand); y++)
223 {
224 pixels=PixelGetNextIteratorRow(iterator,&amp;width);
225 contrast_pixels=PixelGetNextIteratorRow(contrast_iterator,&amp;width);
226 if ((pixels == (PixelWand **) NULL) ||
227 (contrast_pixels == (PixelWand **) NULL))
228 break;
229 for (x=0; x &lt; (long) width; x++)
230 {
231 PixelGetMagickColor(pixels[x],&amp;pixel);
232 pixel.red=SigmoidalContrast(pixel.red);
233 pixel.green=SigmoidalContrast(pixel.green);
234 pixel.blue=SigmoidalContrast(pixel.blue);
235 pixel.index=SigmoidalContrast(pixel.index);
236 PixelSetMagickColor(contrast_pixels[x],&amp;pixel);
237 }
238 (void) PixelSyncIterator(contrast_iterator);
239 }
240 if (y &lt; (long) MagickGetImageHeight(image_wand))
241 ThrowWandException(image_wand);
242 contrast_iterator=DestroyPixelIterator(contrast_iterator);
243 iterator=DestroyPixelIterator(iterator);
244 image_wand=DestroyMagickWand(image_wand);
245 /*
246 Write the image then destroy it.
247 */
248 status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
249 if (status == MagickFalse)
250 ThrowWandException(image_wand);
251 contrast_wand=DestroyMagickWand(contrast_wand);
252 MagickWandTerminus();
253 return(0);
254}
cristyd2432012015-05-04 23:15:15 +0000255</pre>
Cristy6549abb2016-11-10 08:25:55 -0500256<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="http://www.imagemagick.org/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>
cristy3ed852e2009-09-05 21:47:34 +0000257
Cristy594d2772015-11-15 10:47:42 -0500258<pre class="pre-scrollable">#include &lt;stdio.h>
259#include &lt;stdlib.h>
260#include &lt;math.h>
261#include &lt;wand/MagickWand.h>
cristy3ed852e2009-09-05 21:47:34 +0000262
cristy10c87152010-06-26 16:12:21 +0000263static MagickBooleanType SigmoidalContrast(WandView *pixel_view,
cristyc3ebda22010-06-27 17:11:57 +0000264 const ssize_t y,const int id,void *context)
cristy3ed852e2009-09-05 21:47:34 +0000265{
266#define QuantumScale ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
267#define SigmoidalContrast(x) \
268 (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
269
cristyc3ebda22010-06-27 17:11:57 +0000270 RectangleInfo
271 extent;
272
cristy3ed852e2009-09-05 21:47:34 +0000273 MagickPixelPacket
274 pixel;
275
276 PixelWand
277 **pixels;
278
279 register long
280 x;
281
cristyc3ebda22010-06-27 17:11:57 +0000282 extent=GetWandViewExtent(contrast_view);
cristy10c87152010-06-26 16:12:21 +0000283 pixels=GetWandViewPixels(contrast_view);
cristyc3ebda22010-06-27 17:11:57 +0000284 for (x=0; x &lt; (long) (extent.width-extent.height); x++)
cristy3ed852e2009-09-05 21:47:34 +0000285 {
286 PixelGetMagickColor(pixels[x],&amp;pixel);
287 pixel.red=SigmoidalContrast(pixel.red);
288 pixel.green=SigmoidalContrast(pixel.green);
289 pixel.blue=SigmoidalContrast(pixel.blue);
290 pixel.index=SigmoidalContrast(pixel.index);
291 PixelSetMagickColor(contrast_pixels[x],&amp;pixel);
292 }
293 return(MagickTrue);
294}
295
296int main(int argc,char **argv)
297{
cristyc3ebda22010-06-27 17:11:57 +0000298#define ThrowViewException(view) \
cristy3ed852e2009-09-05 21:47:34 +0000299{ \
cristydcca3fa2010-12-29 21:50:07 +0000300 description=GetWandViewException(view,&amp;severity); \
cristy3ed852e2009-09-05 21:47:34 +0000301 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
302 description=(char *) MagickRelinquishMemory(description); \
303 exit(-1); \
304}
cristyc3ebda22010-06-27 17:11:57 +0000305#define ThrowWandException(wand) \
306{ \
cristydcca3fa2010-12-29 21:50:07 +0000307 description=MagickGetException(wand,&amp;severity); \
cristyc3ebda22010-06-27 17:11:57 +0000308 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
309 description=(char *) MagickRelinquishMemory(description); \
310 exit(-1); \
311}
312
313 char
314 *description;
315
316 ExceptionType
317 severity;
cristy3ed852e2009-09-05 21:47:34 +0000318
319 MagickBooleanType
320 status;
321
322 MagickPixelPacket
323 pixel;
324
325 MagickWand
326 *contrast_wand;
327
cristy10c87152010-06-26 16:12:21 +0000328 WandView
cristy3ed852e2009-09-05 21:47:34 +0000329 *contrast_view;
330
331 if (argc != 3)
332 {
333 (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
334 exit(0);
335 }
336 /*
337 Read an image.
338 */
339 MagickWandGenesis();
340 contrast_wand=NewMagickWand();
341 status=MagickReadImage(contrast_wand,argv[1]);
342 if (status == MagickFalse)
343 ThrowWandException(contrast_wand);
344 /*
345 Sigmoidal non-linearity contrast control.
346 */
cristy10c87152010-06-26 16:12:21 +0000347 contrast_view=NewWandView(contrast_wand);
348 if (contrast_view == (WandView *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000349 ThrowWandException(contrast_wand);
cristy10c87152010-06-26 16:12:21 +0000350 status=UpdateWandViewIterator(contrast_view,SigmoidalContrast,(void *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000351 if (status == MagickFalse)
352 ThrowWandException(contrast_wand);
cristy10c87152010-06-26 16:12:21 +0000353 contrast_view=DestroyWandView(contrast_view);
cristy3ed852e2009-09-05 21:47:34 +0000354 /*
355 Write the image then destroy it.
356 */
357 status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
358 if (status == MagickFalse)
359 ThrowWandException(contrast_wand);
360 contrast_wand=DestroyMagickWand(contrast_wand);
361 MagickWandTerminus();
362 return(0);
363}
cristyd2432012015-05-04 23:15:15 +0000364</pre>
Cristya3de16b2017-05-14 12:25:22 -0400365<p><a href="../MagickWand/index.html">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>
cristydcca3fa2010-12-29 21:50:07 +0000366</div>
cristyd2432012015-05-04 23:15:15 +0000367 <footer class="magick-footer">
cristy29f2c972015-07-28 13:37:46 +0000368 <p><a href="support.html">Donate</a>
369 <a href="sitemap.html">Sitemap</a>
cristyd2432012015-05-04 23:15:15 +0000370 <a href="links.html">Related</a>
Cristye0779712016-07-30 17:25:22 -0400371 <a href="security-policy.html">Security</a>
cristy29f2c972015-07-28 13:37:46 +0000372 <a href="architecture.html">Architecture</a>
cristyd2432012015-05-04 23:15:15 +0000373</p>
374 <p><a href="magick-wand.html#">Back to top</a>
cristyea2793e2015-05-31 16:52:26 +0000375 <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a>
Cristy6549abb2016-11-10 08:25:55 -0500376 <a href="https://www.imagemagick.org/script/contact.php">Contact Us</a></p>
Cristyfe36f992016-12-29 20:24:13 -0500377 <p><small>© 1999-2017 ImageMagick Studio LLC</small></p>
cristyd2432012015-05-04 23:15:15 +0000378 </footer>
379</div><!-- /.container -->
cristyad41c0c2012-10-31 00:49:28 +0000380
Cristyb7f455a2017-03-03 19:30:34 -0500381 <script src="https://localhost/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Cristy1f1f3a02016-06-01 07:25:42 -0400382 <script src="../js/magick.html"></script>
cristyad41c0c2012-10-31 00:49:28 +0000383</div>
cristyad41c0c2012-10-31 00:49:28 +0000384</body>
cristy5a367e42015-05-05 12:39:18 +0000385</html>
Cristyd07f4fe2017-06-02 20:36:40 -0400386<!-- Magick Cache 2nd June 2017 20:19 -->