G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing

A Full-Featured Open-Source Framework for Image Processing

Latest stable version: 3.3.5        Current pre-release: 3.3.6 (2024/04/19)


if, elif, else and fi

frontimageAn -if-elif-else  … -fi series conditionally execute  command sequences (aka, conditional blocks). \n\n1. -if introduces a conditional block and takes as a parameter a [mathematical expression](https://gmic.eu/reference/mathematical_expressions.html#top). Expressions returning zero are taken to be __False__; the conditional block does not execute. Otherwise, when __True__, the conditional block executes. The mathematical expression need not be quoted or embedded in curly braces within command scripts. \n\nCommands following -if, up to and excluding -elif-else or -fi constitute the conditional block. Conditional blocks may be empty; that is, an -elif-else or -fi immediately follows. -if must have a matching -fi`, concluding a series of conditional blocks.

2. -elif optionally follows -if or prior -elif commands and introduces alternative conditional blocks. These execute only if -elif's associated mathematical expression is True and none of the preceding conditional blocks have executed. -elif cannot begin conditional command sequences, nor follow -else. In light of this, -elif commands always form contiguous chains with other -elif commands.

3. -else optionally introduces a default command block that executes only if none of the preceding conditional blocks have executed. It does not take a parameter and its commands, if any, follow up to a concluding -fi command.

4. -fi terminates sequences of conditional blocks. -if and -fi must be paired. Commands following -fi unconditionally execute.

At most, only one conditional block executes. The command processor evaluates in order of appearance the mathematical expressions associated with each -if and -elif command and executes the affiliated conditional block for the first mathematical expression that is True. The command processor then skips to the first command following -fi; bypassing the remaining blocks. If no expression returns True, the command processor executes the block associated with the default -else command, if it exists. Otherwise, no blocks in the conditional sequence execute.
Testing For File Objects
Prior to G'MIC version 2.6, -if and -elif could take file system objects as well as mathematical expressions and test for their existence; this is no longer the case. Instead, use math expressions with file system query functions that perform similar roles: isfile( <file system object reference> ) and isdir( <file system object reference> ) return True if the reference exists and is of the correct file object type. Some 1.6x tutorials may reflect the older, now unsupported, notation.
1.    -if 0
    -echo "I never execute!"
2.    -if 1
    -echo "I always execute!"
    -echo "I never execute!"
3.    -if u>0.5
    -echo "More than half!"
    -echo "Half or less than!"
4.    chk={u}
-if   $chk>=0.75
    -echo "Upper quarter. Check is "$chk"."
-elif $chk>=0.50
    -echo "Upper middle quarter. Check is "$chk"."
-elif $chk>=0.25
    -echo "Lower middle quarter. Check is "$chk"."
    -echo "Bottom quarter. Check is "$chk"."

The last example is a sieve. Note that prior tests implicitly set the upper boundary of those that follow, which only need to test for lower boundaries. This is a consequence of at most one block executing. The block following -else can only execute for "bottom quarter" values of u less than 0.25.
Command Reference
$ gmic -h if

if (+):

Start a 'if...[elif]...[else]...fi' block and test if specified condition holds.
'condition' is a mathematical expression, whose evaluation is interpreted as { 0:false | other:true }.

[#1] image.jpg if ia<64 add 50% elif ia<128 add 25% elif ia<192 sub 25% else sub 50% fi cut 0,255

Tutorial: https://gmic.eu/tutorial/iffi
Updated: 11-Jan-2023 19:23 UTC Commit: 8424ea51f320 by David Tschumperle
G'MIC - GREYC's Magic for Image Computing: A Full-Featured Open-Source Framework for Image Processing

G'MIC is an open-source software distributed under the CeCILL free software licenses (LGPL-like and/or
GPL-compatible). Copyrights (C) Since July 2008, David Tschumperlé - GREYC UMR CNRS 6072, Image Team.