use of org.apache.poi.sl.image.ImageHeaderEMF 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;
}
}
}
use of org.apache.poi.sl.image.ImageHeaderEMF in project poi by apache.
the class EMF method setData.
@Override
public void setData(byte[] data) throws IOException {
byte[] compressed = compress(data, 0, data.length);
ImageHeaderEMF nHeader = new ImageHeaderEMF(data, 0);
Header header = new Header();
header.setWmfSize(data.length);
header.setBounds(nHeader.getBounds());
Dimension nDim = nHeader.getSize();
header.setDimension(new Dimension(Units.toEMU(nDim.getWidth()), Units.toEMU(nDim.getHeight())));
header.setZipSize(compressed.length);
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());
}
use of org.apache.poi.sl.image.ImageHeaderEMF in project poi by apache.
the class TestPictures method testEMF.
/**
* Test read/write EMF
*/
@Test
public void testEMF() throws IOException {
HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("wrench.emf");
HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.EMF);
ImageHeaderEMF nHeader = new ImageHeaderEMF(src_bytes, 0);
final int expWidth = 190, expHeight = 115;
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 get 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.EMF, pd.getType());
assertTrue(pd instanceof EMF);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pd.getData();
assertArrayEquals(src_bytes, ppt_bytes);
}
Aggregations