HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

If a picture is worth a thousand words, what words do you use when the picture is in the wrong format? Not exactly a Zen koan, but a valid question whose answer is JIMI, the Java Image Management Interface from Activated Intelligence.

JIMI is a toolkit for reading, writing, viewing and manipulating images in multiple-graphics file formats. JIMI supports an impressive number of image formats, but if your target format is not supported, JIMI's open design allows you to add your format while taking advantage of key JIMI features. As you'd hope and expect, JIMI is platform-neutral 100% Pure Java that works with Java l.0.x and 1.1.x, and with the current beta versions of Java 1.2.

This article will let you know if JIMI has the features that will lighten your load and tighten your code. The "Overview" section examines JIMI's major features and identifies how JIMI can fit into your project. The "Down to Business" section focuses on technical topics and code samples.

Overview
When you finish this overview, you'll understand JIMI's key features and be able to identify how JIMI can provide a strategic advantage to your projects and products.

JIMI Encodes and Decodes Images
Life gets pretty frustrating when you run into communication barriers. The unruly universe of the World Wide Web, with its varied platforms, browsers, languages and users, can make a programmer cry out for simplicity. Java simplifies programming across varied platforms and environments. JIMI delivers stunning simplicity to the Java programmer who needs to work with the following formats:
GIF
JPEG
Portable Network Graphics Format (PNG)
Adobe Photoshop (PSD)
Targa (TGA)
Windows Bitmap (BMP)
OS/2 Bitmap (BMP)
ICO and CUR
Macintosh PICT (PCT)
Tag Image File Format (TIFF)
X Bitmap (XBM)
X Pixmap (XPM)
Sun Rasterfile (RAS)
PCX

The following code snippet reads a Photoshop file and writes it back to disk as a PNG file. It couldn't be easier!

Image image = Jimi.getImage("myImage.PSD");
Jimi.putImage(image, "myImage.PNG");

See Listing 1 for a complete and equally simple application version of this code.

Activated Intelligence's customers are using JIMI's simple encode/decode features to add serious power to their applications. Put JIMI in your applets and applications to easily browse and view most of the images that come your way. Place JIMI in your File Upload Servlet and rest assured that it'll accept a wide range of image files for conversion to popular viewing formats. Expect the number of supported formats to grow with each release.

Image Manipulation with JIMI
You should now be confident that you can read, write and view most image formats. JIMI helps you manipulate these images simply and uniformly. Going far beyond the AWT, JIMI gives you power over your images with pixel-level access. With this feature you can write new and standard image manipulation routines. Activated packed JIMI with a few favorites, such as:
Scaling (does thumbnails)
Cropping
Color-reduction
Smoothing
Flipping
Edge detection
Embossing
Tiling
Oil painting
Blur
Gamma adjustment
Smoothing

If you choose to write a custom routine using the JIMI pixel access interface, it'll work on all the supported formats.

What Makes JIMI Special?
If reading, writing, viewing and manipulating multiple image formats across multiple platforms isn't enough for you, JIMI has more. It's the foundation for imaging within the Java environment. What makes Activated so confident in JIMI? JIMI knows Java! JIMI protects the Java programmer from some vexing conditions in the Java environment. JIMI is ready to work with the Java community and harness its power. Following are four of JIMI's special attributes:

  1. Big images, little JVM: How much memory will your Java application have at runtime? You may not know. JIMI has a fast, powerful and unique virtual memory management (VMM) system that lets you do more in your little JVM than you ever imagined. Using JIMI, a developer can load a 10000x10000 image in a JVM that is allocated only the standard 16 mb of heap space. JIMI uses memory only for the parts in use at that moment. How? JIMI leverages its fine-grained control over image access to implement a highly efficient virtual memory system tailored to the needs of large images. But the developer doesn't need to know the details to use the VMM. JIMI simply has the information you need buffered for fast access.
  2. Can I use JIMI on the server? Yes. JIMI can work independently of the AWT. Use JIMI in servlets, Enterprise Java-Beans or any other AWT-less server-based environment. When you have lots of image translation or manipulation to do, JIMI lets you move the processing from the client to the server.
  3. JIMI is extensible! Adding a new image format? No problem. JIMI's capabilities are dynamically extensible, allowing you to add new image formats. Each new format can seamlessly take advantage of all JIMI's core features.
    Because we designed JIMI to be easily and openly extended, Activated Intelligence is on the lookout for new image manipulation routines and image encoders/decoders. Developers can contact Activated Intelligence at www.activated.com to make arrangements for review and possible equitable use of their JIMI-based code.
  4. Flexible footprint: With Activated Intelligence and independent developers adding new features and formats to the JIMI system, JIMI is growing! But what if you want to use it where memory is a constraint or the code needs to be moved over a slow network? JIMI is a modular system that allows the formats and features to "Plug and Play."
