#58190 - The current picture handling uses raw integers for types and index, replace with enum and reference

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1693825 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2015-08-02 20:21:35 +00:00
parent 74b13dee22
commit ff4bbd9588
35 changed files with 604 additions and 675 deletions

View File

@ -77,6 +77,7 @@ public final class DataExtraction {
if ("Worksheet".equals(name)) {
//read xls
@SuppressWarnings({ "unused", "resource" })
HSSFWorkbook wb = new HSSFWorkbook(data.getData());
} else if ("Document".equals(name)) {
@ -110,31 +111,7 @@ public final class DataExtraction {
picIdx++;
HSLFPictureShape p = (HSLFPictureShape) shape;
HSLFPictureData data = p.getPictureData();
String name = p.getPictureName();
int type = data.getType();
String ext;
switch (type) {
case HSLFPictureShape.JPEG:
ext = ".jpg";
break;
case HSLFPictureShape.PNG:
ext = ".png";
break;
case HSLFPictureShape.WMF:
ext = ".wmf";
break;
case HSLFPictureShape.EMF:
ext = ".emf";
break;
case HSLFPictureShape.PICT:
ext = ".pict";
break;
case HSLFPictureShape.DIB:
ext = ".dib";
break;
default:
continue;
}
String ext = data.getType().extension;
FileOutputStream out = new FileOutputStream("pict-" + picIdx + ext);
out.write(data.getData());
out.close();

View File

@ -25,6 +25,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
/**
* Images
@ -39,7 +40,7 @@ public class Tutorial5 {
XSLFSlide slide = ppt.createSlide();
File img = new File(System.getProperty("POI.testdata.path"), "slideshow/clock.jpg");
byte[] data = IOUtils.toByteArray(new FileInputStream(img));
int pictureIndex = ppt.addPicture(data, XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureData pictureIndex = ppt.addPicture(data, PictureType.PNG);
/*XSLFPictureShape shape =*/ slide.createPicture(pictureIndex);

View File

@ -20,8 +20,80 @@ package org.apache.poi.sl.usermodel;
import java.io.IOException;
public interface PictureData {
public String getContentType();
enum PictureType {
/** Extended windows meta file */
EMF(2,2,"image/x-emf",".emf"),
/** Windows Meta File */
WMF(3,3,"image/x-wmf",".wmf"),
/** Mac PICT format */
PICT(4,4,"image/pict",".pict"), // or image/x-pict (for HSLF) ???
/** JPEG format */
JPEG(5,5,"image/jpeg",".jpg"),
/** PNG format */
PNG(6,6,"image/png",".png"),
/** Device independent bitmap */
DIB(7,7,"image/dib",".dib"),
/** GIF image format */
GIF(-1,8,"image/gif",".gif"),
/** Tag Image File (.tiff) */
TIFF(-1,9,"image/tiff",".tif"),
/** Encapsulated Postscript (.eps) */
EPS(-1,10,"image/x-eps",".eps"),
/** Windows Bitmap (.bmp) */
BMP(-1,11,"image/x-ms-bmp",".bmp"),
/** WordPerfect graphics (.wpg) */
WPG(-1,12,"image/x-wpg",".wpg"),
/** Microsoft Windows Media Photo image (.wdp) */
WDP(-1,13,"image/vnd.ms-photo",".wdp");
public final int nativeId, ooxmlId;
public final String contentType,extension;
public byte[] getData();
public void setData(byte[] data) throws IOException;
PictureType(int nativeId, int ooxmlId,String contentType,String extension) {
this.nativeId = nativeId;
this.ooxmlId = ooxmlId;
this.contentType = contentType;
this.extension = extension;
}
public static PictureType forNativeID(int nativeId) {
for (PictureType ans : values()) {
if (ans.nativeId == nativeId) return ans;
}
return null;
}
public static PictureType forOoxmlID(int ooxmlId) {
for (PictureType ans : values()) {
if (ans.ooxmlId == ooxmlId) return ans;
}
return null;
}
}
/**
* Returns content type (mime type) of this picture.
*
* @return content type of this picture.
*/
String getContentType();
/**
* @return the picture type
*/
PictureType getType();
/**
* Returns the binary data of this Picture
* @return picture data
*/
byte[] getData();
/**
* Sets the binary picture data
* @param data picture data
*/
void setData(byte[] data) throws IOException;
}

View File

@ -21,6 +21,9 @@ import java.awt.Dimension;
import java.io.IOException;
import java.util.List;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
public interface SlideShow {
Slide<? extends Shape, ? extends SlideShow, ? extends Notes<?,?>> createSlide() throws IOException;
@ -41,5 +44,16 @@ public interface SlideShow {
*
* @return the page size
*/
Dimension getPageSize();
Dimension getPageSize();
/**
* Adds a picture to the workbook.
*
* @param pictureData The bytes of the picture
* @param format The format of the picture.
*
* @return the new picture reference
*/
PictureData addPicture(byte[] pictureData, PictureType format) throws IOException;
}

View File

@ -36,6 +36,7 @@ import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.sl.usermodel.MasterSheet;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.Resources;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.util.Beta;
@ -192,7 +193,9 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow {
List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?"));
_pictures = new ArrayList<XSLFPictureData>(mediaParts.size());
for(PackagePart part : mediaParts){
_pictures.add(new XSLFPictureData(part, null));
XSLFPictureData pd = new XSLFPictureData(part, null);
pd.setIndex(_pictures.size());
_pictures.add(pd);
}
}
return Collections.unmodifiableList(_pictures);
@ -442,26 +445,28 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow {
* @see XSLFPictureData#PICTURE_TYPE_PNG
* @see XSLFPictureData#PICTURE_TYPE_DIB
*/
public int addPicture(byte[] pictureData, int format) {
public XSLFPictureData addPicture(byte[] pictureData, PictureType format) {
XSLFPictureData img = findPictureData(pictureData);
// POIXMLRelation relDesc = XSLFPictureData.RELATIONS[format];
if(img == null) {
int imageNumber = _pictures.size();
img = (XSLFPictureData) createRelationship(
XSLFPictureData.RELATIONS[format], XSLFFactory.getInstance(), imageNumber + 1, true);
_pictures.add(img);
try {
OutputStream out = img.getPackagePart().getOutputStream();
out.write(pictureData);
out.close();
} catch (IOException e) {
throw new POIXMLException(e);
}
return _pictures.size() - 1;
} else {
return _pictures.indexOf(img);
if (img != null) return img;
int imageNumber = _pictures.size();
XSLFRelation relType = XSLFPictureData.getRelationForType(format);
if (relType == null) {
throw new IllegalArgumentException("Picture type "+format+" is not supported.");
}
img = (XSLFPictureData) createRelationship(relType, XSLFFactory.getInstance(), imageNumber + 1, true);
img.setIndex(imageNumber);
_pictures.add(img);
try {
OutputStream out = img.getPackagePart().getOutputStream();
out.write(pictureData);
out.close();
} catch (IOException e) {
throw new POIXMLException(e);
}
return img;
}
/**
@ -492,7 +497,7 @@ public class XMLSlideShow extends POIXMLDocument implements SlideShow {
return null;
}
public MasterSheet createMasterSheet() throws IOException {
public MasterSheet<XSLFShape, XMLSlideShow> createMasterSheet() throws IOException {
// TODO: implement!
throw new UnsupportedOperationException();
}

View File

@ -23,7 +23,6 @@ import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
@ -239,16 +238,8 @@ public class XSLFGroupShape extends XSLFShape implements XSLFShapeContainer, Gro
return sh;
}
public XSLFPictureShape createPicture(int pictureIndex){
List<PackagePart> pics = getSheet().getPackagePart().getPackage()
.getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));
if(pics.size() == 0) {
throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");
}
PackagePart pic = pics.get(0);
public XSLFPictureShape createPicture(XSLFPictureData pictureData){
PackagePart pic = pictureData.getPackagePart();
PackageRelationship rel = getSheet().getPackagePart().addRelationship(
pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
@ -321,8 +312,8 @@ public class XSLFGroupShape extends XSLFShape implements XSLFShapeContainer, Gro
} else if (shape instanceof XSLFPictureShape) {
XSLFPictureShape p = (XSLFPictureShape)shape;
XSLFPictureData pd = p.getPictureData();
int picId = getSheet().getSlideShow().addPicture(pd.getData(), pd.getPictureType());
newShape = createPicture(picId);
XSLFPictureData pdNew = getSheet().getSlideShow().addPicture(pd.getData(), pd.getType());
newShape = createPicture(pdNew);
} else if (shape instanceof XSLFGroupShape) {
newShape = createGroup();
} else if (shape instanceof XSLFTable) {

View File

@ -25,7 +25,6 @@ import java.io.OutputStream;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLException;
import org.apache.poi.POIXMLRelation;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.sl.usermodel.PictureData;
@ -34,94 +33,11 @@ import org.apache.poi.util.IOUtils;
/**
* Instantiates sub-classes of POIXMLDocumentPart depending on their relationship type
*
* @author Yegor Kozlov
*/
@Beta
public final class XSLFPictureData extends POIXMLDocumentPart implements PictureData {
/**
* Extended windows meta file
*/
public static final int PICTURE_TYPE_EMF = 2;
/**
* Windows Meta File
*/
public static final int PICTURE_TYPE_WMF = 3;
/**
* Mac PICT format
*/
public static final int PICTURE_TYPE_PICT = 4;
/**
* JPEG format
*/
public static final int PICTURE_TYPE_JPEG = 5;
/**
* PNG format
*/
public static final int PICTURE_TYPE_PNG = 6;
/**
* Device independent bitmap
*/
public static final int PICTURE_TYPE_DIB = 7;
/**
* GIF image format
*/
public static final int PICTURE_TYPE_GIF = 8;
/**
* Tag Image File (.tiff)
*/
public static final int PICTURE_TYPE_TIFF = 9;
/**
* Encapsulated Postscript (.eps)
*/
public static final int PICTURE_TYPE_EPS = 10;
/**
* Windows Bitmap (.bmp)
*/
public static final int PICTURE_TYPE_BMP = 11;
/**
* WordPerfect graphics (.wpg)
*/
public static final int PICTURE_TYPE_WPG = 12;
/**
* Microsoft Windows Media Photo image (.wdp)
*/
public static final int PICTURE_TYPE_WDP = 13;
/**
* Relationships for each known picture type
*/
protected static final POIXMLRelation[] RELATIONS;
static {
RELATIONS = new POIXMLRelation[14];
RELATIONS[PICTURE_TYPE_EMF] = XSLFRelation.IMAGE_EMF;
RELATIONS[PICTURE_TYPE_WMF] = XSLFRelation.IMAGE_WMF;
RELATIONS[PICTURE_TYPE_PICT] = XSLFRelation.IMAGE_PICT;
RELATIONS[PICTURE_TYPE_JPEG] = XSLFRelation.IMAGE_JPEG;
RELATIONS[PICTURE_TYPE_PNG] = XSLFRelation.IMAGE_PNG;
RELATIONS[PICTURE_TYPE_DIB] = XSLFRelation.IMAGE_DIB;
RELATIONS[PICTURE_TYPE_GIF] = XSLFRelation.IMAGE_GIF;
RELATIONS[PICTURE_TYPE_TIFF] = XSLFRelation.IMAGE_TIFF;
RELATIONS[PICTURE_TYPE_EPS] = XSLFRelation.IMAGE_EPS;
RELATIONS[PICTURE_TYPE_BMP] = XSLFRelation.IMAGE_BMP;
RELATIONS[PICTURE_TYPE_WPG] = XSLFRelation.IMAGE_WPG;
RELATIONS[PICTURE_TYPE_WDP] = XSLFRelation.IMAGE_WDP;
}
private Long checksum = null;
private int index = -1;
/**
* Create a new XSLFGraphicData node
@ -150,12 +66,12 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
public InputStream getInputStream() throws IOException {
return getPackagePart().getInputStream();
}
/**
* Gets the picture data as a byte array.
*
* You can grab the picture data directly from the underlying package part with the {@link #getInputStream()} method
*
*
* @return the Picture data.
*/
public byte[] getData() {
@ -187,25 +103,6 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
return getPackagePart().getPartName().getExtension();
}
/**
* Return an integer constant that specifies type of this picture
*
* @return an integer constant that specifies type of this picture
*/
public int getPictureType() {
String contentType = getPackagePart().getContentType();
for (int i = 0; i < RELATIONS.length; i++) {
if (RELATIONS[i] == null) {
continue;
}
if (RELATIONS[i].getContentType().equals(contentType)) {
return i;
}
}
return 0;
}
long getChecksum(){
if(checksum == null){
try {
@ -218,17 +115,17 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
}
/**
* *PictureData objects store the actual content in the part directly without keeping a
* *PictureData objects store the actual content in the part directly without keeping a
* copy like all others therefore we need to handle them differently.
*/
@Override
protected void prepareForCommit() {
// do not clear the part here
}
@Override
public String getContentType() {
POIXMLRelation rel = RELATIONS[getPictureType()];
return (rel == null) ? null : rel.getContentType();
return getPackagePart().getContentType();
}
public void setData(byte[] data) throws IOException {
@ -238,6 +135,65 @@ public final class XSLFPictureData extends POIXMLDocumentPart implements Picture
// recalculate now since we already have the data bytes available anyhow
checksum = IOUtils.calculateChecksum(data);
}
@Override
public PictureType getType() {
String ct = getContentType();
if (XSLFRelation.IMAGE_EMF.getContentType().equals(ct)) {
return PictureType.EMF;
} else if (XSLFRelation.IMAGE_WMF.getContentType().equals(ct)) {
return PictureType.WMF;
} else if (XSLFRelation.IMAGE_PICT.getContentType().equals(ct)) {
return PictureType.PICT;
} else if (XSLFRelation.IMAGE_JPEG.getContentType().equals(ct)) {
return PictureType.JPEG;
} else if (XSLFRelation.IMAGE_PNG.getContentType().equals(ct)) {
return PictureType.PNG;
} else if (XSLFRelation.IMAGE_DIB.getContentType().equals(ct)) {
return PictureType.DIB;
} else if (XSLFRelation.IMAGE_GIF.getContentType().equals(ct)) {
return PictureType.GIF;
} else if (XSLFRelation.IMAGE_EPS.getContentType().equals(ct)) {
return PictureType.EPS;
} else if (XSLFRelation.IMAGE_BMP.getContentType().equals(ct)) {
return PictureType.BMP;
} else if (XSLFRelation.IMAGE_WPG.getContentType().equals(ct)) {
return PictureType.WPG;
} else if (XSLFRelation.IMAGE_WDP.getContentType().equals(ct)) {
return PictureType.WDP;
} else {
return null;
}
}
/* package */ static XSLFRelation getRelationForType(PictureType pt) {
switch (pt) {
case EMF: return XSLFRelation.IMAGE_EMF;
case WMF: return XSLFRelation.IMAGE_WMF;
case PICT: return XSLFRelation.IMAGE_PICT;
case JPEG: return XSLFRelation.IMAGE_JPEG;
case PNG: return XSLFRelation.IMAGE_PNG;
case DIB: return XSLFRelation.IMAGE_DIB;
case GIF: return XSLFRelation.IMAGE_GIF;
case EPS: return XSLFRelation.IMAGE_EPS;
case BMP: return XSLFRelation.IMAGE_BMP;
case WPG: return XSLFRelation.IMAGE_WPG;
case WDP: return XSLFRelation.IMAGE_WDP;
default: return null;
}
}
/**
* @return the 0-based index of this pictures within the picture parts
*/
public int getIndex() {
return index;
}
/**
* @param index sets the 0-based index of this pictures within the picture parts
*/
public void setIndex(int index) {
this.index = index;
}
}

View File

@ -21,15 +21,12 @@ import java.util.Map;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLRelation;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.Beta;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@Beta
public class XSLFRelation extends POIXMLRelation {
private static final POILogger log = POILogFactory.getLogger(XSLFRelation.class);
/**
* A map to lookup POIXMLRelation by its relation type
*/
@ -148,76 +145,76 @@ public class XSLFRelation extends POIXMLRelation {
);
public static final XSLFRelation IMAGE_EMF = new XSLFRelation(
"image/x-emf",
PictureType.EMF.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.emf",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_WMF = new XSLFRelation(
"image/x-wmf",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.wmf",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_PICT = new XSLFRelation(
"image/pict",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.pict",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_JPEG = new XSLFRelation(
"image/jpeg",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.jpeg",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_PNG = new XSLFRelation(
"image/png",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.png",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_DIB = new XSLFRelation(
"image/dib",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.dib",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_GIF = new XSLFRelation(
"image/gif",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.gif",
XSLFPictureData.class
);
);
public static final XSLFRelation IMAGE_WMF = new XSLFRelation(
PictureType.WMF.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.wmf",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_PICT = new XSLFRelation(
PictureType.PICT.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.pict",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_JPEG = new XSLFRelation(
PictureType.JPEG.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.jpeg",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_PNG = new XSLFRelation(
PictureType.PNG.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.png",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_DIB = new XSLFRelation(
PictureType.DIB.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.dib",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_GIF = new XSLFRelation(
PictureType.GIF.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.gif",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_TIFF = new XSLFRelation(
"image/tiff",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.tiff",
XSLFPictureData.class
PictureType.TIFF.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.tiff",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_EPS = new XSLFRelation(
"image/x-eps",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.eps",
XSLFPictureData.class
PictureType.EPS.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.eps",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_BMP = new XSLFRelation(
"image/x-ms-bmp",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.bmp",
XSLFPictureData.class
PictureType.BMP.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.bmp",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_WPG = new XSLFRelation(
"image/x-wpg",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.wpg",
XSLFPictureData.class
PictureType.WPG.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.wpg",
XSLFPictureData.class
);
public static final XSLFRelation IMAGE_WDP = new XSLFRelation(
"image/vnd.ms-photo",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.wdp",
XSLFPictureData.class
PictureType.WDP.contentType,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/image#.wdp",
XSLFPictureData.class
);
public static final XSLFRelation IMAGES = new XSLFRelation(

View File

@ -55,7 +55,7 @@ public interface XSLFShapeContainer extends ShapeContainer<XSLFShape> {
/**
* create a picture belonging to this container
*/
XSLFPictureShape createPicture(int pictureIndex);
XSLFPictureShape createPicture(XSLFPictureData pictureData);
/**
* Removes all of the elements from this container (optional operation).

View File

@ -25,7 +25,6 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.xml.namespace.QName;
@ -182,15 +181,8 @@ public abstract class XSLFSheet extends POIXMLDocumentPart implements XSLFShapeC
return sh;
}
public XSLFPictureShape createPicture(int pictureIndex){
List<PackagePart> pics = getPackagePart().getPackage()
.getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));
if(pics.size() == 0) {
throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");
}
PackagePart pic = pics.get(0);
public XSLFPictureShape createPicture(XSLFPictureData pictureData){
PackagePart pic = pictureData.getPackagePart();
PackageRelationship rel = getPackagePart().addRelationship(
pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
@ -525,8 +517,8 @@ public abstract class XSLFSheet extends POIXMLDocumentPart implements XSLFShapeC
XSLFPictureData data = new XSLFPictureData(blipPart, null);
XMLSlideShow ppt = getSlideShow();
int pictureIdx = ppt.addPicture(data.getData(), data.getPictureType());
PackagePart pic = ppt.getAllPictures().get(pictureIdx).getPackagePart();
XSLFPictureData pictureData = ppt.addPicture(data.getData(), data.getType());
PackagePart pic = pictureData.getPackagePart();
PackageRelationship rel = getPackagePart().addRelationship(
pic.getPartName(), TargetMode.INTERNAL, blipRel.getRelationshipType());

View File

@ -37,6 +37,7 @@ import javax.imageio.ImageIO;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.xslf.usermodel.DrawingParagraph;
import org.apache.poi.xslf.usermodel.DrawingTextBody;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
@ -48,6 +49,8 @@ import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.junit.Ignore;
import org.junit.Test;
public class TestXSLFBugs {
@Test
@ -56,7 +59,6 @@ public class TestXSLFBugs {
XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("51187.pptx");
assertEquals(1, ss.getSlides().size());
XSLFSlide slide = ss.getSlides().get(0);
// Check the relations on it
// Note - rId3 is a self reference
@ -289,11 +291,11 @@ public class TestXSLFBugs {
// Add a few pictures
for (int i=0; i<10; i++) {
int idx = ss.addPicture(pics[i], XSLFPictureData.PICTURE_TYPE_JPEG);
assertEquals(i, idx);
XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
assertEquals(i, data.getIndex());
assertEquals(i+1, ss.getAllPictures().size());
XSLFPictureShape shape = slide.createPicture(idx);
XSLFPictureShape shape = slide.createPicture(data);
assertNotNull(shape.getPictureData());
assertArrayEquals(pics[i], shape.getPictureData().getData());
assertEquals(i+2, slide.getShapes().size());
@ -307,11 +309,11 @@ public class TestXSLFBugs {
// Add past 10
for (int i=10; i<15; i++) {
int idx = ss.addPicture(pics[i], XSLFPictureData.PICTURE_TYPE_JPEG);
assertEquals(i, idx);
XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
assertEquals(i, data.getIndex());
assertEquals(i+1, ss.getAllPictures().size());
XSLFPictureShape shape = slide.createPicture(idx);
XSLFPictureShape shape = slide.createPicture(data);
assertNotNull(shape.getPictureData());
assertArrayEquals(pics[i], shape.getPictureData().getData());
assertEquals(i+2, slide.getShapes().size());
@ -324,11 +326,11 @@ public class TestXSLFBugs {
}
// Add a duplicate, check the right one is picked
int idx = ss.addPicture(pics[3], XSLFPictureData.PICTURE_TYPE_JPEG);
assertEquals(3, idx);
XSLFPictureData data = ss.addPicture(pics[3], PictureType.JPEG);
assertEquals(3, data.getIndex());
assertEquals(15, ss.getAllPictures().size());
XSLFPictureShape shape = slide.createPicture(idx);
XSLFPictureShape shape = slide.createPicture(data);
assertNotNull(shape.getPictureData());
assertArrayEquals(pics[3], shape.getPictureData().getData());
assertEquals(17, slide.getShapes().size());
@ -351,11 +353,11 @@ public class TestXSLFBugs {
assertArrayEquals(pics[3], shape.getPictureData().getData());
// Add another duplicate
idx = ss.addPicture(pics[5], XSLFPictureData.PICTURE_TYPE_JPEG);
assertEquals(5, idx);
data = ss.addPicture(pics[5], PictureType.JPEG);
assertEquals(5, data.getIndex());
assertEquals(15, ss.getAllPictures().size());
shape = slide.createPicture(idx);
shape = slide.createPicture(data);
assertNotNull(shape.getPictureData());
assertArrayEquals(pics[5], shape.getPictureData().getData());
assertEquals(18, slide.getShapes().size());

View File

@ -16,11 +16,17 @@
==================================================================== */
package org.apache.poi.xslf.usermodel;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xslf.XSLFTestDataSamples;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.junit.Test;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
@ -35,21 +41,21 @@ public class TestXSLFPictureShape {
assertEquals(0, ppt.getAllPictures().size());
byte[] data1 = new byte[100];
for(int i = 0;i < 100;i++) { data1[i] = (byte)i; }
int idx1 = ppt.addPicture(data1, XSLFPictureData.PICTURE_TYPE_JPEG);
assertEquals(0, idx1);
XSLFPictureData pdata1 = ppt.addPicture(data1, PictureType.JPEG);
assertEquals(0, pdata1.getIndex());
assertEquals(1, ppt.getAllPictures().size());
XSLFSlide slide = ppt.createSlide();
XSLFPictureShape shape1 = slide.createPicture(idx1);
XSLFPictureShape shape1 = slide.createPicture(pdata1);
assertNotNull(shape1.getPictureData());
assertArrayEquals(data1, shape1.getPictureData().getData());
byte[] data2 = new byte[200];
for(int i = 0;i < 200;i++) { data2[i] = (byte)i; }
int idx2 = ppt.addPicture(data2, XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape shape2 = slide.createPicture(idx2);
XSLFPictureData pdata2 = ppt.addPicture(data2, PictureType.PNG);
XSLFPictureShape shape2 = slide.createPicture(pdata2);
assertNotNull(shape2.getPictureData());
assertEquals(1, idx2);
assertEquals(1, pdata2.getIndex());
assertEquals(2, ppt.getAllPictures().size());
assertArrayEquals(data2, shape2.getPictureData().getData());
@ -75,12 +81,11 @@ public class TestXSLFPictureShape {
// first add 20 images to the slide
for (int i = 0; i < 20; i++, pictureIndex++) {
byte[] data = new byte[]{(byte)pictureIndex};
int elementIndex = ppt.addPicture(data,
XSLFPictureData.PICTURE_TYPE_PNG);
assertEquals(pictureIndex, elementIndex); // added images have indexes 0,1,2....19
XSLFPictureShape picture = slide1.createPicture(elementIndex);
XSLFPictureData elementData = ppt.addPicture(data, PictureType.PNG);
assertEquals(pictureIndex, elementData.getIndex()); // added images have indexes 0,1,2....19
XSLFPictureShape picture = slide1.createPicture(elementData);
// POI saves images as image1.png, image2.png, etc.
String fileName = "image" + (elementIndex + 1) + ".png";
String fileName = "image" + (elementData.getIndex()+1) + ".png";
assertEquals(fileName, picture.getPictureData().getFileName());
assertArrayEquals(data, picture.getPictureData().getData());
}
@ -88,11 +93,10 @@ public class TestXSLFPictureShape {
// and then add next 20 images to a group
for (int i = 0; i < 20; i++, pictureIndex++) {
byte[] data = new byte[]{(byte)pictureIndex};
int elementIndex = ppt.addPicture(data,
XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape picture = group1.createPicture(elementIndex);
XSLFPictureData elementData = ppt.addPicture(data, PictureType.PNG);
XSLFPictureShape picture = group1.createPicture(elementData);
// POI saves images as image1.png, image2.png, etc.
assertEquals(pictureIndex, elementIndex); // added images have indexes 0,1,2....19
assertEquals(pictureIndex, elementData.getIndex()); // added images have indexes 0,1,2....19
String fileName = "image" + (pictureIndex + 1) + ".png";
assertEquals(fileName, picture.getPictureData().getFileName());
assertArrayEquals(data, picture.getPictureData().getData());
@ -122,13 +126,13 @@ public class TestXSLFPictureShape {
XMLSlideShow ppt = new XMLSlideShow();
byte[] img1 = new byte[]{1,2,3};
byte[] img2 = new byte[]{3,4,5};
int idx1 = ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG);
assertEquals(0, idx1);
assertEquals(0, ppt.addPicture(img1, XSLFPictureData.PICTURE_TYPE_PNG));
XSLFPictureData pdata1 = ppt.addPicture(img1, PictureType.PNG);
assertEquals(0, pdata1.getIndex());
assertEquals(0, ppt.addPicture(img1, PictureType.PNG).getIndex());
int idx2 = ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG);
assertEquals(1, idx2);
assertEquals(1, ppt.addPicture(img2, XSLFPictureData.PICTURE_TYPE_PNG));
XSLFPictureData idx2 = ppt.addPicture(img2, PictureType.PNG);
assertEquals(1, idx2.getIndex());
assertEquals(1, ppt.addPicture(img2, PictureType.PNG).getIndex());
XSLFSlide slide1 = ppt.createSlide();
assertNotNull(slide1);
@ -141,10 +145,10 @@ public class TestXSLFPictureShape {
public void testMerge() {
XMLSlideShow ppt1 = new XMLSlideShow();
byte[] data1 = new byte[100];
int idx1 = ppt1.addPicture(data1, XSLFPictureData.PICTURE_TYPE_JPEG);
XSLFPictureData pdata1 = ppt1.addPicture(data1, PictureType.JPEG);
XSLFSlide slide1 = ppt1.createSlide();
XSLFPictureShape shape1 = slide1.createPicture(idx1);
XSLFPictureShape shape1 = slide1.createPicture(pdata1);
CTPicture ctPic1 = (CTPicture)shape1.getXmlObject();
ctPic1.getNvPicPr().getNvPr().addNewCustDataLst().addNewTags().setId("rId99");

View File

@ -25,11 +25,10 @@ import java.io.ByteArrayOutputStream;
/**
* Represents a bitmap picture data: JPEG or PNG.
* The data is not compressed and the exact file content is written in the stream.
*
* @author Yegor Kozlov
*/
public abstract class Bitmap extends HSLFPictureData {
public abstract class Bitmap extends HSLFPictureData {
@Override
public byte[] getData(){
byte[] rawdata = getRawData();
int prefixLen = 16*uidInstanceCount+1;
@ -38,6 +37,7 @@ public abstract class Bitmap extends HSLFPictureData {
return imgdata;
}
@Override
public void setData(byte[] data) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i=0; i<uidInstanceCount; i++) {

View File

@ -17,15 +17,12 @@
package org.apache.poi.hslf.blip;
import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.LittleEndian;
import java.io.IOException;
import org.apache.poi.util.LittleEndian;
/**
* Represents a DIB picture data in a PPT file
*
* @author Yegor Kozlov
*/
public final class DIB extends Bitmap {
/**
@ -33,12 +30,9 @@ public final class DIB extends Bitmap {
*/
private static final int HEADER_SIZE = 14;
/**
* @return type of this picture
* @see org.apache.poi.hslf.usermodel.HSLFPictureShape#DIB
*/
public int getType(){
return HSLFPictureShape.DIB;
@Override
public PictureType getType(){
return PictureType.DIB;
}
/**
@ -66,6 +60,7 @@ public final class DIB extends Bitmap {
}
}
@Override
public byte[] getData(){
return addBMPHeader ( super.getData() );
}
@ -99,14 +94,11 @@ public final class DIB extends Bitmap {
return dib;
}
@Override
public void setData(byte[] data) throws IOException {
//cut off the bitmap file-header
byte[] dib = new byte[data.length-HEADER_SIZE];
System.arraycopy(data, HEADER_SIZE, dib, 0, dib.length);
super.setData(dib);
}
public String getContentType() {
return "image/bmp";
}
}

View File

@ -24,19 +24,14 @@ import java.io.InputStream;
import java.util.zip.InflaterInputStream;
import org.apache.poi.hslf.exceptions.HSLFException;
import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.Units;
/**
* Represents EMF (Windows Enhanced Metafile) picture data.
*
* @author Yegor Kozlov
*/
public final class EMF extends Metafile {
/**
* Extract compressed EMF data from a ppt
*/
@Override
public byte[] getData(){
try {
byte[] rawdata = getRawData();
@ -60,6 +55,7 @@ public final class EMF extends Metafile {
}
}
@Override
public void setData(byte[] data) throws IOException {
byte[] compressed = compress(data, 0, data.length);
@ -79,8 +75,9 @@ public final class EMF extends Metafile {
setRawData(out.toByteArray());
}
public int getType(){
return HSLFPictureShape.EMF;
@Override
public PictureType getType(){
return PictureType.EMF;
}
/**
@ -107,8 +104,4 @@ public final class EMF extends Metafile {
throw new IllegalArgumentException(signature+" is not a valid instance/signature value for EMF");
}
}
public String getContentType() {
return "image/x-emf";
}
}

View File

@ -17,12 +17,9 @@
package org.apache.poi.hslf.blip;
import org.apache.poi.hslf.usermodel.HSLFPictureShape;
/**
* Represents a JPEG picture data in a PPT file
*
* @author Yegor Kozlov
*/
public final class JPEG extends Bitmap {
@ -30,12 +27,9 @@ public final class JPEG extends Bitmap {
private ColorSpace colorSpace = ColorSpace.rgb;
/**
* @return type of this picture
* @see org.apache.poi.hslf.usermodel.HSLFPictureShape#JPEG
*/
public int getType(){
return HSLFPictureShape.JPEG;
@Override
public PictureType getType(){
return PictureType.JPEG;
}
public ColorSpace getColorSpace() {
@ -82,8 +76,4 @@ public final class JPEG extends Bitmap {
throw new IllegalArgumentException(signature+" is not a valid instance/signature value for JPEG");
}
}
public String getContentType() {
return "image/jpeg";
}
}

View File

@ -23,19 +23,14 @@ import java.io.IOException;
import java.util.zip.InflaterInputStream;
import org.apache.poi.hslf.exceptions.HSLFException;
import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.Units;
/**
* Represents Macintosh PICT picture data.
*
* @author Yegor Kozlov
*/
public final class PICT extends Metafile {
/**
* Extract compressed PICT data from a ppt
*/
@Override
public byte[] getData(){
byte[] rawdata = getRawData();
try {
@ -74,6 +69,7 @@ public final class PICT extends Metafile {
return out.toByteArray();
}
@Override
public void setData(byte[] data) throws IOException {
int pos = 512; //skip the first 512 bytes - they are MAC specific crap
byte[] compressed = compress(data, pos, data.length-pos);
@ -97,11 +93,9 @@ public final class PICT extends Metafile {
setRawData(out.toByteArray());
}
/**
* @see org.apache.poi.hslf.usermodel.HSLFPictureShape#PICT
*/
public int getType(){
return HSLFPictureShape.PICT;
@Override
public PictureType getType(){
return PictureType.PICT;
}
/**
@ -128,9 +122,4 @@ public final class PICT extends Metafile {
throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PICT");
}
}
public String getContentType() {
return "image/x-pict";
}
}

View File

@ -17,19 +17,14 @@
package org.apache.poi.hslf.blip;
import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.PngUtils;
/**
* Represents a PNG picture data in a PPT file
*
* @author Yegor Kozlov
*/
public final class PNG extends Bitmap {
/**
* @return PNG data
*/
@Override
public byte[] getData() {
byte[] data = super.getData();
@ -44,12 +39,9 @@ public final class PNG extends Bitmap {
return data;
}
/**
* @return type of this picture
* @see org.apache.poi.hslf.usermodel.HSLFPictureShape#PNG
*/
public int getType(){
return HSLFPictureShape.PNG;
@Override
public PictureType getType(){
return PictureType.PNG;
}
/**
@ -76,8 +68,4 @@ public final class PNG extends Bitmap {
throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PNG");
}
}
public String getContentType() {
return "image/png";
}
}

View File

@ -25,21 +25,16 @@ import java.io.OutputStream;
import java.util.zip.InflaterInputStream;
import org.apache.poi.hslf.exceptions.HSLFException;
import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.Units;
/**
* Represents a WMF (Windows Metafile) picture data.
*
* @author Yegor Kozlov
*/
public final class WMF extends Metafile {
/**
* Extract compressed WMF data from a ppt
*/
@Override
public byte[] getData(){
try {
byte[] rawdata = getRawData();
@ -70,6 +65,7 @@ public final class WMF extends Metafile {
}
}
@Override
public void setData(byte[] data) throws IOException {
int pos = 0;
AldusHeader aldus = new AldusHeader();
@ -97,11 +93,9 @@ public final class WMF extends Metafile {
setRawData(out.toByteArray());
}
/**
* We are of type <code>Picture.WMF</code>
*/
public int getType(){
return HSLFPictureShape.WMF;
@Override
public PictureType getType(){
return PictureType.WMF;
}
/**
@ -208,10 +202,4 @@ public final class WMF extends Metafile {
return 22;
}
}
public String getContentType() {
return "image/x-wmf";
}
}

View File

@ -18,6 +18,7 @@
package org.apache.poi.hslf.extractor;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import java.io.IOException;
import java.io.FileOutputStream;
@ -44,31 +45,8 @@ public final class ImageExtractor {
// picture data
byte[] data = pict.getData();
int type = pict.getType();
String ext;
switch (type) {
case HSLFPictureShape.JPEG:
ext = ".jpg";
break;
case HSLFPictureShape.PNG:
ext = ".png";
break;
case HSLFPictureShape.WMF:
ext = ".wmf";
break;
case HSLFPictureShape.EMF:
ext = ".emf";
break;
case HSLFPictureShape.PICT:
ext = ".pict";
break;
case HSLFPictureShape.DIB:
ext = ".dib";
break;
default:
continue;
}
FileOutputStream out = new FileOutputStream("pict_" + i + ext);
PictureType type = pict.getType();
FileOutputStream out = new FileOutputStream("pict_" + i + type.extension);
out.write(data);
out.close();
}

View File

@ -52,10 +52,10 @@ public final class ActiveXShape extends HSLFPictureShape {
/**
* Create a new <code>Picture</code>
*
* @param pictureIdx the index of the picture
* @param pictureData the picture data
*/
public ActiveXShape(int movieIdx, int pictureIdx){
super(pictureIdx, null);
public ActiveXShape(int movieIdx, HSLFPictureData pictureData){
super(pictureData, null);
setActiveXIndex(movieIdx);
}

View File

@ -41,10 +41,10 @@ public final class MovieShape extends HSLFPictureShape {
/**
* Create a new <code>Picture</code>
*
* @param pictureIdx the index of the picture
* @param pictureData the picture data
*/
public MovieShape(int movieIdx, int pictureIdx){
super(pictureIdx, null);
public MovieShape(int movieIdx, HSLFPictureData pictureData){
super(pictureData, null);
setMovieIndex(movieIdx);
setAutoPlay(true);
}
@ -52,11 +52,11 @@ public final class MovieShape extends HSLFPictureShape {
/**
* Create a new <code>Picture</code>
*
* @param idx the index of the picture
* @param pictureData the picture data
* @param parent the parent shape
*/
public MovieShape(int movieIdx, int idx, ShapeContainer<HSLFShape> parent) {
super(idx, parent);
public MovieShape(int movieIdx, HSLFPictureData pictureData, ShapeContainer<HSLFShape> parent) {
super(pictureData, parent);
setMovieIndex(movieIdx);
}

View File

@ -39,10 +39,10 @@ public final class OLEShape extends HSLFPictureShape {
/**
* Create a new <code>OLEShape</code>
*
* @param idx the index of the picture
* @param data the picture data
*/
public OLEShape(int idx){
super(idx);
public OLEShape(HSLFPictureData data){
super(data);
}
/**
@ -51,8 +51,8 @@ public final class OLEShape extends HSLFPictureShape {
* @param idx the index of the picture
* @param parent the parent shape
*/
public OLEShape(int idx, ShapeContainer<HSLFShape> parent) {
super(idx, parent);
public OLEShape(HSLFPictureData data, ShapeContainer<HSLFShape> parent) {
super(data, parent);
}
/**

View File

@ -295,16 +295,14 @@ public final class HSLFFill {
/**
* Assign picture used to fill the underlying shape.
*
* @param idx 0-based index of the picture added to this ppt by <code>SlideShow.addPicture</code> method.
* @param data the picture data added to this ppt by {@link HSLFSlideShow#addPicture(byte[], org.apache.poi.sl.usermodel.PictureData.PictureType)} method.
*/
public void setPictureData(int idx){
public void setPictureData(HSLFPictureData data){
EscherOptRecord opt = shape.getEscherOptRecord();
HSLFShape.setEscherProperty(opt, (short)(EscherProperties.FILL__PATTERNTEXTURE + 0x4000), idx);
if( idx != 0 ) {
if( shape.getSheet() != null ) {
EscherBSERecord bse = getEscherBSERecord(idx);
bse.setRef(bse.getRef() + 1);
}
HSLFShape.setEscherProperty(opt, (short)(EscherProperties.FILL__PATTERNTEXTURE + 0x4000), (data == null ? 0 : data.getIndex()));
if(data != null && shape.getSheet() != null) {
EscherBSERecord bse = getEscherBSERecord(data.getIndex());
bse.setRef(bse.getRef() + 1);
}
}

View File

@ -54,34 +54,12 @@ public abstract class HSLFPictureData implements PictureData {
* The instance type/signatures defines if one or two UID instances will be included
*/
protected int uidInstanceCount = 1;
/**
* The 1-based index within the pictures stream
*/
protected int index = -1;
/**
* Returns type of this picture.
* Must be one of the static constants defined in the <code>Picture<code> class.
*
* @return type of this picture.
*/
public abstract int getType();
/**
* Returns content type (mime type) of this picture.
*
* @return content type of this picture.
*/
public abstract String getContentType();
/**
* Returns the binary data of this Picture
* @return picture data
*/
public abstract byte[] getData();
/**
* Set picture data
*/
public abstract void setData(byte[] data) throws IOException;
/**
* Blip signature.
*/
@ -159,7 +137,8 @@ public abstract class HSLFPictureData implements PictureData {
out.write(data);
data = new byte[LittleEndian.SHORT_SIZE];
LittleEndian.putUShort(data, 0, getType() + 0xF018);
PictureType pt = getType();
LittleEndian.putUShort(data, 0, pt.nativeId + 0xF018);
out.write(data);
byte[] rawdata = getRawData();
@ -178,27 +157,15 @@ public abstract class HSLFPictureData implements PictureData {
* Must be one of the static constants defined in the <code>Picture<code> class.
* @return concrete instance of <code>PictureData</code>
*/
public static HSLFPictureData create(int type){
public static HSLFPictureData create(PictureType type){
HSLFPictureData pict;
switch (type){
case HSLFPictureShape.EMF:
pict = new EMF();
break;
case HSLFPictureShape.WMF:
pict = new WMF();
break;
case HSLFPictureShape.PICT:
pict = new PICT();
break;
case HSLFPictureShape.JPEG:
pict = new JPEG();
break;
case HSLFPictureShape.PNG:
pict = new PNG();
break;
case HSLFPictureShape.DIB:
pict = new DIB();
break;
case EMF: pict = new EMF(); break;
case WMF: pict = new WMF(); break;
case PICT: pict = new PICT(); break;
case JPEG: pict = new JPEG(); break;
case PNG: pict = new PNG(); break;
case DIB: pict = new DIB(); break;
default:
throw new IllegalArgumentException("Unsupported picture type: " + type);
}
@ -231,4 +198,23 @@ public abstract class HSLFPictureData implements PictureData {
public int getSize(){
return getData().length;
}
/**
* @return the 1-based index of this pictures within the pictures stream
*/
public int getIndex() {
return index;
}
/**
* @param index sets the 1-based index of this pictures within the pictures stream
*/
public void setIndex(int index) {
this.index = index;
}
@Override
public final String getContentType() {
return getType().contentType;
}
}

View File

@ -51,43 +51,13 @@ import org.apache.poi.util.Units;
*/
public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape {
/**
* Windows Enhanced Metafile (EMF)
*/
public static final int EMF = 2;
/**
* Windows Metafile (WMF)
*/
public static final int WMF = 3;
/**
* Macintosh PICT
*/
public static final int PICT = 4;
/**
* JPEG
*/
public static final int JPEG = 5;
/**
* PNG
*/
public static final int PNG = 6;
/**
* Windows DIB (BMP)
*/
public static final byte DIB = 7;
/**
* Create a new <code>Picture</code>
*
* @param idx the index of the picture
*/
public HSLFPictureShape(int idx){
this(idx, null);
public HSLFPictureShape(HSLFPictureData data){
this(data, null);
}
/**
@ -96,9 +66,9 @@ public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape {
* @param idx the index of the picture
* @param parent the parent shape
*/
public HSLFPictureShape(int idx, ShapeContainer<HSLFShape> parent) {
public HSLFPictureShape(HSLFPictureData data, ShapeContainer<HSLFShape> parent) {
super(null, parent);
_escherContainer = createSpContainer(idx, parent instanceof HSLFGroupShape);
_escherContainer = createSpContainer(data.getIndex(), parent instanceof HSLFGroupShape);
}
/**

View File

@ -25,46 +25,31 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherOptRecord;
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.hpsf.ClassID;
import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
import org.apache.poi.hslf.exceptions.HSLFException;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hslf.record.Document;
import org.apache.poi.hslf.record.DocumentAtom;
import org.apache.poi.hslf.record.ExAviMovie;
import org.apache.poi.hslf.record.ExControl;
import org.apache.poi.hslf.record.ExEmbed;
import org.apache.poi.hslf.record.ExEmbedAtom;
import org.apache.poi.hslf.record.ExHyperlink;
import org.apache.poi.hslf.record.ExHyperlinkAtom;
import org.apache.poi.hslf.record.ExMCIMovie;
import org.apache.poi.hslf.record.ExObjList;
import org.apache.poi.hslf.record.ExObjListAtom;
import org.apache.poi.hslf.record.ExOleObjAtom;
import org.apache.poi.hslf.record.ExOleObjStg;
import org.apache.poi.hslf.record.ExVideoContainer;
import org.apache.poi.hslf.record.FontCollection;
import org.apache.poi.hslf.record.FontEntityAtom;
import org.apache.poi.hslf.record.HeadersFootersContainer;
import org.apache.poi.hslf.record.PersistPtrHolder;
import org.apache.poi.hslf.record.PositionDependentRecord;
import org.apache.poi.hslf.record.PositionDependentRecordContainer;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordContainer;
import org.apache.poi.hslf.record.RecordTypes;
import org.apache.poi.hslf.record.SlideListWithText;
import org.apache.poi.hslf.model.HeadersFooters;
import org.apache.poi.hslf.model.MovieShape;
import org.apache.poi.hslf.model.PPFont;
import org.apache.poi.hslf.record.*;
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
import org.apache.poi.hslf.record.SlidePersistAtom;
import org.apache.poi.hslf.record.UserEditAtom;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.sl.usermodel.*;
import org.apache.poi.sl.usermodel.MasterSheet;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.Resources;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.Units;
@ -752,19 +737,20 @@ public final class HSLFSlideShow implements SlideShow {
return slide;
}
/**
* Adds a picture to this presentation and returns the associated index.
*
* @param data
* picture data
* @param format
* the format of the picture. One of constans defined in the
* <code>Picture</code> class.
* @return the index to this picture (1 based).
*/
public int addPicture(byte[] data, int format) throws IOException {
byte[] uid = HSLFPictureData.getChecksum(data);
@Override
public HSLFPictureData addPicture(byte[] data, PictureType format) throws IOException {
if (format == null || format.nativeId == -1) {
throw new IllegalArgumentException("Unsupported picture format: " + format);
}
byte[] uid = HSLFPictureData.getChecksum(data);
for (HSLFPictureData pd : getPictureData()) {
if (Arrays.equals(pd.getUID(), uid)) {
return pd;
}
}
EscherContainerRecord bstore;
EscherContainerRecord dggContainer = _documentRecord.getPPDrawingGroup().getDggContainer();
@ -775,14 +761,6 @@ public final class HSLFSlideShow implements SlideShow {
bstore.setRecordId(EscherContainerRecord.BSTORE_CONTAINER);
dggContainer.addChildBefore(bstore, EscherOptRecord.RECORD_ID);
} else {
Iterator<EscherRecord> iter = bstore.getChildIterator();
for (int i = 0; iter.hasNext(); i++) {
EscherBSERecord bse = (EscherBSERecord) iter.next();
if (Arrays.equals(bse.getUid(), uid)) {
return i + 1;
}
}
}
HSLFPictureData pict = HSLFPictureData.create(format);
@ -792,19 +770,20 @@ public final class HSLFSlideShow implements SlideShow {
EscherBSERecord bse = new EscherBSERecord();
bse.setRecordId(EscherBSERecord.RECORD_ID);
bse.setOptions((short) (0x0002 | (format << 4)));
bse.setOptions((short) (0x0002 | (format.nativeId << 4)));
bse.setSize(pict.getRawData().length + 8);
bse.setUid(uid);
bse.setBlipTypeMacOS((byte) format);
bse.setBlipTypeWin32((byte) format);
bse.setBlipTypeMacOS((byte) format.nativeId);
bse.setBlipTypeWin32((byte) format.nativeId);
if (format == HSLFPictureShape.EMF)
bse.setBlipTypeMacOS((byte) HSLFPictureShape.PICT);
else if (format == HSLFPictureShape.WMF)
bse.setBlipTypeMacOS((byte) HSLFPictureShape.PICT);
else if (format == HSLFPictureShape.PICT)
bse.setBlipTypeWin32((byte) HSLFPictureShape.WMF);
if (format == PictureType.EMF) {
bse.setBlipTypeMacOS((byte) PictureType.PICT.nativeId);
} else if (format == PictureType.WMF) {
bse.setBlipTypeMacOS((byte) PictureType.PICT.nativeId);
} else if (format == PictureType.PICT) {
bse.setBlipTypeWin32((byte) PictureType.WMF.nativeId);
}
bse.setRef(0);
bse.setOffset(offset);
@ -814,7 +793,7 @@ public final class HSLFSlideShow implements SlideShow {
int count = bstore.getChildRecords().size();
bstore.setOptions((short) ((count << 4) | 0xF));
return count;
return pict;
}
/**
@ -827,7 +806,7 @@ public final class HSLFSlideShow implements SlideShow {
* <code>Picture</code> class.
* @return the index to this picture (1 based).
*/
public int addPicture(File pict, int format) throws IOException {
public HSLFPictureData addPicture(File pict, PictureType format) throws IOException {
int length = (int) pict.length();
byte[] data = new byte[length];
FileInputStream is = null;

View File

@ -52,6 +52,7 @@ import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.EntryUtils;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@ -407,13 +408,14 @@ public final class HSLFSlideShowImpl extends POIDocument {
}
// If they type (including the bonus 0xF018) is 0, skip it
if(type == 0) {
PictureType pt = PictureType.forNativeID(type - 0xF018);
if(type == 0 || pt == null) {
logger.log(POILogger.ERROR, "Problem reading picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou document will probably become corrupted if you save it!");
logger.log(POILogger.ERROR, "" + pos);
} else {
// Build the PictureData object from the data
try {
HSLFPictureData pict = HSLFPictureData.create(type - 0xF018);
try {
HSLFPictureData pict = HSLFPictureData.create(pt);
// Copy the data, ready to pass to PictureData
byte[] imgdata = new byte[imgsize];
@ -421,6 +423,7 @@ public final class HSLFSlideShowImpl extends POIDocument {
pict.setRawData(imgdata);
pict.setOffset(offset);
pict.setIndex(_pictures.size());
_pictures.add(pict);
} catch(IllegalArgumentException e) {
logger.log(POILogger.ERROR, "Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
@ -711,6 +714,7 @@ public final class HSLFSlideShowImpl extends POIDocument {
offset = prev.getOffset() + prev.getRawData().length + 8;
}
img.setOffset(offset);
img.setIndex(_pictures.size()+1);
_pictures.add(img);
return offset;
}

View File

@ -33,7 +33,15 @@ import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.ddf.EscherSimpleProperty;
import org.apache.poi.hslf.record.Document;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.usermodel.HSLFAutoShape;
import org.apache.poi.hslf.usermodel.HSLFFill;
import org.apache.poi.hslf.usermodel.HSLFPictureData;
import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSheet;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.ShapeType;
import org.junit.Test;
@ -104,15 +112,15 @@ public final class TestBackground {
HSLFSlide slide;
HSLFFill fill;
HSLFShape shape;
int idx;
HSLFPictureData data;
//slide 1
slide = ppt.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_PICTURE);
fill.setPictureData(idx);
fill.setPictureData(data);
shape = new HSLFAutoShape(ShapeType.RECT);
shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
@ -124,9 +132,9 @@ public final class TestBackground {
slide = ppt.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_PATTERN);
fill.setPictureData(idx);
fill.setPictureData(data);
fill.setBackgroundColor(Color.green);
fill.setForegroundColor(Color.red);
@ -140,16 +148,16 @@ public final class TestBackground {
slide = ppt.createSlide();
slide.setFollowMasterBackground(false);
fill = slide.getBackground().getFill();
idx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
data = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
fill.setFillType(HSLFFill.FILL_TEXTURE);
fill.setPictureData(idx);
fill.setPictureData(data);
shape = new HSLFAutoShape(ShapeType.RECT);
shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
fill = shape.getFill();
fill.setFillType(HSLFFill.FILL_PICTURE);
idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
fill.setPictureData(idx);
data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
fill.setPictureData(data);
slide.addShape(shape);
// slide 4
@ -198,7 +206,6 @@ public final class TestBackground {
assertEquals(HSLFFill.FILL_SHADE_CENTER, fill.getFillType());
shape = slides.get(3).getShapes().get(0);
assertEquals(HSLFFill.FILL_SHADE, shape.getFill().getFillType());
}
private int getFillPictureRefCount(HSLFShape shape, HSLFFill fill) {

View File

@ -17,14 +17,19 @@
package org.apache.poi.hslf.model;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.usermodel.HSLFPictureData;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.junit.Test;
/**
@ -44,9 +49,9 @@ public final class TestMovieShape {
String path = "/test-movie.mpg";
int movieIdx = ppt.addMovie(path, MovieShape.MOVIE_MPEG);
int thumbnailIdx = ppt.addPicture(_slTests.readFile("tomcat.png"), HSLFPictureShape.PNG);
HSLFPictureData thumbnailData = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
MovieShape shape = new MovieShape(movieIdx, thumbnailIdx);
MovieShape shape = new MovieShape(movieIdx, thumbnailData);
shape.setAnchor(new Rectangle2D.Float(300,225,120,90));
slide.addShape(shape);

View File

@ -30,6 +30,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.IOUtils;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.junit.Test;
public final class TestOleEmbedding {
@ -95,7 +96,7 @@ public final class TestOleEmbedding {
HSLFSlideShow ppt = new HSLFSlideShow(_hslfSlideShow);
File pict = POIDataSamples.getSlideShowInstance().getFile("clock.jpg");
int pictId = ppt.addPicture(pict, HSLFPictureShape.JPEG);
HSLFPictureData pictData = ppt.addPicture(pict, PictureType.JPEG);
InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("Employee.xls");
POIFSFileSystem poiData1 = new POIFSFileSystem(is);
@ -104,14 +105,14 @@ public final class TestOleEmbedding {
int oleObjectId1 = ppt.addEmbed(poiData1);
HSLFSlide slide1 = ppt.createSlide();
OLEShape oleShape1 = new OLEShape(pictId);
OLEShape oleShape1 = new OLEShape(pictData);
oleShape1.setObjectID(oleObjectId1);
slide1.addShape(oleShape1);
oleShape1.setAnchor(new Rectangle2D.Double(100,100,100,100));
// add second slide with different order in object creation
HSLFSlide slide2 = ppt.createSlide();
OLEShape oleShape2 = new OLEShape(pictId);
OLEShape oleShape2 = new OLEShape(pictData);
is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleWithImages.xls");
POIFSFileSystem poiData2 = new POIFSFileSystem(is);

View File

@ -17,19 +17,45 @@
package org.apache.poi.hslf.model;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.POIDataSamples;
import org.apache.poi.ddf.*;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.ddf.EscherDgRecord;
import org.apache.poi.ddf.EscherDggRecord;
import org.apache.poi.ddf.EscherOptRecord;
import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.ddf.EscherSimpleProperty;
import org.apache.poi.hslf.usermodel.HSLFAutoShape;
import org.apache.poi.hslf.usermodel.HSLFGroupShape;
import org.apache.poi.hslf.usermodel.HSLFPictureData;
import org.apache.poi.hslf.usermodel.HSLFPictureShape;
import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSimpleShape;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hslf.usermodel.HSLFTextBox;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.hslf.usermodel.HSLFTextRun;
import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.junit.Before;
import org.junit.Test;
@ -303,8 +329,8 @@ public final class TestShapes {
group.setAnchor(new Rectangle(0, 0, (int)pgsize.getWidth(), (int)pgsize.getHeight()));
slide.addShape(group);
int idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
HSLFPictureShape pict = new HSLFPictureShape(idx, group);
HSLFPictureData data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
HSLFPictureShape pict = new HSLFPictureShape(data, group);
pict.setAnchor(new Rectangle(0, 0, 200, 200));
group.addShape(pict);

View File

@ -17,11 +17,16 @@
package org.apache.poi.hslf.usermodel;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.awt.Color;
import java.awt.Rectangle;
import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@ -39,15 +44,16 @@ import org.apache.poi.ddf.EscherOptRecord;
import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.hslf.HSLFTestDataSamples;
import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hslf.model.textproperties.TextPropCollection;
import org.apache.poi.hslf.model.textproperties.TextPropCollection.TextPropType;
import org.apache.poi.hslf.record.*;
import org.apache.poi.hslf.model.HeadersFooters;
import org.apache.poi.hslf.record.Document;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.SlideListWithText;
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.*;
import org.junit.Ignore;
import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;
import org.apache.poi.util.Units;
import org.junit.Test;
/**
@ -73,8 +79,8 @@ public final class TestBugs {
HSLFPictureData[] pict = ppt.getPictureData();
assertEquals(2, pict.length);
assertEquals(HSLFPictureShape.JPEG, pict[0].getType());
assertEquals(HSLFPictureShape.JPEG, pict[1].getType());
assertEquals(PictureType.JPEG, pict[0].getType());
assertEquals(PictureType.JPEG, pict[1].getType());
}
/**
@ -354,7 +360,7 @@ public final class TestBugs {
HSLFPictureData pict = f.getPictureData();
assertNotNull(pict);
assertEquals(HSLFPictureShape.JPEG, pict.getType());
assertEquals(PictureType.JPEG, pict.getType());
}
/**

View File

@ -17,13 +17,24 @@
package org.apache.poi.hslf.usermodel;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import java.awt.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
@ -31,6 +42,7 @@ import org.apache.poi.POIDataSamples;
import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.hssf.usermodel.DummyGraphics2d;
import org.apache.poi.sl.draw.Drawable;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.util.JvmBugs;
@ -58,10 +70,10 @@ public final class TestPicture {
HSLFSlide s2 = ppt.createSlide();
HSLFSlide s3 = ppt.createSlide();
int idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
HSLFPictureShape pict = new HSLFPictureShape(idx);
HSLFPictureShape pict2 = new HSLFPictureShape(idx);
HSLFPictureShape pict3 = new HSLFPictureShape(idx);
HSLFPictureData data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
HSLFPictureShape pict = new HSLFPictureShape(data);
HSLFPictureShape pict2 = new HSLFPictureShape(data);
HSLFPictureShape pict3 = new HSLFPictureShape(data);
pict.setAnchor(new Rectangle(10,10,100,100));
s.addShape(pict);
@ -89,8 +101,9 @@ public final class TestPicture {
public void bug46122() {
HSLFSlideShow ppt = new HSLFSlideShow();
HSLFSlide slide = ppt.createSlide();
HSLFPictureShape pict = new HSLFPictureShape(-1); //index to non-existing picture data
HSLFPictureData pd = HSLFPictureData.create(PictureType.PNG);
HSLFPictureShape pict = new HSLFPictureShape(pd); //index to non-existing picture data
pict.setSheet(slide);
HSLFPictureData data = pict.getPictureData();
assertNull(data);
@ -125,15 +138,19 @@ public final class TestPicture {
null // EMF
};
for (int i = 0; i < pictures.length; i++) {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictures[i].getData()));
if (pictures[i].getType() != HSLFPictureShape.WMF && pictures[i].getType() != HSLFPictureShape.EMF) {
assertNotNull(image);
int[] dimensions = expectedSizes[i];
assertEquals(dimensions[0], image.getWidth());
assertEquals(dimensions[1], image.getHeight());
int i = 0;
for (HSLFPictureData pd : pictures) {
int[] dimensions = expectedSizes[i++];
BufferedImage image = ImageIO.read(new ByteArrayInputStream(pd.getData()));
switch (pd.getType()) {
case WMF:
case EMF:
break;
default:
assertNotNull(image);
assertEquals(dimensions[0], image.getWidth());
assertEquals(dimensions[1], image.getHeight());
break;
}
}
}

View File

@ -28,6 +28,7 @@ import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.blip.*;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
/**
* Test adding/reading pictures
@ -47,9 +48,9 @@ public final class TestPictures extends TestCase{
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("cow.pict");
int idx = ppt.addPicture(src_bytes, HSLFPictureShape.PICT);
HSLFPictureShape pict = new HSLFPictureShape(idx);
assertEquals(idx, pict.getPictureIndex());
HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PICT);
HSLFPictureShape pict = new HSLFPictureShape(data);
assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
@ -63,7 +64,7 @@ public final class TestPictures extends TestCase{
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
assertEquals(idx, pict.getPictureIndex());
assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
@ -71,7 +72,7 @@ public final class TestPictures extends TestCase{
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
assertEquals(HSLFPictureShape.PICT, pictures[0].getType());
assertEquals(PictureType.PICT, pictures[0].getType());
assertTrue(pictures[0] instanceof PICT);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
@ -92,9 +93,9 @@ public final class TestPictures extends TestCase{
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("santa.wmf");
int idx = ppt.addPicture(src_bytes, HSLFPictureShape.WMF);
HSLFPictureShape pict = new HSLFPictureShape(idx);
assertEquals(idx, pict.getPictureIndex());
HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.WMF);
HSLFPictureShape pict = new HSLFPictureShape(data);
assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
@ -108,7 +109,7 @@ public final class TestPictures extends TestCase{
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
assertEquals(idx, pict.getPictureIndex());
assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
@ -116,7 +117,7 @@ public final class TestPictures extends TestCase{
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
assertEquals(HSLFPictureShape.WMF, pictures[0].getType());
assertEquals(PictureType.WMF, pictures[0].getType());
assertTrue(pictures[0] instanceof WMF);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
@ -137,10 +138,10 @@ public final class TestPictures extends TestCase{
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("wrench.emf");
int idx = ppt.addPicture(src_bytes, HSLFPictureShape.EMF);
HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.EMF);
HSLFPictureShape pict = new HSLFPictureShape(idx);
assertEquals(idx, pict.getPictureIndex());
HSLFPictureShape pict = new HSLFPictureShape(data);
assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
@ -154,7 +155,7 @@ public final class TestPictures extends TestCase{
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
assertEquals(idx, pict.getPictureIndex());
assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
@ -162,7 +163,7 @@ public final class TestPictures extends TestCase{
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
assertEquals(HSLFPictureShape.EMF, pictures[0].getType());
assertEquals(PictureType.EMF, pictures[0].getType());
assertTrue(pictures[0] instanceof EMF);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
@ -177,9 +178,9 @@ public final class TestPictures extends TestCase{
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("tomcat.png");
int idx = ppt.addPicture(src_bytes, HSLFPictureShape.PNG);
HSLFPictureShape pict = new HSLFPictureShape(idx);
assertEquals(idx, pict.getPictureIndex());
HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PNG);
HSLFPictureShape pict = new HSLFPictureShape(data);
assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
@ -193,7 +194,7 @@ public final class TestPictures extends TestCase{
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
assertEquals(idx, pict.getPictureIndex());
assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
@ -201,7 +202,7 @@ public final class TestPictures extends TestCase{
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
assertEquals(HSLFPictureShape.PNG, pictures[0].getType());
assertEquals(PictureType.PNG, pictures[0].getType());
assertTrue(pictures[0] instanceof PNG);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
@ -216,10 +217,10 @@ public final class TestPictures extends TestCase{
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("clock.jpg");
int idx = ppt.addPicture(src_bytes, HSLFPictureShape.JPEG);
HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.JPEG);
HSLFPictureShape pict = new HSLFPictureShape(idx);
assertEquals(idx, pict.getPictureIndex());
HSLFPictureShape pict = new HSLFPictureShape(data);
assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
@ -233,7 +234,7 @@ public final class TestPictures extends TestCase{
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
assertEquals(idx, pict.getPictureIndex());
assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
@ -241,7 +242,7 @@ public final class TestPictures extends TestCase{
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
assertEquals(HSLFPictureShape.JPEG, pictures[0].getType());
assertEquals(PictureType.JPEG, pictures[0].getType());
assertTrue(pictures[0] instanceof JPEG);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
@ -256,9 +257,9 @@ public final class TestPictures extends TestCase{
HSLFSlide slide = ppt.createSlide();
byte[] src_bytes = slTests.readFile("clock.dib");
int idx = ppt.addPicture(src_bytes, HSLFPictureShape.DIB);
HSLFPictureShape pict = new HSLFPictureShape(idx);
assertEquals(idx, pict.getPictureIndex());
HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.DIB);
HSLFPictureShape pict = new HSLFPictureShape(data);
assertEquals(data.getIndex(), pict.getPictureIndex());
slide.addShape(pict);
//serialize and read again
@ -272,7 +273,7 @@ public final class TestPictures extends TestCase{
List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
assertEquals(1, sh.size());
pict = (HSLFPictureShape)sh.get(0);
assertEquals(idx, pict.getPictureIndex());
assertEquals(data.getIndex(), pict.getPictureIndex());
//check picture data
HSLFPictureData[] pictures = ppt.getPictureData();
@ -280,7 +281,7 @@ public final class TestPictures extends TestCase{
assertEquals(pict.getPictureData(), pictures[0]);
assertEquals(1, pictures.length);
assertEquals(HSLFPictureShape.DIB, pictures[0].getType());
assertEquals(PictureType.DIB, pictures[0].getType());
assertTrue(pictures[0] instanceof DIB);
//compare the content of the initial file with what is stored in the PictureData
byte[] ppt_bytes = pictures[0].getData();
@ -304,7 +305,7 @@ public final class TestPictures extends TestCase{
pict = (HSLFPictureShape)slides.get(0).getShapes().get(0); //the first slide contains JPEG
pdata = pict.getPictureData();
assertTrue(pdata instanceof JPEG);
assertEquals(HSLFPictureShape.JPEG, pdata.getType());
assertEquals(PictureType.JPEG, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("clock.jpg");
assertArrayEquals(src_bytes, ppt_bytes);
@ -312,7 +313,7 @@ public final class TestPictures extends TestCase{
pict = (HSLFPictureShape)slides.get(1).getShapes().get(0); //the second slide contains PNG
pdata = pict.getPictureData();
assertTrue(pdata instanceof PNG);
assertEquals(HSLFPictureShape.PNG, pdata.getType());
assertEquals(PictureType.PNG, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("tomcat.png");
assertArrayEquals(src_bytes, ppt_bytes);
@ -320,7 +321,7 @@ public final class TestPictures extends TestCase{
pict = (HSLFPictureShape)slides.get(2).getShapes().get(0); //the third slide contains WMF
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
assertEquals(HSLFPictureShape.WMF, pdata.getType());
assertEquals(PictureType.WMF, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("santa.wmf");
assertEquals(src_bytes.length, ppt_bytes.length);
@ -334,7 +335,7 @@ public final class TestPictures extends TestCase{
pict = (HSLFPictureShape)slides.get(3).getShapes().get(0); //the forth slide contains PICT
pdata = pict.getPictureData();
assertTrue(pdata instanceof PICT);
assertEquals(HSLFPictureShape.PICT, pdata.getType());
assertEquals(PictureType.PICT, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("cow.pict");
assertEquals(src_bytes.length, ppt_bytes.length);
@ -348,7 +349,7 @@ public final class TestPictures extends TestCase{
pict = (HSLFPictureShape)slides.get(4).getShapes().get(0); //the fifth slide contains EMF
pdata = pict.getPictureData();
assertTrue(pdata instanceof EMF);
assertEquals(HSLFPictureShape.EMF, pdata.getType());
assertEquals(PictureType.EMF, pdata.getType());
src_bytes = pdata.getData();
ppt_bytes = slTests.readFile("wrench.emf");
assertArrayEquals(src_bytes, ppt_bytes);
@ -365,8 +366,8 @@ public final class TestPictures extends TestCase{
// Should still have 2 real pictures
assertEquals(2, hslf.getPictures().length);
// Both are real pictures, both WMF
assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[0].getType());
assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[1].getType());
assertEquals(PictureType.WMF, hslf.getPictures()[0].getType());
assertEquals(PictureType.WMF, hslf.getPictures()[1].getType());
// Now test what happens when we use the SlideShow interface
HSLFSlideShow ppt = new HSLFSlideShow(hslf);
@ -381,12 +382,12 @@ public final class TestPictures extends TestCase{
pict = (HSLFPictureShape)slides.get(0).getShapes().get(1); // 2nd object on 1st slide
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
assertEquals(HSLFPictureShape.WMF, pdata.getType());
assertEquals(PictureType.WMF, pdata.getType());
pict = (HSLFPictureShape)slides.get(0).getShapes().get(2); // 3rd object on 1st slide
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
assertEquals(HSLFPictureShape.WMF, pdata.getType());
assertEquals(PictureType.WMF, pdata.getType());
}
/**
@ -401,8 +402,8 @@ public final class TestPictures extends TestCase{
// Should still have 2 real pictures
assertEquals(2, hslf.getPictures().length);
// Both are real pictures, both WMF
assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[0].getType());
assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[1].getType());
assertEquals(PictureType.WMF, hslf.getPictures()[0].getType());
assertEquals(PictureType.WMF, hslf.getPictures()[1].getType());
// Now test what happens when we use the SlideShow interface
HSLFSlideShow ppt = new HSLFSlideShow(hslf);
@ -417,12 +418,12 @@ public final class TestPictures extends TestCase{
pict = (HSLFPictureShape)slides.get(6).getShapes().get(13);
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
assertEquals(HSLFPictureShape.WMF, pdata.getType());
assertEquals(PictureType.WMF, pdata.getType());
pict = (HSLFPictureShape)slides.get(7).getShapes().get(13);
pdata = pict.getPictureData();
assertTrue(pdata instanceof WMF);
assertEquals(HSLFPictureShape.WMF, pdata.getType());
assertEquals(PictureType.WMF, pdata.getType());
//add a new picture, it should be correctly appended to the Pictures stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -431,7 +432,7 @@ public final class TestPictures extends TestCase{
int streamSize = out.size();
HSLFPictureData data = HSLFPictureData.create(HSLFPictureShape.JPEG);
HSLFPictureData data = HSLFPictureData.create(PictureType.JPEG);
data.setData(new byte[100]);
int offset = hslf.addPicture(data);
assertEquals(streamSize, offset);
@ -452,8 +453,8 @@ public final class TestPictures extends TestCase{
HSLFSlide slide = ppt.createSlide();
byte[] img = slTests.readFile("tomcat.png");
int idx = ppt.addPicture(img, HSLFPictureShape.PNG);
HSLFPictureShape pict = new HSLFPictureShape(idx);
HSLFPictureData data = ppt.addPicture(img, PictureType.PNG);
HSLFPictureShape pict = new HSLFPictureShape(data);
pict.setPictureName("tomcat.png");
slide.addShape(pict);