/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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 destBounds the bounds of the new destination image * * @return the instance of this class to allow method chaining */ public JiffleBuilder dest(String varName, Rectangle destBounds) { if (destBounds == null || destBounds.isEmpty()) { throw new IllegalArgumentException("destBounds argument cannot be null or empty"); } return dest(varName, destBounds.x, destBounds.y, destBounds.width, destBounds.height); }
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
private void runScript(String script, String destVarName) { try { scriptPane.setText(script); JiffleBuilder builder = new JiffleBuilder(); builder.script(script).dest(destVarName, imageWidth, imageHeight); RenderedImage image = builder.run().getImage(destVarName); imagePane.setImage(image); } catch (JiffleException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Errors compiling or running the script", "Bummer", JOptionPane.ERROR_MESSAGE); } }
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 } }
@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()); }
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
@Test public void clearingDestImage() throws Exception { System.out.println(" clear builder dest image"); int w = 10; int h = 20; jb.dest("dest", new Rectangle(0, 0, w, h)); RenderedImage img = jb.getImage("dest"); assertNotNull(img); jb.clear(); img = jb.getImage("dest"); assertNull(img); }
@Test(expected=JiffleRuntimeException.class) public void readOutsideBoundsWithOptionNotSet() throws Exception { System.out.println(" reading outside image bounds with option not set"); String script = "dest = src[$-1, 0];"; RenderedImage srcImg = ImageUtilities.createConstantImage(4, 4, 0); builder.script(script).source("src", srcImg).dest("dest", 4, 4).run(); }
@Test public void removeImage() throws Exception { System.out.println(" remove image"); String script = "dest = 42;" ; jb.script(script).dest("dest", 10, 10).run(); RenderedImage image = jb.removeImage("dest"); assertNotNull(image); image = jb.getImage("dest"); assertNull(image); }
@Test public void runMethod() throws Exception { System.out.println(" using run() method"); String script = "dest = x();"; jb.script(script).dest("dest", 10, 10).run(); RenderedImage img = jb.getImage("dest"); assertNotNull(img); Evaluator e = new Evaluator() { int x = 0; public double eval(double val) { int xx = x; x = (x + 1) % IMG_WIDTH; return xx; } }; assertImage(null, img, e); }
/** * 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); } }
@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); }
@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 nestedCon() throws Exception { String script = "dest = con(src1, con(src1 > src2, 1, null), null);" ; System.out.println(" " + script); final double threshold = IMG_WIDTH * IMG_WIDTH / 2; RenderedImage src1 = createSequenceImage(); RenderedImage src2 = ImageUtilities.createConstantImage(IMG_WIDTH, IMG_WIDTH, threshold); Evaluator e = new Evaluator() { public double eval(double val) { return val > threshold ? 1 : Double.NaN; } }; JiffleBuilder builder = new JiffleBuilder(); builder.script(script).source("src1", src1).source("src2", src2); builder.dest("dest", IMG_WIDTH, IMG_WIDTH); RenderedImage dest = builder.run().getImage("dest"); assertImage(src1, dest, e); }
private void assertResult(RenderedImage resultImage, String script) throws Exception { JiffleBuilder builder = new JiffleBuilder(); builder.script(script).dest("dest", WIDTH, WIDTH).run(); RenderedImage referenceImage = builder.getImage("dest"); RectIter resultIter = RectIterFactory.create(resultImage, null); RectIter referenceIter = RectIterFactory.create(referenceImage, null); do { do { assertEquals(resultIter.getSample(), referenceIter.getSample()); resultIter.nextPixelDone(); } while (!referenceIter.nextPixelDone()); resultIter.startPixels(); resultIter.nextLineDone(); referenceIter.startPixels(); } while (!referenceIter.nextLineDone()); } }