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,minimum-scale=1,initial-scale=1,shrink-to-fit=no" /> 10 <title>MagickWand, C API @ ImageMagick</title> 11 <meta name="application-name" content="ImageMagick" /> 12 <meta name="description" content="Use ImageMagick® to create, edit, compose, or convert bitmap images. You can resize your image, crop it, change its shades and colors, add captions, among other operations." /> 13 <meta name="application-url" content="https://imagemagick.org" /> 14 <meta name="generator" content="PHP" /> 15 <meta name="keywords" content="magickwc, api, ImageMagick, PerlMagick, image processing, image, photo, software, Magick++, OpenMP, convert" /> 16 <meta name="rating" content="GENERAL" /> 17 <meta name="robots" content="INDEX, FOLLOW" /> 18 <meta name="generator" content="ImageMagick Studio LLC" /> 19 <meta name="author" content="ImageMagick Studio LLC" /> 20 <meta name="revisit-after" content="2 DAYS" /> 21 <meta name="resource-type" content="document" /> 22 <meta name="copyright" content="Copyright (c) 1999-2019 ImageMagick Studio LLC" /> 23 <meta name="distribution" content="Global" /> 24 <meta name="magick-serial" content="P131-S030410-R485315270133-P82224-A6668-G1245-1" /> 25 <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" /> 26 <link href="magick-wand.html" rel="canonical" /> 27 <link href="../images/wand.png" rel="icon" /> 28 <link href="../images/wand.ico" rel="shortcut icon" /> 29 <link href="assets/magick.css" rel="stylesheet" /> 30</head> 31<body> 32 <header> 33 <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark"> 34 <a class="navbar-brand" href="../"><img class="d-block" id="icon" alt="ImageMagick" width="32" height="32" src="../images/wand.ico"/></a> 35 <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsMagick" aria-controls="navbarsMagick" aria-expanded="false" aria-label="Toggle navigation"> 36 <span class="navbar-toggler-icon"></span> 37 </button> 38 39 <div class="navbar-collapse collapse" id="navbarsMagick" style=""> 40 <ul class="navbar-nav mr-auto"> 41 <li class="nav-item "> 42 <a class="nav-link" href="api/quantize.html">Home <span class="sr-only">(current)</span></a> 43 </li> 44 <li class="nav-item "> 45 <a class="nav-link" href="../www/download.html">Download</a> 46 </li> 47 <li class="nav-item "> 48 <a class="nav-link" href="../www/command-line-tools.html">Tools</a> 49 </li> 50 <li class="nav-item "> 51 <a class="nav-link" href="../www/command-line-processing.html">Command-line</a> 52 </li> 53 <li class="nav-item "> 54 <a class="nav-link" href="../www/resources.html">Resources</a> 55 </li> 56 <li class="nav-item "> 57 <a class="nav-link" href="../www/develop.html">Develop</a> 58 </li> 59 <li class="nav-item"> 60 <a class="nav-link" target="_blank" href="https://imagemagick.org/discourse-server/">Community</a> 61 </li> 62 </ul> 63 <form class="form-inline my-2 my-lg-0" action="https://imagemagick.org/script/search.php"> 64 <input class="form-control mr-sm-2" type="text" name="q" placeholder="Search" aria-label="Search"> 65 <button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="sa">Search</button> 66 </form> 67 </div> 68 </nav> 69 <div class="container"> 70 <script async="async" src="https://localhost/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" 71 style="display:block" 72 data-ad-client="ca-pub-3129977114552745" 73 data-ad-slot="6345125851" 74 data-ad-format="auto"></ins> 75 <script> 76 (adsbygoogle = window.adsbygoogle || []).push({}); 77 </script> 78 79 </div> 80 </header> 81 <main class="container"> 82 <div class="magick-template"> 83<div class="magick-header"> 84 85<p class="lead magick-description">The <a href="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> 86 87<ul> 88 <li><a href="api/magick-wand.html">Magick Wand Methods</a></li> 89 <li><a href="api/magick-property.html">Set or Get Magick Wand Properties</a></li> 90 <li><a href="api/magick-image.html">Magick Wand Image Methods</a></li> 91 <li><a href="api/pixel-iterator.html">Pixel Iterator Methods</a></li> 92 <li><a href="api/pixel-wand.html">Pixel Wand Methods</a></li> 93 <li><a href="api/drawing-wand.html">Image Vector Drawing</a></li> 94 <li><a href="api/mogrify.html">Command-line Interface</a></li> 95 <li><a href="api/wand-view.html">Wand View Methods</a></li> 96 <li><a href="api/magick-deprecate.html">Deprecated Methods</a></li> 97 <li><a href="exception.html">Error and Warning Codes</a></li> 98</ul> 99 100<p>After you write your MagickWand program, compile it like this:</p> 101 102<pre class="highlight"><code>cc -o wand wand.c `pkg-config --cflags --libs MagickWand` 103</code></pre> 104 105<p>Set the <code>PKG_CONFIG_PATH</code> environment variable if ImageMagick is not in your default system path:</p> 106 107<pre class="highlight"><code>export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig 108</code></pre> 109 110<p>Here is a example program that utilizes the MagickWand API to get you started, <a href="https://imagemagick.org/source/wand.c">wand.c</a>. It reads an image, creates a thumbnail, and writes the result to disk.</p> 111 112<pre class="pre-scrollable"><code>#include <stdio.h> 113#include <stdlib.h> 114#include <wand/MagickWand.h> 115 116int main(int argc,char **argv) 117{ 118#define ThrowWandException(wand) \ 119{ \ 120 char \ 121 *description; \ 122 \ 123 ExceptionType \ 124 severity; \ 125 \ 126 description=MagickGetException(wand,&severity); \ 127 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \ 128 description=(char *) MagickRelinquishMemory(description); \ 129 exit(-1); \ 130} 131 132 MagickBooleanType 133 status; 134 135 MagickWand 136 *magick_wand; 137 138 if (argc != 3) 139 { 140 (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]); 141 exit(0); 142 } 143 /* 144 Read an image. 145 */ 146 MagickWandGenesis(); 147 magick_wand=NewMagickWand(); 148 status=MagickReadImage(magick_wand,argv[1]); 149 if (status == MagickFalse) 150 ThrowWandException(magick_wand); 151 /* 152 Turn the images into a thumbnail sequence. 153 */ 154 MagickResetIterator(magick_wand); 155 while (MagickNextImage(magick_wand) != MagickFalse) 156 MagickResizeImage(magick_wand,106,80,LanczosFilter,1.0); 157 /* 158 Write the image then destroy it. 159 */ 160 status=MagickWriteImages(magick_wand,argv[2],MagickTrue); 161 if (status == MagickFalse) 162 ThrowWandException(magick_wand); 163 magick_wand=DestroyMagickWand(magick_wand); 164 MagickWandTerminus(); 165 return(0); 166} 167</code></pre> 168 169<p>Here is another program that shows one way to get and set image pixels with the MagickWand API, <a href="https://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> 170 171<pre class="pre-scrollable"><code>#include <stdio.h> 172#include <stdlib.h> 173#include <math.h> 174#include <wand/MagickWand.h> 175 176int main(int argc,char **argv) 177{ 178#define QuantumScale ((MagickRealType) 1.0/(MagickRealType) QuantumRange) 179#define SigmoidalContrast(x) \ 180 (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503) 181#define ThrowWandException(wand) \ 182{ \ 183 char \ 184 *description; \ 185 \ 186 ExceptionType \ 187 severity; \ 188 \ 189 description=MagickGetException(wand,&severity); \ 190 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \ 191 description=(char *) MagickRelinquishMemory(description); \ 192 exit(-1); \ 193} 194 195 long 196 y; 197 198 MagickBooleanType 199 status; 200 201 MagickPixelPacket 202 pixel; 203 204 MagickWand 205 *contrast_wand, 206 *image_wand; 207 208 PixelIterator 209 *contrast_iterator, 210 *iterator; 211 212 PixelWand 213 **contrast_pixels, 214 **pixels; 215 216 register long 217 x; 218 219 unsigned long 220 width; 221 222 if (argc != 3) 223 { 224 (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]); 225 exit(0); 226 } 227 /* 228 Read an image. 229 */ 230 MagickWandGenesis(); 231 image_wand=NewMagickWand(); 232 status=MagickReadImage(image_wand,argv[1]); 233 if (status == MagickFalse) 234 ThrowWandException(image_wand); 235 contrast_wand=CloneMagickWand(image_wand); 236 /* 237 Sigmoidal non-linearity contrast control. 238 */ 239 iterator=NewPixelIterator(image_wand); 240 contrast_iterator=NewPixelIterator(contrast_wand); 241 if ((iterator == (PixelIterator *) NULL) || 242 (contrast_iterator == (PixelIterator *) NULL)) 243 ThrowWandException(image_wand); 244 for (y=0; y < (long) MagickGetImageHeight(image_wand); y++) 245 { 246 pixels=PixelGetNextIteratorRow(iterator,&width); 247 contrast_pixels=PixelGetNextIteratorRow(contrast_iterator,&width); 248 if ((pixels == (PixelWand **) NULL) || 249 (contrast_pixels == (PixelWand **) NULL)) 250 break; 251 for (x=0; x < (long) width; x++) 252 { 253 PixelGetMagickColor(pixels[x],&pixel); 254 pixel.red=SigmoidalContrast(pixel.red); 255 pixel.green=SigmoidalContrast(pixel.green); 256 pixel.blue=SigmoidalContrast(pixel.blue); 257 pixel.index=SigmoidalContrast(pixel.index); 258 PixelSetMagickColor(contrast_pixels[x],&pixel); 259 } 260 (void) PixelSyncIterator(contrast_iterator); 261 } 262 if (y < (long) MagickGetImageHeight(image_wand)) 263 ThrowWandException(image_wand); 264 contrast_iterator=DestroyPixelIterator(contrast_iterator); 265 iterator=DestroyPixelIterator(iterator); 266 image_wand=DestroyMagickWand(image_wand); 267 /* 268 Write the image then destroy it. 269 */ 270 status=MagickWriteImages(contrast_wand,argv[2],MagickTrue); 271 if (status == MagickFalse) 272 ThrowWandException(image_wand); 273 contrast_wand=DestroyMagickWand(contrast_wand); 274 MagickWandTerminus(); 275 return(0); 276} 277</code></pre> 278<p><a class="anchor" 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="https://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> 279 280<pre class="pre-scrollable"><code>#include <stdio.h> 281#include <stdlib.h> 282#include <math.h> 283#include <wand/MagickWand.h> 284 285static MagickBooleanType SigmoidalContrast(WandView *pixel_view, 286 const ssize_t y,const int id,void *context) 287{ 288#define QuantumScale ((MagickRealType) 1.0/(MagickRealType) QuantumRange) 289#define SigmoidalContrast(x) \ 290 (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503) 291 292 RectangleInfo 293 extent; 294 295 MagickPixelPacket 296 pixel; 297 298 PixelWand 299 **pixels; 300 301 register long 302 x; 303 304 extent=GetWandViewExtent(contrast_view); 305 pixels=GetWandViewPixels(contrast_view); 306 for (x=0; x < (long) (extent.width-extent.height); x++) 307 { 308 PixelGetMagickColor(pixels[x],&pixel); 309 pixel.red=SigmoidalContrast(pixel.red); 310 pixel.green=SigmoidalContrast(pixel.green); 311 pixel.blue=SigmoidalContrast(pixel.blue); 312 pixel.index=SigmoidalContrast(pixel.index); 313 PixelSetMagickColor(contrast_pixels[x],&pixel); 314 } 315 return(MagickTrue); 316} 317 318int main(int argc,char **argv) 319{ 320#define ThrowViewException(view) \ 321{ \ 322 description=GetWandViewException(view,&severity); \ 323 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \ 324 description=(char *) MagickRelinquishMemory(description); \ 325 exit(-1); \ 326} 327#define ThrowWandException(wand) \ 328{ \ 329 description=MagickGetException(wand,&severity); \ 330 (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \ 331 description=(char *) MagickRelinquishMemory(description); \ 332 exit(-1); \ 333} 334 335 char 336 *description; 337 338 ExceptionType 339 severity; 340 341 MagickBooleanType 342 status; 343 344 MagickPixelPacket 345 pixel; 346 347 MagickWand 348 *contrast_wand; 349 350 WandView 351 *contrast_view; 352 353 if (argc != 3) 354 { 355 (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]); 356 exit(0); 357 } 358 /* 359 Read an image. 360 */ 361 MagickWandGenesis(); 362 contrast_wand=NewMagickWand(); 363 status=MagickReadImage(contrast_wand,argv[1]); 364 if (status == MagickFalse) 365 ThrowWandException(contrast_wand); 366 /* 367 Sigmoidal non-linearity contrast control. 368 */ 369 contrast_view=NewWandView(contrast_wand); 370 if (contrast_view == (WandView *) NULL) 371 ThrowWandException(contrast_wand); 372 status=UpdateWandViewIterator(contrast_view,SigmoidalContrast,(void *) NULL); 373 if (status == MagickFalse) 374 ThrowWandException(contrast_wand); 375 contrast_view=DestroyWandView(contrast_view); 376 /* 377 Write the image then destroy it. 378 */ 379 status=MagickWriteImages(contrast_wand,argv[2],MagickTrue); 380 if (status == MagickFalse) 381 ThrowWandException(contrast_wand); 382 contrast_wand=DestroyMagickWand(contrast_wand); 383 MagickWandTerminus(); 384 return(0); 385} 386</code></pre> 387<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> 388</div> 389 </div> 390 </main><!-- /.container --> 391 <footer class="magick-footer"> 392 <p><a href="../www/security-policy.html">Security</a> • 393 <a href="../www/architecture.html">Architecture</a> • 394 <a href="../www/links.html">Related</a> • 395 <a href="../www/sitemap.html">Sitemap</a> 396 397 <a href="magick-wand.html#"><img class="d-inline" id="wand" alt="And Now a Touch of Magick" width="16" height="16" src="../images/wand.ico"/></a> 398 399 <a href="http://pgp.mit.edu/pks/lookup?op=get&search=0x89AB63D48277377A">Public Key</a> • 400 <a href="../www/support.html">Donate</a> • 401 <a href="../www/https://imagemagick.org/script/contact.php">Contact Us</a> 402 <br/> 403 <small>© 1999-2019 ImageMagick Studio LLC</small></p> 404 </footer> 405 406 <!-- Javascript assets --> 407 <script src="assets/magick.js" crossorigin="anonymous"></script> 408 <script>window.jQuery || document.write('<script src="https://localhost/ajax/libs/jquery/3.3.1/jquery.min.js"><\/script>')</script> 409</body> 410</html> 411