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