use of org.apache.poi.sl.image.ImageHeaderWMF 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.ImageHeaderWMF in project poi by apache.
the class WMF method setData.
@Override
public void setData(byte[] data) throws IOException {
int pos = 0;
ImageHeaderWMF nHeader = new ImageHeaderWMF(data, pos);
pos += nHeader.getLength();
byte[] compressed = compress(data, pos, data.length - pos);
Header header = new Header();
header.setWmfSize(data.length - nHeader.getLength());
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.ImageHeaderWMF in project poi by apache.
the class WMF method getData.
@Override
public byte[] getData() {
try {
byte[] rawdata = getRawData();
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream is = new ByteArrayInputStream(rawdata);
Header header = new Header();
header.read(rawdata, CHECKSUM_SIZE * getUIDInstanceCount());
long len = is.skip(header.getSize() + (long) CHECKSUM_SIZE * getUIDInstanceCount());
assert (len == header.getSize() + CHECKSUM_SIZE * getUIDInstanceCount());
ImageHeaderWMF aldus = new ImageHeaderWMF(header.getBounds());
aldus.write(out);
InflaterInputStream inflater = new InflaterInputStream(is);
byte[] chunk = new byte[4096];
int count;
while ((count = inflater.read(chunk)) >= 0) {
out.write(chunk, 0, count);
}
inflater.close();
return out.toByteArray();
} catch (IOException e) {
throw new HSLFException(e);
}
}
use of org.apache.poi.sl.image.ImageHeaderWMF 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);
}
Aggregations