public void runScriptWithBuilder(File scriptFile) throws Exception { // docs-begin-builder-example JiffleBuilder builder = new JiffleBuilder(); // These chained methods read the script from a file, // create a new image for the output, and run the script builder.script(scriptFile).dest("destImg", 500, 500).run(); RenderedImage result = builder.getImage("destImg"); // docs-end-builder-example } }
/** * Associates a variable name with a source image. The default coordinate * system will be used for this image. * The image will be stored by the builder as a weak reference. * * @param varName variable name * @param sourceImage the source image * @return the instance of this class to allow method chaining */ public JiffleBuilder source(String varName, RenderedImage sourceImage) { return source(varName, sourceImage, null); }
public void getSourceFromJiffleBuilder(String script) throws JiffleException { JiffleBuilder builder = new JiffleBuilder(); builder.script(script); // Set source and destination parameters, then... String runtimeSource = builder.getRuntimeSource(); } // docs end getSourceFromJiffleBuilder
/** * Compiles and runs the "ripple" script using {@link JiffleBuilder}. * @param args ignored * @throws Exception if there are errors compiling the script. */ public static void main(String[] args) throws Exception { JiffleBuilderDemo me = new JiffleBuilderDemo(); File f = JiffleDemoHelper.getScriptFile(args, ImageChoice.RIPPLES); String script = JiffleDemoHelper.readScriptFile(f); JiffleBuilder jb = new JiffleBuilder(); jb.script(script).dest("result", WIDTH, HEIGHT).getRuntime().evaluateAll(null); ImageFrame frame = new ImageFrame(jb.getImage("result"), "Jiffle image demo"); frame.setVisible(true); } }
public static void main(String[] args) throws IOException, JiffleException { String basePath = "/opt/gisData/evo-odas/coverave-view-hetero/multires-s2/S2A_MSIL1C_20170410T103021_N0204_R108_T32UNU_20170410T103020.SAFE/20170410T103021026Z_fullres_CC2.4251_T32UNU_"; // prepare inputs and outputs RenderedImage red = readImage(new File(basePath + "B04.tif")); RenderedImage nir = readImage(new File(basePath + "B08.tif")); TiledImage result = ImageUtilities.createConstantImage(red.getWidth(), red.getHeight(), (float) 0); // build the operation JiffleBuilder builder = new JiffleBuilder(); builder.dest("res", result).source("red", red).source("nir", nir); builder.script("n = nir; r = red; res = (n - r) / (n + r);"); // HERE IS THE NDVI SCRIPT! 5.5 seconds! // builder.script("res = (nir - red) / (nir + red);"); // HERE IS THE NDVI SCRIPT! 7.65 sec! JiffleDirectRuntime runtime = builder.getRuntime(); // actually running the calculation final double pixels = (double) red.getWidth() * (double) red.getHeight(); System.out.println( "Computing " + NumberFormat.getNumberInstance().format(pixels) + " pixels"); long start = System.currentTimeMillis(); runtime.evaluateAll(null); long end = System.currentTimeMillis(); System.out.println("Computation of output took " + (end - start) / 1000.0 + " secs"); System.out.println("Writing output to disk"); ImageIO.write(result, "TIF", new File("/tmp/ndvi.tif")); System.out.println("Writing complete"); }
public void runScriptWithBuilder(String script, String destVar, WritableRenderedImage destImage) throws JiffleException { // Image bounds are taken from the destination image Rectangle imageBounds = new Rectangle( destImage.getMinX(), destImage.getMinY(), destImage.getWidth(), destImage.getHeight()); // The world bounds are the unit rectangle Rectangle2D worldBounds = new Rectangle2D.Double(0, 0, 1, 1); CoordinateTransform transform = CoordinateTransforms.unitBounds(imageBounds); JiffleBuilder builder = new JiffleBuilder(); // Set the processing area (world units) builder.worldAndNumPixels(worldBounds, destImage.getWidth(), destImage.getHeight()); // Set the script and the destination image with its transform builder.script(script).dest(destVar, destImage, transform); // This executes the script and writes the results into destImage builder.run(); } // docs end builder method
/** * Sets a destination image associated with a variable name in the script. * <p> * See {@link #dest(String, WritableRenderedImage, CoordinateTransform)} * for more details about this method. * * @param varName variable name * @param destImage the destination image * * @return the instance of this class to allow method chaining */ public JiffleBuilder dest(String varName, WritableRenderedImage destImage) { return dest(varName, destImage, null); }
@Test public void scriptFile() throws Exception { System.out.println(" loading script file"); URL url = JiffleBuilderTest.class.getResource("constant.jfl"); File scriptFile = new File(url.toURI()); jb.script(scriptFile).dest("dest", 10, 10).run(); // no checking of dest image - just as long as we didn't // get an exception we are happy }
@Test public void destImageWithRect() throws Exception { System.out.println(" dest image from Rectangle bounds"); int w = 10; int h = 20; jb.dest("dest", new Rectangle(0, 0, w, h)); RenderedImage img = jb.getImage("dest"); assertNotNull(img); assertEquals(w, img.getWidth()); assertEquals(h, img.getHeight()); }
@Before public void setup() { jb = new JiffleBuilder(); }
/** * Runs the script. Equivalent to calling * {@code builder.getRuntime().evaluateAll(null)}. * * @return the instance of this class to allow method chaining * * @throws JiffleException if the script has not been set yet or if * compilation errors occur */ public JiffleBuilder run() throws JiffleException { getRuntime().evaluateAll(null); return this; }
@Test public void builderCreatesDestImage() throws Exception { System.out.println(" builder creating dest image"); String script = "init { n = 0; } dest = n++ ;" ; jb.dest("dest", IMG_WIDTH, IMG_WIDTH).script(script).getRuntime().evaluateAll(null); RenderedImage img = jb.getImage("dest"); assertNotNull(img); RandomIter iter = RandomIterFactory.create(img, null); int k = 0; for (int y = 0; y < IMG_WIDTH; y++) { for (int x = 0; x < IMG_WIDTH; x++) { assertEquals(k, iter.getSample(x, y, 0)); k++ ; } } }
@Test public void runBasicScript() throws Exception { System.out.println(" basic script with provided dest image"); String script = "dest = con(src1 > 10, src1, null);" ; RenderedImage srcImg1 = createSequenceImage(); WritableRenderedImage destImg = ImageUtilities.createConstantImage(IMG_WIDTH, IMG_WIDTH, 0d); jb.script(script).source("src1", srcImg1).dest("dest", destImg); JiffleDirectRuntime runtime = jb.getRuntime(); runtime.evaluateAll(null); Evaluator e = new Evaluator() { public double eval(double val) { return val > 10 ? val : Double.NaN; } }; assertImage(srcImg1, destImg, e); }
/** * Sets a destination image associated with a variable name in the script. * <p> * See {@link #dest(String, WritableRenderedImage, CoordinateTransform)} * for more details about this method. * * @param varName variable name * @param destImage the destination image * * @return the instance of this class to allow method chaining */ public JiffleBuilder dest(String varName, WritableRenderedImage destImage) { return dest(varName, destImage, null); }
@Before public void setup() { builder = new JiffleBuilder(); }
/** * Runs the script. Equivalent to calling * {@code builder.getRuntime().evaluateAll(null)}. * * @return the instance of this class to allow method chaining * * @throws JiffleException if the script has not been set yet or if * compilation errors occur */ public JiffleBuilder run() throws JiffleException { getRuntime().evaluateAll(null); return this; }
public RenderedImage buildAndRunScript(File scriptFile, RenderedImage inputImage) throws JiffleException { JiffleBuilder builder = new JiffleBuilder(); builder.script(scriptFile).source("src", inputImage); builder.dest("dest", inputImage.getWidth(), inputImage.getHeight()); builder.run(); return builder.getImage("dest"); } // docs end method
/** * Creates a new destination image and associates it with a variable name * in the script. * <p> * Note: a {@code JiffleBuilder} maintains only {@code WeakReferences} * to all source images and any destination _images passed to it via * the {@link #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 {@link #clear()} or * {@link #removeImage(String varName)}. * * @param varName variable name * @param minx minimum pixel X ordinate * @param miny minimum pixel Y ordinate * @param width image width (pixels) * @param height image height (pixels) * * @return the instance of this class to allow method chaining */ public JiffleBuilder dest(String varName, int minx, int miny, int width, int height) { return dest(varName, minx, miny, width, height, null); }
/** * Associates a variable name with a source image. The default coordinate * system will be used for this image. * The image will be stored by the builder as a weak reference. * * @param varName variable name * @param sourceImage the source image * @return the instance of this class to allow method chaining */ public JiffleBuilder source(String varName, RenderedImage sourceImage) { return source(varName, sourceImage, null); }