Copyright (c) 2002, Junzo SATO. All rights reserved.

Java Photo Editor 2002

Java Photo Editor is a graphics application which has some mouse operative tools for graphics painting and region-of-interest selection. Because Java Photo Editor has interface to Mathematica J/Link, collaborative work of the image process between Java Photo Editor and Mathematica is enabled.

Junzo SATO

Medical Information Technology & Administration Planning, Kumamoto University Hospital

jsato@fc.kuh.kumamoto-u.ac.jp
http://fc.kuh.kumamoto-u.ac.jp/~jsato/

Nov 4th 2002
Nov 3rd 2002
Nov 1st 2002
Oct 24th 2002

Part One: Java Graphics

InstallJava

JLink is installed.

For Sun's j2sdk user on PC, setting CommandLine option should be required.

[Graphics:Images/JPEGuide_gr_1.gif]
[Graphics:Images/JPEGuide_gr_2.gif]
[Graphics:Images/JPEGuide_gr_3.gif]

<<JLinkProgramming`JavaSwing`

JavaSwing package is a wrapper for JFC Swing classes.

[Graphics:Images/JPEGuide_gr_4.gif]
[Graphics:Images/JPEGuide_gr_5.gif]
[Graphics:Images/JPEGuide_gr_6.gif]

Getting Java Image

[Graphics:Images/JPEGuide_gr_7.gif]

ImageIcon is created.

[Graphics:Images/JPEGuide_gr_8.gif]
[Graphics:Images/JPEGuide_gr_9.gif]

Java Image object is received through the ImageIcon.

[Graphics:Images/JPEGuide_gr_10.gif]
[Graphics:Images/JPEGuide_gr_11.gif]

The size of the image is retrieved.

[Graphics:Images/JPEGuide_gr_12.gif]
[Graphics:Images/JPEGuide_gr_13.gif]
[Graphics:Images/JPEGuide_gr_14.gif]
[Graphics:Images/JPEGuide_gr_15.gif]

<<JLinkProgramming`JavaPhotoEditor`

JavaPhotoEditor package is loaded.

[Graphics:Images/JPEGuide_gr_16.gif]
[Graphics:Images/JPEGuide_gr_17.gif]
[Graphics:Images/JPEGuide_gr_18.gif]
[Graphics:Images/JPEGuide_gr_19.gif]

Java Image To Graphics

Pixels in the Java image can be retrieved by using java.awt.image.PixelGrabber. A pixel in Java is defined by 4 byte integer in the form of ARGB. GrabPixels[ ] decompose the Java image into a matrix of {R,G,B}s.

[Graphics:Images/JPEGuide_gr_20.gif]
[Graphics:Images/JPEGuide_gr_21.gif]
[Graphics:Images/JPEGuide_gr_22.gif]
[Graphics:Images/JPEGuide_gr_23.gif]
[Graphics:Images/JPEGuide_gr_24.gif]

Matrix of colors GrabPixels[ ] has returned can be shown as Graphics object by converting it into a RasterArray[ ].

[Graphics:Images/JPEGuide_gr_25.gif]

[Graphics:Images/JPEGuide_gr_26.gif]

[Graphics:Images/JPEGuide_gr_27.gif]
[Graphics:Images/JPEGuide_gr_28.gif]
[Graphics:Images/JPEGuide_gr_29.gif]

Graphics To Java Image

Graphics object g constructed using RasterArray[ ] or Raster[ ] is converted to the Java Image by ToJavaImage[ ].

[Graphics:Images/JPEGuide_gr_30.gif]
[Graphics:Images/JPEGuide_gr_31.gif]

Then the Java Image ji is shown again as Graphics.

[Graphics:Images/JPEGuide_gr_32.gif]

[Graphics:Images/JPEGuide_gr_33.gif]

[Graphics:Images/JPEGuide_gr_34.gif]

A Graphics object is not always constructed by RasterArray/Raster. For example, Plot[ ] has graphics primitive(s) such as Line[ ](s.).

[Graphics:Images/JPEGuide_gr_35.gif]

[Graphics:Images/JPEGuide_gr_36.gif]

[Graphics:Images/JPEGuide_gr_37.gif]

In this example a Line[ ] is used to generate Graphics.

[Graphics:Images/JPEGuide_gr_38.gif]
[Graphics:Images/JPEGuide_gr_39.gif]

By putting a string "pl" to the ToJavaImage[ ], the Plot[ ] is converted to the Java Image.

[Graphics:Images/JPEGuide_gr_40.gif]
[Graphics:Images/JPEGuide_gr_41.gif]

The rasterized image data is shown by ShowJavaImage[ ].

[Graphics:Images/JPEGuide_gr_42.gif]

[Graphics:Images/JPEGuide_gr_43.gif]

[Graphics:Images/JPEGuide_gr_44.gif]

