View | Details | Raw Unified | Return to bug 39097
Collapse All | Expand All

(-)src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java (-16 / +99 lines)
Lines 25-45 Link Here
25
25
26
import org.apache.poi.hslf.*;
26
import org.apache.poi.hslf.*;
27
import org.apache.poi.hslf.model.*;
27
import org.apache.poi.hslf.model.*;
28
import org.apache.poi.hslf.record.Document;
28
import org.apache.poi.hslf.model.Notes;
29
import org.apache.poi.hslf.record.DocumentAtom;
29
import org.apache.poi.hslf.model.Slide;
30
import org.apache.poi.hslf.record.FontCollection;
31
import org.apache.poi.hslf.record.ParentAwareRecord;
32
import org.apache.poi.hslf.record.Record;
33
import org.apache.poi.hslf.record.RecordContainer;
34
import org.apache.poi.hslf.record.RecordTypes;
35
import org.apache.poi.hslf.record.SlideAtom;
36
import org.apache.poi.hslf.record.SlideListWithText;
37
import org.apache.poi.hslf.record.SlidePersistAtom;
38
import org.apache.poi.hslf.record.UserEditAtom;
39
import org.apache.poi.hslf.record.SlideListWithText.*;
30
import org.apache.poi.hslf.record.SlideListWithText.*;
40
import org.apache.poi.hslf.record.PersistPtrHolder;
31
import org.apache.poi.hslf.record.*;
41
import org.apache.poi.hslf.record.PositionDependentRecord;
42
import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
32
import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
33
import org.apache.poi.util.LittleEndian;
34
import org.apache.poi.ddf.*;
43
35
44
/**
36
/**
45
 * This class is a friendly wrapper on top of the more scary HSLFSlideShow.
37
 * This class is a friendly wrapper on top of the more scary HSLFSlideShow.
Lines 50-55 Link Here
50
 *  - handle Slide creation cleaner
42
 *  - handle Slide creation cleaner
51
 * 
43
 * 
52
 * @author Nick Burch
44
 * @author Nick Burch
45
 * @author Yegor kozlov
53
 */
46
 */
54
47
55
public class SlideShow
48
public class SlideShow
Lines 99-106 Link Here
99
	// Build up the model level Slides and Notes
92
	// Build up the model level Slides and Notes
100
	buildSlidesAndNotes();
93
	buildSlidesAndNotes();
101
  }
94
  }
102
  
95
103
  
96
    /**
97
     * Constructs a new Powerpoint document.
98
     */
99
   public SlideShow() throws IOException {
100
       this(new HSLFSlideShow());
101
  }
102
104
  /**
103
  /**
105
   * Find the records that are parent-aware, and tell them
104
   * Find the records that are parent-aware, and tell them
106
   *  who their parent is
105
   *  who their parent is
Lines 515-521 Link Here
515
	/**
514
	/**
516
	 * Returns all the pictures attached to the SlideShow
515
	 * Returns all the pictures attached to the SlideShow
517
	 */
516
	 */
518
	public Picture[] getPictures() throws IOException {
517
	public PictureData[] getPictures() {
519
		return _hslfSlideShow.getPictures();
518
		return _hslfSlideShow.getPictures();
520
	}
519
	}
521
	
520
	
Lines 535-538 Link Here
535
	 * Helper method for usermodel: Get the document record
534
	 * Helper method for usermodel: Get the document record
536
	 */
535
	 */
537
	protected Document getDocumentRecord() { return _documentRecord; }
536
	protected Document getDocumentRecord() { return _documentRecord; }
537
538
    /**
539
     * Adds a picture to this presentation and returns the associated index.
540
     *
541
     * @param data      picture data
542
     * @param format    the format of the picture.  One of constans defined in the <code>Picture</code> class.
543
     * @return          the index to this picture (1 based).
544
     */
