private void step() { activeRuntime.setSourceImage(WORLD_NAME, curWorld); activeRuntime.setDestinationImage(NEXT_WORLD_NAME, nextWorld); activeRuntime.evaluateAll(null); TiledImage temp = curWorld; curWorld = nextWorld; nextWorld = temp; EventQueue.invokeLater(new Runnable() { public void run() { imagePane.setImage(curWorld); } }); }
JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); runtime.setDefaultTransform(_defaultTransform); if (worldInfo != null) { runtime.setWorldByResolution(worldInfo.bounds, worldInfo.xres, worldInfo.yres); switch (imageParams.get(var)) { case SOURCE: runtime.setSourceImage(var, img, transform); break; runtime.setDestinationImage(var, (WritableRenderedImage)img, transform); break;
public void runtimeExample() throws Exception { // docs start // World bounds Rectangle2D worldBounds = new Rectangle2D.Double( 750000, 6500000, 100000, 50000); // Common image bounds Rectangle imageBounds = new Rectangle(0, 0, 4000, 2000); // Set the bounds (world units) and resolution of the // processing area runtimeObj.setWorldByNumPixels(worldBounds, 4000, 2000); // Create a new transform that converts from world units to // pixel positions using the CoordinateTransforms helper class CoordinateTransform tr = CoordinateTransforms.getTransform( worldBounds, imageBounds); // Set this coordinate transform object to be used with all images runtimeObj.setDefaultTransform(tr); // docs end } }
runtime.setWorldByNumPixels(worldBounds, IMG_WIDTH, IMG_WIDTH); runtime.setDefaultTransform(tr); runtime.setSourceImage("src", srcImg); runtime.setDestinationImage("dest", destImg); runtime.evaluateAll(null);
JiffleDirectRuntime runtime = getRuntime(script); runtime.setWorldByResolution(worldBounds, 1, 1); runtime.setSourceImage("src", srcImg, srcTr); runtime.setDestinationImage("dest", destImg); runtime.evaluateAll(null);
runtime.setWorldByResolution(worldBounds, xres, yres); runtime.setDestinationImage("dest", destImg, tr); runtime.evaluateAll(null);
@Test public void mappedSrcValue() throws Exception { String src = "dest = src[10];"; RenderedImage srcImg = createSequenceImage(); Evaluator evaluator = new Evaluator() { public double eval(double val) { return val; } }; imageParams = new HashMap<>(); imageParams.put("dest", Jiffle.ImageRole.DEST); imageParams.put("src", Jiffle.ImageRole.SOURCE); // test the direct runtime Jiffle jiffle = new Jiffle(src, imageParams); directRuntimeInstance = jiffle.getRuntimeInstance(); directRuntimeInstance.setSourceImage("src", srcImg); // map band 10 to band 0 directRuntimeInstance.setSourceImageBandTransform("src", (x, y, scriptBand) -> 0); TiledImage destImg = ImageUtilities.createConstantImage( srcImg.getMinX(), srcImg.getMinY(), srcImg.getWidth(), srcImg.getHeight(), 0.0); directRuntimeInstance.setDestinationImage("dest", destImg); directRuntimeInstance.evaluateAll(null); assertImage(srcImg, destImg, evaluator); }
private void testInject(JiffleDirectRuntime runtime, final Double value) throws Exception { WritableRenderedImage destImg = ImageUtilities.createConstantImage(IMG_WIDTH, IMG_WIDTH, 0d); runtime.setDestinationImage("dest", destImg); if (value != null) { runtime.setVar("n", value); } runtime.evaluateAll(null); Evaluator e = new Evaluator() { public double eval(double val) { return value; } }; assertImage(null, destImg, e); }
runtimeObj.setWorldByNumPixels(worldBounds, destImage.getWidth(), destImage.getHeight()); runtimeObj.setDestinationImage(destVar, destImage, transform); runtimeObj.evaluateAll(null);
/** * 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 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()); }
/** * 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); } }
/** * 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()); }
/** * Setting a default transform without having previously set the world * bounds should throw a JiffleException. */ @Test(expected=JiffleException.class) public void forgetTheWorldBeforeDefaultTransform() throws Exception { System.out.println(" forget to set the world before setting default transform"); JiffleDirectRuntime runtime = getRuntime("images {dest=write;} dest = 42;"); CoordinateTransform tr = CoordinateTransforms.translation(10, 10); runtime.setDefaultTransform(tr); }
@Test public void getDestinationVarNames_ImageParamsMap() throws Exception { System.out.println(" setup with map and get dest var names"); JiffleDirectRuntime runtime = getRuntimeWithImageParams(); String[] names = runtime.getDestinationVarNames(); assertEquals(3, names.length); List<String> expected = new ArrayList<>(); expected.add("dest1"); expected.add("dest2"); expected.add("dest3"); for (int i = 0; i < names.length; i++) { assertTrue(expected.contains(names[i])); expected.remove(names[i]); } }
Rectangle imageBounds = new Rectangle(0, 0, IMG_WIDTH, IMG_WIDTH); runtime.setWorldByNumPixels(worldBounds, IMG_WIDTH, IMG_WIDTH); runtime.setSourceImage("src", srcImg, tr); WritableRenderedImage destImg = ImageUtilities.createConstantImage(IMG_WIDTH, IMG_WIDTH, 0d); runtime.setDestinationImage("dest", destImg, tr); runtime.evaluateAll(null);
runtime.setWorldByResolution(worldBounds, RES, RES); runtime.setDestinationImage("dest", destImg, tr); runtime.evaluateAll(null);
@Test public void injectThenDefault() throws Exception { System.out.println(" run with injected value then default value"); String script = "init { n = 42; } \n" + "dest = n;" ; JiffleDirectRuntime runtime = getRuntime(script); WritableRenderedImage destImg = ImageUtilities.createConstantImage(IMG_WIDTH, IMG_WIDTH, 0d); runtime.setDestinationImage("dest", destImg); runtime.setVar("n", -1.0); runtime.evaluateAll(null); assertImage(null, destImg, new Evaluator() { public double eval(double val) { return -1.0; } }); // set var back to default value (42) runtime.setVar("n", null); runtime.evaluateAll(null); assertImage(null, destImg, new Evaluator() { public double eval(double val) { return 42.0; } }); }
/** * 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 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()); }