/**
* 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).
* 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;
}
/**
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);
}
private String docName;
private int[] bbox;
- private byte[] epsImage = null;
- private EPSReader epsReader = null;
+ private EPSData epsData = null;
/**
* Initialize docName and bounding box
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;
}
}
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;
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;
+ }
+
}
* @see FopImage
*/
public class GifImage extends AbstractFopImage {
- public GifImage(URL href, ImageReader imgReader) {
+ public GifImage(URL href, FopImage.ImageInfo imgReader) {
super(href, imgReader);
}
}
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) {
} 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) {
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";
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();
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)) {
super(href);
}
- public JAIImage(URL href, ImageReader imgReader) {
+ public JAIImage(URL href, FopImage.ImageInfo imgReader) {
super(href, imgReader);
}
* @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");
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);
}
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;
}
}
+++ /dev/null
-/*
- * $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;
- }
-
-}
-
import java.io.BufferedInputStream;
import java.io.IOException;
+import org.apache.fop.image.FopImage;
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;
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;
}
}
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;
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];
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) {
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);
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;
- }
}
import java.io.BufferedInputStream;
import java.io.IOException;
+import org.apache.fop.image.FopImage;
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;
}
}
import java.io.BufferedInputStream;
import java.io.IOException;
+import org.apache.fop.image.FopImage;
import org.apache.fop.fo.FOUserAgent;
/**
* @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();
}
import java.io.IOException;
import java.util.ArrayList;
+import org.apache.fop.image.FopImage;
import org.apache.fop.fo.FOUserAgent;
/**
* @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;
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) {
import java.io.BufferedInputStream;
import java.io.IOException;
+import org.apache.fop.image.FopImage;
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
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;
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();
import java.io.BufferedInputStream;
import java.io.IOException;
+import org.apache.fop.image.FopImage;
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;
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;
}
}
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;
}
/**
* 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);
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%
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;
}
}
}
import java.io.BufferedInputStream;
import java.io.IOException;
+import org.apache.fop.image.FopImage;
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)
}
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
* 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;
}
}