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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import static org.junit.jupiter.api.Assertions.assertNull;
  19. import static org.junit.jupiter.api.Assertions.assertSame;
  20. import java.awt.Graphics2D;
  21. import java.awt.Rectangle;
  22. import java.awt.geom.Rectangle2D;
  23. import java.awt.image.BufferedImage;
  24. import java.io.ByteArrayInputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.util.List;
  28. import javax.imageio.ImageIO;
  29. import org.apache.poi.POIDataSamples;
  30. import org.apache.poi.ddf.EscherBSERecord;
  31. import org.apache.poi.ddf.EscherContainerRecord;
  32. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  33. import org.junit.jupiter.api.BeforeAll;
  34. import org.junit.jupiter.api.Test;
  35. /**
  36. * Test Picture shape.
  37. */
  38. public final class TestPicture {
  39. private static final POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  40. @BeforeAll
  41. public static void disableImageIOCache() {
  42. ImageIO.setUseCache(false);
  43. }
  44. /**
  45. * Test that the reference count of a blip is incremented every time the picture is inserted.
  46. * This is important when the same image appears multiple times in a slide show.
  47. *
  48. */
  49. @Test
  50. void multiplePictures() throws IOException {
  51. try (HSLFSlideShow ppt = new HSLFSlideShow()) {
  52. HSLFSlide s = ppt.createSlide();
  53. HSLFSlide s2 = ppt.createSlide();
  54. HSLFSlide s3 = ppt.createSlide();
  55. HSLFPictureData data = ppt.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
  56. HSLFPictureShape pict = new HSLFPictureShape(data);
  57. HSLFPictureShape pict2 = new HSLFPictureShape(data);
  58. HSLFPictureShape pict3 = new HSLFPictureShape(data);
  59. pict.setAnchor(new Rectangle(10, 10, 100, 100));
  60. s.addShape(pict);
  61. EscherBSERecord bse1 = pict.getEscherBSERecord();
  62. assertEquals(1, bse1.getRef());
  63. pict2.setAnchor(new Rectangle(10, 10, 100, 100));
  64. s2.addShape(pict2);
  65. EscherBSERecord bse2 = pict.getEscherBSERecord();
  66. assertSame(bse1, bse2);
  67. assertEquals(2, bse1.getRef());
  68. pict3.setAnchor(new Rectangle(10, 10, 100, 100));
  69. s3.addShape(pict3);
  70. EscherBSERecord bse3 = pict.getEscherBSERecord();
  71. assertSame(bse2, bse3);
  72. assertEquals(3, bse1.getRef());
  73. }
  74. }
  75. /**
  76. * {@link HSLFPictureShape#getEscherBSERecord()} threw {@link NullPointerException} if
  77. * {@link EscherContainerRecord#BSTORE_CONTAINER} was not found. The correct behaviour is to return null.
  78. */
  79. @Test
  80. void bug46122() throws IOException {
  81. HSLFPictureData detachedData;
  82. try (HSLFSlideShow ppt = new HSLFSlideShow()) {
  83. detachedData = ppt.addPicture(new byte[0], PictureType.PNG);
  84. }
  85. try (HSLFSlideShow ppt = new HSLFSlideShow()) {
  86. HSLFSlide slide = ppt.createSlide();
  87. HSLFPictureShape pict = new HSLFPictureShape(detachedData); //index to non-existing picture data
  88. pict.setAnchor(new Rectangle2D.Double(50, 50, 100, 100));
  89. pict.setSheet(slide);
  90. HSLFPictureData data = pict.getPictureData();
  91. assertNull(data);
  92. BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
  93. Graphics2D graphics = img.createGraphics();
  94. pict.draw(graphics, null);
  95. }
  96. }
  97. @Test
  98. void macImages() throws IOException {
  99. try (InputStream is = _slTests.openResourceAsStream("53446.ppt");
  100. HSLFSlideShowImpl hss = new HSLFSlideShowImpl(is)) {
  101. List<HSLFPictureData> pictures = hss.getPictureData();
  102. assertEquals(15, pictures.size());
  103. int[][] expectedSizes = {
  104. null, // WMF
  105. {427, 428}, // PNG
  106. {371, 370}, // PNG
  107. {288, 183}, // PNG
  108. {285, 97}, // PNG
  109. {288, 168}, // PNG
  110. null, // WMF
  111. null, // WMF
  112. {199, 259}, // PNG
  113. {432, 244}, // PNG
  114. {261, 258}, // PNG
  115. null, // WMF
  116. null, // WMF
  117. null, // WMF
  118. null // EMF
  119. };
  120. int i = 0;
  121. for (HSLFPictureData pd : pictures) {
  122. int[] dimensions = expectedSizes[i++];
  123. BufferedImage image = ImageIO.read(new ByteArrayInputStream(pd.getData()));
  124. switch (pd.getType()) {
  125. case WMF:
  126. case EMF:
  127. break;
  128. default:
  129. assertNotNull(image);
  130. assertNotNull(dimensions);
  131. assertEquals(dimensions[0], image.getWidth());
  132. assertEquals(dimensions[1], image.getHeight());
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. }