Implementation 
of 
Java Photo Editor

Mathematica Developer's Conference, Oct 13th 2001

Junzo SATO, University of Kumamoto, Japan
Chikara MIYAJI, Japan Institute of Sports Sciences, Japan 
Mariusz JANKOWSKI, University of Southern Maine, USA

Acknowlegdment: I would like to thank Dr. Miyaji for introducing me to this conference.

1. Mathematica Oriented Photo Editor

Providing...

Supplemental photo editing environment 
which is easy-to-use for research and education in Mathematica 

by combining two technologies:

[i] "Digital Image Processing" package

[ii] Interactive Graphics technique with MathLink foundation

2. Programming Tools

[i] Digital Image Processing
	--- for Flexibility of Symbolic Processing


J/Link is Serious Choice for the Network Programming! 

[ii] JFC Swing 
	--- for Construction of GUI

[iii] J/Link Event Handler
	--- for Interactivity of Graphics Manipulation

3. J/Link ( ver 1.1.2 and jdk 1.3.1 )

Most of code is written in Mathematica Language.
Some classes (only 4 or 5) are written in Java Language for efficiency, such as 
memory overload, casting issue related with Java's native Imp class, GUI response, etc.

4. Essential Java Classes

4.1 MathJPanel

MathJPanel is a MathCanvas to support Java Swing

public class MathJPanel extends JPanel implements java.io.Serializable {
	...
	
	// MathJPanel has two images
	private Image im;// memory is allocated inside Java system instead of inside MathKernel
	private Image overlay;
	...
	
	// MathJPanel can create offscreen image
	public Image createImage(int width, int height) { 
		creates an offscreen image 
	}
	
	// The overlay is created to fit to the im.
	public void createOverlay() { creates overlay image }
	public void removeOverlay(){ removes overlay image }
	...
	
	// methods for overlay image
	public void setXORMode( Color c ) { gOverlay.setXORMode( c ); }
	...
	
	// The offscreen im can be drawn by other image including overlay
	public void drawJavaImage(Image anImage, int x, int y) { 
		draws anImage temporaly to the offscreen 
	}
	public void addJavaImage(Image anImage, int x, int y) { 
		adds anImage to the offscreen 
	}
	
	// visualization method
	public void paint(Graphics g) { 
		draws offscreen and overlay 
	}
}

4.2 Java Thread

Default Workflow

User Action 
---> Java's Event Handler 
---> J/Link's callMathHandler()  
---> Mathematica's user-defined customHandler[ ]

customHandler[ ] := (
	Do something... 
		show dialog, read file, draw marquee (area selection tool),
		lengthy work, time-consuming image process, etc...
	Return. // this function returns only after the above work is done
)

customHandler[ ] is defined in MathKernel

Symbol "customHandler" is stored in MathListener by listener@setHandler[ ]

Threaded Workflow

User Action 
---> Java's Event Handler 
---> Java Photo Editor's callMathHandler() wrapped in Java Thread 
---> User-defined customHandler[ ] in Mathematica

ThreadMathListener

public abstract class ThreadMathListener implements EventListener {
        ....

	public Expr callMathHandler( String meth, Object[] args){
	    ....
	}
}

MathListener has callMathHandler( ) in protected.
ThreadMathListener has callMathHandler( ) in public.

ThreadMathActionListener

public class ThreadMathActionListener 
	extends ThreadMathListener implements ActionListener {

	class ActionThread extends Thread {
		ActionEvent evt = null;
		ThreadMathActionListener obj = null;
				
		ActionThread(ThreadMathActionListener tmal, ActionEvent ae) {
			obj = tmal;
			evt = ae;
			start();
		}
		
		public void run() {
			Expr result = obj.callMathHandler( 
				"actionPerformed", new Object[]{evt, evt.getActionCommand()}
			);
			if (result != null) {
				result.dispose();
			}
			stop();
		}
	}
	
	...
	
	public void actionPerformed(ActionEvent e) {
		new ActionThread(this, e);
	}
}

4.3 Listeners for Swing

MathSwingInternalFrameListener

public class MathSwingInternalFrameListener 
	extends MathListener implements InternalFrameListener {

	public MathSwingInternalFrameListener() {
		super();
	}

	public MathSwingInternalFrameListener(KernelLink ml) {
		super(ml);
	}

	public MathSwingInternalFrameListener(String[ ][ ] handlers) {
		super(handlers);
	}

	// Event handler methods 	
	public void internalFrameActivated(InternalFrameEvent e) {
		Expr result = callMathHandler("internalFrameActivated", new Object[]{e});
		if (result != null) result.dispose();
	}

	public void internalFrameClosed(InternalFrameEvent e) { ... }

	public void internalFrameClosing(InternalFrameEvent e) { ... }

	public void internalFrameDeactivated(InternalFrameEvent e) { ... }

	public void internalFrameDeiconified(InternalFrameEvent e) { ... }

	public void internalFrameIconified(InternalFrameEvent e) { ... }

	public void internalFrameOpened(InternalFrameEvent e) { ... }
}

5. Digital Image Processing

Various Functions: 

BoxFilter[ ], GaussianFilter[ ] , ..., Threshold[ ], ...

6. Mathematica Code --- "Calling Java from Mathematica"

7. Summary

Mathematica oriented photo editing program was created by the combination of the Digital Image Processing package and Interactive Graphics technique, where the J/Link was adopted because of its capability of the Network Programming.

8. Future Tasks

[i] More Java Code for Efficiency
( But "Calling Java from Mathematica" is really good! )

[ii] GUI Design in More Convenient Way for DIP Users

9. References

[1] Digital Image Processing:
    Mathematica Addon Package
    http://www.wolfram.com
     
[2] MathLink Programming:
    Chikara MIYAJI & Paul ABBOTT,
    "Network Programming with Mathematica",
    Cambridge University Press, 2001
    
[3] J/Link Programming Examples:
    http://fc.kuh.kumamoto-u.ac.jp/~jsato/mathematica.htm