next up previous contents index
Next: Input and output Up: Mathematical pixel operations (Image Previous: Using image names as   Contents   Index


Evaluating expressions

The expression evaluator can be accessed directly with the evaluate command. This has several uses:
  1. Checking the current value of variables, for example:

    evaluate(abc);or

    evaluate(aaa==12);

  2. Performing calculations and checking syntax: evaluate(sin(erf(2.34)));
  3. Setting variables: evaluate(a=4); (set() is the preferred way of setting variables.)
The evaluate command prints the answer in a small information box.

The command whatis is synonymous with evaluate. Another use of whatis is in conjunction with print. Normally, print would iterate over all the pixels, creating a long printout. However, if you were to enter

          
        whatis(print("The pixel is ", image[0][2][200][200]));

the value of a single pixel would be printed.

Evaluate and whatis should not be used in loops.

Comments and multiline equations

Any line that starts with the # character is a comment and is ignored. Similarly, blank lines and carriage returns in equations are ignored. Thus you could have:

          
       i = image[0][0][x][y] +
           image[2][0][x+4][y+4];
However, comments cannot appear in the middle of an expression.

Relationship between image math and macros

Although pixel math equations and macros use a `C'-like syntax, the main difference is that, by default, all math commands iterate over all the pixels in the currently-selected image or selected area. This makes it consistent with the fact that macro commands such as `filter' and `fft' also work on entire images, even though they are only executed once. Some other commands, such as fopen and fclose are also only executed once. Thus if you have:

          
       fopen("test");
       write("test","the answers are i=",i," x=",x," y=",y,"\n");
       fclose("test");

the write statement is iterated over all the pixels, while the fopen and fclose are only executed once.

A number of sample macros (*.macro) are included with imal.

Mixing image math and macros

The fact that imal commands and image math can appear in the same macro is a tremendous convenience in writing macros. For example, here is a sample macro:

          
   # 1.macro - Simple macro
   open(gray);
   if(x>50) i+=27;
   if(y>x) i+=66;

This macro opens the image file gray, and applies two equations to it. The variable ``i'' is used instead of r,g, and b because gray happens to be a grayscale image. However, imal has to check each statement to determine whether it is a macro or a math equation. Slightly greater speed can be obtained by enclosing the math equations in a ``loop'':

          
   # 1.macro - Simple macro
   open(gray);
   loop
   {
      if(x>0) i+=27;
      if(y>x) i+=66;  
   }

In this macro, the ``loop'' keyword tells imal that the statements enclosed in braces are mathematical equations and not macro statements, so no checking is done against macro names. This has several advantages:

  1. It is slightly faster.
  2. Sometimes it is confusing as to whether a statement is looped or not. The `loop' notation makes it clearer that these equations will be looped over all the pixels.
  3. Variable names that are the same as macro commands can be used.


next up previous contents index
Next: Input and output Up: Mathematical pixel operations (Image Previous: Using image names as   Contents   Index
root 2008-10-10