This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
new bd7b424 Rename CachedImage as ComputedImage and retrofit part of
ImageOperation in it.
bd7b424 is described below
commit bd7b42498065a0850f12cbec5c901ce211488a5b
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Wed Jan 1 00:04:38 2020 +0100
Rename CachedImage as ComputedImage and retrofit part of ImageOperation in
it.
---
.../j2d/{CachedImage.java => ComputedImage.java} | 106 ++++++++--
.../sis/internal/coverage/j2d/ImageOperation.java | 234 ---------------------
.../sis/internal/coverage/j2d/TileCache.java | 10 +-
3 files changed, 89 insertions(+), 261 deletions(-)
diff --git
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/CachedImage.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ComputedImage.java
similarity index 75%
rename from
core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/CachedImage.java
rename to
core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ComputedImage.java
index 8436b8e..8c371bf 100644
---
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/CachedImage.java
+++
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ComputedImage.java
@@ -18,6 +18,8 @@ package org.apache.sis.internal.coverage.j2d;
import java.util.Set;
import java.util.HashSet;
+import java.util.Arrays;
+import java.util.Vector;
import java.awt.Point;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
@@ -40,6 +42,15 @@ import org.apache.sis.util.Disposable;
* discarded at any time, in which case they will need to be recomputed
* when needed again.
*
+ * <p>Subclasses need to implement at least the following methods:</p>
+ * <ul>
+ * <li>{@link #getWidth()}</li>
+ * <li>{@link #getHeight()}</li>
+ * <li>{@link #getTileWidth()}</li>
+ * <li>{@link #getTileHeight()}</li>
+ * <li>{@link #computeTile(int,int)}</li>
+ * </ul>
+ *
* <p>This class is thread-safe. Multiple tiles may be computed in
* different background threads.</p>
*
@@ -48,24 +59,24 @@ import org.apache.sis.util.Disposable;
* @since 1.1
* @module
*/
-public abstract class CachedImage extends PlanarImage {
+public abstract class ComputedImage extends PlanarImage {
/**
* Weak reference to the enclosing image together with necessary
information for releasing resources
* when image is disposed. This class shall not contain any strong
reference to the enclosing image.
*/
// MUST be static
- private static final class Cleaner extends WeakReference<CachedImage>
implements Disposable {
+ private static final class Cleaner extends WeakReference<ComputedImage>
implements Disposable {
/**
* Indices of all cached tiles. Used for removing tiles from the cache
when the image is disposed.
* All accesses to this collection must be synchronized. This field
has to be declared here because
- * {@link Cleaner} is not allowed to keep a strong reference to the
enclosing {@link CachedImage}.
+ * {@link Cleaner} is not allowed to keep a strong reference to the
enclosing {@link ComputedImage}.
*/
private final Set<TileCache.Key> cachedTiles;
/**
* Creates a new weak reference to the given image.
*/
- Cleaner(final CachedImage image) {
+ Cleaner(final ComputedImage image) {
super(image, ReferenceQueueConsumer.QUEUE);
cachedTiles = new HashSet<>();
}
@@ -104,6 +115,13 @@ public abstract class CachedImage extends PlanarImage {
private final Cleaner reference;
/**
+ * The sources of this image, or {@code null} if unknown.
+ *
+ * @see #getSource(int)
+ */
+ private final RenderedImage[] sources;
+
+ /**
* The sample model shared by all tiles in this image.
* The {@linkplain SampleModel#getWidth() sample model width}
* determines this {@linkplain #getTileWidth() image tile width},
@@ -111,7 +129,7 @@ public abstract class CachedImage extends PlanarImage {
* determines this {@linkplain #getTileHeight() image tile height}.
*
* <div class="note"><b>Design note:</b>
- * {@code CachedImage} requires the sample model to have exactly the
desired tile size
+ * {@code ComputedImage} requires the sample model to have exactly the
desired tile size
* otherwise tiles created by {@link #createTile(int, int)} will consume
more memory
* than needed.</div>
*/
@@ -122,11 +140,21 @@ public abstract class CachedImage extends PlanarImage {
* The tile size will be the width and height of the given sample model.
*
* @param sampleModel the sample model shared by all tiles in this image.
+ * @param sources sources of this image (may be an empty array), or
a null array if unknown.
*/
- protected CachedImage(final SampleModel sampleModel) {
+ protected ComputedImage(final SampleModel sampleModel, RenderedImage...
sources) {
ArgumentChecks.ensureNonNull("sampleModel", sampleModel);
- reference = new Cleaner(this);
this.sampleModel = sampleModel;
+ if (sources != null) {
+ sources = sources.clone();
+ this.sources = sources;
+ for (int i=0; i<sources.length; i++) {
+ ArgumentChecks.ensureNonNullElement("sources", i, sources[i]);
+ }
+ } else {
+ this.sources = null;
+ }
+ reference = new Cleaner(this);
}
/**
@@ -140,31 +168,65 @@ public abstract class CachedImage extends PlanarImage {
* unless subclass override {@link #getMinX()}, {@link #getMinY()}, {@link
#getMinTileX()}
* and {@link #getMinTileY()}.</p>
*
- * @param image the image from which to get tile size.
+ * @param image the main image from which to get tile size.
+ * @param others additional sources, or {@code null} if none.
*/
- protected CachedImage(final RenderedImage image) {
+ protected ComputedImage(final RenderedImage image, final RenderedImage...
others) {
ArgumentChecks.ensureNonNull("image", image);
+ /*
+ * Get a sample model compatible with the given one, but with the tile
width and height.
+ * We check if the given sample model can be used as-is and create a
new one only if needed.
+ * This restriction about sample model size matching tile size is for
reducing the amount
+ * of memory consumed by {@link #createTile(int, int)}.
+ */
+ final int width = image.getTileWidth();
+ final int height = image.getTileHeight();
+ SampleModel sm = image.getSampleModel();
+ if (sm.getWidth() != width || sm.getHeight() != height) {
+ sm = sm.createCompatibleSampleModel(width, height);
+ }
+ sampleModel = sm;
+ if (others == null) {
+ sources = new RenderedImage[] {image};
+ } else {
+ sources = new RenderedImage[others.length + 1];
+ sources[0] = image;
+ System.arraycopy(others, 0, sources, 1, others.length);
+ for (int i=1; i<sources.length; i++) {
+ ArgumentChecks.ensureNonNullElement("others", i-1, sources[i]);
+ }
+ }
reference = new Cleaner(this);
- sampleModel = adapt(image.getSampleModel(), image.getTileWidth(),
image.getTileHeight());
}
/**
- * Returns a sample model compatible with the given one, but with the
specified width and height.
- * This method checks if the given sample model can be used as-is and
create a new one only if needed.
- * This restriction about sample model size matching tile size is for
reducing the amount of memory
- * consumed by {@link #createTile(int, int)}.
+ * Returns the source at the given index.
+ *
+ * @param index index of the desired source.
+ * @return source at the given index.
+ * @throws IndexOutOfBoundsException if the given index is out of bounds.
*/
- private static SampleModel adapt(SampleModel sampleModel, final int width,
final int height) {
- if (sampleModel.getWidth() != width || sampleModel.getHeight() !=
height) {
- sampleModel = sampleModel.createCompatibleSampleModel(width,
height);
- }
- return sampleModel;
+ protected final RenderedImage getSource(final int index) {
+ if (sources != null) return sources[index];
+ else throw new IndexOutOfBoundsException();
+ }
+
+ /**
+ * Returns the immediate sources of image data for this image (may be
{@code null}).
+ * This method returns all sources specified at construction time.
+ *
+ * @return the immediate sources, or an empty vector is none, or {@code
null} if unknown.
+ */
+ @Override
+ @SuppressWarnings("UseOfObsoleteCollectionType")
+ public Vector<RenderedImage> getSources() {
+ return (sources != null) ? new Vector<>(Arrays.asList(sources)) : null;
}
/**
* Returns the sample model associated with this image.
* All rasters returned from this image will have this sample model.
- * In {@code CachedImage} implementation, the sample model determines the
tile size
+ * In {@code ComputedImage} implementation, the sample model determines
the tile size
* (this is not necessarily true for all {@link RenderedImage}
implementations).
*
* @return the sample model of this image.
@@ -179,7 +241,7 @@ public abstract class CachedImage extends PlanarImage {
*
* <div class="note"><b>Note:</b>
* a raster can have a smaller width than its sample model, for example
when a raster is a view over a subregion
- * of another raster. But this is not recommended in the particular case
of this {@code CachedImage} class,
+ * of another raster. But this is not recommended in the particular case
of this {@code ComputedImage} class,
* because it would cause {@link #createTile(int, int)} to consume more
memory than necessary.</div>
*
* @return the width of this image in pixels.
@@ -194,7 +256,7 @@ public abstract class CachedImage extends PlanarImage {
*
* <div class="note"><b>Note:</b>
* a raster can have a smaller height than its sample model, for example
when a raster is a view over a subregion
- * of another raster. But this is not recommended in the particular case
of this {@code CachedImage} class,
+ * of another raster. But this is not recommended in the particular case
of this {@code ComputedImage} class,
* because it would cause {@link #createTile(int, int)} to consume more
memory than necessary.</div>
*
* @return the height of this image in pixels.
diff --git
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ImageOperation.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ImageOperation.java
deleted file mode 100644
index f46ccaf..0000000
---
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/ImageOperation.java
+++ /dev/null
@@ -1,234 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.sis.internal.coverage.j2d;
-
-import java.util.Arrays;
-import java.util.Vector;
-import java.awt.image.RenderedImage;
-import java.awt.image.SampleModel;
-import java.awt.image.ColorModel;
-import org.apache.sis.util.ArgumentChecks;
-
-
-/**
- * An image which is computed on-the-fly, usually from other images.
- * Computations are performed on a tile-by-tile basis and the result
- * is stored in a cache shared by all images on the platform.
- *
- * @todo Add an API providing operation parameters.
- *
- * <p>Subclasses need to implement the following methods:</p>
- * <ul>
- * <li>{@link #computeTile(int, int)}</li>
- * </ul>
- *
- * @author Martin Desruisseaux (Geomatys)
- * @version 1.1
- * @since 1.1
- * @module
- */
-public abstract class ImageOperation extends CachedImage {
- /**
- * The sources of this image. Never null and does not contain any null
element.
- */
- private final RenderedImage[] sources;
-
- /**
- * The color model associated to this image, or {@code null} if
unspecified.
- */
- private final ColorModel colorModel;
-
- /**
- * Image size in pixels.
- */
- private final int width, height;
-
- /**
- * Coordinate of the pixel in upper-left corner.
- */
- private final int minX, minY;
-
- /**
- * Coordinate of the tile in the upper-left corner.
- */
- private final int minTileX, minTileY;
-
- /**
- * Creates a new operation with a single image as the main source.
- * This {@code ImageOperation} will use the same color model, data type,
- * tile size, image size and minimum coordinates than the given image.
- *
- * @param image the main source of this operation.
- * @param others additional sources, or {@code null} if none.
- */
- protected ImageOperation(final RenderedImage image, final RenderedImage...
others) {
- super(image);
- if (others == null) {
- sources = new RenderedImage[] {image};
- } else {
- sources = new RenderedImage[others.length + 1];
- sources[0] = image;
- System.arraycopy(others, 0, sources, 1, others.length);
- for (int i=1; i<sources.length; i++) {
- ArgumentChecks.ensureNonNullElement("others", i-1, sources[i]);
- }
- }
- colorModel = image.getColorModel();
- width = image.getWidth();
- height = image.getHeight();
- minX = image.getMinX();
- minY = image.getMinY();
- minTileX = image.getMinTileX();
- minTileY = image.getMinTileY();
- }
-
- /**
- * Creates a new operation with an arbitrary amount of images as the
sources.
- * The tile size will be the width and height of the given sample model.
- *
- * @param sampleModel the sample model shared by all tiles in this image.
- * @param colorModel the color model for all rasters, or {@code null}
if unspecified.
- * @param width the image width in pixels, as a strictly positive
number.
- * @param height the image height in pixels, as a strictly positive
number.
- * @param minX <var>x</var> coordinate (column) of the pixel in
upper-left corner.
- * @param minY <var>y</var> coordinate (row) of the pixel in
upper-left corner.
- * @param minTileX <var>x</var> index of the tile in upper-left
corner.
- * @param minTileY <var>y</var> index of the tile in upper-left
corner.
- * @param sources all sources of this image. May be an empty array.
- */
- protected ImageOperation(final SampleModel sampleModel, final ColorModel
colorModel,
- final int width, final int height, final int
minX, final int minY,
- final int minTileX, final int minTileY,
RenderedImage... sources)
- {
- super(sampleModel);
- this.colorModel = colorModel;
- this.width = width;
- this.height = height;
- this.minX = minX;
- this.minY = minY;
- this.minTileX = minTileX;
- this.minTileY = minTileY;
- ArgumentChecks.ensureStrictlyPositive("width", width);
- ArgumentChecks.ensureStrictlyPositive("height", height);
- ArgumentChecks.ensureNonNull("sources", sources);
- sources = sources.clone();
- this.sources = sources;
- for (int i=0; i<sources.length; i++) {
- ArgumentChecks.ensureNonNullElement("sources", i, sources[i]);
- }
- }
-
- /**
- * Returns the source at the given index.
- *
- * @param index index of the desired source.
- * @return source at the given index.
- * @throws IndexOutOfBoundsException if the given index is out of bounds.
- */
- protected final RenderedImage getSource(final int index) {
- return sources[index];
- }
-
- /**
- * Returns the immediate sources of image data for this image.
- * This method returns the source specified at construction time.
- *
- * @return the immediate sources, or an empty vector is none.
- */
- @Override
- @SuppressWarnings("UseOfObsoleteCollectionType")
- public Vector<RenderedImage> getSources() {
- return new Vector<>(Arrays.asList(sources));
- }
-
- /**
- * Returns the color model associated with this image (may be null).
- * All rasters returned from this image will have this color model.
- *
- * @return the color model of this image, or {@code null} if unspecified.
- */
- @Override
- public ColorModel getColorModel() {
- return colorModel;
- }
-
- /**
- * Returns the width of this image in pixels.
- * This value is set at construction time.
- *
- * @return the width of this image.
- */
- @Override
- public int getWidth() {
- return width;
- }
-
- /**
- * Returns the height of this image in pixels.
- * This value is set at construction time.
- *
- * @return the height of this image.
- */
- @Override
- public int getHeight() {
- return height;
- }
-
- /**
- * Returns the minimum <var>x</var> coordinate (inclusive) of this image.
- * This value is set at construction time.
- *
- * @return the minimum <var>x</var> coordinate (column) of this image.
- */
- @Override
- public int getMinX() {
- return minX;
- }
-
- /**
- * Returns the minimum <var>y</var> coordinate (inclusive) of this image.
- * This value is set at construction time.
- *
- * @return the minimum <var>y</var> coordinate (row) of this image.
- */
- @Override
- public int getMinY() {
- return minY;
- }
-
- /**
- * Returns the minimum tile index in the <var>x</var> direction.
- * This value is set at construction time.
- *
- * @return the minimum tile index in the <var>x</var> direction.
- */
- @Override
- public int getMinTileX() {
- return minTileX;
- }
-
- /**
- * Returns the minimum tile index in the <var>y</var> direction.
- * This value is set at construction time.
- *
- * @return the minimum tile index in the <var>y</var> direction.
- */
- @Override
- public int getMinTileY() {
- return minTileY;
- }
-}
diff --git
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/TileCache.java
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/TileCache.java
index 3da725a..41cd10e 100644
---
a/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/TileCache.java
+++
b/core/sis-feature/src/main/java/org/apache/sis/internal/coverage/j2d/TileCache.java
@@ -23,7 +23,7 @@ import org.apache.sis.util.collection.Cache;
/**
- * A cache of tiles computed by {@link CachedImage}. A common cache is shared
by all images.
+ * A cache of tiles computed by {@link ComputedImage}. A common cache is
shared by all images.
* Tiles are kept by strong references until a memory usage limit is reached,
in which case
* the references of oldest tiles become soft references.
*
@@ -71,7 +71,7 @@ final class TileCache extends Cache<TileCache.Key, Raster> {
}
/**
- * A compound key identifying a tile of a {@link CachedImage}.
+ * A compound key identifying a tile of a {@link ComputedImage}.
*/
static final class Key {
/**
@@ -79,7 +79,7 @@ final class TileCache extends Cache<TileCache.Key, Raster> {
* for the same image will share the same reference. Consequently it
is okay to compare
* {@code image} fields directly instead of {@code image.get()}.
*/
- private final Reference<CachedImage> image;
+ private final Reference<ComputedImage> image;
/**
* Index of the tile owned by the image.
@@ -93,7 +93,7 @@ final class TileCache extends Cache<TileCache.Key, Raster> {
* @param tileX the column index of the cached tile.
* @param tileY the row index of the cached tile.
*/
- Key(final Reference<CachedImage> image, final int tileX, final int
tileY) {
+ Key(final Reference<ComputedImage> image, final int tileX, final int
tileY) {
this.image = image;
this.tileX = tileX;
this.tileY = tileY;
@@ -110,7 +110,7 @@ final class TileCache extends Cache<TileCache.Key, Raster> {
/**
* Returns a hash code value for this key. Note that this is okay to
use {@link #image} directly
* in hash code computation instead of {@link Reference#get()} because
we maintain a one-to-one
- * relationship between {@link CachedImage} and its {@link Reference}.
+ * relationship between {@link ComputedImage} and its {@link
Reference}.
*/
@Override
public int hashCode() {