public class JiffleBuilder extends Object
When working with Jiffle objects directly you end up writing a certain amount of boiler-plate code for image parameters etc. JiffleBuilder offers concise, chained methods to help you get the job done with fewer keystrokes.
// A script to sum values from two source images
String sumScript = "dest = foo + bar;" ;
RenderedImage fooImg = ...
RenderedImage barImg = ...
JiffleBuilder jb = new JiffleBuilder();
jb.script(sumScript).source("foo", fooImg).script("bar", barImg);
// We can get the builder to create the destination image for us
jb.dest("dest", fooImg.getWidth(), fooImg.getHeight());
// Run the script
jb.getRuntime().run();
// Since we asked the builder to create the destination image we
// now need to get a reference to it
RenderedImage destImg = jb.getImage("dest");
When a script does not use any source images, JiffleBuilder
makes
for very concise code:
String script = "waves = sin( 4 * M_PI * x() / width() );" ;
JiffleBuilder jb = new JiffleBuilder();
RenderedImage wavesImg = jb.script(script).dest("waves", 500, 200).run().getImage("waves");
JiffleBuilder
also provides support for setting world units and
coordinate transforms.Constructor and Description |
---|
JiffleBuilder()
Creates a new JiffleBuilder instance.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears all attributes in this builder.
|
JiffleBuilder |
defaultTransform(CoordinateTransform transform)
Sets a default
CoordinateTransform instance to use with all
images that are passed to the builder without an explicit transform
of their own. |
JiffleBuilder |
dest(String varName,
int width,
int height)
Creates a new destination image and associates it with a variable name
in the script.
|
JiffleBuilder |
dest(String varName,
int width,
int height,
CoordinateTransform transform)
Creates a new destination image and associates it with a variable name
in the script.
|
JiffleBuilder |
dest(String varName,
int minx,
int miny,
int width,
int height)
Creates a new destination image and associates it with a variable name
in the script.
|
JiffleBuilder |
dest(String varName,
int minx,
int miny,
int width,
int height,
CoordinateTransform transform)
Creates a new destination image and associates it with a variable name
in the script.
|
JiffleBuilder |
dest(String varName,
Rectangle destBounds)
Creates a new destination image and associates it with a variable name
in the script.
|
JiffleBuilder |
dest(String varName,
Rectangle destBounds,
CoordinateTransform transform)
Creates a new destination image and associates it with a variable name
in the script.
|
JiffleBuilder |
dest(String varName,
WritableRenderedImage destImage)
Sets a destination image associated with a variable name in the script.
|
JiffleBuilder |
dest(String varName,
WritableRenderedImage destImage,
CoordinateTransform transform)
Sets a destination image associated with a variable name in the script.
|
RenderedImage |
getImage(String varName)
Get an image associated with a script variable name.
|
JiffleDirectRuntime |
getRuntime()
Creates a runtime object for the currently set script and images.
|
String |
getRuntimeSource()
Gets the Java run-time class code generated from the compiled script.
|
RenderedImage |
removeImage(String varName)
Removes an image associated with a script variable name.
|
JiffleBuilder |
run()
Runs the script.
|
JiffleBuilder |
script(File scriptFile)
Reads the script from
scriptFile . |
JiffleBuilder |
script(String script)
Sets the script to be compiled.
|
JiffleBuilder |
source(String varName,
RenderedImage sourceImage)
Associates a variable name with a source image.
|
JiffleBuilder |
source(String varName,
RenderedImage sourceImage,
CoordinateTransform transform)
Associates a variable name with a source image and coordinate transform.
|
JiffleBuilder |
worldAndNumPixels(Rectangle2D worldBounds,
int numX,
int numY)
Sets the bound and resolution of the processing area.
|
JiffleBuilder |
worldAndRes(Rectangle2D worldBounds,
double xres,
double yres)
Sets the bound and resolution of the processing area.
|
public void clear()
dest
methods with image bounds
arguments they will also be freed.public JiffleBuilder worldAndRes(Rectangle2D worldBounds, double xres, double yres)
worldBounds
- bounds in world unitsxres
- pixel width in world unitsyres
- pixel height in world unitspublic JiffleBuilder worldAndNumPixels(Rectangle2D worldBounds, int numX, int numY)
worldBounds
- bounds in world unitsnumX
- number of pixels in the X directionnumY
- number of pixels in the Y directionpublic JiffleBuilder script(String script)
script
- the scriptpublic JiffleBuilder script(File scriptFile) throws JiffleException
scriptFile
.scriptFile
- file containing the scriptJiffleException
- if there were problems reading the filepublic JiffleBuilder source(String varName, RenderedImage sourceImage)
varName
- variable namesourceImage
- the source imagepublic JiffleBuilder source(String varName, RenderedImage sourceImage, CoordinateTransform transform)
varName
- variable namesourceImage
- the source imagetransform
- the transform to convert world coordinates to this image's
pixel coordinatespublic JiffleBuilder dest(String varName, Rectangle destBounds)
Note: a JiffleBuilder
maintains only WeakReferences
to all source images and any destination _images passed to it via
the dest(String, WritableRenderedImage)
method. However,
a strong reference is stored to any destination images created with this
method. This can be freed later by calling clear()
or
removeImage(String varName)
.
varName
- variable namedestBounds
- the bounds of the new destination imagepublic JiffleBuilder dest(String varName, Rectangle destBounds, CoordinateTransform transform)
Note: a JiffleBuilder
maintains only WeakReferences
to all source images and any destination _images passed to it via
the dest(String, WritableRenderedImage)
method. However,
a strong reference is stored to any destination images created with this
method. This can be freed later by calling clear()
or
removeImage(String varName)
.
varName
- variable namedestBounds
- the bounds of the new destination imagetransform
- the transform to convert world coordinates to this image's
pixel coordinatespublic JiffleBuilder dest(String varName, int width, int height)
Note: a JiffleBuilder
maintains only WeakReferences
to all source images and any destination _images passed to it via
the dest(String, WritableRenderedImage)
method. However,
a strong reference is stored to any destination images created with this
method. This can be freed later by calling clear()
or
removeImage(String varName)
.
varName
- variable namewidth
- image width (pixels)height
- image height (pixels)public JiffleBuilder dest(String varName, int width, int height, CoordinateTransform transform)
Note: a JiffleBuilder
maintains only WeakReferences
to all source images and any destination _images passed to it via
the dest(String, WritableRenderedImage)
method. However,
a strong reference is stored to any destination images created with this
method. This can be freed later by calling clear()
or
removeImage(String varName)
.
varName
- variable namewidth
- image width (pixels)height
- image height (pixels)transform
- the transform to convert world coordinates to this image's
pixel coordinatespublic JiffleBuilder dest(String varName, int minx, int miny, int width, int height)
Note: a JiffleBuilder
maintains only WeakReferences
to all source images and any destination _images passed to it via
the dest(String, WritableRenderedImage)
method. However,
a strong reference is stored to any destination images created with this
method. This can be freed later by calling clear()
or
removeImage(String varName)
.
varName
- variable nameminx
- minimum pixel X ordinateminy
- minimum pixel Y ordinatewidth
- image width (pixels)height
- image height (pixels)public JiffleBuilder dest(String varName, int minx, int miny, int width, int height, CoordinateTransform transform)
Note: a JiffleBuilder
maintains only WeakReferences
to all source images and any destination _images passed to it via
the dest(String, WritableRenderedImage)
method. However,
a strong reference is stored to any destination images created with this
method. This can be freed later by calling clear()
or
removeImage(String varName)
.
varName
- variable nameminx
- minimum pixel X ordinateminy
- minimum pixel Y ordinatewidth
- image width (pixels)height
- image height (pixels)transform
- the transform to convert world coordinates to this image's
pixel coordinatespublic JiffleBuilder dest(String varName, WritableRenderedImage destImage)
See dest(String, WritableRenderedImage, CoordinateTransform)
for more details about this method.
varName
- variable namedestImage
- the destination imagepublic JiffleBuilder dest(String varName, WritableRenderedImage destImage, CoordinateTransform transform)
Note: The builder will only hold a Weak reference to destImg
so
it's not a good idea to create an image on the fly when calling this
method...
// Creating image on the fly
builder.dest("foo", ImageUtils.createConstantImage(width, height, 0d), transform);
// Later - oops, null is returned here
RenderedImage img = builder.getImage("foo");
To avoid this problem, create your image locally...
WritableRenderedImage img = ImageUtils.createConstantImage(width, height, 0d);
builder.dest("foo", img, transform);
Or use on of the dest
methods with image bounds arguments to
create it for you
builder.dest("foo", width, height, transform);
// Now, we can retrieve the image successfully
RenderedImage img = builder.getImage("foo");
varName
- variable namedestImage
- the destination imagetransform
- the transform to convert world coordinates to this image's
pixel coordinatespublic JiffleBuilder defaultTransform(CoordinateTransform transform)
CoordinateTransform
instance to use with all
images that are passed to the builder without an explicit transform
of their own. If transform
is null
, the system default
transform will be used for any such images.transform
- a transform to use as the default; or null
for
the system default transformJiffleRuntime.setDefaultTransform(CoordinateTransform)
public JiffleBuilder run() throws JiffleException
builder.getRuntime().evaluateAll(null)
.JiffleException
- if the script has not been set yet or if
compilation errors occurpublic JiffleDirectRuntime getRuntime() throws JiffleException
JiffleDirectRuntime
JiffleException
- if the script has not been set yet or if
compilation errors occurpublic String getRuntimeSource() throws JiffleException
JiffleException
- if the script has not been set yet or if
compilation errors occurpublic RenderedImage getImage(String varName)
dest
methods.
In the case of a destination image the object returned can be cast
to WritableRenderedImage
.
varName
- variable namenull
if the variable name is
not recognized or the image has since been garbage collectedpublic RenderedImage removeImage(String varName)
dest
methods.
In the case of a destination image the object returned can be cast
to WritableRenderedImage
.
Note: Thie method also removes any CoordinateTransform
associated with the image.
varName
- variable namenull
if the variable name is
not recognized or the image has since been garbage collectedCopyright © 2006–2018 GeoSolutions. All rights reserved.