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