Restore HWPF support for inline Escher images (stored in Escher rather than direct in a PICFAndOfficeArtData in the main stream), plus add test for this kind of file
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1207497 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
991fd65ee8
commit
bfcf5190eb
@ -18,12 +18,9 @@
|
|||||||
package org.apache.poi.hwpf.model;
|
package org.apache.poi.hwpf.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.poi.util.POILogFactory;
|
|
||||||
|
|
||||||
import org.apache.poi.util.POILogger;
|
|
||||||
|
|
||||||
import org.apache.poi.ddf.DefaultEscherRecordFactory;
|
import org.apache.poi.ddf.DefaultEscherRecordFactory;
|
||||||
import org.apache.poi.ddf.EscherBSERecord;
|
import org.apache.poi.ddf.EscherBSERecord;
|
||||||
import org.apache.poi.ddf.EscherBlipRecord;
|
import org.apache.poi.ddf.EscherBlipRecord;
|
||||||
@ -35,6 +32,8 @@ import org.apache.poi.hwpf.usermodel.Picture;
|
|||||||
import org.apache.poi.hwpf.usermodel.Range;
|
import org.apache.poi.hwpf.usermodel.Range;
|
||||||
import org.apache.poi.util.Internal;
|
import org.apache.poi.util.Internal;
|
||||||
import org.apache.poi.util.LittleEndian;
|
import org.apache.poi.util.LittleEndian;
|
||||||
|
import org.apache.poi.util.POILogFactory;
|
||||||
|
import org.apache.poi.util.POILogger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds information about all pictures embedded in Word Document either via "Insert -> Picture -> From File" or via
|
* Holds information about all pictures embedded in Word Document either via "Insert -> Picture -> From File" or via
|
||||||
@ -180,7 +179,7 @@ public final class PicturesTable
|
|||||||
EscherBlipRecord blip = bse.getBlipRecord();
|
EscherBlipRecord blip = bse.getBlipRecord();
|
||||||
if (blip != null)
|
if (blip != null)
|
||||||
{
|
{
|
||||||
pictures.add(new Picture(blip.getPicturedata()));
|
pictures.add(new Picture(blip));
|
||||||
}
|
}
|
||||||
else if ( bse.getOffset() > 0 )
|
else if ( bse.getOffset() > 0 )
|
||||||
{
|
{
|
||||||
@ -197,7 +196,7 @@ public final class PicturesTable
|
|||||||
record.fillFields( _mainStream, bse.getOffset(),
|
record.fillFields( _mainStream, bse.getOffset(),
|
||||||
recordFactory );
|
recordFactory );
|
||||||
blip = (EscherBlipRecord) record;
|
blip = (EscherBlipRecord) record;
|
||||||
pictures.add( new Picture( blip.getPicturedata() ) );
|
pictures.add( new Picture( blip ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch ( Exception exc )
|
catch ( Exception exc )
|
||||||
|
@ -21,16 +21,10 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.zip.InflaterInputStream;
|
import java.util.zip.InflaterInputStream;
|
||||||
|
|
||||||
import org.apache.poi.ddf.EscherSimpleProperty;
|
|
||||||
|
|
||||||
import org.apache.poi.ddf.EscherProperty;
|
|
||||||
|
|
||||||
import org.apache.poi.ddf.EscherOptRecord;
|
|
||||||
|
|
||||||
import org.apache.poi.ddf.EscherContainerRecord;
|
|
||||||
|
|
||||||
import org.apache.poi.ddf.EscherBSERecord;
|
import org.apache.poi.ddf.EscherBSERecord;
|
||||||
import org.apache.poi.ddf.EscherBlipRecord;
|
import org.apache.poi.ddf.EscherBlipRecord;
|
||||||
import org.apache.poi.ddf.EscherRecord;
|
import org.apache.poi.ddf.EscherRecord;
|
||||||
@ -41,8 +35,6 @@ import org.apache.poi.util.POILogger;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents embedded picture extracted from Word Document
|
* Represents embedded picture extracted from Word Document
|
||||||
*
|
|
||||||
* @author Dmitry Romanov
|
|
||||||
*/
|
*/
|
||||||
public final class Picture
|
public final class Picture
|
||||||
{
|
{
|
||||||
@ -109,6 +101,7 @@ public final class Picture
|
|||||||
|
|
||||||
private PICF _picf;
|
private PICF _picf;
|
||||||
private PICFAndOfficeArtData _picfAndOfficeArtData;
|
private PICFAndOfficeArtData _picfAndOfficeArtData;
|
||||||
|
private List<? extends EscherRecord> _blipRecords;
|
||||||
|
|
||||||
private byte[] content;
|
private byte[] content;
|
||||||
private int dataBlockStartOfsset;
|
private int dataBlockStartOfsset;
|
||||||
@ -116,18 +109,20 @@ public final class Picture
|
|||||||
private int height = -1;
|
private int height = -1;
|
||||||
private int width = -1;
|
private int width = -1;
|
||||||
|
|
||||||
public Picture( byte[] _dataStream )
|
/**
|
||||||
|
* Builds a Picture object for a Picture stored as
|
||||||
|
* Escher.
|
||||||
|
* TODO We need to pass in the PICF data too somehow!
|
||||||
|
*/
|
||||||
|
public Picture( EscherBlipRecord blipRecord )
|
||||||
{
|
{
|
||||||
super();
|
this._blipRecords = Arrays.asList(new EscherBlipRecord[] {blipRecord});
|
||||||
|
|
||||||
// XXX: implement
|
|
||||||
// this._dataStream = _dataStream;
|
|
||||||
// this.dataBlockStartOfsset = 0;
|
|
||||||
// this.dataBlockSize = _dataStream.length;
|
|
||||||
// this.pictureBytesStartOffset = 0;
|
|
||||||
// this.size = _dataStream.length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a Picture object for a Picture stored in the
|
||||||
|
* DataStream
|
||||||
|
*/
|
||||||
public Picture( int dataBlockStartOfsset, byte[] _dataStream,
|
public Picture( int dataBlockStartOfsset, byte[] _dataStream,
|
||||||
boolean fillBytes )
|
boolean fillBytes )
|
||||||
{
|
{
|
||||||
@ -137,9 +132,14 @@ public final class Picture
|
|||||||
|
|
||||||
this.dataBlockStartOfsset = dataBlockStartOfsset;
|
this.dataBlockStartOfsset = dataBlockStartOfsset;
|
||||||
|
|
||||||
if ( fillBytes )
|
if ( _picfAndOfficeArtData != null && _picfAndOfficeArtData.getBlipRecords() != null) {
|
||||||
|
_blipRecords = _picfAndOfficeArtData.getBlipRecords();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( fillBytes ) {
|
||||||
fillImageContent();
|
fillImageContent();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void fillImageContent()
|
private void fillImageContent()
|
||||||
{
|
{
|
||||||
@ -432,13 +432,11 @@ public final class Picture
|
|||||||
*/
|
*/
|
||||||
public byte[] getRawContent()
|
public byte[] getRawContent()
|
||||||
{
|
{
|
||||||
if ( _picfAndOfficeArtData == null ||
|
if (_blipRecords == null || _blipRecords.size() != 1) {
|
||||||
_picfAndOfficeArtData.getBlipRecords()== null ||
|
|
||||||
_picfAndOfficeArtData.getBlipRecords().size() != 1 )
|
|
||||||
return new byte[0];
|
return new byte[0];
|
||||||
|
}
|
||||||
|
|
||||||
EscherRecord escherRecord = _picfAndOfficeArtData.getBlipRecords().get(
|
EscherRecord escherRecord = _blipRecords.get( 0 );
|
||||||
0 );
|
|
||||||
if ( escherRecord instanceof EscherBlipRecord )
|
if ( escherRecord instanceof EscherBlipRecord )
|
||||||
{
|
{
|
||||||
return ( (EscherBlipRecord) escherRecord ).getPicturedata();
|
return ( (EscherBlipRecord) escherRecord ).getPicturedata();
|
||||||
@ -518,13 +516,11 @@ public final class Picture
|
|||||||
|
|
||||||
public PictureType suggestPictureType()
|
public PictureType suggestPictureType()
|
||||||
{
|
{
|
||||||
if ( _picfAndOfficeArtData == null ||
|
if (_blipRecords == null || _blipRecords.size() != 1 ) {
|
||||||
_picfAndOfficeArtData.getBlipRecords()== null ||
|
|
||||||
_picfAndOfficeArtData.getBlipRecords().size() != 1 )
|
|
||||||
return PictureType.UNKNOWN;
|
return PictureType.UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
EscherRecord escherRecord = _picfAndOfficeArtData.getBlipRecords().get(
|
EscherRecord escherRecord = _blipRecords.get( 0 );
|
||||||
0 );
|
|
||||||
switch ( escherRecord.getRecordId() )
|
switch ( escherRecord.getRecordId() )
|
||||||
{
|
{
|
||||||
case (short) 0xF007:
|
case (short) 0xF007:
|
||||||
|
@ -324,4 +324,14 @@ public final class TestPictures extends TestCase {
|
|||||||
assertEquals(0, pic2.getDyaCropBottom());
|
assertEquals(0, pic2.getDyaCropBottom());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testPictureDetectionWithPNG() throws Exception {
|
||||||
|
HWPFDocument document = HWPFTestDataSamples.openSampleFile("PngPicture.doc");
|
||||||
|
PicturesTable pictureTable = document.getPicturesTable();
|
||||||
|
|
||||||
|
assertEquals(1, pictureTable.getAllPictures().size());
|
||||||
|
|
||||||
|
Picture p = pictureTable.getAllPictures().get(0);
|
||||||
|
assertEquals(PictureType.PNG, p.suggestPictureType());
|
||||||
|
assertEquals("png", p.suggestFileExtension());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
BIN
test-data/document/PngPicture.doc
Normal file
BIN
test-data/document/PngPicture.doc
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user