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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 org.apache.poi.ddf.*;
  17. import org.apache.poi.hslf.usermodel.PictureData;
  18. import org.apache.poi.hslf.usermodel.SlideShow;
  19. import org.apache.poi.hslf.record.Document;
  20. import org.apache.poi.hslf.blip.Bitmap;
  21. import org.apache.poi.hslf.exceptions.HSLFException;
  22. import org.apache.poi.util.POILogger;
  23. import javax.imageio.ImageIO;
  24. import java.awt.image.BufferedImage;
  25. import java.awt.*;
  26. import java.awt.geom.AffineTransform;
  27. import java.io.ByteArrayInputStream;
  28. import java.io.IOException;
  29. import java.io.UnsupportedEncodingException;
  30. import java.util.List;
  31. /**
  32. * Represents a picture in a PowerPoint document.
  33. *
  34. * @author Yegor Kozlov
  35. */
  36. public class Picture extends SimpleShape {
  37. /**
  38. * Windows Enhanced Metafile (EMF)
  39. */
  40. public static final int EMF = 2;
  41. /**
  42. * Windows Metafile (WMF)
  43. */
  44. public static final int WMF = 3;
  45. /**
  46. * Macintosh PICT
  47. */
  48. public static final int PICT = 4;
  49. /**
  50. * JPEG
  51. */
  52. public static final int JPEG = 5;
  53. /**
  54. * PNG
  55. */
  56. public static final int PNG = 6;
  57. /**
  58. * Windows DIB (BMP)
  59. */
  60. public static final byte DIB = 7;
  61. /**
  62. * Create a new <code>Picture</code>
  63. *
  64. * @param idx the index of the picture
  65. */
  66. public Picture(int idx){
  67. this(idx, null);
  68. }
  69. /**
  70. * Create a new <code>Picture</code>
  71. *
  72. * @param idx the index of the picture
  73. * @param parent the parent shape
  74. */
  75. public Picture(int idx, Shape parent) {
  76. super(null, parent);
  77. _escherContainer = createSpContainer(idx, parent instanceof ShapeGroup);
  78. }
  79. /**
  80. * Create a <code>Picture</code> object
  81. *
  82. * @param escherRecord the <code>EscherSpContainer</code> record which holds information about
  83. * this picture in the <code>Slide</code>
  84. * @param parent the parent shape of this picture
  85. */
  86. protected Picture(EscherContainerRecord escherRecord, Shape parent){
  87. super(escherRecord, parent);
  88. }
  89. /**
  90. * Returns index associated with this picture.
  91. * Index starts with 1 and points to a EscherBSE record which
  92. * holds information about this picture.
  93. *
  94. * @return the index to this picture (1 based).
  95. */
  96. public int getPictureIndex(){
  97. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  98. EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.BLIP__BLIPTODISPLAY);
  99. return prop == null ? 0 : prop.getPropertyValue();
  100. }
  101. /**
  102. * Create a new Picture and populate the inital structure of the <code>EscherSp</code> record which holds information about this picture.
  103. * @param idx the index of the picture which refers to <code>EscherBSE</code> container.
  104. * @return the create Picture object
  105. */
  106. protected EscherContainerRecord createSpContainer(int idx, boolean isChild) {
  107. _escherContainer = super.createSpContainer(isChild);
  108. _escherContainer.setOptions((short)15);
  109. EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
  110. spRecord.setOptions((short)((ShapeTypes.PictureFrame << 4) | 0x2));
  111. //set default properties for a picture
  112. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  113. setEscherProperty(opt, EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x800080);
  114. //another weird feature of powerpoint: for picture id we must add 0x4000.
  115. setEscherProperty(opt, (short)(EscherProperties.BLIP__BLIPTODISPLAY + 0x4000), idx);
  116. return _escherContainer;
  117. }
  118. /**
  119. * Resize this picture to the default size.
  120. * For PNG and JPEG resizes the image to 100%,
  121. * for other types sets the default size of 200x200 pixels.
  122. */
  123. public void setDefaultSize(){
  124. PictureData pict = getPictureData();
  125. if (pict instanceof Bitmap){
  126. BufferedImage img = null;
  127. try {
  128. img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  129. }
  130. catch (IOException e){}
  131. catch (NegativeArraySizeException ne) {}
  132. if(img != null) {
  133. // Valid image, set anchor from it
  134. setAnchor(new java.awt.Rectangle(0, 0, img.getWidth()*POINT_DPI/PIXEL_DPI, img.getHeight()*POINT_DPI/PIXEL_DPI));
  135. } else {
  136. // Invalid image, go with the default metafile size
  137. setAnchor(new java.awt.Rectangle(0, 0, 200, 200));
  138. }
  139. } else {
  140. //default size of a metafile picture is 200x200
  141. setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
  142. }
  143. }
  144. /**
  145. * Returns the picture data for this picture.
  146. *
  147. * @return the picture data for this picture.
  148. */
  149. public PictureData getPictureData(){
  150. SlideShow ppt = getSheet().getSlideShow();
  151. PictureData[] pict = ppt.getPictureData();
  152. EscherBSERecord bse = getEscherBSERecord();
  153. if (bse == null){
  154. logger.log(POILogger.ERROR, "no reference to picture data found ");
  155. } else {
  156. for ( int i = 0; i < pict.length; i++ ) {
  157. if (pict[i].getOffset() == bse.getOffset()){
  158. return pict[i];
  159. }
  160. }
  161. logger.log(POILogger.ERROR, "no picture found for our BSE offset " + bse.getOffset());
  162. }
  163. return null;
  164. }
  165. protected EscherBSERecord getEscherBSERecord(){
  166. SlideShow ppt = getSheet().getSlideShow();
  167. Document doc = ppt.getDocumentRecord();
  168. EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
  169. EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
  170. if(bstore == null) {
  171. logger.log(POILogger.DEBUG, "EscherContainerRecord.BSTORE_CONTAINER was not found ");
  172. return null;
  173. }
  174. List lst = bstore.getChildRecords();
  175. int idx = getPictureIndex();
  176. if (idx == 0){
  177. logger.log(POILogger.DEBUG, "picture index was not found, returning ");
  178. return null;
  179. }
  180. return (EscherBSERecord)lst.get(idx-1);
  181. }
  182. /**
  183. * Name of this picture.
  184. *
  185. * @return name of this picture
  186. */
  187. public String getPictureName(){
  188. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  189. EscherComplexProperty prop = (EscherComplexProperty)getEscherProperty(opt, EscherProperties.BLIP__BLIPFILENAME);
  190. String name = null;
  191. if(prop != null){
  192. try {
  193. name = new String(prop.getComplexData(), "UTF-16LE");
  194. int idx = name.indexOf('\u0000');
  195. return idx == -1 ? name : name.substring(0, idx);
  196. } catch (UnsupportedEncodingException e){
  197. throw new HSLFException(e);
  198. }
  199. }
  200. return name;
  201. }
  202. /**
  203. * Name of this picture.
  204. *
  205. * @param name of this picture
  206. */
  207. public void setPictureName(String name){
  208. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  209. try {
  210. byte[] data = (name + '\u0000').getBytes("UTF-16LE");
  211. EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, false, data);
  212. opt.addEscherProperty(prop);
  213. } catch (UnsupportedEncodingException e){
  214. throw new HSLFException(e);
  215. }
  216. }
  217. /**
  218. * By default set the orininal image size
  219. */
  220. protected void afterInsert(Sheet sh){
  221. super.afterInsert(sh);
  222. EscherBSERecord bse = getEscherBSERecord();
  223. bse.setRef(bse.getRef() + 1);
  224. java.awt.Rectangle anchor = getAnchor();
  225. if (anchor.equals(new java.awt.Rectangle())){
  226. setDefaultSize();
  227. }
  228. }
  229. public void draw(Graphics2D graphics){
  230. AffineTransform at = graphics.getTransform();
  231. ShapePainter.paint(this, graphics);
  232. PictureData data = getPictureData();
  233. if(data != null) data.draw(graphics, this);
  234. graphics.setTransform(at);
  235. }
  236. }