sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1804111 - in /sis/branches/JDK8/core/sis-raster/src: main/java/org/apache/sis/image/ test/java/org/apache/sis/image/
Date Fri, 04 Aug 2017 12:32:32 GMT
Author: desruisseaux
Date: Fri Aug  4 12:32:32 2017
New Revision: 1804111

URL: http://svn.apache.org/viewvc?rev=1804111&view=rev
Log:
Put WritablePixelIterator in public API.

Modified:
    sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/DefaultIterator.java
    sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/PixelIterator.java
    sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/WritablePixelIterator.java
    sis/branches/JDK8/core/sis-raster/src/test/java/org/apache/sis/image/PixelIteratorTest.java

Modified: sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/DefaultIterator.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/DefaultIterator.java?rev=1804111&r1=1804110&r2=1804111&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/DefaultIterator.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/DefaultIterator.java
[UTF-8] Fri Aug  4 12:32:32 2017
@@ -22,6 +22,8 @@ import java.awt.Rectangle;
 import java.awt.image.DataBuffer;
 import java.awt.image.Raster;
 import java.awt.image.RenderedImage;
+import java.awt.image.WritableRaster;
+import java.awt.image.WritableRenderedImage;
 import java.awt.image.RasterFormatException;
 import java.nio.Buffer;
 import java.nio.IntBuffer;
@@ -52,9 +54,9 @@ import org.apache.sis.util.ArgumentCheck
  *
  * @todo Change iteration order on tiles for using Hilbert iterator.
  */
