You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2021/08/02 21:53:29 UTC

[sis] branch geoapi-4.0 updated: Add PackBits compression.

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 4db6a33  Add PackBits compression.
4db6a33 is described below

commit 4db6a331cdd2a7c65a362e33174b9267103a61a8
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Mon Aug 2 12:22:19 2021 +0200

    Add PackBits compression.
---
 .../org/apache/sis/internal/geotiff/Inflater.java  |  13 ++-
 .../sis/internal/geotiff/InflaterChannel.java      |  87 +++++++++++++++
 .../org/apache/sis/internal/geotiff/PackBits.java  | 122 +++++++++++++++++++++
 .../sis/storage/geotiff/CompressedSubset.java      |   2 +-
 4 files changed, 222 insertions(+), 2 deletions(-)

diff --git a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Inflater.java b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Inflater.java
index 1f9d588..17d7bb2 100644
--- a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Inflater.java
+++ b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/Inflater.java
@@ -115,26 +115,37 @@ public abstract class Inflater {
      * @param  compression        the compression method.
      * @param  input              the source of data to decompress.
      * @param  start              stream position where to start reading.
+     * @param  byteCount          number of bytes to read before decompression.
      * @param  elementsPerRow     number of elements (usually pixels) per row. Must be strictly positive.
      * @param  samplesPerElement  number of sample values per element (usually pixel). Must be strictly positive.
      * @param  skipAfterElements  number of sample values to skip between elements (pixels). May be empty or null.
      * @param  target             where to store sample values.
      * @return the inflater for the given targe type, or {@code null} if the compression method is unknown.
+     * @throws IOException if an I/O operation was required and failed.
      */
-    public static Inflater create(final Compression compression, final ChannelDataInput input, final long start,
+    public static Inflater create(final Compression compression,
+            final ChannelDataInput input, final long start, final long byteCount,
             final int elementsPerRow, final int samplesPerElement, final int[] skipAfterElements, final Buffer target)
+            throws IOException
     {
         ensureNonNull("input", input);
         ensureStrictlyPositive("elementsPerRow",    elementsPerRow);
         ensureStrictlyPositive("samplesPerElement", samplesPerElement);
+        final InflaterChannel inflated;
         switch (compression) {
             case NONE: {
                 return CopyFromBytes.create(input, start, elementsPerRow, samplesPerElement, skipAfterElements, target);
             }
+            case PACKBITS: {
+                inflated = new PackBits(input, start, byteCount);
+                break;
+            }
             default: {
                 return null;
             }
         }
+        return CopyFromBytes.create(inflated.createDataInput(),
+                0, elementsPerRow, samplesPerElement, skipAfterElements, target);
     }
 
     /**
diff --git a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/InflaterChannel.java b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/InflaterChannel.java
new file mode 100644
index 0000000..a49e549
--- /dev/null
+++ b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/InflaterChannel.java
@@ -0,0 +1,87 @@
+/*
+ * 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.geotiff;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.ReadableByteChannel;
+import org.apache.sis.internal.storage.io.ChannelDataInput;
+
+
+/**
+ * Inflater writing all values in a temporary buffer.
+ * This class does not need to care about subsampling.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.1
+ * @since   1.1
+ * @module
+ */
+abstract class InflaterChannel implements ReadableByteChannel {
+    /**
+     * Size of the buffer where to temporarily copy decompressed data.
+     */
+    private static final int BUFFER_SIZE = 4096;
+
+    /**
+     * The source of data to decompress.
+     */
+    protected final ChannelDataInput input;
+
+    /**
+     * Stream position after the last byte to read.
+     */
+    protected final long endPosition;
+
+    /**
+     * Creates a new channel which will decompress data from the given input.
+     *
+     * @param  input      the source of data to decompress.
+     * @param  start      stream position where to start reading.
+     * @param  byteCount  number of byte to read from the input.
+     * @throws IOException if the stream can not be seek to the given start position.
+     */
+    InflaterChannel(final ChannelDataInput input, final long start, final long byteCount) throws IOException {
+        this.input = input;
+        endPosition = Math.addExact(start, byteCount);
+        input.seek(start);
+    }
+
+    /**
+     * Creates the data input stream to use for getting uncompressed data.
+     * The {@linkplain #input} stream must be on the start position before to invoke this method.
+     */
+    final ChannelDataInput createDataInput() throws IOException {
+        return new ChannelDataInput(input.filename, this, ByteBuffer.allocate(InflaterChannel.BUFFER_SIZE), false);
+    }
+
+    /**
+     * Tells whether this channel is still open.
+     */
+    @Override
+    public final boolean isOpen() {
+        return input.channel.isOpen();
+    }
+
+    /**
+     * Do nothing. The {@linkplain #input} channel is not closed by this operation
+     * because it will typically be needed again for decompressing other tiles.
+     */
+    @Override
+    public final void close() {
+    }
+}
diff --git a/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/PackBits.java b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/PackBits.java
new file mode 100644
index 0000000..cf926bf
--- /dev/null
+++ b/storage/sis-geotiff/src/main/java/org/apache/sis/internal/geotiff/PackBits.java
@@ -0,0 +1,122 @@
+/*
+ * 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.geotiff;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.sis.internal.storage.io.ChannelDataInput;
+
+
+/**
+ * Inflater for values encoded with the "PackBits" compression.
+ * This compression is described in section 9 of TIFF 6 specification.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.1
+ * @since   1.1
+ * @module
+ */
+final class PackBits extends InflaterChannel {
+    /**
+     * Number of bytes to copy literally from the input.
+     * Only one of {@code literalCount} and {@link #duplicatedCount} can be non-zero.
+     */
+    private int literalCount;
+
+    /**
+     * Number of times to copy the {@link #duplicated} byte.
+     * Only one of {@link #literalCount} and {@code duplicatedCount} can be non-zero.
+     */
+    private int duplicatedCount;
+
+    /**
+     * Byte to copy many times. The number of copies is given by {@link #duplicatedCount}.
+     */
+    private byte duplicated;
+
+    /**
+     * Creates a new channel which will decompress data from the given input.
+     */
+    PackBits(final ChannelDataInput input, final long start, final long byteCount) throws IOException {
+        super(input, start, byteCount);
+    }
+
+    /**
+     * Decompresses some bytes from the {@linkplain #input} into the given destination buffer.
+     */
+    @Override
+    public int read(final ByteBuffer target) throws IOException {
+        final ByteBuffer source = input.buffer;
+        int total = 0;                              // Number of bytes that have been read.
+        while (target.hasRemaining()) {
+            /*
+             * If we stopped in the middle of a decompression during the previous call of this method,
+             * skip this `if` block for continuing where we were. Otherwise this block decodes the next
+             * byte, but without executing immediately:
+             *
+             *   - If -128, no operation.
+             *   - Otherwise if positive, copy the next n+1 bytes literally.
+             *   - Otherwise if negative, copy the next byte -n+1 times.
+             */
+            if ((literalCount | duplicatedCount) == 0) {
+                int n;
+                do {
+                    if (input.getStreamPosition() >= endPosition) {
+                        return (total != 0) ? total : -1;             // If no byte read, declare end-of-stream.
+                    }
+                    n = input.readByte();
+                } while (n == Byte.MIN_VALUE);
+                if (n >= 0) {
+                    literalCount = n + 1;
+                } else {
+                    duplicatedCount = 1 - n;
+                    duplicated = input.readByte();
+                }
+            }
+            /*
+             * Execute the instruction (literal copy or byte duplication) in the limit of space available
+             * in both buffers. If the instruction can not be executed fully, then one of `literalCount`
+             * or `duplicatedCount` fields will be non-zero.
+             */
+            int n;
+            if (literalCount != 0) {
+                n = Math.min(literalCount, target.remaining());
+                int r = source.remaining();
+                if (r == 0) {
+                    input.ensureBufferContains(Math.min(n, source.capacity()));
+                    r = source.remaining();
+                }
+                if (r <= n) {
+                    target.put(source);
+                    n = r;
+                } else {
+                    final int limit = source.limit();
+                    source.limit(source.position() + n);
+                    target.put(source);
+                    source.limit(limit);
+                }
+                literalCount -= n;
+            } else {
+                n = Math.min(duplicatedCount, target.remaining());
+                for (int i=0; i<n; i++) target.put(duplicated);
+                duplicatedCount -= n;
+            }
+            total += n;
+        }
+        return total;
+    }
+}
diff --git a/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/CompressedSubset.java b/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/CompressedSubset.java
index 07d1a04..bc9e31a 100644
--- a/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/CompressedSubset.java
+++ b/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/CompressedSubset.java
@@ -213,7 +213,7 @@ final class CompressedSubset extends DataSubset {
         final Buffer[] banks = new Buffer[numBanks];
         for (int b=0; b<numBanks; b++) {
             final Buffer   bank = RasterFactory.createBuffer(type, capacity);
-            final Inflater algo = Inflater.create(compression, reader().input, offsets[b],
+            final Inflater algo = Inflater.create(compression, reader().input, offsets[b], byteCounts[b],
                                     elementsPerRow, samplesPerElement, skipAfterElements, bank);
             if (algo == null) {
                 throw new DataStoreContentException(reader().resources().getString(