• 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>MagickCore, Low-level 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="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-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-core.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/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>
86
87<ul>
88  <li><a href="api/magick.html">Initialize or Destroy the ImageMagick Environment</a></li>
89  <li><a href="api/constitute.html">Constitute an Image</a></li>
90  <li><a href="api/composite.html">Composite an Image</a></li>
91  <li><a href="api/image.html">Image Methods</a></li>
92  <li><a href="api/channel.html">Image Channel Methods</a></li>
93  <li><a href="api/color.html">Count the Colors in an Image</a></li>
94  <li><a href="api/colormap.html">Colormap Methods</a></li>
95  <li><a href="api/colorspace.html">Colorspace Methods</a></li>
96  <li><a href="api/distort.html">Image Distortions</a></li>
97  <li><a href="api/layer.html">Dealing with Image Layers</a></li>
98  <li><a href="api/profile.html">Dealing with Image Profiles</a></li>
99  <li><a href="api/quantize.html">Reduce the Number of Unique Colors in an Image</a></li>
100  <li><a href="api/histogram.html">Image Histograms</a></li>
101  <li><a href="api/segment.html">Segment an Image with Thresholding Fuzzy c-Means</a></li>
102  <li><a href="api/resize.html">Resize an Image</a></li>
103  <li><a href="api/transform.html">Transform an Image</a></li>
104  <li><a href="api/shear.html">Shear or Rotate an Image by an Arbitrary Angle</a></li>
105  <li><a href="api/enhance.html">Enhance an Image</a></li>
106  <li><a href="api/effect.html">Add an Effect</a></li>
107  <li><a href="api/morphology.html">Morphological Erosions, Dilations, Openings, and Closings</a></li>
108  <li><a href="api/fx.html">Add a Special Effect</a></li>
109  <li><a href="api/decorate.html">Decorate an Image</a></li>
110  <li><a href="api/attribute.html">Get/Set an Image Attribute</a></li>
111  <li><a href="api/property.html">Get/Set Image Properties</a></li>
112  <li><a href="api/statistic.html">Get Image Statistics</a></li>
113  <li><a href="api/feature.html">Get Image Features</a></li>
114  <li><a href="api/annotate.html">Annotate an Image</a></li>
115  <li><a href="api/paint.html">Paint on an Image</a></li>
116  <li><a href="api/draw.html">Draw on an Image</a></li>
117  <li><a href="api/montage.html">Create an Image Thumbnail</a></li>
118  <li><a href="api/fourier.html">Compute the discrete Fourier transform (DFT)</a></li>
119  <li><a href="api/compare.html">Compare an Image to a Reconstructed Image</a></li>
120  <li><a href="api/display.html">Interactively Display and Edit an Image</a></li>
121  <li><a href="api/animate.html">Interactively Animate an Image Sequence</a></li>
122  <li><a href="api/cipher.html">Convert to and from Cipher Pixels</a></li>
123  <li><a href="api/list.html">Working with Image Lists</a></li>
124  <li><a href="api/image-view.html">Image View Methods</a></li>
125  <li><a href="api/cache.html">Get or Set Image Pixels</a></li>
126  <li><a href="api/cache-view.html">Working with Cache Views</a></li>
127  <li><a href="api/stream.html">The Pixel FIFO</a></li>
128  <li><a href="api/blob.html">Read or Write Binary Large OBjects</a></li>
129  <li><a href="api/module.html">Loadable Modules</a></li>
130  <li><a href="api/signature.html">Compute a Message Digest for an Image</a></li>
131  <li><a href="api/registry.html">The Image Registry</a></li>
132  <li><a href="api/exception.html">Dealing with Exceptions</a></li>
133  <li><a href="api/memory.html">Memory Allocation</a></li>
134  <li><a href="api/resource.html">Monitor or Limit Resource Consumption</a></li>
135  <li><a href="api/monitor.html">Monitor the Progress of an Image Operation</a></li>
136  <li><a href="api/version.html">Get the Version and Copyrights</a></li>
137  <li><a href="api/mime.html">Mime Methods</a></li>
138  <li><a href="api/deprecate.html">Deprecated Methods</a></li>
139  <li><a href="exception.html">Error and Warning Codes</a></li>
140</ul>
141
142<p>After you write your MagickCore program, compile it like this:</p>
143
144<pre class="highlight"><code>cc -o core core.c `pkg-config --cflags --libs MagickWand`
145</code></pre>
146
147<p>Set the <code>PKG_CONFIG_PATH</code> environment variable if ImageMagick is not in your default system path:</p>
148
149<pre class="highlight"><code>export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
150</code></pre>
151
152<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>
153
154<pre class="pre-scrollable"><code>#include &lt;stdio.h>
155#include &lt;stdlib.h>
156#include &lt;string.h>
157#include &lt;time.h>
158#include &lt;MagickCore/MagickCore.h>
159
160int main(int argc,char **argv)
161{
162  ExceptionInfo
163    *exception;
164
165  Image
166    *image,
167    *images,
168    *resize_image,
169    *thumbnails;
170
171  ImageInfo
172    *image_info;
173
174  if (argc != 3)
175    {
176      (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
177      exit(0);
178    }
179  /*
180    Initialize the image info structure and read an image.
181  */
182  MagickCoreGenesis(*argv,MagickTrue);
183  exception=AcquireExceptionInfo();
184  image_info=CloneImageInfo((ImageInfo *) NULL);
185  (void) strcpy(image_info-&gt;filename,argv[1]);
186  images=ReadImage(image_info,exception);
187  if (exception-&gt;severity != UndefinedException)
188    CatchException(exception);
189  if (images == (Image *) NULL)
190    exit(1);
191  /*
192    Convert the image to a thumbnail.
193  */
194  thumbnails=NewImageList();
195  while ((image=RemoveFirstImageFromList(&amp;images)) != (Image *) NULL)
196  {
197    resize_image=ResizeImage(image,106,80,LanczosFilter,exception);
198    if (resize_image == (Image *) NULL)
199      MagickError(exception-&gt;severity,exception-&gt;reason,exception-&gt;description);
200    (void) AppendImageToList(&amp;thumbnails,resize_image);
201    DestroyImage(image);
202  }
203  /*
204    Write the image thumbnail.
205  */
206  (void) strcpy(thumbnails-&gt;filename,argv[2]);
207  WriteImage(image_info,thumbnails,exception);
208  /*
209    Destroy the image thumbnail and exit.
210  */
211  thumbnails=DestroyImageList(thumbnails);
212  image_info=DestroyImageInfo(image_info);
213  exception=DestroyExceptionInfo(exception);
214  MagickCoreTerminus();
215  return(0);
216}</code></pre>
217<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>
218
219<pre class="pre-scrollable"><code>#include &lt;stdio.h>
220#include &lt;stdlib.h>
221#include &lt;math.h>
222#include &lt;MagickCore/MagickCore.h>
223
224static MagickBooleanType SigmoidalContrast(ImageView *contrast_view,
225  const ssize_t y,const int id,void *context)
226{
227#define QuantumScale  ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
228#define SigmoidalContrast(x) \
229  (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
230
231  RectangleInfo
232    extent;
233
234  register IndexPacket
235    *indexes;
236
237  register PixelPacket
238    *pixels;
239
240  register ssize_t
241    x;
242
243  extent=GetImageViewExtent(contrast_view);
244  pixels=GetImageViewAuthenticPixels(contrast_view);
245  for (x=0; x &lt; (ssize_t) (extent.width-extent.x); x++)
246  {
247    SetPixelRed(pixels,RoundToQuantum(SigmoidalContrast(GetPixelRed(pixels)));
248    SetPixelGreen(pixels,RoundToQuantum(SigmoidalContrast(GetPixelGreen(pixels)));
249    SetPixelBlue(pixels,RoundToQuantum(SigmoidalContrast(GetPixelBlue(pixels)));
250    SetPixelOpacity(pixels,RoundToQuantum(SigmoidalContrast(GetPixelOpacity(pixels)));
251    pixels++;
252  }
253  indexes=GetImageViewAuthenticIndexes(contrast_view);
254  if (indexes != (IndexPacket *) NULL)
255    for (x=0; x &lt; (ssize_t) (extent.width-extent.x); x++)
256      SetPixelIndex(indexes+x,RoundToQuantum(SigmoidalContrast(GetPixelIndex(indexes+x))));
257  return(MagickTrue);
258}
259
260int main(int argc,char **argv)
261{
262#define ThrowImageException(image) \
263{ \
264 \
265  CatchException(exception); \
266  if (contrast_image != (Image *) NULL) \
267    contrast_image=DestroyImage(contrast_image); \
268  exit(-1); \
269}
270#define ThrowViewException(view) \
271{ \
272  char \
273    *description; \
274 \
275  ExceptionType \
276    severity; \
277 \
278  description=GetImageViewException(view,&amp;severity); \
279  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
280  description=DestroyString(description); \
281  exit(-1); \
282}
283
284  ExceptionInfo
285    *exception;
286
287  Image
288    *contrast_image;
289
290  ImageInfo
291    *image_info;
292
293  ImageView
294    *contrast_view;
295
296  MagickBooleanType
297    status;
298
299  if (argc != 3)
300    {
301      (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
302      exit(0);
303    }
304  /*
305    Read an image.
306  */
307  MagickCoreGenesis(*argv,MagickTrue);
308  image_info=AcquireImageInfo();
309  (void) CopyMagickString(image_info->filename,argv[1],MaxTextExtent);
310  exception=AcquireExceptionInfo();
311  contrast_image=ReadImage(image_info,exception);
312  if (contrast_image == (Image *) NULL)
313    ThrowImageException(contrast_image);
314  /*
315    Sigmoidal non-linearity contrast control.
316  */
317  contrast_view=NewImageView(contrast_image);
318  if (contrast_view == (ImageView *) NULL)
319    ThrowImageException(contrast_image);
320  status=UpdateImageViewIterator(contrast_view,SigmoidalContrast,(void *) NULL);
321  if (status == MagickFalse)
322    ThrowImageException(contrast_image);
323  contrast_view=DestroyImageView(contrast_view);
324  /*
325    Write the image then destroy it.
326  */
327  status=WriteImages(image_info,contrast_image,argv[2],exception);
328  if (status == MagickFalse)
329    ThrowImageException(contrast_image);
330  contrast_image=DestroyImage(contrast_image);
331  exception=DestroyExceptionInfo(exception);
332  image_info=DestroyImageInfo(image_info);
333  MagickCoreTerminus();
334  return(0);
335}</code></pre>
336</div>
337    </div>
338  </main><!-- /.container -->
339  <footer class="magick-footer">
340    <p><a href="../www/security-policy.html">Security</a> •
341    <a href="../www/architecture.html">Architecture</a> •
342    <a href="../www/links.html">Related</a> •
343     <a href="../www/sitemap.html">Sitemap</a>
344
345    <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>
346
347    <a href="http://pgp.mit.edu/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a> •
348    <a href="../www/support.html">Donate</a> •
349    <a href="../www/https://imagemagick.org/script/contact.php">Contact Us</a>
350    <br/>
351    <small>© 1999-2019 ImageMagick Studio LLC</small></p>
352  </footer>
353
354  <!-- Javascript assets -->
355  <script src="assets/magick.js" crossorigin="anonymous"></script>
356  <script>window.jQuery || document.write('<script src="https://localhost/ajax/libs/jquery/3.3.1/jquery.min.js"><\/script>')</script>
357</body>
358</html>
359