PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/ppt/scratchpad/src/org/apache/poi/hslf/model/Picture.java

https://github.com/minstrelsy/POI-Android
Java | 279 lines | 143 code | 36 blank | 100 comment | 22 complexity | c0426f09ab6f17114414ade32966e4ad MD5 | raw file
  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 net.pbdavey.awt.Graphics2D;
  17. import org.apache.poi.ddf.*;
  18. import org.apache.poi.hslf.usermodel.PictureData;
  19. import org.apache.poi.hslf.usermodel.SlideShow;
  20. import org.apache.poi.hslf.record.Document;
  21. import org.apache.poi.hslf.blip.Bitmap;
  22. import org.apache.poi.hslf.exceptions.HSLFException;
  23. import org.apache.poi.util.POILogger;
  24. import and.awt.*;
  25. import and.awt.geom.AffineTransform;
  26. import android.util.Log;
  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 referes 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. Log.d("Picture", "setDefaultSize");
  125. PictureData pict = getPictureData();
  126. if (pict instanceof Bitmap){
  127. BufferedImage img = null;
  128. try {
  129. img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  130. }
  131. catch (NegativeArraySizeException ne) {}
  132. if(img != null) {
  133. // Valid image, set anchor from it
  134. setAnchor(new and.awt.Rectangle(0, 0, img.getWidth()*POINT_DPI/PIXEL_DPI, img.getHeight()*POINT_DPI/PIXEL_DPI));
  135. img.bm.recycle();
  136. } else {
  137. // Invalid image, go with the default metafile size
  138. setAnchor(new and.awt.Rectangle(0, 0, 200, 200));
  139. }
  140. } else {
  141. //default size of a metafile picture is 200x200
  142. setAnchor(new and.awt.Rectangle(50, 50, 200, 200));
  143. }
  144. }
  145. /**
  146. * Returns the picture data for this picture.
  147. *
  148. * @return the picture data for this picture.
  149. */
  150. public PictureData getPictureData(){
  151. SlideShow ppt = getSheet().getSlideShow();
  152. PictureData[] pict = ppt.getPictureData();
  153. EscherBSERecord bse = getEscherBSERecord();
  154. if (bse == null){
  155. logger.log(POILogger.ERROR, "no reference to picture data found ");
  156. } else {
  157. for ( int i = 0; i < pict.length; i++ ) {
  158. if (pict[i].getOffset() == bse.getOffset()){
  159. return pict[i];
  160. }
  161. }
  162. logger.log(POILogger.ERROR, "no picture found for our BSE offset " + bse.getOffset());
  163. }
  164. return null;
  165. }
  166. protected EscherBSERecord getEscherBSERecord(){
  167. SlideShow ppt = getSheet().getSlideShow();
  168. Document doc = ppt.getDocumentRecord();
  169. EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
  170. EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
  171. if(bstore == null) {
  172. logger.log(POILogger.DEBUG, "EscherContainerRecord.BSTORE_CONTAINER was not found ");
  173. return null;
  174. }
  175. List lst = bstore.getChildRecords();
  176. int idx = getPictureIndex();
  177. if (idx == 0){
  178. logger.log(POILogger.DEBUG, "picture index was not found, returning ");
  179. return null;
  180. }
  181. return (EscherBSERecord)lst.get(idx-1);
  182. }
  183. /**
  184. * Name of this picture.
  185. *
  186. * @return name of this picture
  187. */
  188. public String getPictureName(){
  189. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  190. EscherComplexProperty prop = (EscherComplexProperty)getEscherProperty(opt, EscherProperties.BLIP__BLIPFILENAME);
  191. String name = null;
  192. if(prop != null){
  193. try {
  194. name = new String(prop.getComplexData(), "UTF-16LE");
  195. int idx = name.indexOf('\u0000');
  196. return idx == -1 ? name : name.substring(0, idx);
  197. } catch (UnsupportedEncodingException e){
  198. throw new HSLFException(e);
  199. }
  200. }
  201. return name;
  202. }
  203. /**
  204. * Name of this picture.
  205. *
  206. * @param name of this picture
  207. */
  208. public void setPictureName(String name){
  209. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  210. try {
  211. byte[] data = (name + '\u0000').getBytes("UTF-16LE");
  212. EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, false, data);
  213. opt.addEscherProperty(prop);
  214. } catch (UnsupportedEncodingException e){
  215. throw new HSLFException(e);
  216. }
  217. }
  218. /**
  219. * By default set the orininal image size
  220. */
  221. protected void afterInsert(Sheet sh){
  222. super.afterInsert(sh);
  223. EscherBSERecord bse = getEscherBSERecord();
  224. bse.setRef(bse.getRef() + 1);
  225. and.awt.Rectangle anchor = getAnchor();
  226. if (anchor.equals(new and.awt.Rectangle())){
  227. setDefaultSize();
  228. }
  229. }
  230. public void draw(Graphics2D graphics){
  231. // AffineTransform at = graphics.getTransform();
  232. graphics.canvas.save();
  233. ShapePainter.paint(this, graphics);
  234. PictureData data = getPictureData();
  235. Log.d("Picture", "draw: getType: " + data.getType());
  236. if(data != null) data.draw(graphics, this);
  237. // graphics.setTransform(at);
  238. graphics.canvas.restore();
  239. }
  240. }