Class AbstractOpImpl
- All Implemented Interfaces:
ImageOp,Op<BufferedImage>
- Direct Known Subclasses:
AbstractTiledOpImpl,AbstractTileOpImpl
Image. ImageOp is the base class for all such
operations. The results of all operations are memoized (using a
memory-sensitive cache), so retrieving results is both fast and
memory-efficient.
Warning: For efficiency reasons, the methods getImage()
and getTile(java.awt.Point, VASSAL.tools.imageop.ImageOpObserver) do not return Images
defensively, nor do the Future<Image>s returned by
getFutureImage(VASSAL.tools.imageop.ImageOpObserver) and getFutureTile(java.awt.Point, VASSAL.tools.imageop.ImageOpObserver). That is, the
Image returned is possibly the one retained internally by
the ImageOp. Therefore, Images obtained from
an ImageOp must not be altered, as this might
interfere with image caching. If an Image obtained this way
needs to be modified, copy the Image first and alter the
copy.
- Since:
- 3.1.0
- Author:
- Joel Uckelman
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidabstract BufferedImageeval()Runs theOp.protected abstract voidfixSize()Submits a request for theBufferedImageproduced by this operation, and returns a reference to that request.abstract Future<BufferedImage>getFutureTile(int tileX, int tileY, ImageOpObserver obs) Submits a request for tile(tileX,tileY), and returns a reference to that request.getFutureTile(Point p, ImageOpObserver obs) Submits a request for tile(tileX,tileY), and returns a reference to that request.intReturns the height of theBufferedImagewhich would be returned byImageOp.getImage().getImage()Calculates theBufferedImageproduced by this operation.getImage(ImageOpObserver obs) Calculates theBufferedImageproduced by this operation, and reports completion or failure to the specifiedImageOpObserver.abstract intReturns the number of tiles along the x-axis.abstract intReturns the number of tiles along the y-axis.getSize()Returns the size of theBufferedImagewhich would be returned byImageOp.getImage().protected DimensionA utility method for retrieving the size of the computedImagefrom the cache if theImageis cached.abstract BufferedImagegetTile(int tileX, int tileY, ImageOpObserver obs) Calculates tile(tileX,tileY), and reports completion or failure to the specifiedImageOpObserver.getTile(Point p, ImageOpObserver obs) Calculates tile(p.x,p.y), and reports completion or failure to the specifiedImageOpObserver.abstract intReturns the standard height of theBufferedImagetiles which are returned byImageOp.getTile(java.awt.Point, VASSAL.tools.imageop.ImageOpObserver).abstract Point[]getTileIndices(Rectangle rect) Returns an array ofPoints representing the tiles intersecting the givenRectangle.abstract ImageOpgetTileOp(int tileX, int tileY) Returns anImageOpwhich can produce the requested tile.Returns anImageOpwhich can produce the requested tile.abstract DimensionReturns the standard size of theBufferedImagetiles which are returned byImageOp.getTile(java.awt.Point, VASSAL.tools.imageop.ImageOpObserver).abstract intReturns the standard width of theBufferedImagetiles which are returned byImageOp.getTile(java.awt.Point, VASSAL.tools.imageop.ImageOpObserver).intgetWidth()Returns the width of theBufferedImagewhich would be returned byImageOp.getImage().Methods inherited from class VASSAL.tools.opcache.AbstractOpImpl
get, get, getFuture, getSources, newKey, updateMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface VASSAL.tools.opcache.Op
getSources, newKey, update
-
Field Details
-
size
The cached size of this operation's resultingImage. -
cache
The cache which contains calculatedImages.
-
-
Constructor Details
-
AbstractOpImpl
public AbstractOpImpl()
-
-
Method Details
-
clearCache
public static void clearCache() -
eval
Runs theOp. This method should be called only by the caching framework.- Specified by:
evalin interfaceImageOp- Specified by:
evalin interfaceOp<BufferedImage>- Specified by:
evalin classAbstractOpImpl<BufferedImage>- Returns:
- the result of running this
Op - Throws:
Exception- The operation represented by thisImageOpcould be anything, so any exception may be thrown.
-
getImage
Calculates theBufferedImageproduced by this operation. Calls to this method are memoized to prevent redundant computations.Warning:
BufferedImages returned by this method must not be modified. -
getImage
public BufferedImage getImage(ImageOpObserver obs) throws CancellationException, InterruptedException, ExecutionException Calculates theBufferedImageproduced by this operation, and reports completion or failure to the specifiedImageOpObserver. Calls to this method are memoized to prevent redundant computations. If a non-nullobserver is given, then the operation may be done asynchronously. If the observer isnull, then this method will block on completion of the operation.When a non-blocking call is made (i.e., when
obs != null), the cache is checked and if the image is found, it is returned immediately. If the image is already being calculated,obsis notified when the pre-existing request completes. Otherwise, a new request is queued andobsis notified when that completes.When a blocking call is made (i.e., when
obs == null), the cache is checked and if the image is found, it is returned immediately. If the image is already being calculated, this method blocks on the completion of the existing calculation. Otherwise, a new calculation is started and this method blocks on it. In all cases, when a calculation is completed, the result is cached.Warning:
BufferedImages returned by this method must not be modified.- Specified by:
getImagein interfaceImageOp- Parameters:
obs- the observer to be notified on completion- Returns:
- the resulting
BufferedImage - Throws:
CancellationException- if the operation was cancelledInterruptedException- if the operation was interruptedExecutionException- if the operation failed- See Also:
-
getFutureImage
Submits a request for theBufferedImageproduced by this operation, and returns a reference to that request. If a non-nullobserver is given, then the operation may be done asynchronously. If the observer isnull, then this method will block on completion of the operation.This implementation uses a memory-sensitive cache to memoize calls to
getFutureImage. It returns aFuture<BufferedImage>so that the request may be cancelled if no longer needed.Futures are returned immediately, except in the case where the is no observer and no pre-existingFuturefor thisImageOp'sBufferedImage, in which case this method blocks on completion of the computation.Warning:
BufferedImages obtained from theFutures returned by this method must not be modified.- Specified by:
getFutureImagein interfaceImageOp- Parameters:
obs- the observer to be notified on completion- Returns:
- a
Futurefor the resultingBufferedImage - Throws:
ExecutionException- if the operation failed- See Also:
-
getSizeFromCache
A utility method for retrieving the size of the computedImagefrom the cache if theImageis cached.- Returns:
- the size of the cached
Image, ornullif theImageisn't cached
-
fixSize
protected abstract void fixSize() -
getSize
Returns the size of theBufferedImagewhich would be returned byImageOp.getImage(). The size is cached so that it need not be recalculated on each call. -
getWidth
public int getWidth()Returns the width of theBufferedImagewhich would be returned byImageOp.getImage(). The width is cached so that it need not be recalculated on each call. -
getHeight
public int getHeight()Returns the height of theBufferedImagewhich would be returned byImageOp.getImage(). The height is cached so that it need not be recalculated on each call. -
getTileSize
Returns the standard size of theBufferedImagetiles which are returned byImageOp.getTile(java.awt.Point, VASSAL.tools.imageop.ImageOpObserver). Tiles which are in the extreme right column will not have full width if theBufferedImagewidth is not an integral multiple of the tile width. Similarly, tiles in the bottom row will not have full height if theBufferedImageheight is not an integral multiple of the tile height.- Specified by:
getTileSizein interfaceImageOp- Returns:
- the size of
BufferedImagetiles in pixels - See Also:
-
getTileHeight
public abstract int getTileHeight()Returns the standard height of theBufferedImagetiles which are returned byImageOp.getTile(java.awt.Point, VASSAL.tools.imageop.ImageOpObserver).- Specified by:
getTileHeightin interfaceImageOp- Returns:
- the height of
BufferedImagetiles in pixels - See Also:
-
getTileWidth
public abstract int getTileWidth()Returns the standard width of theBufferedImagetiles which are returned byImageOp.getTile(java.awt.Point, VASSAL.tools.imageop.ImageOpObserver).- Specified by:
getTileWidthin interfaceImageOp- Returns:
- the width of
BufferedImagetiles in pixels - See Also:
-
getNumXTiles
public abstract int getNumXTiles()Returns the number of tiles along the x-axis. There will always be at least one column of tiles. The number of columns should equal(int) Math.ceil((double) getWidth() / getTileWidth()).- Specified by:
getNumXTilesin interfaceImageOp- Returns:
- the number of tiles along the x-axis
-
getNumYTiles
public abstract int getNumYTiles()Returns the number of tiles along the y-axis. There will always be at least one row of tiles. The number of rows should equal(int) Math.ceil((double) getHeight() / getTileHeight()).- Specified by:
getNumYTilesin interfaceImageOp- Returns:
- the number of tiles along the y-axis
-
getTile
public BufferedImage getTile(Point p, ImageOpObserver obs) throws CancellationException, InterruptedException, ExecutionException Calculates tile(p.x,p.y), and reports completion or failure to the specifiedImageOpObserver. If a non-nullobserver is given, then the operation may be done asynchronously. If the observer isnull, then this method will block on completion of the operation. Tiles are numbered from zero, so the tile in the upper-left corner of the mainBufferedImageis(0,0). Note thatp.xandp.yare indices into the tile array, not pixel locations.This convenience method is equivalent to
getTile(p.x, p.y, obs).Warning:
BufferedImages returned by this method must not be modified.- Specified by:
getTilein interfaceImageOp- Parameters:
p- the position of the requested tileobs- the observer- Returns:
- the resulting
BufferedImage - Throws:
CancellationException- if the operation was cancelledInterruptedException- if the operation was interruptedExecutionException- if the operation failed
-
getTile
public abstract BufferedImage getTile(int tileX, int tileY, ImageOpObserver obs) throws CancellationException, InterruptedException, ExecutionException Calculates tile(tileX,tileY), and reports completion or failure to the specifiedImageOpObserver. If a non-nullobserver is given, then the operation may be done asynchronously. If the observer isnull, then this method will block on completion of the operation. Tiles are numbered from zero, so the tile in the upper-left corner of the mainBufferedImageis(0,0). Note thattileXandtileYare indices into the tile array, not pixel locations.Warning:
BufferedImages returned by this method must not be modified.- Specified by:
getTilein interfaceImageOp- Parameters:
tileX- the x position of the requested tiletileY- the y position of the requested tileobs- the observer to be notified on completion- Returns:
- the resulting
BufferedImage - Throws:
CancellationException- if the operation was cancelledInterruptedException- if the operation was interruptedExecutionException- if the operation failed
-
getFutureTile
Submits a request for tile(tileX,tileY), and returns a reference to that request. If a non-nullobserver is given, then the operation may be done asynchronously. If the observer isnull, then this method will block on completion of the operation. Tiles are numbered from zero, so the tile in the upper-left corner of the mainBufferedImageis(0,0). Note thattileXandtileYare indices into the tile array, not pixel locations.This convenience method is equivalent to
getFutureTile(p.x, p.y, obs).Warning:
BufferedImages obtained from theFutures returned by this method must not be modified.- Specified by:
getFutureTilein interfaceImageOp- Parameters:
p- the position of the requested tileobs- the observer to be notified on completion- Returns:
- a
Futurefor the resultingBufferedImage - Throws:
ExecutionException- if the operation failed
-
getFutureTile
public abstract Future<BufferedImage> getFutureTile(int tileX, int tileY, ImageOpObserver obs) throws ExecutionException Submits a request for tile(tileX,tileY), and returns a reference to that request. If a non-nullobserver is given, then the operation may be done asynchronously. If the observer isnull, then this method will block on completion of the operation. Tiles are numbered from zero, so the tile in the upper-left corner of the mainBufferedImageis(0,0). Note thattileXandtileYare indices into the tile array, not pixel locations.Warning:
BufferedImages obtained from theFutures returned by this method must not be modified.- Specified by:
getFutureTilein interfaceImageOp- Parameters:
tileX- the x position of the requested tiletileY- the y position of the requested tileobs- the observer to be notified on completion- Returns:
- a
Futurefor the resultingBufferedImage - Throws:
ExecutionException- if the operation failed
-
getTileOp
Returns anImageOpwhich can produce the requested tile.This convenience method is equivalent to
getTileOp(p.x, p.y). -
getTileOp
Returns anImageOpwhich can produce the requested tile. -
getTileIndices
Returns an array ofPoints representing the tiles intersecting the givenRectangle.- Specified by:
getTileIndicesin interfaceImageOp- Parameters:
rect- the rectangle- Returns:
- the positions of the tiles hit by the rectangle
-