]> granicus.if.org Git - imagemagick/blob - www/magick-core.html
(no commit message)
[imagemagick] / www / magick-core.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4   <meta charset="utf-8">
5   <meta http-equiv="X-UA-Compatible" content="IE=edge">
6   <meta name="viewport" content="width=device-width, initial-scale=1">
7   <title>ImageMagick: MagickCore, Low-level C API for ImageMagick</title>
8   <meta http-equiv="content-language" content="en-US">
9   <meta http-equiv="content-type" content="text/html; charset=utf-8">
10   <meta http-equiv="reply-to" content="magick-users@imagemagick.org">
11   <meta name="application-name" content="ImageMagick">
12   <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.">
13   <meta name="application-url" content="http://www.imagemagick.org">
14   <meta name="generator" content="PHP">
15   <meta name="keywords" content="magickcore, low-level, c, api, for, imagemagick, 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-2015 ImageMagick Studio LLC">
23   <meta name="distribution" content="Global">
24   <meta name="magick-serial" content="P131-S030410-R485315270133-P82224-A6668-G1245-1">
25   <link rel="icon" href="../images/wand.png">
26   <link rel="shortcut icon" href="../images/wand.ico" type="images/x-icon">
27   <link rel="stylesheet" href="css/bootstrap.min.css">
28   <link rel="stylesheet" href="../css/magick.css">
29 </head>
30
31 <body>
32 <div class="main">
33 <div class="magick-masthead">
34   <div class="container">
35     <script type="text/javascript">
36     <!--
37       google_ad_client = "pub-3129977114552745";
38       google_ad_slot = "5439289906";
39       google_ad_width = 728;
40       google_ad_height = 90;
41     //-->
42     </script>
43     <center><script type="text/javascript" src="http://localhost/pagead/show_ads.js">
44     </script></center>
45     <nav class="magick-nav">
46       <a class="magick-nav-item " href="../index.html">Home</a>
47       <a class="magick-nav-item " href="binary-releases.html">Download</a>
48       <a class="magick-nav-item " href="command-line-tools.html">Tools</a>
49       <a class="magick-nav-item " href="command-line-options.html">Options</a>
50       <a class="magick-nav-item " href="resources.html">Resources</a>
51       <a class="magick-nav-item " href="api.html">Develop</a>
52       <a class="magick-nav-item " href="http://nextgen.imagemagick.org/script/search.php">Search</a>
53       <a class="magick-nav-item pull-right" href="http://www.imagemagick.org/discourse-server/">Community</a>
54     </nav>
55   </div>
56 </div>
57 <div class="container">
58 <div class="magick-header">
59
60 <p class="lead magick-description">The <a href="http://nextgen.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>
61
62 <ul>
63   <li><a href="api/magick.html">Initialize or Destroy the ImageMagick Environment</a></li>
64   <li><a href="api/constitute.html">Constitute an Image</a></li>
65   <li><a href="api/composite.html">Composite an Image</a></li>
66   <li><a href="api/image.html">Image Methods</a></li>
67   <li><a href="api/channel.html">Image Channel Methods</a></li>
68   <li><a href="api/color.html">Count the Colors in an Image</a></li>
69   <li><a href="api/colormap.html">Colormap Methods</a></li>
70   <li><a href="api/colorspace.html">Colorspace Methods</a></li>
71   <li><a href="api/distort.html">Image Distortions</a></li>
72   <li><a href="api/layer.html">Dealing with Image Layers</a></li>
73   <li><a href="api/profile.html">Dealing with Image Profiles</a></li>
74   <li><a href="api/quantize.html">Reduce the Number of Unique Colors in an Image</a></li>
75   <li><a href="api/histogram.html">Image Histograms</a></li>
76   <li><a href="api/segment.html">Segment an Image with Thresholding Fuzzy c-Means</a></li>
77   <li><a href="api/resize.html">Resize an Image</a></li>
78   <li><a href="api/transform.html">Transform an Image</a></li>
79   <li><a href="api/shear.html">Shear or Rotate an Image by an Arbitrary Angle</a></li>
80   <li><a href="api/enhance.html">Enhance an Image</a></li>
81   <li><a href="api/effect.html">Add an Effect</a></li>
82   <li><a href="api/morphology.html">Morphological Erosions, Dilations, Openings, and Closings</a></li>
83   <li><a href="api/fx.html">Add a Special Effect</a></li>
84   <li><a href="api/decorate.html">Decorate an Image</a></li>
85   <li><a href="api/attribute.html">Get/Set an Image Attribute</a></li>
86   <li><a href="api/property.html">Get/Set Image Properties</a></li>
87   <li><a href="api/statistic.html">Get Image Statistics</a></li>
88   <li><a href="api/feature.html">Get Image Features</a></li>
89   <li><a href="api/annotate.html">Annotate an Image</a></li>
90   <li><a href="api/paint.html">Paint on an Image</a></li>
91   <li><a href="api/draw.html">Draw on an Image</a></li>
92   <li><a href="api/montage.html">Create an Image Thumbnail</a></li>
93   <li><a href="api/fourier.html">Compute the discrete Fourier transform (DFT)</a></li>
94   <li><a href="api/compare.html">Compare an Image to a Reconstructed Image</a></li>
95   <li><a href="api/display.html">Interactively Display and Edit an Image</a></li>
96   <li><a href="api/animate.html">Interactively Animate an Image Sequence</a></li>
97   <li><a href="api/cipher.html">Convert to and from Cipher Pixels</a></li>
98   <li><a href="api/list.html">Working with Image Lists</a></li>
99   <li><a href="api/image-view.html">Image View Methods</a></li>
100   <li><a href="api/cache.html">Get or Set Image Pixels</a></li>
101   <li><a href="api/cache-view.html">Working with Cache Views</a></li>
102   <li><a href="api/stream.html">The Pixel FIFO</a></li>
103   <li><a href="api/blob.html">Read or Write Binary Large OBjects</a></li>
104   <li><a href="api/module.html">Loadable Modules</a></li>
105   <li><a href="api/signature.html">Compute a Message Digest for an Image</a></li>
106   <li><a href="api/registry.html">The Image Registry</a></li>
107   <li><a href="api/exception.html">Dealing with Exceptions</a></li>
108   <li><a href="api/memory.html">Memory Allocation</a></li>
109   <li><a href="api/resource.html">Monitor or Limit Resource Consumption</a></li>
110   <li><a href="api/monitor.html">Monitor the Progress of an Image Operation</a></li>
111   <li><a href="api/version.html">Get the Version and Copyrights</a></li>
112   <li><a href="api/mime.html">Mime Methods</a></li>
113   <li><a href="api/deprecate.html">Deprecated Methods</a></li>
114   <li><a href="exception.html">Error and Warning Codes</a></li>
115 </ul>
116
117 <p>After you write your MagickCore program, compile it like this:</p>
118
119 <pre>
120 cc -o core core.c `pkg-config --cflags --libs MagickCore`
121 </pre>
122
123 <p>Set the <code>PKG_CONFIG_PATH</code> environment variable if ImageMagick is not in your default system path:</p>
124
125 <pre>
126 export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
127 </pre>
128
129 <p>Here is a example program that utilizes the MagickCore API to get you started, <a href="../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>
130
131 <pre class="pre-scrollable">#include &lt;stdio.h&gt;
132 #include &lt;stdlib.h&gt;
133 #include &lt;string.h&gt;
134 #include &lt;time.h&gt;
135 #include &lt;magick/MagickCore.h&gt;
136
137 int main(int argc,char **argv)
138 {
139   ExceptionInfo
140     *exception;
141
142   Image
143     *image,
144     *images,
145     *resize_image,
146     *thumbnails;
147
148   ImageInfo
149     *image_info;
150
151   if (argc != 3)
152     {
153       (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
154       exit(0);
155     }
156   /*
157     Initialize the image info structure and read an image.
158   */
159   MagickCoreGenesis(*argv,MagickTrue);
160   exception=AcquireExceptionInfo();
161   image_info=CloneImageInfo((ImageInfo *) NULL);
162   (void) strcpy(image_info-&gt;filename,argv[1]);
163   images=ReadImage(image_info,exception);
164   if (exception-&gt;severity != UndefinedException)
165     CatchException(exception);
166   if (images == (Image *) NULL)
167     exit(1);
168   /*
169     Convert the image to a thumbnail.
170   */
171   thumbnails=NewImageList();
172   while ((image=RemoveFirstImageFromList(&amp;images)) != (Image *) NULL)
173   {
174     resize_image=ResizeImage(image,106,80,LanczosFilter,1.0,exception);
175     if (resize_image == (Image *) NULL)
176       MagickError(exception-&gt;severity,exception-&gt;reason,exception-&gt;description);
177     (void) AppendImageToList(&amp;thumbnails,resize_image);
178     DestroyImage(image);
179   }
180   /*
181     Write the image thumbnail.
182   */
183   (void) strcpy(thumbnails-&gt;filename,argv[2]);
184   WriteImage(image_info,thumbnails);
185   /*
186     Destroy the image thumbnail and exit.
187   */
188   thumbnails=DestroyImageList(thumbnails);
189   image_info=DestroyImageInfo(image_info);
190   exception=DestroyExceptionInfo(exception);
191   MagickCoreTerminus();
192   return(0);
193 }</pre>
194 <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="../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>
195
196 <pre class="pre-scrollable">#include &lt;stdio.h&gt;
197 #include &lt;stdlib.h&gt;
198 #include &lt;math.h&gt;
199 #include &lt;magick/MagickCore.h&gt;
200
201 static MagickBooleanType SigmoidalContrast(ImageView *contrast_view,
202   const ssize_t y,const int id,void *context)
203 {
204 #define QuantumScale  ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
205 #define SigmoidalContrast(x) \
206   (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
207
208   RectangleInfo
209     extent;
210
211   register IndexPacket
212     *indexes;
213
214   register PixelPacket
215     *pixels;
216
217   register ssize_t
218     x;
219
220   extent=GetImageViewExtent(contrast_view);
221   pixels=GetImageViewAuthenticPixels(contrast_view);
222   for (x=0; x &lt; (ssize_t) (extent.width-extent.x); x++)
223   {
224     SetPixelRed(pixels,RoundToQuantum(SigmoidalContrast(GetPixelRed(pixels)));
225     SetPixelGreen(pixels,RoundToQuantum(SigmoidalContrast(GetPixelGreen(pixels)));
226     SetPixelBlue(pixels,RoundToQuantum(SigmoidalContrast(GetPixelBlue(pixels)));
227     SetPixelOpacity(pixels,RoundToQuantum(SigmoidalContrast(GetPixelOpacity(pixels)));
228     pixels++;
229   }
230   indexes=GetImageViewAuthenticIndexes(contrast_view);
231   if (indexes != (IndexPacket *) NULL)
232     for (x=0; x &lt; (ssize_t) (extent.width-extent.x); x++)
233       SetPixelIndex(indexes+x,RoundToQuantum(SigmoidalContrast(GetPixelIndex(indexes+x))));
234   return(MagickTrue);
235 }
236
237 int main(int argc,char **argv)
238 {
239 #define ThrowImageException(image) \
240 { \
241  \
242   CatchException(exception); \
243   if (contrast_image != (Image *) NULL) \
244     contrast_image=DestroyImage(contrast_image); \
245   exit(-1); \
246 }
247 #define ThrowViewException(view) \
248 { \
249   char \
250     *description; \
251  \
252   ExceptionType \
253     severity; \
254  \
255   description=GetImageViewException(view,&amp;severity); \
256   (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
257   description=DestroyString(description); \
258   exit(-1); \
259 }
260
261   ExceptionInfo
262     *exception;
263
264   Image
265     *contrast_image;
266
267   ImageInfo
268     *image_info;
269
270   ImageView
271     *contrast_view;
272
273   MagickBooleanType
274     status;
275
276   if (argc != 3)
277     {
278       (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
279       exit(0);
280     }
281   /*
282     Read an image.
283   */
284   MagickCoreGenesis(*argv,MagickTrue);
285   image_info=AcquireImageInfo();
286   (void) CopyMagickString(image_info-&gt;filename,argv[1],MaxTextExtent);
287   exception=AcquireExceptionInfo();
288   contrast_image=ReadImage(image_info,exception);
289   if (contrast_image == (Image *) NULL)
290     ThrowImageException(contrast_image);
291   /*
292     Sigmoidal non-linearity contrast control.
293   */
294   contrast_view=NewImageView(contrast_image);
295   if (contrast_view == (ImageView *) NULL)
296     ThrowImageException(contrast_image);
297   status=UpdateImageViewIterator(contrast_view,SigmoidalContrast,(void *) NULL);
298   if (status == MagickFalse)
299     ThrowImageException(contrast_image);
300   contrast_view=DestroyImageView(contrast_view);
301   /*
302     Write the image then destroy it.
303   */
304   status=WriteImages(image_info,contrast_image,argv[2],exception);
305   if (status == MagickFalse)
306     ThrowImageException(contrast_image);
307   contrast_image=DestroyImage(contrast_image);
308   exception=DestroyExceptionInfo(exception);
309   image_info=DestroyImageInfo(image_info);
310   MagickCoreTerminus();
311   return(0);
312 }</pre>
313 </div>
314   <footer class="magick-footer">
315     <div class="magick-nav-item pull-left">
316       <a href="support.html">Donate</a>
317     </div>
318     <p><a href="sitemap.html">Sitemap</a> •
319     <a href="links.html">Related</a> •
320     <a href="http://www.imagemagick.org/MagickStudio/scripts/MagickStudio.cgi">Image Studio</a> •
321     <a href="http://jqmagick.imagemagick.org/">JqMagick</a>
322 </p>
323     <p><a href="magick-core.html#">Back to top</a> •
324     <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a> •
325     <a href="http://nextgen.imagemagick.org/script/contact.php">Contact Us</a></p>
326     <p><small>©  1999-2015 ImageMagick Studio LLC</small></p>
327   </footer>
328 </div><!-- /.container -->
329
330   <script src="https://localhost/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
331   <script src="../js/bootstrap.min.js"></script>
332   <script type="text/javascript">
333     /* <![CDATA[ */
334     (function() {
335         var s = document.createElement('offline-script'), t = document.getElementsByTagName('offline-script')[0];
336         s.type = 'text/javascript';
337         s.async = true;
338         s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
339         t.parentNode.insertBefore(s, t);
340     })();
341     /* ]]> */
342   </script>
343 </div>
344 </body>
345 </html>