• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>ImageMagick - MagickCore, Low-level C API</title>
11  <meta name="application-name" content="ImageMagick" />
12  <meta name="description" content="Use ImageMagick® to create, edit, compose, and convert bitmap images. Resize an image, crop it, change its shades and colors, add captions, and more." />
13  <meta name="application-url" content="https://imagemagick.org" />
14  <meta name="generator" content="PHP" />
15  <meta name="keywords" content="magickcore, low-level, c, 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-2020 ImageMagick Studio LLC" />
23  <meta name="distribution" content="Global" />
24  <meta name="magick-serial" content="P131-S030410-R485315270133-P82224-A6668-G1245-1" />
25  <meta property='og:url' content='../' />
26  <meta property='og:title' content='ImageMagick' />
27  <meta property='og:image' content='../images/logo.png' />
28  <meta property='og:type' content='website' />
29  <meta property='og:site_name' content='ImageMagick' />
30  <meta property='og:description' content="Convert, Edit, or Compose Bitmap Images" />
31  <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" />
32  <link href="../www/magick-core.html" rel="canonical" />
33  <link href="../images/wand.png" rel="icon" />
34  <link href="../images/wand.ico" rel="shortcut icon" />
35  <link href="assets/magick.css" rel="stylesheet" />
36</head>
37<body>
38  <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
39    <a class="navbar-brand" href="../index.html"><img class="d-block" id="icon" alt="ImageMagick" width="32" height="32" src="../images/wand.ico"/></a>
40    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsMagick" aria-controls="navbarsMagick" aria-expanded="false" aria-label="Toggle navigation">
41      <span class="navbar-toggler-icon"></span>
42    </button>
43
44    <div class="navbar-collapse collapse" id="navbarsMagick" style="">
45    <ul class="navbar-nav mr-auto">
46      <li class="nav-item ">
47        <a class="nav-link" href="../index.html">Home <span class="sr-only">(current)</span></a>
48      </li>
49      <li class="nav-item ">
50        <a class="nav-link" href="download.html">Download</a>
51      </li>
52      <li class="nav-item ">
53        <a class="nav-link" href="command-line-tools.html">Tools</a>
54      </li>
55      <li class="nav-item ">
56        <a class="nav-link" href="command-line-processing.html">Command-line</a>
57      </li>
58      <li class="nav-item ">
59        <a class="nav-link" href="resources.html">Resources</a>
60      </li>
61      <li class="nav-item ">
62        <a class="nav-link" href="develop.html">Develop</a>
63      </li>
64      <li class="nav-item">
65        <a class="nav-link" target="_blank" href="https://imagemagick.org/discourse-server/">Community</a>
66      </li>
67    </ul>
68    <form class="form-inline my-2 my-lg-0" action="https://imagemagick.org/script/search.php">
69      <input class="form-control mr-sm-2" type="text" name="q" placeholder="Search" aria-label="Search">
70      <button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="sa">Search</button>
71    </form>
72    </div>
73  </nav>
74  <div class="container">
75   <script async="async" src="http://localhost/pagead/js/adsbygoogle.js"></script>    <ins class="adsbygoogle"
76         style="display:block"
77         data-ad-client="ca-pub-3129977114552745"
78         data-ad-slot="6345125851"
79         data-full-width-responsive="true"
80         data-ad-format="horizontal"></ins>
81    <script>
82      (adsbygoogle = window.adsbygoogle || []).push({});
83    </script>
84
85  </div>
86
87  <main class="container">
88    <div class="magick-template">
89<div class="magick-header">
90
91<p  class="lead magick-description">The <a href="api/MagickCore/index.html">MagickCore API</a> is a low-level interface between the C programming language and the ImageMagick image processing libraries and is recommended for wizard-level programmers only.  Unlike the <a href="magick-wand.html">MagickWand</a> C API which uses only a few opaque types and accessors, with MagickCore you almost exlusively access the structure members directly.  A description of the MagickCore public methods are found here:</p>
92
93<ul>
94  <li><a href="api/magick.html">Initialize or Destroy the ImageMagick Environment</a></li>
95  <li><a href="api/constitute.html">Constitute an Image</a></li>
96  <li><a href="api/composite.html">Composite an Image</a></li>
97  <li><a href="api/image.html">Image Methods</a></li>
98  <li><a href="api/channel.html">Image Channel Methods</a></li>
99  <li><a href="api/color.html">Count the Colors in an Image</a></li>
100  <li><a href="api/colormap.html">Colormap Methods</a></li>
101  <li><a href="api/colorspace.html">Colorspace Methods</a></li>
102  <li><a href="api/distort.html">Image Distortions</a></li>
103  <li><a href="api/layer.html">Dealing with Image Layers</a></li>
104  <li><a href="api/profile.html">Dealing with Image Profiles</a></li>
105  <li><a href="api/quantize.html">Reduce the Number of Unique Colors in an Image</a></li>
106  <li><a href="api/histogram.html">Image Histograms</a></li>
107  <li><a href="api/segment.html">Segment an Image with Thresholding Fuzzy c-Means</a></li>
108  <li><a href="api/resize.html">Resize an Image</a></li>
109  <li><a href="api/transform.html">Transform an Image</a></li>
110  <li><a href="api/shear.html">Shear or Rotate an Image by an Arbitrary Angle</a></li>
111  <li><a href="api/enhance.html">Enhance an Image</a></li>
112  <li><a href="api/effect.html">Add an Effect</a></li>
113  <li><a href="api/morphology.html">Morphological Erosions, Dilations, Openings, and Closings</a></li>
114  <li><a href="api/fx.html">Add a Special Effect</a></li>
115  <li><a href="api/decorate.html">Decorate an Image</a></li>
116  <li><a href="api/attribute.html">Get/Set an Image Attribute</a></li>
117  <li><a href="api/property.html">Get/Set Image Properties</a></li>
118  <li><a href="api/statistic.html">Get Image Statistics</a></li>
119  <li><a href="api/feature.html">Get Image Features</a></li>
120  <li><a href="api/annotate.html">Annotate an Image</a></li>
121  <li><a href="api/paint.html">Paint on an Image</a></li>
122  <li><a href="api/draw.html">Draw on an Image</a></li>
123  <li><a href="api/montage.html">Create an Image Thumbnail</a></li>
124  <li><a href="api/fourier.html">Compute the discrete Fourier transform (DFT)</a></li>
125  <li><a href="api/compare.html">Compare an Image to a Reconstructed Image</a></li>
126  <li><a href="api/display.html">Interactively Display and Edit an Image</a></li>
127  <li><a href="api/animate.html">Interactively Animate an Image Sequence</a></li>
128  <li><a href="api/cipher.html">Convert to and from Cipher Pixels</a></li>
129  <li><a href="api/list.html">Working with Image Lists</a></li>
130  <li><a href="api/image-view.html">Image View Methods</a></li>
131  <li><a href="api/cache.html">Get or Set Image Pixels</a></li>
132  <li><a href="api/cache-view.html">Working with Cache Views</a></li>
133  <li><a href="api/stream.html">The Pixel FIFO</a></li>
134  <li><a href="api/blob.html">Read or Write Binary Large OBjects</a></li>
135  <li><a href="api/module.html">Loadable Modules</a></li>
136  <li><a href="api/signature.html">Compute a Message Digest for an Image</a></li>
137  <li><a href="api/registry.html">The Image Registry</a></li>
138  <li><a href="api/exception.html">Dealing with Exceptions</a></li>
139  <li><a href="api/memory.html">Memory Allocation</a></li>
140  <li><a href="api/resource.html">Monitor or Limit Resource Consumption</a></li>
141  <li><a href="api/monitor.html">Monitor the Progress of an Image Operation</a></li>
142  <li><a href="api/version.html">Get the Version and Copyrights</a></li>
143  <li><a href="api/mime.html">Mime Methods</a></li>
144  <li><a href="api/deprecate.html">Deprecated Methods</a></li>
145  <li><a href="../www/exception.html">Error and Warning Codes</a></li>
146</ul>
147
148<p>After you write your MagickCore program, compile it like this:</p>
149
150<pre class="highlight"><code>cc -o core core.c `pkg-config --cflags --libs MagickWand`
151</code></pre>
152
153<p>Set the <code>PKG_CONFIG_PATH</code> environment variable if ImageMagick is not in your default system path:</p>
154
155<pre class="highlight"><code>export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
156</code></pre>
157
158<p>Here is a example program that utilizes the MagickCore API to get you started, <a href="https://imagemagick.org/source/core.c">core.c</a>. It reads a GIF image, creates a thumbnail, and writes it to disk in the PNG image format.</p>
159
160<pre class="pre-scrollable"><code>#include &lt;stdio.h>
161#include &lt;stdlib.h>
162#include &lt;string.h>
163#include &lt;time.h>
164#include &lt;MagickCore/MagickCore.h>
165
166int main(int argc,char **argv)
167{
168  ExceptionInfo
169    *exception;
170
171  Image
172    *image,
173    *images,
174    *resize_image,
175    *thumbnails;
176
177  ImageInfo
178    *image_info;
179
180  if (argc != 3)
181    {
182      (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
183      exit(0);
184    }
185  /*
186    Initialize the image info structure and read an image.
187  */
188  MagickCoreGenesis(*argv,MagickTrue);
189  exception=AcquireExceptionInfo();
190  image_info=CloneImageInfo((ImageInfo *) NULL);
191  (void) strcpy(image_info-&gt;filename,argv[1]);
192  images=ReadImage(image_info,exception);
193  if (exception-&gt;severity != UndefinedException)
194    CatchException(exception);
195  if (images == (Image *) NULL)
196    exit(1);
197  /*
198    Convert the image to a thumbnail.
199  */
200  thumbnails=NewImageList();
201  while ((image=RemoveFirstImageFromList(&amp;images)) != (Image *) NULL)
202  {
203    resize_image=ResizeImage(image,106,80,LanczosFilter,exception);
204    if (resize_image == (Image *) NULL)
205      MagickError(exception-&gt;severity,exception-&gt;reason,exception-&gt;description);
206    (void) AppendImageToList(&amp;thumbnails,resize_image);
207    DestroyImage(image);
208  }
209  /*
210    Write the image thumbnail.
211  */
212  (void) strcpy(thumbnails-&gt;filename,argv[2]);
213  WriteImage(image_info,thumbnails,exception);
214  /*
215    Destroy the image thumbnail and exit.
216  */
217  thumbnails=DestroyImageList(thumbnails);
218  image_info=DestroyImageInfo(image_info);
219  exception=DestroyExceptionInfo(exception);
220  MagickCoreTerminus();
221  return(0);
222}</code></pre>
223<p><a class="anchor" id="image-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/core/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>
224
225<pre class="pre-scrollable"><code>#include &lt;stdio.h>
226#include &lt;stdlib.h>
227#include &lt;math.h>
228#include &lt;MagickCore/MagickCore.h>
229
230static MagickBooleanType SigmoidalContrast(ImageView *contrast_view,
231  const ssize_t y,const int id,void *context)
232{
233#define QuantumScale  ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
234#define SigmoidalContrast(x) \
235  (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
236
237  RectangleInfo
238    extent;
239
240  register IndexPacket
241    *indexes;
242
243  register PixelPacket
244    *pixels;
245
246  register ssize_t
247    x;
248
249  extent=GetImageViewExtent(contrast_view);
250  pixels=GetImageViewAuthenticPixels(contrast_view);
251  for (x=0; x &lt; (ssize_t) (extent.width-extent.x); x++)
252  {
253    SetPixelRed(pixels,RoundToQuantum(SigmoidalContrast(GetPixelRed(pixels)));
254    SetPixelGreen(pixels,RoundToQuantum(SigmoidalContrast(GetPixelGreen(pixels)));
255    SetPixelBlue(pixels,RoundToQuantum(SigmoidalContrast(GetPixelBlue(pixels)));
256    SetPixelOpacity(pixels,RoundToQuantum(SigmoidalContrast(GetPixelOpacity(pixels)));
257    pixels++;
258  }
259  indexes=GetImageViewAuthenticIndexes(contrast_view);
260  if (indexes != (IndexPacket *) NULL)
261    for (x=0; x &lt; (ssize_t) (extent.width-extent.x); x++)
262      SetPixelIndex(indexes+x,RoundToQuantum(SigmoidalContrast(GetPixelIndex(indexes+x))));
263  return(MagickTrue);
264}
265
266int main(int argc,char **argv)
267{
268#define ThrowImageException(image) \
269{ \
270 \
271  CatchException(exception); \
272  if (contrast_image != (Image *) NULL) \
273    contrast_image=DestroyImage(contrast_image); \
274  exit(-1); \
275}
276#define ThrowViewException(view) \
277{ \
278  char \
279    *description; \
280 \
281  ExceptionType \
282    severity; \
283 \
284  description=GetImageViewException(view,&amp;severity); \
285  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
286  description=DestroyString(description); \
287  exit(-1); \
288}
289
290  ExceptionInfo
291    *exception;
292
293  Image
294    *contrast_image;
295
296  ImageInfo
297    *image_info;
298
299  ImageView
300    *contrast_view;
301
302  MagickBooleanType
303    status;
304
305  if (argc != 3)
306    {
307      (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
308      exit(0);
309    }
310  /*
311    Read an image.
312  */
313  MagickCoreGenesis(*argv,MagickTrue);
314  image_info=AcquireImageInfo();
315  (void) CopyMagickString(image_info->filename,argv[1],MaxTextExtent);
316  exception=AcquireExceptionInfo();
317  contrast_image=ReadImage(image_info,exception);
318  if (contrast_image == (Image *) NULL)
319    ThrowImageException(contrast_image);
320  /*
321    Sigmoidal non-linearity contrast control.
322  */
323  contrast_view=NewImageView(contrast_image);
324  if (contrast_view == (ImageView *) NULL)
325    ThrowImageException(contrast_image);
326  status=UpdateImageViewIterator(contrast_view,SigmoidalContrast,(void *) NULL);
327  if (status == MagickFalse)
328    ThrowImageException(contrast_image);
329  contrast_view=DestroyImageView(contrast_view);
330  /*
331    Write the image then destroy it.
332  */
333  status=WriteImages(image_info,contrast_image,argv[2],exception);
334  if (status == MagickFalse)
335    ThrowImageException(contrast_image);
336  contrast_image=DestroyImage(contrast_image);
337  exception=DestroyExceptionInfo(exception);
338  image_info=DestroyImageInfo(image_info);
339  MagickCoreTerminus();
340  return(0);
341}</code></pre>
342</div>
343    </div>
344  </main><!-- /.container -->
345  <footer class="magick-footer">
346    <p><a href="security-policy.html">Security</a> •
347    <a href="architecture.html">Architecture</a>
348
349    <a href="magick-core.html#"><img class="d-inline" id="wand" alt="And Now a Touch of Magick" width="16" height="16" src="../images/wand.ico"/></a>
350
351    <a href="links.html">Related</a> •
352     <a href="sitemap.html">Sitemap</a>
353    <br/>
354    <a href="support.html">Donate</a> •
355    <a href="http://pgp.mit.edu/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a> •
356    <a href="https://imagemagick.org/script/contact.php">Contact Us</a>
357    <br/>
358    <small>© 1999-2020 ImageMagick Studio LLC</small></p>
359  </footer>
360
361  <!-- Javascript assets -->
362  <script src="assets/magick.js" crossorigin="anonymous"></script>
363  <script>window.jQuery || document.write('<script src="https://localhost/ajax/libs/jquery/3.4.1/jquery.min.js"><\/script>')</script>
364</body>
365</html>
366<!-- Magick Cache 5th January 2020 09:22 -->