Part Two: Java Photo Editor

Creating JPEFrame

[Graphics:Images/JPEGuide_gr_45.gif]

The Java class JPEFrame is a main entry of the Java Photo Editor application. New instantce of JPEFrame is created. The default image in the resource of javaphotoeditor is painted at first.

[Graphics:Images/JPEGuide_gr_46.gif]
[Graphics:Images/JPEGuide_gr_47.gif]
[Graphics:Images/JPEGuide_gr_48.gif]
[Graphics:Images/JPEGuide_gr_49.gif]
[Graphics:Images/JPEGuide_gr_50.gif]

JPEFrame has a JPEPanel which is a subclass of JLink's class MathGraphicsJPanel.

[Graphics:Images/JPEGuide_gr_51.gif]
[Graphics:Images/JPEGuide_gr_52.gif]

The image drawn on the panel is stored inside JPEPanel. It is accessible from Mathematica by calling getSourceImage() method.

[Graphics:Images/JPEGuide_gr_53.gif]
[Graphics:Images/JPEGuide_gr_54.gif]

Loading Image

Loading Java Image

[Graphics:Images/JPEGuide_gr_55.gif]

The javaImage which is created by previous example is set to Java Photo Editor.

[Graphics:Images/JPEGuide_gr_56.gif]
[Graphics:Images/JPEGuide_gr_57.gif]

Loading Image From URL

[Graphics:Images/JPEGuide_gr_58.gif]

Loading image data on the web is possible by specifying URL.

[Graphics:Images/JPEGuide_gr_59.gif]
[Graphics:Images/JPEGuide_gr_60.gif]
[Graphics:Images/JPEGuide_gr_61.gif]
[Graphics:Images/JPEGuide_gr_62.gif]

Loading Image From File

[Graphics:Images/JPEGuide_gr_63.gif]

An image file on the local disk is possible by specifying File object.

[Graphics:Images/JPEGuide_gr_64.gif]
[Graphics:Images/JPEGuide_gr_65.gif]
[Graphics:Images/JPEGuide_gr_66.gif]
[Graphics:Images/JPEGuide_gr_67.gif]

Loading Image From Path

[Graphics:Images/JPEGuide_gr_68.gif]

An image is loaded by specifying full path of the file.

[Graphics:Images/JPEGuide_gr_69.gif]
[Graphics:Images/JPEGuide_gr_70.gif]

Using setMathCommand( )

command

A function which creates Graphics3D is defined.

[Graphics:Images/JPEGuide_gr_71.gif]

Showing graphics operation is defined as a string.

[Graphics:Images/JPEGuide_gr_72.gif]

Converting it to an expression and evaluation makes output graphics.

[Graphics:Images/JPEGuide_gr_73.gif]

[Graphics:Images/JPEGuide_gr_74.gif]

[Graphics:Images/JPEGuide_gr_75.gif]
setMathCommand( )

[Graphics:Images/JPEGuide_gr_76.gif]

JPEFrame is disposed.

[Graphics:Images/JPEGuide_gr_77.gif]

New JPEFrame whose panel has specified width and height is created. Note that 4 pixels on each side are added for insets used by JLink.

[Graphics:Images/JPEGuide_gr_78.gif]
[Graphics:Images/JPEGuide_gr_79.gif]

JPEPanel on the frame is received.

[Graphics:Images/JPEGuide_gr_80.gif]
[Graphics:Images/JPEGuide_gr_81.gif]

Panel size is shown.

[Graphics:Images/JPEGuide_gr_82.gif]
[Graphics:Images/JPEGuide_gr_83.gif]

The method setMathCommand[ ] evaluates command string by making it to an expression.

[Graphics:Images/JPEGuide_gr_84.gif]

Panel's preferred size is adjusted and the frame is shown.

[Graphics:Images/JPEGuide_gr_85.gif]
[Graphics:Images/JPEGuide_gr_86.gif]

Note that panel's size is changed after showing the frame.

[Graphics:Images/JPEGuide_gr_87.gif]
[Graphics:Images/JPEGuide_gr_88.gif]

Panel's preferred size is set. It has no insets of 4 pixels.

[Graphics:Images/JPEGuide_gr_89.gif]
[Graphics:Images/JPEGuide_gr_90.gif]

Menu Item "File/Load..."

Choosing a command "File/Load..." opens a dialog to specify image source by URL or File path.

[Graphics:Images/JPEGuide_gr_91.gif]

Load Image dialog is shown.

[Graphics:Images/JPEGuide_gr_92.gif]

The specified image is loaded to Java Photo Editor.

[Graphics:Images/JPEGuide_gr_93.gif]

Region Of Interest Bounds --- Marquee Rect

Painting some shapes and texts are done using painting tool. The region of interest bounds on the image can be selected by the marquee rect tool.

