package org.apache.poi.hslf.model; import org.apache.poi.ddf.*; import org.apache.poi.hslf.usermodel.PictureData; import org.apache.poi.hslf.usermodel.SlideShow; import org.apache.poi.hslf.record.Document; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.List; import java.util.Arrays; /** * Represents a picture in a PowerPoint document. *
* The information about an image in PowerPoint document is stored in * two places: *
* Data in the "Pictures" OLE stream is organized as follows:
* For each image there is an entry: 25 byte header + image data.
* Image data is the exact content of the JPEG file, i.e. PowerPoint
* puts the whole jpeg file there without any modifications.
* Header format:
*
Picture
*
* @param idx the index of the picture
*/
public Picture(int idx){
super(null, null);
_escherContainer = createSpContainer(idx);
}
/**
* Create a Picture
object
*
* @param escherRecord the EscherSpContainer
record which holds information about
* this picture in the Slide
* @param parent the parent shape of this picture
*/
protected Picture(EscherContainerRecord escherRecord, Shape parent){
super(escherRecord, parent);
}
/**
* Returns index associated with this picture.
* Index starts with 1 and points to a EscherBSE record which
* holds information about this picture.
*
* @return the index to this picture (1 based).
*/
public int getPictureIndex(){
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.BLIP__BLIPTODISPLAY + 0x4000);
return prop.getPropertyValue();
}
/**
* Create a new Picture and populate the inital structure of the EscherSp
record which holds information about this picture.
* @param idx the index of the picture which referes to EscherBSE
container.
* @return the create Picture object
*/
protected EscherContainerRecord createSpContainer(int idx) {
EscherContainerRecord spContainer = super.createSpContainer(false);
spContainer.setOptions((short)15);
EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID);
spRecord.setOptions((short)((ShapeTypes.PictureFrame << 4) | 0x2));
//set default properties for a picture
EscherOptRecord opt = (EscherOptRecord)getEscherChild(spContainer, EscherOptRecord.RECORD_ID);
setEscherProperty(opt, EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 8388736);
//another weird feature of powerpoint: for picture id we must add 0x4000.
setEscherProperty(opt, (short)(EscherProperties.BLIP__BLIPTODISPLAY + 0x4000), idx);
return spContainer;
}
/**
* Resize this picture to the default size.
*/
public void setDefaultSize(){
PictureData pict = getPictureData();
try {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
setAnchor(new java.awt.Rectangle(0, 0, img.getWidth(), img.getHeight()));
} catch (IOException e){
throw new RuntimeException(e);
}
}
/**
* Returns the picture data for this picture.
*
* @return the picture data for this picture.
*/
public PictureData getPictureData(){
SlideShow ppt = getSheet().getSlideShow();
PictureData[] pict = ppt.getPictureData();
Document doc = ppt.getDocumentRecord();
EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
List lst = bstore.getChildRecords();
int idx = getPictureIndex()-1;
EscherBSERecord bse = (EscherBSERecord)lst.get(idx);
for ( int i = 0; i < pict.length; i++ ) {
if (pict[i].getOffset() == bse.getOffset()){
return pict[i];
}
}
return null;
}
/**
* By default set the orininal image size
*/
protected void afterInsert(Sheet sh){
java.awt.Rectangle anchor = getAnchor();
if (anchor.equals(new java.awt.Rectangle())){
setDefaultSize();
}
}
}