This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits 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 1487eb854c Recompute the target aggregated image if a source changed.
1487eb854c is described below

commit 1487eb854ce64e322f807183d15be5584249c622
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Fri Nov 13 20:32:37 2026 +0100

    Recompute the target aggregated image if a source changed.
---
 .../main/org/apache/sis/image/AggregatedImage.java |  49 ++++++++-
 .../main/org/apache/sis/image/ImageProcessor.java  |   8 ++
 .../main/org/apache/sis/image/MonolithicImage.java | 112 +++++++++++++++++++++
 3 files changed, 166 insertions(+), 3 deletions(-)

diff --git 
a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/AggregatedImage.java
 
b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/AggregatedImage.java
index db2c1f8257..b082c22707 100644
--- 
a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/AggregatedImage.java
+++ 
b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/AggregatedImage.java
@@ -42,6 +42,7 @@ import org.apache.sis.image.internal.shared.ImageUtilities;
 import org.apache.sis.coverage.grid.PixelInCell;
 import org.apache.sis.pending.jdk.JDK18;
 import org.apache.sis.system.CommonExecutor;
+import org.apache.sis.util.Disposable;
 import org.apache.sis.util.Exceptions;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
@@ -76,7 +77,7 @@ import org.opengis.coverage.grid.SequenceType;
  *
  * @since SIS 1.8
  */