-final class DefaultIterator extends PixelIterator {
+final class DefaultIterator extends WritablePixelIterator {
     /**
-     * Current tile coordinate of current raster.
+     * Tile coordinate of {@link #currentRaster}.
      */
     private int tileX, tileY;
 
@@ -77,13 +79,14 @@ final class DefaultIterator extends Pixe
     /**
      * Creates an iterator for the given region in the given raster.
      *
-     * @param  data     the raster which contains the sample values on which to iterate.
+     * @param  input    the raster which contains the sample values to read.
+     * @param  output   the raster where to write the sample values, or {@code null} for
read-only iterator.
      * @param  subArea  the raster region where to perform the iteration, or {@code null}
      *                  for iterating over all the raster domain.
      * @param  window   size of the window to use in {@link #createWindow(TransferType)}
method, or {@code null} if none.
      */
-    DefaultIterator(final Raster data, final Rectangle subArea, final Dimension window) {
-        super(data, subArea, window);
+    DefaultIterator(final Raster input, final WritableRaster output, final Rectangle subArea,
final Dimension window) {
+        super(input, output, subArea, window);
         currentLowerX = lowerX;
         currentUpperX = upperX;
         currentUpperY = upperY;
@@ -94,13 +97,14 @@ final class DefaultIterator extends Pixe
     /**
      * Creates an iterator for the given region in the given image.
      *
-     * @param  data     the image which contains the sample values on which to iterate.
+     * @param  input    the image which contains the sample values to read.
+     * @param  output   the image where to write the sample values, or {@code null} for read-only
iterator.
      * @param  subArea  the image region where to perform the iteration, or {@code null}
      *                  for iterating over all the image domain.
      * @param  window   size of the window to use in {@link #createWindow(TransferType)}
method, or {@code null} if none.
      */
-    DefaultIterator(final RenderedImage data, final Rectangle subArea, final Dimension window)
{
-        super(data, subArea, window);
+    DefaultIterator(final RenderedImage input, final WritableRenderedImage output, final
Rectangle subArea, final Dimension window) {
+        super(input, output, subArea, window);
         tileX = Math.decrementExact(tileLowerX);
         tileY = tileLowerY;
         currentLowerX = lowerX;
@@ -115,6 +119,7 @@ final class DefaultIterator extends Pixe
      */
     @Override
     public void rewind() {
+        close();                        // Release current writable raster, if any.
         if (image == null) {
             tileX = 0;
             tileY = 0;
@@ -179,6 +184,7 @@ final class DefaultIterator extends Pixe
             final int tx = Math.floorDiv(px - tileGridXOffset, tileWidth);
             final int ty = Math.floorDiv(py - tileGridYOffset, tileHeight);
             if (tx != tileX || ty != tileY) {
+                close();                                    // Release current writable raster,
if any.
                 tileX = tx;
                 tileY = ty;
                 fetchTile();
@@ -199,6 +205,7 @@ final class DefaultIterator extends Pixe
     public boolean next() {
         if (++x >= currentUpperX) {
             if (++y >= currentUpperY) {                     // Strict equality (==) would
work, but use >= as a safety.
+                close();                                    // Release current writable raster,
if any.
                 if (++tileX >= tileUpperX) {                // Strict equality (==) would
work, but use >= as a safety.
                     tileY = Math.incrementExact(tileY);     // 'incrementExact' because 'tileY
> tileUpperY' is allowed.
                     if (tileY >= tileUpperY) {
@@ -232,7 +239,16 @@ final class DefaultIterator extends Pixe
      * the {@link #y} field, but caller is responsible for updating the {@link #x} field.
      */
     private void fetchTile() {
-        currentRaster  = image.getTile(tileX, tileY);
+        currentRaster = null;
+        if (destination != null) {
+            destRaster = destination.getWritableTile(tileX, tileY);
+            if (destination == image) {
+                currentRaster = destRaster;
+            }
+        }
+        if (currentRaster == null) {
+            currentRaster = image.getTile(tileX, tileY);
+        }
         final int minX = currentRaster.getMinX();
         final int minY = currentRaster.getMinY();
         currentLowerX  = Math.max(lowerX, minX);
@@ -272,11 +288,38 @@ final class DefaultIterator extends Pixe
     }
 
     /**
+     * Writes a sample value in the specified band of current pixel.
+     * This method assumes that {@link #next()} or {@link #moveTo(int,int)} has been invoked.
+     */
+    @Override
+    public void setSample(final int band, final int value) {
+        destRaster.setSample(x, y, band, value);
+    }
+
+    /**
+     * Writes a sample value in the specified band of current pixel.
+     * This method assumes that {@link #next()} or {@link #moveTo(int,int)} has been invoked.
+     */
+    @Override
+    public void setSample(final int band, final float value) {
+        destRaster.setSample(x, y, band, value);
+    }
+
+    /**
+     * Writes a sample value in the specified band of current pixel.
+     * This method assumes that {@link #next()} or {@link #moveTo(int,int)} has been invoked.
+     */
+    @Override
+    public void setSample(final int band, final double value) {
+        destRaster.setSample(x, y, band, value);
+    }
+
+    /**
      * Returns the sample values of current pixel for all bands.
      * This method assumes that {@link #next()} or {@link #moveTo(int,int)} has been invoked.
      */
     @Override
-    public double[] getPixel​(double[] dest) {
+    public int[] getPixel​(int[] dest) {
         return currentRaster.getPixel(x, y, dest);
     }
 
@@ -294,11 +337,38 @@ final class DefaultIterator extends Pixe
      * This method assumes that {@link #next()} or {@link #moveTo(int,int)} has been invoked.
      */
     @Override
-    public int[] getPixel​(int[] dest) {
+    public double[] getPixel​(double[] dest) {
         return currentRaster.getPixel(x, y, dest);
     }
 
     /**
+     * Sets the sample values of current pixel for all bands.
+     * This method assumes that {@link #next()} or {@link #moveTo(int,int)} has been invoked.
+     */
+    @Override
+    public void setPixel​(int[] dest) {
+        destRaster.setPixel(x, y, dest);
+    }
+
+    /**
+     * Sets the sample values of current pixel for all bands.
+     * This method assumes that {@link #next()} or {@link #moveTo(int,int)} has been invoked.
+     */
+    @Override
+    public void setPixel​(float[] dest) {
+        destRaster.setPixel(x, y, dest);
+    }
+
+    /**
+     * Sets the sample values of current pixel for all bands.
+     * This method assumes that {@link #next()} or {@link #moveTo(int,int)} has been invoked.
+     */
+    @Override
+    public void setPixel​(double[] dest) {
+        destRaster.setPixel(x, y, dest);
+    }
+
+    /**
      * Returns a moving window over the sample values in a rectangular region starting at
iterator position.
      */
     @Override
@@ -540,4 +610,16 @@ final class DefaultIterator extends Pixe
             fullWidth  = (subWidth == windowWidth);
         }
     }
+
+    /**
+     * Releases the tiles acquired by this iterator, if any.
+     * This method does nothing if the iterator is read-only.
+     */
+    @Override
+    public void close() {
+        if (destination != null && destRaster != null) {
+            destRaster = null;
+            destination.releaseWritableTile(tileX, tileY);
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/PixelIterator.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/PixelIterator.java?rev=1804111&r1=1804110&r2=1804111&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/PixelIterator.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/PixelIterator.java
[UTF-8] Fri Aug  4 12:32:32 2017
@@ -64,6 +64,8 @@ public abstract class PixelIterator {
     /**
      * The current raster in which iteration is occurring. This may change when the iterator
      * reaches a new {@link #image} tile. May be {@code null} if not yet determined.
+     *
+     * @see RenderedImage#getTile(int, int)
      */
     Raster currentRaster;
 
@@ -238,7 +240,7 @@ public abstract class PixelIterator {
         // TODO: check here for cases that we can optimize (after we ported corresponding
implementations).
 
         if (order == null || order.equals(SequenceType.LINEAR)) {
-            return new DefaultIterator(data, subArea, window);
+            return new DefaultIterator(data, null, subArea, window);
         } else {
             throw new IllegalArgumentException(Errors.format(Errors.Keys.UnsupportedType_1,
order));
         }
@@ -270,7 +272,7 @@ public abstract class PixelIterator {
         // TODO: check here for cases that we can optimize (after we ported corresponding
implementations).
 
         if (order == null) {
-            return new DefaultIterator(data, subArea, window);
+            return new DefaultIterator(data, null, subArea, window);
         } else {
             throw new IllegalArgumentException(Errors.format(Errors.Keys.UnsupportedType_1,
order));
         }
@@ -412,9 +414,9 @@ public abstract class PixelIterator {
      * @param  dest  a pre-allocated array where to store the sample values, or {@code null}
if none.
      * @return the sample values for current pixel.
      *
-     * @see Raster#getPixel(int, int, double[])
+     * @see Raster#getPixel(int, int, int[])
      */
-    public abstract double[] getPixel​(double[] dest);
+    public abstract int[] getPixel​(int[] dest);
 
     /**
      * Returns the sample values of current pixel for all bands.
@@ -440,9 +442,9 @@ public abstract class PixelIterator {
      * @param  dest  a pre-allocated array where to store the sample values, or {@code null}
if none.
      * @return the sample values for current pixel.
      *
-     * @see Raster#getPixel(int, int, int[])
+     * @see Raster#getPixel(int, int, double[])
      */
-    public abstract int[] getPixel​(int[] dest);
+    public abstract double[] getPixel​(double[] dest);
 
     /**
      * Returns a moving window over the sample values in a rectangular region starting at
iterator position.

Modified: sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/WritablePixelIterator.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/WritablePixelIterator.java?rev=1804111&r1=1804110&r2=1804111&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/WritablePixelIterator.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-raster/src/main/java/org/apache/sis/image/WritablePixelIterator.java
[UTF-8] Fri Aug  4 12:32:32 2017
@@ -17,7 +17,7 @@
 package org.apache.sis.image;
 
 import java.io.Closeable;
-import java.io.IOException;
+import java.awt.Dimension;
 import java.awt.Rectangle;
 import java.awt.image.Raster;
 import java.awt.image.RasterFormatException;
@@ -28,7 +28,21 @@ import org.apache.sis.internal.raster.Re
 
 
 /**
- * A pixel iterator capable to write sample values.
+ * A pixel iterator capable to write sample values. This iterator can edit pixel values in
place,
+ * or write values in a different destination image than the source image. Source and destination
+ * images must use the same sample model,
+ *
+ * <p>Usage example:</p>
+ * {@preformat java
+ *     try (WritablePixelIterator it = WritablePixelIterator.create(image)) {
+ *         double[] samples = null;
+ *         while (it.next()) {
+ *             samples = it.getPixel(samples);      // Get values in all bands.
+ *             // Perform computation here...
+ *             it.setPixels(sample);                // Replace values in all bands.
+ *         }
+ *     }
+ * }
  *
  * @author  Rémi Maréchal (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
@@ -36,22 +50,51 @@ import org.apache.sis.internal.raster.Re
  * @since   0.8
  * @module
  */
-abstract class WritablePixelIterator extends PixelIterator implements Closeable {
-
-    private final WritableRaster wRaster;
+public abstract class WritablePixelIterator extends PixelIterator implements Closeable {
+    /**
+     * The image where pixels will be written, or {@code null} if the image is read-only.
+     * The destination image may or may not be the same instance than the source {@link #image}.
+     * However the sample model, the minimal X and Y values and the tile grid must be the
same.
+     */
+    final WritableRenderedImage destination;
 
-    private final WritableRenderedImage wRenderedImage;
+    /**
+     * The current tile where pixels will be written, or {@code null} if no write operation
is under way.
+     * It may or may not be the same instance than {@link #currentRaster}.
+     *
+     * @see WritableRenderedImage#getWritableTile(int, int)
+     * @see WritableRenderedImage#releaseWritableTile(int, int)
+     */
+    WritableRaster destRaster;
 
-    public WritablePixelIterator(WritableRaster raster, Rectangle subArea) {
-        super(raster, subArea, null);
-        wRaster        = raster;
-        wRenderedImage = null;
+    /**
+     * Creates an iterator for the given region in the given raster.
+     *
+     * @param  input    the raster which contains the sample values to read.
+     * @param  output   the raster where to write the sample values, or {@code null} for
read-only iterator.
+     * @param  subArea  the raster region where to perform the iteration, or {@code null}
+     *                  for iterating over all the raster domain.
+     * @param  window   size of the window to use in {@link #createWindow(TransferType)}
method, or {@code null} if none.
+     */
+    WritablePixelIterator(Raster input, WritableRaster output, Rectangle subArea, Dimension
window) {
+        super(input, subArea, window);
+        destRaster  = output;
+        destination = null;
     }
 
-    public WritablePixelIterator(WritableRenderedImage renderedImage, Rectangle subArea)
{
-        super(renderedImage, subArea, null);
-        wRaster        = null;
-        wRenderedImage = renderedImage;
+    /**
+     * Creates an iterator for the given region in the given image.
+     *
+     * @param  input    the image which contains the sample values to read.
+     * @param  output   the image where to write the sample values, or {@code null} for read-only
iterator.
+     * @param  subArea  the image region where to perform the iteration, or {@code null}
+     *                  for iterating over all the image domain.
+     * @param  window   size of the window to use in {@link #createWindow(TransferType)}
method, or {@code null} if none.
+     */
+    WritablePixelIterator(RenderedImage input, WritableRenderedImage output, Rectangle subArea,
Dimension window) {
+        super(input, subArea, window);
+        destRaster  = null;
+        destination = output;
     }
 
     /**
@@ -95,30 +138,90 @@ abstract class WritablePixelIterator ext
     }
 
     /**
-     * Write integer value at current iterator position.
+     * Writes a sample value in the specified band of current pixel.
+     * The {@link #next()} method must have returned {@code true}, or the {@link #moveTo(int,int)}
method must have
+     * been invoked successfully, before this {@code setSample(int, int)} method is invoked.
If above condition is
+     * not met, then this method behavior is undefined (there is no explicit bounds check
for performance reasons).
+     *
+     * @param  band   the band in which to set the sample value.
+     * @param  value  the sample value to write in the specified band.
      *
-     * @param value integer to write.
+     * @see WritableRaster#setSample(int, int, int, int)
+     * @see #getSample(int)
      */
-    public abstract void setSample(final int value);
+    public abstract void setSample(int band, int value);
 
     /**
-     * Write float value at current iterator position.
+     * Writes a sample value in the specified band of current pixel.
+     * The {@link #next()} method must have returned {@code true}, or the {@link #moveTo(int,int)}
method must have
+     * been invoked successfully, before this {@code setSample(int, float)} method is invoked.
If above condition is
+     * not met, then this method behavior is undefined (there is no explicit bounds check
for performance reasons).
      *
-     * @param value float to write.
+     * @param  band   the band in which to set the sample value.
+     * @param  value  the sample value to write in the specified band.
+     *
+     * @see WritableRaster#setSample(int, int, int, float)
+     * @see #getSampleFloat(int)
      */
-    public abstract void setSampleFloat(final float value);
+    public abstract void setSample(int band, float value);
 
     /**
-     * Write double value at current iterator position.
+     * Writes a sample value in the specified band of current pixel.
+     * The {@link #next()} method must have returned {@code true}, or the {@link #moveTo(int,int)}
method must have
+     * been invoked successfully, before this {@code setSample(int, double)} method is invoked.
If above condition is
+     * not met, then this method behavior is undefined (there is no explicit bounds check
for performance reasons).
+     *
+     * @param  band   the band in which to set the sample value.
+     * @param  value  the sample value to write in the specified band.
+     *
+     * @see WritableRaster#setSample(int, int, int, double)
+     * @see #getSampleDouble(int)
+     */
+    public abstract void setSample(int band, double value);
+
+    /**
+     * Sets the sample values of current pixel for all bands.
+     * The {@link #next()} method must have returned {@code true}, or the {@link #moveTo(int,int)}
method must have
+     * been invoked successfully, before this {@code setPixel(…)} method is invoked. If
above condition is not met,
+     * then this method behavior is undefined (there is no explicit bounds check for performance
reasons).
+     *
+     * @param  values  the new sample values for current pixel.
+     *
+     * @see WritableRaster#setPixel(int, int, int[])
+     * @see #getPixel(int[])
+     */
+    public abstract void setPixel​(int[] values);
+
+    /**
+     * Sets the sample values of current pixel for all bands.
+     * The {@link #next()} method must have returned {@code true}, or the {@link #moveTo(int,int)}
method must have
+     * been invoked successfully, before this {@code setPixel(…)} method is invoked. If
above condition is not met,
+     * then this method behavior is undefined (there is no explicit bounds check for performance
reasons).
+     *
+     * @param  values  the new sample values for current pixel.
+     *
+     * @see WritableRaster#setPixel(int, int, float[])
+     * @see #getPixel(float[])
+     */
+    public abstract void setPixel​(float[] values);
+
+    /**
+     * Sets the sample values of current pixel for all bands.
+     * The {@link #next()} method must have returned {@code true}, or the {@link #moveTo(int,int)}
method must have
+     * been invoked successfully, before this {@code setPixel(…)} method is invoked. If
above condition is not met,
+     * then this method behavior is undefined (there is no explicit bounds check for performance
reasons).
+     *
+     * @param  values  the new sample values for current pixel.
      *
-     * @param value double to write.
+     * @see WritableRaster#setPixel(int, int, double[])
+     * @see #getPixel(double[])
      */
-    public abstract void setSampleDouble(final double value);
+    public abstract void setPixel​(double[] values);
 
     /**
      * Releases any resources hold by this iterator.
      * Invoking this method may flush some tiles content to disk.
      */
     @Override
-    public abstract void close() throws IOException;
+    public abstract void close();
 }

Modified: sis/branches/JDK8/core/sis-raster/src/test/java/org/apache/sis/image/PixelIteratorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-raster/src/test/java/org/apache/sis/image/PixelIteratorTest.java?rev=1804111&r1=1804110&r2=1804111&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-raster/src/test/java/org/apache/sis/image/PixelIteratorTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-raster/src/test/java/org/apache/sis/image/PixelIteratorTest.java
[UTF-8] Fri Aug  4 12:32:32 2017
@@ -247,7 +247,7 @@ public strictfp class PixelIteratorTest
      * @param  subArea  the boundary of the raster sub-area where to perform iteration.
      */
     void createPixelIterator(WritableRaster raster, Rectangle subArea) {
-        iterator = new DefaultIterator(raster, subArea, null);
+        iterator = new DefaultIterator(raster, null, subArea, null);
         assertEquals("getIterationOrder()", SequenceType.LINEAR, iterator.getIterationOrder());
     }
 
@@ -262,7 +262,7 @@ public strictfp class PixelIteratorTest
      * @param  subArea  the boundary of the image sub-area where to perform iteration.
      */
     void createPixelIterator(WritableRenderedImage image, Rectangle subArea) {
-        iterator = new DefaultIterator(image, subArea, null);
+        iterator = new DefaultIterator(image, null, subArea, null);
     }
 
     /**
@@ -276,7 +276,7 @@ public strictfp class PixelIteratorTest
      * @param  window   size of the window to use in {@link PixelIterator#createWindow(TransferType)}
method.
      */
     void createWindowIterator(WritableRenderedImage image, Dimension window) {
-        iterator = new DefaultIterator(image, null, window);
+        iterator = new DefaultIterator(image, null, null, window);
     }
 
     /**



Mime
View raw message