Bridges > Java Import Bridge > Accessing RGB Images Example

# Accessing RGB Images Example

This example imports an RGB (red, green, and blue) image from the IDL distribution into a Java class. The image is in the glowing_gas.jpg file, which is in the examples/data directory of the IDL distribution. The Java class also displays the image in a Java Swing user-interface. Then, the image is accessed into IDL and displayed with the new iImage tool.

Note: The Java and IDL code for this example is provided in the resource/bridges/import/java/examples directory, but the Java code has not been built as part of the jbexamples.jar file.

Note: This example uses functionality only available in Java 1.6 and later.

Note: Due to a Java bug, this example (and any other example using Swing on AWT) will not work on Linux platforms.

The first and main Java class is FrameTest, which creates the Java Swing application that imports the image from the glowing_gas.jpg file. Copy and paste the following text into a file, then save it as FrameTest.java:

import java.awt.*;

import java.awt.image.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.io.File;

public class FrameTest extends JFrame

{

RSIImageArea c_imgArea;

int m_xsize;

int m_ysize;

Box c_controlBox;

public FrameTest()

{

super("This is a JAVA Swing Program called from IDL");

// Dispose the frame when the sys close is hit

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

m_xsize = 350;

m_ysize = 371;

buildGUI();

}

public void buildGUI()

{

c_controlBox = Box.createVerticalBox();

JLabel l1 = new JLabel("Example Java/IDL Interaction");

JButton bLoadFile = new JButton("Load new file");

bLoadFile.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JFileChooser chooser = new JFileChooser(new

File("c:\\ITT\\IDL63\\EXAMPLES\\DATA"));

chooser.setDialogTitle("Enter a JPEG file");

if (chooser.showOpenDialog(FrameTest.this) == JFileChooser.APPROVE_OPTION)

{

java.io.File fname = chooser.getSelectedFile();

String filename = fname.getPath();

System.out.println(filename);

c_imgArea.setImageFile(filename);

}

}

});

JButton b1 = new JButton("Close this example");

b1.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

dispose();

}

});

c_imgArea = new

RSIImageArea("c:\\itt\\idl63\\examples\\data\\glowing_gas.jpg", new Dimension(m_xsize,m_ysize));

Box mainBox = Box.createVerticalBox();

Box rowBox = Box.createHorizontalBox();

rowBox.add(b1);

rowBox.add(bLoadFile);

c_controlBox.add(l1);

c_controlBox.add(rowBox);

mainBox.add(c_controlBox);

mainBox.add(c_imgArea);

getContentPane().add(mainBox);

pack(); setVisible(true);

c_imgArea.displayImage();

c_imgArea.addResizeListener(new RSIImageAreaResizeListener()

{

public void areaResized(int newx, int newy)

{

Dimension cdim = c_controlBox.getSize(null);

Insets i = getInsets();

newx = i.left + i.right + newx;

newy = i.top + cdim.height + newy + i.bottom;

setSize(new Dimension(newx, newy));

}

});

}

public void setImageData(int [] imgData, int xsize, int ysize)

{

MemoryImageSource ims = new MemoryImageSource(xsize, ysize, imgData, 0, ysize);

Image imgtmp = createImage(ims);

Graphics g = c_imgArea.getGraphics();

g.drawImage(imgtmp, 0, 0, null);

}

public void setImageData(byte [][][] imgData, int xsize, int ysize)

{

System.out.println("SIZE = "+xsize+"x"+ysize);

int newArray [] = new int[xsize*ysize];

int pixi = 0;

int curpix = 0;

short [] currgb = new short[3];

for (int i=0;i<m_xsize;i++)

{

for (int j=0;j<m_ysize;j++)

{

for (int k=0;k<3;k++)

{

currgb[k] = (short) imgData[k][i][j];

currgb[k] = (currgb[k] < 128) ? (short) currgb[k] : (short) (currgb[k]-256);

}

curpix = (int) currgb[0] * +

((int) currgb[1] * (int) Math.pow(2,8)) +

((int) currgb[2] * (int) Math.pow(2,16));

if (pixi % 1000 == 0) System.out.println("PIXI = "+pixi+" "+curpix);

newArray[pixi++] = curpix;

}

}

MemoryImageSource ims = new MemoryImageSource(xsize, ysize, newArray, 0, ysize);

c_imgArea.setImageObj(c_imgArea.createImage(ims));

}

public byte[][][] getImageData()

{

int width = 1;

int height = 1;

PixelGrabber pGrab;

width = m_xsize;

height = m_ysize;

// pixarray for the grab - 3D bytearray for display

int [] pixarray = new int[width*height];

byte [][][] bytearray = new byte[3][width][height];

// create a pixel grabber

pGrab = new PixelGrabber(c_imgArea.getImageObj(),0,0, width,height, pixarray, 0, width);

// grab the pixels from the image

try

{

boolean b = pGrab.grabPixels();

}

catch (InterruptedException e)

{

System.err.println("pixel grab interrupted");

return bytearray;

}

// break down the 32-bit integers from the grab into 8-bit bytes

// and fill the return 3D array

int pixi = 0;

int curpix = 0;

for (int j=0;j<m_ysize;j++)

{

for (int i=0;i<m_xsize;i++)

{

curpix = pixarray[pixi++];

bytearray[0][i][j] = (byte) ((curpix >> 16) & 0xff);

bytearray[1][i][j] = (byte) ((curpix >> 8) & 0xff);

bytearray[2][i][j] = (byte) ((curpix ) & 0xff);

}

}

return bytearray;

}

public static void main(String [] args)

