From bfcf5190ebfb4ba4862a4f6a42f54874aa17d240 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 28 Nov 2011 18:42:30 +0000 Subject: [PATCH] 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 --- .../apache/poi/hwpf/model/PicturesTable.java | 11 ++-- .../apache/poi/hwpf/usermodel/Picture.java | 58 ++++++++---------- .../poi/hwpf/usermodel/TestPictures.java | 10 +++ test-data/document/PngPicture.doc | Bin 0 -> 14336 bytes 4 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 test-data/document/PngPicture.doc diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java b/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java index c0b440ff1..0b8b501bf 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/PicturesTable.java @@ -18,12 +18,9 @@ package org.apache.poi.hwpf.model; import java.util.ArrayList; +import java.util.Arrays; 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.EscherBSERecord; 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.util.Internal; 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 @@ -180,7 +179,7 @@ public final class PicturesTable EscherBlipRecord blip = bse.getBlipRecord(); if (blip != null) { - pictures.add(new Picture(blip.getPicturedata())); + pictures.add(new Picture(blip)); } else if ( bse.getOffset() > 0 ) { @@ -197,7 +196,7 @@ public final class PicturesTable record.fillFields( _mainStream, bse.getOffset(), recordFactory ); blip = (EscherBlipRecord) record; - pictures.add( new Picture( blip.getPicturedata() ) ); + pictures.add( new Picture( blip ) ); } } catch ( Exception exc ) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java index 06e84a4f1..413c03095 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java @@ -21,16 +21,10 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; +import java.util.List; 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.EscherBlipRecord; import org.apache.poi.ddf.EscherRecord; @@ -41,8 +35,6 @@ import org.apache.poi.util.POILogger; /** * Represents embedded picture extracted from Word Document - * - * @author Dmitry Romanov */ public final class Picture { @@ -109,6 +101,7 @@ public final class Picture private PICF _picf; private PICFAndOfficeArtData _picfAndOfficeArtData; + private List _blipRecords; private byte[] content; private int dataBlockStartOfsset; @@ -116,18 +109,20 @@ public final class Picture private int height = -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(); - - // XXX: implement - // this._dataStream = _dataStream; - // this.dataBlockStartOfsset = 0; - // this.dataBlockSize = _dataStream.length; - // this.pictureBytesStartOffset = 0; - // this.size = _dataStream.length; + this._blipRecords = Arrays.asList(new EscherBlipRecord[] {blipRecord}); } + /** + * Builds a Picture object for a Picture stored in the + * DataStream + */ public Picture( int dataBlockStartOfsset, byte[] _dataStream, boolean fillBytes ) { @@ -137,8 +132,13 @@ public final class Picture this.dataBlockStartOfsset = dataBlockStartOfsset; - if ( fillBytes ) + if ( _picfAndOfficeArtData != null && _picfAndOfficeArtData.getBlipRecords() != null) { + _blipRecords = _picfAndOfficeArtData.getBlipRecords(); + } + + if ( fillBytes ) { fillImageContent(); + } } private void fillImageContent() @@ -432,13 +432,11 @@ public final class Picture */ public byte[] getRawContent() { - if ( _picfAndOfficeArtData == null || - _picfAndOfficeArtData.getBlipRecords()== null || - _picfAndOfficeArtData.getBlipRecords().size() != 1 ) - return new byte[0]; + if (_blipRecords == null || _blipRecords.size() != 1) { + return new byte[0]; + } - EscherRecord escherRecord = _picfAndOfficeArtData.getBlipRecords().get( - 0 ); + EscherRecord escherRecord = _blipRecords.get( 0 ); if ( escherRecord instanceof EscherBlipRecord ) { return ( (EscherBlipRecord) escherRecord ).getPicturedata(); @@ -518,13 +516,11 @@ public final class Picture public PictureType suggestPictureType() { - if ( _picfAndOfficeArtData == null || - _picfAndOfficeArtData.getBlipRecords()== null || - _picfAndOfficeArtData.getBlipRecords().size() != 1 ) + if (_blipRecords == null || _blipRecords.size() != 1 ) { return PictureType.UNKNOWN; + } - EscherRecord escherRecord = _picfAndOfficeArtData.getBlipRecords().get( - 0 ); + EscherRecord escherRecord = _blipRecords.get( 0 ); switch ( escherRecord.getRecordId() ) { case (short) 0xF007: diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java index 526cb5a47..a3fcd3abf 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java @@ -324,4 +324,14 @@ public final class TestPictures extends TestCase { 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()); + } } diff --git a/test-data/document/PngPicture.doc b/test-data/document/PngPicture.doc new file mode 100644 index 0000000000000000000000000000000000000000..4d8c41792c8097f9da68ca14321d26f8a90349a7 GIT binary patch literal 14336 zcmeG?2|ShC_WPJZNVqax4rx%PsHn^tqRb?vZpS>!7)r=+Qz;asn?zK~kd!7O_$j`0{WJKulpBk%UQbMOD&?+vZepfDgFTTY-e z0l5IV0eJvt0m2kOvjO=4`2ht0zX22koC7EXh}(;Nr2j!~`JXY>0{Or%0FjX%@&@XM zLSVH1JP5*!nRXW54}K36l|2=FO{+-EP)Q3fsAgPQ*l-){p6*6@lj} zh9$@fQIP!?+rR_*@g9iR&Y#tf>#K1<0zg8m!T(-p8~DK-#cwfL~{v+!j?;&{oivo%PehVlLC;>F@TL3A* z`)&=1B>>QS4GW%`pMwt4*6JrRkPZ|GBW=*`4_X6ID9jm4pcL5O93l0C)E_w@SID`6 zWw^wTVcE|T)ev2xpXftsz!Lnd2V`W%XhA_P(2fEt*A-^6KCEKAe`3&w)+EG7(ue#E zvpIe5Nv zz&_=Kf)%0rVov6rTzB zc!zz0X3|U`8wrTeZbll#NTV4ki#9fw4JSyGkw)T!4@EKZ?OePGF>ojFKcq9{NSY228tSam!w!jk|vJhPxEhLdo#@H3au6gUf*5o2jgKLhJ4e1Qk} z6u+R+A2j)FJ-|5OP3{ljFIy7viAaLLZ;Tig!wPVPc}PaSFbmz_RQ7Cy`ivb$#5?Ez$q(>3>a&e3=(mfKck&9w89ZV zAY-NC)0hm;zpig8S|Br8e<~SU&BfSVg1}yEp$Ta8gr5&s$Hj;h{6G)x#~RRtOPm;I zHE#J!AVDO{4BJ0GE7>@vMx$b?nm&$!jolpleEfZ!1IVV%&TbA)WE&qpNAe04IVD9! zWwNf1L!g(FcK{m0N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