You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestPictures.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.usermodel;
  16. import static org.junit.Assert.assertArrayEquals;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.net.URL;
  20. import java.util.List;
  21. import junit.framework.TestCase;
  22. import org.apache.poi.POIDataSamples;
  23. import org.apache.poi.hslf.blip.*;
  24. /**
  25. * Test adding/reading pictures
  26. *
  27. * @author Yegor Kozlov
  28. */
  29. public final class TestPictures extends TestCase{
  30. private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  31. //protected File cwd;
  32. /**
  33. * Test read/write Macintosh PICT
  34. */
  35. public void testPICT() throws Exception {
  36. HSLFSlideShow ppt = new HSLFSlideShow();
  37. HSLFSlide slide = ppt.createSlide();
  38. byte[] src_bytes = slTests.readFile("cow.pict");
  39. int idx = ppt.addPicture(src_bytes, HSLFPictureShape.PICT);
  40. HSLFPictureShape pict = new HSLFPictureShape(idx);
  41. assertEquals(idx, pict.getPictureIndex());
  42. slide.addShape(pict);
  43. //serialize and read again
  44. ByteArrayOutputStream out = new ByteArrayOutputStream();
  45. ppt.write(out);
  46. out.close();
  47. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  48. //make sure we can read this picture shape and it refers to the correct picture data
  49. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  50. assertEquals(1, sh.size());
  51. pict = (HSLFPictureShape)sh.get(0);
  52. assertEquals(idx, pict.getPictureIndex());
  53. //check picture data
  54. HSLFPictureData[] pictures = ppt.getPictureData();
  55. //the Picture shape refers to the PictureData object in the Presentation
  56. assertEquals(pict.getPictureData(), pictures[0]);
  57. assertEquals(1, pictures.length);
  58. assertEquals(HSLFPictureShape.PICT, pictures[0].getType());
  59. assertTrue(pictures[0] instanceof PICT);
  60. //compare the content of the initial file with what is stored in the PictureData
  61. byte[] ppt_bytes = pictures[0].getData();
  62. assertEquals(src_bytes.length, ppt_bytes.length);
  63. //in PICT the first 512 bytes are MAC specific and may not be preserved, ignore them
  64. byte[] b1 = new byte[src_bytes.length-512];
  65. System.arraycopy(src_bytes, 512, b1, 0, b1.length);
  66. byte[] b2 = new byte[ppt_bytes.length-512];
  67. System.arraycopy(ppt_bytes, 512, b2, 0, b2.length);
  68. assertArrayEquals(b1, b2);
  69. }
  70. /**
  71. * Test read/write WMF
  72. */
  73. public void testWMF() throws Exception {
  74. HSLFSlideShow ppt = new HSLFSlideShow();
  75. HSLFSlide slide = ppt.createSlide();
  76. byte[] src_bytes = slTests.readFile("santa.wmf");
  77. int idx = ppt.addPicture(src_bytes, HSLFPictureShape.WMF);
  78. HSLFPictureShape pict = new HSLFPictureShape(idx);
  79. assertEquals(idx, pict.getPictureIndex());
  80. slide.addShape(pict);
  81. //serialize and read again
  82. ByteArrayOutputStream out = new ByteArrayOutputStream();
  83. ppt.write(out);
  84. out.close();
  85. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  86. //make sure we can read this picture shape and it refers to the correct picture data
  87. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  88. assertEquals(1, sh.size());
  89. pict = (HSLFPictureShape)sh.get(0);
  90. assertEquals(idx, pict.getPictureIndex());
  91. //check picture data
  92. HSLFPictureData[] pictures = ppt.getPictureData();
  93. //the Picture shape refers to the PictureData object in the Presentation
  94. assertEquals(pict.getPictureData(), pictures[0]);
  95. assertEquals(1, pictures.length);
  96. assertEquals(HSLFPictureShape.WMF, pictures[0].getType());
  97. assertTrue(pictures[0] instanceof WMF);
  98. //compare the content of the initial file with what is stored in the PictureData
  99. byte[] ppt_bytes = pictures[0].getData();
  100. assertEquals(src_bytes.length, ppt_bytes.length);
  101. //in WMF the first 22 bytes - is a metafile header
  102. byte[] b1 = new byte[src_bytes.length-22];
  103. System.arraycopy(src_bytes, 22, b1, 0, b1.length);
  104. byte[] b2 = new byte[ppt_bytes.length-22];
  105. System.arraycopy(ppt_bytes, 22, b2, 0, b2.length);
  106. assertArrayEquals(b1, b2);
  107. }
  108. /**
  109. * Test read/write EMF
  110. */
  111. public void testEMF() throws Exception {
  112. HSLFSlideShow ppt = new HSLFSlideShow();
  113. HSLFSlide slide = ppt.createSlide();
  114. byte[] src_bytes = slTests.readFile("wrench.emf");
  115. int idx = ppt.addPicture(src_bytes, HSLFPictureShape.EMF);
  116. HSLFPictureShape pict = new HSLFPictureShape(idx);
  117. assertEquals(idx, pict.getPictureIndex());
  118. slide.addShape(pict);
  119. //serialize and read again
  120. ByteArrayOutputStream out = new ByteArrayOutputStream();
  121. ppt.write(out);
  122. out.close();
  123. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  124. //make sure we can get this picture shape and it refers to the correct picture data
  125. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  126. assertEquals(1, sh.size());
  127. pict = (HSLFPictureShape)sh.get(0);
  128. assertEquals(idx, pict.getPictureIndex());
  129. //check picture data
  130. HSLFPictureData[] pictures = ppt.getPictureData();
  131. //the Picture shape refers to the PictureData object in the Presentation
  132. assertEquals(pict.getPictureData(), pictures[0]);
  133. assertEquals(1, pictures.length);
  134. assertEquals(HSLFPictureShape.EMF, pictures[0].getType());
  135. assertTrue(pictures[0] instanceof EMF);
  136. //compare the content of the initial file with what is stored in the PictureData
  137. byte[] ppt_bytes = pictures[0].getData();
  138. assertArrayEquals(src_bytes, ppt_bytes);
  139. }
  140. /**
  141. * Test read/write PNG
  142. */
  143. public void testPNG() throws Exception {
  144. HSLFSlideShow ppt = new HSLFSlideShow();
  145. HSLFSlide slide = ppt.createSlide();
  146. byte[] src_bytes = slTests.readFile("tomcat.png");
  147. int idx = ppt.addPicture(src_bytes, HSLFPictureShape.PNG);
  148. HSLFPictureShape pict = new HSLFPictureShape(idx);
  149. assertEquals(idx, pict.getPictureIndex());
  150. slide.addShape(pict);
  151. //serialize and read again
  152. ByteArrayOutputStream out = new ByteArrayOutputStream();
  153. ppt.write(out);
  154. out.close();
  155. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  156. //make sure we can read this picture shape and it refers to the correct picture data
  157. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  158. assertEquals(1, sh.size());
  159. pict = (HSLFPictureShape)sh.get(0);
  160. assertEquals(idx, pict.getPictureIndex());
  161. //check picture data
  162. HSLFPictureData[] pictures = ppt.getPictureData();
  163. //the Picture shape refers to the PictureData object in the Presentation
  164. assertEquals(pict.getPictureData(), pictures[0]);
  165. assertEquals(1, pictures.length);
  166. assertEquals(HSLFPictureShape.PNG, pictures[0].getType());
  167. assertTrue(pictures[0] instanceof PNG);
  168. //compare the content of the initial file with what is stored in the PictureData
  169. byte[] ppt_bytes = pictures[0].getData();
  170. assertArrayEquals(src_bytes, ppt_bytes);
  171. }
  172. /**
  173. * Test read/write JPEG
  174. */
  175. public void testJPEG() throws Exception {
  176. HSLFSlideShow ppt = new HSLFSlideShow();
  177. HSLFSlide slide = ppt.createSlide();
  178. byte[] src_bytes = slTests.readFile("clock.jpg");
  179. int idx = ppt.addPicture(src_bytes, HSLFPictureShape.JPEG);
  180. HSLFPictureShape pict = new HSLFPictureShape(idx);
  181. assertEquals(idx, pict.getPictureIndex());
  182. slide.addShape(pict);
  183. //serialize and read again
  184. ByteArrayOutputStream out = new ByteArrayOutputStream();
  185. ppt.write(out);
  186. out.close();
  187. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  188. //make sure we can read this picture shape and it refers to the correct picture data
  189. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  190. assertEquals(1, sh.size());
  191. pict = (HSLFPictureShape)sh.get(0);
  192. assertEquals(idx, pict.getPictureIndex());
  193. //check picture data
  194. HSLFPictureData[] pictures = ppt.getPictureData();
  195. //the Picture shape refers to the PictureData object in the Presentation
  196. assertEquals(pict.getPictureData(), pictures[0]);
  197. assertEquals(1, pictures.length);
  198. assertEquals(HSLFPictureShape.JPEG, pictures[0].getType());
  199. assertTrue(pictures[0] instanceof JPEG);
  200. //compare the content of the initial file with what is stored in the PictureData
  201. byte[] ppt_bytes = pictures[0].getData();
  202. assertArrayEquals(src_bytes, ppt_bytes);
  203. }
  204. /**
  205. * Test read/write DIB
  206. */
  207. public void testDIB() throws Exception {
  208. HSLFSlideShow ppt = new HSLFSlideShow();
  209. HSLFSlide slide = ppt.createSlide();
  210. byte[] src_bytes = slTests.readFile("clock.dib");
  211. int idx = ppt.addPicture(src_bytes, HSLFPictureShape.DIB);
  212. HSLFPictureShape pict = new HSLFPictureShape(idx);
  213. assertEquals(idx, pict.getPictureIndex());
  214. slide.addShape(pict);
  215. //serialize and read again
  216. ByteArrayOutputStream out = new ByteArrayOutputStream();
  217. ppt.write(out);
  218. out.close();
  219. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  220. //make sure we can read this picture shape and it refers to the correct picture data
  221. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  222. assertEquals(1, sh.size());
  223. pict = (HSLFPictureShape)sh.get(0);
  224. assertEquals(idx, pict.getPictureIndex());
  225. //check picture data
  226. HSLFPictureData[] pictures = ppt.getPictureData();
  227. //the Picture shape refers to the PictureData object in the Presentation
  228. assertEquals(pict.getPictureData(), pictures[0]);
  229. assertEquals(1, pictures.length);
  230. assertEquals(HSLFPictureShape.DIB, pictures[0].getType());
  231. assertTrue(pictures[0] instanceof DIB);
  232. //compare the content of the initial file with what is stored in the PictureData
  233. byte[] ppt_bytes = pictures[0].getData();
  234. assertArrayEquals(src_bytes, ppt_bytes);
  235. }
  236. /**
  237. * Read pictures in different formats from a reference slide show
  238. */
  239. public void testReadPictures() throws Exception {
  240. byte[] src_bytes, ppt_bytes, b1, b2;
  241. HSLFPictureShape pict;
  242. HSLFPictureData pdata;
  243. HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream("pictures.ppt"));
  244. List<HSLFSlide> slides = ppt.getSlides();
  245. HSLFPictureData[] pictures = ppt.getPictureData();
  246. assertEquals(5, pictures.length);
  247. pict = (HSLFPictureShape)slides.get(0).getShapes().get(0); //the first slide contains JPEG
  248. pdata = pict.getPictureData();
  249. assertTrue(pdata instanceof JPEG);
  250. assertEquals(HSLFPictureShape.JPEG, pdata.getType());
  251. src_bytes = pdata.getData();
  252. ppt_bytes = slTests.readFile("clock.jpg");
  253. assertArrayEquals(src_bytes, ppt_bytes);
  254. pict = (HSLFPictureShape)slides.get(1).getShapes().get(0); //the second slide contains PNG
  255. pdata = pict.getPictureData();
  256. assertTrue(pdata instanceof PNG);
  257. assertEquals(HSLFPictureShape.PNG, pdata.getType());
  258. src_bytes = pdata.getData();
  259. ppt_bytes = slTests.readFile("tomcat.png");
  260. assertArrayEquals(src_bytes, ppt_bytes);
  261. pict = (HSLFPictureShape)slides.get(2).getShapes().get(0); //the third slide contains WMF
  262. pdata = pict.getPictureData();
  263. assertTrue(pdata instanceof WMF);
  264. assertEquals(HSLFPictureShape.WMF, pdata.getType());
  265. src_bytes = pdata.getData();
  266. ppt_bytes = slTests.readFile("santa.wmf");
  267. assertEquals(src_bytes.length, ppt_bytes.length);
  268. //ignore the first 22 bytes - it is a WMF metafile header
  269. b1 = new byte[src_bytes.length-22];
  270. System.arraycopy(src_bytes, 22, b1, 0, b1.length);
  271. b2 = new byte[ppt_bytes.length-22];
  272. System.arraycopy(ppt_bytes, 22, b2, 0, b2.length);
  273. assertArrayEquals(b1, b2);
  274. pict = (HSLFPictureShape)slides.get(3).getShapes().get(0); //the forth slide contains PICT
  275. pdata = pict.getPictureData();
  276. assertTrue(pdata instanceof PICT);
  277. assertEquals(HSLFPictureShape.PICT, pdata.getType());
  278. src_bytes = pdata.getData();
  279. ppt_bytes = slTests.readFile("cow.pict");
  280. assertEquals(src_bytes.length, ppt_bytes.length);
  281. //ignore the first 512 bytes - it is a MAC specific crap
  282. b1 = new byte[src_bytes.length-512];
  283. System.arraycopy(src_bytes, 512, b1, 0, b1.length);
  284. b2 = new byte[ppt_bytes.length-512];
  285. System.arraycopy(ppt_bytes, 512, b2, 0, b2.length);
  286. assertArrayEquals(b1, b2);
  287. pict = (HSLFPictureShape)slides.get(4).getShapes().get(0); //the fifth slide contains EMF
  288. pdata = pict.getPictureData();
  289. assertTrue(pdata instanceof EMF);
  290. assertEquals(HSLFPictureShape.EMF, pdata.getType());
  291. src_bytes = pdata.getData();
  292. ppt_bytes = slTests.readFile("wrench.emf");
  293. assertArrayEquals(src_bytes, ppt_bytes);
  294. }
  295. /**
  296. * Test that on a party corrupt powerpoint document, which has
  297. * crazy pictures of type 0, we do our best.
  298. */
  299. public void testZeroPictureType() throws Exception {
  300. HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("PictureTypeZero.ppt"));
  301. // Should still have 2 real pictures
  302. assertEquals(2, hslf.getPictures().length);
  303. // Both are real pictures, both WMF
  304. assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[0].getType());
  305. assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[1].getType());
  306. // Now test what happens when we use the SlideShow interface
  307. HSLFSlideShow ppt = new HSLFSlideShow(hslf);
  308. List<HSLFSlide> slides = ppt.getSlides();
  309. HSLFPictureData[] pictures = ppt.getPictureData();
  310. assertEquals(12, slides.size());
  311. assertEquals(2, pictures.length);
  312. HSLFPictureShape pict;
  313. HSLFPictureData pdata;
  314. pict = (HSLFPictureShape)slides.get(0).getShapes().get(1); // 2nd object on 1st slide
  315. pdata = pict.getPictureData();
  316. assertTrue(pdata instanceof WMF);
  317. assertEquals(HSLFPictureShape.WMF, pdata.getType());
  318. pict = (HSLFPictureShape)slides.get(0).getShapes().get(2); // 3rd object on 1st slide
  319. pdata = pict.getPictureData();
  320. assertTrue(pdata instanceof WMF);
  321. assertEquals(HSLFPictureShape.WMF, pdata.getType());
  322. }
  323. /**
  324. * YK: The test is disabled because the owner asked to delete the test file from POI svn.
  325. * See "Please remove my file from your svn" on @poi-dev from Dec 12, 2013
  326. */
  327. public void disabled_testZeroPictureLength() throws Exception {
  328. // take the data from www instead of test directory
  329. URL url = new URL("http://www.cs.sfu.ca/~anoop/courses/CMPT-882-Fall-2002/chris.ppt");
  330. HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(url.openStream());
  331. // Should still have 2 real pictures
  332. assertEquals(2, hslf.getPictures().length);
  333. // Both are real pictures, both WMF
  334. assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[0].getType());
  335. assertEquals(HSLFPictureShape.WMF, hslf.getPictures()[1].getType());
  336. // Now test what happens when we use the SlideShow interface
  337. HSLFSlideShow ppt = new HSLFSlideShow(hslf);
  338. List<HSLFSlide> slides = ppt.getSlides();
  339. HSLFPictureData[] pictures = ppt.getPictureData();
  340. assertEquals(27, slides.size());
  341. assertEquals(2, pictures.length);
  342. HSLFPictureShape pict;
  343. HSLFPictureData pdata;
  344. pict = (HSLFPictureShape)slides.get(6).getShapes().get(13);
  345. pdata = pict.getPictureData();
  346. assertTrue(pdata instanceof WMF);
  347. assertEquals(HSLFPictureShape.WMF, pdata.getType());
  348. pict = (HSLFPictureShape)slides.get(7).getShapes().get(13);
  349. pdata = pict.getPictureData();
  350. assertTrue(pdata instanceof WMF);
  351. assertEquals(HSLFPictureShape.WMF, pdata.getType());
  352. //add a new picture, it should be correctly appended to the Pictures stream
  353. ByteArrayOutputStream out = new ByteArrayOutputStream();
  354. for(HSLFPictureData p : pictures) p.write(out);
  355. out.close();
  356. int streamSize = out.size();
  357. HSLFPictureData data = HSLFPictureData.create(HSLFPictureShape.JPEG);
  358. data.setData(new byte[100]);
  359. int offset = hslf.addPicture(data);
  360. assertEquals(streamSize, offset);
  361. assertEquals(3, ppt.getPictureData().length);
  362. }
  363. public void testGetPictureName() throws Exception {
  364. HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream("ppt_with_png.ppt"));
  365. HSLFSlide slide = ppt.getSlides().get(0);
  366. HSLFPictureShape p = (HSLFPictureShape)slide.getShapes().get(0); //the first slide contains JPEG
  367. assertEquals("test", p.getPictureName());
  368. }
  369. public void testSetPictureName() throws Exception {
  370. HSLFSlideShow ppt = new HSLFSlideShow();
  371. HSLFSlide slide = ppt.createSlide();
  372. byte[] img = slTests.readFile("tomcat.png");
  373. int idx = ppt.addPicture(img, HSLFPictureShape.PNG);
  374. HSLFPictureShape pict = new HSLFPictureShape(idx);
  375. pict.setPictureName("tomcat.png");
  376. slide.addShape(pict);
  377. //serialize and read again
  378. ByteArrayOutputStream out = new ByteArrayOutputStream();
  379. ppt.write(out);
  380. out.close();
  381. ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
  382. HSLFPictureShape p = (HSLFPictureShape)ppt.getSlides().get(0).getShapes().get(0);
  383. assertEquals("tomcat.png", p.getPictureName());
  384. }
  385. }