001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.usermodel;
019:
020: import org.apache.poi.hslf.*;
021: import org.apache.poi.hslf.blip.*;
022: import org.apache.poi.hslf.model.*;
023: import junit.framework.TestCase;
024:
025: import java.io.*;
026: import java.util.Arrays;
027:
028: /**
029: * Test adding/reading pictures
030: *
031: * @author Yegor Kozlov
032: */
033: public class TestPictures extends TestCase {
034:
035: protected File cwd;
036:
037: public void setUp() throws Exception {
038: cwd = new File(System.getProperty("HSLF.testdata.path"));
039: }
040:
041: /**
042: * Test read/write Macintosh PICT
043: */
044: public void testPICT() throws Exception {
045: SlideShow ppt = new SlideShow();
046:
047: Slide slide = ppt.createSlide();
048: File img = new File(cwd, "cow.pict");
049: int idx = ppt.addPicture(img, Picture.PICT);
050: Picture pict = new Picture(idx);
051: assertEquals(idx, pict.getPictureIndex());
052: slide.addShape(pict);
053:
054: //serialize and read again
055: ByteArrayOutputStream out = new ByteArrayOutputStream();
056: ppt.write(out);
057: out.close();
058:
059: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
060: out.toByteArray())));
061:
062: //make sure we can read this picture shape and it refers to the correct picture data
063: Shape[] sh = ppt.getSlides()[0].getShapes();
064: assertEquals(1, sh.length);
065: pict = (Picture) sh[0];
066: assertEquals(idx, pict.getPictureIndex());
067:
068: //check picture data
069: PictureData[] pictures = ppt.getPictureData();
070: //the Picture shape refers to the PictureData object in the Presentation
071: assertEquals(pict.getPictureData(), pictures[0]);
072:
073: assertEquals(1, pictures.length);
074: assertEquals(Picture.PICT, pictures[0].getType());
075: assertTrue(pictures[0] instanceof PICT);
076: //compare the content of the initial file with what is stored in the PictureData
077: byte[] src_bytes = read(img);
078: byte[] ppt_bytes = pictures[0].getData();
079: assertEquals(src_bytes.length, ppt_bytes.length);
080: //in PICT the first 512 bytes are MAC specific and may not be preserved, ignore them
081: byte[] b1 = new byte[src_bytes.length - 512];
082: System.arraycopy(src_bytes, 512, b1, 0, b1.length);
083: byte[] b2 = new byte[ppt_bytes.length - 512];
084: System.arraycopy(ppt_bytes, 512, b2, 0, b2.length);
085: assertTrue(Arrays.equals(b1, b2));
086: }
087:
088: /**
089: * Test read/write WMF
090: */
091: public void testWMF() throws Exception {
092: SlideShow ppt = new SlideShow();
093:
094: Slide slide = ppt.createSlide();
095: File img = new File(cwd, "santa.wmf");
096: int idx = ppt.addPicture(img, Picture.WMF);
097: Picture pict = new Picture(idx);
098: assertEquals(idx, pict.getPictureIndex());
099: slide.addShape(pict);
100:
101: //serialize and read again
102: ByteArrayOutputStream out = new ByteArrayOutputStream();
103: ppt.write(out);
104: out.close();
105:
106: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
107: out.toByteArray())));
108:
109: //make sure we can read this picture shape and it refers to the correct picture data
110: Shape[] sh = ppt.getSlides()[0].getShapes();
111: assertEquals(1, sh.length);
112: pict = (Picture) sh[0];
113: assertEquals(idx, pict.getPictureIndex());
114:
115: //check picture data
116: PictureData[] pictures = ppt.getPictureData();
117: //the Picture shape refers to the PictureData object in the Presentation
118: assertEquals(pict.getPictureData(), pictures[0]);
119:
120: assertEquals(1, pictures.length);
121: assertEquals(Picture.WMF, pictures[0].getType());
122: assertTrue(pictures[0] instanceof WMF);
123: //compare the content of the initial file with what is stored in the PictureData
124: byte[] src_bytes = read(img);
125: byte[] ppt_bytes = pictures[0].getData();
126: assertEquals(src_bytes.length, ppt_bytes.length);
127: //in WMF the first 22 bytes - is a metafile header
128: byte[] b1 = new byte[src_bytes.length - 22];
129: System.arraycopy(src_bytes, 22, b1, 0, b1.length);
130: byte[] b2 = new byte[ppt_bytes.length - 22];
131: System.arraycopy(ppt_bytes, 22, b2, 0, b2.length);
132: assertTrue(Arrays.equals(b1, b2));
133: }
134:
135: /**
136: * Test read/write EMF
137: */
138: public void testEMF() throws Exception {
139: SlideShow ppt = new SlideShow();
140:
141: Slide slide = ppt.createSlide();
142: File img = new File(cwd, "wrench.emf");
143: int idx = ppt.addPicture(img, Picture.EMF);
144: Picture pict = new Picture(idx);
145: assertEquals(idx, pict.getPictureIndex());
146: slide.addShape(pict);
147:
148: //serialize and read again
149: ByteArrayOutputStream out = new ByteArrayOutputStream();
150: ppt.write(out);
151: out.close();
152:
153: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
154: out.toByteArray())));
155:
156: //make sure we can get this picture shape and it refers to the correct picture data
157: Shape[] sh = ppt.getSlides()[0].getShapes();
158: assertEquals(1, sh.length);
159: pict = (Picture) sh[0];
160: assertEquals(idx, pict.getPictureIndex());
161:
162: //check picture data
163: PictureData[] pictures = ppt.getPictureData();
164: //the Picture shape refers to the PictureData object in the Presentation
165: assertEquals(pict.getPictureData(), pictures[0]);
166:
167: assertEquals(1, pictures.length);
168: assertEquals(Picture.EMF, pictures[0].getType());
169: assertTrue(pictures[0] instanceof EMF);
170: //compare the content of the initial file with what is stored in the PictureData
171: byte[] src_bytes = read(img);
172: byte[] ppt_bytes = pictures[0].getData();
173: assertTrue(Arrays.equals(src_bytes, ppt_bytes));
174: }
175:
176: /**
177: * Test read/write PNG
178: */
179: public void testPNG() throws Exception {
180: SlideShow ppt = new SlideShow();
181:
182: Slide slide = ppt.createSlide();
183: File img = new File(cwd, "tomcat.png");
184: int idx = ppt.addPicture(img, Picture.PNG);
185: Picture pict = new Picture(idx);
186: assertEquals(idx, pict.getPictureIndex());
187: slide.addShape(pict);
188:
189: //serialize and read again
190: ByteArrayOutputStream out = new ByteArrayOutputStream();
191: ppt.write(out);
192: out.close();
193:
194: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
195: out.toByteArray())));
196:
197: //make sure we can read this picture shape and it refers to the correct picture data
198: Shape[] sh = ppt.getSlides()[0].getShapes();
199: assertEquals(1, sh.length);
200: pict = (Picture) sh[0];
201: assertEquals(idx, pict.getPictureIndex());
202:
203: //check picture data
204: PictureData[] pictures = ppt.getPictureData();
205: //the Picture shape refers to the PictureData object in the Presentation
206: assertEquals(pict.getPictureData(), pictures[0]);
207:
208: assertEquals(1, pictures.length);
209: assertEquals(Picture.PNG, pictures[0].getType());
210: assertTrue(pictures[0] instanceof PNG);
211: //compare the content of the initial file with what is stored in the PictureData
212: byte[] src_bytes = read(img);
213: byte[] ppt_bytes = pictures[0].getData();
214: assertTrue(Arrays.equals(src_bytes, ppt_bytes));
215: }
216:
217: /**
218: * Test read/write JPEG
219: */
220: public void testJPEG() throws Exception {
221: SlideShow ppt = new SlideShow();
222:
223: Slide slide = ppt.createSlide();
224: File img = new File(cwd, "clock.jpg");
225: int idx = ppt.addPicture(img, Picture.JPEG);
226: Picture pict = new Picture(idx);
227: assertEquals(idx, pict.getPictureIndex());
228: slide.addShape(pict);
229:
230: //serialize and read again
231: ByteArrayOutputStream out = new ByteArrayOutputStream();
232: ppt.write(out);
233: out.close();
234:
235: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
236: out.toByteArray())));
237:
238: //make sure we can read this picture shape and it refers to the correct picture data
239: Shape[] sh = ppt.getSlides()[0].getShapes();
240: assertEquals(1, sh.length);
241: pict = (Picture) sh[0];
242: assertEquals(idx, pict.getPictureIndex());
243:
244: //check picture data
245: PictureData[] pictures = ppt.getPictureData();
246: //the Picture shape refers to the PictureData object in the Presentation
247: assertEquals(pict.getPictureData(), pictures[0]);
248:
249: assertEquals(1, pictures.length);
250: assertEquals(Picture.JPEG, pictures[0].getType());
251: assertTrue(pictures[0] instanceof JPEG);
252: //compare the content of the initial file with what is stored in the PictureData
253: byte[] src_bytes = read(img);
254: byte[] ppt_bytes = pictures[0].getData();
255: assertTrue(Arrays.equals(src_bytes, ppt_bytes));
256: }
257:
258: /**
259: * Test read/write DIB
260: */
261: public void testDIB() throws Exception {
262: SlideShow ppt = new SlideShow();
263:
264: Slide slide = ppt.createSlide();
265: File img = new File(cwd, "sci_cec.dib");
266:
267: // Check we can read the test DIB image
268: assertTrue(img.exists());
269:
270: // Add the image
271: int idx = ppt.addPicture(img, Picture.DIB);
272: Picture pict = new Picture(idx);
273: assertEquals(idx, pict.getPictureIndex());
274: slide.addShape(pict);
275:
276: //serialize and read again
277: ByteArrayOutputStream out = new ByteArrayOutputStream();
278: ppt.write(out);
279: out.close();
280:
281: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
282: out.toByteArray())));
283:
284: //make sure we can read this picture shape and it refers to the correct picture data
285: Shape[] sh = ppt.getSlides()[0].getShapes();
286: assertEquals(1, sh.length);
287: pict = (Picture) sh[0];
288: assertEquals(idx, pict.getPictureIndex());
289:
290: //check picture data
291: PictureData[] pictures = ppt.getPictureData();
292: //the Picture shape refers to the PictureData object in the Presentation
293: assertEquals(pict.getPictureData(), pictures[0]);
294:
295: assertEquals(1, pictures.length);
296: assertEquals(Picture.DIB, pictures[0].getType());
297: assertTrue(pictures[0] instanceof DIB);
298: //compare the content of the initial file with what is stored in the PictureData
299: byte[] src_bytes = read(img);
300: byte[] ppt_bytes = pictures[0].getData();
301: assertTrue(Arrays.equals(src_bytes, ppt_bytes));
302: }
303:
304: /**
305: * Read file into a byte array
306: */
307: protected byte[] read(File f) throws IOException {
308: byte[] bytes = new byte[(int) f.length()];
309: FileInputStream is = new FileInputStream(f);
310: is.read(bytes);
311: is.close();
312: return bytes;
313: }
314:
315: /**
316: * Read pictures in different formats from a reference slide show
317: */
318: public void testReadPictures() throws Exception {
319:
320: byte[] src_bytes, ppt_bytes, b1, b2;
321: Picture pict;
322: PictureData pdata;
323:
324: SlideShow ppt = new SlideShow(new HSLFSlideShow(new File(cwd,
325: "pictures.ppt").getPath()));
326: Slide[] slides = ppt.getSlides();
327: PictureData[] pictures = ppt.getPictureData();
328: assertEquals(5, pictures.length);
329:
330: pict = (Picture) slides[0].getShapes()[0]; //the first slide contains JPEG
331: pdata = pict.getPictureData();
332: assertTrue(pdata instanceof JPEG);
333: assertEquals(Picture.JPEG, pdata.getType());
334: src_bytes = pdata.getData();
335: ppt_bytes = read(new File(cwd, "clock.jpg"));
336: assertTrue(Arrays.equals(src_bytes, ppt_bytes));
337:
338: pict = (Picture) slides[1].getShapes()[0]; //the second slide contains PNG
339: pdata = pict.getPictureData();
340: assertTrue(pdata instanceof PNG);
341: assertEquals(Picture.PNG, pdata.getType());
342: src_bytes = pdata.getData();
343: ppt_bytes = read(new File(cwd, "tomcat.png"));
344: assertTrue(Arrays.equals(src_bytes, ppt_bytes));
345:
346: pict = (Picture) slides[2].getShapes()[0]; //the third slide contains WMF
347: pdata = pict.getPictureData();
348: assertTrue(pdata instanceof WMF);
349: assertEquals(Picture.WMF, pdata.getType());
350: src_bytes = pdata.getData();
351: ppt_bytes = read(new File(cwd, "santa.wmf"));
352: assertEquals(src_bytes.length, ppt_bytes.length);
353: //ignore the first 22 bytes - it is a WMF metafile header
354: b1 = new byte[src_bytes.length - 22];
355: System.arraycopy(src_bytes, 22, b1, 0, b1.length);
356: b2 = new byte[ppt_bytes.length - 22];
357: System.arraycopy(ppt_bytes, 22, b2, 0, b2.length);
358: assertTrue(Arrays.equals(b1, b2));
359:
360: pict = (Picture) slides[3].getShapes()[0]; //the forth slide contains PICT
361: pdata = pict.getPictureData();
362: assertTrue(pdata instanceof PICT);
363: assertEquals(Picture.PICT, pdata.getType());
364: src_bytes = pdata.getData();
365: ppt_bytes = read(new File(cwd, "cow.pict"));
366: assertEquals(src_bytes.length, ppt_bytes.length);
367: //ignore the first 512 bytes - it is a MAC specific crap
368: b1 = new byte[src_bytes.length - 512];
369: System.arraycopy(src_bytes, 512, b1, 0, b1.length);
370: b2 = new byte[ppt_bytes.length - 512];
371: System.arraycopy(ppt_bytes, 512, b2, 0, b2.length);
372: assertTrue(Arrays.equals(b1, b2));
373:
374: pict = (Picture) slides[4].getShapes()[0]; //the fifth slide contains EMF
375: pdata = pict.getPictureData();
376: assertTrue(pdata instanceof EMF);
377: assertEquals(Picture.EMF, pdata.getType());
378: src_bytes = pdata.getData();
379: ppt_bytes = read(new File(cwd, "wrench.emf"));
380: assertTrue(Arrays.equals(src_bytes, ppt_bytes));
381:
382: }
383:
384: /**
385: * Test that on a party corrupt powerpoint document, which has
386: * crazy pictures of type 0, we do our best.
387: */
388: public void testZeroPictureType() throws Exception {
389: HSLFSlideShow hslf = new HSLFSlideShow(new File(cwd,
390: "PictureTypeZero.ppt").getPath());
391:
392: // Should still have 2 real pictures
393: assertEquals(2, hslf.getPictures().length);
394: // Both are real pictures, both WMF
395: assertEquals(Picture.WMF, hslf.getPictures()[0].getType());
396: assertEquals(Picture.WMF, hslf.getPictures()[1].getType());
397:
398: // Now test what happens when we use the SlideShow interface
399: SlideShow ppt = new SlideShow(hslf);
400: Slide[] slides = ppt.getSlides();
401: PictureData[] pictures = ppt.getPictureData();
402: assertEquals(12, slides.length);
403: assertEquals(2, pictures.length);
404:
405: Picture pict;
406: PictureData pdata;
407:
408: pict = (Picture) slides[0].getShapes()[1]; // 2nd object on 1st slide
409: pdata = pict.getPictureData();
410: assertTrue(pdata instanceof WMF);
411: assertEquals(Picture.WMF, pdata.getType());
412:
413: pict = (Picture) slides[0].getShapes()[2]; // 3rd object on 1st slide
414: pdata = pict.getPictureData();
415: assertTrue(pdata instanceof WMF);
416: assertEquals(Picture.WMF, pdata.getType());
417: }
418:
419: public void testZeroPictureLength() throws Exception {
420: HSLFSlideShow hslf = new HSLFSlideShow(new File(cwd,
421: "PictureLengthZero.ppt").getPath());
422:
423: // Should still have 2 real pictures
424: assertEquals(2, hslf.getPictures().length);
425: // Both are real pictures, both WMF
426: assertEquals(Picture.WMF, hslf.getPictures()[0].getType());
427: assertEquals(Picture.WMF, hslf.getPictures()[1].getType());
428:
429: // Now test what happens when we use the SlideShow interface
430: SlideShow ppt = new SlideShow(hslf);
431: Slide[] slides = ppt.getSlides();
432: PictureData[] pictures = ppt.getPictureData();
433: assertEquals(27, slides.length);
434: assertEquals(2, pictures.length);
435:
436: Picture pict;
437: PictureData pdata;
438:
439: pict = (Picture) slides[6].getShapes()[13];
440: pdata = pict.getPictureData();
441: assertTrue(pdata instanceof WMF);
442: assertEquals(Picture.WMF, pdata.getType());
443:
444: pict = (Picture) slides[7].getShapes()[13];
445: pdata = pict.getPictureData();
446: assertTrue(pdata instanceof WMF);
447: assertEquals(Picture.WMF, pdata.getType());
448: }
449: }
|