The producer-consumer approach is conceptually sound but rather clumsy for straightforward image processing applications.
This deficiency has been addressed with the introduction of the Java2D Application Programming Interface (API) into Java. With the release of Java 2, Java2D has become one of the standard APIs provided with the language.
The Java2D API consists of a number of classes, distributed amongst the old packages java.awt and java.awt.image and six new packages:
With Java 1.2 and the Java2D classes, came the java.awt.image.BufferedImage, which is a subclass of Image. It provides a more open and structured internal design with much greater access to the image data.
BufferedImage DataBuffer ColorModel Raster SampleModel Composition of a BufferedImage object
BufferedImage includes a ColorModel object and a Raster object, which in turn contains instances of SampleModel and Databuffer.
A color model refers to how to the data describes the colors as they will appear in the display.
A common type of color model is RGB model. Java offers an RGB model where an alpha or transparency factor is included with the RGB values. One can also have grey scale models and index models where a data value holds an index into a palette of a fixed number of colors.
The raster refers to the data that specifies the colors of the individual pixels.
The Raster instance is actually composed of the raw data in a Databuffer and a SampleModel that details how the raw data values get translated into the color specifications for the model.
The constructor of BufferedImage takes image dimensions and image type as it parameters. There are 13 different standard image type, representing different combination of ColorModel and SampleModel.
* * Selected types of BufferedImage, * the most useful types for our purposes. 8-bit red, green and blue samples; packed into an int. TYPE_INT_RGB 8-bit blue, green and red samples; each stored in one byte. TYPE_3BYTE_BGR 16-bit sample for each pixel; stored in a short. TYPE_UNSHORT_GRAY 8-bit sample for each pixel; stored in a byte. TYPE_BYTE_GRAY 1-bit sample for each pixel; 8 samples packed into a byte. TYPE_BYTE_BINARY Description Constant
The dimension and type of an existing BufferedImage object can be acquired using:
Its colour can be retrieve with getColorModel().
Pixel values can be inspected or modify:
Method getRGB() and setRGB() provided by the BufferedImage class int getRGB(int x, int y); int getRGB(int x, int y, int w, int h, int data, int offset,int scansize); void setRGB(int x, int y, int value); void setRGB(int x, int y, int w, int h, int data, int offset,int scansize);
The pixel access method of Raster and WritableRaster have 2 broad classes:
Sample methods: operate on a particular sample of each pixel in a rectangular block.
int getPixel(int x, int y, int data) float getPixel(int x, int y, float data) double getPixel(int x, int y, double data) int getPixels(int x, int y, mt w, mt h, int data) float getPixels(int x, int y, int w, int h, float data) double getPixels(int x, int y, int w, int h, double data) int getSample(int x, int y, int band) float getSampleFloat(int x, int y, int band) double getSampleDouble(int x, int y, int band) int getSamples(int x, int y, int w, int h, int band, int data) float getsamples(int x, int y, int w, int h, int band, float data) double getSamples(int x, int y, int w, int h, int band, double data) Table 4.4 Methods provided by the Raster class to inspect pixel values.
void setPixel(int x, int y, int data) void setPixel(int x, int y, float data) void setPixel(int x, int y, double data) void setPixels(int x, int y, int w, int h, int data) void setPixels(int x, int y, int w, int h, float data) void setPixels(int x, int y, int w, int h, double data) void setSample(int x, int y, int band, int value) void setSample(int x, int y, int band, float value) void setSample(int x, int y, int band, double value) void setSample(int x, int y, int w, int h, int band, int data) void setSample(int x, int y, int w, int h, int band, float data) void setSample(int x, int y, int w, int h, int band, double data) Table 4.5 Methods provided by WritableRaster to modify pixel values.
The package com.sun.image.coded.jpeg contains classes to support the reading of images from, and writing images to, datastreams that have been compressed using JPEG compression.
Example: To load an image from file “in.jpg” FileInputStream fileStream = new FileInputStream(“in.jpg”); JPEGImageDecoder input=JPEGCodec.createJPEGDecoder(fileStream); BufferedImage image=input.decodeAsBufferedImage(); Example: Writing the image out to a file “out.jpg” FileOutputStream fileStream = new FileOutputStream(“out.jpg”); JPEGImageEncoder output=JPEGCodec.createJPEGEncoder(fileStream); output.encode(image);
The JAI, provides tools for "sophisticated, high performance image processing."
First developed in the late 1990s some of the JAI features are now available in J2SE5.0 (e.g. I/O for a wide range of image formats) but it still provides a large and robust set of imaging tools.
For example, many high end imaging tasks involve dealing with extremely large images. JAI allows for "tiling" , in which users can grab a section of an image for display and thus save lots of bandwidth.
JSky is used by astronomers to plan observations for the Gemini Telescope . The JSky home site describes how the program manages catalogs of imagery obtained in many formats and provides imaging tools to analyze individual images as well as to combine and compare multiple images.
JAI is used as part of the Scientist's Expert Assistant developed at NASA Goddard Space Flight Center in conjunction with the Space Telescope Science Institute. This is "a system that combines interactive visual and expert system approaches to assist astronomers in planning observations." See the papers and presentations describing the program.
GeoVirgil is a "planetary data analysis tool works as a map program (or Graphical Information System) for NASA planetary imagery. Developed by Steve McDonald, it "combines imagery, elevation and other data sets into an integrated map view." Various image processing functions are available. A 3D version is under development.
AstroVirgil is another Java program from Steve McDonald that is used to analyze data from the space-based Chandra X-ray telescope .
JadeDisplay (http://www.openchannelfoundation.org/projects/JadeDisplay/) is "a high-performance image display component for serious imaging applications using JAI. It works asynchronously, loading/computing image tiles in background threads. This frees up the GUI thread so the user doesn't have to wait for the entire image to load, and scrolling is fast regardless of image size (as long as it is tiled)."