[Graphics:Images/JPEGuide_gr_94.gif]

[Graphics:Images/JPEGuide_gr_95.gif]

Rectangular area selected by the marquee tool is obtained.

[Graphics:Images/JPEGuide_gr_96.gif]
[Graphics:Images/JPEGuide_gr_97.gif]
[Graphics:Images/JPEGuide_gr_98.gif]
[Graphics:Images/JPEGuide_gr_99.gif]

[Graphics:Images/JPEGuide_gr_100.gif]

[Graphics:Images/JPEGuide_gr_101.gif]

Region Of Interest --- Area Selection

[Graphics:Images/JPEGuide_gr_102.gif]

Selected area, created by moving a mouse clockwise, is returned as Java's Polygon object.

[Graphics:Images/JPEGuide_gr_103.gif]
[Graphics:Images/JPEGuide_gr_104.gif]

Bounds of the selected area is returned.

[Graphics:Images/JPEGuide_gr_105.gif]
[Graphics:Images/JPEGuide_gr_106.gif]
[Graphics:Images/JPEGuide_gr_107.gif]
[Graphics:Images/JPEGuide_gr_108.gif]

[Graphics:Images/JPEGuide_gr_109.gif]

[Graphics:Images/JPEGuide_gr_110.gif]
[Graphics:Images/JPEGuide_gr_111.gif]
[Graphics:Images/JPEGuide_gr_112.gif]
[Graphics:Images/JPEGuide_gr_113.gif]

[Graphics:Images/JPEGuide_gr_114.gif]

[Graphics:Images/JPEGuide_gr_115.gif]
[Graphics:Images/JPEGuide_gr_116.gif]

[Graphics:Images/JPEGuide_gr_117.gif]

[Graphics:Images/JPEGuide_gr_118.gif]

Part Three: Digital Image Processing

ImageData

Digital Image Processing package is loaded.

[Graphics:Images/JPEGuide_gr_119.gif]

A sample image file is loaded by ImageRead[ ] function. ImageData object is returned.

[Graphics:Images/JPEGuide_gr_120.gif]
[Graphics:Images/JPEGuide_gr_121.gif]

The data is shown.

[Graphics:Images/JPEGuide_gr_122.gif]

[Graphics:Images/JPEGuide_gr_123.gif]

[Graphics:Images/JPEGuide_gr_124.gif]

Digital Image Processing package has many image filtering functions. Some of them is used.

[Graphics:Images/JPEGuide_gr_125.gif]
[Graphics:Images/JPEGuide_gr_126.gif]

[Graphics:Images/JPEGuide_gr_127.gif]

[Graphics:Images/JPEGuide_gr_128.gif]

BufferedImage

ImageProcessing`BufferedImage` has fine tuned functions for converting ImageData from/to Java BufferedImage. Java Photo Editor uses this function internally when the user wants to collaborate with it.

[Graphics:Images/JPEGuide_gr_129.gif]
[Graphics:Images/JPEGuide_gr_130.gif]
[Graphics:Images/JPEGuide_gr_131.gif]
[Graphics:Images/JPEGuide_gr_132.gif]
[Graphics:Images/JPEGuide_gr_133.gif]
[Graphics:Images/JPEGuide_gr_134.gif]

[Graphics:Images/JPEGuide_gr_135.gif]

[Graphics:Images/JPEGuide_gr_136.gif]

