Search in sources :

Example 1 with ImageHeaderPICT

use of org.apache.poi.sl.image.ImageHeaderPICT in project poi by apache.

the class XSLFPictureData method cacheProperties.

/**
     * Determine and cache image properties
     */
protected void cacheProperties() {
    if (origSize == null || checksum == null) {
        byte[] data = getData();
        checksum = IOUtils.calculateChecksum(data);
        PictureType pt = getType();
        if (pt == null) {
            origSize = new Dimension(1, 1);
            return;
        }
        switch(pt) {
            case EMF:
                origSize = new ImageHeaderEMF(data, 0).getSize();
                break;
            case WMF:
                // wmf files in pptx usually have their placeable header 
                // stripped away, so this returns only the dummy size
                origSize = new ImageHeaderWMF(data, 0).getSize();
                break;
            case PICT:
                origSize = new ImageHeaderPICT(data, 0).getSize();
                break;
            default:
                origSize = new ImageHeaderBitmap(data, 0).getSize();
                break;
        }
    }
}
Also used : ImageHeaderBitmap(org.apache.poi.sl.image.ImageHeaderBitmap) ImageHeaderPICT(org.apache.poi.sl.image.ImageHeaderPICT) ImageHeaderEMF(org.apache.poi.sl.image.ImageHeaderEMF) ImageHeaderWMF(org.apache.poi.sl.image.ImageHeaderWMF) Dimension(java.awt.Dimension)

Example 2 with ImageHeaderPICT

use of org.apache.poi.sl.image.ImageHeaderPICT in project poi by apache.

the class PICT method setData.

@Override
public void setData(byte[] data) throws IOException {
    // skip the first 512 bytes - they are MAC specific crap
    final int nOffset = ImageHeaderPICT.PICT_HEADER_OFFSET;
    ImageHeaderPICT nHeader = new ImageHeaderPICT(data, nOffset);
    Header header = new Header();
    int wmfSize = data.length - nOffset;
    header.setWmfSize(wmfSize);
    byte[] compressed = compress(data, nOffset, wmfSize);
    header.setZipSize(compressed.length);
    header.setBounds(nHeader.getBounds());
    Dimension nDim = nHeader.getSize();
    header.setDimension(new Dimension(Units.toEMU(nDim.getWidth()), Units.toEMU(nDim.getHeight())));
    byte[] checksum = getChecksum(data);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write(checksum);
    if (getUIDInstanceCount() == 2) {
        out.write(checksum);
    }
    header.write(out);
    out.write(compressed);
    setRawData(out.toByteArray());
}
Also used : ImageHeaderPICT(org.apache.poi.sl.image.ImageHeaderPICT) Dimension(java.awt.Dimension) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with ImageHeaderPICT

use of org.apache.poi.sl.image.ImageHeaderPICT in project poi by apache.

the class TestPictures method testPICT.

/**
     * Test read/write Macintosh PICT
     */
@Test
public void testPICT() throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow();
    HSLFSlide slide = ppt.createSlide();
    byte[] src_bytes = slTests.readFile("cow.pict");
    HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PICT);
    ImageHeaderPICT nHeader = new ImageHeaderPICT(src_bytes, 512);
    final int expWidth = 197, expHeight = 137;
    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(1, pictures.size());
    assertEquals(PictureType.PICT, pd.getType());
    assertTrue(pd instanceof PICT);
    //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 PICT the first 512 bytes are MAC specific and may not be preserved, ignore them
    byte[] b1 = new byte[src_bytes.length - 512];
    System.arraycopy(src_bytes, 512, b1, 0, b1.length);
    byte[] b2 = new byte[ppt_bytes.length - 512];
    System.arraycopy(ppt_bytes, 512, b2, 0, b2.length);
    assertArrayEquals(b1, b2);
}
Also used : Dimension(java.awt.Dimension) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PICT(org.apache.poi.hslf.blip.PICT) ImageHeaderPICT(org.apache.poi.sl.image.ImageHeaderPICT) ImageHeaderPICT(org.apache.poi.sl.image.ImageHeaderPICT) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Aggregations

Dimension (java.awt.Dimension)3 ImageHeaderPICT (org.apache.poi.sl.image.ImageHeaderPICT)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 PICT (org.apache.poi.hslf.blip.PICT)1 ImageHeaderBitmap (org.apache.poi.sl.image.ImageHeaderBitmap)1 ImageHeaderEMF (org.apache.poi.sl.image.ImageHeaderEMF)1 ImageHeaderWMF (org.apache.poi.sl.image.ImageHeaderWMF)1 Test (org.junit.Test)1