View | Details | Raw Unified | Return to bug 49658
Collapse All | Expand All

(-)src/java/org/apache/poi/ddf/EscherMetafileBlip.java (-5 / +32 lines)
Lines 17-34 Link Here
17
17
18
package org.apache.poi.ddf;
18
package org.apache.poi.ddf;
19
19
20
import org.apache.poi.util.HexDump;
21
import org.apache.poi.util.LittleEndian;
22
import org.apache.poi.util.POILogFactory;
23
import org.apache.poi.util.POILogger;
24
25
import java.awt.Dimension;
20
import java.awt.Dimension;
26
import java.awt.Rectangle;
21
import java.awt.Rectangle;
27
import java.io.ByteArrayInputStream;
22
import java.io.ByteArrayInputStream;
28
import java.io.ByteArrayOutputStream;
23
import java.io.ByteArrayOutputStream;
29
import java.io.IOException;
24
import java.io.IOException;
25
import java.util.zip.DeflaterOutputStream;
30
import java.util.zip.InflaterInputStream;
26
import java.util.zip.InflaterInputStream;
31
27
28
import org.apache.poi.util.HexDump;
29
import org.apache.poi.util.LittleEndian;
30
import org.apache.poi.util.POILogFactory;
31
import org.apache.poi.util.POILogger;
32
32
/**
33
/**
33
 * @author Daniel Noll
34
 * @author Daniel Noll
34
 */
35
 */
Lines 295-298 Link Here
295
        log.log(POILogger.WARN, "Unknown metafile: " + getRecordId());
296
        log.log(POILogger.WARN, "Unknown metafile: " + getRecordId());
296
        return 0;
297
        return 0;
297
    }
298
    }
299
300
    public void setPictureData(byte[] pictureData) {
301
    	super.setPictureData(pictureData);
302
        setUncompressedSize(pictureData.length);
303
304
        // info of chicago project:
305
        // "... LZ compression algorithm in the format used by GNU Zip deflate/inflate with a 32k window ..."
306
        // not sure what to do, when lookup tables exceed 32k ...
307
308
        try {
309
	        ByteArrayOutputStream bos = new ByteArrayOutputStream();
310
	        DeflaterOutputStream dos = new DeflaterOutputStream(bos);
311
	        dos.write(pictureData);
312
	        dos.close();
313
	        raw_pictureData = bos.toByteArray();
314
        } catch (IOException e) {
315
        	throw new RuntimeException("Can't compress metafile picture data", e);
316
        }
317
        
318
        setCompressedSize(raw_pictureData.length);
319
        setCompressed(true);
320
    }
321
322
    public void setFilter(byte filter) {
323
    	field_7_fFilter = filter;
324
    }
298
}
325
}
(-)src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (-9 / +27 lines)
Lines 34-39 Link Here
34
import org.apache.poi.ddf.EscherBSERecord;
34
import org.apache.poi.ddf.EscherBSERecord;
35
import org.apache.poi.ddf.EscherBitmapBlip;
35
import org.apache.poi.ddf.EscherBitmapBlip;
36
import org.apache.poi.ddf.EscherBlipRecord;
36
import org.apache.poi.ddf.EscherBlipRecord;
37
import org.apache.poi.ddf.EscherMetafileBlip;
37
import org.apache.poi.ddf.EscherRecord;
38
import org.apache.poi.ddf.EscherRecord;
38
import org.apache.poi.hssf.OldExcelFormatException;
39
import org.apache.poi.hssf.OldExcelFormatException;
39
import org.apache.poi.hssf.model.DrawingManager2;
40
import org.apache.poi.hssf.model.DrawingManager2;
Lines 1587-1593 Link Here
1587
        initDrawings();
1588
        initDrawings();
1588
1589
1589
        byte[] uid = DigestUtils.md5(pictureData);
1590
        byte[] uid = DigestUtils.md5(pictureData);
1590
        EscherBitmapBlip blipRecord = new EscherBitmapBlip();
1591
        EscherBlipRecord blipRecord;
1592
        int blipSize;
1593
        short escherTag;
1594
        if (format == PICTURE_TYPE_EMF) {
1595
            EscherMetafileBlip blipRecordMeta = new EscherMetafileBlip();
1596
	        blipRecord = blipRecordMeta;
1597
            blipRecordMeta.setUID(uid);
1598
            blipRecordMeta.setPictureData(pictureData);
1599
            // taken from libre office export, it won't open, if this is left to 0
1600
            blipRecordMeta.setFilter((byte)-2);
1601
            blipSize = blipRecordMeta.getCompressedSize() + 58;
1602
	        escherTag = 0;
1603
        } else {
1604
	        EscherBitmapBlip blipRecordBitmap = new EscherBitmapBlip();
1605
	        blipRecord = blipRecordBitmap;
1606
	        blipRecordBitmap.setUID( uid );
1607
	        blipRecordBitmap.setMarker( (byte) 0xFF );
1608
	        blipRecordBitmap.setPictureData( pictureData );
1609
	        blipSize = pictureData.length + 25;
1610
	        escherTag = (short) 0xFF;
1611
        }
1612
1591
        blipRecord.setRecordId( (short) ( EscherBitmapBlip.RECORD_ID_START + format ) );
1613
        blipRecord.setRecordId( (short) ( EscherBitmapBlip.RECORD_ID_START + format ) );
1592
        switch (format)
1614
        switch (format)
