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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.*;
  17. import java.awt.*;
  18. import java.awt.image.BufferedImage;
  19. import java.io.*;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import javax.imageio.ImageIO;
  23. import org.apache.poi.POIDataSamples;
  24. import org.apache.poi.ddf.EscherBSERecord;
  25. import org.apache.poi.hssf.usermodel.DummyGraphics2d;
  26. import org.apache.poi.sl.draw.Drawable;
  27. import org.apache.poi.sl.usermodel.Slide;
  28. import org.apache.poi.sl.usermodel.SlideShow;
  29. import org.apache.poi.util.JvmBugs;
  30. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  31. import org.junit.Ignore;
  32. import org.junit.Test;
  33. /**
  34. * Test Picture shape.
  35. *
  36. * @author Yegor Kozlov
  37. */
  38. public final class TestPicture {
  39. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  40. /**
  41. * Test that the reference count of a blip is incremented every time the picture is inserted.
  42. * This is important when the same image appears multiple times in a slide show.
  43. *
  44. */
  45. @Test
  46. public void multiplePictures() throws Exception {
  47. HSLFSlideShow ppt = new HSLFSlideShow();
  48. HSLFSlide s = ppt.createSlide();
  49. HSLFSlide s2 = ppt.createSlide();
  50. HSLFSlide s3 = ppt.createSlide();
  51. int idx = ppt.addPicture(_slTests.readFile("clock.jpg"), HSLFPictureShape.JPEG);
  52. HSLFPictureShape pict = new HSLFPictureShape(idx);
  53. HSLFPictureShape pict2 = new HSLFPictureShape(idx);
  54. HSLFPictureShape pict3 = new HSLFPictureShape(idx);
  55. pict.setAnchor(new Rectangle(10,10,100,100));
  56. s.addShape(pict);
  57. EscherBSERecord bse1 = pict.getEscherBSERecord();
  58. assertEquals(1, bse1.getRef());
  59. pict2.setAnchor(new Rectangle(10,10,100,100));
  60. s2.addShape(pict2);
  61. EscherBSERecord bse2 = pict.getEscherBSERecord();
  62. assertSame(bse1, bse2);
  63. assertEquals(2, bse1.getRef());
  64. pict3.setAnchor(new Rectangle(10,10,100,100));
  65. s3.addShape(pict3);
  66. EscherBSERecord bse3 = pict.getEscherBSERecord();
  67. assertSame(bse2, bse3);
  68. assertEquals(3, bse1.getRef());
  69. }
  70. /**
  71. * Picture#getEscherBSERecord threw NullPointerException if EscherContainerRecord.BSTORE_CONTAINER
  72. * was not found. The correct behaviour is to return null.
  73. */
  74. @Test
  75. public void bug46122() {
  76. HSLFSlideShow ppt = new HSLFSlideShow();
  77. HSLFSlide slide = ppt.createSlide();
  78. HSLFPictureShape pict = new HSLFPictureShape(-1); //index to non-existing picture data
  79. pict.setSheet(slide);
  80. HSLFPictureData data = pict.getPictureData();
  81. assertNull(data);
  82. BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
  83. Graphics2D graphics = img.createGraphics();
  84. pict.draw(graphics);
  85. }
  86. @Test
  87. public void macImages() throws Exception {
  88. HSLFSlideShowImpl hss = new HSLFSlideShowImpl(_slTests.openResourceAsStream("53446.ppt"));
  89. HSLFPictureData[] pictures = hss.getPictures();
  90. assertEquals(15, pictures.length);
  91. int[][] expectedSizes = {
  92. null, // WMF
  93. { 427, 428 }, // PNG
  94. { 371, 370 }, // PNG
  95. { 288, 183 }, // PNG
  96. { 285, 97 }, // PNG
  97. { 288, 168 }, // PNG
  98. null, // WMF
  99. null, // WMF
  100. { 199, 259 }, // PNG
  101. { 432, 244 }, // PNG
  102. { 261, 258 }, // PNG
  103. null, // WMF
  104. null, // WMF
  105. null, // WMF
  106. null // EMF
  107. };
  108. for (int i = 0; i < pictures.length; i++) {
  109. BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictures[i].getData()));
  110. if (pictures[i].getType() != HSLFPictureShape.WMF && pictures[i].getType() != HSLFPictureShape.EMF) {
  111. assertNotNull(image);
  112. int[] dimensions = expectedSizes[i];
  113. assertEquals(dimensions[0], image.getWidth());
  114. assertEquals(dimensions[1], image.getHeight());
  115. }
  116. }
  117. }
  118. @Test
  119. // @Ignore("Just for visual validation - antialiasing is different on various systems")
  120. public void bug54541() throws Exception {
  121. String file = new String[]{
  122. "54542_cropped_bitmap.pptx",
  123. "54541_cropped_bitmap.ppt",
  124. "54541_cropped_bitmap2.ppt",
  125. "sample_pptx_grouping_issues.pptx"
  126. }[3];
  127. InputStream is = _slTests.openResourceAsStream(file);
  128. SlideShow ss = file.endsWith("pptx") ? new XMLSlideShow(is) : new HSLFSlideShow(is);
  129. is.close();
  130. boolean debugOut = false;
  131. Dimension pg = ss.getPageSize();
  132. int i=1;
  133. for(Slide<?,?,?> slide : ss.getSlides()) {
  134. if (debugOut) {
  135. DummyGraphics2d graphics = new DummyGraphics2d();
  136. slide.draw(graphics);
  137. } else {
  138. BufferedImage img = new BufferedImage(pg.width, pg.height, BufferedImage.TYPE_INT_RGB);
  139. Graphics2D graphics = img.createGraphics();
  140. fixFonts(graphics);
  141. slide.draw(graphics);
  142. ImageIO.write(img, "PNG", new File("test"+(i++)+"hslf.png"));
  143. }
  144. }
  145. }
  146. @SuppressWarnings("unchecked")
  147. private void fixFonts(Graphics2D graphics) {
  148. if (!JvmBugs.hasLineBreakMeasurerBug()) return;
  149. Map<String,String> fontMap = (Map<String,String>)graphics.getRenderingHint(Drawable.FONT_MAP);
  150. if (fontMap == null) fontMap = new HashMap<String,String>();
  151. fontMap.put("Calibri", "Lucida Sans");
  152. fontMap.put("Cambria", "Lucida Bright");
  153. graphics.setRenderingHint(Drawable.FONT_MAP, fontMap);
  154. }
  155. }