-public class AggregatedImage extends PlanarImage {
+public class AggregatedImage extends MonolithicImage implements Disposable {
     /**
      * The images containing the pixels to aggregate in this image.
      */
@@ -142,6 +143,17 @@ public class AggregatedImage extends PlanarImage {
      */
     private WritableRaster[] tiles;
 
+    /**
+     * Whether the tile content have been computed.
+     */
+    private boolean isComputed;
+
+    /**
+     * Whether listeners to writable images have been registered.
+     * This is done only when first needed and use weak references.
+     */
+    private boolean hasAddedListeners;
+
     /**
      * Whether parallel execution is allowed.
      * If {@code true}, may threads can be used for aggregating the pixel 
values.
@@ -378,7 +390,14 @@ public class AggregatedImage extends PlanarImage {
         checkTileIndex("tileX", minTileX, numXTiles, tileX);
         checkTileIndex("tileY", minTileY, numYTiles, tileY);
         final int chunkCellStride = effectiveNumBands * NUM_ACCUMULATORS;
-        if (tiles == null) try {
+        if (!isComputed) try {
+            if (tiles == null) {
+                tiles = new WritableRaster[Math.multiplyExact(numXTiles, 
numYTiles)];
+            }
+            if (!hasAddedListeners) {
+                hasAddedListeners = true;
+                addListeners(sources);
+            }
             // Execute the aggregation now, possibly using many threads.
             final double[][] data = new SourceScanner().call();
             /*
@@ -394,7 +413,7 @@ public class AggregatedImage extends PlanarImage {
                     getSampleModel(),
                     new Rectangle(minX, minY, width, height),
                     new Point(minTileX, minTileY),
-                    tiles = new WritableRaster[numXTiles * numYTiles]);
+                    tiles);
 
             for (int i=0; i<data.length; i++) {
                 final double[] tileData = data[i];
@@ -426,6 +445,7 @@ public class AggregatedImage extends PlanarImage {
                     }
                 }
             }
+            isComputed = true;
         } catch (ImagingOpException e) {
             throw e;
         } catch (Exception cause) {
@@ -675,4 +695,27 @@ public class AggregatedImage extends PlanarImage {
             return data;
         }
     }
+
+    /**
+     * Discards all tiles and marks this image as in need to be recomputed.
+     * This method is invoked automatically if any source image is an instance 
of
+     * {@link java.awt.image.WritableRenderedImage} and the content of that 
image changed.
+     */
+    @Override
+    protected synchronized void recompute() {
+        isComputed = false;
+    }
+
+    /**
+     * Advises this image that its tiles will no longer be requested.
+     * This method removes all tiles from the cache and stops observation
+     * of {@link java.awt.image.WritableRenderedImage} sources.
+     * This image should not be used anymore after this method call.
+     */
+    @Override
+    public synchronized void dispose() {
+        tiles = null;
+        isComputed = false;
+        removeListeners(sources);
+    }
 }
diff --git 
a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/ImageProcessor.java
 
b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/ImageProcessor.java
index 6b5ecad65d..6d84cabaed 100644
--- 
a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/ImageProcessor.java
+++ 
b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/ImageProcessor.java
@@ -1286,6 +1286,10 @@ public class ImageProcessor implements Cloneable {
      *   <li>{@linkplain #getExecutionMode() Execution mode} (parallel or 
sequential).</li>
      * </ul>
      *
+     * <p><b>Result relationship with source:</b>
+     * Changes in the source image will cause a recomputation of the returned 
image
+     * if the source image notifies {@linkplain java.awt.image.TileObserver 
tile observers}.</p>
+     *
      * @param  source      the image to be resampled.
      * @param  bounds      domain of pixel coordinates of the aggregated 
image, or {@code null} for automatic.
      * @param  fromSource  conversion of pixel corner coordinates from {@code 
source} image to aggregated image.
@@ -1316,6 +1320,10 @@ public class ImageProcessor implements Cloneable {
      *   <li>{@linkplain #getExecutionMode() Execution mode} (parallel or 
sequential).</li>
      * </ul>
      *
+     * <p><b>Result relationship with source:</b>
+     * Changes in any source image will cause a recomputation of the returned 
image
+     * if the source images notify {@linkplain java.awt.image.TileObserver 
tile observers}.</p>
+     *
      * @param  sources   the images to aggregate with their transforms from 
<em>source</em> to <em>new</em> image.
      * @param  bounds    domain of pixel coordinates of the aggregated image, 
or {@code null} for automatic.
      * @return aggregation of pixel values of all given source images.
diff --git 
a/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/MonolithicImage.java
 
b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/MonolithicImage.java
new file mode 100644
index 0000000000..b658a7a56e
--- /dev/null
+++ 
b/endorsed/src/org.apache.sis.feature/main/org/apache/sis/image/MonolithicImage.java
@@ -0,0 +1,112 @@
+/*
+ * 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.image;
+
+import java.lang.ref.WeakReference;
+import java.awt.image.TileObserver;
+import java.awt.image.RenderedImage;
+import java.awt.image.WritableRenderedImage;
+
+
+/**
+ * Base classes of images computed in a monolithic way.
+ * All tiles are computed immediately when a tile is requested.
+ * This behavior is of course not desired, which is why this base class is not 
public.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ */
+abstract class MonolithicImage extends PlanarImage {
+    /**
+     * Creates a new monolithic image.
+     */
+    MonolithicImage() {
+    }
+
+    /**
+     * The listener, or {@code null} if none.
+     */
+    private TileObserver listener;
+
+    /**
+     * Removes the listener from all images that are writable.
+     * This method should be invoked in a synchronized block.
+     *
+     * @param  sources  the source images for which to remove the listener.
+     */
+    final void removeListeners(final RenderedImage[] sources) {
+        if (listener != null) {
+            for (final RenderedImage source : sources) {
+                if (source instanceof WritableRenderedImage) {
+                    ((WritableRenderedImage) 
source).removeTileObserver(listener);
+                }
+            }
+            listener = null;
+        }
+    }
+
+    /**
+     * Adds a listener by weak reference to all images that are writable.
+     * This method should be invoked in a synchronized block.
+     *
+     * @param  sources  the source images for which to add a listener.
+     */
+    final void addListeners(final RenderedImage[] sources) {
+        for (final RenderedImage source : sources) {
+            if (source instanceof WritableRenderedImage) {
+                if (listener == null) {
+                    listener = new Listener(this);
+                }
+                ((WritableRenderedImage) source).addTileObserver(listener);
+            }
+        }
+    }
+
+    /**
+     * A listener using weak reference for notifying the enclosed image that a 
source changed.
+     */
+    private static final class Listener extends WeakReference<MonolithicImage> 
implements TileObserver {
+        /**
+         * Creates a new listener which will notify the given image.
+         *
+         * @param  target  the image to notify when a source changed.
+         */
+        Listener(final MonolithicImage target) {
+            super(target);
+        }
+
+        /**
+         * Invoked when a source image changed.
+         * The enclosed image is notified only after the tile calculation 
finished.
+         */
+        @Override
+        public void tileUpdate(WritableRenderedImage source, int tileX, int 
tileY, boolean willBeWritable) {
+            if (!willBeWritable) {
+                final MonolithicImage target = get();
+                if (target != null) {
+                    target.recompute();
+                } else {
+                    source.removeTileObserver(this);
+                }
+            }
+        }
+    }
+
+    /**
+     * Invoked when the image needs to be recomputed.
+     */
+    protected abstract void recompute();
+}

Reply via email to