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 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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.jupiter.api.Assertions.assertArrayEquals;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertTrue;
  19. import java.awt.Dimension;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.net.URL;
  24. import java.util.Arrays;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.Random;
  28. import org.apache.poi.POIDataSamples;
  29. import org.apache.poi.ddf.EscherBSERecord;
  30. import org.apache.poi.ddf.EscherContainerRecord;
  31. import org.apache.poi.ddf.EscherRecord;
  32. import org.apache.poi.hslf.HSLFTestDataSamples;
  33. import org.apache.poi.hslf.blip.DIB;
  34. import org.apache.poi.hslf.blip.EMF;
  35. import org.apache.poi.hslf.blip.JPEG;
  36. import org.apache.poi.hslf.blip.PICT;
  37. import org.apache.poi.hslf.blip.PNG;
  38. import org.apache.poi.hslf.blip.WMF;
  39. import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
  40. import org.apache.poi.sl.image.ImageHeaderEMF;
  41. import org.apache.poi.sl.image.ImageHeaderPICT;
  42. import org.apache.poi.sl.image.ImageHeaderWMF;
  43. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  44. import org.apache.poi.util.Units;
  45. import org.junit.jupiter.api.Disabled;
  46. import org.junit.jupiter.api.Test;
  47. /**
  48. * Test adding/reading pictures
  49. *
  50. * @author Yegor Kozlov
  51. */
  52. public final class TestPictures {
  53. private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  54. /**
  55. * Test read/write Macintosh PICT
  56. */
  57. @Test
  58. void testPICT() throws IOException {
  59. HSLFSlideShow ppt = new HSLFSlideShow();
  60. HSLFSlide slide = ppt.createSlide();
  61. byte[] src_bytes = slTests.readFile("cow.pict");
  62. HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PICT);
  63. ImageHeaderPICT nHeader = new ImageHeaderPICT(src_bytes, 512);
  64. final int expWidth = 197, expHeight = 137;
  65. Dimension nDim = nHeader.getSize();
  66. assertEquals(expWidth, nDim.getWidth(), 0);
  67. assertEquals(expHeight, nDim.getHeight(), 0);
  68. Dimension dim = data.getImageDimensionInPixels();
  69. assertEquals(Units.pointsToPixel(expWidth), dim.getWidth(), 0);
  70. assertEquals(Units.pointsToPixel(expHeight), dim.getHeight(), 0);
  71. HSLFPictureShape pict = new HSLFPictureShape(data);
  72. assertEquals(data.getIndex(), pict.getPictureIndex());
  73. slide.addShape(pict);
  74. //serialize and read again
  75. ByteArrayOutputStream out = new ByteArrayOutputStream();
  76. ppt.write(out);
  77. out.close();
  78. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  79. //make sure we can read this picture shape and it refers to the correct picture data
  80. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  81. assertEquals(1, sh.size());
  82. pict = (HSLFPictureShape)sh.get(0);
  83. assertEquals(data.getIndex(), pict.getPictureIndex());
  84. //check picture data
  85. List<HSLFPictureData> pictures = ppt.getPictureData();
  86. assertEquals(1, pictures.size());
  87. HSLFPictureData pd = pictures.get(0);
  88. dim = pd.getImageDimension();
  89. assertEquals(expWidth, dim.width);
  90. assertEquals(expHeight, dim.height);
  91. //the Picture shape refers to the PictureData object in the Presentation
  92. assertEquals(pict.getPictureData(), pd);
  93. assertEquals(1, pictures.size());
  94. assertEquals(PictureType.PICT, pd.getType());
  95. assertTrue(pd instanceof PICT);
  96. //compare the content of the initial file with what is stored in the PictureData
  97. byte[] ppt_bytes = pd.getData();
  98. assertEquals(src_bytes.length, ppt_bytes.length);
  99. //in PICT the first 512 bytes are MAC specific and may not be preserved, ignore them
  100. byte[] b1 = Arrays.copyOfRange(src_bytes, 512, src_bytes.length);
  101. byte[] b2 = Arrays.copyOfRange(ppt_bytes, 512, ppt_bytes.length);
  102. assertArrayEquals(b1, b2);
  103. }
  104. /**
  105. * Test read/write WMF
  106. */
  107. @Test
  108. void testWMF() throws IOException {
  109. HSLFSlideShow ppt = new HSLFSlideShow();
  110. HSLFSlide slide = ppt.createSlide();
  111. byte[] src_bytes = slTests.readFile("santa.wmf");
  112. HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.WMF);
  113. ImageHeaderWMF nHeader = new ImageHeaderWMF(src_bytes, 0);
  114. final int expWidth = 136, expHeight = 146;
  115. Dimension nDim = nHeader.getSize();
  116. assertEquals(expWidth, nDim.getWidth(), 0);
  117. assertEquals(expHeight, nDim.getHeight(), 0);
  118. Dimension dim = data.getImageDimensionInPixels();
  119. assertEquals(Units.pointsToPixel(expWidth), dim.getWidth(), 0);
  120. assertEquals(Units.pointsToPixel(expHeight), dim.getHeight(), 0);
  121. HSLFPictureShape pict = new HSLFPictureShape(data);
  122. assertEquals(data.getIndex(), pict.getPictureIndex());
  123. slide.addShape(pict);
  124. //serialize and read again
  125. ByteArrayOutputStream out = new ByteArrayOutputStream();
  126. ppt.write(out);
  127. out.close();
  128. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  129. //make sure we can read this picture shape and it refers to the correct picture data
  130. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  131. assertEquals(1, sh.size());
  132. pict = (HSLFPictureShape)sh.get(0);
  133. assertEquals(data.getIndex(), pict.getPictureIndex());
  134. //check picture data
  135. List<HSLFPictureData> pictures = ppt.getPictureData();
  136. assertEquals(1, pictures.size());
  137. HSLFPictureData pd = pictures.get(0);
  138. dim = pd.getImageDimension();
  139. assertEquals(expWidth, dim.width);
  140. assertEquals(expHeight, dim.height);
  141. //the Picture shape refers to the PictureData object in the Presentation
  142. assertEquals(pict.getPictureData(), pd);
  143. assertEquals(PictureType.WMF, pd.getType());
  144. assertTrue(pd instanceof WMF);
  145. //compare the content of the initial file with what is stored in the PictureData
  146. byte[] ppt_bytes = pd.getData();
  147. assertEquals(src_bytes.length, ppt_bytes.length);
  148. //in WMF the first 22 bytes - is a metafile header
  149. byte[] b1 = Arrays.copyOfRange(src_bytes, 22, src_bytes.length);
  150. byte[] b2 = Arrays.copyOfRange(ppt_bytes, 22, ppt_bytes.length);
  151. assertArrayEquals(b1, b2);
  152. }
  153. /**
  154. * Test read/write EMF
  155. */
  156. @Test
  157. void testEMF() throws IOException {
  158. HSLFSlideShow ppt = new HSLFSlideShow();
  159. HSLFSlide slide = ppt.createSlide();
  160. byte[] src_bytes = slTests.readFile("wrench.emf");
  161. HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.EMF);
  162. ImageHeaderEMF nHeader = new ImageHeaderEMF(src_bytes, 0);
  163. final int expWidth = 190, expHeight = 115;
  164. Dimension nDim = nHeader.getSize();
  165. assertEquals(expWidth, nDim.getWidth(), 0);
  166. assertEquals(expHeight, nDim.getHeight(), 0);
  167. Dimension dim = data.getImageDimensionInPixels();
  168. assertEquals(Units.pointsToPixel(expWidth), dim.getWidth(), 0);
  169. assertEquals(Units.pointsToPixel(expHeight), dim.getHeight(), 0);
  170. HSLFPictureShape pict = new HSLFPictureShape(data);
  171. assertEquals(data.getIndex(), pict.getPictureIndex());
  172. slide.addShape(pict);
  173. //serialize and read again
  174. ByteArrayOutputStream out = new ByteArrayOutputStream();
  175. ppt.write(out);
  176. out.close();
  177. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  178. //make sure we can get this picture shape and it refers to the correct picture data
  179. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  180. assertEquals(1, sh.size());
  181. pict = (HSLFPictureShape)sh.get(0);
  182. assertEquals(data.getIndex(), pict.getPictureIndex());
  183. //check picture data
  184. List<HSLFPictureData> pictures = ppt.getPictureData();
  185. assertEquals(1, pictures.size());
  186. HSLFPictureData pd = pictures.get(0);
  187. dim = pd.getImageDimension();
  188. assertEquals(expWidth, dim.width);
  189. assertEquals(expHeight, dim.height);
  190. //the Picture shape refers to the PictureData object in the Presentation
  191. assertEquals(pict.getPictureData(), pd);
  192. assertEquals(1, pictures.size());
  193. assertEquals(PictureType.EMF, pd.getType());
  194. assertTrue(pd instanceof EMF);
  195. //compare the content of the initial file with what is stored in the PictureData
  196. byte[] ppt_bytes = pd.getData();
  197. assertArrayEquals(src_bytes, ppt_bytes);
  198. }
  199. /**
  200. * Test read/write PNG
  201. */
  202. @Test
  203. void testPNG() throws IOException {
  204. HSLFSlideShow ppt = new HSLFSlideShow();
  205. HSLFSlide slide = ppt.createSlide();
  206. byte[] src_bytes = slTests.readFile("tomcat.png");
  207. HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.PNG);
  208. HSLFPictureShape pict = new HSLFPictureShape(data);
  209. assertEquals(data.getIndex(), pict.getPictureIndex());
  210. slide.addShape(pict);
  211. //serialize and read again
  212. ByteArrayOutputStream out = new ByteArrayOutputStream();
  213. ppt.write(out);
  214. out.close();
  215. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  216. //make sure we can read this picture shape and it refers to the correct picture data
  217. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  218. assertEquals(1, sh.size());
  219. pict = (HSLFPictureShape)sh.get(0);
  220. assertEquals(data.getIndex(), pict.getPictureIndex());
  221. //check picture data
  222. List<HSLFPictureData> pictures = ppt.getPictureData();
  223. //the Picture shape refers to the PictureData object in the Presentation
  224. assertEquals(pict.getPictureData(), pictures.get(0));
  225. assertEquals(1, pictures.size());
  226. assertEquals(PictureType.PNG, pictures.get(0).getType());
  227. assertTrue(pictures.get(0) instanceof PNG);
  228. //compare the content of the initial file with what is stored in the PictureData
  229. byte[] ppt_bytes = pictures.get(0).getData();
  230. assertArrayEquals(src_bytes, ppt_bytes);
  231. }
  232. /**
  233. * Test read/write JPEG
  234. */
  235. @Test
  236. void testJPEG() throws IOException {
  237. HSLFSlideShow ppt = new HSLFSlideShow();
  238. HSLFSlide slide = ppt.createSlide();
  239. byte[] src_bytes = slTests.readFile("clock.jpg");
  240. HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.JPEG);
  241. HSLFPictureShape pict = new HSLFPictureShape(data);
  242. assertEquals(data.getIndex(), pict.getPictureIndex());
  243. slide.addShape(pict);
  244. //serialize and read again
  245. ByteArrayOutputStream out = new ByteArrayOutputStream();
  246. ppt.write(out);
  247. out.close();
  248. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  249. //make sure we can read this picture shape and it refers to the correct picture data
  250. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  251. assertEquals(1, sh.size());
  252. pict = (HSLFPictureShape)sh.get(0);
  253. assertEquals(data.getIndex(), pict.getPictureIndex());
  254. //check picture data
  255. List<HSLFPictureData> pictures = ppt.getPictureData();
  256. //the Picture shape refers to the PictureData object in the Presentation
  257. assertEquals(pict.getPictureData(), pictures.get(0));
  258. assertEquals(1, pictures.size());
  259. assertEquals(PictureType.JPEG, pictures.get(0).getType());
  260. assertTrue(pictures.get(0) instanceof JPEG);
  261. //compare the content of the initial file with what is stored in the PictureData
  262. byte[] ppt_bytes = pictures.get(0).getData();
  263. assertArrayEquals(src_bytes, ppt_bytes);
  264. }
  265. /**
  266. * Test read/write DIB
  267. */
  268. @Test
  269. void testDIB() throws IOException {
  270. HSLFSlideShow ppt = new HSLFSlideShow();
  271. HSLFSlide slide = ppt.createSlide();
  272. byte[] src_bytes = slTests.readFile("clock.dib");
  273. HSLFPictureData data = ppt.addPicture(src_bytes, PictureType.DIB);
  274. HSLFPictureShape pict = new HSLFPictureShape(data);
  275. assertEquals(data.getIndex(), pict.getPictureIndex());
  276. slide.addShape(pict);
  277. //serialize and read again
  278. ByteArrayOutputStream out = new ByteArrayOutputStream();
  279. ppt.write(out);
  280. out.close();
  281. ppt = new HSLFSlideShow(new HSLFSlideShowImpl(new ByteArrayInputStream(out.toByteArray())));
  282. //make sure we can read this picture shape and it refers to the correct picture data
  283. List<HSLFShape> sh = ppt.getSlides().get(0).getShapes();
  284. assertEquals(1, sh.size());
  285. pict = (HSLFPictureShape)sh.get(0);
  286. assertEquals(data.getIndex(), pict.getPictureIndex());
  287. //check picture data
  288. List<HSLFPictureData> pictures = ppt.getPictureData();
  289. //the Picture shape refers to the PictureData object in the Presentation
  290. assertEquals(pict.getPictureData(), pictures.get(0));
  291. assertEquals(1, pictures.size());
  292. assertEquals(PictureType.DIB, pictures.get(0).getType());
  293. assertTrue(pictures.get(0) instanceof DIB);
  294. //compare the content of the initial file with what is stored in the PictureData
  295. byte[] ppt_bytes = pictures.get(0).getData();
  296. assertArrayEquals(src_bytes, ppt_bytes);
  297. }
  298. /**
  299. * Read pictures in different formats from a reference slide show
  300. */
  301. @Test
  302. void testReadPictures() throws IOException {
  303. byte[] src_bytes, ppt_bytes, b1, b2;
  304. HSLFPictureShape pict;
  305. HSLFPictureData pdata;
  306. HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("pictures.ppt");
  307. List<HSLFSlide> slides = ppt.getSlides();
  308. List<HSLFPictureData> pictures = ppt.getPictureData();
  309. assertEquals(5, pictures.size());
  310. pict = (HSLFPictureShape)slides.get(0).getShapes().get(0); //the first slide contains JPEG
  311. pdata = pict.getPictureData();
  312. assertTrue(pdata instanceof JPEG);
  313. assertEquals(PictureType.JPEG, pdata.getType());
  314. src_bytes = pdata.getData();
  315. ppt_bytes = slTests.readFile("clock.jpg");
  316. assertArrayEquals(src_bytes, ppt_bytes);
  317. pict = (HSLFPictureShape)slides.get(1).getShapes().get(0); //the second slide contains PNG
  318. pdata = pict.getPictureData();
  319. assertTrue(pdata instanceof PNG);
  320. assertEquals(PictureType.PNG, pdata.getType());
  321. src_bytes = pdata.getData();
  322. ppt_bytes = slTests.readFile("tomcat.png");
  323. assertArrayEquals(src_bytes, ppt_bytes);
  324. pict = (HSLFPictureShape)slides.get(2).getShapes().get(0); //the third slide contains WMF
  325. pdata = pict.getPictureData();
  326. assertTrue(pdata instanceof WMF);
  327. assertEquals(PictureType.WMF, pdata.getType());
  328. src_bytes = pdata.getData();
  329. ppt_bytes = slTests.readFile("santa.wmf");
  330. assertEquals(src_bytes.length, ppt_bytes.length);
  331. //ignore the first 22 bytes - it is a WMF metafile header
  332. b1 = Arrays.copyOfRange(src_bytes, 22, src_bytes.length);
  333. b2 = Arrays.copyOfRange(ppt_bytes, 22, ppt_bytes.length);
  334. assertArrayEquals(b1, b2);
  335. pict = (HSLFPictureShape)slides.get(3).getShapes().get(0); //the forth slide contains PICT
  336. pdata = pict.getPictureData();
  337. assertTrue(pdata instanceof PICT);
  338. assertEquals(PictureType.PICT, pdata.getType());
  339. src_bytes = pdata.getData();
  340. ppt_bytes = slTests.readFile("cow.pict");
  341. assertEquals(src_bytes.length, ppt_bytes.length);
  342. //ignore the first 512 bytes - it is a MAC specific crap
  343. b1 = Arrays.copyOfRange(src_bytes, 512, src_bytes.length);
  344. b2 = Arrays.copyOfRange(ppt_bytes, 512, ppt_bytes.length);
  345. assertArrayEquals(b1, b2);
  346. pict = (HSLFPictureShape)slides.get(4).getShapes().get(0); //the fifth slide contains EMF
  347. pdata = pict.getPictureData();
  348. assertTrue(pdata instanceof EMF);
  349. assertEquals(PictureType.EMF, pdata.getType());
  350. src_bytes = pdata.getData();
  351. ppt_bytes = slTests.readFile("wrench.emf");
  352. assertArrayEquals(src_bytes, ppt_bytes);
  353. ppt.close();
  354. }
  355. /**
  356. * Test that on a party corrupt powerpoint document, which has
  357. * crazy pictures of type 0, we do our best.
  358. */
  359. @Test
  360. void testZeroPictureType() throws IOException {
  361. HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(slTests.openResourceAsStream("PictureTypeZero.ppt"));
  362. // Should still have 2 real pictures
  363. assertEquals(2, hslf.getPictureData().size());
  364. // Both are real pictures, both WMF
  365. assertEquals(PictureType.WMF, hslf.getPictureData().get(0).getType());
  366. assertEquals(PictureType.WMF, hslf.getPictureData().get(1).getType());
  367. // Now test what happens when we use the SlideShow interface
  368. HSLFSlideShow ppt = new HSLFSlideShow(hslf);
  369. List<HSLFSlide> slides = ppt.getSlides();
  370. List<HSLFPictureData> pictures = ppt.getPictureData();
  371. assertEquals(12, slides.size());
  372. assertEquals(2, pictures.size());
  373. HSLFPictureShape pict;
  374. HSLFPictureData pdata;
  375. pict = (HSLFPictureShape)slides.get(0).getShapes().get(1); // 2nd object on 1st slide
  376. pdata = pict.getPictureData();
  377. assertTrue(pdata instanceof WMF);
  378. assertEquals(PictureType.WMF, pdata.getType());
  379. pict = (HSLFPictureShape)slides.get(0).getShapes().get(2); // 3rd object on 1st slide
  380. pdata = pict.getPictureData();
  381. assertTrue(pdata instanceof WMF);
  382. assertEquals(PictureType.WMF, pdata.getType());
  383. ppt.close();
  384. }
  385. /**
  386. * YK: The test is disabled because the owner asked to delete the test file from POI svn.
  387. * See "Please remove my file from your svn" on @poi-dev from Dec 12, 2013
  388. */
  389. @Test
  390. @Disabled("requires an internet connection to a 3rd party site")
  391. // As of 2017-06-20, the file still exists at the specified URL and the test passes.
  392. void testZeroPictureLength() throws IOException {
  393. // take the data from www instead of test directory
  394. URL url = new URL("http://www.cs.sfu.ca/~anoop/courses/CMPT-882-Fall-2002/chris.ppt");
  395. HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(url.openStream());
  396. /* Assume that the file could retrieved...
  397. InputStream is;
  398. HSLFSlideShowImpl hslf;
  399. try {
  400. is = url.openStream();
  401. hslf = new HSLFSlideShowImpl(is);
  402. is.close();
  403. } catch (final IOException e) {
  404. Assume.assumeTrue(e.getMessage(), false);
  405. throw e;
  406. }
  407. */
  408. // Should still have 2 real pictures
  409. assertEquals(2, hslf.getPictureData().size());
  410. // Both are real pictures, both WMF
  411. assertEquals(PictureType.WMF, hslf.getPictureData().get(0).getType());
  412. assertEquals(PictureType.WMF, hslf.getPictureData().get(1).getType());
  413. // Now test what happens when we use the SlideShow interface
  414. HSLFSlideShow ppt = new HSLFSlideShow(hslf);
  415. List<HSLFSlide> slides = ppt.getSlides();
  416. List<HSLFPictureData> pictures = ppt.getPictureData();
  417. assertEquals(27, slides.size());
  418. assertEquals(2, pictures.size());
  419. HSLFPictureShape pict;
  420. HSLFPictureData pdata;
  421. pict = (HSLFPictureShape)slides.get(6).getShapes().get(13);
  422. pdata = pict.getPictureData();
  423. assertTrue(pdata instanceof WMF);
  424. assertEquals(PictureType.WMF, pdata.getType());
  425. pict = (HSLFPictureShape)slides.get(7).getShapes().get(13);
  426. pdata = pict.getPictureData();
  427. assertTrue(pdata instanceof WMF);
  428. assertEquals(PictureType.WMF, pdata.getType());
  429. //add a new picture, it should be correctly appended to the Pictures stream
  430. ByteArrayOutputStream out = new ByteArrayOutputStream();
  431. for(HSLFPictureData p : pictures) p.write(out);
  432. out.close();
  433. int streamSize = out.size();
  434. HSLFPictureData data = ppt.addPicture(new byte[100], PictureType.JPEG);
  435. int offset = data.getOffset();
  436. assertEquals(streamSize, offset);
  437. assertEquals(3, ppt.getPictureData().size());
  438. ppt.close();
  439. }
  440. @Test
  441. void testGetPictureName() throws IOException {
  442. HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("ppt_with_png.ppt");
  443. HSLFSlide slide = ppt.getSlides().get(0);
  444. HSLFPictureShape p = (HSLFPictureShape)slide.getShapes().get(0); //the first slide contains JPEG
  445. assertEquals("test", p.getPictureName());
  446. ppt.close();
  447. }
  448. @Test
  449. void testSetPictureName() throws IOException {
  450. HSLFSlideShow ppt = new HSLFSlideShow();
  451. HSLFSlide slide = ppt.createSlide();
  452. byte[] img = slTests.readFile("tomcat.png");
  453. HSLFPictureData data = ppt.addPicture(img, PictureType.PNG);
  454. HSLFPictureShape pict = new HSLFPictureShape(data);
  455. pict.setPictureName("tomcat.png");
  456. slide.addShape(pict);
  457. //serialize and read again
  458. ByteArrayOutputStream out = new ByteArrayOutputStream();
  459. ppt.write(out);
  460. out.close();
  461. ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
  462. HSLFPictureShape p = (HSLFPictureShape)ppt.getSlides().get(0).getShapes().get(0);
  463. assertEquals("tomcat.png", p.getPictureName());
  464. }
  465. @Test
  466. void testPictureIndexIsOneBased() throws IOException {
  467. try (HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("ppt_with_png.ppt")) {
  468. HSLFPictureData picture = ppt.getPictureData().get(0);
  469. assertEquals(1, picture.getIndex());
  470. }
  471. }
  472. /**
  473. * Verify that it is possible for a user to change the contents of a {@link HSLFPictureData} using
  474. * {@link HSLFPictureData#setData(byte[])}, and that the changes are saved to the slideshow.
  475. */
  476. @Test
  477. void testEditPictureData() throws IOException {
  478. byte[] newImage = slTests.readFile("tomcat.png");
  479. ByteArrayOutputStream modifiedSlideShow = new ByteArrayOutputStream();
  480. // Load an existing slideshow and modify the image
  481. try (HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("ppt_with_png.ppt")) {
  482. HSLFPictureData picture = ppt.getPictureData().get(0);
  483. picture.setData(newImage);
  484. ppt.write(modifiedSlideShow);
  485. }
  486. // Load the modified slideshow and verify the image content
  487. try (HSLFSlideShow ppt = new HSLFSlideShow(new ByteArrayInputStream(modifiedSlideShow.toByteArray()))) {
  488. HSLFPictureData picture = ppt.getPictureData().get(0);
  489. byte[] modifiedImageData = picture.getData();
  490. assertArrayEquals(newImage, modifiedImageData);
  491. }
  492. }
  493. /**
  494. * Verify that it is possible for a user to change the contents of an encrypted {@link HSLFPictureData} using
  495. * {@link HSLFPictureData#setData(byte[])}, and that the changes are saved to the slideshow.
  496. */
  497. @Test
  498. void testEditPictureDataEncrypted() throws IOException {
  499. byte[] newImage = slTests.readFile("tomcat.png");
  500. ByteArrayOutputStream modifiedSlideShow = new ByteArrayOutputStream();
  501. Biff8EncryptionKey.setCurrentUserPassword("password");
  502. try {
  503. // Load an existing slideshow and modify the image
  504. try (HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("ppt_with_png_encrypted.ppt")) {
  505. HSLFPictureData picture = ppt.getPictureData().get(0);
  506. picture.setData(newImage);
  507. ppt.write(modifiedSlideShow);
  508. }
  509. // Load the modified slideshow and verify the image content
  510. try (HSLFSlideShow ppt = new HSLFSlideShow(new ByteArrayInputStream(modifiedSlideShow.toByteArray()))) {
  511. HSLFPictureData picture = ppt.getPictureData().get(0);
  512. byte[] modifiedImageData = picture.getData();
  513. assertArrayEquals(newImage, modifiedImageData);
  514. }
  515. } finally {
  516. Biff8EncryptionKey.setCurrentUserPassword(null);
  517. }
  518. }
  519. /**
  520. * Verify that the {@link EscherBSERecord#getOffset()} values are modified for all images after the image being
  521. * changed.
  522. */
  523. @Test
  524. void testEditPictureDataRecordOffsetsAreShifted() throws IOException {
  525. int[] originalOffsets = {0, 12013, 15081, 34162, 59563};
  526. int[] modifiedOffsets = {0, 35, 3103, 22184, 47585};
  527. ByteArrayOutputStream inMemory = new ByteArrayOutputStream();
  528. try (HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("pictures.ppt")) {
  529. int[] offsets = ppt.getPictureData().stream().mapToInt(HSLFPictureData::getOffset).toArray();
  530. assertArrayEquals(originalOffsets, offsets);
  531. HSLFPictureData imageBeingChanged = ppt.getPictureData().get(0);
  532. // It doesn't matter that this isn't a valid image. We are just testing offsets here.
  533. imageBeingChanged.setData(new byte[10]);
  534. // Verify that the in-memory representations have all been updated
  535. offsets = ppt.getPictureData().stream().mapToInt(HSLFPictureData::getOffset).toArray();
  536. assertArrayEquals(modifiedOffsets, offsets);
  537. ppt.write(inMemory);
  538. }
  539. try (HSLFSlideShow ppt = new HSLFSlideShow(new ByteArrayInputStream(inMemory.toByteArray()))) {
  540. // Verify that the persisted representations have all been updated
  541. int[] offsets = ppt.getPictureData().stream().mapToInt(HSLFPictureData::getOffset).toArray();
  542. assertArrayEquals(modifiedOffsets, offsets);
  543. }
  544. }
  545. /**
  546. * Verify that the {@link EscherBSERecord#getOffset()} values are modified for all images after the image being
  547. * changed, but assuming that the records are not stored in a sorted-by-offset fashion.
  548. *
  549. * We have not encountered a file that has meaningful data that is not sorted. However, we have encountered files
  550. * that have records with an offset of 0 interspersed between meaningful records. See {@code 53446.ppt} and
  551. * {@code alterman_security.ppt} for examples.
  552. */
  553. @Test
  554. void testEditPictureDataOutOfOrderRecords() throws IOException {
  555. int[] modifiedOffsets = {0, 35, 3103, 22184, 47585};
  556. ByteArrayOutputStream inMemory = new ByteArrayOutputStream();
  557. try (HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("pictures.ppt")) {
  558. // For this test we're going to intentionally manipulate the records into a shuffled order.
  559. EscherContainerRecord container = ppt.getPictureData().get(0).bStore;
  560. List<EscherRecord> children = container.getChildRecords();
  561. for (EscherRecord child : children) {
  562. container.removeChildRecord(child);
  563. }
  564. Collections.shuffle(children);
  565. for (EscherRecord child : children) {
  566. container.addChildRecord(child);
  567. }
  568. HSLFPictureData imageBeingChanged = ppt.getPictureData().get(0);
  569. // It doesn't matter that this isn't a valid image. We are just testing offsets here.
  570. imageBeingChanged.setData(new byte[10]);
  571. // Verify that the in-memory representations have all been updated
  572. int[] offsets = ppt.getPictureData().stream().mapToInt(HSLFPictureData::getOffset).toArray();
  573. Arrays.sort(offsets);
  574. assertArrayEquals(modifiedOffsets, offsets);
  575. ppt.write(inMemory);
  576. }
  577. try (HSLFSlideShow ppt = new HSLFSlideShow(new ByteArrayInputStream(inMemory.toByteArray()))) {
  578. // Verify that the persisted representations have all been updated
  579. int[] offsets = ppt.getPictureData().stream().mapToInt(HSLFPictureData::getOffset).toArray();
  580. Arrays.sort(offsets);
  581. assertArrayEquals(modifiedOffsets, offsets);
  582. }
  583. }
  584. /**
  585. * Verify that a slideshow with records that have offsets not matching those of the pictures in the stream still
  586. * correctly pairs the records and pictures.
  587. */
  588. @Test
  589. void testSlideshowWithIncorrectOffsets() throws IOException {
  590. int[] originalOffsets;
  591. int originalNumberOfRecords;
  592. // Create a presentation that has records with unmatched offsets, but with matched UIDs.
  593. ByteArrayOutputStream inMemory = new ByteArrayOutputStream();
  594. try (HSLFSlideShow ppt = HSLFTestDataSamples.getSlideShow("pictures.ppt")) {
  595. originalOffsets = ppt.getPictureData().stream().mapToInt(HSLFPictureData::getOffset).toArray();
  596. originalNumberOfRecords = ppt.getPictureData().get(0).bStore.getChildRecords().size();
  597. Random random = new Random();
  598. for (HSLFPictureData picture : ppt.getPictureData()) {
  599. // Bound is arbitrary and irrelevant to the test.
  600. picture.bse.setOffset(random.nextInt(500_000));
  601. }
  602. ppt.write(inMemory);
  603. }
  604. try (HSLFSlideShow ppt = new HSLFSlideShow(new ByteArrayInputStream(inMemory.toByteArray()))) {
  605. // Verify that the offsets all got fixed.
  606. int[] offsets = ppt.getPictureData().stream().mapToInt(HSLFPictureData::getOffset).toArray();
  607. assertArrayEquals(originalOffsets, offsets);
  608. // Verify that there are the same number of records as in the original slideshow.
  609. int numberOfRecords = ppt.getPictureData().get(0).bStore.getChildRecords().size();
  610. assertEquals(originalNumberOfRecords, numberOfRecords);
  611. }
  612. }
  613. }