com.ngs.image.source
Class CompositeSource

java.lang.Object
  extended by com.ngs.image.ImageSource
      extended by com.ngs.image.source.DefaultThumbnailSource
          extended by com.ngs.image.source.CompositeSource
Direct Known Subclasses:
ScalingCompositeSource

public class CompositeSource
extends DefaultThumbnailSource

A CompositeSource allows for layering of discrete ImageSources into a single aggregate ImageSource.

The first layer of the CompositeSource becomes the destination for all subsequent layers to render to. Each layer is rendered to the output of all previous layers. Thus, the third layer is composited against the result of the fisrt and second layers. The fourt layer against the first, second, and third layers, etc.

In the event that you want to composite two multi-page sources that have differing page counts, if either the source or destination image passed to doComposite() is null, the opposing image is returned. This allows us to automate the process of setting the bottom layer as the destination, as well as simply returning the destination if a source is null. So long as ImageSources added as additional layers will return null when asked for a page they do not contain, rendering will simply skip that layer by returning the existing destination.

There is an opportunity for subclasses to compute an AffineTransform to use prior to compositing. For instance, if you want all of source images scaled to respect the bounds of the destination, override calculateTransform() and return an AffineTransform that will perform the function you require.

Author:
Bryan.Varner

Nested Class Summary
protected static class CompositeSource.Compositable
           
 
Field Summary
protected  AffineTransform identity
          An Identity AffineTransform.
protected  ArrayList<CompositeSource.Compositable> layers
           
 
Constructor Summary
CompositeSource()
          Creates a new CompositeSource with no sources to composite.
CompositeSource(ImageSource bottom)
          Creates a new CompositeSource with a specified ImageSource as the bottom layer (a.k.a.
 
Method Summary
 void addLayer(ImageSource layer)
          Adds the given ImageSource as the top layer, using a SRC_OVER AlphaComposite (the default for all Java2D Graphics2D renderings).
 void addLayer(ImageSource layer, Composite mode)
          Adds the given ImageSource as the top layer, using the specified Composite object to render.
protected  AffineTransform calculateTransform(BufferedImage src, BufferedImage dst)
          Hook function that allows subclasses to calculate AffineTransforms to be applied during the composite rendering.
 void dispose()
          Releases all cached resources, and invokes dispose() on all ImageSource layers.
protected  BufferedImage doComposite(BufferedImage src, BufferedImage dst, Composite mode, AffineTransform transform)
          Composite src into dst using mode and transform.
 BufferedImage getImage(int index)
          Renders and retrieves the image at the current index, by compositing all added layers with the specified Composite methods.
 int getImageCount()
          Returns the maximum number of images from any single Layer in this CompositeSource's stack.
 String getImageName()
          Implements ImageSource
 CompositeSource.Compositable getLayer(int layer)
           
 int getNumLayers()
           
 boolean isLayerVisible(int layer)
           
 void removeAllLayers()
          Removes all layers, and disposes of them.
 void removeLayer(int layer)
           
 void setLayerVisibility(int layer, boolean visible)
           
 
Methods inherited from class com.ngs.image.source.DefaultThumbnailSource
getThumbnail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

layers

protected ArrayList<CompositeSource.Compositable> layers

identity

protected AffineTransform identity
An Identity AffineTransform.

Constructor Detail

CompositeSource

public CompositeSource()
Creates a new CompositeSource with no sources to composite.


CompositeSource

public CompositeSource(ImageSource bottom)
Creates a new CompositeSource with a specified ImageSource as the bottom layer (a.k.a. The destination for all compositing)

Parameters:
bottom - The lowest level in the composite stack.
Method Detail

addLayer

public void addLayer(ImageSource layer,
                     Composite mode)
Adds the given ImageSource as the top layer, using the specified Composite object to render.

Parameters:
layer - The ImageSource to add.
mode - The Composite to use when rendering

addLayer

public void addLayer(ImageSource layer)
Adds the given ImageSource as the top layer, using a SRC_OVER AlphaComposite (the default for all Java2D Graphics2D renderings).

Parameters:
layer - The IMageSource to add

removeLayer

public void removeLayer(int layer)

removeAllLayers

public void removeAllLayers()
Removes all layers, and disposes of them.


getNumLayers

public int getNumLayers()

getLayer

public CompositeSource.Compositable getLayer(int layer)

setLayerVisibility

public void setLayerVisibility(int layer,
                               boolean visible)

isLayerVisible

public boolean isLayerVisible(int layer)

getImage

public BufferedImage getImage(int index)
Renders and retrieves the image at the current index, by compositing all added layers with the specified Composite methods.

Specified by:
getImage in class DefaultThumbnailSource
Parameters:
index - The index of the image in the ImageSource to composite.
Returns:
A BufferedImage composition of all layers in this CompositeSource.

calculateTransform

protected AffineTransform calculateTransform(BufferedImage src,
                                             BufferedImage dst)
Hook function that allows subclasses to calculate AffineTransforms to be applied during the composite rendering. This method is called immediately prior to the composite of the src and dst being rendered.

Parameters:
src - The source image about to be composited.
dst - The destination image about to be composited.
Returns:
An AffineTransform to use. This implementation returns identity

doComposite

protected BufferedImage doComposite(BufferedImage src,
                                    BufferedImage dst,
                                    Composite mode,
                                    AffineTransform transform)
Composite src into dst using mode and transform.

Parameters:
src - The source image to composite from
dst - The destination image to composite into
mode - The Composite mode to employ.
transform - An AffineTransform that should be applied to src as it is composited into dst.

getImageCount

public int getImageCount()
Returns the maximum number of images from any single Layer in this CompositeSource's stack.

Specified by:
getImageCount in class DefaultThumbnailSource
Returns:
The maximum number of images from any single layer.

dispose

public void dispose()
Releases all cached resources, and invokes dispose() on all ImageSource layers.

Specified by:
dispose in class DefaultThumbnailSource

getImageName

public String getImageName()
Description copied from class: DefaultThumbnailSource
Implements ImageSource

Specified by:
getImageName in class DefaultThumbnailSource