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.

TestPicture.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertNull;
  19. import static org.junit.Assert.assertSame;
  20. import java.awt.BasicStroke;
  21. import java.awt.Color;
  22. import java.awt.Dimension;
  23. import java.awt.Graphics2D;
  24. import java.awt.Rectangle;
  25. import java.awt.geom.Rectangle2D;
  26. import java.awt.image.BufferedImage;
  27. import java.io.ByteArrayInputStream;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.lang.reflect.Constructor;
  32. import java.lang.reflect.InvocationTargetException;
  33. import java.util.BitSet;
  34. import java.util.List;
  35. import javax.imageio.ImageIO;
  36. import org.apache.poi.POIDataSamples;
  37. import org.apache.poi.ddf.EscherBSERecord;
  38. import org.apache.poi.hssf.usermodel.DummyGraphics2d;
  39. import org.apache.poi.sl.draw.DrawFactory;
  40. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  41. import org.apache.poi.sl.usermodel.Slide;
  42. import org.apache.poi.sl.usermodel.SlideShow;
  43. import org.junit.BeforeClass;
  44. import org.junit.Ignore;
  45. import org.junit.Test;
  46. /**
  47. * Test Picture shape.
  48. *
  49. * @author Yegor Kozlov
  50. */
  51. public final class TestPicture {
  52. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  53. @BeforeClass
  54. public static void disableImageIOCache() {
  55. ImageIO.setUseCache(false);
  56. }
  57. /**
  58. * Test that the reference count of a blip is incremented every time the picture is inserted.
  59. * This is important when the same image appears multiple times in a slide show.
  60. *
  61. */
  62. @Test
  63. public void multiplePictures() throws IOException {
  64. HSLFSlideShow ppt = new HSLFSlideShow();
  65. HSLFSlide s = ppt.createSlide();
  66. HSLFSlide s2 = ppt.createSlide();
  67. HSLFSlide s3 = ppt.createSlide();
  68. HSLFPictureData data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
  69. HSLFPictureShape pict = new HSLFPictureShape(data);
  70. HSLFPictureShape pict2 = new HSLFPictureShape(data);
  71. HSLFPictureShape pict3 = new HSLFPictureShape(data);
  72. pict.setAnchor(new Rectangle(10,10,100,100));
  73. s.addShape(pict);
  74. EscherBSERecord bse1 = pict.getEscherBSERecord();
  75. assertEquals(1, bse1.getRef());
  76. pict2.setAnchor(new Rectangle(10,10,100,100));
  77. s2.addShape(pict2);
  78. EscherBSERecord bse2 = pict.getEscherBSERecord();
  79. assertSame(bse1, bse2);
  80. assertEquals(2, bse1.getRef());
  81. pict3.setAnchor(new Rectangle(10,10,100,100));
  82. s3.addShape(pict3);
  83. EscherBSERecord bse3 = pict.getEscherBSERecord();
  84. assertSame(bse2, bse3);
  85. assertEquals(3, bse1.getRef());
  86. ppt.close();
  87. }
  88. /**
  89. * Picture#getEscherBSERecord threw NullPointerException if EscherContainerRecord.BSTORE_CONTAINER
  90. * was not found. The correct behaviour is to return null.
  91. */
  92. @Test
  93. public void bug46122() throws IOException {
  94. HSLFSlideShow ppt = new HSLFSlideShow();
  95. HSLFSlide slide = ppt.createSlide();
  96. HSLFPictureData pd = HSLFPictureData.create(PictureType.PNG);
  97. HSLFPictureShape pict = new HSLFPictureShape(pd); //index to non-existing picture data
  98. pict.setAnchor(new Rectangle2D.Double(50,50,100,100));
  99. pict.setSheet(slide);
  100. HSLFPictureData data = pict.getPictureData();
  101. assertNull(data);
  102. BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
  103. Graphics2D graphics = img.createGraphics();
  104. pict.draw(graphics, null);
  105. ppt.close();
  106. }
  107. @Test
  108. public void macImages() throws IOException {
  109. HSLFSlideShowImpl hss = new HSLFSlideShowImpl(_slTests.openResourceAsStream("53446.ppt"));
  110. List<HSLFPictureData> pictures = hss.getPictureData();
  111. assertEquals(15, pictures.size());
  112. int[][] expectedSizes = {
  113. null, // WMF
  114. { 427, 428 }, // PNG
  115. { 371, 370 }, // PNG
  116. { 288, 183 }, // PNG
  117. { 285, 97 }, // PNG
  118. { 288, 168 }, // PNG
  119. null, // WMF
  120. null, // WMF
  121. { 199, 259 }, // PNG
  122. { 432, 244 }, // PNG
  123. { 261, 258 }, // PNG
  124. null, // WMF
  125. null, // WMF
  126. null, // WMF
  127. null // EMF
  128. };
  129. int i = 0;
  130. for (HSLFPictureData pd : pictures) {
  131. int[] dimensions = expectedSizes[i++];
  132. BufferedImage image = ImageIO.read(new ByteArrayInputStream(pd.getData()));
  133. switch (pd.getType()) {
  134. case WMF:
  135. case EMF:
  136. break;
  137. default:
  138. assertNotNull(image);
  139. assertEquals(dimensions[0], image.getWidth());
  140. assertEquals(dimensions[1], image.getHeight());
  141. break;
  142. }
  143. }
  144. hss.close();
  145. }
  146. @Test
  147. @Ignore("Just for visual validation - antialiasing is different on various systems")
  148. public void bug54541()
  149. throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
  150. String files[] = {
  151. // "sample_pptx_grouping_issues.pptx",
  152. // "54542_cropped_bitmap.pptx",
  153. // "54541_cropped_bitmap.ppt",
  154. // "54541_cropped_bitmap2.ppt",
  155. "alterman_security.ppt",
  156. // "alterman_security3.pptx",
  157. };
  158. BitSet pages = new BitSet();
  159. pages.set(2);
  160. for (String file : files) {
  161. InputStream is = _slTests.openResourceAsStream(file);
  162. SlideShow<?,?> ss;
  163. if (file.endsWith("pptx")) {
  164. Class<?> cls = Class.forName("org.apache.poi.xslf.usermodel.XMLSlideShow");
  165. Constructor<?> ct = cls.getDeclaredConstructor(InputStream.class);
  166. ss = (SlideShow<?,?>)ct.newInstance(is);
  167. } else {
  168. ss = new HSLFSlideShow(is);
  169. }
  170. is.close();
  171. boolean debugOut = false;
  172. Dimension pg = ss.getPageSize();
  173. for (Slide<?,?> slide : ss.getSlides()) {
  174. int slideNo = slide.getSlideNumber();
  175. if (!pages.get(slideNo-1)) {
  176. if (pages.nextSetBit(slideNo-1) == -1) break; else continue;
  177. }
  178. if (debugOut) {
  179. DummyGraphics2d graphics = new DummyGraphics2d();
  180. slide.draw(graphics);
  181. } else {
  182. BufferedImage img = new BufferedImage(pg.width, pg.height, BufferedImage.TYPE_INT_ARGB);
  183. Graphics2D graphics = img.createGraphics();
  184. slide.draw(graphics);
  185. graphics.setColor(Color.BLACK);
  186. graphics.setStroke(new BasicStroke(1));
  187. graphics.drawRect(0, 0, (int)pg.getWidth()-1, (int)pg.getHeight()-1);
  188. ImageIO.write(img, "PNG", new File(file.replaceFirst(".pptx?", "-")+slideNo+".png"));
  189. }
  190. }
  191. ss.close();
  192. }
  193. }
  194. }