sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1753822 - in /sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff: GeoTIFF.java GeoTiffStore.java ImageFileDirectory.java Reader.java
Date Fri, 22 Jul 2016 17:45:11 GMT
Author: desruisseaux
Date: Fri Jul 22 17:45:11 2016
New Revision: 1753822

URL: http://svn.apache.org/viewvc?rev=1753822&view=rev
Log:
Begin the port of GeoTIFF reader.

Added:
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTIFF.java
  (with props)
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/ImageFileDirectory.java
  (with props)
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
  (with props)
Modified:
    sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTiffStore.java

Added: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTIFF.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTIFF.java?rev=1753822&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTIFF.java
(added)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTIFF.java
[UTF-8] Fri Jul 22 17:45:11 2016
@@ -0,0 +1,39 @@
+/*
+ * 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.storage.geotiff;
+
+import java.io.Closeable;
+
+
+/**
+ * Base class of GeoTIFF image reader and writer.
+ *
+ * @author  Rémi Marechal (Geomatys)
+ * @author  Alexis Manin (Geomatys)
+ * @author  Johann Sorel (Geomatys)
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.8
+ * @version 0.8
+ * @module
+ */
+abstract class GeoTIFF implements Closeable {
+    /**
+     * For subclass constructors.
+     */
+    GeoTIFF() {
+    }
+}

Propchange: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTIFF.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTIFF.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTiffStore.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTiffStore.java?rev=1753822&r1=1753821&r2=1753822&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTiffStore.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/GeoTiffStore.java
[UTF-8] Fri Jul 22 17:45:11 2016
@@ -16,11 +16,15 @@
  */
 package org.apache.sis.storage.geotiff;
 
+import java.io.IOException;
 import org.opengis.metadata.Metadata;
+import org.apache.sis.internal.storage.ChannelDataInput;
 import org.apache.sis.storage.DataStore;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.StorageConnector;
+import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.Classes;
 
 
 /**
@@ -34,6 +38,11 @@ import org.apache.sis.util.ArgumentCheck
  */
 public class GeoTiffStore extends DataStore {
     /**
+     * The GeoTIFF reader implementation, or {@code null} if none.
+     */
+    private Reader reader;
+
+    /**
      * Creates a new GeoTIFF store from the given file, URL or stream object.
      * This constructor invokes {@link StorageConnector#closeAllExcept(Object)},
      * keeping open only the needed resource.
@@ -43,6 +52,17 @@ public class GeoTiffStore extends DataSt
      */
     public GeoTiffStore(final StorageConnector storage) throws DataStoreException {
         ArgumentChecks.ensureNonNull("storage", storage);
+        final ChannelDataInput input = storage.getStorageAs(ChannelDataInput.class);
+        if (input == null) {
+            throw new DataStoreException(Errors.format(Errors.Keys.IllegalInputTypeForReader_2,
+                    "TIFF", Classes.getClass(storage.getStorage())));
+        }
+        storage.closeAllExcept(input);
+        try {
+            reader = new Reader(input);
+        } catch (IOException e) {
+            throw new DataStoreException(e);
+        }
     }
 
     /**
@@ -65,5 +85,12 @@ public class GeoTiffStore extends DataSt
      */
     @Override
     public void close() throws DataStoreException {
+        final Reader r = reader;
+        reader = null;
+        if (r != null) try {
+            r.close();
+        } catch (IOException e) {
+            throw new DataStoreException(e);
+        }
     }
 }

Added: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/ImageFileDirectory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/ImageFileDirectory.java?rev=1753822&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/ImageFileDirectory.java
(added)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/ImageFileDirectory.java
[UTF-8] Fri Jul 22 17:45:11 2016
@@ -0,0 +1,59 @@
+/*
+ * 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.storage.geotiff;
+
+import java.io.IOException;
+
+
+/**
+ * An Image File Directory (FID) in a TIFF image.
+ *
+ * @author  Rémi Marechal (Geomatys)
+ * @author  Alexis Manin (Geomatys)
+ * @author  Johann Sorel (Geomatys)
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.8
+ * @version 0.8
+ * @module
+ */
+final class ImageFileDirectory {
+    /**
+     * The offset (in bytes) of the Image File Directory (FID).
+     * The directory may be at any location in the file after the header.
+     * In particular, an Image File Directory may follow the image that it describes.
+     */
+    final long offset;
+
+    /**
+     * The size of the image described by this FID.
+     */
+    private int imageWidth, imageHeight;
+
+    /**
+     * The size of each tile.
+     */
+    private int tileWidth, tileHeight;
+
+    private int samplesPerPixel;
+
+    /**
+     * Creates a new image file directory located at the given offset (in bytes) in the TIFF
file.
+     */
+    ImageFileDirectory(final Reader reader, long offset) throws IOException {
+        this.offset = offset;
+    }
+}

Propchange: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/ImageFileDirectory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/ImageFileDirectory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Added: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java?rev=1753822&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
(added)
+++ sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
[UTF-8] Fri Jul 22 17:45:11 2016
@@ -0,0 +1,152 @@
+/*
+ * 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.storage.geotiff;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.io.IOException;
+import java.nio.ByteOrder;
+import org.apache.sis.internal.storage.ChannelDataInput;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.util.resources.Errors;
+
+
+/**
+ * An image reader for GeoTIFF files. This reader duplicates the implementations performed
by other libraries, but we
+ * nevertheless provide our own reader in Apache SIS for better control on the process of
decoding geospatial metadata.
+ * We also measured better performance with this reader at least for uncompressed files,
and added support for some
+ * unusual data layout not supported by other libraries.
+ *
+ * <p>This image reader can also process <cite>Big TIFF</cite> images.</p>
+ *
+ * <p>The TIFF format specification version 6.0 (June 3, 1992) is available
+ * <a href="https://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf">here</a>.</p>
+ *
+ * @author  Rémi Marechal (Geomatys)
+ * @author  Alexis Manin (Geomatys)
+ * @author  Johann Sorel (Geomatys)
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.8
+ * @version 0.8
+ * @module
+ */
+final class Reader extends GeoTIFF {
+    /**
+     * The stream from which to read the data.
+     */
+    private final ChannelDataInput input;
+
+    /**
+     * Stream position of the first byte of the GeoTIFF file. This is usually zero.
+     */
+    private final long origin;
+
+    /**
+     * {@code true} if the file uses the BigTIFF format, or (@code false} for standard TIFF.
+     */
+    private final boolean isBigTIFF;
+
+    /**
+     * Positions of each <cite>Image File Directory</cite> (IFD) in this file.
Those positions are fetched
+     * when first needed.
+     */
+    private final List<ImageFileDirectory> imageFileDirectories = new ArrayList<>();
+
+    /**
+     * Creates a new GeoTIFF reader which will read data from the given input.
+     * The input must be at the beginning of the GeoTIFF file.
+     *
+     * @throws IOException if an error occurred while reading bytes from the stream.
+     * @throws DataStoreException if the file is not encoded in the TIFF or BigTIFF format.
+     */
+    Reader(final ChannelDataInput input) throws IOException, DataStoreException {
+        this.input = input;
+        origin = input.getStreamPosition();
+        /*
+         * A TIFF file begins with either "II" (0x4949) or "MM" (0x4D4D) characters.
+         * Those characters identify the byte order. Note we we do not need to care
+         * about the byte order for this flag since the two bytes shall have the same value.
+         */
+        final short order = input.readShort();
+        final boolean isBigEndian = (order == 0x4D4D);
+        if (isBigEndian || order == 0x4949) {
+            input.buffer.order(isBigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
+            /*
+             * Magic number of TIFF file is 42, followed by nothing else.
+             * The pointer type is implicitely the Java Integer type (4 bytes).
+             *
+             * Magic number of BigTIFF file is 43, followed by the pointer size.
+             * Currently, that pointer type canonly be the Java Long type (8 bytes),
+             * but a future BigTIFF version may allow 16 bytes wide pointers.
+             */
+            final short magicNumber = input.readShort();
+            if (isBigTIFF = (magicNumber == 43)) {
+                if (input.readShort() == Long.BYTES && input.readShort() == 0) {
+                    return;
+                }
+            } else if (magicNumber == 42) {
+                return;
+            }
+        }
+        throw new DataStoreException(Errors.format(Errors.Keys.UnexpectedFileFormat_2, "TIFF",
input.filename));
+    }
+
+    /**
+     * Reads the {@code int} or {@code long} value (depending if the file is
+     * a standard of big TIFF) at the current {@linkplain #input} position.
+     *
+     * @return The next pointer value.
+     */
+    private long readPointer() throws IOException {
+        return isBigTIFF ? input.readLong() : input.readUnsignedInt();
+    }
+
+    /**
+     * Reads the {@code short} or {@code long} value (depending if the file is
+     * standard of big TIFF) at the current {@linkplain #input} position.
+     *
+     * @return The next directory entry value.
+     */
+    final long readDirectoryEntry() throws IOException {
+        return isBigTIFF ? input.readLong() : input.readUnsignedShort();
+    }
+
+    /**
+     * Reads the next bytes in the {@linkplain #input}, which must be the 32 or 64 bits offset
to the
+     * next <cite>Image File Directory</cite> (IFD).
+     *
+     * @return {@code true} if we found a new IFD, or {@code false} if there is no more
IFD.
+     */
+    private boolean readImageFileDirectory() throws IOException {
+        final long offset = readPointer();
+        if (offset == 0) {
+            return false;
+        }
+        input.seek(offset);
+        return imageFileDirectories.add(new ImageFileDirectory(this, offset));
+    }
+
+    /**
+     * Closes this reader.
+     *
+     * @throws IOException if an error occurred while closing this reader.
+     */
+    @Override
+    public void close() throws IOException {
+        input.channel.close();
+    }
+}

Propchange: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/storage/sis-geotiff/src/main/java/org/apache/sis/storage/geotiff/Reader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8



Mime
View raw message