PageRenderTime 70ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/usermodel/HSSFPicture.java

http://github.com/openmicroscopy/bioformats
Java | 201 lines | 100 code | 28 blank | 73 comment | 9 complexity | 54b349b51e41e2778e6c5dd6bb3b994a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one or more
  25. * contributor license agreements. See the NOTICE file distributed with
  26. * this work for additional information regarding copyright ownership.
  27. * The ASF licenses this file to You under the Apache License, Version 2.0
  28. * (the "License"); you may not use this file except in compliance with
  29. * the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing, software
  34. * distributed under the License is distributed on an "AS IS" BASIS,
  35. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  36. * See the License for the specific language governing permissions and
  37. * limitations under the License.
  38. */
  39. package loci.poi.hssf.usermodel;
  40. import loci.poi.ddf.EscherBSERecord;
  41. import loci.poi.util.POILogFactory;
  42. import loci.poi.util.POILogger;
  43. import org.w3c.dom.Element;
  44. import org.w3c.dom.NodeList;
  45. import javax.imageio.ImageIO;
  46. import javax.imageio.ImageReader;
  47. import javax.imageio.stream.ImageInputStream;
  48. import java.awt.image.BufferedImage;
  49. import java.io.ByteArrayInputStream;
  50. import java.io.IOException;
  51. import java.util.Iterator;
  52. /**
  53. * Represents a escher picture. Eg. A GIF, JPEG etc...
  54. *
  55. * @author Glen Stampoultzis
  56. * @author Yegor Kozlov (yegor at apache.org)
  57. */
  58. public class HSSFPicture
  59. extends HSSFSimpleShape
  60. {
  61. public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF; // Windows Enhanced Metafile
  62. public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF; // Windows Metafile
  63. public static final int PICTURE_TYPE_PICT = HSSFWorkbook.PICTURE_TYPE_PICT; // Macintosh PICT
  64. public static final int PICTURE_TYPE_JPEG = HSSFWorkbook.PICTURE_TYPE_JPEG; // JFIF
  65. public static final int PICTURE_TYPE_PNG = HSSFWorkbook.PICTURE_TYPE_PNG; // PNG
  66. public static final int PICTURE_TYPE_DIB = HSSFWorkbook.PICTURE_TYPE_DIB; // Windows DIB
  67. int pictureIndex;
  68. HSSFPatriarch patriarch;
  69. private static final POILogger log = POILogFactory.getLogger(HSSFPicture.class);
  70. /**
  71. * Constructs a picture object.
  72. */
  73. HSSFPicture( HSSFShape parent, HSSFAnchor anchor )
  74. {
  75. super( parent, anchor );
  76. setShapeType(OBJECT_TYPE_PICTURE);
  77. }
  78. public int getPictureIndex()
  79. {
  80. return pictureIndex;
  81. }
  82. public void setPictureIndex( int pictureIndex )
  83. {
  84. this.pictureIndex = pictureIndex;
  85. }
  86. /**
  87. * Reset the image to the original size.
  88. *
  89. * @since POI 3.0.2
  90. */
  91. public void resize(){
  92. HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
  93. anchor.setAnchorType(2);
  94. HSSFClientAnchor pref = getPreferredSize();
  95. int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
  96. int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
  97. anchor.setCol2((short)col2);
  98. anchor.setDx1(0);
  99. anchor.setDx2(pref.getDx2());
  100. anchor.setRow2(row2);
  101. anchor.setDy1(0);
  102. anchor.setDy2(pref.getDy2());
  103. }
  104. /**
  105. * Calculate the preferred size for this picture.
  106. *
  107. * @return HSSFClientAnchor with the preferred size for this image
  108. * @since POI 3.0.2
  109. */
  110. public HSSFClientAnchor getPreferredSize(){
  111. HSSFClientAnchor anchor = new HSSFClientAnchor();
  112. EscherBSERecord bse = (EscherBSERecord)patriarch.sheet.book.getBSERecord(pictureIndex);
  113. byte[] data = bse.getBlipRecord().getPicturedata();
  114. int type = bse.getBlipTypeWin32();
  115. switch (type){
  116. //we can calculate the preferred size only for JPEG and PNG
  117. //other formats like WMF, EMF and PICT are not supported in Java
  118. case HSSFWorkbook.PICTURE_TYPE_JPEG:
  119. case HSSFWorkbook.PICTURE_TYPE_PNG:
  120. BufferedImage img = null;
  121. ImageReader r = null;
  122. try {
  123. //read the image using javax.imageio.*
  124. ImageInputStream iis = ImageIO.createImageInputStream( new ByteArrayInputStream(data) );
  125. Iterator i = ImageIO.getImageReaders( iis );
  126. r = (ImageReader) i.next();
  127. r.setInput( iis );
  128. img = r.read(0);
  129. int[] dpi = getResolution(r);
  130. int imgWidth = img.getWidth()*96/dpi[0];
  131. int imgHeight = img.getHeight()*96/dpi[1];
  132. //Excel measures cells in units of 1/256th of a character width.
  133. //The cell width calculated based on this info is always "off".
  134. //A better approach seems to be to use empirically obtained cell width and row height
  135. int cellwidth = 64;
  136. int rowheight = 17;
  137. int col2 = imgWidth/cellwidth;
  138. int row2 = imgHeight/rowheight;
  139. int dx2 = (int)((float)(imgWidth % cellwidth)/cellwidth * 1024);
  140. int dy2 = (int)((float)(imgHeight % rowheight)/rowheight * 256);
  141. anchor.setCol2((short)col2);
  142. anchor.setDx2(dx2);
  143. anchor.setRow2(row2);
  144. anchor.setDy2(dy2);
  145. } catch (IOException e){
  146. //silently return if ImageIO failed to read the image
  147. log.log(POILogger.WARN, e);
  148. img = null;
  149. }
  150. break;
  151. }
  152. return anchor;
  153. }
  154. /**
  155. * The metadata of PNG and JPEG can contain the width of a pixel in millimeters.
  156. * Return the the "effective" dpi calculated as <code>25.4/HorizontalPixelSize</code>
  157. * and <code>25.4/VerticalPixelSize</code>. Where 25.4 is the number of mm in inch.
  158. *
  159. * @return array of two elements: <code>{horisontalPdi, verticalDpi}</code>.
  160. * {96, 96} is the default.
  161. */
  162. protected int[] getResolution(ImageReader r) throws IOException {
  163. int hdpi=96, vdpi=96;
  164. double mm2inch = 25.4;
  165. NodeList lst;
  166. Element node = (Element)r.getImageMetadata(0).getAsTree("javax_imageio_1.0");
  167. lst = node.getElementsByTagName("HorizontalPixelSize");
  168. if(lst != null && lst.getLength() == 1) hdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));
  169. lst = node.getElementsByTagName("VerticalPixelSize");
  170. if(lst != null && lst.getLength() == 1) vdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));
  171. return new int[]{hdpi, vdpi};
  172. }
  173. }