]> granicus.if.org Git - imagemagick/blob - www/fx.html
(no commit message)
[imagemagick] / www / fx.html
1
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
3   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html version="-//W3C//DTD XHTML 1.1//EN"
5       xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
6       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7       xsi:schemaLocation="http://www.w3.org/1999/xhtml
8                           http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd">
9 <head>
10   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
11   <meta name=viewport content="width=device-width, initial-scale=0.3333334">
12   <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4"/>
13   <title>ImageMagick: The Fx Special Effects Image Operator</title>
14   <meta http-equiv="content-language" content="en-US"/>
15   <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
16   <meta http-equiv="reply-to" content="magick-users@imagemagick.org"/>
17   <meta name="application-name" content="ImageMagick"/>
18   <meta name="description" content="A collection of tools and libraries to read, write, and manipulate an image in any of the more popular image formats."/>
19   <meta name="application-url" content="http://www.imagemagick.org"/>
20   <meta name="generator" content="PHP"/>
21   <meta name="keywords" content="the, fx, special, effects, image, operator, convert, edit, compose, image, pixel"/>
22   <meta name="rating" content="GENERAL"/>
23   <meta name="robots" content="INDEX, FOLLOW"/>
24   <meta name="generator" content="ImageMagick Studio LLC"/>
25   <meta name="author" content="ImageMagick Studio LLC"/>
26   <meta name="revisit-after" content="2 DAYS"/>
27   <meta name="resource-type" content="document"/>
28   <meta name="copyright" content="Copyright (c) 1999-2012 ImageMagick Studio LLC"/>
29   <meta name="distribution" content="Global"/>
30   <link rel="icon" href="../images/wand.png"/>
31   <link rel="shortcut icon" href="../images/wand.ico" type="images/x-icon"/>
32   <link rel="canonical" href="http://www.imagemagick.org" />
33   <link rel="meta" type="application/rdf+xml" title="ICI" href="http://imagemagick.org/ici.rdf"/>
34
35   <!-- Add jQuery library -->
36   <script type="text/javascript" src="http://localhost/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
37   
38   <!-- Add mousewheel plugin (this is optional) -->
39   <script type="text/javascript" src="http://www.imagemagick.org/fancybox/lib/jquery.mousewheel-3.0.6.pack.js"></script>
40   
41   <!-- Add fancyBox -->
42   <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/jquery.fancybox.css?v=2.1.5" type="text/css" media="screen" />
43   <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
44   
45   <!-- Optionally add helpers - button, thumbnail and/or media -->
46   <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-buttons.css?v=1.0.5" type="text/css" media="screen" />
47   <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-buttons.js?v=1.0.5"></script>
48   <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-media.js?v=1.0.6"></script>
49   
50   <link rel="stylesheet" href="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-thumbs.css?v=1.0.7" type="text/css" media="screen" />
51   <script type="text/javascript" src="http://www.imagemagick.org/fancybox/source/helpers/jquery.fancybox-thumbs.js?v=1.0.7"></script>
52   
53   <script type="text/javascript">
54         $(document).ready(function() {
55                 $(".fancybox").fancybox();
56         });
57   </script>
58
59   <!-- ImageMagick style -->
60   <style type="text/css" media="all">
61     @import url("../www/magick.css");
62   </style>
63 </head>
64
65 <body id="www-imagemagick-org">
66
67 <script type="text/javascript">
68   (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
69   (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
70   m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
71   })(window,document,'offline-script','//www.google-analytics.com/analytics.js','ga');
72
73   ga('create', 'UA-40133312-1', 'imagemagick.org');
74   ga('send', 'pageview');
75 </script>
76
77 <div class="titlebar">
78 <div style="margin: 17px auto; float: left;">
79   <script type="text/javascript">
80   <!--
81     google_ad_client = "pub-3129977114552745";
82     google_ad_slot = "5439289906";
83     google_ad_width = 728;
84     google_ad_height = 90;
85   //-->
86   </script>
87   <script type="text/javascript"
88     src="http://localhost/pagead/show_ads.js">
89   </script>
90 </div>
91 <a href="../discourse-server">
92   <img src="../images/logo.jpg"
93   alt="ImageMagick Logo" width="123" height="118"
94   style="width: 123px; height: 118px; border: 0px; float: right;" /></a>
95 <a href="http://www.imagemagick.org/ImageMagick-7.0.0/index.html">
96   <img src="../images/sprite.jpg"
97   alt="ImageMagick Sprite" width="114" height="118"
98   style="width: 114px; height: 118px; border: 0px; float: right;" /></a>
99 </div>
100
101 <div class="westbar">
102
103 <div class="menu">
104   <a title="About ImageMagick" href="http://www.imagemagick.org/ImageMagick-7.0.0/index.html">About ImageMagick</a>
105 </div>
106 <div class="menu">
107   <a title="Binary Releases" href="binary-releases.html">Binary Releases</a>
108 </div>
109 <div class="sub">
110     <a title="Binary Release: Unix" href="binary-releases.html#unix">Unix</a>
111 </div>
112 <div class="sub">
113     <a title="Binary Release: MacOS X" href="binary-releases.html#macosx">Mac OS X</a>
114 </div>
115 <div class="sub">
116     <a title="Binary Release: iOS" href="binary-releases.html#iOS">iOS</a>
117 </div>
118 <div class="sub">
119     <a title="Binary Release: Windows" href="binary-releases.html#windows">Windows</a>
120 </div>
121 <div class="sep"></div>
122 <div class="menu">
123   <a title="Command-line Tools" href="command-line-tools.html">Command-line Tools</a>
124 </div>
125 <div class="sub">
126     <a title="Command-line Tools: Processing" href="command-line-processing.html">Processing</a>
127 </div>
128 <div class="sub">
129     <a title="Command-line Tools: Options" href="command-line-options.html">Options</a>
130 </div>
131 <div class="sub">
132     <a title="Command-line Tools: Usage" href="http://www.imagemagick.org/Usage/">Usage</a>
133 </div>
134 <div class="menu">
135   <a title="Program Interfaces" href="api.html">Program Interfaces</a>
136 </div>
137 <div class="sub">
138     <a title="Program Interface: MagickWand" href="magick-wand.html">MagickWand</a>
139 </div>
140 <div class="sub">
141     <a title="Program Interface: MagickCore" href="magick-core.html">MagickCore</a>
142 </div>
143 <div class="sub">
144     <a title="Program Interface: PerlMagick" href="perl-magick.html">PerlMagick</a>
145 </div>
146 <div class="sub">
147     <a title="Program Interface: Magick++" href="magick++.html">Magick++</a>
148 </div>
149 <div class="sep"></div>
150 <div  class="menu">
151    <a title="Install from Source" href="install-source.html">Install from Source</a>
152 </div>
153 <div class="sub">
154     <a title="Install from Source: Unix" href="install-source.html#unix">Unix</a>
155 </div>
156 <div class="sub">
157     <a title="Install from Source: Windows" href="install-source.html#windows">Windows</a>
158  </div>
159 <div class="menu">
160   <a title="Resources" href="resources.html">Resources</a>
161 </div>
162 <div class="menu">
163   <a title="Architecture" href="architecture.html">Architecture</a>
164 </div>
165 <div class="menu">
166   <a title="Download" href="download.html">Download</a>
167 </div>
168 <div class="sep"></div>
169 <div class="menu">
170   <a title="Search" href="search.html">Search</a>
171 </div>
172 <div class="sep"></div>
173 <div class="menu">
174   <a title="Site Map" href="sitemap.html">Site Map</a>
175 </div>
176 <div  class="sub">
177   <a title="Site Map: Links" href="links.html">Links</a>
178 </div>
179 <div class="sep"></div>
180 </div>
181
182 <div class="eastbar">
183   <script type="text/javascript">
184     /* <![CDATA[ */
185     window.___gcfg = {
186       lang: 'en-US'
187     };
188
189     (function() {
190       var po = document.createElement('offline-script');
191       po.type = 'text/javascript';
192       po.async = true;
193       po.src = 'https://apis.google.com/js/plusone.js';
194       var script = document.getElementsByTagName('offline-script')[0];
195       script.parentNode.insertBefore(po, script);
196     })();
197     var gplusone = document.getElementById("gplusone");
198     gplusone.setAttribute("data-size","medium");
199     gplusone.setAttribute("data-count","true");
200     /* ]]> */
201   </script>
202   <script type="text/javascript">
203     /* <![CDATA[ */
204     (function() {
205         var po = document.createElement('offline-script'), t = document.getElementsByTagName('offline-script')[0];
206         po.type = 'text/javascript';
207         po.async = true;
208         po.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
209         var script = document.getElementsByTagName('offline-script')[0];
210         script.parentNode.insertBefore(po, script);
211     })();
212     /* ]]> */
213   </script>
214   <div> <a class="FlattrButton" style="display:none;" rev="flattr;button:compact;" href="http://www.imagemagick.org/"></a></div>
215   <noscript>
216     <a href="http://flattr.com/thing/947300/Convert-Edit-And-Compose-Images" target="_blank">
217     <img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" width="93" height="20" border="0" /></a>
218   </noscript>
219   <div class="g-plusone" data-size="standard" data-count="true"></div>
220 </div>
221
222 <div class="main">
223
224 <h1>Apply Special Effects to an Image with an Fx Expression</h1>
225 <p class="navigation-index">[<a href="fx.html#fx">The Fx Special Effects Image Operator</a> • <a href="fx.html#anatomy">The Anatomy of an Fx Expression</a>]</p>
226
227 <a id="fx"></a>
228 <div class="doc-section">
229
230 <p>Use the Fx special effects image operator to apply a mathematical expression to an image or image channels.  Use Fx to:</p>
231
232 <ul>
233   <li>create canvases, gradients, mathematical colormaps</li>
234   <li>move color values between images and channels</li>
235   <li>translate, flip, mirror, rotate, scale, shear and generally distort images</li>
236   <li>merge or composite multiple images together</li>
237   <li>convolve or merge neighboring pixels together</li>
238   <li>generate image metrics or 'fingerprints'</li>
239 </ul>
240
241 <p>The expression can be simple:</p>
242
243 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -size 64x64 canvas:black -channel blue -fx "1/2" fx_navy.png</span></p>
244 <p>Here, we convert a black to a navy blue image:</p>
245
246 <p class="image">
247   <a href="../images/black.png"><img src="../images/black.png" width="64" height="64" alt="black" /></a>
248   <img style="margin-top:22px; margin-bottom:22px;" src="../images/right.gif" width="20" height="20" alt="==>" />
249   <a href="../images/navy.png"><img src="../images/navy.png" width="64" height="64" alt="navy" /></a>
250 </p>
251
252 <p>Or the expression can be complex:</p>
253
254 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert rose.jpg \  <br/>
255           -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" \ <br/>
256           rose-sigmoidal.png</span></p>
257 <p>This expression results in a high contrast version of the image:</p>
258
259 <p class="image">
260   <a href="../images/rose.jpg"><img src="../images/rose.jpg" width="70" height="46" alt="rose" /></a>
261   <img style="margin-top:13px; margin-bottom:13px;" src="../images/right.gif" width="20" height="20" alt="==>" />
262   <a href="../images/rose-sigmoidal.png"><img src="../images/rose-sigmoidal.png" width="70" height="46" alt="rose-sigmoidal" /></a>
263 </p>
264
265 <p>The expression can include variable assignments.  Assignments, in most cases, reduce the complexity of an expression and permit some operations that might not be possible any other way.  For example, lets create a radial gradient:</p>
266
267 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -size 70x70 canvas: \  <br/>
268           -fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5" \ <br/>
269           radial-gradient.png</span></p>
270 <p>The command above returns this image:</p>
271
272 <p class="image">
273   <a href="../images/radial-gradient.png"><img src="../images/radial-gradient.png" width="70" height="70" alt="radial-gradient" /></a>
274 </p>
275
276 <p>This FX expression adds random noise to an image:</p>
277
278 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert photo.jpg -fx 'iso=32; rone=rand(); rtwo=rand(); \<br />
279      myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))* \<br />
280      cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* \ <br />
281      channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)' noisy.png</span></p>
282 <p>See <a href="http://www.imagemagick.org/Usage/transform/index.html#fx">Using FX, The Special Effects Image Operator</a> for more examples.</p>
283
284 <p>The next section discusses the Fx expression language.</p>
285
286 </div>
287
288 <h2><a id="anatomy"></a>The Anatomy of an Fx Expression</h2>
289 <div class="doc-section">
290
291 <h3>The Fx Expression Language</h3>
292 <div class="doc-section">
293
294 <p>The formal Fx expression language is defined here:</p>
295
296 <dl class="doc">
297   <dt class="doc"> numbers:</dt>
298         <dd> integer, floating point, scientfic notation (+/- required, e.g. 3.81469e-06), International System number postfixes (.e.g KB, Mib, GB, etc.)</dd>
299   <dt class="doc"> constants: </dt>
300     <dd> E (Euler's number), Epsilon, QuantumRange, QuantumScale, Opaque, Phi (golden ratio), Pi, Transparent</dd>
301   <dt class="doc"> Fx operators (in order of precedence): </dt>
302      <dd> ^ (power), unary -, *, /, % (modulo), +, -,
303      &lt;&lt;, &gt;&gt;, &lt;, &lt;=, &gt;, &gt;=, ==, !=,
304      &amp; (bitwise AND),   | (bitwise OR),
305      &amp;&amp; (logical AND),  || (logical OR),
306      ~ (logical NOT),  ?: (ternary conditional)</dd>
307   <dt class="doc"> math functions: </dt>
308      <dd> abs(), acos(), acosh(), airy(), alt(), asin(), asinh(), atan(), atanh(), atan2(), ceil(), cos(), cosh(), debug(), drc(), exp(), floor(), gauss(), gcd(), hypot(), int(), isnan(), j0(), j1(), jinc(), ln(), log(), logtwo(), max(), min(), mod(), not(), pow(), rand(), round(), sign(), sin(), sinc(), sinh(), sqrt(), squish(), tan(), tanh(), trunc()</dd>
309   <dt class="doc"> channel functions: </dt>
310     <dd> channel(r,g,b,a), channel(c,m,y,k,a)</dd>
311   <dt class="doc"> color names:</dt>
312     <dd> red, cyan, black, etc.</dd>
313   <dt class="doc"> color functions:</dt>
314     <dd> srgb(), srgba(), rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla(), etc.</dd>
315   <dt class="doc"> color hex values:</dt>
316     <dd> #ccc, #cbfed0, #b9e1cc00, etc.</dd>
317   <dt class="doc"> symbols:</dt>
318      <dd style="white-space:pre">
319      u = first image in list
320      v = second image in list
321      s = current image in list (for %[fx:] otherwise = u)
322      t = index of current image (s) in list
323      n = number of images in list
324
325      i = column offset
326      j = row offset
327      p = pixel to use (absolute or relative to current pixel)
328
329      w = width of this image
330      h = height of this image
331      z = channel depth
332
333      r = red value (from RGBA), of a specific or current pixel
334      g = green    ''
335      b = blue     ''
336      a = alpha    ''
337      o = opacity  ''
338
339      c = cyan value of CMYK color of pixel
340      y = yellow   ''
341      m = magenta  ''
342      k = black    ''
343
344      intensity = pixel intensity
345
346      hue = pixel hue
347      saturation = pixel saturation
348      lightness = pixel lightness
349      luma = pixel luma
350
351      page.width = page width
352      page.height = page height
353      page.x = page x offset
354      page.y = page y offset
355
356      resolution.x = x resolution
357      resolution.y = y resolution
358
359      depth = image depth
360      minima = image minima
361      maxima = image maxima
362      mean = image mean
363      standard_deviation = image standard deviation
364      kurtosis = image kurtosis
365      skewness = image skewness
366        (add a channel specifier to compute a statistic for that channel, e.g. depth.r)
367      </dd>
368   <dt class="doc"> iterators:</dt>
369     <dd> while()</dd>
370 </dl>
371
372 </div>
373
374 <h3>The Fx Expression</h3>
375 <div class="doc-section">
376
377 <p>An Fx expression may include any combination of the following:</p>
378 <ul>
379 <li> <em>x</em> <code>^</code> <em>y</em>:  exponentiation (<em>x<sup>y</sup></em>)</li>
380 <li> <code>(</code> ... <code>)</code>:  grouping</li>
381 <li> <em>x</em> <code>*</code> <em>y</em>:  multiplication (the asterisk <code>*</code> is optional, for example, <code>2u</code> or <code>2(x+y)</code> are acceptable)</li>
382 <li> <em>x</em> <code>/</code> <em>y</em>:  division</li>
383 <li> <em>x</em> <code>%</code> <em>y</em>:  modulo</li>
384 <li> <em>x</em> <code>+</code> <em>y</em>:  addition</li>
385 <li> <em>x</em> <code>-</code> <em>y</em>:  subtraction</li>
386 <li> <em>x</em> <code>&lt;&lt;</code> <em>y</em>:  left shift</li>
387 <li> <em>x</em> <code>&gt;&gt;</code> <em>y</em>:  right shift</li>
388 <li> <em>x</em> <code>&lt;</code> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &lt; <em>y</em>,  otherwise 0.0</li>
389 <li> <em>x</em> <code>&lt;=</code> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &lt;= <em>y</em>,  otherwise 0.0</li>
390 <li> <em>x</em> <code>&gt;</code> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &gt; <em>y</em>,  otherwise 0.0</li>
391 <li> <em>x</em> <code>&gt;=</code> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &gt;= <em>y</em>,  otherwise 0.0</li>
392 <li> <em>x</em> <code>==</code> <em>y</em>:  boolean relation, return value 1.0 if <em>x </em>==<em> y</em>, otherwise 0.0</li>
393 <li> <em>x</em> <code>!=</code> <em>y</em>:  boolean relation, return value 1.0 if <em>x </em>!=<em> y</em>, otherwise 0.0</li>
394 <li> <em>x</em> <code>&amp;</code> <em>y</em>:  binary AND</li>
395 <li> <em>x</em> <code>|</code> <em>y</em>:  binary OR</li>
396 <li> <em>x</em> <code>&amp;&amp;</code> <em>y</em>:  logical AND connective, return value 1.0 if <em>x</em> &gt; 0 and <em>y</em> &gt; 0,  otherwise 0.0</li>
397 <li> <em>x</em> <code>||</code> <em>y</em>:  logical OR connective (inclusive), return value 1.0 if <em>x</em> &gt; 0 or <em>y</em> &gt; 0 (or both),  otherwise 0.0</li>
398 <li> <code>~</code><em>x</em>:  logical NOT operator, return value 1.0 if <em>not</em> <em>x</em> &gt; 0,  otherwise 0.0</li>
399 <li> <code>+</code><em>x</em>:  unary plus, return 1.0*value</li>
400 <li> <code>-</code><em>x</em>:  unary minus, return -1.0*value</li>
401 <li> <em>x</em> <code>?</code> <em>y</em> : <em>z</em>: ternary conditional expression, return value <em>y</em> if <em>x</em> != 0, otherwise <em>z</em>; only one ternary conditional permitted per statement</li>
402 <li> <em>x</em> <code>=</code> <em>y</em>: assignment; assignment variables are restricted to letter combinations only (e.g. Xi not X1)</li>
403 <li> <em>x</em> <code>;</code> <em>y</em>: statement separator </li>
404 <li> <code>phi</code>:  constant (1.618034...)</li>
405 <li> <code>pi</code>:  constant (3.141659...)</li>
406 <li> <code>e</code>:  constant (2.71828...)</li>
407 <li> <code>QuantumRange</code>:  constant maximum pixel value (255 for Q8, 65535 for Q16)</li>
408 <li> <code>QuantumScale</code>:  constant 1.0/<code>QuantumRange</code></li>
409 <li> <code>intensity</code>:  pixel intensity; equivalent to 0.299*red+0.587*green+0.114*blue</li>
410 <li> <code>hue</code>:  pixel hue</li>
411 <li> <code>saturation</code>:  pixel saturation</li>
412 <li> <code>lightness</code>:  pixel lightness; equivalent to 0.5*max(red,green,blue) + 0.5*min(red,green,blue)</li>
413 <li> <code>luminance</code>:  pixel luminance; equivalent to 0.212656*red + 0.715158*green + 0.072186*blue</li>
414 <li> <code>red, green, blue</code>, etc.:  color names</li>
415 <li> <code>#ccc, #cbfed0, #b9e1cc00</code>, etc.:  color hex values</li>
416 <li> <code>rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</code>:  color functions</li>
417 <li> <code>s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k</code>:  symbols</li>
418 <li> <code>abs(</code><em>x</em><code>)</code>:  absolute value function</li>
419 <li> <code>acos(</code><em>x</em><code>)</code>:  arc cosine function</li>
420 <li> <code>acosh(</code><em>x</em><code>)</code>:  inverse hyperbolic cosine function</li>
421 <li> <code>airy(</code><em>x</em><code>)</code>:  Airy function (max=1, min=0); airy(<em>x</em>)=[jinc(<em>x</em>)]<sup>2</sup>=[2*j1(<em>pi*x</em>)/(<em>pi*x</em>)]<sup>2</sup></li>
422 <li> <code>alt(</code><em>x</em><code>)</code>:  sign alternation function (return 1.0 if int(<em>x</em>) is even, -1.0 if int(<em>x</em>) is odd)</li>
423 <li> <code>asin(</code><em>x</em><code>)</code>:  arc sine function</li>
424 <li> <code>asinh(</code><em>x</em><code>)</code>:  inverse hyperbolic sine function</li>
425 <li> <code>atan(</code><em>x</em><code>)</code>:  arc tangent function</li>
426 <li> <code>atanh(</code><em>x</em><code>)</code>:  inverse hyperbolic tangent function</li>
427 <li> <code>atan2(</code><em>y</em>,<em>x</em><code>)</code>:  arc tangent function of two variables</li>
428 <li> <code>ceil(</code><em>x</em><code>)</code>: smallest integral value not less than argument</li>
429 <li> <code>channel(</code><em>r</em>,<em>g</em>,<em>b</em>,<em>a</em><code>)</code>: select numeric argument based on current channel</li>
430 <li> <code>channel(</code><em>c</em>,<em>m</em>,<em>y</em>,<em>k</em>,<em>a</em><code>)</code>: select numeric argument based on current channel</li>
431 <li> <code>cos(</code><em>x</em><code>)</code>:  cosine function</li>
432 <li> <code>cosh(</code><em>x</em><code>)</code>:  hyperbolic cosine function</li>
433 <li> <code>debug(</code><em>x</em><code>)</code>:  print <em>x</em> (useful for debugging your expression)</li>
434 <li> <code>drc(</code><em>x</em>,<em>y</em><code>)</code>:  dynamic range compression (knee curve); drc(<em>x</em>,<em>y</em>)=(<em>x</em>)/(<em>y</em>*(<em>x</em>-1)+1); -1&lt;<em>y</em>&lt;1 </li>
435 <li> <code>exp(</code><em>x</em><code>)</code>:  natural exponential function (<em>e<sup>x</sup></em>)</li>
436 <li> <code>floor(</code><em>x</em><code>)</code>:  largest integral value not greater than argument</li>
437 <li> <code>gauss(</code><em>x</em><code>)</code>:  gaussian function; gauss(<em>x</em>)=exp(<em>-x*x/2</em>)/sqrt(2*pi)</li>
438 <li> <code>gcd(</code><em>x</em>,<em>y</em><code>)</code>:  greatest common denominator</li>
439 <li> <code>hypot(</code><em>x</em>,<em>y</em><code>)</code>:  the square root of x<sup>2</sup>+y<sup>2</sup></li>
440 <li> <code>int(</code><em>x</em><code>)</code>:  greatest integer function (return greatest integer less than or equal to <em>x</em>)</li>
441 <li> <code>isnan(</code><em>x</em><code>)</code>: return 1.0 if <em>x</em> is NAN, 0.0 otherwise</li>
442 <li> <code>j0(</code><em>x</em><code>)</code>:  Bessel functions of <em>x</em> of the first kind of order 0</li>
443 <li> <code>j1(</code><em>x</em><code>)</code>:  Bessel functions of <em>x</em> of the first kind of order 1</li>
444 <li> <code>jinc(</code><em>x</em><code>)</code>:  jinc function (max=1, min=-0.1323); jinc(<em>x</em>)=2*j1(pi*<em>x</em>)/(pi*<em>*x</em>)</li>
445 <li> <code>ln(</code><em>x</em><code>)</code>:  natural logarithm function</li>
446 <li> <code>log(</code><em>x</em><code>)</code>:  logarithm base 10</li>
447 <li> <code>logtwo(</code><em>x</em><code>)</code>:  logarithm base 2</li>
448 <li> <code>ln(</code><em>x</em><code>)</code>:  natural logarithm</li>
449 <li> <code>max(</code><em>x</em>, <em>y</em><code>)</code>:  maximum of <em>x</em> and <em>y</em></li>
450 <li> <code>min(</code><em>x</em>, <em>y</em><code>)</code>:  minimum of <em>x</em> and <em>y</em></li>
451 <li> <code>mod(</code><em>x</em>, <em>y</em><code>)</code>:  floating-point remainder function</li>
452 <li> <code>not(</code><em>x</em><code>)</code>:  return 1.0 if <em>x</em> is zero, 0.0 otherwise</li>
453 <li> <code>pow(</code><em>x</em>,<em>y</em><code>)</code>:  power function (<em>x<sup>y</sup></em>)</li>
454 <li> <code>rand()</code>:  value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period</li>
455 <li> <code>round()</code>:  round to integral value, regardless of rounding direction</li>
456 <li> <code>sign(</code><em>x</em><code>)</code>:  return -1.0 if <em>x</em> is less than 0.0 otherwise 1.0</li>
457 <li> <code>sin(</code><em>x</em><code>)</code>:  sine function</li>
458 <li> <code>sinc(</code><em>x</em><code>)</code>:  sinc function (max=1, min=-0.21); sinc(<em>x</em>)=sin(<em>pi*x</em>)/(<em>pi*x</em>)</li>
459 <li> <code>squish(</code><em>x</em><code>)</code>:  squish function; squish(<em>x</em>)=1.0/(1.0+exp(<em>-x</em>))</li>
460 <li> <code>sinh(</code><em>x</em><code>)</code>:  hyperbolic sine function</li>
461 <li> <code>sqrt(</code><em>x</em><code>)</code>:  square root function</li>
462 <li> <code>tan(</code><em>x</em><code>)</code>:  tangent function</li>
463 <li> <code>tanh(</code><em>x</em><code>)</code>:  hyperbolic tangent function</li>
464 <li> <code>trunc(</code><em>x</em><code>)</code>:  round to integer, towards zero</li>
465 <li> <code>while(</code><em>condition</em>,<em>expression</em><code>)</code>:  interate while the condition is not equal to 0</li>
466 </ul>
467
468 <p>The expression semantics include these rules:</p>
469
470 <ul>
471 <li>symbols are case insensitive</li>
472 <li>only one ternary conditional (e.g. x ? y : z) per statement</li>
473 <li>statements are assignments or the final expression to return</li>
474 <li>an assignment starts a statement, it is not an operator</li>
475 <li>assignments to built-ins do not throw an exception and have no effect;  e.g. <code>r=3.0; r</code> returns the pixel red color value, not 3.0</li>
476 <li>Unary operators have a lower priority than binary operators, that is, the unary minus (negation) has lower precedence than exponentiation, so -3^2 is interpreted as -(3^2) = -9.  Use parentheses to clarify your intent (e.g. (-3)^2 = 9).</li>
477 <li>Similarly, care must be exercised when using the slash ('/') symbol. The string of characters <em>1/2x</em> is interpreted as (1/2)x. The contrary interpretation should be written explicitly as 1/(2x). Again, the use of parentheses helps clarify the meaning and should be used whenever there is any chance of misinterpretation.</li>
478 </ul>
479
480
481 </div>
482
483 <h3>Source Images</h3>
484 <div class="doc-section">
485
486 <p>The symbols <code>u</code> and <code>v</code> refer to the first and second images, respectively, in the current image sequence.  Refer to a particular image in a sequence by appending its index to any image reference (usually <code>u</code>), with a zero index for the beginning of the sequence. A negative index counts from the end.  For example, <code>u[0]</code> is the first image in the sequence, <code>u[2]</code> is the third, <code>u[-1]</code> is the last image, and <code>u[t]</code> is the current image. The current image can also be referenced by <code>s</code>. If the sequence number exceeds the length of the sequence, the count is wrapped. Thus in a 3-image sequence,  <code>u[-1]</code>, <code>u[2]</code>, and <code>u[5]</code> all refer to the same (third) image.</p>
487
488 <p>As an example, we form an image by averaging the first image and third images (the second (index 1) image is ignored and just junked):</p>
489
490 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert image1.jpg image2.jpg image3.jpg -fx "(u+u[2])/2.0" image.jpg</span></p>
491 <p>By default, the image to which <code>p</code>, <code>r</code>, <code>g</code>, <code>b</code>, <code>a</code>, etc., are applied is the current image <code>s</code> in the image list. This is equivelent to <code>u</code> except when used in an escape sequence <code>%[fx:...]</code>. </p>
492
493 <p>It is important to note the special role played by the first image. This is the only image in the image sequence that is modified, other images are used only for their data. As an illustrative example, consider the following, and note that the setting <a href="command-line-options.html#channel">-channel red</a> instructs <a href="command-line-options.html#fx">-fx</a> to modify only the red channel; nothing in the green or blue channels will change. It is instructive to ponder why the result is not symmetric.</p>
494
495 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -channel red logo: -flop logo: -resize "20%"  -fx "(u+v)/2" image.jpg</span></p>
496 <div class="image">
497   <a href="../images/logo-sm-flop.png"><img src="../images/logo-sm-flop.png" width="128" height="96" alt="logo-sm-flop.png" /></a>
498   <a href="../images/logo-sm.png"><img src="../images/logo-sm.png" width="128" height="96" alt="logo-sm.png" /></a>
499 <img style="margin-top:38px; margin-bottom:38px;" src="../images/right.gif" width="20" height="20" alt="==>" />
500   <a href="../images/logo-sm-fx.png"><img src="../images/logo-sm-fx.png" width="128" height="96" alt="logo-sm-fx.png" /></a>
501 </div>
502
503
504 </div>
505
506 <h3>Accessing Pixels</h3>
507 <div class="doc-section">
508
509 <p>All color values are normalized to the range of 0.0 to 1.0.  The alpha channel ranges from 0.0 (fully transparent) to 1.0 (fully opaque).</p>
510
511 <p>The pixels are processed one at a time, but a different pixel of an image can be specified using a pixel index represented by <code>p</code>. For example,</p>
512
513 <pre class="text">
514    p[-1].g      green value of pixel to the immediate left of the current pixel
515    p[-1,-1].r   red value of the pixel diagonally left and up from current pixel
516 </pre>
517
518 <p>To specify an absolute position, use braces, rather than brackets.</p>
519
520 <pre class="text">
521   p{0,0}.r     red value of the pixel in the upper left corner of the image
522   p{12,34}.b   blue pixel value at column number 12, row 34 of the image
523 </pre>
524
525 <p>Integer values of the position retrieve the color of the pixel referenced, while non-integer position values return a blended color according to the current <a href="command-line-options.html#interpolate">-interpolate</a> setting.</p>
526
527 <p>A position outside the boundary of the image retrieves a value dictated by the <a href="command-line-options.html#virtual-pixel">-virtual-pixel</a> option setting.</p>
528
529 </div>
530
531 <h3>Apply an Expression to Select Image Channels</h3>
532 <div class="doc-section">
533
534 <p>Use the <a href="command-line-options.html#channel">-channel</a> setting to specify the output channel of the result. If no output channel is given, the result is set over all channels except the opacity channel. For example, to replace the red channel of <code>alpha.png</code> with the average of the green channels from the images <code>alpha.png</code> and <code>beta.png</code>, use:</p>
535
536 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert alpha.png beta.png -channel red -fx "(u.g+v.g)/2" gamma.png</span></p>
537 </div>
538
539 <h3>Results</h3>
540 <div class="doc-section">
541
542 <p>The <a href="command-line-options.html#fx">-fx</a> operator evaluates the given expression for each channel (set by <a href="command-line-options.html#channel">-channel</a>) of each pixel in the first image (<code>u</code>) in the sequence. The computed values are temporarily stored in a copy (clone) of that first image until all the pixels have been processed, after which this single new image replaces the list of images in the current image sequence.  As such, in the previous example the updated version of <code>alpha.png</code> replaces both of the original images, <code>alpha.png</code> and <code>beta.png</code>, before being saved as <code>gamma.png</code>.</p>
543
544 <p>The current image <code>s</code> is set to the first image in the sequence (<code>u</code>), and <code>t</code> to its index, 0.  The symbols <code>i</code> and <code>j</code> reference the current pixel being processed.</p>
545
546
547 <p>For use with <a href="command-line-options.html#format_identify_">-format</a>, the value-escape <code>%[fx: ]</code> is evaluated just once for each image in the current image sequence. As each image in the sequence is being evaluated, <code>s</code> and <code>t</code> successively refer to the current image and its index, while <code>i</code> and <code>j</code> are set to zero, and the current channel set to red (<a href="command-line-options.html#channel">-channel</a> is ignored). An example:</p>
548
549 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert  canvas:'rgb(25%,50%,75%)' rose: -colorspace rgb  \ <br/>
550   -format 'Red channel of NW corner of image #%[fx:t] is %[fx: s]' info:</span><span class='crtout'>Red channel of NW corner of image #0 is 0.453758 <br/>
551   Red channel of NW corner of image #1 is 0.184588</span></p>
552 <p>Here we use the image indexes to <em>rotate</em> each image differently, and use <code>-set</code> with the image index to set a different <em>pause delay</em> on the first image in the animation:</p>
553
554 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert rose: -duplicate 29 -virtual-pixel Gray -distort SRT '%[fx:360.0*t/n]' \ <br />
555     -set delay '%[fx:t == 0 ? 240 : 10]' -loop 0 rose.gif</span></p><p>The color-escape <code>%[pixel: ]</code> is evaluated once per image and per color channel in that image (<a href="command-line-options.html#channel">-channel</a> is ignored), The values generated are then converted into a color string (a named color or hex color value).  The symbols <code>i</code> and <code>j</code> are set to zero, and <code>s</code> and <code>t</code> refer to each successively current image and index.</p>
556
557 </div>
558 </div>
559
560 </div>
561
562 <div id="linkbar">
563     <span id="linkbar-west"> </span>
564
565     <span id="linkbar-center">
566       <a href="../discourse-server">Discourse Server</a> •
567       <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a> •
568       <a href="http://www.imagemagick.org/MagickStudio/scripts/MagickStudio.cgi">Studio</a> •
569       <a href="http://jqmagick.imagemagick.org/">JqMagick</a>
570     </span>
571     <span id="linkbar-east"> </span>
572 </div>
573 <div class="footer">
574   <span id="footer-west">©  1999-2015 ImageMagick Studio LLC</span>
575   <span id="footer-east"> <a href="contact.html">Contact the Wizards</a></span>
576 </div>
577 <div style="clear: both; margin: 0; width: 100%; "></div>
578 </body>
579 </html>