Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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