Refactoring image conversion + some math bugfixes
git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@168 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
parent
9babea1b6c
commit
74d7599b11
19 changed files with 533 additions and 369 deletions
|
@ -20,160 +20,163 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2014-08-25)
|
||||
* Version 1.4 (2014-09-03)
|
||||
*
|
||||
*/
|
||||
|
||||
package writer2latex.base;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.InputStream;
|
||||
//import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import writer2latex.api.OutputFile;
|
||||
import writer2latex.util.Misc;
|
||||
|
||||
|
||||
/**
|
||||
* <p>Class representing a binary graphics document.
|
||||
* This class is used for representing graphics documents that are <i>not</i>
|
||||
* interpreted in any way, but simply copied verbatim from the source format
|
||||
* to the target format.</p>
|
||||
*
|
||||
* <p><code>GraphicsDocument</code> is used to create new graphics documents.</p>
|
||||
*
|
||||
/** This class is used to represent a binary graphics document to be included in the converter result.
|
||||
* I may also represent a linked image, which should <em>not</em> be included (and will produce an empty file
|
||||
* if it is).
|
||||
*/
|
||||
public class BinaryGraphicsDocument implements OutputFile {
|
||||
|
||||
//private final static int BUFFERSIZE = 1024;
|
||||
|
||||
private String docName;
|
||||
|
||||
private byte[] data;
|
||||
private int nOff;
|
||||
private int nLen;
|
||||
|
||||
private String sFileName;
|
||||
private String sFileExtension;
|
||||
private String sMimeType;
|
||||
|
||||
private boolean bIsLinked = false;
|
||||
private boolean bIsAcceptedFormat = false;
|
||||
|
||||
// Data for an embedded image
|
||||
private byte[] blob = null;
|
||||
private int nOff;
|
||||
private int nLen;
|
||||
|
||||
// Data for a linked image
|
||||
private String sURL = null;
|
||||
|
||||
/**
|
||||
* <p>Constructs a new graphics document.</p>
|
||||
/**Constructs a new graphics document.
|
||||
* This new document does not contain any data. Document data must
|
||||
* be added using the appropriate methods.
|
||||
*
|
||||
* <p>This new document does not contain any information. Document data must
|
||||
* either be added using appropriate methods, or an existing file can be
|
||||
* {@link #read(InputStream) read} from an <code>InputStream</code>.</p>
|
||||
*
|
||||
* @param name The name of the <code>GraphicsDocument</code>.
|
||||
* @param sName The name of the <code>GraphicsDocument</code>.
|
||||
* @param sFileExtension the file extension
|
||||
* @param sMimeType the MIME type of the document
|
||||
*/
|
||||
public BinaryGraphicsDocument(String name, String sFileExtension, String sMimeType) {
|
||||
this.sFileExtension = sFileExtension;
|
||||
this.sMimeType = sMimeType;
|
||||
docName = trimDocumentName(name);
|
||||
sFileName = Misc.trimDocumentName(name, sFileExtension);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>This method reads <code>byte</code> data from the InputStream.</p>
|
||||
|
||||
/** Set image contents to a byte array
|
||||
*
|
||||
* @param is InputStream containing a binary data file.
|
||||
*
|
||||
* @throws IOException In case of any I/O errors.
|
||||
* @param data the image data
|
||||
*/
|
||||
public void read(InputStream is) throws IOException {
|
||||
data = Misc.inputStreamToByteArray(is);
|
||||
}
|
||||
|
||||
public void read(byte[] data) {
|
||||
read(data,0,data.length);
|
||||
public void setData(byte[] data, boolean bIsAcceptedFormat) {
|
||||
setData(data,0,data.length,bIsAcceptedFormat);
|
||||
}
|
||||
|
||||
public void read(byte[] data, int nOff, int nLen) {
|
||||
this.data = data;
|
||||
/** Set image contents to part of a byte array
|
||||
*
|
||||
* @param data the image data
|
||||
* @param nOff the offset into the byte array
|
||||
* @param nLen the number of bytes to use
|
||||
* @param bIsAcceptedFormat flag to indicate that the format of the image is acceptable for the converter
|
||||
*/
|
||||
public void setData(byte[] data, int nOff, int nLen, boolean bIsAcceptedFormat) {
|
||||
this.blob = data;
|
||||
this.nOff = nOff;
|
||||
this.nLen = nLen;
|
||||
this.bIsAcceptedFormat = bIsAcceptedFormat;
|
||||
this.bIsLinked = false;
|
||||
this.sURL = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Utility method to make sure the document name is stripped of any file
|
||||
* extensions before use.
|
||||
/** Set the URL of a linked image
|
||||
*
|
||||
* @param sURL the URL
|
||||
*/
|
||||
private String trimDocumentName(String name) {
|
||||
String temp = name.toLowerCase();
|
||||
|
||||
if (temp.endsWith(getFileExtension())) {
|
||||
// strip the extension
|
||||
int nlen = name.length();
|
||||
int endIndex = nlen - getFileExtension().length();
|
||||
name = name.substring(0,endIndex);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns the <code>Document</code> name with no file extension.</p>
|
||||
*
|
||||
* @return The <code>Document</code> name with no file extension.
|
||||
*/
|
||||
public String getName() {
|
||||
return docName;
|
||||
public void setURL(String sURL) {
|
||||
this.blob = null;
|
||||
this.nOff = 0;
|
||||
this.nLen = 0;
|
||||
this.bIsAcceptedFormat = false; // or rather don't know
|
||||
this.bIsLinked = true;
|
||||
this.sURL = sURL;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns the <code>Document</code> name with file extension.</p>
|
||||
*
|
||||
* @return The <code>Document</code> name with file extension.
|
||||
/** Get the URL of a linked image
|
||||
*
|
||||
* @return the URL or null if this is an embedded image
|
||||
*/
|
||||
public String getFileName() {
|
||||
return new String(docName + getFileExtension());
|
||||
public String getURL() {
|
||||
return sURL;
|
||||
}
|
||||
|
||||
/** Does this <code>BinaryGraphicsDocument</code> represent a linked image?
|
||||
*
|
||||
* @return true if so
|
||||
*/
|
||||
public boolean isLinked() {
|
||||
return bIsLinked;
|
||||
}
|
||||
|
||||
/** Is this image in an acceptable format for the converter?
|
||||
*
|
||||
* @return true if so (always returns false for linked images)
|
||||
*/
|
||||
public boolean isAcceptedFormat() {
|
||||
return bIsAcceptedFormat;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
return blob;
|
||||
}
|
||||
|
||||
// Implement OutputFile
|
||||
|
||||
/**
|
||||
* <p>Writes out the <code>Document</code> content to the specified
|
||||
* <code>OutputStream</code>.</p>
|
||||
/** Writes out the content to the specified <code>OutputStream</code>.
|
||||
* Linked images will not write any data.
|
||||
*
|
||||
* <p>This method may not be thread-safe.
|
||||
* Implementations may or may not synchronize this
|
||||
* method. User code (i.e. caller) must make sure that
|
||||
* calls to this method are thread-safe.</p>
|
||||
*
|
||||
* @param os <code>OutputStream</code> to write out the
|
||||
* <code>Document</code> content.
|
||||
* @param os <code>OutputStream</code> to write out the content.
|
||||
*
|
||||
* @throws IOException If any I/O error occurs.
|
||||
*/
|
||||
public void write(OutputStream os) throws IOException {
|
||||
os.write(data, nOff, nLen);
|
||||
if (blob!=null) {
|
||||
os.write(blob, nOff, nLen);
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the file extension
|
||||
*
|
||||
* @return the file extension
|
||||
*/
|
||||
public String getFileExtension() {
|
||||
return sFileExtension;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file extension for this type of
|
||||
* <code>Document</code>.
|
||||
/** Get the document with file extension.</p>
|
||||
*
|
||||
* @return The document with file extension.
|
||||
*/
|
||||
public String getFileName() {
|
||||
return sFileName + sFileExtension;
|
||||
}
|
||||
|
||||
/** Get the MIME type of the document.
|
||||
*
|
||||
* @return The file extension of <code>Document</code>.
|
||||
* @return The MIME type or null if this is unknown
|
||||
*/
|
||||
public String getFileExtension(){ return sFileExtension; }
|
||||
|
||||
/**
|
||||
* Method to return the MIME type of the document.
|
||||
*
|
||||
* @return String The document's MIME type.
|
||||
*/
|
||||
public String getDocumentMIMEType(){ return sMimeType; }
|
||||
|
||||
|
||||
public String getMIMEType() {
|
||||
return sMimeType;
|
||||
}
|
||||
|
||||
public boolean isMasterDocument() {
|
||||
/** Is this document a master document?
|
||||
*
|
||||
* @return false - a graphics file is never a master document
|
||||
*/
|
||||
public boolean isMasterDocument() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2014-08-27)
|
||||
* Version 1.4 (2014-09-03)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -100,20 +100,20 @@ public abstract class ConverterBase implements Converter {
|
|||
// Read document
|
||||
odDoc = new OfficeDocument();
|
||||
odDoc.read(is);
|
||||
return convert(sTargetFileName);
|
||||
return convert(sTargetFileName,true);
|
||||
}
|
||||
|
||||
public ConverterResult convert(org.w3c.dom.Document dom, String sTargetFileName) throws IOException {
|
||||
public ConverterResult convert(org.w3c.dom.Document dom, String sTargetFileName, boolean bDestructive) throws IOException {
|
||||
// Read document
|
||||
odDoc = new OfficeDocument();
|
||||
odDoc.read(dom);
|
||||
return convert(sTargetFileName);
|
||||
return convert(sTargetFileName,bDestructive);
|
||||
}
|
||||
|
||||
private ConverterResult convert(String sTargetFileName) throws IOException {
|
||||
private ConverterResult convert(String sTargetFileName, boolean bDestructive) throws IOException {
|
||||
ofr = new OfficeReader(odDoc,false);
|
||||
metaData = new MetaData(odDoc);
|
||||
imageConverter = new ImageConverter(odDoc,true);
|
||||
imageConverter = new ImageConverter(ofr,bDestructive,true);
|
||||
imageConverter.setGraphicConverter(graphicConverter);
|
||||
|
||||
// Prepare output
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/************************************************************************
|
||||
*
|
||||
* ImageLoader.java
|
||||
* ImageConverter.java
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
|
@ -16,11 +16,11 @@
|
|||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
||||
* MA 02111-1307 USA
|
||||
*
|
||||
* Copyright: 2002-2012 by Henrik Just
|
||||
* Copyright: 2002-2014 by Henrik Just
|
||||
*
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Version 1.4 (2012-04-03)
|
||||
* Version 1.4 (2014-09-03)
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -30,6 +30,7 @@ import java.text.DecimalFormat;
|
|||
import java.text.NumberFormat;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
|
@ -37,20 +38,22 @@ import writer2latex.api.GraphicConverter;
|
|||
import writer2latex.office.EmbeddedBinaryObject;
|
||||
import writer2latex.office.EmbeddedObject;
|
||||
import writer2latex.office.MIMETypes;
|
||||
import writer2latex.office.OfficeDocument;
|
||||
import writer2latex.office.OfficeReader;
|
||||
import writer2latex.office.SVMReader;
|
||||
import writer2latex.office.XMLString;
|
||||
import writer2latex.util.Base64;
|
||||
import writer2latex.util.Misc;
|
||||
|
||||
/**
|
||||
* <p>This class extracts images from an OOo file.
|
||||
* The images are returned as BinaryGraphicsDocument.</p>
|
||||
/** This class extracts and converts images from an office document.
|
||||
* The images are returned as <code>BinaryGraphicsDocument</code>.
|
||||
* The image converter can be configured as destructive. In this case, the returned
|
||||
* graphics documents will contain the only reference to the image (the original data
|
||||
* will be removed).
|
||||
*/
|
||||
public final class ImageConverter {
|
||||
// The Office document to load images from
|
||||
private OfficeDocument oooDoc;
|
||||
|
||||
private OfficeReader ofr;
|
||||
private boolean bDestructive;
|
||||
|
||||
// Data for file name generation
|
||||
private String sBaseFileName = "";
|
||||
private String sSubDirName = "";
|
||||
|
@ -67,135 +70,210 @@ public final class ImageConverter {
|
|||
private String sDefaultVectorFormat = null;
|
||||
private HashSet<String> acceptedFormats = new HashSet<String>();
|
||||
|
||||
public ImageConverter(OfficeDocument oooDoc, boolean bExtractEPS) {
|
||||
this.oooDoc = oooDoc;
|
||||
/** Construct a new <code>ImageConverter</code> referring to a specific document
|
||||
*
|
||||
* @param doc the office document used
|
||||
* @param bExtractEPS set true if EPS content should be extracted from SVM files
|
||||
*/
|
||||
public ImageConverter(OfficeReader ofr, boolean bDestructive, boolean bExtractEPS) {
|
||||
this.ofr = ofr;
|
||||
this.bDestructive = bDestructive;
|
||||
this.bExtractEPS = bExtractEPS;
|
||||
this.formatter = new DecimalFormat("000");
|
||||
}
|
||||
|
||||
public void setBaseFileName(String sBaseFileName) { this.sBaseFileName = sBaseFileName; }
|
||||
/** Define the base file name to use for generating file names
|
||||
*
|
||||
* @param sBaseFileName the base file name
|
||||
*/
|
||||
public void setBaseFileName(String sBaseFileName) {
|
||||
this.sBaseFileName = sBaseFileName;
|
||||
}
|
||||
|
||||
public void setUseSubdir(String sSubDirName) { this.sSubDirName = sSubDirName+"/"; }
|
||||
/** Define the name of a sub directory to prepend to file names
|
||||
*
|
||||
* @param sSubDirName the sub directory
|
||||
*/
|
||||
public void setUseSubdir(String sSubDirName) {
|
||||
this.sSubDirName = sSubDirName+"/";
|
||||
}
|
||||
|
||||
public void setAcceptOtherFormats(boolean b) { bAcceptOtherFormats = b; }
|
||||
/** Specify that the <code>ImageConverter</code> should return an image even if it was not possible
|
||||
* to convert it to an acceptable format.
|
||||
*
|
||||
* @param b true if other formats should be accepted
|
||||
*/
|
||||
public void setAcceptOtherFormats(boolean b) {
|
||||
bAcceptOtherFormats = b;
|
||||
}
|
||||
|
||||
/** Define the default format for raster graphics
|
||||
*
|
||||
* @param sMime the MIME type of the default raster format
|
||||
*/
|
||||
public void setDefaultFormat(String sMime) {
|
||||
addAcceptedFormat(sMime);
|
||||
sDefaultFormat = sMime;
|
||||
}
|
||||
|
||||
/** Define the default format for vector graphics
|
||||
*
|
||||
* @param sMime the MIME type for the default vector format
|
||||
*/
|
||||
public void setDefaultVectorFormat(String sMime) {
|
||||
addAcceptedFormat(sMime);
|
||||
sDefaultVectorFormat = sMime;
|
||||
}
|
||||
|
||||
public void addAcceptedFormat(String sMime) { acceptedFormats.add(sMime); }
|
||||
/** Define an accepted graphics format
|
||||
*
|
||||
* @param sMime the MIME type of the format
|
||||
*/
|
||||
public void addAcceptedFormat(String sMime) {
|
||||
acceptedFormats.add(sMime);
|
||||
}
|
||||
|
||||
private boolean isAcceptedFormat(String sMime) { return acceptedFormats.contains(sMime); }
|
||||
/** Is a given format accepted?
|
||||
*
|
||||
* @param sMime the MIME type to query
|
||||
* @return true if this is an accepted format
|
||||
*/
|
||||
private boolean isAcceptedFormat(String sMime) {
|
||||
return acceptedFormats.contains(sMime);
|
||||
}
|
||||
|
||||
public void setGraphicConverter(GraphicConverter gcv) { this.gcv = gcv; }
|
||||
/** Define the <code>GraphicConverter</code> to use for image conversion
|
||||
*
|
||||
* @param gcv the graphics converter
|
||||
*/
|
||||
public void setGraphicConverter(GraphicConverter gcv) {
|
||||
this.gcv = gcv;
|
||||
}
|
||||
|
||||
public BinaryGraphicsDocument getImage(Node node) {
|
||||
// node must be a draw:image element.
|
||||
// variables to hold data about the image:
|
||||
String sMIME = null;
|
||||
String sExt = null;
|
||||
byte[] blob = null;
|
||||
/** Get an image from a <code>draw:image</code> element. If the converter is destructive, the returned
|
||||
* <code>BinaryGraphicsDocument</code> will hold the only reference to the image data (the original
|
||||
* data will be removed).
|
||||
*
|
||||
* @param node the image element
|
||||
* @return a document containing the (converted) image, or null if it was not possible to read the image
|
||||
* or convert it to an accepted format
|
||||
*/
|
||||
public BinaryGraphicsDocument getImage(Element node) {
|
||||
assert(XMLString.DRAW_IMAGE.equals(node.getTagName()));
|
||||
|
||||
String sHref = Misc.getAttribute(node,XMLString.XLINK_HREF);
|
||||
if (sHref==null || sHref.length()==0) {
|
||||
// Image must be contained in an office:binary-element as base64:
|
||||
Node obd = Misc.getChildByTagName(node,XMLString.OFFICE_BINARY_DATA);
|
||||
if (obd!=null) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
NodeList nl = obd.getChildNodes();
|
||||
int nLen = nl.getLength();
|
||||
for (int i=0; i<nLen; i++) {
|
||||
if (nl.item(i).getNodeType()==Node.TEXT_NODE) {
|
||||
buf.append(nl.item(i).getNodeValue());
|
||||
}
|
||||
}
|
||||
blob = Base64.decode(buf.toString());
|
||||
sMIME = MIMETypes.getMagicMIMEType(blob);
|
||||
sExt = MIMETypes.getFileExtension(sMIME);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Image may be embedded in package:
|
||||
if (sHref.startsWith("#")) { sHref = sHref.substring(1); }
|
||||
if (sHref.startsWith("./")) { sHref = sHref.substring(2); }
|
||||
EmbeddedObject obj = oooDoc.getEmbeddedObject(sHref);
|
||||
if (obj!=null && obj instanceof EmbeddedBinaryObject) {
|
||||
EmbeddedBinaryObject object = (EmbeddedBinaryObject) obj;
|
||||
blob = object.getBinaryData();
|
||||
sMIME = object.getType();
|
||||
if (sMIME.length()>0) {
|
||||
// If the manifest provides a media type, trust that
|
||||
sExt = MIMETypes.getFileExtension(sMIME);
|
||||
}
|
||||
else {
|
||||
// Otherwise determine it by byte inspection
|
||||
sMIME = MIMETypes.getMagicMIMEType(blob);
|
||||
sExt = MIMETypes.getFileExtension(sMIME);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// This is a linked image
|
||||
// TODO: Perhaps we should download the image from the url in sHref?
|
||||
// Alternatively BinaryGraphicsDocument should be extended to
|
||||
// handle external graphics.
|
||||
}
|
||||
}
|
||||
|
||||
if (blob==null) { return null; }
|
||||
|
||||
// Assign a name (without extension)
|
||||
// Image data
|
||||
String sMIME = null;
|
||||
String sExt = null;
|
||||
byte[] blob = null;
|
||||
|
||||
// First try to extract the image using the xlink:href attribute
|
||||
if (node.hasAttribute(XMLString.XLINK_HREF)) {
|
||||
String sHref = node.getAttribute(XMLString.XLINK_HREF);
|
||||
if (sHref.length()>0) {
|
||||
// Image may be embedded in package:
|
||||
String sPath = sHref;
|
||||
if (sPath.startsWith("#")) { sPath = sPath.substring(1); }
|
||||
if (sPath.startsWith("./")) { sPath = sPath.substring(2); }
|
||||
EmbeddedObject obj = ofr.getEmbeddedObject(sPath);
|
||||
if (obj!=null && obj instanceof EmbeddedBinaryObject) {
|
||||
EmbeddedBinaryObject object = (EmbeddedBinaryObject) obj;
|
||||
blob = object.getBinaryData();
|
||||
sMIME = object.getType();
|
||||
if (sMIME.length()==0) {
|
||||
// If the manifest provides a media type, trust that
|
||||
// Otherwise determine it by byte inspection
|
||||
sMIME = MIMETypes.getMagicMIMEType(blob);
|
||||
}
|
||||
sExt = MIMETypes.getFileExtension(sMIME);
|
||||
if (bDestructive) {
|
||||
object.dispose();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// This is a linked image
|
||||
// TODO: Add option to download image from the URL?
|
||||
BinaryGraphicsDocument bgd
|
||||
= new BinaryGraphicsDocument(Misc.getFileName(sHref),Misc.getFileExtension(sHref),null);
|
||||
bgd.setURL(ofr.fixRelativeLink(sHref));
|
||||
return bgd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If there is no suitable xlink:href attribute, the image must be contained in an office:binary-element as base64
|
||||
if (blob==null) {
|
||||
Node obd = Misc.getChildByTagName(node,XMLString.OFFICE_BINARY_DATA);
|
||||
if (obd!=null) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
NodeList nl = obd.getChildNodes();
|
||||
int nLen = nl.getLength();
|
||||
for (int i=0; i<nLen; i++) {
|
||||
if (nl.item(i).getNodeType()==Node.TEXT_NODE) {
|
||||
buf.append(nl.item(i).getNodeValue());
|
||||
}
|
||||
}
|
||||
blob = Base64.decode(buf.toString());
|
||||
sMIME = MIMETypes.getMagicMIMEType(blob);
|
||||
sExt = MIMETypes.getFileExtension(sMIME);
|
||||
if (bDestructive) {
|
||||
node.removeChild(obd);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// There is no image data
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// We have an embedded image. Assign a name (without extension)
|
||||
String sName = sSubDirName+sBaseFileName+formatter.format(++nImageCount);
|
||||
|
||||
BinaryGraphicsDocument bgd = null;
|
||||
|
||||
if (bExtractEPS && MIMETypes.SVM.equals(MIMETypes.getMagicMIMEType(blob))) {
|
||||
// Is this an EPS file embedded in an SVM file?
|
||||
if (bExtractEPS && MIMETypes.SVM.equals(sMIME)) {
|
||||
// Look for postscript:
|
||||
int[] offlen = new int[2];
|
||||
if (SVMReader.readSVM(blob,offlen)) {
|
||||
bgd = new BinaryGraphicsDocument(sName,
|
||||
MIMETypes.EPS_EXT,MIMETypes.EPS);
|
||||
bgd.read(blob,offlen[0],offlen[1]);
|
||||
BinaryGraphicsDocument bgd
|
||||
= new BinaryGraphicsDocument(sName,MIMETypes.EPS_EXT,MIMETypes.EPS);
|
||||
bgd.setData(blob,offlen[0],offlen[1],true);
|
||||
return bgd;
|
||||
}
|
||||
}
|
||||
|
||||
// If we have a converter AND a default format AND this image
|
||||
// is not in an accepted format AND the converter knows how to
|
||||
// convert it - try to convert...
|
||||
if (gcv!=null && !isAcceptedFormat(sMIME) && sDefaultFormat!=null) {
|
||||
byte[] newBlob = null;
|
||||
String sTargetMIME = null;
|
||||
|
||||
if (bgd==null) {
|
||||
// If we have a converter AND a default format AND this image
|
||||
// is not in an accepted format AND the converter knows how to
|
||||
// convert it - try to convert...
|
||||
if (gcv!=null && !isAcceptedFormat(sMIME) && sDefaultFormat!=null) {
|
||||
byte[] newBlob = null;
|
||||
String sTargetMIME = null;
|
||||
|
||||
if (MIMETypes.isVectorFormat(sMIME) && sDefaultVectorFormat!=null &&
|
||||
gcv.supportsConversion(sMIME,sDefaultVectorFormat,false,false)) {
|
||||
// Try vector format first
|
||||
newBlob = gcv.convert(blob, sMIME, sTargetMIME=sDefaultVectorFormat);
|
||||
}
|
||||
if (newBlob==null && gcv.supportsConversion(sMIME,sDefaultFormat,false,false)) {
|
||||
// Then try bitmap format
|
||||
newBlob = gcv.convert(blob,sMIME,sTargetMIME=sDefaultFormat);
|
||||
}
|
||||
|
||||
if (newBlob!=null) {
|
||||
// Conversion successful - create new data
|
||||
blob = newBlob;
|
||||
sMIME = sTargetMIME;
|
||||
sExt = MIMETypes.getFileExtension(sMIME);
|
||||
}
|
||||
if (MIMETypes.isVectorFormat(sMIME) && sDefaultVectorFormat!=null &&
|
||||
gcv.supportsConversion(sMIME,sDefaultVectorFormat,false,false)) {
|
||||
// Try vector format first
|
||||
newBlob = gcv.convert(blob, sMIME, sTargetMIME=sDefaultVectorFormat);
|
||||
}
|
||||
if (newBlob==null && gcv.supportsConversion(sMIME,sDefaultFormat,false,false)) {
|
||||
// Then try bitmap format
|
||||
newBlob = gcv.convert(blob,sMIME,sTargetMIME=sDefaultFormat);
|
||||
}
|
||||
|
||||
if (isAcceptedFormat(sMIME) || bAcceptOtherFormats) {
|
||||
bgd = new BinaryGraphicsDocument(sName,sExt,sMIME);
|
||||
bgd.read(blob);
|
||||
if (newBlob!=null) {
|
||||
// Conversion successful - create new data
|
||||
blob = newBlob;
|
||||
sMIME = sTargetMIME;
|
||||
sExt = MIMETypes.getFileExtension(sMIME);
|
||||
}
|
||||
}
|
||||
|
||||
return bgd;
|
||||
|
||||
// Create the result
|
||||
if (isAcceptedFormat(sMIME) || bAcceptOtherFormats) {
|
||||
BinaryGraphicsDocument bgd = new BinaryGraphicsDocument(sName,sExt,sMIME);
|
||||
bgd.setData(blob,isAcceptedFormat(sMIME));
|
||||
return bgd;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue