== Using plugins == Click "Plugins -> Load Plugin" from the Transformer window menu bar. You should be presented with a file browser, select a jar file containing one or more Batchable and/or Plugin classes. The plugin will add a new cascaded menu to the plugins menu, the cascaded menu will contain each "Batchable" class as both a single and a batch item on the "Plugins" sub menu. Any Plugin classes may make additional changes to the user interface, such as adding a new menu. == Writing Batchable and Plugin classes == JPsychomorph handles two plugin interfaces, the main one is Batchable, which allows single and batch processing of images. The second interface, Plugin, is more general, and simply supports a setup and cleanup method. The internal handling of these interface types is different. For Batchable classes a new instance of the class is created each time it is used (in batch or single mode). For Plugin classes a single instance is created on loading and the setup method is called, this instance persists until the programme closes or the plugin is unloaded. A single jar file can contain a mixture of Plugin and Batchable classes, and all will be loaded or unloaded together. If a class implements both interfaces, one instance will be created as the Plugin, and will persist until unloaded, another instance will be created each time for batch or single mode Batchable processing. Any implementing class for either of these classes should have an empty (or no) constructor, if they do not they will not be loaded. These interfaces are part of the Facemorph library available here: * http://users.aber.ac.uk/bpt/jpsychomorph/version4/facemorphlib.jar -- facemorphlib.jar Documentation for all the classes in the library is available here: * http://users.aber.ac.uk/bpt/jpsychomorph/version4/javadoc/ -- facemorphlib documentation === Writing Plugin classes === The Plugin interface supports just two methods, setup and cleanup: {{{ public interface Plugin { public boolean setup(PsychoMorphForm psychomorph); public boolean cleanup(PsychoMorphForm psychomorph); } }}} A very simple example of a Plugin is given below, this simply adds a menu "Boo!" to the Psychomorph window, with one item "Boo!", which when pressed brings up a dialog saying, you guessed, "Boo!". {{{ import Facemorph.psychomorph.Plugin; import Facemorph.psychomorph.PsychoMorphForm; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; public class AddMenuTest extends JMenu implements Plugin { PsychoMorphForm psychomorph; public boolean setup(PsychoMorphForm psychomorph) { this.psychomorph = psychomorph; JMenuItem anItem = new JMenuItem("Boo!"); anItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(AddMenuTest.this.psychomorph, "Boo!"); } }); setText("Boo!"); add(anItem); psychomorph.addMenu(this); return true; } public boolean cleanup(PsychoMorphForm psychomorph) { psychomorph.removeMenu(this); return true; } } }}} === Writing Batchable classes === To implement a Batchable plugin for JPsychoMorph you need to create a Java class that overrides the Facemorph.Batchable interface: {{{ public interface Batchable { public boolean process(ImageZoomPanel izp, boolean single); public boolean initialise(PsychoMorphForm psychomorph); public void finish(); public String getName(); public boolean getReadTemplate(); public boolean getWriteTemplate(); public boolean getWriteImage(); } }}} The main method in the Batchable interface is ''process'', which will be called for each image in the batch. The ''single'' parameter is true if the processing is '''not''' operating in batch mode, but on a single loaded image. This is useful for things like pushing undo information on the stack, which you may not want to do in batch mode. The [http://users.aber.ac.uk/bpt/jpsychomorph/version4/javadoc/Facemorph/psychomorph/ImageZoomPanel.html ImageZoomPanel] allows access to the currently loaded Image and Template. The ''initialise'' method is called once before a batch of images, or before a single image is processed. It allows access to the rest of the system state via the [http://users.aber.ac.uk/bpt/jpsychomorph/version4/javadoc/Facemorph/psychomorph/PsychoMorphForm.html PsychoMorphForm] parameter. The ''finish'' method is called after all the images in the list have been processed, it is not called when operating on a single image. The ''getName'' method should return the name of the Batchable for inclusion in the ''Plugins'' menu on the psychomorph Transform window. Both Batch and single versions of the plugin will be added to the menu. The three methods getReadTemplate(), getWriteTemplate() and getWriteImage() simply return true or false, to indicate if the template or image should be read before processing or written after processing. The image is always read before processing. The class must also have a no parameter (default) constructor for initialising. This is called both when the plugin is first loaded into the system and every time the (single or batch) menu item is clicked. == Examples == Full source code including a NetBeans project is available: * http://users.aber.ac.uk/bpt/jpsychomorph/version4/pluginexamples.zip -- NetBeans project and source code for example plugins * http://users.aber.ac.uk/bpt/jpsychomorph/version4/pluginexamples.jar -- Just the compiled plugins === Simple example === An example of a simple Batchable is given below. This Batchable checks that the inner lip points are correctly positioned vertically, and swaps corresponding points if needed. {{{ import Facemorph.Template; import Facemorph.psychomorph.Batchable; import Facemorph.psychomorph.ImageZoomPanel; import Facemorph.psychomorph.PsychoMorphForm; import java.awt.geom.Point2D; /** * Batch corrects the mouth in the "standard" template, by checking if the top -lip is below the bottom lip points and swapping if needed */ public class BatchCorrectMouth implements Batchable { public boolean process(ImageZoomPanel izp, boolean single) { Template tem = izp.getTemplate(); int[] topMouth = {94, 95, 96, 97, 98}, bottomMouth = {99, 100, 101, 102, 103}; for (int i=0; i