1593
        {
1615
        {
Lines 1610-1620 Link Here
1610
                blipRecord.setOptions(HSSFPictureData.MSOBI_DIB);
1632
                blipRecord.setOptions(HSSFPictureData.MSOBI_DIB);
1611
                break;
1633
                break;
1612
        }
1634
        }
1613
1635
        
1614
        blipRecord.setUID( uid );
1615
        blipRecord.setMarker( (byte) 0xFF );
1616
        blipRecord.setPictureData( pictureData );
1617
1618
        EscherBSERecord r = new EscherBSERecord();
1636
        EscherBSERecord r = new EscherBSERecord();
1619
        r.setRecordId( EscherBSERecord.RECORD_ID );
1637
        r.setRecordId( EscherBSERecord.RECORD_ID );
1620
        r.setOptions( (short) ( 0x0002 | ( format << 4 ) ) );
1638
        r.setOptions( (short) ( 0x0002 | ( format << 4 ) ) );
Lines 1621-1632 Link Here
1621
        r.setBlipTypeMacOS( (byte) format );
1639
        r.setBlipTypeMacOS( (byte) format );
1622
        r.setBlipTypeWin32( (byte) format );
1640
        r.setBlipTypeWin32( (byte) format );
1623
        r.setUid( uid );
1641
        r.setUid( uid );
1624
        r.setTag( (short) 0xFF );
1642
        r.setTag( escherTag );
1625
        r.setSize( pictureData.length + 25 );
1643
        r.setSize( blipSize );
1626
        r.setRef( 0 );
1644
        r.setRef( 0 );
1627
        r.setOffset( 0 );
1645
        r.setOffset( 0 );
1628
        r.setBlipRecord( blipRecord );
1646
        r.setBlipRecord( blipRecord );
1629
1647
        
1630
        return workbook.addBSERecord( r );
1648
        return workbook.addBSERecord( r );
1631
    }
1649
    }
1632
1650
(-)src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java (-4 / +44 lines)
Lines 17-33 Link Here
17
17
18
package org.apache.poi.hssf.usermodel;
18
package org.apache.poi.hssf.usermodel;
19
19
20
import java.io.IOException;
21
import java.util.Arrays;
22
import java.util.List;
23
24
import org.apache.poi.POIDataSamples;
20
import org.apache.poi.ddf.EscherBSERecord;
25
import org.apache.poi.ddf.EscherBSERecord;
26
import org.apache.poi.hssf.HSSFITestDataProvider;
21
import org.apache.poi.hssf.HSSFTestDataSamples;
27
import org.apache.poi.hssf.HSSFTestDataSamples;
22
import org.apache.poi.hssf.HSSFITestDataProvider;
23
import org.apache.poi.hssf.model.InternalSheet;
28
import org.apache.poi.hssf.model.InternalSheet;
24
import org.apache.poi.ss.usermodel.BaseTestPicture;
29
import org.apache.poi.ss.usermodel.BaseTestPicture;
30
import org.apache.poi.ss.usermodel.ClientAnchor;
31
import org.apache.poi.ss.usermodel.CreationHelper;
25
import org.apache.poi.ss.usermodel.PictureData;
32
import org.apache.poi.ss.usermodel.PictureData;
26
import org.apache.poi.ss.usermodel.Workbook;
33
import org.apache.poi.ss.usermodel.Workbook;
27
34
28
import java.util.Arrays;
29
import java.util.List;
30
31
/**
35
/**
32
 * Test <code>HSSFPicture</code>.
36
 * Test <code>HSSFPicture</code>.
33
 *
37
 *
Lines 210-213 Link Here
210
        p1 = (HSSFPicture) dr.getChildren().get(0);
214
        p1 = (HSSFPicture) dr.getChildren().get(0);
211
        assertEquals(p1.getFileName(), "aaa");
215
        assertEquals(p1.getFileName(), "aaa");
212
    }
216
    }
217
218
    public void testInsertBugzillaIdHere() throws IOException {
219
    	// test if inserted EscherMetafileBlip will be read again
220
        HSSFWorkbook wb = new HSSFWorkbook();
221
222
        byte pictureDataEmf[] = POIDataSamples.getDocumentInstance().readFile("vector_image.emf");
223
        int indexEmf = wb.addPicture(pictureDataEmf, HSSFWorkbook.PICTURE_TYPE_EMF);
224
        byte pictureDataPng[] = POIDataSamples.getSpreadSheetInstance().readFile("logoKarmokar4.png");
225
        int indexPng = wb.addPicture(pictureDataPng, HSSFWorkbook.PICTURE_TYPE_PNG);
226
        
227
        CreationHelper ch = wb.getCreationHelper();
228
        ClientAnchor anchor = ch.createClientAnchor();
229
        anchor.setCol1(2);
230
        anchor.setCol2(6);
231
        anchor.setRow1(1);
232
        anchor.setRow2(6);
233
234
        HSSFSheet sheet = wb.createSheet();
235
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
236
        patriarch.createPicture(anchor, indexEmf);
237
        
238
        anchor = ch.createClientAnchor();
239
        anchor.setCol1(2);
240
        anchor.setCol2(6);
241
        anchor.setRow1(10);
242
        anchor.setRow2(16);
243
        patriarch.createPicture(anchor, indexPng);
244
        
245
        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
246
        byte pictureDataOut[] = wb.getAllPictures().get(0).getData();
247
        assertTrue(Arrays.equals(pictureDataEmf, pictureDataOut));
248
249
        // FileOutputStream fos = new FileOutputStream("vect.xls");
250
        // wb.write(fos);
251
        // fos.close();
252
    }
213
}
253
}

Return to bug 49658