ImageExprParse.h
Classes
- ImageExprParse -- Class to hold values from image expression parser (full description)
Interface
- Public Members
- static LatticeExprNode command (const String& str)
- static LatticeExprNode command (const String& str, const Block<LatticeExprNode>& tempLattices, const PtrBlock<const ImageRegion*>& tempRegions)
- ImageExprParse (Bool value)
- ImageExprParse (Int value)
- ImageExprParse (Float value)
- ImageExprParse (Double value)
- ImageExprParse (const Complex& value)
- ImageExprParse (const DComplex& value)
- ImageExprParse (const Char* value)
- ImageExprParse (const String& value)
- LatticeExprNode makeFuncNode () const
- LatticeExprNode makeFuncNode (const LatticeExprNode& arg1) const
- LatticeExprNode makeFuncNode (const LatticeExprNode& arg1, const LatticeExprNode& arg2) const
- LatticeExprNode makeFuncNode (const LatticeExprNode& arg1, const LatticeExprNode& arg2, const LatticeExprNode& arg3) const
- LatticeExprNode makeLRNode() const
- LatticeExprNode makeLitLRNode() const
- LatticeExprNode makeRegionNode() const
- LatticeExprNode makeLiteralNode() const
- static Slice* makeSlice (const ImageExprParse& start)
- static Slice* makeSlice (const ImageExprParse& start, const ImageExprParse& end)
- static Slice* makeSlice (const ImageExprParse& start, const ImageExprParse& end, const ImageExprParse& incr)
- static LatticeExprNode makeIndexinNode (const LatticeExprNode& axis, const vector<Slice>& slices)
- static LatticeExprNode makeValueList (const Block<LatticeExprNode>& values)
- static IPosition makeBinning (const LatticeExprNode& values)
- static void setNode (const LatticeExprNode& node)
- static void addNode (LatticeExprNode* node)
- static void addNode (ImageExprParse* node)
- static void deleteNodes()
- Bool tryLatticeNode (LatticeExprNode& node, const String& name) const
- LatticeExprNode makeImageNode (const String& name, const String& mask) const
- static Table& getRegionTable (void*, Bool)
Prerequisite
Etymology
ImageExprParse is the class used to parse an image expression command.
Synopsis
ImageExprParse is used by the parser of image expression statements.
The parser is written in Bison and Flex in files ImageExprGram.y and .l.
The statements in there use the routines in this file to act
upon a reduced rule.
The main function (and the only function to be used by a user) is the
static function ImageExprParse::command which parses an expression command.
It returns a LatticeExprNode
object containing the expression represented as a tree.
The object can be used as a Lattice(Expr)<T> in other operations.
The syntax of the command is similar to that of expressions in C++.
E.g.
min(img1, img2) + sin(img3)
The following items can be used in an expression:
- Binary operators +, -, *, /, % (modulo), and ^ (power).
- Unary operators + and -.
- Comparison operators ==, >, >=, <, <=, and !=.
- Logical operators &&, ||, and !.
- Constant single and double precision values.
No exponent or exponent "e" results in single precision (Float),
while "d" results in double precision (Double).
- The imaginary part of a complex value can be given by the suffix "i".
A full complex number can be given by addition. E.g. "3+4i".
The complex is single (Complex) or double (DComplex) precision
depending on the constituting parts.
- The special constants pi and e can be given as a double precision
value by means of the functions pi() and e().
- Boolean constants T and F can be given.
- A lot of functions are available.
They are the same as the ones supported by class
LatticeExprNode.
- Explicit conversion functions float, double, complex and dcomplex
are available. Conversions are automatically done where needed,
but for performance reasons it may sometimes be better to do
explicit conversions. See also below in the first example.
- An image can to be given using its file name. The file name
can contain environment variables and user home directories
using the standard UNIX syntax $ENVVAR and ~username.
There are 3 ways to specify a file name:
- When the name contains no other special characters than
$, ~, and . it can be given as such.
- Backslashes can be used to escape individual special characters.
- The full name can be enclosed in quotes (single or double)
to escape the entire name. Adjacent quoted parts
are combined to one name, which can be used to use quotes
in the file name.
Note that escaping has to be used too for the file name
T or F (otherwise it is the boolean constant).
E.g.
image.data
"~noordam/data/image.data"
"~/image.data"
"$HOME/image.data"
$HOME\/image.data
"ab'c"'d"e' results in ab'cd"e
Only input images with data type Float and Complex are supported,
because those data types are the only ones used so far.
Support of Bool, Double, and DComplex is very simple to build in.
The resulting lattice can be of type Bool, Float, Double,
Complex, and DComplex.
- An image can also be given by means of the $n notation,
where n is the sequence number in the
tempLattices argument given to the command
function. Note that the sequence numbers start counting at 1
(to be compliant with glish indexing).
It can, for instance, be used to use non-persistent lattices
in an expression.
When the expression is parsed, it is checked if the images and lattices
involved have conforming shapes and coordinates. Note, however, that
some functions (e.g. mean) reduce an image to a scalar. Such an image
can have a different shape and coordinates.
The data types of the images and constants involved can be different.
The data type of a subexpression is the common data type (e.g.
Float and Double result in Double; Complex and Double result in DComplex).
Automatic implicit conversions are done where needed. However, for
performance reasons it may sometimes be better to convert explicitly.
See below in the first example.
The expression evaluator (which is not part of the parser) evaluates
the expression in chunks to avoid having to keep large temporary
results. A scalar subexpression is evaluated only once to avoid
unnecessary (possibly expensive) calculations.
Some examples:
- img1 + min(float(pi()), mean(img2))
- Suppose img1 and img2 are images with single precision data.
They do not need to have conforming shapes and coordinates,
because only the mean of img2 is used.
Note that pi is explicitly converted to single precision,
because pi() results in a Double. If that was not done,
the expression result would be a Double with the effect that
all data of img1 had to be converted to Double.
- min(img1, (min(img1)+max(img1))/2)
- This example shows that there are 2 min functions. One with a
single argument returning the minimum value of that image.
The other with 2 arguments returning a lattice containing
img1 data clipped at the value of the 2nd argument.
Example
LatticeExpr<Double> expr ("a + sin(b)");
ArrayLattice<Double> arr(expr.shape());
arr.copyData (expr);
Line 1 creates a LatticeExpr object for the given expression. Note that
a and b are names of lattice files (e.g. PagedImage).
Line 2 creates an ArrayLattice with the same shape as the expression
(which is the shape of lattice a (and b)).
Line 3 copies the result of the expression to the ArrayLattice.
Motivation
It is necessary to be able to give an image expression command in ASCII.
This can be used in glish to operate on lattices/images.
Member Description
Parse the given command.
It will open all lattices needed.
It returns the resulting image expression.
The tempLattices/tempRegions arguments make it possible
to use temporary lattices/images and regions in the expression by means
of the $n notation.
Construct a literal object for the given type.
Make a LatticeExprNode for a function.
Make a LatticeExprNode object for the lattice or region name.
Make a LatticeExprNode object for the name of constant, lattice,
or region.
Make a LatticeExprNode object for the temporary region number.
Make a LatticeExprNode object for the literal value.
static Slice* makeSlice (const ImageExprParse& start)
static Slice* makeSlice (const ImageExprParse& start, const ImageExprParse& end)
static Slice* makeSlice (const ImageExprParse& start, const ImageExprParse& end, const ImageExprParse& incr)
Make a Slice object from 1-3 literals.
Make a node for the INDEXIN function.
Make an array from a value list.
Make an IPosition containing the binning values.
Set the static node object (used by the .y file).
Keep track of the nodes allocated while parsing the expression.
private:
Try if the name represent a lattice or image.
Return False if not.
Make the node from the image name and a mask name.
The mask name can be NOMASK (case insensitive) meaning that no mask
is applied to the image.
Callback function for RegionHandlerTable to get the table to be used.