Search in sources :

Example 1 with EscherBlipRecord

use of org.apache.poi.ddf.EscherBlipRecord 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 EscherBlipRecord

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

the class OfficeDrawingsImpl method getOfficeDrawing.

private OfficeDrawing getOfficeDrawing(final FSPA fspa) {
    return new OfficeDrawing() {

        public HorizontalPositioning getHorizontalPositioning() {
            int value = getTertiaryPropertyValue(EscherProperties.GROUPSHAPE__POSH, -1);
            switch(value) {
                case 0:
                    return HorizontalPositioning.ABSOLUTE;
                case 1:
                    return HorizontalPositioning.LEFT;
                case 2:
                    return HorizontalPositioning.CENTER;
                case 3:
                    return HorizontalPositioning.RIGHT;
                case 4:
                    return HorizontalPositioning.INSIDE;
                case 5:
                    return HorizontalPositioning.OUTSIDE;
            }
            return HorizontalPositioning.ABSOLUTE;
        }

        public HorizontalRelativeElement getHorizontalRelative() {
            int value = getTertiaryPropertyValue(EscherProperties.GROUPSHAPE__POSRELH, -1);
            switch(value) {
                case 1:
                    return HorizontalRelativeElement.MARGIN;
                case 2:
                    return HorizontalRelativeElement.PAGE;
                case 3:
                    return HorizontalRelativeElement.TEXT;
                case 4:
                    return HorizontalRelativeElement.CHAR;
            }
            return HorizontalRelativeElement.TEXT;
        }

        public EscherContainerRecord getOfficeArtSpContainer() {
            return getEscherShapeRecordContainer(getShapeId());
        }

        public byte[] getPictureData() {
            EscherContainerRecord shapeDescription = getEscherShapeRecordContainer(getShapeId());
            if (shapeDescription == null)
                return null;
            EscherOptRecord escherOptRecord = shapeDescription.getChildById(EscherOptRecord.RECORD_ID);
            if (escherOptRecord == null)
                return null;
            EscherSimpleProperty escherProperty = escherOptRecord.lookup(EscherProperties.BLIP__BLIPTODISPLAY);
            if (escherProperty == null)
                return null;
            int bitmapIndex = escherProperty.getPropertyValue();
            EscherBlipRecord escherBlipRecord = getBitmapRecord(bitmapIndex);
            if (escherBlipRecord == null)
                return null;
            return escherBlipRecord.getPicturedata();
        }

        public int getRectangleBottom() {
            return fspa.getYaBottom();
        }

        public int getRectangleLeft() {
            return fspa.getXaLeft();
        }

        public int getRectangleRight() {
            return fspa.getXaRight();
        }

        public int getRectangleTop() {
            return fspa.getYaTop();
        }

        public int getShapeId() {
            return fspa.getSpid();
        }

        private int getTertiaryPropertyValue(int propertyId, int defaultValue) {
            EscherContainerRecord shapeDescription = getEscherShapeRecordContainer(getShapeId());
            if (shapeDescription == null)
                return defaultValue;
            EscherTertiaryOptRecord escherTertiaryOptRecord = shapeDescription.getChildById(EscherTertiaryOptRecord.RECORD_ID);
            if (escherTertiaryOptRecord == null)
                return defaultValue;
            EscherSimpleProperty escherProperty = escherTertiaryOptRecord.lookup(propertyId);
            if (escherProperty == null)
                return defaultValue;
            int value = escherProperty.getPropertyValue();
            return value;
        }

        public VerticalPositioning getVerticalPositioning() {
            int value = getTertiaryPropertyValue(EscherProperties.GROUPSHAPE__POSV, -1);
            switch(value) {
                case 0:
                    return VerticalPositioning.ABSOLUTE;
                case 1:
                    return VerticalPositioning.TOP;
                case 2:
                    return VerticalPositioning.CENTER;
                case 3:
                    return VerticalPositioning.BOTTOM;
                case 4:
                    return VerticalPositioning.INSIDE;
                case 5:
                    return VerticalPositioning.OUTSIDE;
            }
            return VerticalPositioning.ABSOLUTE;
        }

        public VerticalRelativeElement getVerticalRelativeElement() {
            int value = getTertiaryPropertyValue(EscherProperties.GROUPSHAPE__POSV, -1);
            switch(value) {
                case 1:
                    return VerticalRelativeElement.MARGIN;
                case 2:
                    return VerticalRelativeElement.PAGE;
                case 3:
                    return VerticalRelativeElement.TEXT;
                case 4:
                    return VerticalRelativeElement.LINE;
            }
            return VerticalRelativeElement.TEXT;
        }

        @Override
        public String toString() {
            return "OfficeDrawingImpl: " + fspa;
        }
    };
}
Also used : EscherSimpleProperty(org.apache.poi.ddf.EscherSimpleProperty) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) EscherTertiaryOptRecord(org.apache.poi.ddf.EscherTertiaryOptRecord) EscherOptRecord(org.apache.poi.ddf.EscherOptRecord) EscherBlipRecord(org.apache.poi.ddf.EscherBlipRecord)

Example 3 with EscherBlipRecord

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

the class PicturesTable method searchForPictures.

/**
     * Performs a recursive search for pictures in the given list of escher records.
     *
     * @param escherRecords the escher records.
     * @param pictures the list to populate with the pictures.
     */
