javax.media.jai
Class WarpGrid

java.lang.Object
  extended byjavax.media.jai.Warp
      extended byjavax.media.jai.WarpGrid
All Implemented Interfaces:
Serializable

public final class WarpGrid
extends Warp

A regular grid-based description of an image warp.

The mapping from destination pixels to source positions is described by bilinear interpolation within a rectilinear grid of points with known mappings.

Given a destination pixel coordinate (x, y) that lies within a cell having corners at (x0, y0), (x1, y0), (x0, y1) and (x1, y1), with source coordinates defined at each respective corner equal to (sx0, sy0), (sx1, sy1), (sx2, sy2) and (sx3, sy3), the source position (sx, sy) that maps onto (x, y) is given by the formulas:

 xfrac = (x - x0)/(x1 - x0)
 yfrac = (y - y0)/(y1 - y0)

 s = sx0 + (sx1 - sx0)*xfrac
 t = sy0 + (sy1 - sy0)*xfrac

 u = sx2 + (sx3 - sx2)*xfrac
 v = sy2 + (sy3 - sy2)*xfrac

 sx = s + (u - s)*yfrac
 sy = t + (v - t)*yfrac
 

In other words, the source x and y values are interpolated horizontally along the top and bottom edges of the grid cell, and the results are interpolated vertically:

 (x0, y0) ->            (x1, y0) ->
   (sx0, sy0)             (sx1, sy1)
    +------------+---------+
    |            |\        |
    |            | (s, t)  |
    |            |         |
    |            |         |
    |            |         |
    |            |         |
    | (x, y) ->  |         |
    |  (sx, sy)--+         |
    |            |         |
    |            |         |
    |            | (u, v)  |
    |            |/        |
    +------------+---------+
 (x0, y1) ->          (x1, y1) ->
   (sx2, sy2)           (sx3, sy3)
 

Points outside the bounds of the cells defining the grid warp will be mapped to the source image using the identity transformation.

WarpGrid is marked final so that it may be more easily inlined.

See Also:
Serialized Form

Constructor Summary
WarpGrid(int xStart, int xStep, int xNumCells, int yStart, int yStep, int yNumCells, float[] warpPositions)
          Constructs a WarpGrid with a given grid-based transform mapping destination pixels into source space.
WarpGrid(Warp master, int xStart, int xStep, int xNumCells, int yStart, int yStep, int yNumCells)
          Constructs a WarpGrid object by sampling the displacements given by another Warp object of any kind.
 
Method Summary
 int getXNumCells()
          Returns the number of grid cell columns.
 int getXStart()
          Returns the minimum X coordinate of the grid.
 int getXStep()
          Returns the horizontal spacing between grid cells.
 float[] getXWarpPos()
          Returns the horizontal warp positions at the grid points.
 int getYNumCells()
          Returns the number of grid cell rows.
 int getYStart()
          Returns the minimum Y coordinate of the grid.
 int getYStep()
          Returns the vertical spacing between grid cells.
 float[] getYWarpPos()
          Returns the vertical warp positions at the grid points.
 Point2D mapDestPoint(Point2D destPt)
          Computes the source point corresponding to the supplied point.
 float[] warpSparseRect(int x, int y, int width, int height, int periodX, int periodY, float[] destRect)
          Computes the source subpixel positions for a given rectangular destination region, subsampled with an integral period.
 
Methods inherited from class javax.media.jai.Warp
mapDestRect, mapSourcePoint, mapSourceRect, warpPoint, warpPoint, warpRect, warpRect, warpSparseRect
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WarpGrid

public WarpGrid(int xStart,
                int xStep,
                int xNumCells,
                int yStart,
                int yStep,
                int yNumCells,
                float[] warpPositions)
Constructs a WarpGrid with a given grid-based transform mapping destination pixels into source space. Note that this is a backward mapping as opposed to the forward mapping used in AffineOpImage.

The grid is defined by a set of equal-sized cells. The grid starts at (xStart, yStart). Each cell has width equal to xStep and height equal to yStep, and there are xNumCells cells horizontally and yNumCells cells vertically.

The local mapping within each cell is defined by the values in the table parameter. This parameter must contain 2*(xNumCells + 1)*(yNumCells + 1) values, which alternately contain the source X and Y coordinates to which each destination grid intersection point maps. The cells are enumerated in row-major order, that is, all the grid points along a row are enumerated first, then the grid points for the next row are enumerated, and so on.

