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.

Picture.java 10KB

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