javax.media.jai
Class RecyclingTileFactory

java.lang.Object
  extended byjava.util.Observable
      extended byjavax.media.jai.RecyclingTileFactory
All Implemented Interfaces:
TileFactory, TileRecycler

public class RecyclingTileFactory
extends Observable
implements TileFactory, TileRecycler

A simple implementation of TileFactory wherein the tiles returned from createTile() attempt to re-use primitive arrays provided by the TileRecycler method recycleTile().

A simple example of the use of this class is as follows wherein image files are read, each image is filtered, and each output written to a file:

 String[] sourceFiles; // source file paths
 KernelJAI kernel; // filtering kernel

 // Create a RenderingHints object and set hints.
 RenderingHints rh = new RenderingHints(null);
 RecyclingTileFactory rtf = new RecyclingTileFactory();
 rh.put(JAI.KEY_TILE_RECYCLER, rtf);
 rh.put(JAI.KEY_TILE_FACTORY, rtf);
 rh.put(JAI.KEY_IMAGE_LAYOUT,
        new ImageLayout().setTileWidth(32).setTileHeight(32));

 int counter = 0;

 // Read each image, filter it, and save the output to a file.
 for(int i = 0; i < sourceFiles.length; i++) {
     PlanarImage source = JAI.create("fileload", sourceFiles[i]);
     ParameterBlock pb =
         (new ParameterBlock()).addSource(source).add(kernel);

     // The TileFactory hint will cause tiles to be created by 'rtf'.
     RenderedOp dest = JAI.create("convolve", pb, rh);
     String fileName = "image_"+(++counter)+".tif";
     JAI.create("filestore", dest, fileName);

     // The TileRecycler hint will cause arrays to be reused by 'rtf'.
     dest.dispose();
 }
 
In the above code, if the SampleModel of all source images is identical, then data arrays should only be created in the first iteration.

Since:
JAI 1.1.2

Constructor Summary
RecyclingTileFactory()
          Constructs a RecyclingTileFactory.
 
Method Summary
 boolean canReclaimMemory()
          Returns true.
 WritableRaster createTile(SampleModel sampleModel, Point location)
          Create a tile with the specified SampleModel and location, possibly using a DataBuffer constructed using a reclaimed data bank (array).
 void flush()
          Removes references to all internally cached data arrays, if any.
 long getMemoryUsed()
          Returns the amount of memory currently being consumed by data arrays cached within this object.
 boolean isMemoryCache()
          Returns true.
 void recycleTile(Raster tile)
          Recycle the given tile.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RecyclingTileFactory

public RecyclingTileFactory()
Constructs a RecyclingTileFactory.

Method Detail

canReclaimMemory

public boolean canReclaimMemory()
Returns true.

Specified by:
canReclaimMemory in interface TileFactory

isMemoryCache

public boolean isMemoryCache()
Returns true.

Specified by:
isMemoryCache in interface TileFactory

getMemoryUsed

public long getMemoryUsed()
Description copied from interface: TileFactory
Returns the amount of memory currently being consumed by data arrays cached within this object. If this object does not cache data arrays then this method will always return zero.

Specified by:
getMemoryUsed in interface TileFactory
Returns:
The amount of memory used by internally cached data arrays, measured in bytes.

flush

public void flush()
Description copied from interface: TileFactory
Removes references to all internally cached data arrays, if any. If such arrays are objects potentially subject to finalization, then this should make them available for garbage collection provided no references to them are held elsewhere.

Specified by:
flush in interface TileFactory

createTile

public WritableRaster createTile(SampleModel sampleModel,
                                 Point location)
Description copied from interface: TileFactory
Create a tile with the specified SampleModel and location, possibly using a DataBuffer constructed using a reclaimed data bank (array). If it is not possible to reclaim an array, a new one will be allocated so that a tile is guaranteed to be generated. If a reclaimed array is used to create the tile, its elements should be set to zero before the tile is returned to the caller.

Specified by:
createTile in interface TileFactory
Parameters:
sampleModel - The SampleModel to use in creating the WritableRaster.
location - The location (minX, minY) of the WritableRaster; if null, (0, 0) will be used.
Returns:
A WritableRaster which might have a DataBuffer created using a previously allocated array.

recycleTile

public void recycleTile(Raster tile)
Recycle the given tile.

Specified by:
recycleTile in interface TileRecycler
Parameters:
tile - A tile which mey be re-used either directly or by reclaiming its internal DataBuffer or primitive data array.