diff options
author | Keiron Liddle <keiron@apache.org> | 2002-02-25 09:31:03 +0000 |
---|---|---|
committer | Keiron Liddle <keiron@apache.org> | 2002-02-25 09:31:03 +0000 |
commit | 38b2ca9fad8885b42d51c9a6f34f98323651eaa7 (patch) | |
tree | 84ccf0ac387af132f7a574c7a05b7a70cb218791 /src/org/apache/fop/image | |
parent | 76705cc4238b38ecec2e4088b48358cbe3231934 (diff) | |
download | xmlgraphics-fop-38b2ca9fad8885b42d51c9a6f34f98323651eaa7.tar.gz xmlgraphics-fop-38b2ca9fad8885b42d51c9a6f34f98323651eaa7.zip |
improved thread safety of readers
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194670 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/org/apache/fop/image')
20 files changed, 271 insertions, 327 deletions
diff --git a/src/org/apache/fop/image/AbstractFopImage.java b/src/org/apache/fop/image/AbstractFopImage.java index f55d4f01c..3fa021eea 100644 --- a/src/org/apache/fop/image/AbstractFopImage.java +++ b/src/org/apache/fop/image/AbstractFopImage.java @@ -45,7 +45,7 @@ public abstract class AbstractFopImage implements FopImage { /** * ImageReader object (to obtain image header informations). */ - protected ImageReader m_imageReader = null; + protected FopImage.ImageInfo imageInfo = null; /** * Image color space (org.apache.fop.datatypes.ColorSpace). @@ -95,16 +95,16 @@ public abstract class AbstractFopImage implements FopImage { * imgReader ImageReader object * @return a new FopImage object */ - public AbstractFopImage(URL href, ImageReader imgReader) { + public AbstractFopImage(URL href, FopImage.ImageInfo info) { this.m_href = href; - this.m_imageReader = imgReader; - this.m_width = this.m_imageReader.getWidth(); - this.m_height = this.m_imageReader.getHeight(); + this.imageInfo = info; + this.m_width = this.imageInfo.width; + this.m_height = this.imageInfo.height; loaded = loaded & DIMENSIONS; } public String getMimeType() { - return m_imageReader.getMimeType(); + return imageInfo.mimeType; } /** diff --git a/src/org/apache/fop/image/BmpImage.java b/src/org/apache/fop/image/BmpImage.java index 7d2c275e7..d58fe94e9 100644 --- a/src/org/apache/fop/image/BmpImage.java +++ b/src/org/apache/fop/image/BmpImage.java @@ -26,7 +26,7 @@ import org.apache.fop.image.analyser.ImageReader; import org.apache.fop.fo.FOUserAgent; public class BmpImage extends AbstractFopImage { - public BmpImage(URL href, ImageReader imgReader) { + public BmpImage(URL href, FopImage.ImageInfo imgReader) { super(href, imgReader); } diff --git a/src/org/apache/fop/image/EPSImage.java b/src/org/apache/fop/image/EPSImage.java index bef874847..bb66eb116 100644 --- a/src/org/apache/fop/image/EPSImage.java +++ b/src/org/apache/fop/image/EPSImage.java @@ -32,8 +32,7 @@ public class EPSImage extends AbstractFopImage { private String docName; private int[] bbox; - private byte[] epsImage = null; - private EPSReader epsReader = null; + private EPSData epsData = null; /** * Initialize docName and bounding box @@ -62,23 +61,44 @@ public class EPSImage extends AbstractFopImage { return bbox; } - public EPSImage(URL href, ImageReader imgReader) { - super(href, imgReader); + public EPSImage(URL href, FopImage.ImageInfo imgInfo) { + super(href, imgInfo); init(href); - if (imgReader instanceof EPSReader) { - EPSReader eimgReader = (EPSReader) imgReader; - epsReader = eimgReader; - epsImage = eimgReader.getEpsFile(); - m_bitmaps = epsImage; - bbox = eimgReader.getBBox(); + if (imgInfo.data instanceof EPSData) { + epsData = (EPSData) imgInfo.data; + bbox = new int[4]; + bbox[0] = (int) epsData.bbox[0]; + bbox[1] = (int) epsData.bbox[1]; + bbox[2] = (int) epsData.bbox[2]; + bbox[3] = (int) epsData.bbox[3]; + } } public byte[] getEPSImage() { - if (epsImage == null) { + if (epsData.epsFile == null) { //log.error("ERROR LOADING EXTERNAL EPS"); } - return epsImage; + return epsData.epsFile; + } + + public static class EPSData { + public long[] bbox; + public boolean isAscii; // True if plain ascii eps file + + // offsets if not ascii + public long psStart = 0; + public long psLength = 0; + public long wmfStart = 0; + public long wmfLength = 0; + public long tiffStart = 0; + public long tiffLength = 0; + + /** raw eps file */ + public byte[] rawEps; + /** eps part */ + public byte[] epsFile; + public byte[] preview = null; } } diff --git a/src/org/apache/fop/image/FopImage.java b/src/org/apache/fop/image/FopImage.java index 6cb674988..703616aae 100644 --- a/src/org/apache/fop/image/FopImage.java +++ b/src/org/apache/fop/image/FopImage.java @@ -10,6 +10,8 @@ package org.apache.fop.image; +import java.io.InputStream; + import org.apache.fop.datatypes.ColorSpace; import org.apache.fop.pdf.PDFColor; import org.apache.fop.pdf.PDFFilter; @@ -61,5 +63,14 @@ public interface FopImage { public int getRessourceBytesSize(); // return null if no corresponding PDFFilter public PDFFilter getPDFFilter(); + + public static class ImageInfo { + public InputStream stream; + public int width; + public int height; + public Object data; + public String mimeType; + } + } diff --git a/src/org/apache/fop/image/GifImage.java b/src/org/apache/fop/image/GifImage.java index 56dbac64c..dc27dbe21 100644 --- a/src/org/apache/fop/image/GifImage.java +++ b/src/org/apache/fop/image/GifImage.java @@ -27,7 +27,7 @@ import org.apache.fop.fo.FOUserAgent; * @see FopImage */ public class GifImage extends AbstractFopImage { - public GifImage(URL href, ImageReader imgReader) { + public GifImage(URL href, FopImage.ImageInfo imgReader) { super(href, imgReader); } diff --git a/src/org/apache/fop/image/ImageFactory.java b/src/org/apache/fop/image/ImageFactory.java index 837a84f7c..e2217ba56 100644 --- a/src/org/apache/fop/image/ImageFactory.java +++ b/src/org/apache/fop/image/ImageFactory.java @@ -119,7 +119,7 @@ public class ImageFactory { } imgIS = absoluteURL.openStream(); } catch (MalformedURLException e_context) { - log.error("Error with image URL: " + e_context.getMessage()); + log.error("Error with image URL: " + e_context.getMessage(), e_context); return null; } catch (Exception e) { @@ -134,22 +134,22 @@ public class ImageFactory { } catch (MalformedURLException e_context) { // pb context url log.error( "Invalid Image URL - error on relative URL : " + - e_context.getMessage()); + e_context.getMessage(), e_context); return null; } } // If not, check image type - ImageReader imgReader = null; + FopImage.ImageInfo imgInfo = null; try { if (imgIS == null) { imgIS = absoluteURL.openStream(); } - imgReader = ImageReaderFactory.make( + imgInfo = ImageReaderFactory.make( absoluteURL.toExternalForm(), imgIS, ua); } catch (Exception e) { log.error("Error while recovering Image Informations (" + - absoluteURL.toString() + ") : " + e.getMessage()); + absoluteURL.toString() + ") : " + e.getMessage(), e); return null; } finally { if (imgIS != null) { @@ -157,13 +157,13 @@ public class ImageFactory { imgIS.close(); } catch (IOException e) {} } - } if (imgReader == null) { + } if (imgInfo == null) { log.error("No ImageReader for this type of image (" + absoluteURL.toString() + ")"); return null; } // Associate mime-type to FopImage class - String imgMimeType = imgReader.getMimeType(); + String imgMimeType = imgInfo.mimeType; String imgClassName = null; if ("image/gif".equals(imgMimeType)) { imgClassName = "org.apache.fop.image.GifImage"; @@ -201,14 +201,14 @@ public class ImageFactory { try { imageClass = Class.forName(imgClassName); Class[] imageConstructorParameters = new Class[2]; - imageConstructorParameters[0] = Class.forName("java.net.URL"); - imageConstructorParameters[1] = Class.forName("org.apache.fop.image.analyser.ImageReader"); + imageConstructorParameters[0] = java.net.URL.class; + imageConstructorParameters[1] = org.apache.fop.image.FopImage.ImageInfo.class; Constructor imageConstructor = imageClass.getDeclaredConstructor( imageConstructorParameters); Object[] initArgs = new Object[2]; initArgs[0] = absoluteURL; - initArgs[1] = imgReader; + initArgs[1] = imgInfo; imageInstance = imageConstructor.newInstance(initArgs); } catch (java.lang.reflect.InvocationTargetException ex) { Throwable t = ex.getTargetException(); @@ -219,13 +219,13 @@ public class ImageFactory { msg = ex.getMessage(); } log.error("Error creating FopImage object (" + - absoluteURL.toString() + ") : " + msg); + absoluteURL.toString() + ") : " + msg, (t == null) ? ex:t); return null; } catch (Exception ex) { log.error("Error creating FopImage object (" + "Error creating FopImage object (" + - absoluteURL.toString() + ") : " + ex.getMessage()); + absoluteURL.toString() + ") : " + ex.getMessage(), ex); return null; } if (!(imageInstance instanceof org.apache.fop.image.FopImage)) { diff --git a/src/org/apache/fop/image/JAIImage.java b/src/org/apache/fop/image/JAIImage.java index f62e39b62..e5e15bc62 100644 --- a/src/org/apache/fop/image/JAIImage.java +++ b/src/org/apache/fop/image/JAIImage.java @@ -38,7 +38,7 @@ public class JAIImage extends AbstractFopImage { super(href); } - public JAIImage(URL href, ImageReader imgReader) { + public JAIImage(URL href, FopImage.ImageInfo imgReader) { super(href, imgReader); } diff --git a/src/org/apache/fop/image/JimiImage.java b/src/org/apache/fop/image/JimiImage.java index 6ed39c722..6c6eb6b64 100644 --- a/src/org/apache/fop/image/JimiImage.java +++ b/src/org/apache/fop/image/JimiImage.java @@ -29,7 +29,7 @@ import org.apache.fop.image.analyser.ImageReader; * @see FopImage */ public class JimiImage extends AbstractFopImage { - public JimiImage(URL href, ImageReader imgReader) { + public JimiImage(URL href, FopImage.ImageInfo imgReader) { super(href, imgReader); try { Class c = Class.forName("com.sun.jimi.core.Jimi"); diff --git a/src/org/apache/fop/image/JpegImage.java b/src/org/apache/fop/image/JpegImage.java index cc0032bf5..7e3b756dd 100644 --- a/src/org/apache/fop/image/JpegImage.java +++ b/src/org/apache/fop/image/JpegImage.java @@ -32,7 +32,7 @@ public class JpegImage extends AbstractFopImage { boolean found_icc_profile = false; boolean found_dimensions = false; - public JpegImage(URL href, ImageReader imgReader) { + public JpegImage(URL href, FopImage.ImageInfo imgReader) { super(href, imgReader); } diff --git a/src/org/apache/fop/image/SVGImage.java b/src/org/apache/fop/image/SVGImage.java index 49e69865e..95d7b5d09 100644 --- a/src/org/apache/fop/image/SVGImage.java +++ b/src/org/apache/fop/image/SVGImage.java @@ -26,10 +26,10 @@ import org.apache.batik.dom.svg.SAXSVGDocumentFactory; public class SVGImage extends AbstractFopImage { SVGDocument doc; - public SVGImage(URL href, ImageReader imgReader) { - super(href, imgReader); - if(imgReader instanceof SVGReader) { - doc = (SVGDocument)((SVGReader)imgReader).getDocument(); + public SVGImage(URL href, FopImage.ImageInfo imgInfo) { + super(href, imgInfo); + if(imgInfo.data instanceof SVGDocument) { + doc = (SVGDocument)imgInfo.data; } } diff --git a/src/org/apache/fop/image/analyser/AbstractImageReader.java b/src/org/apache/fop/image/analyser/AbstractImageReader.java deleted file mode 100644 index 999ffa127..000000000 --- a/src/org/apache/fop/image/analyser/AbstractImageReader.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. - * For details on use and redistribution please refer to the - * LICENSE file included with these sources. - */ - -package org.apache.fop.image.analyser; - -// Java -import java.io.BufferedInputStream; -import java.io.IOException; - -import org.apache.fop.fo.FOUserAgent; - -/** - * Base class implementing ImageReader. - * @author Pankaj Narula - * @version 1.0 - * @see ImageReader - */ -public abstract class AbstractImageReader implements ImageReader { - - /** - * Image width. - */ - protected int width = 0; - - /** - * Image height. - */ - protected int height = 0; - - /** - * Image stream. - */ - protected BufferedInputStream imageStream = null; - - public abstract boolean verifySignature(String uri, - BufferedInputStream fis, FOUserAgent ua) throws IOException; - - public int getHeight() { - return this.height; - } - - public int getWidth() { - return this.width; - } - - public abstract String getMimeType(); - - public BufferedInputStream getInputStream() { - return this.imageStream; - } - -} - diff --git a/src/org/apache/fop/image/analyser/BMPReader.java b/src/org/apache/fop/image/analyser/BMPReader.java index 5e7cd8513..86fd8b77f 100644 --- a/src/org/apache/fop/image/analyser/BMPReader.java +++ b/src/org/apache/fop/image/analyser/BMPReader.java @@ -11,6 +11,7 @@ package org.apache.fop.image.analyser; import java.io.BufferedInputStream; import java.io.IOException; +import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** @@ -18,29 +19,29 @@ import org.apache.fop.fo.FOUserAgent; * @author Pankaj Narula * @version 1.0 */ -public class BMPReader extends AbstractImageReader { +public class BMPReader implements ImageReader { static protected final int BMP_SIG_LENGTH = 26; - protected byte[] header; - - public boolean verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, FOUserAgent ua) throws IOException { - this.imageStream = fis; - this.setDefaultHeader(); + byte[] header = getDefaultHeader(fis); boolean supported = ((header[0] == (byte) 0x42) && (header[1] == (byte) 0x4d)); if (supported) { - setDimension(); - return true; - } else - return false; + return getDimension(header); + } else { + return null; + } } public String getMimeType() { return "image/bmp"; } - protected void setDimension() { + protected FopImage.ImageInfo getDimension(byte[] header) { + FopImage.ImageInfo info = new FopImage.ImageInfo(); + info.mimeType = getMimeType(); + // little endian notation int byte1 = header[18] & 0xff; int byte2 = header[19] & 0xff; @@ -48,28 +49,30 @@ public class BMPReader extends AbstractImageReader { int byte4 = header[21] & 0xff; long l = (long)((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1); - this.width = (int)(l & 0xffffffff); + info.width = (int)(l & 0xffffffff); byte1 = header[22] & 0xff; byte2 = header[23] & 0xff; byte3 = header[24] & 0xff; byte4 = header[25] & 0xff; l = (long)((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1); - this.height = (int)(l & 0xffffffff); + info.height = (int)(l & 0xffffffff); + return info; } - protected void setDefaultHeader() throws IOException { - this.header = new byte[BMP_SIG_LENGTH]; + protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + byte[] header = new byte[BMP_SIG_LENGTH]; try { - this.imageStream.mark(BMP_SIG_LENGTH + 1); - this.imageStream.read(header); - this.imageStream.reset(); + imageStream.mark(BMP_SIG_LENGTH + 1); + imageStream.read(header); + imageStream.reset(); } catch (IOException ex) { try { - this.imageStream.reset(); + imageStream.reset(); } catch (IOException exbis) {} throw ex; } + return header; } } diff --git a/src/org/apache/fop/image/analyser/EPSReader.java b/src/org/apache/fop/image/analyser/EPSReader.java index fbaaffe6d..60a6c5624 100644 --- a/src/org/apache/fop/image/analyser/EPSReader.java +++ b/src/org/apache/fop/image/analyser/EPSReader.java @@ -17,28 +17,14 @@ import java.net.URL; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; +import org.apache.fop.image.FopImage; +import org.apache.fop.image.EPSImage; import org.apache.fop.fo.FOUserAgent; /** * ImageReader object for EPS document image type. */ -public class EPSReader extends AbstractImageReader { - private long[] bbox; - private boolean isAscii; // True if plain ascii eps file - - // offsets if not ascii - long psStart = 0; - long psLength = 0; - long wmfStart = 0; - long wmfLength = 0; - long tiffStart = 0; - long tiffLength = 0; - - /** raw eps file */ - private byte[] rawEps; - /** eps part */ - private byte[] epsFile; - private byte[] preview = null; +public class EPSReader implements ImageReader { private long getLong(byte[] buf, int idx) { int b1 = buf[idx] & 0xff; @@ -49,55 +35,60 @@ public class EPSReader extends AbstractImageReader { return (long)((b4 << 24) | (b3 << 16) | (b2 << 8) | b1); } - public boolean verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, FOUserAgent ua) throws IOException { boolean isEPS = false; - this.imageStream = fis; fis.mark(32); byte[] header = new byte[30]; fis.read(header, 0, 30); fis.reset(); + EPSImage.EPSData data = new EPSImage.EPSData(); + // Check if binary header if (getLong(header, 0) == 0xC6D3D0C5) { - isAscii = false; + data.isAscii = false; isEPS = true; - psStart = getLong(header, 4); - psLength = getLong(header, 8); - wmfStart = getLong(header, 12); - wmfLength = getLong(header, 16); - tiffStart = getLong(header, 20); - tiffLength = getLong(header, 24); + data.psStart = getLong(header, 4); + data.psLength = getLong(header, 8); + data.wmfStart = getLong(header, 12); + data.wmfLength = getLong(header, 16); + data.tiffStart = getLong(header, 20); + data.tiffLength = getLong(header, 24); } else { // Check if plain ascii byte[] epsh = "%!PS".getBytes(); if (epsh[0] == header[0] && epsh[1] == header[1] && epsh[2] == header[2] && epsh[3] == header[3]) { - isAscii = true; + data.isAscii = true; isEPS = true; } } if (isEPS) { - readEPSImage(fis); - bbox = readBBox(); - - if (bbox != null) { - width = (int)(bbox[2] - bbox[0]); - height = (int)(bbox[3] - bbox[1]); + FopImage.ImageInfo info = new FopImage.ImageInfo(); + info.mimeType = getMimeType(); + info.data = data; + readEPSImage(fis, data); + data.bbox = readBBox(data); + + if (data.bbox != null) { + info.width = (int)(data.bbox[2] - data.bbox[0]); + info.height = (int)(data.bbox[3] - data.bbox[1]); + return info; } else { // Ain't eps if no BoundingBox isEPS = false; } } - return isEPS; + return null; } /** read the eps file and extract eps part */ - private void readEPSImage(BufferedInputStream fis) throws IOException { + private void readEPSImage(BufferedInputStream fis, EPSImage.EPSData data) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] file; byte[] readBuf = new byte[20480]; @@ -117,49 +108,44 @@ public class EPSReader extends AbstractImageReader { file = baos.toByteArray(); - if (isAscii) { - rawEps = null; - epsFile = new byte[file.length]; - System.arraycopy(file, 0, epsFile, 0, epsFile.length); + if (data.isAscii) { + data.rawEps = null; + data.epsFile = new byte[file.length]; + System.arraycopy(file, 0, data.epsFile, 0, data.epsFile.length); } else { - rawEps = new byte[file.length]; - epsFile = new byte[(int) psLength]; - System.arraycopy(file, 0, rawEps, 0, rawEps.length); - System.arraycopy(rawEps, (int) psStart, epsFile, 0, - (int) psLength); + data.rawEps = new byte[file.length]; + data.epsFile = new byte[(int) data.psLength]; + System.arraycopy(file, 0, data.rawEps, 0, data.rawEps.length); + System.arraycopy(data.rawEps, (int) data.psStart, data.epsFile, 0, + (int) data.psLength); } } - public byte[] getEpsFile() { - return epsFile; - } - /* Get embedded preview or null */ - public byte[] getPreview() { - InputStream is = null; - if (preview == null) { - if (tiffLength > 0) { - preview = new byte[(int) tiffLength]; - System.arraycopy(rawEps, (int) tiffStart, preview, 0, - (int) tiffLength); + public byte[] getPreview(EPSImage.EPSData data) { + if (data.preview == null) { + if (data.tiffLength > 0) { + data.preview = new byte[(int) data.tiffLength]; + System.arraycopy(data.rawEps, (int) data.tiffStart, data.preview, 0, + (int) data.tiffLength); } } - return preview; + return data.preview; } /** Extract bounding box from eps part */ - private long[] readBBox() { + private long[] readBBox(EPSImage.EPSData data) { long[] mbbox = null; int idx = 0; byte[] bbxName = "%%BoundingBox: ".getBytes(); boolean found = false; - while (!found && (epsFile.length > (idx + bbxName.length))) { + while (!found && (data.epsFile.length > (idx + bbxName.length))) { boolean sfound = true; int i = idx; for (i = idx; sfound && (i - idx) < bbxName.length; i++) { - if (bbxName[i - idx] != epsFile[i]) + if (bbxName[i - idx] != data.epsFile[i]) sfound = false; } if (sfound) { @@ -175,26 +161,26 @@ public class EPSReader extends AbstractImageReader { mbbox = new long[4]; - idx += readLongString(mbbox, 0, idx); - idx += readLongString(mbbox, 1, idx); - idx += readLongString(mbbox, 2, idx); - idx += readLongString(mbbox, 3, idx); + idx += readLongString(data, mbbox, 0, idx); + idx += readLongString(data, mbbox, 1, idx); + idx += readLongString(data, mbbox, 2, idx); + idx += readLongString(data, mbbox, 3, idx); return mbbox; } - private int readLongString(long[] mbbox, int i, int idx) { - while (idx < epsFile.length && (epsFile[idx] == 32)) + private int readLongString(EPSImage.EPSData data, long[] mbbox, int i, int idx) { + while (idx < data.epsFile.length && (data.epsFile[idx] == 32)) idx++; int nidx = idx; - while (nidx < epsFile.length && - (epsFile[nidx] >= 48 && epsFile[nidx] <= 57)) + while (nidx < data.epsFile.length && + (data.epsFile[nidx] >= 48 && data.epsFile[nidx] <= 57)) nidx++; byte[] num = new byte[nidx - idx]; - System.arraycopy(epsFile, idx, num, 0, nidx - idx); + System.arraycopy(data.epsFile, idx, num, 0, nidx - idx); String ns = new String(num); mbbox[i] = Long.parseLong(ns); @@ -205,16 +191,5 @@ public class EPSReader extends AbstractImageReader { return "image/eps"; } - /** - * Return the BoundingBox - */ - public int[] getBBox() { - int[] bbox = new int[4]; - bbox[0] = (int) this.bbox[0]; - bbox[1] = (int) this.bbox[1]; - bbox[2] = (int) this.bbox[2]; - bbox[3] = (int) this.bbox[3]; - return bbox; - } } diff --git a/src/org/apache/fop/image/analyser/GIFReader.java b/src/org/apache/fop/image/analyser/GIFReader.java index 0618d9540..00ca6e532 100644 --- a/src/org/apache/fop/image/analyser/GIFReader.java +++ b/src/org/apache/fop/image/analyser/GIFReader.java @@ -11,6 +11,7 @@ package org.apache.fop.image.analyser; import java.io.BufferedInputStream; import java.io.IOException; +import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** @@ -18,52 +19,54 @@ import org.apache.fop.fo.FOUserAgent; * @author Pankaj Narula * @version 1.0 */ -public class GIFReader extends AbstractImageReader { +public class GIFReader implements ImageReader { static protected final int GIF_SIG_LENGTH = 10; - protected byte[] header; - public boolean verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, FOUserAgent ua) throws IOException { - this.imageStream = fis; - this.setDefaultHeader(); + byte[] header = getDefaultHeader(fis); boolean supported = ((header[0] == 'G') && (header[1] == 'I') && (header[2] == 'F') && (header[3] == '8') && (header[4] == '7' || header[4] == '9') && (header[5] == 'a')); if (supported) { - setDimension(); - return true; + FopImage.ImageInfo info = getDimension(header); + info.mimeType = getMimeType(); + return info; } else - return false; + return null; } public String getMimeType() { return "image/gif"; } - protected void setDimension() { + protected FopImage.ImageInfo getDimension(byte[] header) { + FopImage.ImageInfo info = new FopImage.ImageInfo(); // little endian notation int byte1 = header[6] & 0xff; int byte2 = header[7] & 0xff; - this.width = ((byte2 << 8) | byte1) & 0xffff; + info.width = ((byte2 << 8) | byte1) & 0xffff; byte1 = header[8] & 0xff; byte2 = header[9] & 0xff; - this.height = ((byte2 << 8) | byte1) & 0xffff; + info.height = ((byte2 << 8) | byte1) & 0xffff; + return info; } - protected void setDefaultHeader() throws IOException { - this.header = new byte[GIF_SIG_LENGTH]; + protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + byte[] header = new byte[GIF_SIG_LENGTH]; try { - this.imageStream.mark(GIF_SIG_LENGTH + 1); - this.imageStream.read(header); - this.imageStream.reset(); + imageStream.mark(GIF_SIG_LENGTH + 1); + imageStream.read(header); + imageStream.reset(); } catch (IOException ex) { try { - this.imageStream.reset(); + imageStream.reset(); } catch (IOException exbis) {} throw ex; } + return header; } } diff --git a/src/org/apache/fop/image/analyser/ImageReader.java b/src/org/apache/fop/image/analyser/ImageReader.java index 5554b5db5..7460a5b22 100644 --- a/src/org/apache/fop/image/analyser/ImageReader.java +++ b/src/org/apache/fop/image/analyser/ImageReader.java @@ -11,6 +11,7 @@ package org.apache.fop.image.analyser; import java.io.BufferedInputStream; import java.io.IOException; +import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** @@ -26,31 +27,8 @@ public interface ImageReader { * @return true if image type is the handled one * @exception IOException io error */ - public boolean verifySignature(String uri, BufferedInputStream bis, + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, FOUserAgent ua) throws IOException; - /** - * Return the used InputStream. - * @return BufferedInputStream used to verify image type - */ - public BufferedInputStream getInputStream(); - - /** - * Return correspondig mime type. - * @return image mime type - */ - public String getMimeType(); - - /** - * Return the image height. - * @return image height - */ - public int getHeight(); - - /** - * Return the image width. - * @return image width - */ - public int getWidth(); } diff --git a/src/org/apache/fop/image/analyser/ImageReaderFactory.java b/src/org/apache/fop/image/analyser/ImageReaderFactory.java index 62591eac3..8d45e48a7 100644 --- a/src/org/apache/fop/image/analyser/ImageReaderFactory.java +++ b/src/org/apache/fop/image/analyser/ImageReaderFactory.java @@ -13,6 +13,7 @@ import java.io.BufferedInputStream; import java.io.IOException; import java.util.ArrayList; +import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** @@ -40,7 +41,7 @@ public class ImageReaderFactory { * @return ImageReader object * image type is not supported */ - static public ImageReader make(String uri, InputStream in, + static public FopImage.ImageInfo make(String uri, InputStream in, FOUserAgent ua) { ImageReader reader; @@ -48,8 +49,9 @@ public class ImageReaderFactory { try { for (int count = 0; count < formats.size(); count++) { reader = (ImageReader) formats.get(count); - if (reader.verifySignature(uri, bis, ua)) { - return reader; + FopImage.ImageInfo info = reader.verifySignature(uri, bis, ua); + if(info != null) { + return info; } } } catch (IOException ex) { diff --git a/src/org/apache/fop/image/analyser/JPEGReader.java b/src/org/apache/fop/image/analyser/JPEGReader.java index 16964e4fe..eda900f1d 100644 --- a/src/org/apache/fop/image/analyser/JPEGReader.java +++ b/src/org/apache/fop/image/analyser/JPEGReader.java @@ -11,6 +11,7 @@ package org.apache.fop.image.analyser; import java.io.BufferedInputStream; import java.io.IOException; +import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** @@ -18,7 +19,7 @@ import org.apache.fop.fo.FOUserAgent; * @author Pankaj Narula * @version 1.0 */ -public class JPEGReader extends AbstractImageReader { +public class JPEGReader implements ImageReader { /** * Only SOFn and APPn markers are defined as SOFn is needed for the height and @@ -39,40 +40,41 @@ public class JPEGReader extends AbstractImageReader { static protected final int SOI = 0xd8; // start of Image static protected final int JPG_SIG_LENGTH = 2; - protected byte[] header; - - public boolean verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, FOUserAgent ua) throws IOException { - this.imageStream = fis; - this.setDefaultHeader(); + byte[] header = getDefaultHeader(fis); boolean supported = ((header[0] == (byte) 0xff) && (header[1] == (byte) 0xd8)); if (supported) { - setDimension(); - return true; - } else - return false; + FopImage.ImageInfo info = getDimension(fis); + info.mimeType = getMimeType(); + return info; + } else { + return null; + } } public String getMimeType() { return "image/jpeg"; } - protected void setDefaultHeader() throws IOException { - this.header = new byte[JPG_SIG_LENGTH]; + protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + byte[] header = new byte[JPG_SIG_LENGTH]; try { - this.imageStream.mark(JPG_SIG_LENGTH + 1); - this.imageStream.read(header); - this.imageStream.reset(); + imageStream.mark(JPG_SIG_LENGTH + 1); + imageStream.read(header); + imageStream.reset(); } catch (IOException ex) { try { - this.imageStream.reset(); + imageStream.reset(); } catch (IOException exbis) {} throw ex; } + return header; } - protected void setDimension() throws IOException { + protected FopImage.ImageInfo getDimension(BufferedInputStream imageStream) throws IOException { + FopImage.ImageInfo info = new FopImage.ImageInfo(); try { int marker = NULL; long length, skipped; @@ -94,32 +96,33 @@ public class JPEGReader extends AbstractImageReader { case SOF2: case SOF3: // SOF3 and SOFA are only supported by PDF 1.3 case SOFA: - this.skip(3); - this.height = this.read2bytes(); - this.width = this.read2bytes(); + this.skip(imageStream, 3); + info.height = this.read2bytes(imageStream); + info.width = this.read2bytes(imageStream); break outer; default: - length = this.read2bytes(); - skipped = this.skip(length - 2); + length = this.read2bytes(imageStream); + skipped = this.skip(imageStream, length - 2); if (skipped != length - 2) throw new IOException("Skipping Error"); } } } catch (IOException ioe) { try { - this.imageStream.reset(); + imageStream.reset(); } catch (IOException exbis) {} throw ioe; } + return info; } - protected int read2bytes() throws IOException { + protected int read2bytes(BufferedInputStream imageStream) throws IOException { int byte1 = imageStream.read(); int byte2 = imageStream.read(); return (int)((byte1 << 8) | byte2); } - protected long skip(long n) throws IOException { + protected long skip(BufferedInputStream imageStream, long n) throws IOException { long discarded = 0; while (discarded != n) { imageStream.read(); diff --git a/src/org/apache/fop/image/analyser/PNGReader.java b/src/org/apache/fop/image/analyser/PNGReader.java index 29d794504..b41f19e37 100644 --- a/src/org/apache/fop/image/analyser/PNGReader.java +++ b/src/org/apache/fop/image/analyser/PNGReader.java @@ -11,6 +11,7 @@ package org.apache.fop.image.analyser; import java.io.BufferedInputStream; import java.io.IOException; +import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** @@ -18,31 +19,33 @@ import org.apache.fop.fo.FOUserAgent; * @author Pankaj Narula * @version 1.0 */ -public class PNGReader extends AbstractImageReader { +public class PNGReader implements ImageReader { static protected final int PNG_SIG_LENGTH = 24; - protected byte[] header; - public boolean verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, FOUserAgent ua) throws IOException { - this.imageStream = fis; - this.setDefaultHeader(); + byte[] header = getDefaultHeader(fis); boolean supported = ((header[0] == (byte) 0x89) && (header[1] == (byte) 0x50) && (header[2] == (byte) 0x4e) && (header[3] == (byte) 0x47) && (header[4] == (byte) 0x0d) && (header[5] == (byte) 0x0a) && (header[6] == (byte) 0x1a) && (header[7] == (byte) 0x0a)); if (supported) { - setDimension(); - return true; - } else - return false; + FopImage.ImageInfo info = getDimension(header); + info.mimeType = getMimeType(); + return info; + } else { + return null; + } } public String getMimeType() { return "image/png"; } - protected void setDimension() { + protected FopImage.ImageInfo getDimension(byte[] header) { + FopImage.ImageInfo info = new FopImage.ImageInfo(); + // png is always big endian int byte1 = header[16] & 0xff; int byte2 = header[17] & 0xff; @@ -50,29 +53,30 @@ public class PNGReader extends AbstractImageReader { int byte4 = header[19] & 0xff; long l = (long)((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4); - this.width = (int)(l); + info.width = (int)(l); byte1 = header[20] & 0xff; byte2 = header[21] & 0xff; byte3 = header[22] & 0xff; byte4 = header[23] & 0xff; l = (long)((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4); - this.height = (int)(l); - + info.height = (int)(l); + return info; } - protected void setDefaultHeader() throws IOException { - this.header = new byte[PNG_SIG_LENGTH]; + protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + byte[] header = new byte[PNG_SIG_LENGTH]; try { - this.imageStream.mark(PNG_SIG_LENGTH + 1); - this.imageStream.read(header); - this.imageStream.reset(); + imageStream.mark(PNG_SIG_LENGTH + 1); + imageStream.read(header); + imageStream.reset(); } catch (IOException ex) { try { - this.imageStream.reset(); + imageStream.reset(); } catch (IOException exbis) {} throw ex; } + return header; } } diff --git a/src/org/apache/fop/image/analyser/SVGReader.java b/src/org/apache/fop/image/analyser/SVGReader.java index 34e7406a6..feecadcb2 100644 --- a/src/org/apache/fop/image/analyser/SVGReader.java +++ b/src/org/apache/fop/image/analyser/SVGReader.java @@ -46,52 +46,46 @@ import java.awt.Point; import java.awt.geom.Dimension2D; import java.awt.Dimension; +import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; import org.apache.fop.svg.SVGUserAgent; /** * ImageReader object for SVG document image type. */ -public class SVGReader extends AbstractImageReader { +public class SVGReader implements ImageReader { public static final String SVG_MIME_TYPE = "image/svg+xml"; - FOUserAgent userAgent; - Document doc; boolean batik = true; public SVGReader() { } - public boolean verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, FOUserAgent ua) throws IOException { - this.imageStream = fis; - userAgent = ua; - return loadImage(uri); + return loadImage(uri, fis, ua); } public String getMimeType() { return SVG_MIME_TYPE; } - public Document getDocument() { - return doc; - } - /** * This means the external svg document will be loaded twice. * Possibly need a slightly different design for the image stuff. */ - protected boolean loadImage(String uri) { + protected FopImage.ImageInfo loadImage(String uri, BufferedInputStream fis, + FOUserAgent ua) { if(batik) { try { Loader loader = new Loader(); - return loader.getImage(uri); + return loader.getImage(uri, fis, ua); } catch (NoClassDefFoundError e) { batik = false; - //userAgent.getLogger().error("Batik not in class path", e); - return false; + //ua.getLogger().error("Batik not in class path", e); + return null; } } - return false; + return null; } /** @@ -100,21 +94,26 @@ public class SVGReader extends AbstractImageReader { * the SVGReader class. */ class Loader { - private boolean getImage(String uri) { + private FopImage.ImageInfo getImage(String uri, BufferedInputStream fis, + FOUserAgent ua) { // parse document and get the size attributes of the svg element try { - int length = imageStream.available(); - imageStream.mark(length); + FopImage.ImageInfo info = new FopImage.ImageInfo(); + info.mimeType = getMimeType(); + + int length = fis.available(); + fis.mark(length); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory( SVGImage.getParserName()); - doc = factory.createDocument(uri, imageStream); + SVGDocument doc = factory.createDocument(uri, fis); + info.data = doc; Element e = ((SVGDocument) doc).getRootElement(); String s; SVGUserAgent userAg = new SVGUserAgent(new AffineTransform()); - userAg.setLogger(userAgent.getLogger()); + userAg.setLogger(ua.getLogger()); BridgeContext ctx = new BridgeContext(userAg); UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e); @@ -125,7 +124,7 @@ public class SVGReader extends AbstractImageReader { if (s.length() == 0) { s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE; } - width = (int) UnitProcessor.svgHorizontalLengthToUserSpace ( + info.width = (int) UnitProcessor.svgHorizontalLengthToUserSpace ( s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx); // 'height' attribute - default is 100% @@ -134,27 +133,27 @@ public class SVGReader extends AbstractImageReader { if (s.length() == 0) { s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE; } - height = (int) UnitProcessor.svgVerticalLengthToUserSpace ( + info.height = (int) UnitProcessor.svgVerticalLengthToUserSpace ( s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx); - return true; + return info; } catch (NoClassDefFoundError ncdfe) { batik = false; - //userAgent.getLogger().error("Batik not in class path", ncdfe); - return false; + //ua.getLogger().error("Batik not in class path", ncdfe); + return null; } catch (Exception e) { // If the svg is invalid then it throws an IOException // so there is no way of knowing if it is an svg document - // userAgent.getLogger().error("Could not load external SVG: " + + // ua.getLogger().error("Could not load external SVG: " + // e.getMessage(), e); // assuming any exception means this document is not svg // or could not be loaded for some reason try { - imageStream.reset(); + fis.reset(); } catch (IOException ioe) { } - return false; + return null; } } } diff --git a/src/org/apache/fop/image/analyser/TIFFReader.java b/src/org/apache/fop/image/analyser/TIFFReader.java index ea8d2fec9..8b5d5660a 100644 --- a/src/org/apache/fop/image/analyser/TIFFReader.java +++ b/src/org/apache/fop/image/analyser/TIFFReader.java @@ -11,6 +11,7 @@ package org.apache.fop.image.analyser; import java.io.BufferedInputStream; import java.io.IOException; +import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** @@ -18,14 +19,12 @@ import org.apache.fop.fo.FOUserAgent; * @author Pankaj Narula, Michael Lee * @version 1.0 */ -public class TIFFReader extends AbstractImageReader { +public class TIFFReader implements ImageReader { static protected final int TIFF_SIG_LENGTH = 8; - protected byte[] header; - public boolean verifySignature(String uri, BufferedInputStream fis, + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, FOUserAgent ua) throws IOException { - this.imageStream = fis; - this.setDefaultHeader(); + byte[] header = getDefaultHeader(fis); boolean supported = false; if (header[0] == (byte) 0x49 && header[1] == (byte) 0x49)// first 2 bytes = II (little endian encoding) @@ -43,17 +42,19 @@ public class TIFFReader extends AbstractImageReader { } if (supported) { - setDimension(); - return true; - } else - return false; + FopImage.ImageInfo info = getDimension(header); + info.mimeType = getMimeType(); + return info; + } else { + return null; + } } public String getMimeType() { return "image/tiff"; } - protected void setDimension() { + protected FopImage.ImageInfo getDimension(byte[] header) { // currently not setting the width and height // these are set again by the Jimi image reader. // I suppose I'll do it one day to be complete. Or @@ -76,20 +77,22 @@ public class TIFFReader extends AbstractImageReader { * byte4 ); * this.height = ( int ) ( l ); */ + return null; } - protected void setDefaultHeader() throws IOException { - this.header = new byte[TIFF_SIG_LENGTH]; + protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + byte[] header = new byte[TIFF_SIG_LENGTH]; try { - this.imageStream.mark(TIFF_SIG_LENGTH + 1); - this.imageStream.read(header); - this.imageStream.reset(); + imageStream.mark(TIFF_SIG_LENGTH + 1); + imageStream.read(header); + imageStream.reset(); } catch (IOException ex) { try { - this.imageStream.reset(); + imageStream.reset(); } catch (IOException exbis) {} throw ex; } + return header; } } |