aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache
diff options
context:
space:
mode:
Diffstat (limited to 'src/java/org/apache')
-rw-r--r--src/java/org/apache/poi/ddf/EscherMetafileBlip.java41
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java49
2 files changed, 71 insertions, 19 deletions
diff --git a/src/java/org/apache/poi/ddf/EscherMetafileBlip.java b/src/java/org/apache/poi/ddf/EscherMetafileBlip.java
index 37f4054694..428d11e4e9 100644
--- a/src/java/org/apache/poi/ddf/EscherMetafileBlip.java
+++ b/src/java/org/apache/poi/ddf/EscherMetafileBlip.java
@@ -21,6 +21,7 @@ import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.hssf.usermodel.HSSFPictureData;
import java.awt.Dimension;
import java.awt.Rectangle;
@@ -28,6 +29,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;
+import java.util.zip.DeflaterOutputStream;
/**
* @author Daniel Noll
@@ -39,13 +41,6 @@ public final class EscherMetafileBlip extends EscherBlipRecord {
public static final short RECORD_ID_WMF = (short) 0xF018 + 3;
public static final short RECORD_ID_PICT = (short) 0xF018 + 4;
- /**
- * BLIP signatures as defined in the escher spec
- */
- public static final short SIGNATURE_EMF = 0x3D40;
- public static final short SIGNATURE_WMF = 0x2160;
- public static final short SIGNATURE_PICT = 0x5420;
-
private static final int HEADER_SIZE = 8;
private byte[] field_1_UID;
@@ -288,11 +283,37 @@ public final class EscherMetafileBlip extends EscherBlipRecord {
*/
public short getSignature() {
switch (getRecordId()) {
- case RECORD_ID_EMF: return SIGNATURE_EMF;
- case RECORD_ID_WMF: return SIGNATURE_WMF;
- case RECORD_ID_PICT: return SIGNATURE_PICT;
+ case RECORD_ID_EMF: return HSSFPictureData.MSOBI_EMF;
+ case RECORD_ID_WMF: return HSSFPictureData.MSOBI_WMF;
+ case RECORD_ID_PICT: return HSSFPictureData.MSOBI_PICT;
}
log.log(POILogger.WARN, "Unknown metafile: " + getRecordId());
return 0;
}
+
+ public void setPictureData(byte[] pictureData) {
+ super.setPictureData(pictureData);
+ setUncompressedSize(pictureData.length);
+
+ // info of chicago project:
+ // "... LZ compression algorithm in the format used by GNU Zip deflate/inflate with a 32k window ..."
+ // not sure what to do, when lookup tables exceed 32k ...
+
+ try {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ DeflaterOutputStream dos = new DeflaterOutputStream(bos);
+ dos.write(pictureData);
+ dos.close();
+ raw_pictureData = bos.toByteArray();
+ } catch (IOException e) {
+ throw new RuntimeException("Can't compress metafile picture data", e);
+ }
+
+ setCompressedSize(raw_pictureData.length);
+ setCompressed(true);
+ }
+
+ public void setFilter(byte filter) {
+ field_7_fFilter = filter;
+ }
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
index cfbfdc43a2..9e42beab98 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
@@ -34,6 +34,7 @@ import org.apache.poi.POIDocument;
import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.ddf.EscherBitmapBlip;
import org.apache.poi.ddf.EscherBlipRecord;
+import org.apache.poi.ddf.EscherMetafileBlip;
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.hssf.OldExcelFormatException;
import org.apache.poi.hssf.model.DrawingManager2;
@@ -57,6 +58,7 @@ import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.Configurator;
+import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@@ -1587,7 +1589,40 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
initDrawings();
byte[] uid = DigestUtils.md5(pictureData);
- EscherBitmapBlip blipRecord = new EscherBitmapBlip();
+ EscherBlipRecord blipRecord;
+ int blipSize;
+ short escherTag;
+ switch (format) {
+ case PICTURE_TYPE_WMF:
+ // remove first 22 bytes if file starts with magic bytes D7-CD-C6-9A
+ // see also http://de.wikipedia.org/wiki/Windows_Metafile#Hinweise_zur_WMF-Spezifikation
+ if (LittleEndian.getInt(pictureData) == 0x9AC6CDD7) {
+ byte picDataNoHeader[] = new byte[pictureData.length-22];
+ System.arraycopy(pictureData, 22, picDataNoHeader, 0, pictureData.length-22);
+ pictureData = picDataNoHeader;
+ }
+ // fall through
+ case PICTURE_TYPE_EMF:
+ EscherMetafileBlip blipRecordMeta = new EscherMetafileBlip();
+ blipRecord = blipRecordMeta;
+ blipRecordMeta.setUID(uid);
+ blipRecordMeta.setPictureData(pictureData);
+ // taken from libre office export, it won't open, if this is left to 0
+ blipRecordMeta.setFilter((byte)-2);
+ blipSize = blipRecordMeta.getCompressedSize() + 58;
+ escherTag = 0;
+ break;
+ default:
+ EscherBitmapBlip blipRecordBitmap = new EscherBitmapBlip();
+ blipRecord = blipRecordBitmap;
+ blipRecordBitmap.setUID( uid );
+ blipRecordBitmap.setMarker( (byte) 0xFF );
+ blipRecordBitmap.setPictureData( pictureData );
+ blipSize = pictureData.length + 25;
+ escherTag = (short) 0xFF;
+ break;
+ }
+
blipRecord.setRecordId( (short) ( EscherBitmapBlip.RECORD_ID_START + format ) );
switch (format)
{
@@ -1610,23 +1645,19 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
blipRecord.setOptions(HSSFPictureData.MSOBI_DIB);
break;
}
-
- blipRecord.setUID( uid );
- blipRecord.setMarker( (byte) 0xFF );
- blipRecord.setPictureData( pictureData );
-
+
EscherBSERecord r = new EscherBSERecord();
r.setRecordId( EscherBSERecord.RECORD_ID );
r.setOptions( (short) ( 0x0002 | ( format << 4 ) ) );
r.setBlipTypeMacOS( (byte) format );
r.setBlipTypeWin32( (byte) format );
r.setUid( uid );
- r.setTag( (short) 0xFF );
- r.setSize( pictureData.length + 25 );
+ r.setTag( escherTag );
+ r.setSize( blipSize );
r.setRef( 0 );
r.setOffset( 0 );
r.setBlipRecord( blipRecord );
-
+
return workbook.addBSERecord( r );
}