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;
+ /**
+ * The primary UID is only saved to disk if (blip_instance ^ blip_signature == 1)
+ */
+ private byte[] field_2_UID;
private int field_2_cb;
private int field_3_rcBounds_x1;
private int field_3_rcBounds_y1;
field_1_UID = new byte[16];
System.arraycopy( data, pos, field_1_UID, 0, 16 ); pos += 16;
+
+ if((getOptions() ^ getSignature()) == 0x10){
+ field_2_UID = new byte[16];
+ System.arraycopy( data, pos, field_2_UID, 0, 16 ); pos += 16;
+ }
+
field_2_cb = LittleEndian.getInt( data, pos ); pos += 4;
field_3_rcBounds_x1 = LittleEndian.getInt( data, pos ); pos += 4;
field_3_rcBounds_y1 = LittleEndian.getInt( data, pos ); pos += 4;
field_6_fCompression = data[pos]; pos++;
field_7_fFilter = data[pos]; pos++;
- // Bit of a snag - trusting field_5_cbSave results in inconsistent
- // record size in some cases. So, just check the data left
- int remainingBytes = bytesAfterHeader - 50;
- raw_pictureData = new byte[remainingBytes];
- System.arraycopy( data, pos, raw_pictureData, 0, remainingBytes );
+ raw_pictureData = new byte[field_5_cbSave];
+ System.arraycopy( data, pos, raw_pictureData, 0, field_5_cbSave );
// 0 means DEFLATE compression
// 0xFE means no compression
int pos = offset;
LittleEndian.putShort( data, pos, getOptions() ); pos += 2;
LittleEndian.putShort( data, pos, getRecordId() ); pos += 2;
- LittleEndian.putInt( data, getRecordSize() - HEADER_SIZE ); pos += 4;
+ LittleEndian.putInt( data, pos, getRecordSize() - HEADER_SIZE ); pos += 4;
- System.arraycopy( field_1_UID, 0, data, pos, 16 ); pos += 16;
+ System.arraycopy( field_1_UID, 0, data, pos, field_1_UID.length ); pos += field_1_UID.length;
+ if((getOptions() ^ getSignature()) == 0x10){
+ System.arraycopy( field_2_UID, 0, data, pos, field_2_UID.length ); pos += field_2_UID.length;
+ }
LittleEndian.putInt( data, pos, field_2_cb ); pos += 4;
LittleEndian.putInt( data, pos, field_3_rcBounds_x1 ); pos += 4;
LittleEndian.putInt( data, pos, field_3_rcBounds_y1 ); pos += 4;
System.arraycopy( raw_pictureData, 0, data, pos, raw_pictureData.length );
listener.afterRecordSerialize(offset + getRecordSize(), getRecordId(), getRecordSize(), this);
- return HEADER_SIZE + 16 + 1 + raw_pictureData.length;
+ return getRecordSize();
}
/**
}
catch ( IOException e )
{
- log.log(POILogger.INFO, "Possibly corrupt compression or non-compressed data", e);
+ log.log(POILogger.WARN, "Possibly corrupt compression or non-compressed data", e);
return data;
}
}
*/
public int getRecordSize()
{
- return 8 + 50 + raw_pictureData.length;
+ int size = 8 + 50 + raw_pictureData.length;
+ if((getOptions() ^ getSignature()) == 0x10){
+ size += field_2_UID.length;
+ }
+ return size;
}
public byte[] getUID()
this.field_1_UID = field_1_UID;
}
+ public byte[] getPrimaryUID()
+ {
+ return field_2_UID;
+ }
+
+ public void setPrimaryUID( byte[] field_2_UID )
+ {
+ this.field_2_UID = field_2_UID;
+ }
+
public int getUncompressedSize()
{
return field_2_cb;
" RecordId: 0x" + HexDump.toHex( getRecordId() ) + nl +
" Options: 0x" + HexDump.toHex( getOptions() ) + nl +
" UID: 0x" + HexDump.toHex( field_1_UID ) + nl +
+ (field_2_UID == null ? "" : (" UID2: 0x" + HexDump.toHex( field_2_UID ) + nl)) +
" Uncompressed Size: " + HexDump.toHex( field_2_cb ) + nl +
" Bounds: " + getBounds() + nl +
" Size in EMU: " + getSizeEMU() + nl +
" Extra Data:" + nl + extraData;
}
+ /**
+ * Return the blip signature
+ *
+ * @return the blip signature
+ */
+ public short getSignature(){
+ short sig = 0;
+ switch(getRecordId()){
+ case RECORD_ID_EMF: sig = SIGNATURE_EMF; break;
+ case RECORD_ID_WMF: sig = SIGNATURE_WMF; break;
+ case RECORD_ID_PICT: sig = SIGNATURE_PICT; break;
+ default: log.log(POILogger.WARN, "Unknown metafile: " + getRecordId()); break;
+ }
+ return sig;
+ }
}
--- /dev/null
+\r
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+ \r
+package org.apache.poi.ddf;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.util.HexRead;\r
+import org.apache.poi.util.HexDump;\r
+\r
+import java.io.IOException;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.util.Iterator;\r
+import java.util.Arrays;\r
+\r
+/**\r
+ * Test read/serialize of escher blip records\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestEscherBlipRecord extends TestCase\r
+{\r
+ protected String cwd = System.getProperty("DDF.testdata.path");\r
+\r
+ //test reading/serializing of a PNG blip\r
+ public void testReadPNG() throws IOException {\r
+ //provided in bug-44886\r
+ byte[] data = read(new File(cwd, "Container.dat"));\r
+\r
+ EscherContainerRecord record = new EscherContainerRecord();\r
+ record.fillFields(data, 0, new DefaultEscherRecordFactory());\r
+ EscherContainerRecord bstore = (EscherContainerRecord)record.getChildRecords().get(1);\r
+ EscherBSERecord bse1 = (EscherBSERecord)bstore.getChildRecords().get(0);\r
+ assertEquals(EscherBSERecord.BT_PNG, bse1.getBlipTypeWin32());\r
+ assertEquals(EscherBSERecord.BT_PNG, bse1.getBlipTypeMacOS());\r
+ assertTrue(Arrays.equals(new byte[]{\r
+ 0x65, 0x07, 0x4A, (byte)0x8D, 0x3E, 0x42, (byte)0x8B, (byte)0xAC,\r
+ 0x1D, (byte)0x89, 0x35, 0x4F, 0x48, (byte)0xFA, 0x37, (byte)0xC2\r
+ }, bse1.getUid()));\r
+ assertEquals(255, bse1.getTag());\r
+ assertEquals(32308, bse1.getSize());\r
+\r
+ EscherBitmapBlip blip1 = (EscherBitmapBlip)bse1.getBlipRecord();\r
+ assertEquals(0x6E00, blip1.getOptions());\r
+ assertEquals(EscherBitmapBlip.RECORD_ID_PNG, blip1.getRecordId());\r
+ assertTrue(Arrays.equals(new byte[]{\r
+ 0x65, 0x07, 0x4A, (byte)0x8D, 0x3E, 0x42, (byte)0x8B, (byte)0xAC,\r
+ 0x1D, (byte)0x89, 0x35, 0x4F, 0x48, (byte)0xFA, 0x37, (byte)0xC2\r
+ }, blip1.getUID()));\r
+\r
+ //serialize and read again\r
+ byte[] ser = bse1.serialize();\r
+ EscherBSERecord bse2 = new EscherBSERecord();\r
+ bse2.fillFields(ser, 0, new DefaultEscherRecordFactory());\r
+ assertEquals(bse1.getRecordId(), bse2.getRecordId());\r
+ assertEquals(bse1.getBlipTypeWin32(), bse2.getBlipTypeWin32());\r
+ assertEquals(bse1.getBlipTypeMacOS(), bse2.getBlipTypeMacOS());\r
+ assertTrue(Arrays.equals(bse1.getUid(), bse2.getUid()));\r
+ assertEquals(bse1.getTag(), bse2.getTag());\r
+ assertEquals(bse1.getSize(), bse2.getSize());\r
+\r
+ EscherBitmapBlip blip2 = (EscherBitmapBlip)bse1.getBlipRecord();\r
+ assertEquals(blip1.getOptions(), blip2.getOptions());\r
+ assertEquals(blip1.getRecordId(), blip2.getRecordId());\r
+ assertEquals(blip1.getUID(), blip2.getUID());\r
+\r
+ assertTrue(Arrays.equals(blip1.getPicturedata(), blip1.getPicturedata()));\r
+ }\r
+\r
+ //test reading/serializing of a PICT metafile\r
+ public void testReadPICT() throws IOException {\r
+ //provided in bug-44886\r
+ byte[] data = read(new File(cwd, "Container.dat"));\r
+\r
+ EscherContainerRecord record = new EscherContainerRecord();\r
+ record.fillFields(data, 0, new DefaultEscherRecordFactory());\r
+ EscherContainerRecord bstore = (EscherContainerRecord)record.getChildRecords().get(1);\r
+ EscherBSERecord bse1 = (EscherBSERecord)bstore.getChildRecords().get(1);\r
+ //System.out.println(bse1);\r
+ assertEquals(EscherBSERecord.BT_WMF, bse1.getBlipTypeWin32());\r
+ assertEquals(EscherBSERecord.BT_PICT, bse1.getBlipTypeMacOS());\r
+ assertTrue(Arrays.equals(new byte[]{\r
+ (byte)0xC7, 0x15, 0x69, 0x2D, (byte)0xE5, (byte)0x89, (byte)0xA3, 0x6F,\r
+ 0x66, 0x03, (byte)0xD6, 0x24, (byte)0xF7, (byte)0xDB, 0x1D, 0x13\r
+ }, bse1.getUid()));\r
+ assertEquals(255, bse1.getTag());\r
+ assertEquals(1133, bse1.getSize());\r
+\r
+ EscherMetafileBlip blip1 = (EscherMetafileBlip)bse1.getBlipRecord();\r
+ assertEquals(0x5430, blip1.getOptions());\r
+ assertEquals(EscherMetafileBlip.RECORD_ID_PICT, blip1.getRecordId());\r
+ assertTrue(Arrays.equals(new byte[]{\r
+ 0x57, 0x32, 0x7B, (byte)0x91, 0x23, 0x5D, (byte)0xDB, 0x36,\r
+ 0x7A, (byte)0xDB, (byte)0xFF, 0x17, (byte)0xFE, (byte)0xF3, (byte)0xA7, 0x05\r
+ }, blip1.getUID()));\r
+ assertTrue(Arrays.equals(new byte[]{\r
+ (byte)0xC7, 0x15, 0x69, 0x2D, (byte)0xE5, (byte)0x89, (byte)0xA3, 0x6F,\r
+ 0x66, 0x03, (byte)0xD6, 0x24, (byte)0xF7, (byte)0xDB, 0x1D, 0x13\r
+ }, blip1.getPrimaryUID()));\r
+\r
+ //serialize and read again\r
+ byte[] ser = bse1.serialize();\r
+ EscherBSERecord bse2 = new EscherBSERecord();\r
+ bse2.fillFields(ser, 0, new DefaultEscherRecordFactory());\r
+ assertEquals(bse1.getRecordId(), bse2.getRecordId());\r
+ assertEquals(bse1.getOptions(), bse2.getOptions());\r
+ assertEquals(bse1.getBlipTypeWin32(), bse2.getBlipTypeWin32());\r
+ assertEquals(bse1.getBlipTypeMacOS(), bse2.getBlipTypeMacOS());\r
+ assertTrue(Arrays.equals(bse1.getUid(), bse2.getUid()));\r
+ assertEquals(bse1.getTag(), bse2.getTag());\r
+ assertEquals(bse1.getSize(), bse2.getSize());\r
+\r
+ EscherMetafileBlip blip2 = (EscherMetafileBlip)bse1.getBlipRecord();\r
+ assertEquals(blip1.getOptions(), blip2.getOptions());\r
+ assertEquals(blip1.getRecordId(), blip2.getRecordId());\r
+ assertEquals(blip1.getUID(), blip2.getUID());\r
+ assertEquals(blip1.getPrimaryUID(), blip2.getPrimaryUID());\r
+\r
+ assertTrue(Arrays.equals(blip1.getPicturedata(), blip1.getPicturedata()));\r
+ }\r
+\r
+ //integral test: check that the read-write-read round trip is consistent\r
+ public void testContainer() throws IOException {\r
+ byte[] data = read(new File(cwd, "Container.dat"));\r
+\r
+ EscherContainerRecord record = new EscherContainerRecord();\r
+ record.fillFields(data, 0, new DefaultEscherRecordFactory());\r
+\r
+ byte[] ser = record.serialize();\r
+ assertTrue(Arrays.equals(data, ser));\r
+ }\r
+\r
+ private byte[] read(File file) throws IOException {\r
+ byte[] data = new byte[(int)file.length()];\r
+ FileInputStream is = new FileInputStream(file);\r
+ is.read(data);\r
+ is.close();\r
+ return data;\r
+ }\r
+\r
+}\r