Examples of MIPAV plug-ins

From MIPAV
Jump to: navigation, search

Examples of MIPAV plug-ins

To build plug-in programs, three files are typically required:
PluginFoo.java-Provides an interface to MIPAV and the plugin.
PluginDialogFoo.java-Invokes the dialog to get user-supplied parameters; it can be hidden when no parameters are required.
PluginAlgorithmFoo.java-Provides the actual algorithm to be implemented. It can be a mixture of calls to MIPAV's API, C programs, Perl, ITK, etc.

Where Foo is the name that you supply for the program. The following sample plug-in program(s) are included in MIPAV documentation:

PlugInSample-a sample plug-in, see [MIPAV_Plugins.html#1300237 "Sample plug-in program" ] below.
PlugInCT_MD-a typical plug-in. (Refer to the MIPAV Users Guide, PDF version.)
PlugInAlgorithm.Median-a very complicated plug-in. Refer to MIPAV Volume 1 Users Guide, Appendix D.
PlugInDialogImageVOIDisplay.java - a self contained plug-in.

Sample plug-in program

The source code for the plug-in program, PlugInSample.java is an example of a simple algorithm type of plug-in. See Figure 20.
Figure 20. PlugInSample.java

PlugInSample.java
1 import gov.nih.mipav.plugins.*; // needed to load PluginAlgorithm / PluginView /
2 // PlugInFile interface
3 import gov.nih.mipav.view.*;
4 import gov.nih.mipav.model.structures.*;
5 import java.awt.*;
6
7 /*** This is a simple plugin to display a image in a new frame @see PlugInAlgorithm */
8
9 /** This is an Algorithm type of PlugIn and therefore must implement PlugInAlgorithm
10 ** Implementing the PlugInAlgorithm requires this class to implement the run method
11 ** with the correct parameters */
12
13 public class PlugInSample implements PlugInAlgorithm {
14 /**
15 * Defines body of run method, which was declared in the interface.
16 * @param UI User Interface
17 * @param parentFrame ParentFrame
18 * @param image Current ModelImage--this is an image already loaded into
19 * MIPAV. Can be null.
20 */
21
22 public void run (ViewUserInterface UI, Frame parentFrame, ModelImage image){
23 if (parentFrame instanceof ViewJFrameImage) {
24 new PlugInDialogSample(parentFrame,image);
25 } else {
26 MipavUtil.displayError("PlugInSample only runs on an image frame.");
27 }
28 }
29 }

Plug-in Dialog Sample


PlugInSample.java opens an image in a new image frame using its own dialog box. It requires three files:
PlugInSample.java-Provides an interface to MIPAV and the plug-in program. See Figure 20.
PlugInDialogSample.java-Invokes the dialog to get user-supplied parameters. Refer to Figure 21.
PlugInAlgorithmSample.java-Implements the algorithm. See Figure 22.

Figure 21. PlugInDialogSample.java

PlugInDialogSample.java
1 import gov.nih.mipav.view.*;
2 import gov.nih.mipav.view.dialogs.*;
3 import gov.nih.mipav.model.structures.*;
4 import gov.nih.mipav.model.algorithms.*;
5
6 import java.awt.event.*;
7 import java.awt.*;
8 import java.util.*;
9 import javax.swing.*;
10
11
12 public class PlugInDialogSample extends JDialogBase implements AlgorithmInterface {
13
14 /** Source image reference. */
15 private ModelImage image; // source image
16 private ViewUserInterface userInterface;
17
18 /** Sample algorithm reference. */
19 private PlugInAlgorithmSample sampleAlgo = null;
20
21 public PlugInDialogSample(Frame theParentFrame, ModelImage im) {
22 super(theParentFrame, false);
23
24 if ((im.getType() == ModelImage.BOOLEAN) || im.isColorImage()) {
25 MipavUtil.displayError("Source Image must NOT be Boolean or Color");
26 dispose();
27
28 return;
29 }
30
31 image = im;
32 userInterface = ViewUserInterface.getReference();
33 init();
34 }
35
36 // ************************************************************************
37 // ************************** Event Processing ****************************
38 // ************************************************************************
39
40 /**
41 * Closes dialog box when the OK button is pressed and calls the algorithm.
42 * @param event Event that triggers function.
43 */
44
45 public void actionPerformed(ActionEvent event) {
46 String command = event.getActionCommand();
47
48 if (command.equals("OK")) {
49 callAlgorithm();
50 } else if (command.equals("Cancel")) {
51 dispose();
52 }
53 }
54
55 /**
56 * Sets up the GUI (panels, buttons, etc) and displays it on the screen.
57 */
58 private void init() {
59
60 // Build the Panel that holds the OK and CANCEL Buttons
61 JPanel OKCancelPanel = new JPanel();
62
63 JLabel questionLabel = new JLabel("Display Images?");
64
65 // size and place the OK button
66 buildOKButton();
67 OKCancelPanel.add(OKButton, BorderLayout.WEST);
68
69 // size and place the CANCEL button
70 buildCancelButton();
71 OKCancelPanel.add(cancelButton, BorderLayout.EAST);
72 getContentPane().add(questionLabel, BorderLayout.NORTH);
73 getContentPane().add(OKCancelPanel, BorderLayout.SOUTH);
74
75 pack();
76 setVisible(true);
77 setResizable(false);
78 System.gc();
79 }
80 /*** This method is required if the AlgorithmPerformed interface is implemented. It is called by the algorithm when it has completed or failed to to complete, so that the dialog can be display the result image and/or clean up. */
81
82 /** @param algorithm Algorithm that caused the event. */
83
84 public void algorithmPerformed(AlgorithmBase algorithm) {
85 if (algorithm instanceof PlugInAlgorithmCT_MD) {
86 if ( sampleAlgo.isCompleted() ) {
87 dispose();
88 }
89 }
90 }
91
92
93 /*** Once all the necessary variables are set, call the Gaussian Blur algorithm based on what type of image this is and whether or not there is a separate destination image. */
94
95 protected void callAlgorithm() {
96 sampleAlgo = new PlugInAlgorithmSample(null, image);
97 sampleAlgo.addListener(this);
98 setVisible(false); // Hide dialog
99
100 if (isRunInSeparateThread()) {
101
102 //*** Start the thread as a low priority because we wish to still have user interface work fast.*/
103 if (sampleAlgo.startMethod(Thread.MIN_PRIORITY) == false) {
104 MipavUtil.displayError("A thread is already running on this object");
105 }
106 } else {
107 sampleAlgo.run();
108 }
109 }
110
111 }


Plug-in Algorithm Sample

Figure 22. PlugInAlgorithmSample.java

PlugInAlgorithmSample.java
1 import gov.nih.mipav.model.algorithms.AlgorithmBase;
2 import gov.nih.mipav.model.structures.*;
3
4 import gov.nih.mipav.view.*;
5
6
7 public class PlugInAlgorithmSample extends AlgorithmBase {
8
9 private ViewJFrameImage frame;
10
11 /*** Constructor for 3D images in which changes are placed in a predetermined destination image.
12 */
13
14 /**
15 * @param destImg Image model where result image is to stored.
16 * @param srcImg Source image model.
17 */
18 public PlugInAlgorithmSample(ModelImage destImg, ModelImage srcImg) {
19 super(destImg, srcImg);
20 }
21
22 //~ Methods -----------------------------------------------------------------------------/
23
24 /**
25 * Prepares this class for destruction.
26 */
27 public void finalize() {
28 destImage = null;
29 srcImage = null;
30 super.finalize();
31 }
32
33
34 /**
35 * Starts the algorithm.
36 */
37 public void runAlgorithm() {
38 frame = new ViewJFrameImage((ModelImage)srcImage.clone());
39 setCompleted(true);
40 }
41
42 }


Next: Plug-in CT_MD, a typical plug-in program

See also: