You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2007/12/27 17:21:37 UTC

svn commit: r607082 - in /xmlgraphics/commons/trunk: src/java/org/apache/xmlgraphics/image/codec/tiff/ src/java/org/apache/xmlgraphics/image/loader/ src/java/org/apache/xmlgraphics/image/loader/impl/ src/java/org/apache/xmlgraphics/image/loader/impl/im...

Author: jeremias
Date: Thu Dec 27 08:21:37 2007
New Revision: 607082

URL: http://svn.apache.org/viewvc?rev=607082&view=rev
Log:
Bugfix in TIFF codec: detect last directory properly.
Support in TIFF preloader to signal the availability of additional/multiple (sub-)images.
Renamed ImageUtil.extractPageIndexFromURI() to needPageIndexFromURI() and added a getPageIndexFromURI() with slightly different semantics.

Added:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/SubImageNotFoundException.java   (with props)
Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFDirectory.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageInfo.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderTIFF.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageUtilTestCase.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFDirectory.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFDirectory.java?rev=607082&r1=607081&r2=607082&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFDirectory.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/codec/tiff/TIFFDirectory.java Thu Dec 27 08:21:37 2007
@@ -21,11 +21,11 @@
 
 import java.io.IOException;
 import java.io.Serializable;
-import java.util.Iterator;
+import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Map;
+import java.util.Iterator;
 import java.util.List;
-import java.util.ArrayList;
+import java.util.Map;
 
 import org.apache.xmlgraphics.image.codec.util.SeekableStream;
 
@@ -123,6 +123,10 @@
             stream.skip(12*entries);
 
             ifd_offset = readUnsignedInt(stream);
+        }
+        if (ifd_offset == 0L) {
+            throw new
+               IllegalArgumentException("TIFFDirectory3");
         }
 
         stream.seek(ifd_offset);

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageInfo.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageInfo.java?rev=607082&r1=607081&r2=607082&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageInfo.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageInfo.java Thu Dec 27 08:21:37 2007
@@ -36,6 +36,15 @@
      */
     public static final Object ORIGINAL_IMAGE = Image.class;
 
+    /**
+     * Key to register information about additional (sub-)images in the image file after the
+     * selected one. Valid values for this key is either a positive Integer or the constant
+     * {@link Boolean#TRUE} or {@link Boolean#FALSE}. A value of TRUE indicates that there are
+     * more subimages available but the exact number of additional images has not been determined
+     * for performance reasons.
+     */
+    public static final Object HAS_MORE_IMAGES = "HAS_MORE_IMAGES";
+
     /** Original URI the image was accessed with */
     private String originalURI;
     /** MIME type of the image */

Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/SubImageNotFoundException.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/SubImageNotFoundException.java?rev=607082&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/SubImageNotFoundException.java (added)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/SubImageNotFoundException.java Thu Dec 27 08:21:37 2007
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+
+/* $Id$ */
+ 
+package org.apache.xmlgraphics.image.loader;
+
+/**
+ * Exception which indicates that a particular (sub-)image could not be found.
+ */
+public class SubImageNotFoundException extends ImageException {
+
+    private static final long serialVersionUID = 3785613905389979249L;
+
+    /**
+     * Constructs an <code>ImageException</code> with the specified detail
+     * message. The error message string <code>s</code> can later be
+     * retrieved by the <code>{@link java.lang.Throwable#getMessage}</code>
+     * method of class <code>java.lang.Throwable</code>.
+     *
+     * @param   s   the detail message.
+     */
+    public SubImageNotFoundException(String s) {
+        super(s);
+    }
+
+    /**
+     * Constructs a new <code>ImageException</code> with the specified detail message and
+     * cause.  <p>Note that the detail message associated with
+     * <code>cause</code> is <i>not</i> automatically incorporated in
+     * this exception's detail message.
+     *
+     * @param  message the detail message (which is saved for later retrieval
+     *         by the {@link #getMessage()} method).
+     * @param  cause the cause (which is saved for later retrieval by the
+     *         {@link #getCause()} method).  (A <tt>null</tt> value is
+     *         permitted, and indicates that the cause is nonexistent or
+     *         unknown.)
+     */
+    public SubImageNotFoundException(String message, Throwable cause) {
+        super(message, cause);
+    }
+    
+}

Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/SubImageNotFoundException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/SubImageNotFoundException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderTIFF.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderTIFF.java?rev=607082&r1=607081&r2=607082&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderTIFF.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/PreloaderTIFF.java Thu Dec 27 08:21:37 2007
@@ -20,6 +20,7 @@
 package org.apache.xmlgraphics.image.loader.impl;
 
 import java.io.IOException;
