// Example ImageJ scripts using the BeanShell interpreter
// from www.beanshell.org. Requires ImageJ v1.17s, which adds the
// IJ.run() method for running ImageJ commands in the current
// thread

// Use "java -cp ij.jar;bsh-1.0.jar bsh.Console" to start BeanShell 
// and try pasting, one at a time, the following commands and methods
// into the BeanShell console. Requires JDK 1.2 or JDK 1.3 and
// both bsh-1.0.jar and ij.jar must be in the current directory.

new ij.ImageJ(null); // start ImageJ

ij.IJ.run("Blobs (25K)"); // open the "Blobs" sample image

macro1() {
  ij.ImagePlus imp = ij.WindowManager.getCurrentImage();
  if (imp==null) {
    ij.IJ.error("No images are open");
    return;
  }
  for (int i=0; i<10; i++) {
    ij.IJ.run("Invert");
    ij.IJ.wait(100);
  }
  for (int i=0; i<5; i++) {
    ij.IJ.run("Sharpen");
    ij.IJ.wait(200);
  }
  ij.IJ.run("Revert");
  ij.IJ.wait(200);
  ij.IJ.run("Find Edges");
  ij.IJ.wait(1000);
  ij.IJ.run("Undo");
  ij.IJ.wait(10);
}
macro1(); // runs macro1

macro2() {
  imp = ij.WindowManager.getCurrentImage();
  if (imp==null) {
    ij.IJ.error("No images are open");
    return;
  }
  ip = imp.getProcessor();
  width = ip.getWidth();
  height = ip.getHeight();
  factor = 2.0;
  ij.IJ.write(width+" "+height);
 		
  ip.snapshot();
  ip.setInterpolate(false);
  for (int i=0; i<7; i++) {
    ip2 = ip.resize((int)(width/factor), (int)(height/factor));
    x = 0; y = 0;
    do {
      ip.insert(ip2, (int)x, (int)y);
      x += width/factor;
      if (x >= width) {
        x = 0;
        y += height/factor;
      }
    } while (y<height);
    imp.updateAndDraw();
    ij.IJ.wait(100);
  }
  ip.reset();
  imp.updateAndDraw();
}
macro2(); // runs macro2