545
    public int addPicture(byte[] data, int format) {
546
        byte[] uid = PictureData.getChecksum(data);
547
548
        EscherContainerRecord bstore;
549
        int offset = 0;
550
551
        EscherContainerRecord dggContainer = _documentRecord.getPPDrawingGroup().getDggContainer();
552
        bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
553
        if (bstore == null){
554
            bstore = new EscherContainerRecord();
555
            bstore.setRecordId( EscherContainerRecord.BSTORE_CONTAINER);
556
557
            List child = dggContainer.getChildRecords();
558
            for ( int i = 0; i < child.size(); i++ ) {
559
                EscherRecord rec = (EscherRecord)child.get(i);
560
                if (rec.getRecordId() == EscherOptRecord.RECORD_ID){
561
                    child.add(i, bstore);
562
                    i++;
563
                }
564
            }
565
            dggContainer.setChildRecords(child);
566
        } else {
567
            List lst = bstore.getChildRecords();
568
            for ( int i = 0; i < lst.size(); i++ ) {
569
                EscherBSERecord bse = (EscherBSERecord) lst.get(i);
570
                if (Arrays.equals(bse.getUid(), uid)){
571
                    return i + 1;
572
                }
573
                offset += bse.getSize();
574
             }
575
        }
576
577
        EscherBSERecord bse = new EscherBSERecord();
578
        bse.setRecordId(EscherBSERecord.RECORD_ID);
579
        bse.setOptions( (short) ( 0x0002 | ( format << 4 ) ) );
580
        bse.setSize(data.length + PictureData.HEADER_SIZE);
581
        bse.setUid(uid);
582
        bse.setBlipTypeMacOS((byte)format);
583
        bse.setBlipTypeWin32((byte)format);
584
585
        bse.setRef(1);
586
        bse.setOffset(offset);
587
588
        bstore.addChildRecord(bse);
589
        int count = bstore.getChildRecords().size();
590
        bstore.setOptions((short)( (count << 4) | 0xF ));
591
592
        PictureData pict = new PictureData();
593
        pict.setUID(uid);
594
        pict.setData(data);
595
        pict.setType(format);
596
597
        _hslfSlideShow.addPicture(pict);
598
599
        return count;
600
    }
601
602
    /**
603
     * Adds a picture to this presentation and returns the associated index.
604
     *
605
     * @param pict       the file containing the image to add
606
     * @param format    the format of the picture.  One of constans defined in the <code>Picture</code> class.
607
     * @return          the index to this picture (1 based).
608
     */
609
    public int addPicture(File pict, int format) {
610
        int length = (int)pict.length();
611
        byte[] data = new byte[length];
612
        try {
613
            FileInputStream is = new FileInputStream(pict);
614
            is.read(data);
615
            is.close();
616
        } catch (IOException e){
617
            throw new RuntimeException(e);
618
        }
619
        return addPicture(data, format);
620
    }
538
}
621
}
(-)src/scratchpad/src/org/apache/poi/hslf/model/Shape.java (-15 / +44 lines)
Lines 26-52 Link Here
26
  *
26
  *
27
  * @author Yegor Kozlov
27
  * @author Yegor Kozlov
28
 */
28
 */