+import java.text.MessageFormat;
 
 import javax.imageio.stream.ImageInputStream;
 import javax.xml.transform.Source;
@@ -32,6 +33,7 @@
 import org.apache.xmlgraphics.image.loader.ImageException;
 import org.apache.xmlgraphics.image.loader.ImageInfo;
 import org.apache.xmlgraphics.image.loader.ImageSize;
+import org.apache.xmlgraphics.image.loader.SubImageNotFoundException;
 import org.apache.xmlgraphics.image.loader.util.ImageUtil;
 import org.apache.xmlgraphics.image.loader.util.SeekableStreamAdapter;
 import org.apache.xmlgraphics.util.MimeConstants;
@@ -89,16 +91,16 @@
         ImageInfo info = null;
         in.mark();
         try {
-            int pageIndex = ImageUtil.extractPageIndexFromURI(uri);
+            int pageIndex = ImageUtil.needPageIndexFromURI(uri);
             
             SeekableStream seekable = new SeekableStreamAdapter(in);
             TIFFDirectory dir;
             try {
                 dir = new TIFFDirectory(seekable, pageIndex);
             } catch (IllegalArgumentException iae) {
-                //Fall back to page 0
-                pageIndex = 0;
-                dir = new TIFFDirectory(seekable, pageIndex);
+                String errorMessage = MessageFormat.format(
+                        "Subimage {0} does not exist.", new Object[] {new Integer(pageIndex)});
+                throw new SubImageNotFoundException(errorMessage);
             }
             int width = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_WIDTH);
             int height = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_LENGTH);
@@ -157,6 +159,13 @@
             }
             info.getCustomObjects().put("TIFF_STRIP_COUNT", new Integer(stripCount));
             
+            try {
+                //Check if there is a next page
+                new TIFFDirectory(seekable, pageIndex + 1);
+                info.getCustomObjects().put(ImageInfo.HAS_MORE_IMAGES, Boolean.TRUE);
+            } catch (IllegalArgumentException iae) {
+                info.getCustomObjects().put(ImageInfo.HAS_MORE_IMAGES, Boolean.FALSE);
+            }
         } finally {
             in.reset();
         }

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java?rev=607082&r1=607081&r2=607082&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java Thu Dec 27 08:21:37 2007
@@ -101,7 +101,7 @@
                     imgStream.mark();
                     ImageReadParam param = reader.getDefaultReadParam();
                     reader.setInput(imgStream, false, ignoreMetadata);
-                    final int pageIndex = ImageUtil.extractPageIndexFromURI(info.getOriginalURI());
+                    final int pageIndex = ImageUtil.needPageIndexFromURI(info.getOriginalURI());
                     try {
                         if (ImageFlavor.BUFFERED_IMAGE.equals(this.targetFlavor)) {
                             imageData = reader.read(pageIndex, param);

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java?rev=607082&r1=607081&r2=607082&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java Thu Dec 27 08:21:37 2007
@@ -296,12 +296,12 @@
      * 3.)
      * <p>
      * If no page index information is found in the URI or if the URI cannot be parsed, the
-     * method just returns 0 which indicates the first page.
+     * method returns null.
      * @param uri the URI that should be inspected
-     * @return the page index (0 is the first page)
+     * @return the page index (0 is the first page) or null if there's no page index information
+     *         in the URI
      */
-    public static int extractPageIndexFromURI(String uri) {
-        int pageIndex = 0;
+    public static Integer getPageIndexFromURI(String uri) {
         try {
             URI u = new URI(uri);
             String fragment = u.getFragment();
@@ -320,15 +320,38 @@
                         pos++;
                     }
                     if (sb.length() > 0) {
-                        pageIndex = Integer.parseInt(sb.toString()) - 1;
+                        int pageIndex = Integer.parseInt(sb.toString()) - 1;
                         pageIndex = Math.max(0, pageIndex);
+                        return new Integer(pageIndex);
                     }
                 }
             }
         } catch (URISyntaxException e) {
             //ignore
         }
