Bridges > Java Import Bridge > Accessing Grayscale Images Example

# Accessing Grayscale Images Example

This example creates a a grayscale ramp image within a Java class, which is contained in a file named GreyBandsImage.java. IDL then accesses this data through the ShowGreyImage routine, which is in the showgreyimage.pro file.

Note: These files are located in the resource/bridges/import/java/examples directory of the IDL distribution. Run this example procedure by entering window by entering .EDIT showgreyimage.pro.

The GreyBandsImage.java file contains the following text for creating a grayscale image in Java:

import java.awt.*;

import java.awt.image.*;

public class GreyBandsImage extends BufferedImage

{

// Members

private int m_height;

private int m_width;

//

// ctor

//

public GreyBandsImage()

{

super(100, 100, BufferedImage.TYPE_INT_ARGB);

generateImage();

m_height = 100;

m_width = 100;

}

//

// private method to generate the image

//

private void generateImage()

{

Color c;

int width = getWidth();

int height = getHeight();

WritableRaster raster = getRaster();

ColorModel model = getColorModel();

int BAND_PIXEL_WIDTH = 5;

int nBands = width/BAND_PIXEL_WIDTH;

int greyDelta = 255 / nBands;

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

{

c = new Color(i*greyDelta, i*greyDelta, i*greyDelta);

int argb = c.getRGB();

Object colorData = model.getDataElements(argb, null);

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

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

raster.setDataElements(j, (i*5)+k, colorData);

}

}

//

// mutators

//

public int[] getRawData()

{

Raster oRaster = getRaster();

Rectangle oBounds = oRaster.getBounds();

int[] data = new int[m_height * m_width * 4];

data = oRaster.getPixels(0,0,100,100, data);

return data;

}

public int getH()

{

return m_height;

}

public int getW()

{

return m_width;

}

}

The showgreyimage.pro file contains the following text for accessing the grayscale image within IDL:

PRO ShowGreyImage

; Construct the GreyBandImage in Java. This is a sub-class of BufferedImage.

; It is actually a 4 band image that happens to display bands in greyscale.

; It is 100x100 pixels.

oGrey = OBJ_NEW('IDLjavaObject$GreyBandsImage', 'GreyBandsImage')

; Get the 4 byte pixel values.

data = oGrey -> GetRawData()

; Get the height and width.

h = oGrey -> GetH()

w = oGrey -> GetW()

; Display the graphic in an IDL window

WINDOW, 0, XSIZE = 100, YSIZE = 100

TV, REBIN(data, h, w)

; Cleanup

OBJ_DESTROY, oGrey

END

After saving and compiling the above files (GreyBandsImage.java in Java and showgreyimage.pro in IDL), you can run the ShowGreyImage routine in IDL. The routine should produce the following image: