1.4 The VMask class

The VMask class is an abstraction over the VIPS DOUBLEMASK and INTMASK types which gives convenient and safe representation of matrices.

VMask has two sub-classes, VIMask and VDMask. These represent matrices of integers and doubles respectively.

1.4.1 Constructors

There are four constructors for VIMask and VDMask:

VIMask( int xsize, int ysize );  
VIMask( int xsize, int ysize,  
  int scale, int offset, ... );  
VIMask( int xsize, int ysize,  
  int scale, int offset,  
  std::vector<int> coeff );  
VIMask( const char ⋆name );  
VIMask();  
VDMask( int xsize, int ysize );  
VDMask( int xsize, int ysize,  
  double scale, double offset, ... );  
VDMask( int xsize, int ysize,  
  double scale, double offset,  
  std::vector<double> coeff );  
VDMask( const char ⋆name );  
VDMask();

The first form creates an empty matrix, with the specified dimensions; the second form initialises a matrix from a varargs list; the third form sets the matrix from a vector of coefficients; the fourth from the named file. The final form makes a mask object with no contents yet.

The varargs constructors are not wrapped in Python — use the vector constructor instead. For example:

m = VMask.VIMask (3, 3, 1, 0,  
  [-1, -1, -1,  
   -1,  8, -1,  
   -1, -1, -1])

1.4.2 Projection functions

A set of member functions of VIMask provide access to the fields in the matrix:

int xsize() const;  
int ysize() const;  
int scale() const;  
int offset() const;  
const char ⋆filename() const;

VDMask is the same, except that the scale() and offset() members return double. VMask allows all operations that are common to VIMask and VDMask.

1.4.3 Assignment

VMask defines copy and assignment with pointer-style semantics. You can write stuff like:

VIMask fred( "mask" );  
VMask jim;  
 
jim = fred;

This reads the file mask, noting a pointer to the mask in fred. It then makes jim also point to it, so jim and fred are sharing the same underlying matrix values.

Internally, a VMask object is just a pointer to a reference-counting block, which in turn holds a pointer to the underlying VIPS MASK type. You can therefore efficiently pass VMask objects to functions by value, and return VMask objects as function results.

1.4.4 Computing with VMask

You can use [] to get at matrix elements, numbered left-to-right, top-to-bottom. Alternatively, use () to address elements by x,y position. For example:

VIMask fred( "mask" );  
 
for( int i = 0; i < fred.xsize(); i++ )  
    fred[i] = 12;

will set the first line of the matrix to 12, and:

VDMask fred( "mask" );  
 
for( int x = 0; x < fred.xsize(); x++ )  
    fred(x, x) = 12.0;

will set the leading diagonal to 12.

These don’t work well in Python, so there’s an extra member, get(), which will get an element by x,y position.

x = mat.get (2, 4)

See the member functions below for other operations on VMask.

1.4.5 VIMask operations

The following operations are defined for VIMask:

// Cast to VDMask and VImage  
operator VDMask();  
operator VImage();  
 
// Build gaussian and log masks  
static VIMask gauss( double, double );  
static VIMask gauss_sep( double, double );  
static VIMask log( double, double );  
 
// Rotate  
VIMask rotate45();  
VIMask rotate90();  
 
// Transpose, invert, join and multiply  
VDMask trn() ;  
VDMask inv();  
VDMask cat( VDMask );  
VDMask mul( VDMask );

1.4.6 VDMask operations

The following operations are defined for VDMask:

// Cast to VIMask and VImage  
operator VIMask();  
operator VImage();  
 
// Build gauss and log masks  
static VDMask gauss( double, double );  
static VDMask log( double, double );  
 
// Rotate  
VDMask rotate45();  
VDMask rotate90();  
 
// Scale to intmask  
VIMask scalei();  
 
// Transpose, invert, join and multiply  
VDMask trn();  
VDMask inv();  
VDMask cat( VDMask );  
VDMask mul( VDMask );

1.4.7 Output of masks

You can output masks with the usual << operator.