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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 java.awt.Graphics2D;
  17. import java.awt.Rectangle;
  18. import java.awt.image.BufferedImage;
  19. import java.io.ByteArrayInputStream;
  20. import javax.imageio.ImageIO;
  21. import junit.framework.TestCase;
  22. import org.apache.poi.ddf.EscherBSERecord;
  23. import org.apache.poi.hslf.HSLFSlideShow;
  24. import org.apache.poi.hslf.usermodel.PictureData;
  25. import org.apache.poi.hslf.usermodel.SlideShow;
  26. import org.apache.poi.POIDataSamples;
  27. /**
  28. * Test Picture shape.
  29. *
  30. * @author Yegor Kozlov
  31. */
  32. public final class TestPicture extends TestCase {
  33. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  34. /**
  35. * Test that the reference count of a blip is incremented every time the picture is inserted.
  36. * This is important when the same image appears multiple times in a slide show.
  37. *
  38. */
  39. public void testMultiplePictures() throws Exception {
  40. SlideShow ppt = new SlideShow();
  41. Slide s = ppt.createSlide();
  42. Slide s2 = ppt.createSlide();
  43. Slide s3 = ppt.createSlide();
  44. int idx = ppt.addPicture(_slTests.readFile("clock.jpg"), Picture.JPEG);
  45. Picture pict = new Picture(idx);
  46. Picture pict2 = new Picture(idx);
  47. Picture pict3 = new Picture(idx);
  48. pict.setAnchor(new Rectangle(10,10,100,100));
  49. s.addShape(pict);
  50. EscherBSERecord bse1 = pict.getEscherBSERecord();
  51. assertEquals(1, bse1.getRef());
  52. pict2.setAnchor(new Rectangle(10,10,100,100));
  53. s2.addShape(pict2);
  54. EscherBSERecord bse2 = pict.getEscherBSERecord();
  55. assertSame(bse1, bse2);
  56. assertEquals(2, bse1.getRef());
  57. pict3.setAnchor(new Rectangle(10,10,100,100));
  58. s3.addShape(pict3);
  59. EscherBSERecord bse3 = pict.getEscherBSERecord();
  60. assertSame(bse2, bse3);
  61. assertEquals(3, bse1.getRef());
  62. }
  63. /**
  64. * Picture#getEscherBSERecord threw NullPointerException if EscherContainerRecord.BSTORE_CONTAINER
  65. * was not found. The correct behaviour is to return null.
  66. */
  67. public void test46122() {
  68. SlideShow ppt = new SlideShow();
  69. Slide slide = ppt.createSlide();
  70. Picture pict = new Picture(-1); //index to non-existing picture data
  71. pict.setSheet(slide);
  72. PictureData data = pict.getPictureData();
  73. assertNull(data);
  74. BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
  75. Graphics2D graphics = img.createGraphics();
  76. pict.draw(graphics);
  77. }
  78. public void testMacImages() throws Exception {
  79. HSLFSlideShow hss = new HSLFSlideShow(_slTests.openResourceAsStream("53446.ppt"));
  80. PictureData[] pictures = hss.getPictures();
  81. assertEquals(15, pictures.length);
  82. int[][] expectedSizes = {
  83. null, // WMF
  84. { 427, 428 }, // PNG
  85. { 371, 370 }, // PNG
  86. { 288, 183 }, // PNG
  87. { 285, 97 }, // PNG
  88. { 288, 168 }, // PNG
  89. null, // WMF
  90. null, // WMF
  91. { 199, 259 }, // PNG
  92. { 432, 244 }, // PNG
  93. { 261, 258 }, // PNG
  94. null, // WMF
  95. null, // WMF
  96. null, // WMF
  97. null // EMF
  98. };
  99. for (int i = 0; i < pictures.length; i++) {
  100. BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictures[i].getData()));
  101. if (pictures[i].getType() != Picture.WMF && pictures[i].getType() != Picture.EMF) {
  102. assertNotNull(image);
  103. int[] dimensions = expectedSizes[i];
  104. assertEquals(dimensions[0], image.getWidth());
  105. assertEquals(dimensions[1], image.getHeight());
  106. }
  107. }
  108. }
  109. }