import java.awt.*; import java.awt.event.*; import java.io.*; import ij.plugin.frame.*; import ij.*; import ij.process.*; import ij.gui.*; /** *The Two_D_Manipulation class uses much of the same code in IP_Demo.java *written by Wayne Rasband and can be found under the ImageJ website at *http://rsbweb.nih.gov/ij/plugins/ip-demo.html. This versioon however modifies *some of the parameters in the panel and has a new button that allows six *stacks to be combined into one. * *@author Chris Tsang & Gurinder Kler *@version 1.0 */ public class Two_D_Manipulation extends PlugInFrame implements ActionListener { Panel panel; int previousID; static Frame instance; public Two_D_Manipulation() { super("Two D Manipulations"); if (instance!=null) { instance.toFront(); return; } instance = this; IJ.register(Two_D_Manipulation.class); setLayout(new FlowLayout()); panel = new Panel(); panel.setLayout(new GridLayout(4, 4, 5, 5)); addButton("Reset"); addButton("Histogram"); addButton("Invert"); addButton("Rotate"); addButton("Lighten"); addButton("Darken"); addButton("Zoom In"); addButton("Zoom Out"); addButton("Smooth"); addButton("Sharpen"); addButton("Find Edges"); addButton("Add Noise"); addButton("Reduce Noise"); addButton("Start Animation"); addButton("Stop Animation"); addButton("Combine Stacks"); addButton("Create 3D"); add(panel); pack(); show(); } void addButton(String label) { Button b = new Button(label); b.addActionListener(this); panel.add(b); } public void actionPerformed(ActionEvent e) { ImagePlus imp = WindowManager.getCurrentImage(); if (imp==null) { IJ.beep(); IJ.showStatus("No image"); previousID = 0; return; } if (!imp.lock()) {previousID = 0; return;} ImageProcessor ip = imp.getProcessor(); int id = imp.getID(); if (id!=previousID) ip.snapshot(); previousID = id; String label = e.getActionCommand(); if (label==null) return; new Runner(label, imp, ip); } public void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID()==WindowEvent.WINDOW_CLOSING) { instance = null; } } } class Runner extends Thread { private String command; private ImagePlus imp; private ImageProcessor ip; Runner(String command, ImagePlus imp, ImageProcessor ip) { super(command); this.command = command; this.imp = imp; this.ip = ip; setPriority(Math.max(getPriority()-2, MIN_PRIORITY)); start(); } public void run() { try {runCommand(command, imp, ip);} catch(OutOfMemoryError e) { IJ.outOfMemory(command); if (imp!=null) imp.unlock(); } catch(Exception e) { CharArrayWriter caw = new CharArrayWriter(); PrintWriter pw = new PrintWriter(caw); e.printStackTrace(pw); IJ.write(caw.toString()); IJ.showStatus(""); if (imp!=null) imp.unlock(); } } void runCommand(String command, ImagePlus imp, ImageProcessor ip) { IJ.showStatus(command + "..."); if (command.equals("Reset")) ip.reset(); else if(command.equals("Histogram")) { imp.unlock(); IJ.run("Histogram"); } else if (command.equals("Invert")) ip.invert(); else if (command.equals("Lighten")) { if (imp.isInvertedLut()) ip.multiply(0.9); else ip.multiply(1.1); } else if (command.equals("Darken")) { if (imp.isInvertedLut()) ip.multiply(1.1); else ip.multiply(0.9); } else if (command.equals("Rotate")) ip.rotate(90); else if (command.equals("Zoom In")) ip.scale(1.2, 1.2); else if (command.equals("Zoom Out")) ip.scale(.8, .8); //else if (command.equals("Threshsold")) //ip.autoThreshold(); else if (command.equals("Smooth")) ip.smooth(); else if (command.equals("Sharpen")) ip.sharpen(); else if (command.equals("Find Edges")) ip.findEdges(); else if (command.equals("Add Noise")) ip.noise(20); else if (command.equals("Reduce Noise")) ip.medianFilter(); else if (command.equals("Combine Stacks")) IJ.runPlugIn("Combine_Stacks","Combine Stacks"); else if (command.equals("Start Animation")) IJ.doCommand("Start Animation [=]"); else if(command.equals("Stop Animation")) IJ.doCommand("Stop Animation"); else if(command.equals("Create 3D")) IJ.showMessage("Gurinder Kler!!!!! How come it doesn't work??"); Roi roi = imp.getRoi(); if (roi!=null) { int[] mask = roi.getMask(); if (mask!=null) ip.reset(mask); } imp.updateAndDraw(); imp.unlock(); IJ.showStatus(""); } }