29
public class Shape {
29
public abstract class Shape {
30
30
31
    public static final int EMU_PER_POINT = 12700;
31
    public static final int EMU_PER_POINT = 12700;
32
32
33
    /**
33
    /**
34
     *  The parent of the shape
34
     * Either EscherSpContainer or EscheSpgrContainer record
35
     * which holds information about this shape.
35
     */
36
     */
37
    protected EscherContainerRecord _escherContainer;
38
39
    /**
40
     * Parent of this shape.
41
     * <code>null</code> for the topmost shapes.
42
     */
36
    protected Shape _parent;
43
    protected Shape _parent;
37
44
38
    /**
45
    /**
39
     * Either EscherSpContainer or EscheSpgrContainer record
46
     * Create a Shape object. This constructor is used when an existing Shape is read from from a PowerPoint document.
40
     * which holds information about this shape.
47
     *
48
     * @param escherRecord       <code>EscherSpContainer</code> container which holds information about this shape
49
     * @param parent             the parent of this Shape
41
     */
50
     */
42
    protected EscherContainerRecord _escherContainer;
51
      protected Shape(EscherContainerRecord escherRecord, Shape parent){
43
44
    protected Shape(EscherContainerRecord escherRecord, Shape parent){
45
        _escherContainer = escherRecord;
52
        _escherContainer = escherRecord;
46
        _parent = parent;
53
        _parent = parent;
47
    }
54
     }
48
55
49
    /**
56
    /**
57
     * Creates the lowerlevel escher records for this shape.
58
     */
59
    protected abstract EscherContainerRecord createSpContainer(boolean isChild);
60
61
    /**
50
     *  @return the parent of this shape
62
     *  @return the parent of this shape
51
     */
63
     */
52
    public Shape getParent(){
64
    public Shape getParent(){
Lines 128-144 Link Here
128
        setAnchor(anchor);
140
        setAnchor(anchor);
129
    }
141
    }
130
142
131
    protected static EscherRecord getEscherChild(EscherContainerRecord owner, int recordId){
143
    /**
144
     * Helper method to return escher child by record ID
145
     *
146
     * @return escher record or <code>null</code> if not found.
147
     */
148
    public static EscherRecord getEscherChild(EscherContainerRecord owner, int recordId){
132
        for ( Iterator iterator = owner.getChildRecords().iterator(); iterator.hasNext(); )
149
        for ( Iterator iterator = owner.getChildRecords().iterator(); iterator.hasNext(); )
133
        {
150
        {
134
            EscherRecord escherRecord = (EscherRecord) iterator.next();
151
            EscherRecord escherRecord = (EscherRecord) iterator.next();
135
            if (escherRecord.getRecordId() == recordId)
152
            if (escherRecord.getRecordId() == recordId)
136
                return (EscherRecord) escherRecord;
153
                return escherRecord;
137
        }
154
        }
138
        return null;
155
        return null;
139
    }
156
    }
140
157
141
    protected static EscherProperty getEscherProperty(EscherOptRecord opt, int propId){
158
    /**
159
     * Returns  escher property by id.
160
     *
161
     * @return escher property or <code>null</code> if not found.
162
     */
163
     public static EscherProperty getEscherProperty(EscherOptRecord opt, int propId){
142
        for ( Iterator iterator = opt.getEscherProperties().iterator(); iterator.hasNext(); )
164
        for ( Iterator iterator = opt.getEscherProperties().iterator(); iterator.hasNext(); )
143
        {
165
        {
144
            EscherProperty prop = (EscherProperty) iterator.next();
166
            EscherProperty prop = (EscherProperty) iterator.next();
Lines 148-154 Link Here
148
        return null;
170
        return null;
149
    }
171
    }
150
172
151
    protected static void setEscherProperty(EscherOptRecord opt, short propId, int value){
173
    /**
174
     * Set an escher property in the opt record.
175
     *
176
     * @param opt       The opt record to set the properties to.
177
     * @param propId    The id of the property. One of the constants defined in EscherOptRecord.
178
     * @param value     value of the property
179
     */
180
     public static void setEscherProperty(EscherOptRecord opt, short propId, int value){
152
        java.util.List props = opt.getEscherProperties();
181
        java.util.List props = opt.getEscherProperties();
153
        for ( Iterator iterator = props.iterator(); iterator.hasNext(); ) {
182
        for ( Iterator iterator = props.iterator(); iterator.hasNext(); ) {
154
            EscherProperty prop = (EscherProperty) iterator.next();
183
            EscherProperty prop = (EscherProperty) iterator.next();
Lines 163-172 Link Here
163
    }
192
    }
164
193
165
    /**
194
    /**
166
     *
195
     * @return  The shape container and it's children that can represent this
167
     * @return escher container which holds information about this shape
196
     *          shape.
168
     */
197
     */
169
    public EscherContainerRecord getShapeRecord(){
198
    public EscherContainerRecord getSpContainer(){
170
        return _escherContainer;
199
        return _escherContainer;
171
    }
200
    }
172
}
201
}
(-)src/scratchpad/src/org/apache/poi/hslf/model/Rectangle.java (-3 / +3 lines)
Lines 33-47 Link Here
33
33
34
    public Rectangle(Shape parent){
34
    public Rectangle(Shape parent){
35
        super(null, parent);
35
        super(null, parent);
36
        _escherContainer = create(parent instanceof ShapeGroup);
36
        _escherContainer = createSpContainer(parent instanceof ShapeGroup);
37
    }
37
    }
38
38
39
    public Rectangle(){
39
    public Rectangle(){
40
        this(null);
40
        this(null);
41
    }
41
    }
42
42
43
    protected EscherContainerRecord create(boolean isChild){
43
    protected EscherContainerRecord createSpContainer(boolean isChild){
44
        EscherContainerRecord spcont = super.create(isChild);
44
        EscherContainerRecord spcont = super.createSpContainer(isChild);
45
        spcont.setOptions((short)15);
45
        spcont.setOptions((short)15);
46
46
47
        EscherSpRecord spRecord = spcont.getChildById(EscherSpRecord.RECORD_ID);
47
        EscherSpRecord spRecord = spcont.getChildById(EscherSpRecord.RECORD_ID);
(-)src/scratchpad/src/org/apache/poi/hslf/model/Line.java (-4 / +3 lines)
Lines 96-111 Link Here
96
96
97
    public Line(Shape parent){
97
    public Line(Shape parent){
98
        super(null, parent);
98
        super(null, parent);
99
        _escherContainer = create(parent instanceof ShapeGroup);
99
        _escherContainer = createSpContainer(parent instanceof ShapeGroup);
100
    }
100
    }
101
101
102
    public Line(){
102
    public Line(){
103
        this(null);
103
        this(null);
104
    }
104
    }
105
105
106
    protected EscherContainerRecord create(boolean isChild){
106
    protected EscherContainerRecord createSpContainer(boolean isChild){
107
        EscherContainerRecord spcont = super.create(isChild);
107
        EscherContainerRecord spcont = super.createSpContainer(isChild);
108
        spcont.setOptions((short)15);
109
108
110
        EscherSpRecord spRecord = spcont.getChildById(EscherSpRecord.RECORD_ID);
109
        EscherSpRecord spRecord = spcont.getChildById(EscherSpRecord.RECORD_ID);
111
        short type = (ShapeTypes.Line << 4) + 2;
110
        short type = (ShapeTypes.Line << 4) + 2;
(-)src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java (-1 / +1 lines)
Lines 158-164 Link Here
158
158
159
	EscherContainerRecord dgContainer = (EscherContainerRecord)ppdrawing.getEscherRecords()[0];
159
	EscherContainerRecord dgContainer = (EscherContainerRecord)ppdrawing.getEscherRecords()[0];
160
	EscherContainerRecord spgr = (EscherContainerRecord)Shape.getEscherChild(dgContainer, EscherContainerRecord.SPGR_CONTAINER);
160
	EscherContainerRecord spgr = (EscherContainerRecord)Shape.getEscherChild(dgContainer, EscherContainerRecord.SPGR_CONTAINER);
161
	spgr.addChildRecord(shape.getShapeRecord());
161
	spgr.addChildRecord(shape.getSpContainer());
162
162
163
	EscherDgRecord dg = (EscherDgRecord)Shape.getEscherChild(dgContainer, EscherDgRecord.RECORD_ID);
163
	EscherDgRecord dg = (EscherDgRecord)Shape.getEscherChild(dgContainer, EscherDgRecord.RECORD_ID);
164
	dg.setNumShapes(dg.getNumShapes()+1);
164
	dg.setNumShapes(dg.getNumShapes()+1);
(-)src/scratchpad/src/org/apache/poi/hslf/model/ShapeFactory.java (-3 / +3 lines)
Lines 37-46 Link Here
37
        switch (type){
37
        switch (type){
38
            case ShapeTypes.TextBox:
38
            case ShapeTypes.TextBox:
39
            case ShapeTypes.Rectangle:
39
            case ShapeTypes.Rectangle:
40
                shape = new Shape(spContainer, parent);
40
                shape = new Rectangle(spContainer, parent);
41
                break;
41
                break;
42
            case ShapeTypes.PictureFrame:
42
            case ShapeTypes.PictureFrame:
43
                shape = new Shape(spContainer, parent);
43
                shape = new Picture(spContainer, parent);
44
                break;
44
                break;
45
            case ShapeTypes.Line:
45
            case ShapeTypes.Line:
46
                shape = new Line(spContainer, parent);
46
                shape = new Line(spContainer, parent);
Lines 52-58 Link Here
52
                shape = new ShapeGroup(spContainer, parent);
52
                shape = new ShapeGroup(spContainer, parent);
53
                break;
53
                break;
54
            default:
54
            default:
55
                shape = new Shape(spContainer, parent);
55
                shape = new SimpleShape(spContainer, parent);
56
                break;
56
                break;
57
        }
57
        }
58
        return shape;
58
        return shape;
(-)src/scratchpad/src/org/apache/poi/hslf/model/SimpleShape.java (-2 / +2 lines)
Lines 39-48 Link Here
39
     * @param isChild   <code>true</code> if the Line is inside a group, <code>false</code> otherwise
39
     * @param isChild   <code>true</code> if the Line is inside a group, <code>false</code> otherwise
40
     * @return the record container which holds this shape
40
     * @return the record container which holds this shape
41
     */
41
     */
42
    protected EscherContainerRecord create(boolean isChild) {
42
    protected EscherContainerRecord createSpContainer(boolean isChild) {
43
        EscherContainerRecord spContainer = new EscherContainerRecord();
43
        EscherContainerRecord spContainer = new EscherContainerRecord();
44
        spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
44
        spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
45
        //spContainer.setOptions((short)15);
45
        spContainer.setOptions((short)15);
46
46
47
        EscherSpRecord sp = new EscherSpRecord();
47
        EscherSpRecord sp = new EscherSpRecord();
48
        int flags = EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE;
48
        int flags = EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE;
(-)src/scratchpad/src/org/apache/poi/hslf/model/Ellipse.java (-4 / +3 lines)
Lines 33-48 Link Here
33
33
34
    public Ellipse(Shape parent){
34
    public Ellipse(Shape parent){
35
        super(null, parent);
35
        super(null, parent);
36
        _escherContainer = create(parent instanceof ShapeGroup);
36
        _escherContainer = createSpContainer(parent instanceof ShapeGroup);
37
    }
37
    }
38
38
39
    public Ellipse(){
39
    public Ellipse(){
40
        this(null);
40
        this(null);
41
    }
41
    }
42
42
43
    protected EscherContainerRecord create(boolean isChild){
43
    protected EscherContainerRecord createSpContainer(boolean isChild){
44
        EscherContainerRecord spcont = super.create(isChild);
44
        EscherContainerRecord spcont = super.createSpContainer(isChild);
45
        spcont.setOptions((short)15);
46
45
47
        EscherSpRecord spRecord = spcont.getChildById(EscherSpRecord.RECORD_ID);
46
        EscherSpRecord spRecord = spcont.getChildById(EscherSpRecord.RECORD_ID);
48
        short type = (ShapeTypes.Ellipse << 4) + 2;
47
        short type = (ShapeTypes.Ellipse << 4) + 2;
(-)src/scratchpad/src/org/apache/poi/hslf/model/ShapeGroup.java (-8 / +4 lines)
Lines 27-39 Link Here
27
 */
27
 */
28
public class ShapeGroup extends Shape{
28
public class ShapeGroup extends Shape{
29
29
30
    public ShapeGroup(Shape parent){
31
        super(null, parent);
32
        _escherContainer = create();
33
    }
34
35
    public ShapeGroup(){
30
    public ShapeGroup(){
36
        this(null);
31
        this(null, null);
32
        _escherContainer = createSpContainer(false);
37
    }
33
    }
38
34
39
    protected ShapeGroup(EscherContainerRecord escherRecord, Shape parent){
35
    protected ShapeGroup(EscherContainerRecord escherRecord, Shape parent){
Lines 91-97 Link Here
91
    /**
87
    /**
92
     * Create a new ShapeGroup and create an instance of <code>EscherSpgrContainer</code> which represents a group of shapes
88
     * Create a new ShapeGroup and create an instance of <code>EscherSpgrContainer</code> which represents a group of shapes
93
     */
89
     */
94
    protected EscherContainerRecord create() {
90
    protected EscherContainerRecord createSpContainer(boolean isChild) {
95
        EscherContainerRecord spgr = new EscherContainerRecord();
91
        EscherContainerRecord spgr = new EscherContainerRecord();
96
        spgr.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
92
        spgr.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
97
        spgr.setOptions((short)15);
93
        spgr.setOptions((short)15);
Lines 124-130 Link Here
124
     * @param shape - the Shape to add
120
     * @param shape - the Shape to add
125
     */
121
     */
126
    public void addShape(Shape shape){
122
    public void addShape(Shape shape){
127
        _escherContainer.addChildRecord(shape.getShapeRecord());
123
        _escherContainer.addChildRecord(shape.getSpContainer());
128
    }
124
    }
129
125
130
    /**
126
    /**
(-)src/scratchpad/src/org/apache/poi/hslf/record/RecordTypes.java (-1 / +1 lines)
Lines 60-66 Link Here
60
    public static final Type SorterViewInfo = new Type(1032,null);
60
    public static final Type SorterViewInfo = new Type(1032,null);
61
    public static final Type ExObjList = new Type(1033,null);
61
    public static final Type ExObjList = new Type(1033,null);
62
    public static final Type ExObjListAtom = new Type(1034,null);
62
    public static final Type ExObjListAtom = new Type(1034,null);
63
    public static final Type PPDrawingGroup = new Type(1035,null);
63
    public static final Type PPDrawingGroup = new Type(1035,PPDrawingGroup.class);
64
    public static final Type PPDrawing = new Type(1036,PPDrawing.class);
64
    public static final Type PPDrawing = new Type(1036,PPDrawing.class);
65
    public static final Type NamedShows = new Type(1040,null);
65
    public static final Type NamedShows = new Type(1040,null);
66
    public static final Type NamedShow = new Type(1041,null);
66
    public static final Type NamedShow = new Type(1041,null);
(-)src/scratchpad/src/org/apache/poi/hslf/record/Document.java (+11 lines)
Lines 36-41 Link Here
36
	// Links to our more interesting children
36
	// Links to our more interesting children
37
	private DocumentAtom documentAtom;
37
	private DocumentAtom documentAtom;
38
	private Environment environment;
38
	private Environment environment;
39
    private PPDrawingGroup ppDrawing;
39
	private SlideListWithText[] slwts;
40
	private SlideListWithText[] slwts;
40
41
41
	/**
42
	/**
Lines 47-52 Link Here
47
	 *  settings for the document in it
48
	 *  settings for the document in it
48
	 */
49
	 */
49
	public Environment getEnvironment() { return environment; }
50
	public Environment getEnvironment() { return environment; }
51
52
    /**
53
     *
54
     * @return PPDrawingGroup container
55
     */
56
    public PPDrawingGroup getPPDrawingGroup() { return ppDrawing; }
57
50
	/**
58
	/**
51
	 * Returns all the SlideListWithTexts that are defined for
59
	 * Returns all the SlideListWithTexts that are defined for
52
	 *  this Document. They hold the text, and some of the text
60
	 *  this Document. They hold the text, and some of the text
Lines 82-87 Link Here
82
			if(_children[i] instanceof Environment) {
90
			if(_children[i] instanceof Environment) {
83
				environment = (Environment)_children[i];
91
				environment = (Environment)_children[i];
84
			}
92
			}
93
            if(_children[i] instanceof PPDrawingGroup) {
94
                ppDrawing = (PPDrawingGroup)_children[i];
95
            }
85
		}
96
		}
86
		// Now grab them all
97
		// Now grab them all
87
		slwts = new SlideListWithText[slwtcount];
98
		slwts = new SlideListWithText[slwtcount];
(-)src/scratchpad/src/org/apache/poi/hslf/HSLFSlideShow.java (-6 / +51 lines)
Lines 36-42 Link Here
36
import org.apache.poi.util.LittleEndian;
36
import org.apache.poi.util.LittleEndian;
37
37
38
import org.apache.poi.hslf.record.*;
38
import org.apache.poi.hslf.record.*;
39
import org.apache.poi.hslf.usermodel.Picture;
39
import org.apache.poi.hslf.usermodel.PictureData;
40
40
41
/**
41
/**
42
 * This class contains the main functionality for the Powerpoint file 
42
 * This class contains the main functionality for the Powerpoint file 
Lines 61-66 Link Here
61
  // Low level contents
61
  // Low level contents
62
  private Record[] _records;
62
  private Record[] _records;
63
63
64
  //pictures
65
  private PictureData[] _pictures;
66
64
  /**
67
  /**
65
   * Constructs a Powerpoint document from fileName. Parses the document 
68
   * Constructs a Powerpoint document from fileName. Parses the document 
66
   * and places all the important stuff into data structures.
69
   * and places all the important stuff into data structures.
Lines 104-112 Link Here
104
107
105
		// Look for Property Streams:
108
		// Look for Property Streams:
106
		readProperties();
109
		readProperties();
110
111
        //read pictures
112
        readPictures();
107
  }
113
  }
108
114
109
115
116
    /**
117
     * Constructs a new Powerpoint document.
118
     */
119
    public HSLFSlideShow() throws IOException
120
    {
121
          this(HSLFSlideShow.class.getResourceAsStream("/org/apache/poi/hslf/data/empty.ppt"));
122
    }
123
110
  /**
124
  /**
111
   * Shuts things down. Closes underlying streams etc
125
   * Shuts things down. Closes underlying streams etc
112
   *
126
   *
Lines 288-293 Link Here
288
	currentUser.writeToFS(outFS);
302
	currentUser.writeToFS(outFS);
289
303
290
304
305
    //write pictures
306
    if (_pictures != null) {
307
        ByteArrayOutputStream pict = new ByteArrayOutputStream();
308
        for (int i = 0; i < _pictures.length; i++ ) {
309
            _pictures[i].write(pict);
310
        }
311
        outFS.createDocument(new ByteArrayInputStream(pict.toByteArray()), "Pictures");
312
    }
313
291
	// Send the POIFSFileSystem object out to the underlying stream
314
	// Send the POIFSFileSystem object out to the underlying stream
292
	outFS.writeFilesystem(out);
315
	outFS.writeFilesystem(out);
293
   }
316
   }
Lines 371-377 Link Here
371
	 *  @return array with the read pictures ot <code>null</code> if the
394
	 *  @return array with the read pictures ot <code>null</code> if the
372
	 *  presentation doesn't contain pictures.
395
	 *  presentation doesn't contain pictures.
373
	 */
396
	 */
374
	public Picture[] getPictures() throws IOException {
397
	private void readPictures() throws IOException {
375
		byte[] pictstream;
398
		byte[] pictstream;
376
399
377
		try {
400
		try {
Lines 381-397 Link Here
381
			is.read(pictstream);
404
			is.read(pictstream);
382
		} catch (FileNotFoundException e){
405
		} catch (FileNotFoundException e){
383
			//silently catch exceptions if the presentation doesn't contain pictures
406
			//silently catch exceptions if the presentation doesn't contain pictures
384
			return null;
407
			return;
385
		}
408
		}
386
409
387
		ArrayList p = new ArrayList();
410
		ArrayList p = new ArrayList();
388
		int pos = 0; 
411
		int pos = 0; 
389
		while (pos < pictstream.length) {
412
		while (pos < pictstream.length) {
390
			Picture pict = new Picture(pictstream, pos);
413
			PictureData pict = new PictureData(pictstream, pos);
391
			p.add(pict);
414
			p.add(pict);
392
			pos += Picture.HEADER_SIZE + pict.getSize();
415
			pos += PictureData.HEADER_SIZE + pict.getSize();
393
		}
416
		}
394
417
395
		return (Picture[])p.toArray(new Picture[p.size()]);
418
		_pictures = (PictureData[])p.toArray(new PictureData[p.size()]);
396
	}
419
	}
420
421
    /**
422
     *  Return array of pictures contained in this presentation
423
     *
424
     *  @return array with the read pictures ot <code>null</code> if the
425
     *  presentation doesn't contain pictures.
426
     */
427
    public PictureData[] getPictures() {
428
        return _pictures;
429
    }
430
431
    /**
432
     *  Add a new picture to this presentation.
433
     */
434
    public void addPicture(PictureData img) {
435
        PictureData[] lst = new PictureData[_pictures == null ? 1: (_pictures.length + 1)];
436
        if(_pictures != null ) for (int i = 0; i < _pictures.length; i++){
437
            lst[i] = _pictures[i];
438
        }
439
        lst[lst.length - 1] = img;
440
        _pictures = lst;
441
    }
397
}
442
}
(-)src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java (-4 / +54 lines)
Lines 17-27 Link Here
17
package org.apache.poi.hslf.usermodel;
17
package org.apache.poi.hslf.usermodel;
18
18
19
import org.apache.poi.hslf.*;
19
import org.apache.poi.hslf.*;
20
import org.apache.poi.hslf.usermodel.PictureData;
21
import org.apache.poi.hslf.usermodel.SlideShow;
22
import org.apache.poi.hslf.model.Slide;
23
import org.apache.poi.hslf.model.Shape;
24
import org.apache.poi.hslf.model.Picture;
25
import org.apache.poi.util.LittleEndian;
20
import junit.framework.TestCase;
26
import junit.framework.TestCase;
21
27
22
import javax.imageio.ImageIO;
28
import javax.imageio.ImageIO;
23
import java.awt.image.BufferedImage;
29
import java.awt.image.BufferedImage;
24
import java.io.ByteArrayInputStream;
30
import java.io.ByteArrayInputStream;
31
import java.io.ByteArrayOutputStream;
32
import java.io.File;
25
33
26
/**
34
/**
27
 * Test extracting images from a ppt file
35
 * Test extracting images from a ppt file
Lines 29-47 Link Here
29
 * @author Yegor Kozlov
37
 * @author Yegor Kozlov
30
 */
38
 */
31
public class TestPictures extends TestCase{
39
public class TestPictures extends TestCase{
40
    public static String dirname = System.getProperty("HSLF.testdata.path");
41
    public static String filename = dirname + "/ppt_with_png.ppt";
32
42
33
    public void testPictures() throws Exception {
43
    public void testReadPictures() throws Exception {
34
        String dirname = System.getProperty("HSLF.testdata.path");
35
        String filename = dirname + "/ppt_with_png.ppt";
36
44
37
        HSLFSlideShow ppt = new HSLFSlideShow(filename);
45
        HSLFSlideShow ppt = new HSLFSlideShow(filename);
38
        Picture[] pict = ppt.getPictures();
46
        PictureData[] pict = ppt.getPictures();
39
        assertNotNull(pict);
47
        assertNotNull(pict);
40
        for (int i = 0; i < pict.length; i++) {
48
        for (int i = 0; i < pict.length; i++) {
41
            byte[] data = pict[i].getData();
49
            byte[] data = pict[i].getData();
50
42
            BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
51
            BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));
43
            assertNotNull(img);
52
            assertNotNull(img);
44
        }
53
        }
45
        ppt.close();
54
        ppt.close();
46
    }
55
    }
56
57
    public void testSerializePictures() throws Exception {
58
        HSLFSlideShow ppt = new HSLFSlideShow(filename);
59
        PictureData[] pict = ppt.getPictures();
60
        assertNotNull(pict);
61
62
        ByteArrayOutputStream out = new ByteArrayOutputStream();
63
        ppt.write(out);
64
        out.close();
65
66
        ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
67
        pict = ppt.getPictures();
68
        assertNotNull(pict);
69
    }
70
71
    public void testAddPictures() throws Exception {
72
        int idx;
73
        Slide slide;
74
        Picture pict;
75
76
        SlideShow ppt = new SlideShow();
77
78
        idx = ppt.addPicture(new File(dirname + "/clock.jpg"), Picture.JPEG);
79
        slide = ppt.createSlide();
80
        pict = new Picture(idx);
81
        pict.setDefaultSize(ppt);
82
        slide.addShape(pict);
83
84
        idx = ppt.addPicture(new File(dirname + "/painting.png"), Picture.PNG);
85
        pict = new Picture(idx);
86
        pict.setDefaultSize(ppt);
87
        slide.addShape(pict);
88
89
        ByteArrayOutputStream out = new ByteArrayOutputStream();
90
        ppt.write(out);
91
        out.close();
92
93
        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));
94
        assertTrue(ppt.getPictures().length == 2 );
95
    }
96
47
}
97
}

Return to bug 39097