As an example, suppose xNumCells is equal to 2 and yNumCells is equal 1. Then the order of the data in table would be:

 x00, y00, x10, y10, x20, y20, x01, y01, x11, y11, x21, y21
 
for a total of 2*(2 + 1)*(1 + 1) = 12 elements.

Parameters:
xStart - the minimum X coordinate of the grid.
xStep - the horizontal spacing between grid cells.
xNumCells - the number of grid cell columns.
yStart - the minimum Y coordinate of the grid.
yStep - the vertical spacing between grid cells.
yNumCells - the number of grid cell rows.
warpPositions - a float array of length 2*(xNumCells + 1)* (yNumCells + 1) containing the warp positions at the grid points, in row-major order.
Throws:
IllegalArgumentException - if the length of warpPositions is incorrect

WarpGrid

public WarpGrid(Warp master,
                int xStart,
                int xStep,
                int xNumCells,
                int yStart,
                int yStep,
                int yNumCells)
Constructs a WarpGrid object by sampling the displacements given by another Warp object of any kind.

The grid is defined by a set of equal-sized cells. The grid starts at (xStart, yStart). Each cell has width equal to xStep and height equal to yStep, and there are xNumCells cells horizontally and yNumCells cells vertically.

Parameters:
master - the Warp object used to initialize the grid displacements.
xStart - the minimum X coordinate of the grid.
xStep - the horizontal spacing between grid cells.
xNumCells - the number of grid cell columns.
yStart - the minimum Y coordinate of the grid.
yStep - the vertical spacing between grid cells.
yNumCells - the number of grid cell rows.
Method Detail

getXStart

public int getXStart()
Returns the minimum X coordinate of the grid.


getYStart

public int getYStart()
Returns the minimum Y coordinate of the grid.


getXStep

public int getXStep()
Returns the horizontal spacing between grid cells.


getYStep

public int getYStep()
Returns the vertical spacing between grid cells.


getXNumCells

public int getXNumCells()
Returns the number of grid cell columns.


getYNumCells

public int getYNumCells()
Returns the number of grid cell rows.


getXWarpPos

public float[] getXWarpPos()
Returns the horizontal warp positions at the grid points.


getYWarpPos

public float[] getYWarpPos()
Returns the vertical warp positions at the grid points.


warpSparseRect

public float[] warpSparseRect(int x,
                              int y,
                              int width,
                              int height,
                              int periodX,
                              int periodY,
                              float[] destRect)
Computes the source subpixel positions for a given rectangular destination region, subsampled with an integral period.

Points outside the bounds of the cells defining the grid warp will be mapped to the source image using the identity transformation.

Specified by:
warpSparseRect in class Warp
Parameters:
x - The minimum X coordinate of the destination region.
y - The minimum Y coordinate of the destination region.
width - The width of the destination region.
height - The height of the destination region.
periodX - The horizontal sampling period.
periodY - The vertical sampling period.
destRect - An int array containing at least 2*((width+periodX-1)/periodX)*((height+periodY-1)/periodY) elements, or null. If null, a new array will be constructed.
Returns:
a reference to the destRect parameter if it is non-null, or a new int array of length 2*width*height otherwise.
Throws:
ArrayBoundsException - if destRect is too small

mapDestPoint

public Point2D mapDestPoint(Point2D destPt)
Computes the source point corresponding to the supplied point.

This method returns the value of pt in the following code snippet:

 float[] sxy = warpSparseRect((int)destPt.getX(), (int)destPt.getY(),
                              2, 2, 1, 1, null);

 double wtRight  = destPt.getX() - (int)destPt.getX();
 double wtLeft   = 1.0 - wtRight;
 double wtBottom = destPt.getY() - (int)destPt.getY();
 double wtTop    = 1.0 - wtBottom;

 Point2D pt = (Point2D)destPt.clone();
 pt.setLocation((sxy[0]*wtLeft + sxy[2]*wtRight)*wtTop +
                (sxy[4]*wtLeft + sxy[6]*wtRight)*wtBottom,
                (sxy[1]*wtLeft + sxy[3]*wtRight)*wtTop +
                (sxy[5]*wtLeft + sxy[7]*wtRight)*wtBottom);
 

Overrides:
mapDestPoint in class Warp
Parameters:
destPt - the position in destination image coordinates to map to source image coordinates.
Returns:
a Point2D of the same class as destPt.
Throws:
IllegalArgumentException - if destPt is null.
Since:
JAI 1.1.2