Search in sources :

Example 1 with EscherBSERecord

use of org.apache.poi.ddf.EscherBSERecord in project poi by apache.

the class HSSFWorkbook method addPicture.

/**
     * Adds a picture to the workbook.
     *
     * @param pictureData       The bytes of the picture
     * @param format            The format of the picture.  One of <code>PICTURE_TYPE_*</code>
     *
     * @return the index to this picture (1 based).
     * @see #PICTURE_TYPE_WMF
     * @see #PICTURE_TYPE_EMF
     * @see #PICTURE_TYPE_PICT
     * @see #PICTURE_TYPE_PNG
     * @see #PICTURE_TYPE_JPEG
     * @see #PICTURE_TYPE_DIB
     */
@SuppressWarnings("fallthrough")
@Override
public int addPicture(byte[] pictureData, int format) {
    initDrawings();
    byte[] uid = DigestUtils.md5(pictureData);
    EscherBlipRecord blipRecord;
    int blipSize;
    short escherTag;
    switch(format) {
        case PICTURE_TYPE_WMF:
            // see also http://de.wikipedia.org/wiki/Windows_Metafile#Hinweise_zur_WMF-Spezifikation
            if (LittleEndian.getInt(pictureData) == 0x9AC6CDD7) {
                byte[] picDataNoHeader = new byte[pictureData.length - 22];
                System.arraycopy(pictureData, 22, picDataNoHeader, 0, pictureData.length - 22);
                pictureData = picDataNoHeader;
            }
        // fall through
        case PICTURE_TYPE_EMF:
            EscherMetafileBlip blipRecordMeta = new EscherMetafileBlip();
            blipRecord = blipRecordMeta;
            blipRecordMeta.setUID(uid);
            blipRecordMeta.setPictureData(pictureData);
            // taken from libre office export, it won't open, if this is left to 0
            blipRecordMeta.setFilter((byte) -2);
            blipSize = blipRecordMeta.getCompressedSize() + 58;
            escherTag = 0;
            break;
        default:
            EscherBitmapBlip blipRecordBitmap = new EscherBitmapBlip();
            blipRecord = blipRecordBitmap;
            blipRecordBitmap.setUID(uid);
            blipRecordBitmap.setMarker((byte) 0xFF);
            blipRecordBitmap.setPictureData(pictureData);
            blipSize = pictureData.length + 25;
            escherTag = (short) 0xFF;
            break;
    }
    blipRecord.setRecordId((short) (EscherBlipRecord.RECORD_ID_START + format));
    switch(format) {
        case PICTURE_TYPE_EMF:
            blipRecord.setOptions(HSSFPictureData.MSOBI_EMF);
            break;
        case PICTURE_TYPE_WMF:
            blipRecord.setOptions(HSSFPictureData.MSOBI_WMF);
            break;
        case PICTURE_TYPE_PICT:
            blipRecord.setOptions(HSSFPictureData.MSOBI_PICT);
            break;
        case PICTURE_TYPE_PNG:
            blipRecord.setOptions(HSSFPictureData.MSOBI_PNG);
            break;
        case PICTURE_TYPE_JPEG:
            blipRecord.setOptions(HSSFPictureData.MSOBI_JPEG);
            break;
        case PICTURE_TYPE_DIB:
            blipRecord.setOptions(HSSFPictureData.MSOBI_DIB);
            break;
        default:
            throw new IllegalStateException("Unexpected picture format: " + format);
    }
    EscherBSERecord r = new EscherBSERecord();
    r.setRecordId(EscherBSERecord.RECORD_ID);
    r.setOptions((short) (0x0002 | (format << 4)));
    r.setBlipTypeMacOS((byte) format);
    r.setBlipTypeWin32((byte) format);
    r.setUid(uid);
    r.setTag(escherTag);
    r.setSize(blipSize);
    r.setRef(0);
    r.setOffset(0);
    r.setBlipRecord(blipRecord);
    return workbook.addBSERecord(r);
}
Also used : EscherMetafileBlip(org.apache.poi.ddf.EscherMetafileBlip) EscherBlipRecord(org.apache.poi.ddf.EscherBlipRecord) EscherBitmapBlip(org.apache.poi.ddf.EscherBitmapBlip) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord)

