fix picture matching procedure

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1151766 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-07-28 08:49:38 +00:00
parent b958f6fb8c
commit aeeed4ee06
2 changed files with 41 additions and 20 deletions

View File

@ -168,26 +168,26 @@ public final class Picture extends PictureDescriptor
return dataBlockStartOfsset; return dataBlockStartOfsset;
} }
/** /**
* @return picture's content as byte array * @return picture's content as byte array
*/ */
public byte[] getContent() public byte[] getContent()
{
if (content == null || content.length<=0)
{ {
fillImageContent(); fillImageContent();
return content;
} }
return content;
}
public byte[] getRawContent() /**
{ * Returns picture's content as it stored in Word file, i.e. possibly in
if (rawContent == null || rawContent.length <= 0) * compressed form.
*
* @return picture's content as it stored in Word file
*/
public byte[] getRawContent()
{ {
fillRawImageContent(); fillRawImageContent();
return rawContent;
} }
return rawContent;
}
/** /**
* *
@ -304,12 +304,10 @@ public final class Picture extends PictureDescriptor
public PictureType suggestPictureType() public PictureType suggestPictureType()
{ {
fillImageContent(); final byte[] imageContent = getContent();
for ( PictureType pictureType : PictureType.values() ) for ( PictureType pictureType : PictureType.values() )
for ( byte[] signature : pictureType.getSignatures() ) for ( byte[] signature : pictureType.getSignatures() )
if ( matchSignature( _dataStream, signature, if ( matchSignature( imageContent, signature, 0 ) )
pictureBytesStartOffset ) )
return pictureType; return pictureType;
// TODO: DIB, PICT // TODO: DIB, PICT
@ -355,12 +353,18 @@ public final class Picture extends PictureDescriptor
private void fillRawImageContent() private void fillRawImageContent()
{ {
if ( rawContent != null && rawContent.length > 0 )
return;
this.rawContent = new byte[size]; this.rawContent = new byte[size];
System.arraycopy(_dataStream, pictureBytesStartOffset, rawContent, 0, size); System.arraycopy(_dataStream, pictureBytesStartOffset, rawContent, 0, size);
} }
private void fillImageContent() private void fillImageContent()
{ {
if ( content != null && content.length > 0 )
return;
byte[] rawContent = getRawContent(); byte[] rawContent = getRawContent();
// HACK: Detect compressed images. In reality there should be some way to determine // HACK: Detect compressed images. In reality there should be some way to determine

View File

@ -262,6 +262,23 @@ public final class TestPictures extends TestCase {
assertEquals("image/unknown", picture.getMimeType()); assertEquals("image/unknown", picture.getMimeType());
} }
public void testEquation()
{
HWPFDocument doc = HWPFTestDataSamples.openSampleFile( "equation.doc" );
PicturesTable pictures = doc.getPicturesTable();
final List<Picture> allPictures = pictures.getAllPictures();
assertEquals( 1, allPictures.size() );
Picture picture = allPictures.get( 0 );
assertNotNull( picture );
assertEquals( PictureType.EMF, picture.suggestPictureType() );
assertEquals( PictureType.EMF.getExtension(),
picture.suggestFileExtension() );
assertEquals( PictureType.EMF.getMime(), picture.getMimeType() );
assertEquals( "0.emf", picture.suggestFullFileName() );
}
/** /**
* In word you can have floating or fixed pictures. * In word you can have floating or fixed pictures.
* Fixed have a \u0001 in place with an offset to the * Fixed have a \u0001 in place with an offset to the