use of org.apache.poi.hslf.blip.PICT 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);
}
Aggregations