Down to Business: Programming with JIMI
Now that you know what JIMI can do for you, the rest of this article will show you how to become a JIMI developer. When you finish reading, you should know how to program with JIMI.

JIMI is made up of several tightly integrated components that provide a powerful set of features in a modular and extensible architecture. Figure 1 illustrates JIMI's internals.

Figure 1
Figure 1:

The JIMI core provides the central functionality to support all other components. These classes handle the transparent AWT Image import/export, seamless VMM and other advanced imaging features. With a high-level understanding of the JIMI core, the developer can concentrate on the project.

The JIMI Image Read/Write Interface
The most fundamental use for JIMI is loading and saving images. JIMI's Image Read/Write Interface includes an intuitive front end with familiar and easy-to-use methods for image load-and-save operations. See how the "Jimi" class provides intuitive one-call methods for loading and saving images:

public static Image getImage(String filename);

public static Image putImage(Image image, String filename)
throws JimiException

Developers that have loaded images using the core Java API will be familiar with the getImage method. Jimi.getImage also provides asynchronous image loading, but with JIMI's added full range of format support, on-demand VMM and animated image support. Other convenient variations of getImage are provided, including a similar getImageProducer method used to apply filters or for use in an AWT-less environment. With these variations there's always a single method you can use to load an image to suit your needs.

Not surprisingly, the putImage methods are equally easy. Give JIMI the image (or ImageProducer), tell it where to save it and JIMI does the rest. Again, putImage has all the common variations to ensure that saving your image with JIMI is a one-call operation. Images saved will also automatically inherit any properties they were loaded with, such as compression scheme and interlacing options, even if you're saving to a different format.

With these methods in your toolbox you have everything you need to quickly add basic load/save support for a diverse range of formats to your programs. JIMI takes things a step further by also enabling you to dissect and reassemble image files containing several frames of image data, such as animated GIFs. This is achieved with two new interfaces: JimiReader and JimiWriter. For more on working with multi-image files, see Listing 2.

See how easily JIMI's features enhance your programs? To get the JIMI advantage, just change the one getImage line in your code. Add one call to JIMI's "putImage" and the job is done.

The Image Manipulation Interface
Now you can easily load and save images, but how about modifying them? For this, JIMI has full compatibility with the AWT's ImageFilter model of image manipulation as well as compatibility with new JDK features like Java 2D. A common image-processing operation is applying an ImageFilter.

With JIMI you can easily load an image, apply a filter and then save the image again. This code will crop a 100x100 region of an image and then save it:

ImageProducer p1=Jimi.getImageProducer("i.gif");
ImageFilter f=new CropImageFilter(0,0,100,100);
ImageProducer p2=new FilteredImageSource(f, p1);
Jimi.putImage(p2, "i.gif");

JIMI also provides more sophisticated image manipulation tools. Without JIMI, a developer who wants to access specific pieces of image data needs to use a PixelGrabber to get an image into an array of pixel data and then create a MemoryImageSource to build the modified image. This is expensive in both time and memory. JIMI provides random access to pixel data in its images. You have the power associated with an in-memory buffer and the flexibility that comes with all of JIMI's interfaces. Access your image in comfortable chunks and still benefit from JIMI's VMM capabilities.

Two additional key interfaces are JimiRasterImage and JimiBufferedRasterImage. The former provides a uniform interface to images created by JIMI regardless of their ColorModel type and form of primitive storage, letting you access their pixel data as RGB color values. The methods for this access are:


public void getRectangleRGB(int x, int y,
int w, int h, int[] buffer,
int offset, int scansize)

public void getRowRGB(int y, int[] buffer,
int offset)

public int getPixelRGB(int x, int y)

JimiBufferedRasterImage extends this set of methods to include symmetrical "setters" for changing pixel data. The Jimi class provides a one-call method for accessing these objects. Just replace "getImage" with "getRasterImage." JIMI also provides buffered access to these images with "getBufferedRasterImage" and the JimiBufferedRasterImage object. For more detail see Listing 3.

