Implementation of Java Photo Editor

Junzo Sato, Chikara Miyaji, and Mariusz Jankowski

University of Kumamoto, Japan
Japan Institute of Sports Sciences, Japan
University of Southern Maine, USA

Email:
<jsato@fc.kuh.kumamoto-u.ac.jp>,
<Chikara.MIYAJI@jiss.ntgk.go.jp>,
<mjkcc@usm.maine.edu>

Introduction

J/Link is not just a wrapper for MathLink, but also a powerful application development environment because of Java's variety of GUI components and built-in event handling mechanism. Digital Image Processing is an add-on application packages for Mathematica. It covers a vast area of scientific and engineering knowledge and works as a powerful engine for image processing. Combination of a customizable frontend application and the Digital Image Processing package is therefore the ideal environment for research and education. The advantages of Java Photo Editor are as follows.

(1) Platform independent customizable GUI components constructed by Mathematica.
(2) Customizable event handlers defined by Mathematica.
(3) Easy manipulations of visualized image data in a manner similar to Photoshop or NIHImage.
(4) Decrease in memory overload of Mathematica to maintain image data.
(5) Benefits from the comprehensive functionality of the Digital Image Processing application package.
(6) Data acquisition and processing enhancements are available supporting external technologies such as TWAIN, QuickTime, Graphic boards, Java Graphics, etc.

Specification

The following functionality is to be implemented in Java Photo Editor.

(1) Multiple images are shown in frames on the desktop.
(2) Menu commands to import, plot, and process image data are listed in the menubar.
(3) Image manipulation tools to select a region of interest of the image are prepared in the palette.
(4) Definitions of user action handlers are written by Mathematica so that they can be customized in the middle of runtime.
(5) Image data is stored in Java Photo Editor to decrease memory overload of Mathematica.

Implementation

J/Link is a sufficient environment to implement Photo Editor. Because Mathematica can load any Java classes, creating GUI components with JFC Swing using Mathematica expressions is done with ease. Writing code in Mathematica language instead of in Java is also convenient when the configuration of components is changed because the user can evaluate expressions in the middle of runtime. For convenience, the authors wrote a Mathematica package JavaSwing.m which has wrapper functions for JFC Swing constructors.

One of advantages of J/Link is that easy-to-use event handling mechanism for Mathematica is introduced based on the Java framework. The built-in method, callMathHandler( ) in the class MathListener, relates an user action event in Java with a specified Mathematica function. In most cases, when the user creates some GUI components using Mathematica expressions, event handlers are also written with Mathematica to benefits from Mathematica's flexible scriptability. For example, when a user clicks a menu item "Box Filter" after selecting an area of image, a method actionPerformed( ) in MathActionListener is invoked in Java. Because callMathHandler( ) is called inside this method, corresponding menu handler which is defined in Mathematica is evaluated through J/Link. Tasks the handler performs are: send a part of image data from Java Photo Editor to MathKernel, create source ImageData using Digital Image Processing package, get resulting ImageData by applying BoxFilter[ ] function defined in the package, and send this result back to Java Photo Editor. The total time of data transfers between Java and MathKernel in this process is negligible compared to the MathKernel execution times of many image processing operations.

User actions, such as menu selection and mouse operation, are asynchronous events. When handlers of these events call MathKernel, invocations are appropriately synchronized by J/Link's built-in features. To avoid the blocking of GUI while the evaluation is being processed in MathKernel, the callMathHandler( ) is wrapped in the Java thread. The figure shows the Java Photo Editor desktop with some images displayed including examples of filtering effects.

It is not always possible to write entire systems with Mathematica. One reason is the execution rate of the system. For instance, it would be better for a handler to be implemented in Java rather than Mathematica if the same functionality can be achieved using Java code. Another reason is that there is the case that J/Link cannot cast a Java object to an appropriate class. For example, when the user creates an instance of the abstract class java.awt.Image from Mathematica by calling Java's createImage( ) method, the returned type of the instance, which is shown in Notebook, is some specific one such as "com.apple.mrj.internal.awt.VImage". Passing this object as an argument of some method which receives java.awt.Image fails when the method is invoked from Mathematica. Casting the object type explicitly from the Mathematica is impossible for an arbitrary object. In some cases, the user needs to cast the object to access some methods. This requires the user to write Java code.

Summary

Because of Java's rich resources and event handling mechanism, J/Link is very effective environment to implement the photo editing application, Java Photo Editor. To benefits from  Mathematica's flexible scriptability, it is better to write code in Mathematica instead of in Java. However careful consideration of the proper balance between Mathematica code and Java code is required mainly for the efficiency of execution.

References

[1] Todd Gayley: J/Link User Guide, Wolfram Research, 2000

[2] C.Miyaji and P.Abbott: MathLink -- Network Programming with Mathematica, Cambridge University Press, 2001

[3] J.Sato, C.Miyaji, and M.Jankowski: Photo Editor on Java, IMS2001, 2001

[4] S.Jarmuz and M.Jankowski: RIVA - real-time image acquisition and viewing, Proceedings of IMS, 2001.

[5] URL: <http://fc.kuh.kumamoto-u.ac.jp/~jsato/mathematica.htm>