Search in sources :

Example 1 with WMF

use of org.apache.poi.hslf.blip.WMF in project poi by apache.

the class TestPictures method testZeroPictureLength.

/**
     * YK: The test is disabled because the owner asked to delete the test file from POI svn.
     * See "Please remove my file from your svn" on @poi-dev from Dec 12, 2013
     */
@Test
@Ignore
public void testZeroPictureLength() throws IOException {
    // take the data from www instead of test directory
    URL url = new URL("http://www.cs.sfu.ca/~anoop/courses/CMPT-882-Fall-2002/chris.ppt");
    HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(url.openStream());
    // Should still have 2 real pictures
    assertEquals(2, hslf.getPictureData().size());
    // Both are real pictures, both WMF
    assertEquals(PictureType.WMF, hslf.getPictureData().get(0).getType());
    assertEquals(PictureType.WMF, hslf.getPictureData().get(1).getType());
    // Now test what happens when we use the SlideShow interface
    HSLFSlideShow ppt = new HSLFSlideShow(hslf);
    List<HSLFSlide> slides = ppt.getSlides();
    List<HSLFPictureData> pictures = ppt.getPictureData();
    assertEquals(27, slides.size());
    assertEquals(2, pictures.size());
    HSLFPictureShape pict;
    HSLFPictureData pdata;
    pict = (HSLFPictureShape) slides.get(6).getShapes().get(13);
    pdata = pict.getPictureData();
    assertTrue(pdata instanceof WMF);
    assertEquals(PictureType.WMF, pdata.getType());
    pict = (HSLFPictureShape) slides.get(7).getShapes().get(13);
    pdata = pict.getPictureData();
    assertTrue(pdata instanceof WMF);
    assertEquals(PictureType.WMF, pdata.getType());
    //add a new picture, it should be correctly appended to the Pictures stream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    for (HSLFPictureData p : pictures) p.write(out);
    out.close();
    int streamSize = out.size();
    HSLFPictureData data = HSLFPictureData.create(PictureType.JPEG);
    data.setData(new byte[100]);
    int offset = hslf.addPicture(data);
    assertEquals(streamSize, offset);
    assertEquals(3, ppt.getPictureData().size());
    ppt.close();
}
Also used : WMF(org.apache.poi.hslf.blip.WMF) ImageHeaderWMF(org.apache.poi.sl.image.ImageHeaderWMF) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with WMF

use of org.apache.poi.hslf.blip.WMF in project poi by apache.

the class TestPictures method testWMF.

/**
     * Test read/write WMF
     */
@Test
public void testWMF() throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow();
    HSLFSlide slide = ppt.createSlide();
    byte[] src_bytes = slTests.readFile("santa.wmf");
    HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.WMF);
    ImageHeaderWMF nHeader = new ImageHeaderWMF(src_bytes, 0);
    final int expWidth = 136, expHeight = 146;
    Dimension nDim = nHeader.getSize();
    assertEquals(expWidth, nDim.getWidth(), 0);
    assertEquals(expHeight, nDim.getHeight(), 0);
    Dimension dim = data.getImageDimensionInPixels();
    assertEquals(Units.pointsToPixel(expWidth), dim.getWidth(), 0);
    assertEquals(Units.pointsToPixel(expHeight), dim.getHeight(), 0);
    HSLFPictureShape pict = new HSLFPictureShape(data);
    assertEquals(data.getIndex(), pict.getPictureIndex());
    slide.addShape(pict);
    //serialize and read again
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ppt.write(out);
    out.close();
    ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
    //make sure we can read this picture shape and it refers to the correct picture data
    List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
    assertEquals(1, sh.size());
    pict = (HSLFPictureShape) sh.get(0);
    assertEquals(data.getIndex(), pict.getPictureIndex());
    //check picture data
    List<HSLFPictureData> pictures = ppt.getPictureData();
    assertEquals(1, pictures.size());
    HSLFPictureData pd = pictures.get(0);
    dim = pd.getImageDimension();
    assertEquals(expWidth, dim.width);
    assertEquals(expHeight, dim.height);
    //the Picture shape refers to the PictureData object in the Presentation
    assertEquals(pict.getPictureData(), pd);
    assertEquals(PictureType.WMF, pd.getType());
    assertTrue(pd instanceof WMF);
    //compare the content of the initial file with what is stored in the PictureData
    byte[] ppt_bytes = pd.getData();
    assertEquals(src_bytes.length, ppt_bytes.length);
    //in WMF the first 22 bytes - is a metafile header
    byte[] b1 = new byte[src_bytes.length - 22];
    System.arraycopy(src_bytes, 22, b1, 0, b1.length);
    byte[] b2 = new byte[ppt_bytes.length - 22];
    System.arraycopy(ppt_bytes, 22, b2, 0, b2.length);
    assertArrayEquals(b1, b2);
}
Also used : Dimension(java.awt.Dimension) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WMF(org.apache.poi.hslf.blip.WMF) ImageHeaderWMF(org.apache.poi.sl.image.ImageHeaderWMF) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageHeaderWMF(org.apache.poi.sl.image.ImageHeaderWMF) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 WMF (org.apache.poi.hslf.blip.WMF)2 ImageHeaderWMF (org.apache.poi.sl.image.ImageHeaderWMF)2 Test (org.junit.Test)2 Dimension (java.awt.Dimension)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URL (java.net.URL)1 Ignore (org.junit.Ignore)1