]> granicus.if.org Git - imagemagick/blob - www/fx.html
(no commit message)
[imagemagick] / www / fx.html
1 \r
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" \r
3   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\r
4 <html version="-//W3C//DTD XHTML 1.1//EN"\r
5       xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"\r
6       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
7       xsi:schemaLocation="http://www.w3.org/1999/xhtml\r
8                           http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd">\r
9 <head>\r
10   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\r
11   <meta name="google-site-verification" content="MxsMq6bdLOx0KSuz1MY6yG9ZTIJ7_7DVRfl5NCAT5Yg"/>\r
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 and 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, ImageMagic, MagickCore, MagickWand, PerlMagick, Magick++, RMagick, PythonMagick, JMagick, TclMagick, Image, Magick, Magic, Wand, ImageMagickObject, Image, Processing, Automagically"/>
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-2011 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" />\r
32   <link rel="meta" type="application/rdf+xml" title="ICI" href="http://imagemagick.org/ici.rdf"/>\r
33   <link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />\r
34   <link rel="stylesheet" type="text/css" href="http://www.imagemagick.org/fancybox/jquery.fancybox-1.3.4.css" media="screen" />\r
35   <style type="text/css" media="all">\r
36     @import url("../www/magick.css");\r
37   </style>\r
38   <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>\r
39   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>\r
40   <script type="text/javascript" src="http://www.imagemagick.org/fancybox/jquery.fancybox-1.3.4.pack.js"></script>\r
41   <script type="text/javascript">\r
42     $(document).ready(function() {\r
43       $("a[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox({\r
44         'transitionIn'  : 'elastic',\r
45         'transitionOut' : 'elastic',\r
46         'overlayShow'   : false,\r
47         'opacity'       : true\r
48       });\r
49     });\r
50   </script>\r
51 </head>\r
52 \r
53 <body id="www-imagemagick-org">\r
54 <div class="titlebar">\r
55 <div style="margin: 17px auto; float: left;">\r
56   <script type="text/javascript">\r
57   <!--\r
58     google_ad_client = "pub-3129977114552745";\r
59     google_ad_slot = "5439289906";\r
60     google_ad_width = 728;\r
61     google_ad_height = 90;\r
62   //-->\r
63   </script>\r
64   <script type="text/javascript"\r
65     src="http://pagead2.googlesyndication.com/pagead/show_ads.js">\r
66   </script>\r
67 </div>\r
68 <a href="http://www.imagemagick.org/discourse-server/">\r
69   <img src="../images/logo.jpg"\r
70   alt="ImageMagick Logo"\r
71   style="width: 123px; height: 118px; border: 0px; float: right;" /></a>\r
72 <a href="../index.html">\r
73   <img src="../images/sprite.jpg"\r
74   alt="ImageMagick Sprite"\r
75   style="width: 114px; height: 118px; border: 0px; float: right;" /></a>\r
76 </div>\r
77 \r
78 <div class="westbar">\r
79 \r
80 <div class="menu">
81   <a title="About ImageMagick" href="../index.html">About ImageMagick</a>
82 </div>
83 <div class="menu">
84   <a title="Binary Releases" href="../www/binary-releases.html">Binary Releases</a>
85 </div>
86 <div class="sub">
87     <a title="Binary Release: Unix" href="../www/binary-releases.html#unix">Unix</a>
88 </div>
89 <div class="sub">
90     <a title="Binary Release: MacOS X" href="../www/binary-releases.html#macosx">Mac OS X</a>
91 </div>
92 <div class="sub">
93     <a title="Binary Release: iOS" href="../www/binary-releases.html#iOS">iOS</a>
94 </div>
95 <div class="sub">
96     <a title="Binary Release: Windows" href="../www/binary-releases.html#windows">Windows</a>
97 </div>
98 <div class="sep"></div>\r
99 <div class="menu">
100   <a title="Command-line Tools" href="../www/command-line-tools.html">Command-line Tools</a>
101 </div>
102 <div class="sub">
103     <a title="Command-line Tools: Processing" href="../www/command-line-processing.html">Processing</a>
104 </div>
105 <div class="sub">
106     <a title="Command-line Tools: Options" href="../www/command-line-options.html">Options</a>
107 </div>
108 <div class="sub">
109     <a title="Command-line Tools: Usage" href="http://www.imagemagick.org/Usage/">Usage</a>
110 </div>
111 <div class="menu">
112   <a title="Program Interfaces" href="../www/api.html">Program Interfaces</a>
113 </div>
114 <div class="sub">
115     <a title="Program Interface: MagickWand" href="../www/magick-wand.html">MagickWand</a>
116 </div>
117 <div class="sub">
118     <a title="Program Interface: MagickCore" href="../www/magick-core.html">MagickCore</a>
119 </div>
120 <div class="sub">
121     <a title="Program Interface: PerlMagick" href="../www/perl-magick.html">PerlMagick</a>
122 </div>
123 <div class="sub">
124     <a title="Program Interface: Magick++" href="../www/magick++.html">Magick++</a>
125 </div>
126 <div class="sep"></div>\r
127 <div  class="menu">
128    <a title="Install from Source" href="../www/install-source.html">Install from Source</a>
129 </div>
130 <div class="sub">
131     <a title="Install from Source: Unix" href="../www/install-source.html#unix">Unix</a>
132 </div>
133 <div class="sub">
134     <a title="Install from Source: Windows" href="../www/install-source.html#windows">Windows</a>
135  </div>
136 <div class="menu">
137   <a title="Resources" href="../www/resources.html">Resources</a>
138 </div>
139 <div class="menu">
140   <a title="Architecture" href="../www/architecture.html">Architecture</a>
141 </div>
142 <div class="menu">
143   <a title="Download" href="../www/download.html">Download</a>
144 </div>
145 <div class="sep"></div>\r
146 <div class="menu">
147   <a title="Search" href="../www/search.html">Search</a>
148 </div>
149 <div class="sep"></div>\r
150 <div class="menu">
151   <a title="Site Map" href="../www/sitemap.html">Site Map</a>
152 </div>
153 <div  class="sub">
154   <a title="Site Map: Links" href="../www/links.html">Links</a>
155 </div>
156 <div class="sep"></div>\r
157 <div  class="menu">
158   <a rel="follow" title="Sponsors" href="../www/sponsors.html">Sponsors:</a>
159
160 <a href="http://www.networkredux.com">
161   <img src="../images/networkredux.png" width="140" height="31" alt="[sponsor]"
162   style="margin-top: 4px; margin-left: 4px; border: 0px; float: left;" /></a>
163 <div class="sponsbox">
164 <div  class="sponsor">
165   <a rel="follow" title="Sponsor: Autoteile" href="http://www.q11-shop.de">Autoteile</a><!-- 201205010540s suvalj -->
166 </div>
167 <div  class="sponsor">
168   <a rel="follow" title="Sponsor: Web Hosting" href="http://www.micfo.com">Web Hosting</a><!-- 209901010090s golestan -->
169 </div>
170 <div  class="sponsor">
171   <a rel="follow" title="Sponsor: Search engine optimisation" href="http://www.seomoves.com.au">Search engine optimisation</a><!-- 201110010090s jen@seo lowprofilelinks -->
172 </div>
173 <div  class="sponsor">
174   <a rel="follow" title="Sponsor: Druckerei" href="http://print24.com/de/">Druckerei</a><!-- 201110010720 -->
175 </div>
176 <div  class="sponsor">
177   <a rel="follow" title="Sponsor: Web Hosting Deals" href="http://www.webhostingdeals.org">Web Hosting Deals</a><!-- 201111010270 chee.hoa darin -->
178 </div>
179 <div  class="sponsor">
180   <a rel="follow" title="Sponsor: Autos part" href="http://www.pkwteile.de/autoteile">Autos part</a><!-- 201112010090s jr@pkwte... -->
181 </div>
182 <div  class="sponsor">
183   <a rel="follow" title="Sponsor: Web Hosting Reviews" href="http://www.webhostingsecretrevealed.com">Web Hosting Secret Revealed</a><!-- 201204010540 iamchee jerry -->
184 </div>
185 <div  class="sponsor">
186   <a rel="follow" title="Sponsor: Web Hosting Break" href="http://www.webhostingbreak.com">Web Hosting Break</a><!-- 201109010090 ian@ian-m... -->
187 </div>
188 <div  class="sponsor">
189   <a rel="follow" title="Sponsor: Druckerei" href="http://www.allesdruck.de">Druckerei</a><!-- 201303011500 r.leo -->
190 </div>
191 <div  class="sponsor">
192   <a rel="follow" title="Sponsor: Website Hosting" href="http://www.hostreviewgeeks.com">Website Hosting</a><!-- 201110010090 alexanian media -->
193 </div>
194 <div  class="sponsor">
195   <a rel="follow" title="Sponsor: Hotel München" href="http://www.messehotel-erb-muenchen.de">Hotel München</a><!-- 201111010450 cerb -->
196 </div>
197 <div  class="sponsor">
198   <a rel="follow" title="Sponsor: Best Web Hosting" href="http://webhostinggeeks.com">Best Web Hosting</a><!-- 201110010720 -->
199 </div>
200 <div  class="sponsor">
201   <a rel="follow" title="Sponsor: Flyer drucken" href="http://www.online-druck.biz">Flyer drucken</a><!-- 201109010900 Floeter-->
202 </div>
203 <div  class="sponsor">
204   <a rel="follow" title="Sponsor: Web Hosting Ratings" href="http://webhostingrating.com">Web Hosting Ratings</a><!-- 201110010720 -->
205 </div>
206 <div  class="sponsor">
207    <a rel="follow" title="Kaffeevollautomaten Reparatur Kundendienst Berlin" href="http://www.kaffeemaschinen-center.de">Kaffeevollautomaten Reparatur Berlin</a><!-- 2011090100025 c.karule-->
208 </div>
209 </div>
210 </div>
211 </div>\r
212 \r
213 <div class="eastbar">\r
214   <div class="g-plusone" data-size="standard" data-count="false"></div>\r
215 </div>\r
216 \r
217 <div class="main">\r
218 \r
219 <h1>Apply Special Effects to an Image with an Fx Expression</h1>
220 <p class="navigation-index">[<a href="#fx">The Fx Special Effects Image Operator</a> &bull; <a href="#anatomy">The Anatomy of an Fx Expression</a>]</p>
221
222 <a id="fx"></a>
223 <div class="doc-section">
224
225 <p>Use the Fx special effects image operator to apply a mathematical expression to an image or image channels.  Use Fx to:</p>
226
227 <ul>
228   <li>create canvases, gradients, mathematical colormaps</li>
229   <li>move color values between images and channels</li>
230   <li>translate, flip, mirror, rotate, scale, shear and generally distort images</li>
231   <li>merge or composite multiple images together</li>
232   <li>convolve or merge neighboring pixels together</li>
233   <li>generate image metrics or 'fingerprints'</li>
234 </ul>
235
236 <p>The expression can be simple:</p>
237
238 <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>
239 <p>Here, we convert a black to a navy blue image:</p>
240
241 <p class="image">
242   <a href="../images/black.png"><img src="../images/black.png" width="64" height="64" alt="black" /></a>
243   <img style="margin-top:22px; margin-bottom:22px;" src="../images/right.gif" width="20" height="20" alt="==>" />
244   <a href="../images/navy.png"><img src="../images/navy.png" width="64" height="64" alt="navy" /></a>
245 </p>
246
247 <p>Or the expression can be complex:</p>
248
249 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert rose.jpg \  <br/>
250           -fx "(1.0/(1.0+exp(10.0*(0.5-u)))-0.006693)*1.0092503" \ <br/>
251           rose-sigmoidal.png</span></p>
252 <p>This expression results in a high contrast version of the image:</p>
253
254 <p class="image">
255   <a href="../images/rose.jpg"><img src="../images/rose.jpg" width="70" height="46" alt="rose" /></a>
256   <img style="margin-top:13px; margin-bottom:13px;" src="../images/right.gif" width="20" height="20" alt="==>" />
257   <a href="../images/rose-sigmoidal.png"><img src="../images/rose-sigmoidal.png" width="70" height="46" alt="rose-sigmoidal" /></a>
258 </p>
259
260 <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>
261
262 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert -size 70x70 canvas: \  <br/>
263           -fx "Xi=i-w/2; Yj=j-h/2; 1.2*(0.5-hypot(Xi,Yj)/70.0)+0.5" \ <br/>
264           radial-gradient.png</span></p>
265 <p>The command above returns this image:</p>
266
267 <p class="image">
268   <a href="../images/radial-gradient.png"><img src="../images/radial-gradient.png" width="70" height="70" alt="radial-gradient" /></a>
269 </p>
270
271 <p>This FX expression adds random noise to an image:</p>
272
273 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert photo.jpg -fx 'iso=32; rone=rand(); rtwo=rand(); \<br />
274      myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))* \<br />
275      cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* \ <br />
276      channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)' noisy.png</span></p>
277 <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>
278
279 <p>The next section discusses the Fx expression language.</p>
280
281 </div>
282
283 <h2><a id="anatomy"></a>The Anatomy of an Fx Expression</h2>
284 <div class="doc-section">
285
286 <h3>The Fx Expression Language</h3>
287 <div class="doc-section">
288
289 <p>The formal Fx expression language is defined here:</p>
290
291 <dl class="doc">
292   <dt class="doc"> numbers:</dt>
293         <dd> integer, floating point, or scientfic notation (+/- required, e.g. 3.81469e-06)</dd>
294   <dt class="doc"> constants: </dt>
295     <dd> e, Epsilon, QuantumRange, QuantumScale, Opaque, Pi, Transparent</dd>
296   <dt class="doc"> Fx operators (in order of precedence): </dt>
297      <dd> ^ (power), unary -, *, /, % (modulo), +, -,
298      &lt;&lt;, &gt;&gt;, &lt;, &lt;=, &gt;, &gt;=, ==, !=,
299      &amp; (bitwise AND),   | (bitwise OR),
300      &amp;&amp; (logical AND),  || (logical OR),
301      ~ (logical NOT),  ?: (ternary conditional)</dd>
302   <dt class="doc"> math functions: </dt>
303      <dd> abs(), acos(), acosh(), airy(), alt(), asin(), asinh(), atan(), atanh(), atan2(), ceil(), cos(), cosh(),
304      debug(), exp(), floor(), hypot(), int(), j0(), j1(), jinc(), ln(), log(), logtwo(), max(), min(), mod(),
305      pow(), rand(), round(), sign(), sin(), sinc(), sinh(), sqrt(), tan(), tanh(), trunc()</dd>
306   <dt class="doc"> channel functions: </dt>
307     <dd> channel(r,g,b,a), channel(c,m,y,k,a)</dd>
308   <dt class="doc"> color names:</dt>
309     <dd> red, cyan, black, etc.</dd>
310   <dt class="doc"> color functions:</dt>
311     <dd> rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</dd>
312   <dt class="doc"> color hex values:</dt>
313     <dd> #ccc, #cbfed0, #b9e1cc00</dd>
314   <dt class="doc"> symbols:</dt>
315      <dd style="white-space:pre">
316      s = current image in sequence
317      t = scene number of current image in sequence
318      u = first image in sequence
319      v = second image in sequence
320      n = number of images in sequence
321
322      i = column offset
323      j = row offset
324      p = pixel to use (absolute or relative to current pixel)
325
326      w = width of this image
327      h = height of this image
328      z = channel depth
329
330      r = red value (from RGBA), of a specific or current pixel
331      g = green    ''
332      b = blue     ''
333      a = alpha    ''
334      o = opacity  ''
335
336      c = cyan value of CMYK color of pixel
337      y = yellow   ''
338      m = magenta  ''
339      k = black    ''
340
341      intensity = pixel intensity
342
343      hue = pixel hue
344      saturation = pixel saturation
345      lightness = pixel lightness
346      luminance = pixel luminance
347
348      page.width = page width
349      page.height = page height
350      page.x = page x offset
351      page.y = page y offset
352
353      resolution.x = x resolution
354      resolution.y = y resolution
355
356      depth = image depth
357      minima = image minima
358      maxima = image maxima
359      mean = image mean
360      standard_deviation = image standard deviation
361      kurtosis = image kurtosis
362      skewness = image skewness
363        (add a channel specifier to compute a statistic for that channel, e.g. depth.r)
364      </dd>
365 </dl>
366
367 </div>
368
369 <h3>The Fx Expression</h3>
370 <div class="doc-section">
371
372 <p>An Fx expression may include any combination of the following:</p>
373 <ul>
374 <li> <em>x</em> <kbd>^</kbd> <em>y</em>:  exponentiation (<em>x<sup>y</sup></em>)</li>
375 <li> <kbd>(</kbd> ... <kbd>)</kbd>:  grouping</li>
376 <li> <em>x</em> <kbd>*</kbd> <em>y</em>:  multiplication (the asterisk <kbd>*</kbd> is optional, for example, <kbd>2u</kbd> or <kbd>2(x+y)</kbd> are acceptable)</li>
377 <li> <em>x</em> <kbd>/</kbd> <em>y</em>:  division</li>
378 <li> <em>x</em> <kbd>%</kbd> <em>y</em>:  modulo</li>
379 <li> <em>x</em> <kbd>+</kbd> <em>y</em>:  addition</li>
380 <li> <em>x</em> <kbd>-</kbd> <em>y</em>:  subtraction</li>
381 <li> <em>x</em> <kbd>&lt;&lt;</kbd> <em>y</em>:  left shift</li>
382 <li> <em>x</em> <kbd>&gt;&gt;</kbd> <em>y</em>:  right shift</li>
383 <li> <em>x</em> <kbd>&lt;</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &lt; <em>y</em>,  otherwise 0.0</li>
384 <li> <em>x</em> <kbd>&lt;=</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &lt;= <em>y</em>,  otherwise 0.0</li>
385 <li> <em>x</em> <kbd>&gt;</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &gt; <em>y</em>,  otherwise 0.0</li>
386 <li> <em>x</em> <kbd>&gt;=</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x</em> &gt;= <em>y</em>,  otherwise 0.0</li>
387 <li> <em>x</em> <kbd>==</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x </em>==<em> y</em>, otherwise 0.0</li>
388 <li> <em>x</em> <kbd>!=</kbd> <em>y</em>:  boolean relation, return value 1.0 if <em>x </em>!=<em> y</em>, otherwise 0.0</li>
389 <li> <em>x</em> <kbd>&amp;</kbd> <em>y</em>:  binary AND</li>
390 <li> <em>x</em> <kbd>|</kbd> <em>y</em>:  binary OR</li>
391 <li> <em>x</em> <kbd>&amp;&amp;</kbd> <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>
392 <li> <em>x</em> <kbd>||</kbd> <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>
393 <li> <kbd>~</kbd><em>x</em>:  logical NOT operator, return value 1.0 if <em>not</em> <em>x</em> &gt; 0,  otherwise 0.0</li>
394 <li> <kbd>+</kbd><em>x</em>:  unary plus, return 1.0*value</li>
395 <li> <kbd>-</kbd><em>x</em>:  unary minus, return -1.0*value</li>
396 <li> <em>x</em> <kbd>?</kbd> <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>
397 <li> <em>x</em> <kbd>=</kbd> <em>y</em>: assignment; assignment variables are restricted to letter combinations only (e.g. Xi not X1)</li>
398 <li> <em>x</em> <kbd>;</kbd> <em>y</em>: statement separator </li>
399 <li> <kbd>pi</kbd>:  constant (3.141659...)</li>
400 <li> <kbd>e</kbd>:  constant (2.71828...)</li>
401 <li> <kbd>QuantumRange</kbd>:  constant maximum pixel value (255 for Q8, 65535 for Q16)</li>
402 <li> <kbd>QuantumScale</kbd>:  constant 1.0/<kbd>QuantumRange</kbd></li>
403 <li> <kbd>intensity</kbd>:  pixel intensity; equivalent to 0.299*red+0.587*green+0.114*blue</li>
404 <li> <kbd>hue</kbd>:  pixel hue</li>
405 <li> <kbd>saturation</kbd>:  pixel saturation</li>
406 <li> <kbd>lightness</kbd>:  pixel lightness; equivalent to 0.5*max(red,green,blue) + 0.5*min(red,green,blue)</li>
407 <li> <kbd>luminance</kbd>:  pixel luminance; equivalent to 0.2126*red + 0.7152*green + 0.0722*blue</li>
408 <li> <kbd>red, green, blue</kbd>, etc.:  color names</li>
409 <li> <kbd>#ccc, #cbfed0, #b9e1cc00</kbd>, etc.:  color hex values</li>
410 <li> <kbd>rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla()</kbd>:  color functions</li>
411 <li> <kbd>s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k</kbd>:  symbols</li>
412 <li> <kbd>abs(</kbd><em>x</em><kbd>)</kbd>:  absolute value function</li>
413 <li> <kbd>acos(</kbd><em>x</em><kbd>)</kbd>:  arc cosine function</li>
414 <li> <kbd>acosh(</kbd><em>x</em><kbd>)</kbd>:  inverse hyperbolic cosine function</li>
415 <li> <kbd>airy(</kbd><em>x</em><kbd>)</kbd>:  Airy function (max=1, min=0); <kbd>airy(</kbd><em>x</em><kbd>)</kbd>=<kbd>[jinc(</kbd><em>x</em><kbd>)]<sup>2</sup></kbd>=<kbd>[2*j1(</kbd><em>pi*x</em><kbd>)/(</kbd><em>pi*x</em><kbd>)]<sup>2</sup></kbd></li>
416 <li> <kbd>alt(</kbd><em>x</em><kbd>)</kbd>:  sign alternation function (return 1.0 if <kbd>int(</kbd><em>x</em><kbd>)</kbd> is even, -1.0 if <kbd>int(</kbd><em>x</em><kbd>)</kbd> is odd)</li>
417 <li> <kbd>asin(</kbd><em>x</em><kbd>)</kbd>:  arc sine function</li>
418 <li> <kbd>asinh(</kbd><em>x</em><kbd>)</kbd>:  inverse hyperbolic sine function</li>
419 <li> <kbd>atan(</kbd><em>x</em><kbd>)</kbd>:  arc tangent function</li>
420 <li> <kbd>atanh(</kbd><em>x</em><kbd>)</kbd>:  inverse hyperbolic tangent function</li>
421 <li> <kbd>atan2(</kbd><em>y</em>,<em>x</em><kbd>)</kbd>:  arc tangent function of two variables</li>
422 <li> <kbd>ceil(</kbd><em>x</em><kbd>)</kbd>: smallest integral value not less than argument</li>
423 <li> <kbd>channel(</kbd><em>r</em>,<em>g</em>,<em>b</em>,<em>a</em><kbd>)</kbd>: select numeric argument based on current channel</li>
424 <li> <kbd>channel(</kbd><em>c</em>,<em>m</em>,<em>y</em>,<em>k</em>,<em>a</em><kbd>)</kbd>: select numeric argument based on current channel</li>
425 <li> <kbd>cos(</kbd><em>x</em><kbd>)</kbd>:  cosine function</li>
426 <li> <kbd>cosh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic cosine function</li>
427 <li> <kbd>debug(</kbd><em>x</em><kbd>)</kbd>:  print <em>x</em> (useful for debugging your expression)</li>
428 <li> <kbd>exp(</kbd><em>x</em><kbd>)</kbd>:  natural exponential function (<em>e<sup>x</sup></em>)</li>
429 <li> <kbd>floor(</kbd><em>x</em><kbd>)</kbd>:  largest integral value not greater than argument</li>
430 <li> <kbd>hypot(</kbd><em>x</em>,<em>y</em><kbd>)</kbd>:  the square root of x<sup>2</sup>+y<sup>2</sup></li>
431 <li> <kbd>int(</kbd><em>x</em><kbd>)</kbd>:  greatest integer function (return greatest integer less than or equal to <em>x</em>)</li>
432 <li> <kbd>j0(</kbd><em>x</em><kbd>)</kbd>:  Bessel functions of <em>x</em> of the first kind of order 0</li>
433 <li> <kbd>j1(</kbd><em>x</em><kbd>)</kbd>:  Bessel functions of <em>x</em> of the first kind of order 1</li>
434 <li> <kbd>jinc(</kbd><em>x</em><kbd>)</kbd>:  jinc function (max=1, min=-0.1323); <kbd>jinc(</kbd><em>x</em><kbd>)</kbd>=<kbd>2*j1(</kbd><em>pi*x</em><kbd>)/(</kbd><em>pi*x</em><kbd>)</kbd></li>
435 <li> <kbd>ln(</kbd><em>x</em><kbd>)</kbd>:  natural logarithm function</li>
436 <li> <kbd>log(</kbd><em>x</em><kbd>)</kbd>:  logarithm base 10</li>
437 <li> <kbd>logtwo(</kbd><em>x</em><kbd>)</kbd>:  logarithm base 2</li>
438 <li> <kbd>ln(</kbd><em>x</em><kbd>)</kbd>:  natural logarithm</li>
439 <li> <kbd>max(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  maximum of <em>x</em> and <em>y</em></li>
440 <li> <kbd>min(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  minimum of <em>x</em> and <em>y</em></li>
441 <li> <kbd>mod(</kbd><em>x</em>, <em>y</em><kbd>)</kbd>:  floating-point remainder function</li>
442 <li> <kbd>pow(</kbd><em>x</em>,<em>y</em><kbd>)</kbd>:  power function (<em>x<sup>y</sup></em>)</li>
443 <li> <kbd>rand()</kbd>:  value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period</li>
444 <li> <kbd>round()</kbd>:  round to integral value, regardless of rounding direction</li>
445 <li> <kbd>sign(</kbd><em>x</em><kbd>)</kbd>:  return -1.0 if <em>x</em> is less than 0.0 otherwise 1.0</li>
446 <li> <kbd>sin(</kbd><em>x</em><kbd>)</kbd>:  sine function</li>
447 <li> <kbd>sinc(</kbd><em>x</em><kbd>)</kbd>:  sinc function (max=1, min=-0.21); <kbd>sinc(</kbd><em>x</em><kbd>)</kbd>=<kbd>sin(</kbd><em>pi*x</em><kbd>)/(</kbd><em>pi*x</em><kbd>)</kbd></li>
448 <li> <kbd>sinh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic sine function</li>
449 <li> <kbd>sqrt(</kbd><em>x</em><kbd>)</kbd>:  square root function</li>
450 <li> <kbd>tan(</kbd><em>x</em><kbd>)</kbd>:  tangent function</li>
451 <li> <kbd>tanh(</kbd><em>x</em><kbd>)</kbd>:  hyperbolic tangent function</li>
452 <li> <kbd>trunc(</kbd><em>x</em><kbd>)</kbd>:  round to integer, towards zero</li>
453 </ul>
454
455 <p>The expression semantics include these rules:</p>
456
457 <ul>
458 <li>symbols are case insensitive</li>
459 <li>only one ternary conditional (e.g. x ? y : z) per statement</li>
460 <li>statements are assignments or the final expression to return</li>
461 <li>an assignment starts a statement, it is not an operator</li>
462 <li>assignments to built-ins do not throw an exception and have no effect;  e.g. <kbd>r=3.0; r</kbd> returns the pixel red color value, not 3.0</li>
463 <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>
464 <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>
465 </ul>
466
467
468 </div>
469
470 <h3>Source Images</h3>
471 <div class="doc-section">
472
473 <p>The symbols <kbd>u</kbd> and <kbd>v</kbd> 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 <kbd>u</kbd>), with a zero index for the beginning of the sequence. A negative index counts from the end.  For example, <kbd>u[0]</kbd> is the first image in the sequence, <kbd>u[2]</kbd> is the third, <kbd>u[-1]</kbd> is the last image, and <kbd>u[t]</kbd> is the current image. The current image can also be referenced by <kbd>s</kbd>. If the sequence number exceeds the length of the sequence, the count is wrapped. Thus in a 3-image sequence,  <kbd>u[-1]</kbd>, <kbd>u[2]</kbd>, and <kbd>u[5]</kbd> all refer to the same (third) image.</p>
474
475 <p>As an example, we form an image by averaging the first image and third images:</p>
476
477 <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>
478 <p>By default, the image to which <kbd>p</kbd>, <kbd>r</kbd>, <kbd>g</kbd>, <kbd>b</kbd>, <kbd>a</kbd>, etc., are applied is the first image <kbd>u</kbd> in the image list, which may also be referenced using <kbd>s</kbd>. </p>
479
480 <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="../www/command-line-options.html#channel">-channel red</a> instructs <a href="../www/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>
481
482 <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>
483 <div class="image">
484   <a href="../images/logo-sm-flop.png"><img src="../images/logo-sm-flop.png" width="128" height="96" alt="logo-sm-flop.png" /></a>
485   <a href="../images/logo-sm.png"><img src="../images/logo-sm.png" width="128" height="96" alt="logo-sm.png" /></a>
486 <img style="margin-top:38px; margin-bottom:38px;" src="../images/right.gif" width="20" height="20" alt="==>" />
487   <a href="../images/logo-sm-fx.png"><img src="../images/logo-sm-fx.png" width="128" height="96" alt="logo-sm-fx.png" /></a>
488 </div>
489
490
491 </div>
492
493 <h3>Accessing Pixels</h3>
494 <div class="doc-section">
495
496 <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>
497
498 <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 <kbd>p</kbd>. For example,</p>
499
500 <pre class="text">
501    p[-1].g      green value of pixel to the immediate left of the current pixel
502    p[-1,-1].r   red value of the pixel diagonally left and up from current pixel
503 </pre>
504
505 <p>To specify an absolute position, use braces, rather than brackets.</p>
506
507 <pre class="text">
508   p{0,0}.r     red value of the pixel in the upper left corner of the image
509   p{12,34}.b   blue pixel value at column number 12, row 34 of the image
510 </pre>
511
512 <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="../www/command-line-options.html#interpolate">-interpolate</a> setting.</p>
513
514 <p>A position outside the boundary of the image retrieves a value dictated by the <a href="../www/command-line-options.html#virtual-pixel">-virtual-pixel</a> option setting.</p>
515
516 </div>
517
518 <h3>Apply an Expression to Select Image Channels</h3>
519 <div class="doc-section">
520
521 <p>Use the <a href="../www/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 <kbd>alpha.png</kbd> with the average of the green channels from the images <kbd>alpha.png</kbd> and <kbd>beta.png</kbd>, use:</p>
522
523 <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>
524 </div>
525
526 <h3>Results</h3>
527 <div class="doc-section">
528
529 <p>The <a href="../www/command-line-options.html#fx">-fx</a> operator evaluates the given expression for each channel (set by <a href="../www/command-line-options.html#channel">-channel</a>) of each pixel in the first image (<kbd>u</kbd>) 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 <kbd>alpha.png</kbd> replaces both of the original images, <kbd>alpha.png</kbd> and <kbd>beta.png</kbd>, before being saved as <kbd>gamma.png</kbd>.</p>
530
531 <p>The current image <kbd>s</kbd> is set to the first image in the sequence (<kbd>u</kbd>), and <kbd>t</kbd> to its index, 0.  The symbols <kbd>i</kbd> and <kbd>j</kbd> reference the current pixel being processed.</p>
532
533
534 <p>For use with <a href="../www/command-line-options.html#format_identify_">-format</a>, the value-escape <kbd>%[fx: ]</kbd> is evaluated just once for each image in the current image sequence. As each image in the sequence is being evaluated, <kbd>s</kbd> and <kbd>t</kbd> successively refer to the current image and its index, while <kbd>i</kbd> and <kbd>j</kbd> are set to zero, and the current channel set to red (<a href="../www/command-line-options.html#channel">-channel</a> is ignored). An example:</p>
535
536 <p class='crt'><span class="crtprompt"> $magick&gt; </span><span class='crtin'>convert  canvas:'rgb(25%,50%,75%)' rose: -colorspace rgb  \ <br/>
537   -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/>
538   Red channel of NW corner of image #1 is 0.184588</span></p>
539 <p>Here we use the image indexes to <em>rotate</em> each image differently, and use <kbd>-set</kbd> with the image index to set a different <em>pause delay</em> on the first image in the animation:</p>
540
541 <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 />
542     -set delay '%[fx:t == 0 ? 240 : 10]' -loop 0 rose.gif</span></p><p>The color-escape <kbd>%[pixel: ]</kbd> is evaluated once per image and per color channel in that image (<a href="../www/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 <kbd>i</kbd> and <kbd>j</kbd> are set to zero, and <kbd>s</kbd> and <kbd>t</kbd> refer to each successively current image and index.</p>
543
544 </div>
545 </div>
546 \r
547 </div>\r
548 \r
549 <div id="linkbar">\r
550     <span id="linkbar-west">&nbsp;</span>\r
551     <span id="linkbar-center">\r
552       <a href="http://www.imagemagick.org/discourse-server/">Discourse Server</a> &bull;\r
553       <a href="http://www.imagemagick.org/MagickStudio/scripts/MagickStudio.cgi">Studio</a>\r
554     </span>\r
555     <span id="linkbar-east">&nbsp;</span>\r
556   </div>\r
557   <div class="footer">\r
558     <span id="footer-west">&copy; 1999-2011 ImageMagick Studio LLC</span>\r
559     <span id="footer-east"> <a href="http://www.imagemagick.org/script/contact.php">Contact the Wizards</a></span>\r
560   </div>\r
561   <div style="clear: both; margin: 0; width: 100%; "></div>\r
562   <script type="text/javascript">\r
563     var _gaq = _gaq || [];\r
564     _gaq.push(['_setAccount', 'UA-17690367-1']);\r
565     _gaq.push(['_trackPageview']);\r
566 \r
567     (function() {\r
568       var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\r
569       ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\r
570       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\r
571     })();\r
572   </script>\r
573 </body>\r
574 </html>\r