You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2007/12/18 09:42:42 UTC
svn commit: r605138 - in /xmlgraphics/fop/branches/Temp_ImagePackageRedesign:
src/java/org/apache/fop/image2/impl/
src/java/org/apache/fop/image2/impl/imageio/
src/java/org/apache/fop/image2/util/ test/java/org/apache/fop/image2/
test/resources/images/
Author: jeremias
Date: Tue Dec 18 00:42:41 2007
New Revision: 605138
URL: http://svn.apache.org/viewvc?rev=605138&view=rev
Log:
Support for specifying a particular page number of a multi-page image (such as TIFF).
Format: http://localhost/images/scan1.tif#page=3
Added:
xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/java/org/apache/fop/image2/ImageUtilTestCase.java (with props)
xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/resources/images/multi-page.tiff (with props)
Modified:
xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/PreloaderTIFF.java
xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/imageio/ImageLoaderImageIO.java
xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/util/ImageUtil.java
Modified: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/PreloaderTIFF.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/PreloaderTIFF.java?rev=605138&r1=605137&r2=605138&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/PreloaderTIFF.java (original)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/PreloaderTIFF.java Tue Dec 18 00:42:41 2007
@@ -90,8 +90,17 @@
ImageInfo info = null;
in.mark();
try {
+ int pageIndex = ImageUtil.extractPageIndexFromURI(uri);
+
SeekableStream seekable = new SeekableStreamAdapter(in);
- TIFFDirectory dir = new TIFFDirectory(seekable, 0);
+ TIFFDirectory dir;
+ try {
+ dir = new TIFFDirectory(seekable, pageIndex);
+ } catch (IllegalArgumentException iae) {
+ //Fall back to page 0
+ pageIndex = 0;
+ dir = new TIFFDirectory(seekable, pageIndex);
+ }
int width = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_WIDTH);
int height = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_LENGTH);
ImageSize size = new ImageSize();
Modified: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/imageio/ImageLoaderImageIO.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/imageio/ImageLoaderImageIO.java?rev=605138&r1=605137&r2=605138&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/imageio/ImageLoaderImageIO.java (original)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/impl/imageio/ImageLoaderImageIO.java Tue Dec 18 00:42:41 2007
@@ -101,7 +101,7 @@
imgStream.mark();
ImageReadParam param = reader.getDefaultReadParam();
reader.setInput(imgStream, false, ignoreMetadata);
- final int pageIndex = 0; //Always the first page at the moment
+ final int pageIndex = ImageUtil.extractPageIndexFromURI(info.getOriginalURI());
try {
if (ImageFlavor.BUFFERED_IMAGE.equals(this.targetFlavor)) {
imageData = reader.read(pageIndex, param);
@@ -112,6 +112,9 @@
iiometa = reader.getImageMetadata(pageIndex);
}
break; //Quit early, we have the image
+ } catch (IndexOutOfBoundsException indexe) {
+ throw new ImageException("Page does not exist. Invalid image index: "
+ + pageIndex);
} catch (IIOException iioe) {
if (firstException == null) {
firstException = iioe;
Modified: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/util/ImageUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/util/ImageUtil.java?rev=605138&r1=605137&r2=605138&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/util/ImageUtil.java (original)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/src/java/org/apache/fop/image2/util/ImageUtil.java Tue Dec 18 00:42:41 2007
@@ -24,6 +24,8 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Map;
import java.util.zip.GZIPInputStream;
@@ -281,6 +283,52 @@
hints.put(ImageProcessingHints.TARGET_RESOLUTION,
new Float(session.getTargetResolution()));
return hints;
+ }
+
+ private static final String PAGE_INDICATOR = "page=";
+
+ /**
+ * 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 extractPageIndexFromURI(String uri) {
+ int pageIndex = 0;
+ try {
+ URI u = new URI(uri);
+ String fragment = u.getFragment();
+ if (fragment != null) {
+ int pos = fragment.indexOf(PAGE_INDICATOR);
+ if (pos >= 0) {
+ pos += PAGE_INDICATOR.length();
+ StringBuffer sb = new StringBuffer();
+ while (pos < fragment.length()) {
+ char c = fragment.charAt(pos);
+ if (c >= '0' && c <= '9') {
+ sb.append(c);
+ } else {
+ break;
+ }
+ pos++;
+ }
+ if (sb.length() > 0) {
+ pageIndex = Integer.parseInt(sb.toString()) - 1;
+ pageIndex = Math.max(0, pageIndex);
+ }
+ }
+ }
+ } catch (URISyntaxException e) {
+ //ignore
+ }
+ return pageIndex;
}
}
Added: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/java/org/apache/fop/image2/ImageUtilTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/java/org/apache/fop/image2/ImageUtilTestCase.java?rev=605138&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/java/org/apache/fop/image2/ImageUtilTestCase.java (added)
+++ xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/java/org/apache/fop/image2/ImageUtilTestCase.java Tue Dec 18 00:42:41 2007
@@ -0,0 +1,61 @@
+/*
+ * 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.fop.image2;
+
+import junit.framework.TestCase;
+
+import org.apache.fop.image2.util.ImageUtil;
+
+/**
+ * Tests for the ImageUtil class.
+ */
+public class ImageUtilTestCase extends TestCase {
+
+ /**
+ * Tests {@link ImageUtil.extractPageIndexFromURI(String)}.
+ * @throws Exception if an error occurs
+ */
+ public void testPageIndexExtraction() throws Exception {
+ int pageIndex;
+
+ pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif");
+ assertEquals(0, pageIndex);
+ pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=3");
+ assertEquals(2, pageIndex);
+ pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=0");
+ assertEquals(0, pageIndex);
+ pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=");
+ assertEquals(0, pageIndex);
+ pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=x");
+ assertEquals(0, pageIndex);
+ pageIndex = ImageUtil.extractPageIndexFromURI("http://localhost/images/scan1.tif#page=-1");
+ assertEquals(0, pageIndex);
+ pageIndex = ImageUtil.extractPageIndexFromURI("#page=2");
+ assertEquals(1, pageIndex);
+
+ //Not a valid URI
+ pageIndex = ImageUtil.extractPageIndexFromURI("C:\\images\\scan1.tif#page=44");
+ assertEquals(0, pageIndex);
+ //Valid URI
+ pageIndex = ImageUtil.extractPageIndexFromURI("file:///C:/images/scan1.tif#page=44");
+ assertEquals(43, pageIndex);
+ }
+
+}
Propchange: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/java/org/apache/fop/image2/ImageUtilTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/java/org/apache/fop/image2/ImageUtilTestCase.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/resources/images/multi-page.tiff
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/resources/images/multi-page.tiff?rev=605138&view=auto
==============================================================================
Binary file - no diff available.
Propchange: xmlgraphics/fop/branches/Temp_ImagePackageRedesign/test/resources/images/multi-page.tiff
------------------------------------------------------------------------------
svn:mime-type = image/tiff
---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org