{

FrameTest f = new FrameTest();

}

}

Note: The above text is for the FrameTest class that accesses the glowing_gas.jpg file in the examples/data directory of a default installation of IDL on a Windows system. The file’s location is specified as c:\\ITT\\IDL70\\EXAMPLES\\DATA in the above text. If the glowing_gas.jpg file is not in the same location on system, edit the text to change the location of this file to match your system.

The FrameTest class uses two other user-defined classes, RSIImageArea and RSIImageAreaResizeListener. These classes help to define the viewing area and display the image in Java. Copy and paste the following text into a file, then save it as RSIImageArea.java:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Vector;

import java.io.File;

public class RSIImageArea extends JComponent implements MouseMotionListener, MouseListener

{

Image c_img;

int m_boxw = 100;

int m_boxh = 100;

Dimension c_dim;

boolean m_pressed = false;

int m_button = 0;

Vector c_resizelisteners = null;

public RSIImageArea(String imgFile, Dimension dim)

{

c_img = getToolkit().getImage(imgFile);

c_dim = dim;

setPreferredSize(dim);

setSize(dim);

addMouseMotionListener(this);

addMouseListener(this);

}

public void addResizeListener(RSIImageAreaResizeListener l)

{

if (c_resizelisteners == null) c_resizelisteners = new Vector();

if (! c_resizelisteners.contains(l)) c_resizelisteners.add(l);

}

public void removeResizeListener(RSIImageAreaResizeListener l)

{

if (c_resizelisteners == null) return;

if (c_resizelisteners.contains(l)) c_resizelisteners.remove(l);

}

public void displayImage()

{

repaint();

}

public void paint(Graphics g)

{

int xsize = c_img.getWidth(null);

int ysize = c_img.getHeight(null);

if (xsize != -1 && ysize != -1)

{

if (xsize != c_dim.width || ysize != c_dim.height)

{

c_dim.width = xsize;

c_dim.height = ysize;

setPreferredSize(c_dim);

setSize(c_dim);

if (c_resizelisteners != null)

{

RSIImageAreaResizeListener l = null;

for (int j=0;j<c_resizelisteners.size();j++)

{

l = (RSIImageAreaResizeListener)

c_resizelisteners.elementAt(j);

l.areaResized(xsize, ysize);

}

}

}

}

g.drawImage(c_img, 0, 0, null);

}

public void setImageFile(String fileName)

{

c_img = null;

c_img = getToolkit().getImage(fileName);

repaint();

}

public Image getImageObj()

{

return c_img;

}

public void setImageObj(Image img)

{

c_img = img;

repaint();

}

public void drawZoomBox(MouseEvent e)

{

int bx = e.getX() - m_boxw/2;

bx = (bx >=0) ? bx :0;

int by = e.getY() - m_boxh/2;

by = (by >=0) ? by :0;

int ex = bx + m_boxw;

if (ex > c_dim.width)

{

ex = c_dim.width;

bx = c_dim.width-m_boxw;

}

int ey = by + m_boxh;

if (ey > c_dim.height)

{

ey = c_dim.height;

by = c_dim.height-m_boxh;

}

repaint();

Graphics g = getGraphics();

g.drawImage(c_img, bx, by, ex, ey, bx+(m_boxw/4), by+(m_boxh/4), ex-(m_boxw/4),ey-(m_boxh/4), null);

g.setColor(Color.white);

g.drawRect(bx, by, m_boxw, m_boxh);

}

public void mouseDragged(MouseEvent e)

{

drawZoomBox(e);

}

public void mouseMoved(MouseEvent e)

{

Graphics g = getGraphics();

if (m_pressed && (m_button == 1))

{

drawZoomBox(e);

g.setColor(Color.white);

g.drawString("DRAG", 10,10);

}

else

{

g.setColor(Color.white);

String s = "("+e.getX()+","+e.getY()+")";

repaint();

g.drawString(s, e.getX(), e.getY());

}

}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e)

{

m_pressed = true;

m_button = e.getButton();

repaint();

if (m_button == 1) drawZoomBox(e);

}

public void mouseReleased(MouseEvent e)

{

m_pressed = false;

m_button = 0;

}

}

And copy and paste the following text into a file, then save it as RSIImageAreaResizeListener.java:

public interface RSIImageAreaResizeListener

{

public void areaResized(int newx, int newy);

}

Compile these classes in Java. Then either update the jbexamples.jar file in the resource/bridges/import/java directory with the new compiled class, place the resulting compiled classes in your Java class path, or edit the JVM Classpath setting in the IDL-Java bridge configuration file to specify the location (path) of these compiled classes. See Location of the Bridge Configuration File for more information.

With the Java classes compiled, you can now access them in IDL. Copy and paste the following text into the IDL Editor window, then save it as ImageFromJava.pro:

PRO ImageFromJava

; Create a Swing Java object and have it load image data into IDL.

; Create the Java object first.

oJSwing = OBJ_NEW('IDLjavaObject$FrameTest', 'FrameTest')

; Get the image from the Java object.

image = oJSwing -> GetImageData()

PRINT, 'Loaded Image Information:'

HELP, image

; Delete the Java object. OBJ_DESTROY, oJSwing

; Interactively display the image.

IIMAGE, image

END

After compiling the above routine, you can run it in IDL. This routine produces the following Java Swing application.

Then, the routine produces the following iImage tool.

Note: After IDL starts the Java Swing application, the two displays are independent of each other. If a new image is loaded into the Java application, the IDL iImage tool is not updated. If the iImage tool modifies the existing image or opens a new image, the Java Swing application is not updated.