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