The Encoder/Decoder Interface
Now you can load, view, save and manipulate images for all JIMI's image formats, but what if your pet format isn't supported? Using JIMI's extension API you can add a new format module, leveraging all of JIMI's features and much of the work done for you. Implementing an encoder or decoder with JIMI gives you free image production, VMM support and seamless integration with the rest of JIMI.

Although greatly simplified, implementing format modules still deserves a more thorough guide than can be given in this article. Interested developers should take a look at www.activated.com/jimi/addformat/ for a step-by-step guide.

Conclusion
Now you know: JIMI's simple elegance lets you read, write, view and manipulate most images across multiple platforms in varied Java environments. You know when and where JIMI can help bring your product to release faster and with more features. You also know enough to get started writing powerful and effective imaging code using the JIMI toolkit. So go ahead, visit us at www.activated.com, download JIMI and "get the picture."

About the Authors
Luke Gorrie is a Java programmer and the lead developer of JIMI at Activated Intelligence. He can be reached at [email protected]

Michael Sick is a Java programmer, a Java advocate and the director of strategic development for Activated Intelligence. Contact him at [email protected]

	

Listing 1.
 
Basic image conversion with JIMI 

import com.activated.jimi.*; 
import java.awt.Image; 

public class JimiIsReallyEasy { 
 public static void main(String[] args) 
  throws JimiException { 
  Image image = Jimi.getImage("myImage.PSD"); 
  Jimi.putImage(image, "myImage.PNG"); 
  System.exit(0); 
 } 
} 

Listing 2.
 
Reading and writing multiple image files 

import com.activated.jimi.*; 

import java.awt.*; 
import java.util.*; 

/** 
 * A simple example program for reversing the 
 * order of frames in a multi-frame image. 
 */ 
public class ImageSeriesReverser 
{ 
 public static void main(String[] args) 
 { 
  // print usage if wrong arguments are given 
  if (args.length != 2) { 
   System.err.println("Requires args: <src> <dest>"); 
   System.exit(1); 
  } 

  // vector to store loaded images in 
  Vector images = new Vector(); 

  // read the images 
  try { 
   // create a JimiReader to read the image 
   // series from 
   JimiReader jr = Jimi.createJimiReader(args[0]); 
   // enumerate the images in the series 
   Enumeration e = jr.getImageEnumeration(); 
   // insert them into the vector in reverse 
   while (e.hasMoreElements()) { 
    Image i = (Image)e.nextElement(); 
    images.insertElementAt(e, 0); 
   } 
  } 
  // catch exception if the source file 
  // is malformed 
  catch (JimiException e) { 
   System.err.println("Error: " + e); 
   System.exit(1); 
  } 
  // write the images back in reverse 
  try { 
   // create a JimiWriter for the output file 
   JimiWriter jw = Jimi.createJimiWriter(args[1]); 
   // pull the images out of the vector and 
   // into an array 
   Image[] series = new Image[images.size()]; 
   images.copyInto(series); 
   // set the image array as the source for 
   // the JimiWriter 
   jw.setSource(series); 
   // write the images! 
   jw.putImage(args[1]); 
  } 
  // catch exception if the output file 
  // can't be written to 
  catch (JimiException e) { 
   System.err.println("Error: " + e); 
   System.exit(1); 
  } 
  // finished! 
  System.exit(0); 
 } 
} 

Listing 3. 
Image Processing example method 

public JimiBufferedRasterImage flipImage(JimiRasterImage source) 
 throws ImageAccessException 
{ 
 // the dimensions of the source image, which 
 // are used to create a same-sized target image 
 int width = source.getWidth(); 
 int height = source.getHeight(); 
 // create an empty target image to populate 
 // with flipped data 
 JimiBufferedRasterImage target = 
  Jimi.createBufferedRasterImage(width,height); 
 // a small buffer for pixel data to copy-between 
 int[] rowBuffer = new int[width]; 
 // the index of the last row of the image 
 int lastRow = height - 1; 
 // loop through, copying each row from source to destination 
 for (int row = 0; row < height; row++) { 
  // get the source row 
  source.getRowRGB(row, rowBuffer, 0); 
  // copy it into the destination image at the 
  // opposite location 

  target.setRowRGB(lastRow-row, rowBuffer, 0); 
 } 
 // all finished, the target image is now fully populated 
 return target; 
} 
  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.