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

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