private void searchForPictures(List<EscherRecord> escherRecords, List<Picture> pictures) {
    for (EscherRecord escherRecord : escherRecords) {
        if (escherRecord instanceof EscherBSERecord) {
            EscherBSERecord bse = (EscherBSERecord) escherRecord;
            EscherBlipRecord blip = bse.getBlipRecord();
            if (blip != null) {
                pictures.add(new Picture(blip));
            } else if (bse.getOffset() > 0) {
                try {
                    // Blip stored in delay stream, which in a word doc, is
                    // the main stream
                    EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
                    EscherRecord record = recordFactory.createRecord(_mainStream, bse.getOffset());
                    if (record instanceof EscherBlipRecord) {
                        record.fillFields(_mainStream, bse.getOffset(), recordFactory);
                        blip = (EscherBlipRecord) record;
                        pictures.add(new Picture(blip));
                    }
                } catch (Exception exc) {
                    logger.log(POILogger.WARN, "Unable to load picture from BLIB record at offset #", Integer.valueOf(bse.getOffset()), exc);
                }
            }
        }
        // Recursive call.
        searchForPictures(escherRecord.getChildRecords(), pictures);
    }
}
Also used : Picture(org.apache.poi.hwpf.usermodel.Picture) EscherRecord(org.apache.poi.ddf.EscherRecord) DefaultEscherRecordFactory(org.apache.poi.ddf.DefaultEscherRecordFactory) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord) EscherBlipRecord(org.apache.poi.ddf.EscherBlipRecord) EscherRecordFactory(org.apache.poi.ddf.EscherRecordFactory) DefaultEscherRecordFactory(org.apache.poi.ddf.DefaultEscherRecordFactory)

Example 4 with EscherBlipRecord

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

the class OfficeDrawingsImpl method getBitmapRecord.

private EscherBlipRecord getBitmapRecord(int bitmapIndex) {
    List<? extends EscherContainerRecord> bContainers = _escherRecordHolder.getBStoreContainers();
    if (bContainers == null || bContainers.size() != 1)
        return null;
    EscherContainerRecord bContainer = bContainers.get(0);
    final List<EscherRecord> bitmapRecords = bContainer.getChildRecords();
    if (bitmapRecords.size() < bitmapIndex)
        return null;
    EscherRecord imageRecord = bitmapRecords.get(bitmapIndex - 1);
    if (imageRecord instanceof EscherBlipRecord) {
        return (EscherBlipRecord) imageRecord;
    }
    if (imageRecord instanceof EscherBSERecord) {
        EscherBSERecord bseRecord = (EscherBSERecord) imageRecord;
        EscherBlipRecord blip = bseRecord.getBlipRecord();
        if (blip != null) {
            return blip;
        }
        if (bseRecord.getOffset() > 0) {
            /*
                 * Blip stored in delay stream, which in a word doc, is the main
                 * stream
                 */
            EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
            EscherRecord record = recordFactory.createRecord(_mainStream, bseRecord.getOffset());
            if (record instanceof EscherBlipRecord) {
                record.fillFields(_mainStream, bseRecord.getOffset(), recordFactory);
                return (EscherBlipRecord) record;
            }
        }
    }
    return null;
}
Also used : EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) DefaultEscherRecordFactory(org.apache.poi.ddf.DefaultEscherRecordFactory) EscherBlipRecord(org.apache.poi.ddf.EscherBlipRecord) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord) EscherRecordFactory(org.apache.poi.ddf.EscherRecordFactory) DefaultEscherRecordFactory(org.apache.poi.ddf.DefaultEscherRecordFactory)

Example 5 with EscherBlipRecord

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

the class HSSFWorkbook method searchForPictures.

/**
     * Performs a recursive search for pictures in the given list of escher records.
     *
     * @param escherRecords the escher records.
     * @param pictures the list to populate with the pictures.
     */
private void searchForPictures(List<EscherRecord> escherRecords, List<HSSFPictureData> pictures) {
    for (EscherRecord escherRecord : escherRecords) {
        if (escherRecord instanceof EscherBSERecord) {
            EscherBlipRecord blip = ((EscherBSERecord) escherRecord).getBlipRecord();
            if (blip != null) {
                // TODO: Some kind of structure.
                HSSFPictureData picture = new HSSFPictureData(blip);
                pictures.add(picture);
            }
        }
        // Recursive call.
        searchForPictures(escherRecord.getChildRecords(), pictures);
    }
}
Also used : EscherRecord(org.apache.poi.ddf.EscherRecord) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord) EscherBlipRecord(org.apache.poi.ddf.EscherBlipRecord)

Aggregations

EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)6 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)5 EscherRecord (org.apache.poi.ddf.EscherRecord)3 DefaultEscherRecordFactory (org.apache.poi.ddf.DefaultEscherRecordFactory)2 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)2 EscherRecordFactory (org.apache.poi.ddf.EscherRecordFactory)2 EscherBitmapBlip (org.apache.poi.ddf.EscherBitmapBlip)1 EscherMetafileBlip (org.apache.poi.ddf.EscherMetafileBlip)1 EscherOptRecord (org.apache.poi.ddf.EscherOptRecord)1 EscherSimpleProperty (org.apache.poi.ddf.EscherSimpleProperty)1 EscherTertiaryOptRecord (org.apache.poi.ddf.EscherTertiaryOptRecord)1 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)1 Picture (org.apache.poi.hwpf.usermodel.Picture)1