Correctly increment the reference count of a blip when a picture is inserted

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@664490 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-06-08 12:30:25 +00:00
parent e7f24cd7d4
commit ab0486268d
4 changed files with 101 additions and 23 deletions

View File

@ -238,6 +238,10 @@ public class EscherDggRecord
return maxDgId;
}
public void setMaxDrawingGroupId(int id){
maxDgId = id;
}
public FileIdCluster[] getFileIdClusters()
{
return field_5_fileIdClusters;

View File

@ -176,16 +176,11 @@ public class Picture extends SimpleShape {
public PictureData getPictureData(){
SlideShow ppt = getSheet().getSlideShow();
PictureData[] pict = ppt.getPictureData();
Document doc = ppt.getDocumentRecord();
EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
List lst = bstore.getChildRecords();
int idx = getPictureIndex();
if (idx == 0){
EscherBSERecord bse = getEscherBSERecord();
if (bse == null){
logger.log(POILogger.ERROR, "no reference to picture data found ");
} else {
EscherBSERecord bse = (EscherBSERecord)lst.get(idx-1);
for ( int i = 0; i < pict.length; i++ ) {
if (pict[i].getOffset() == bse.getOffset()){
return pict[i];
@ -196,6 +191,21 @@ public class Picture extends SimpleShape {
return null;
}
protected EscherBSERecord getEscherBSERecord(){
SlideShow ppt = getSheet().getSlideShow();
Document doc = ppt.getDocumentRecord();
EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
List lst = bstore.getChildRecords();
int idx = getPictureIndex();
if (idx == 0){
return null;
} else {
return (EscherBSERecord)lst.get(idx-1);
}
}
/**
* Name of this picture.
*
@ -238,6 +248,10 @@ public class Picture extends SimpleShape {
*/
protected void afterInsert(Sheet sh){
super.afterInsert(sh);
EscherBSERecord bse = getEscherBSERecord();
bse.setRef(bse.getRef() + 1);
java.awt.Rectangle anchor = getAnchor();
if (anchor.equals(new java.awt.Rectangle())){
setDefaultSize();
@ -249,21 +263,8 @@ public class Picture extends SimpleShape {
ShapePainter.paint(this, graphics);
PictureData data = getPictureData();
if (data instanceof Bitmap){
BufferedImage img = null;
try {
img = ImageIO.read(new ByteArrayInputStream(data.getData()));
}
catch (Exception e){
logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + data.getType());
return;
}
Rectangle anchor = getAnchor();
Image scaledImg = img.getScaledInstance(anchor.width, anchor.height, Image.SCALE_SMOOTH);
graphics.drawImage(scaledImg, anchor.x, anchor.y, null);
} else {
logger.log(POILogger.WARN, "Rendering of metafiles is not yet supported. image.type: " + (data == null ? "NA" : data.getClass().getName()));
}
data.draw(graphics, this);
graphics.setTransform(at);
}
}

View File

@ -734,7 +734,7 @@ public class SlideShow
else if (format == Picture.WMF) bse.setBlipTypeMacOS((byte)Picture.PICT);
else if (format == Picture.PICT) bse.setBlipTypeWin32((byte)Picture.WMF);
bse.setRef(1);
bse.setRef(0);
bse.setOffset(offset);
bstore.addChildRecord(bse);

View File

@ -0,0 +1,73 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.model;
import junit.framework.*;
import java.io.FileOutputStream;
import java.io.File;
import java.awt.*;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.ddf.EscherBSERecord;
/**
* Test Picture shape.
*
* @author Yegor Kozlov
*/
public class TestPicture extends TestCase {
/**
* Test that the reference count of a blip is incremented every time the picture is inserted.
* This is important when the same image appears multiple times in a slide show.
*
*/
public void testMultiplePictures() throws Exception {
String cwd = System.getProperty("HSLF.testdata.path");
SlideShow ppt = new SlideShow();
Slide s = ppt.createSlide();
Slide s2 = ppt.createSlide();
Slide s3 = ppt.createSlide();
int idx = ppt.addPicture(new File(cwd, "clock.jpg"), Picture.JPEG);
Picture pict = new Picture(idx);
Picture pict2 = new Picture(idx);
Picture pict3 = new Picture(idx);
pict.setAnchor(new Rectangle(10,10,100,100));
s.addShape(pict);
EscherBSERecord bse1 = pict.getEscherBSERecord();
assertEquals(1, bse1.getRef());
pict2.setAnchor(new Rectangle(10,10,100,100));
s2.addShape(pict2);
EscherBSERecord bse2 = pict.getEscherBSERecord();
assertSame(bse1, bse2);
assertEquals(2, bse1.getRef());
pict3.setAnchor(new Rectangle(10,10,100,100));
s3.addShape(pict3);
EscherBSERecord bse3 = pict.getEscherBSERecord();
assertSame(bse2, bse3);
assertEquals(3, bse1.getRef());
}
}