<changes>
<release version="3.6-beta1" date="2009-??-??">
+ <action dev="POI-DEVELOPERS" type="fix">48134 - release system resources when using Picture.resize()</action>
<action dev="POI-DEVELOPERS" type="fix">48087 - avoid NPE in XSSFChartSheet when calling methods of the superclass</action>
<action dev="POI-DEVELOPERS" type="fix">48038 - handle reading HWPF stylesheets from non zero offsets</action>
<action dev="POI-DEVELOPERS" type="add">When running the "compile-ooxml-xsds" ant task, also generate the source jar for the OOXML Schemas</action>
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.ss.usermodel.Picture;
+import org.apache.poi.ss.util.ImageUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
return cw == def ? PX_DEFAULT : PX_MODIFIED;
}
- /**
- * The metadata of PNG and JPEG can contain the width of a pixel in millimeters.
- * Return the the "effective" dpi calculated as <code>25.4/HorizontalPixelSize</code>
- * and <code>25.4/VerticalPixelSize</code>. Where 25.4 is the number of mm in inch.
- *
- * @return array of two elements: <code>{horisontalPdi, verticalDpi}</code>.
- * {96, 96} is the default.
- */
- protected int[] getResolution(ImageReader r) throws IOException {
- int hdpi=96, vdpi=96;
- double mm2inch = 25.4;
-
- NodeList lst;
- Element node = (Element)r.getImageMetadata(0).getAsTree("javax_imageio_1.0");
- lst = node.getElementsByTagName("HorizontalPixelSize");
- if(lst != null && lst.getLength() == 1) hdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));
-
- lst = node.getElementsByTagName("VerticalPixelSize");
- if(lst != null && lst.getLength() == 1) vdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));
-
- return new int[]{hdpi, vdpi};
- }
-
/**
* Return the dimension of this image
*
EscherBSERecord bse = _patriarch._sheet._book.getBSERecord(_pictureIndex);
byte[] data = bse.getBlipRecord().getPicturedata();
int type = bse.getBlipTypeWin32();
- Dimension size = new Dimension();
-
- switch (type){
- //we can calculate the preferred size only for JPEG and PNG
- //other formats like WMF, EMF and PICT are not supported in Java
- case HSSFWorkbook.PICTURE_TYPE_JPEG:
- case HSSFWorkbook.PICTURE_TYPE_PNG:
- case HSSFWorkbook.PICTURE_TYPE_DIB:
- try {
- //read the image using javax.imageio.*
- ImageInputStream iis = ImageIO.createImageInputStream( new ByteArrayInputStream(data) );
- Iterator<ImageReader> i = ImageIO.getImageReaders( iis );
- ImageReader r = i.next();
- r.setInput( iis );
- BufferedImage img = r.read(0);
-
- int[] dpi = getResolution(r);
-
- //if DPI is zero then assume standard 96 DPI
- //since cannot divide by zero
- if (dpi[0] == 0) dpi[0] = 96;
- if (dpi[1] == 0) dpi[1] = 96;
-
- size.width = img.getWidth()*96/dpi[0];
- size.height = img.getHeight()*96/dpi[1];
-
- } catch (IOException e){
- //silently return if ImageIO failed to read the image
- log.log(POILogger.WARN, e);
- }
-
- break;
- }
- return size;
+ return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
}
}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.ss.util;\r
+\r
+import org.w3c.dom.NodeList;\r
+import org.w3c.dom.Element;\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.util.POILogger;\r
+import org.apache.poi.util.POILogFactory;\r
+\r
+import javax.imageio.ImageReader;\r
+import javax.imageio.ImageIO;\r
+import javax.imageio.stream.ImageInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.awt.*;\r
+import java.awt.image.BufferedImage;\r
+import java.util.Iterator;\r
+\r
+/**\r
+ * @author Yegor Kozlov\r
+ */\r
+public class ImageUtils {\r
+ private static final POILogger logger = POILogFactory.getLogger(ImageUtils.class);\r
+\r
+ public static final int PIXEL_DPI = 96;\r
+\r
+ /**\r
+ * Return the dimension of this image\r
+ *\r
+ * @param is the stream containing the image data\r
+ * @param type type of the picture: {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_JPEG},\r
+ * {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_PNG} or {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_DIB}\r
+ *\r
+ * @return image dimension in pixels\r
+ */\r
+ public static Dimension getImageDimension(InputStream is, int type){\r
+ Dimension size = new Dimension();\r
+\r
+ switch (type){\r
+ //we can calculate the preferred size only for JPEG, PNG and BMP\r
+ //other formats like WMF, EMF and PICT are not supported in Java\r
+ case Workbook.PICTURE_TYPE_JPEG:\r
+ case Workbook.PICTURE_TYPE_PNG:\r
+ case Workbook.PICTURE_TYPE_DIB:\r
+ try {\r
+ //read the image using javax.imageio.*\r
+ ImageInputStream iis = ImageIO.createImageInputStream( is );\r
+ Iterator i = ImageIO.getImageReaders( iis );\r
+ ImageReader r = (ImageReader) i.next();\r
+ r.setInput( iis );\r
+ BufferedImage img = r.read(0);\r
+\r
+ int[] dpi = getResolution(r);\r
+\r
+ //if DPI is zero then assume standard 96 DPI\r
+ //since cannot divide by zero\r
+ if (dpi[0] == 0) dpi[0] = PIXEL_DPI;\r
+ if (dpi[1] == 0) dpi[1] = PIXEL_DPI;\r
+\r
+ size.width = img.getWidth()*PIXEL_DPI/dpi[0];\r
+ size.height = img.getHeight()*PIXEL_DPI/dpi[1];\r
+\r
+ r.dispose();\r
+ iis.close();\r
+\r
+ } catch (IOException e){\r
+ //silently return if ImageIO failed to read the image\r
+ logger.log(POILogger.WARN, e);\r
+ }\r
+\r
+ break;\r
+ default:\r
+ logger.log(POILogger.WARN, "Only JPEG, PNG and DIB pictures can be automatically sized");\r
+ }\r
+ return size;\r
+ }\r
+\r
+ /**\r
+ * The metadata of PNG and JPEG can contain the width of a pixel in millimeters.\r
+ * Return the the "effective" dpi calculated as <code>25.4/HorizontalPixelSize</code>\r
+ * and <code>25.4/VerticalPixelSize</code>. Where 25.4 is the number of mm in inch.\r
+ *\r
+ * @return array of two elements: <code>{horisontalPdi, verticalDpi}</code>.\r
+ * {96, 96} is the default.\r
+ */\r
+ public static int[] getResolution(ImageReader r) throws IOException {\r
+ int hdpi=96, vdpi=96;\r
+ double mm2inch = 25.4;\r
+\r
+ NodeList lst;\r
+ Element node = (Element)r.getImageMetadata(0).getAsTree("javax_imageio_1.0");\r
+ lst = node.getElementsByTagName("HorizontalPixelSize");\r
+ if(lst != null && lst.getLength() == 1) hdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));\r
+\r
+ lst = node.getElementsByTagName("VerticalPixelSize");\r
+ if(lst != null && lst.getLength() == 1) vdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));\r
+\r
+ return new int[]{hdpi, vdpi};\r
+ }\r
+\r
+}\r
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.ImageUtils;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
* @return image dimension in pixels
*/
protected static Dimension getImageDimension(PackagePart part, int type){
- Dimension size = new Dimension();
-
- switch (type){
- //we can calculate the preferred size only for JPEG, PNG and BMP
- //other formats like WMF, EMF and PICT are not supported in Java
- case Workbook.PICTURE_TYPE_JPEG:
- case Workbook.PICTURE_TYPE_PNG:
- case Workbook.PICTURE_TYPE_DIB:
- try {
- //read the image using javax.imageio.*
- ImageInputStream iis = ImageIO.createImageInputStream( part.getInputStream() );
- Iterator i = ImageIO.getImageReaders( iis );
- ImageReader r = (ImageReader) i.next();
- r.setInput( iis );
- BufferedImage img = r.read(0);
-
- int[] dpi = getResolution(r);
-
- //if DPI is zero then assume standard 96 DPI
- //since cannot divide by zero
- if (dpi[0] == 0) dpi[0] = PIXEL_DPI;
- if (dpi[1] == 0) dpi[1] = PIXEL_DPI;
-
- size.width = img.getWidth()*PIXEL_DPI/dpi[0];
- size.height = img.getHeight()*PIXEL_DPI/dpi[1];
-
- } catch (IOException e){
- //silently return if ImageIO failed to read the image
- logger.log(POILogger.WARN, e);
- }
-
- break;
- default:
- logger.log(POILogger.WARN, "Only JPEG, PNG and DIB pictures can be automatically sized");
+ try {
+ return ImageUtils.getImageDimension(part.getInputStream(), type);
+ } catch (IOException e){
+ //return a "singulariry" if ImageIO failed to read the image
+ logger.log(POILogger.WARN, e);
+ return new Dimension();
}
- return size;
- }
-
- /**
- * The metadata of PNG and JPEG can contain the width of a pixel in millimeters.
- * Return the the "effective" dpi calculated as <code>25.4/HorizontalPixelSize</code>
- * and <code>25.4/VerticalPixelSize</code>. Where 25.4 is the number of mm in inch.
- *
- * @return array of two elements: <code>{horisontalPdi, verticalDpi}</code>.
- * {96, 96} is the default.
- */
- protected static int[] getResolution(ImageReader r) throws IOException {
- int hdpi = PIXEL_DPI, vdpi = PIXEL_DPI;
- double mm2inch = 25.4;
-
- NodeList lst;
- Element node = (Element)r.getImageMetadata(0).getAsTree("javax_imageio_1.0");
- lst = node.getElementsByTagName("HorizontalPixelSize");
- if(lst != null && lst.getLength() == 1) hdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));
-
- lst = node.getElementsByTagName("VerticalPixelSize");
- if(lst != null && lst.getLength() == 1) vdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value")));
-
- return new int[]{hdpi, vdpi};
}
/**