/** * Creates an instance of the default runtime class. * <p> * The default runtime class implements {@link JiffleDirectRuntime} and * extends an abstract base class provided by the Jiffle compiler. Objects * of this class evaluate the Jiffle script and write results directly to * the destination image(s). Client code can call either of the methods: * <ul> * <li>{@code evaluate(int x, int y)} * <li>{@code evaluateAll(JiffleProgressListener listener} * </ul> * The {@code Jiffle} object must be compiled before calling this method. * * @return the runtime object * @throws JiffleException if the script has not been compiled or if errors * occur in creating the runtime instance */ public JiffleDirectRuntime getRuntimeInstance() throws it.geosolutions.jaiext.jiffle.JiffleException { return (JiffleDirectRuntime) getRuntimeInstance(RuntimeModel.DIRECT); }
/** * Creates an instance of the default runtime class. * <p> * The default runtime class implements {@link JiffleDirectRuntime} and * extends an abstract base class provided by the Jiffle compiler. Objects * of this class evaluate the Jiffle script and write results directly to * the destination image(s). Client code can call either of the methods: * <ul> * <li>{@code evaluate(int x, int y)} * <li>{@code evaluateAll(JiffleProgressListener listener} * </ul> * The {@code Jiffle} object must be compiled before calling this method. * * @return the runtime object * @throws JiffleException if the script has not been compiled or if errors * occur in creating the runtime instance */ public JiffleDirectRuntime getRuntimeInstance() throws it.geosolutions.jaiext.jiffle.JiffleException { return (JiffleDirectRuntime) getRuntimeInstance(RuntimeModel.DIRECT); }
private JiffleDirectRuntime getRuntimeWithImageParams() throws Exception { String script = "dest1 = src1; dest2 = src2; dest3 = src3;" ; Map<String, Jiffle.ImageRole> imageParams = new HashMap<>(); imageParams.put("dest1", Jiffle.ImageRole.DEST); imageParams.put("dest2", Jiffle.ImageRole.DEST); imageParams.put("dest3", Jiffle.ImageRole.DEST); imageParams.put("src1", Jiffle.ImageRole.SOURCE); imageParams.put("src2", Jiffle.ImageRole.SOURCE); imageParams.put("src3", Jiffle.ImageRole.SOURCE); Jiffle jiffle = new Jiffle(script, imageParams); return jiffle.getRuntimeInstance(); }
/** * Compiles a script read from a file and submits it for execution. * * @param scriptFile file containing the Jiffle script * @throws Exception on problems compiling the script */ public void compileAndRun(File scriptFile) throws Exception { Map<String, Jiffle.ImageRole> imageParams = new HashMap<>(); imageParams.put("result", Jiffle.ImageRole.DEST); Jiffle jiffle = new Jiffle(scriptFile, imageParams); JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); WritableRenderedImage destImage = ImageUtilities.createConstantImage(WIDTH, HEIGHT, 0d); runtime.setDestinationImage("result", destImage); executor.submit(runtime, new NullProgressListener()); }
/** * Compiles a script read from a file and submits it for execution. * * @param scriptFile file containing the Jiffle script * @throws Exception on an error in the Jiffle compiler */ public void compileAndRun(File scriptFile) throws Exception { Map<String, Jiffle.ImageRole> imageParams = new HashMap<>(); imageParams.put("result", Jiffle.ImageRole.DEST); Jiffle jiffle = new Jiffle(scriptFile, imageParams); Map<String, RenderedImage> images = new HashMap<>(); images.put("result", ImageUtilities.createConstantImage(WIDTH, HEIGHT, Double.valueOf(0d))); if (jiffle.isCompiled()) { JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); final TiledImage destImg = ImageUtilities.createConstantImage(WIDTH, HEIGHT, 0d); runtime.setDestinationImage("result", destImg); runtime.evaluateAll(null); ImageFrame frame = new ImageFrame(destImg, "Jiffle image demo"); frame.setVisible(true); } }
protected void testScript(String script, RenderedImage srcImg, Evaluator evaluator) throws Exception { imageParams = new HashMap<>(); imageParams.put("dest", Jiffle.ImageRole.DEST); imageParams.put("src", Jiffle.ImageRole.SOURCE); // test the direct runtime Jiffle jiffle = new Jiffle(script, imageParams); directRuntimeInstance = jiffle.getRuntimeInstance(); testDirectRuntime(srcImg, directRuntimeInstance, evaluator); // and now the indirect one indirectRuntimeInstance = (JiffleIndirectRuntime) jiffle.getRuntimeInstance(Jiffle.RuntimeModel.INDIRECT); evaluator.reset(); testIndirectRuntime(srcImg, indirectRuntimeInstance, evaluator); }
@Test(expected=JiffleException.class) public void getRuntimeBeforeCompiling() throws Exception { System.out.println(" getRuntimeInstance before compiling"); String script = "dest = 42;"; jiffle.setScript(script); imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(imageParams); JiffleRuntime runtime = jiffle.getRuntimeInstance(); } }
@Test(expected=JiffleException.class) public void invalidBaseClass() throws Exception { class Foo extends NullRuntime { } setupSingleDestScript(); Object runtime = jiffle.getRuntimeInstance(Foo.class); }
private JiffleDirectRuntime getRuntime(String script) throws Exception { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); Map<String, Jiffle.ImageRole> params = new HashMap<>(); params.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(params); jiffle.compile(); return jiffle.getRuntimeInstance(); }
private JiffleDirectRuntime getRuntimeWithImagesBlock() throws Exception { String script = "images {" + " src1=read; src2=read; src3=read;" + " dest1=write; dest2=write; dest3=write;" + "}" + "dest1 = src1; dest2 = src2; dest3 = src3;" ; Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); return jiffle.getRuntimeInstance(); }
@Test public void getDirectRuntime() throws Exception { setupSingleDestScript(); Object runtime = jiffle.getRuntimeInstance(Jiffle.RuntimeModel.DIRECT); assertTrue(runtime instanceof JiffleDirectRuntime); }
private JiffleDirectRuntime getRuntime(String script) throws Exception { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); return jiffle.getRuntimeInstance(); } }
@Test public void customIndirectBaseClass() throws Exception { setupSingleDestScript(); Object runtime = jiffle.getRuntimeInstance(MockIndirectBaseClass.class); assertTrue(runtime instanceof MockIndirectBaseClass); }
@Test public void getIndirectRuntime() throws Exception { setupSingleDestScript(); Object runtime = jiffle.getRuntimeInstance(Jiffle.RuntimeModel.INDIRECT); assertTrue(runtime instanceof JiffleIndirectRuntime); }
@Test public void customDirectBaseClass() throws Exception { setupSingleDestScript(); Object runtime = jiffle.getRuntimeInstance(MockDirectBaseClass.class); assertTrue(runtime instanceof MockDirectBaseClass); }
private JiffleDirectRuntime getRuntime(String script) throws Exception { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); Map<String, Jiffle.ImageRole> imageParams = new HashMap<>(); imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(imageParams); jiffle.compile(); JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); WritableRenderedImage destImg = ImageUtilities.createConstantImage(WIDTH, WIDTH, 0d); runtime.setDestinationImage("dest", destImg); return runtime; }
@Test public void countValues() throws Exception { System.out.println(" counting pixels that meet a condition"); final int testVal = 10; String script = String.format( "images { src=read; } \n" + "init { count = 0; } \n" + "count += src > %d;", testVal); TestData testData = createTestData(testVal); Jiffle jiffle = getCompiledJiffle(script); JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); runtime.setSourceImage("src", testData.image); runtime.evaluateAll(null); Double count = runtime.getVar("count"); assertNotNull(count); assertEquals(testData.expectedCount, count.intValue()); }
@Test public void noDestImage() throws Exception { System.out.println(" destination-less script with images block"); String script = String.format( "images { inimage = read; } \n" + "init { n = 0; } \n" + "n += inimage >= %d;", NUM_PIXELS - 5); Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); directRuntimeInstance = jiffle.getRuntimeInstance(); directRuntimeInstance.setSourceImage("inimage", createSequenceImage()); directRuntimeInstance.evaluateAll(null); Double var = directRuntimeInstance.getVar("n"); assertNotNull(var); assertEquals(5, var.intValue()); }
private void testScript(String script, int numSrcBands, Evaluator evaluator) throws Exception { imageParams = new HashMap<>(); imageParams.put("dest", Jiffle.ImageRole.DEST); imageParams.put("src", Jiffle.ImageRole.SOURCE); Jiffle jiffle = new Jiffle(script, imageParams); JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); TiledImage srcImg = createSequenceImage(numSrcBands); runtime.setSourceImage("src", srcImg); TiledImage destImg = ImageUtilities.createConstantImage(WIDTH, WIDTH, 0d); runtime.setDestinationImage("dest", destImg); runtime.evaluateAll(null); double[] values = new double[numSrcBands]; for (int y = 0; y < WIDTH; y++) { for (int x = 0; x < WIDTH; x++) { for (int b = 0; b < numSrcBands; b++) { values[b] = srcImg.getSampleDouble(x, y, b); } assertEquals(evaluator.eval(values), destImg.getSampleDouble(x, y, 0), TOL); } } }
private void assertScriptResult(String script, Evaluator e, String srcVarName, String destVarName) throws Exception { RenderedImage srcImg = null; WritableRenderedImage destImg = null; Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); directRuntimeInstance = (JiffleDirectRuntime) jiffle.getRuntimeInstance(); if (srcVarName != null && srcVarName.length() > 0) { srcImg = createSequenceImage(); directRuntimeInstance.setSourceImage(srcVarName, srcImg); } if (destVarName != null && destVarName.length() > 0) { destImg = ImageUtilities.createConstantImage(IMG_WIDTH, IMG_WIDTH, 0d); directRuntimeInstance.setDestinationImage(destVarName, destImg); } directRuntimeInstance.evaluateAll(null); assertImage(srcImg, destImg, e); }