<<JLinkProgramming`JavaPhotoEditor`DIPSupportJ`

JavaImageToImageData

A package to make Java Photo Editor collaborate with Digital Image Processing package is loaded.

[Graphics:Images/JPEGuide_gr_137.gif]

Collaboration With Java Photo Editor

Marquee Rect

ImageData is created by JavaImageToImageData[ ] function.

[Graphics:Images/JPEGuide_gr_138.gif]
[Graphics:Images/JPEGuide_gr_139.gif]

The image previously clipped by the marquee rect tool is shown again through the ImageData.

[Graphics:Images/JPEGuide_gr_140.gif]

[Graphics:Images/JPEGuide_gr_141.gif]

[Graphics:Images/JPEGuide_gr_142.gif]

An image processing operation is applied to the ImageData.

[Graphics:Images/JPEGuide_gr_143.gif]
[Graphics:Images/JPEGuide_gr_144.gif]

The result ImageData is shown.

[Graphics:Images/JPEGuide_gr_145.gif]

[Graphics:Images/JPEGuide_gr_146.gif]

[Graphics:Images/JPEGuide_gr_147.gif]
Area Selection

The selected area by using area selection tool is now returned as an ImageData.

[Graphics:Images/JPEGuide_gr_148.gif]
[Graphics:Images/JPEGuide_gr_149.gif]

The same image is shown again through this ImageData.

[Graphics:Images/JPEGuide_gr_150.gif]

[Graphics:Images/JPEGuide_gr_151.gif]

[Graphics:Images/JPEGuide_gr_152.gif]

Function to apply filter operation to the region of interest is defined.

[Graphics:Images/JPEGuide_gr_153.gif]
[Graphics:Images/JPEGuide_gr_154.gif]

The Polygon[ ] poly derived from the Java Photo Editor is used as the region of interest in this case.

[Graphics:Images/JPEGuide_gr_155.gif]

The result ImageData is shown.

[Graphics:Images/JPEGuide_gr_156.gif]

[Graphics:Images/JPEGuide_gr_157.gif]

[Graphics:Images/JPEGuide_gr_158.gif]

ImageDataToJavaImage

[Graphics:Images/JPEGuide_gr_159.gif]

The filtered image data is converted to the Java Image and then drawn on the Java Photo Editor's panel.

[Graphics:Images/JPEGuide_gr_160.gif]

Part Four: Menu Commands

Menu Bar

The menu bar in the frame is received.

[Graphics:Images/JPEGuide_gr_161.gif]
[Graphics:Images/JPEGuide_gr_162.gif]

Number of menus currently added in the menu bar is shown.

[Graphics:Images/JPEGuide_gr_163.gif]
[Graphics:Images/JPEGuide_gr_164.gif]

Informations about menus are retrieved.

[Graphics:Images/JPEGuide_gr_165.gif]
[Graphics:Images/JPEGuide_gr_166.gif]
[Graphics:Images/JPEGuide_gr_167.gif]
[Graphics:Images/JPEGuide_gr_168.gif]

Adding New Menu

New menu is created and added to the menu bar.

[Graphics:Images/JPEGuide_gr_169.gif]
[Graphics:Images/JPEGuide_gr_170.gif]

Menu item is added to the menu.

[Graphics:Images/JPEGuide_gr_171.gif]
[Graphics:Images/JPEGuide_gr_172.gif]

Listener class for menu action is loaded.

[Graphics:Images/JPEGuide_gr_173.gif]
[Graphics:Images/JPEGuide_gr_174.gif]
[Graphics:Images/JPEGuide_gr_175.gif]
[Graphics:Images/JPEGuide_gr_176.gif]

A command handler is defined and is set to the listener.

[Graphics:Images/JPEGuide_gr_177.gif]
[Graphics:Images/JPEGuide_gr_178.gif]
[Graphics:Images/JPEGuide_gr_179.gif]

The listener itself is attached to the menu item.

[Graphics:Images/JPEGuide_gr_180.gif]

BEFORE USING ANY MENU COMMAND DEFINED IN MATHEMATICA, SHARING FRONTEND SHOULD BE STARTED.

[Graphics:Images/JPEGuide_gr_181.gif]
[Graphics:Images/JPEGuide_gr_182.gif]

Now clicking a menu item makes Mathematica print a string. Because this 'calculation' is done very fast, it might be impossible to see the change of frame's title.

[Graphics:Images/JPEGuide_gr_183.gif]
[Graphics:Images/JPEGuide_gr_184.gif]
[Graphics:Images/JPEGuide_gr_185.gif]
[Graphics:Images/JPEGuide_gr_186.gif]
[Graphics:Images/JPEGuide_gr_187.gif]
[Graphics:Images/JPEGuide_gr_188.gif]
[Graphics:Images/JPEGuide_gr_189.gif]
[Graphics:Images/JPEGuide_gr_190.gif]
[Graphics:Images/JPEGuide_gr_191.gif]
[Graphics:Images/JPEGuide_gr_192.gif]

The definition of the handlerItemFilter[ ] is changed.

[Graphics:Images/JPEGuide_gr_193.gif]

Clicking a menu item invokes this command handler.

[Graphics:Images/JPEGuide_gr_194.gif]
[Graphics:Images/JPEGuide_gr_195.gif]
[Graphics:Images/JPEGuide_gr_196.gif]
[Graphics:Images/JPEGuide_gr_197.gif]
[Graphics:Images/JPEGuide_gr_198.gif]
[Graphics:Images/JPEGuide_gr_199.gif]

Doing More With Menu

The definition of menu command handler is changed so that it receives the marquee rect and convert rectangular image to the ImageData to evaluate TryFilter[ ] function in newly created Notebook.

[Graphics:Images/JPEGuide_gr_200.gif]

After evaluation, new Notebook shows the result.

[Graphics:Images/JPEGuide_gr_201.gif]

[Graphics:Images/JPEGuide_gr_202.gif]

Quit

Sharing FrontEnd is halted.

[Graphics:Images/JPEGuide_gr_203.gif]

Installed link is closed.

[Graphics:Images/JPEGuide_gr_204.gif]
[Graphics:Images/JPEGuide_gr_205.gif]
[Graphics:Images/JPEGuide_gr_206.gif]


Converted by Mathematica      November 4, 2002