]> granicus.if.org Git - imagemagick/blob - www/magick-wand.html
...
[imagemagick] / www / magick-wand.html
1
2
3
4
5 <!DOCTYPE html>
6 <html lang="en">
7 <head>
8   <meta charset="utf-8" />
9   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
10   <meta http-equiv="x-ua-compatible" content="ie=edge" />
11   <title>MagickWand, C API @ ImageMagick</title>
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="https://www.imagemagick.org"/>
16   <meta name="generator" content="PHP"/>
17   <meta name="keywords" content="magickwc, api, 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-2017 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="canonical" href="https://www.imagemagick.org/script/magick-wand.html"/>
29   <link rel="icon" href="../images/wand.png"/>
30   <link rel="shortcut icon" href="../images/wand.ico"/>
31   <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" />
32   <link rel="stylesheet" href="css/magick.css"/>
33 </head>
34 <body>
35 <div class="main">
36 <div class="magick-masthead">
37   <div class="container">
38     <script async="async" src="http://localhost/pagead/js/adsbygoogle.js"></script>    <ins class="adsbygoogle"
39          style="display:block"
40          data-ad-client="ca-pub-3129977114552745"
41          data-ad-slot="6345125851"
42          data-ad-format="auto"></ins>
43     <script>
44       (adsbygoogle = window.adsbygoogle || []).push({});
45     </script>
46     <nav class="magick-nav">
47       <a class="magick-nav-item " href="../index.html">Home</a>
48       <a class="magick-nav-item " href="download.html">Download</a>
49       <a class="magick-nav-item " href="command-line-tools.html">Tools</a>
50       <a class="magick-nav-item " href="command-line-processing.html">Command-line</a>
51       <a class="magick-nav-item " href="resources.html">Resources</a>
52       <a class="magick-nav-item " href="develop.html">Develop</a>
53       <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>
55     </nav>
56   </div>
57 </div>
58 <div class="container">
59 <div class="magick-header">
60
61 <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>
62
63 <ul>
64   <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>
71   <li><a href="api/wand-view.html">Wand View Methods</a></li>
72   <li><a href="http://www.imagemagick.org/api/magick-deprecate.html">Deprecated Methods</a></li>
73   <li><a href="exception.html">Error and Warning Codes</a></li>
74 </ul>
75
76 <p>After you write your MagickWand program, compile it like this:</p>
77
78 <pre>
79 cc -o wand wand.c `pkg-config --cflags --libs MagickWand`
80 </pre>
81
82 <p>Set the <code>PKG_CONFIG_PATH</code> environment variable if ImageMagick is not in your default system path:</p>
83
84 <pre>
85 export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
86 </pre>
87
88 <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>
89
90 <pre class="pre-scrollable">#include &lt;stdio.h>
91 #include &lt;stdlib.h>
92 #include &lt;wand/MagickWand.h>
93
94 int 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); \
105   (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
106   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 }
145 </pre>
146
147 <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>
148
149 <pre class="pre-scrollable">#include &lt;stdio.h>
150 #include &lt;stdlib.h>
151 #include &lt;math.h>
152 #include &lt;wand/MagickWand.h>
153
154 int 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 }
255 </pre>
256 <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>
257
258 <pre class="pre-scrollable">#include &lt;stdio.h>
259 #include &lt;stdlib.h>
260 #include &lt;math.h>
261 #include &lt;wand/MagickWand.h>
262
263 static MagickBooleanType SigmoidalContrast(WandView *pixel_view,
264   const ssize_t y,const int id,void *context)
265 {
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
270   RectangleInfo
271     extent;
272
273   MagickPixelPacket
274     pixel;
275
276   PixelWand
277     **pixels;
278
279   register long
280     x;
281
282   extent=GetWandViewExtent(contrast_view);
283   pixels=GetWandViewPixels(contrast_view);
284   for (x=0; x &lt; (long) (extent.width-extent.height); x++)
285   {
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
296 int main(int argc,char **argv)
297 {
298 #define ThrowViewException(view) \
299 { \
300   description=GetWandViewException(view,&amp;severity); \
301   (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
302   description=(char *) MagickRelinquishMemory(description); \
303   exit(-1); \
304 }
305 #define ThrowWandException(wand) \
306 { \
307   description=MagickGetException(wand,&amp;severity); \
308   (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;
318
319   MagickBooleanType
320     status;
321
322   MagickPixelPacket
323     pixel;
324
325   MagickWand
326     *contrast_wand;
327
328   WandView
329     *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   */
347   contrast_view=NewWandView(contrast_wand);
348   if (contrast_view == (WandView *) NULL)
349     ThrowWandException(contrast_wand);
350   status=UpdateWandViewIterator(contrast_view,SigmoidalContrast,(void *) NULL);
351   if (status == MagickFalse)
352     ThrowWandException(contrast_wand);
353   contrast_view=DestroyWandView(contrast_view);
354   /*
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 }
364 </pre>
365 <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>
366 </div>
367   <footer class="magick-footer">
368     <p><a href="support.html">Donate</a> •
369      <a href="sitemap.html">Sitemap</a> •
370     <a href="links.html">Related</a> •
371     <a href="security-policy.html">Security</a> •
372     <a href="architecture.html">Architecture</a>
373 </p>
374     <p><a href="magick-wand.html#">Back to top</a> •
375     <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a> •
376     <a href="https://www.imagemagick.org/script/contact.php">Contact Us</a></p>
377         <p><small>© 1999-2017 ImageMagick Studio LLC</small></p>
378   </footer>
379 </div><!-- /.container -->
380
381   <script src="https://localhost/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
382   <script src="../js/magick.html"></script>
383 </div>
384 </body>
385 </html>
386 <!-- Magick Cache 21st January 2017 12:17 -->