-        return pageIndex;
+        return null;
+    }
+    
+    /**
+     * Extracts page index information from a URI. The expected pattern is "page=x" where x is
+     * a non-negative integer number. The page index must be specified as part of the URI fragment
+     * and is 1-based, i.e. the first page is 1 but the the method returns a zero-based page
+     * index.
+     * An example: <code>http://www.foo.bar/images/scan1.tif#page=4</code> (The method will return
+     * 3.)
+     * <p>
+     * If no page index information is found in the URI or if the URI cannot be parsed, the
+     * method just returns 0 which indicates the first page.
+     * @param uri the URI that should be inspected
+     * @return the page index (0 is the first page)
+     */
+    public static int needPageIndexFromURI(String uri) {
+        Integer res = getPageIndexFromURI(uri);
+        if (res != null) {
+            return res.intValue();
+        } else {
+            return 0;
+        }
     }
     
 }

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageUtilTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageUtilTestCase.java?rev=607082&r1=607081&r2=607082&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageUtilTestCase.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageUtilTestCase.java Thu Dec 27 08:21:37 2007
@@ -29,33 +29,47 @@
 public class ImageUtilTestCase extends TestCase {
 
     /**
-     * Tests {@link ImageUtil.extractPageIndexFromURI(String)}.
+     * Tests {@link ImageUtil.needPageIndexFromURI(String)}.
      * @throws Exception if an error occurs
      */
-    public void testPageIndexExtraction() throws Exception {
+    public void testNeedPageIndex() throws Exception {
         int pageIndex;
         
-        pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif");
+        pageIndex = ImageUtil.needPageIndexFromURI("http://localhost/images/scan1.tif");
         assertEquals(0, pageIndex);
-        pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=3");
+        pageIndex = ImageUtil.needPageIndexFromURI("http://localhost/images/scan1.tif#page=3");
         assertEquals(2, pageIndex);
-        pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=0");
+        pageIndex = ImageUtil.needPageIndexFromURI("http://localhost/images/scan1.tif#page=0");
         assertEquals(0, pageIndex);
-        pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=");
+        pageIndex = ImageUtil.needPageIndexFromURI("http://localhost/images/scan1.tif#page=");
         assertEquals(0, pageIndex);
-        pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=x");
+        pageIndex = ImageUtil.needPageIndexFromURI("http://localhost/images/scan1.tif#page=x");
         assertEquals(0, pageIndex);
-        pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=-1");
+        pageIndex = ImageUtil.needPageIndexFromURI("http://localhost/images/scan1.tif#page=-1");
         assertEquals(0, pageIndex);
-        pageIndex = ImageUtil.extractPageIndexFromURI("#page=2");
+        pageIndex = ImageUtil.needPageIndexFromURI("#page=2");
         assertEquals(1, pageIndex);
 
         //Not a valid URI
-        pageIndex = ImageUtil.extractPageIndexFromURI("C:\\images\\scan1.tif#page=44");
+        pageIndex = ImageUtil.needPageIndexFromURI("C:\\images\\scan1.tif#page=44");
         assertEquals(0, pageIndex);
         //Valid URI
-        pageIndex = ImageUtil.extractPageIndexFromURI("file:///C:/images/scan1.tif#page=44");
+        pageIndex = ImageUtil.needPageIndexFromURI("file:///C:/images/scan1.tif#page=44");
         assertEquals(43, pageIndex);
+    }
+    
+    /**
+     * Tests {@link ImageUtil.getPageIndexFromURI(String)}.
+     * @throws Exception if an error occurs
+     */
+    public void testGetPageIndex() throws Exception {
+        Integer pageIndex;
+        
+        pageIndex = ImageUtil.getPageIndexFromURI("http://localhost/images/scan1.tif");
+        assertNull(pageIndex);
+        pageIndex = ImageUtil.getPageIndexFromURI("http://localhost/images/scan1.tif#page=3");
+        assertEquals(2, pageIndex.intValue());
+        //Note: no detailed test anymore as this is tested through needPageIndexFromURI().
     }
     
 }



---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org