Example 2 with EscherBSERecord

use of org.apache.poi.ddf.EscherBSERecord in project poi by apache.

the class HSSFPicture method getImageDimension.

/**
     * Return the dimension of the embedded image in pixel
     *
     * @return image dimension in pixels
     */
@Override
public Dimension getImageDimension() {
    InternalWorkbook iwb = getPatriarch().getSheet().getWorkbook().getWorkbook();
    EscherBSERecord bse = iwb.getBSERecord(getPictureIndex());
    byte[] data = bse.getBlipRecord().getPicturedata();
    int type = bse.getBlipTypeWin32();
    return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord)

Example 3 with EscherBSERecord

use of org.apache.poi.ddf.EscherBSERecord in project poi by apache.

the class HSSFComment method setBackgroundImage.

public void setBackgroundImage(int pictureIndex) {
    setPropertyValue(new EscherSimpleProperty(EscherProperties.FILL__PATTERNTEXTURE, false, true, pictureIndex));
    setPropertyValue(new EscherSimpleProperty(EscherProperties.FILL__FILLTYPE, false, false, FILL_TYPE_PICTURE));
    EscherBSERecord bse = getPatriarch().getSheet().getWorkbook().getWorkbook().getBSERecord(pictureIndex);
    bse.setRef(bse.getRef() + 1);
}
Also used : EscherSimpleProperty(org.apache.poi.ddf.EscherSimpleProperty) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord)

Example 4 with EscherBSERecord

use of org.apache.poi.ddf.EscherBSERecord in project poi by apache.

the class HSLFPictureShape method afterInsert.

/**
     * By default set the orininal image size
     */
@Override
protected void afterInsert(HSLFSheet sh) {
    super.afterInsert(sh);
    EscherBSERecord bse = getEscherBSERecord();
    bse.setRef(bse.getRef() + 1);
    Rectangle2D anchor = getAnchor();
    if (anchor.isEmpty()) {
        new DrawPictureShape(this).resize();
    }
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) DrawPictureShape(org.apache.poi.sl.draw.DrawPictureShape) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord)

Example 5 with EscherBSERecord

use of org.apache.poi.ddf.EscherBSERecord in project poi by apache.

the class HSLFPictureShape method getPictureData.

@SuppressWarnings("resource")
@Override
public HSLFPictureData getPictureData() {
    HSLFSlideShow ppt = getSheet().getSlideShow();
    List<HSLFPictureData> pict = ppt.getPictureData();
    EscherBSERecord bse = getEscherBSERecord();
    if (bse == null) {
        LOG.log(POILogger.ERROR, "no reference to picture data found ");
    } else {
        for (HSLFPictureData pd : pict) {
            if (pd.getOffset() == bse.getOffset()) {
                return pd;
            }
        }
        LOG.log(POILogger.ERROR, "no picture found for our BSE offset " + bse.getOffset());
    }
    return null;
}
Also used : EscherBSERecord(org.apache.poi.ddf.EscherBSERecord)

Aggregations

EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)23 EscherRecord (org.apache.poi.ddf.EscherRecord)9 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)8 EscherSimpleProperty (org.apache.poi.ddf.EscherSimpleProperty)6 EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)5 AbstractEscherOptRecord (org.apache.poi.ddf.AbstractEscherOptRecord)4 Document (org.apache.poi.hslf.record.Document)4 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)3 Test (org.junit.Test)3 DefaultEscherRecordFactory (org.apache.poi.ddf.DefaultEscherRecordFactory)2 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)2 EscherRecordFactory (org.apache.poi.ddf.EscherRecordFactory)2 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)2 DrawPaint (org.apache.poi.sl.draw.DrawPaint)2 GradientPaint (org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)2 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)2 Rectangle (java.awt.Rectangle)1 Rectangle2D (java.awt.geom.Rectangle2D)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 EscherBitmapBlip (org.apache.poi.ddf.EscherBitmapBlip)1