2009-02-20 09:37:06 +00:00
|
|
|
/************************************************************************
|
|
|
|
*
|
2014-09-03 07:04:31 +00:00
|
|
|
* ImageConverter.java
|
2009-02-20 09:37:06 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*
|
2014-09-03 07:04:31 +00:00
|
|
|
* Copyright: 2002-2014 by Henrik Just
|
2009-02-20 09:37:06 +00:00
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
2014-09-03 07:04:31 +00:00
|
|
|
* Version 1.4 (2014-09-03)
|
2009-02-20 09:37:06 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-27 07:25:22 +00:00
|
|
|
package writer2latex.base;
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2009-12-07 13:29:45 +00:00
|
|
|
import java.text.DecimalFormat;
|
|
|
|
import java.text.NumberFormat;
|
2009-02-20 09:37:06 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
import org.w3c.dom.Element;
|
2009-02-20 09:37:06 +00:00
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
|
|
|
|
import writer2latex.api.GraphicConverter;
|
2014-08-27 07:25:22 +00:00
|
|
|
import writer2latex.office.EmbeddedBinaryObject;
|
|
|
|
import writer2latex.office.EmbeddedObject;
|
|
|
|
import writer2latex.office.MIMETypes;
|
2014-09-03 07:04:31 +00:00
|
|
|
import writer2latex.office.OfficeReader;
|
2014-08-27 07:25:22 +00:00
|
|
|
import writer2latex.office.SVMReader;
|
|
|
|
import writer2latex.office.XMLString;
|
2009-02-20 09:37:06 +00:00
|
|
|
import writer2latex.util.Base64;
|
|
|
|
import writer2latex.util.Misc;
|
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** 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).
|
2009-02-20 09:37:06 +00:00
|
|
|
*/
|
2014-08-27 07:25:22 +00:00
|
|
|
public final class ImageConverter {
|
2014-09-03 07:04:31 +00:00
|
|
|
private OfficeReader ofr;
|
|
|
|
private boolean bDestructive;
|
|
|
|
|
2009-02-20 09:37:06 +00:00
|
|
|
// Data for file name generation
|
2011-02-20 16:06:55 +00:00
|
|
|
private String sBaseFileName = "";
|
|
|
|
private String sSubDirName = "";
|
2009-02-20 09:37:06 +00:00
|
|
|
private int nImageCount = 0;
|
2009-12-07 13:29:45 +00:00
|
|
|
private NumberFormat formatter;
|
2009-02-20 09:37:06 +00:00
|
|
|
|
|
|
|
// should EPS be extracted from SVM?
|
|
|
|
private boolean bExtractEPS;
|
|
|
|
|
|
|
|
// Data for image conversion
|
|
|
|
private GraphicConverter gcv = null;
|
|
|
|
private boolean bAcceptOtherFormats = true;
|
|
|
|
private String sDefaultFormat = null;
|
|
|
|
private String sDefaultVectorFormat = null;
|
2009-03-30 07:38:37 +00:00
|
|
|
private HashSet<String> acceptedFormats = new HashSet<String>();
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** Construct a new <code>ImageConverter</code> referring to a specific document
|
|
|
|
*
|
2014-09-04 17:47:49 +00:00
|
|
|
* @param ofr the office reader to use
|
2014-09-03 07:04:31 +00:00
|
|
|
* @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;
|
2009-02-20 09:37:06 +00:00
|
|
|
this.bExtractEPS = bExtractEPS;
|
2009-12-07 13:29:45 +00:00
|
|
|
this.formatter = new DecimalFormat("000");
|
2009-02-20 09:37:06 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** 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;
|
|
|
|
}
|
2011-02-20 16:06:55 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** 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+"/";
|
|
|
|
}
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** 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;
|
|
|
|
}
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** Define the default format for raster graphics
|
|
|
|
*
|
|
|
|
* @param sMime the MIME type of the default raster format
|
|
|
|
*/
|
2009-02-20 09:37:06 +00:00
|
|
|
public void setDefaultFormat(String sMime) {
|
|
|
|
addAcceptedFormat(sMime);
|
|
|
|
sDefaultFormat = sMime;
|
|
|
|
}
|
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** Define the default format for vector graphics
|
|
|
|
*
|
|
|
|
* @param sMime the MIME type for the default vector format
|
|
|
|
*/
|
2009-02-20 09:37:06 +00:00
|
|
|
public void setDefaultVectorFormat(String sMime) {
|
|
|
|
addAcceptedFormat(sMime);
|
|
|
|
sDefaultVectorFormat = sMime;
|
|
|
|
}
|
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** Define an accepted graphics format
|
|
|
|
*
|
|
|
|
* @param sMime the MIME type of the format
|
|
|
|
*/
|
|
|
|
public void addAcceptedFormat(String sMime) {
|
|
|
|
acceptedFormats.add(sMime);
|
|
|
|
}
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** 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);
|
|
|
|
}
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** Define the <code>GraphicConverter</code> to use for image conversion
|
|
|
|
*
|
|
|
|
* @param gcv the graphics converter
|
|
|
|
*/
|
|
|
|
public void setGraphicConverter(GraphicConverter gcv) {
|
|
|
|
this.gcv = gcv;
|
|
|
|
}
|
2009-12-07 13:29:45 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
/** 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()));
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
// 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)
|
2011-02-20 16:06:55 +00:00
|
|
|
String sName = sSubDirName+sBaseFileName+formatter.format(++nImageCount);
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
// Is this an EPS file embedded in an SVM file?
|
|
|
|
if (bExtractEPS && MIMETypes.SVM.equals(sMIME)) {
|
2009-02-20 09:37:06 +00:00
|
|
|
// Look for postscript:
|
|
|
|
int[] offlen = new int[2];
|
|
|
|
if (SVMReader.readSVM(blob,offlen)) {
|
2014-09-03 07:04:31 +00:00
|
|
|
BinaryGraphicsDocument bgd
|
|
|
|
= new BinaryGraphicsDocument(sName,MIMETypes.EPS_EXT,MIMETypes.EPS);
|
|
|
|
bgd.setData(blob,offlen[0],offlen[1],true);
|
|
|
|
return bgd;
|
2009-02-20 09:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-03 07:04:31 +00:00
|
|
|
|
|
|
|
// 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;
|
2009-02-20 09:37:06 +00:00
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
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);
|
2009-02-20 09:37:06 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 07:04:31 +00:00
|
|
|
if (newBlob!=null) {
|
|
|
|
// Conversion successful - create new data
|
|
|
|
blob = newBlob;
|
|
|
|
sMIME = sTargetMIME;
|
|
|
|
sExt = MIMETypes.getFileExtension(sMIME);
|
2009-02-20 09:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-03 07:04:31 +00:00
|
|
|
|
|
|
|
// Create the result
|
|
|
|
if (isAcceptedFormat(sMIME) || bAcceptOtherFormats) {
|
|
|
|
BinaryGraphicsDocument bgd = new BinaryGraphicsDocument(sName,sExt,sMIME);
|
|
|
|
bgd.setData(blob,isAcceptedFormat(sMIME));
|
|
|
|
return bgd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
2009-02-20 09:37:06 +00:00
|
|
|
}
|
2014-09-03 07:04:31 +00:00
|
|
|
|
2009-02-20 09:37:06 +00:00
|
|
|
}
|