]> source.dussan.org Git - poi.git/commitdiff
applied fix suggested in bug#46197: missing cast to float resulted in incorect calcul...
authorYegor Kozlov <yegor@apache.org>
Sun, 16 Nov 2008 13:24:42 +0000 (13:24 +0000)
committerYegor Kozlov <yegor@apache.org>
Sun, 16 Nov 2008 13:24:42 +0000 (13:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@718023 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPictures.java
src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Picture.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java

index 679a77cdac3edcd7217ecf176d8bd343a213531d..64d9b49811c8c630862eaab603e7d274bc58495b 100755 (executable)
@@ -17,6 +17,9 @@
 package org.apache.poi.xssf.usermodel.examples;\r
 \r
 import org.apache.poi.xssf.usermodel.*;\r
+import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.util.IOUtils;\r
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
 \r
 import java.io.IOException;\r
 import java.io.InputStream;\r
@@ -32,27 +35,34 @@ public class WorkingWithPictures {
     public static void main(String[] args) throws IOException {\r
 \r
         //create a new workbook\r
-        XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
+        Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
+        CreationHelper helper = wb.getCreationHelper();\r
 \r
         //add a picture in this workbook.\r
-        InputStream is = new FileInputStream("lilies.jpg");\r
-        int pictureIdx = wb.addPicture(is, XSSFWorkbook.PICTURE_TYPE_JPEG);\r
+        InputStream is = new FileInputStream(args[0]);\r
+        byte[] bytes = IOUtils.toByteArray(is);\r
         is.close();\r
+        int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);\r
 \r
         //create sheet\r
-        XSSFSheet sheet = wb.createSheet();\r
+        Sheet sheet = wb.createSheet();\r
 \r
         //create drawing\r
-        XSSFDrawing drawing = sheet.createDrawingPatriarch();\r
+        Drawing drawing = sheet.createDrawingPatriarch();\r
 \r
         //add a picture shape\r
-        XSSFPicture pict = drawing.createPicture(new XSSFClientAnchor(), pictureIdx);\r
+        ClientAnchor anchor = helper.createClientAnchor();\r
+        anchor.setCol1(1);\r
+        anchor.setRow1(1);\r
+        Picture pict = drawing.createPicture(anchor, pictureIdx);\r
 \r
         //auto-size picture\r
-        pict.resize();\r
+        pict.resize(2);\r
 \r
         //save workbook\r
-        FileOutputStream fileOut = new FileOutputStream("xssf-picture.xlsx");\r
+        String file = "picture.xls";\r
+        if(wb instanceof XSSFWorkbook) file += "x";\r
+        FileOutputStream fileOut = new FileOutputStream(file);\r
         wb.write(fileOut);\r
         fileOut.close();\r
 \r
index 4b7ac45f1495a2681039475c71288a803e10fbbb..66d32b129e93e89f1927963de58927268eb7c13e 100644 (file)
@@ -87,15 +87,17 @@ public class HSSFPicture
     }
 
     /**
-     * Reset the image to the original size.
+     * Resize the image
      *
-     * @since POI 3.0.2
+     * @param scale the amount by which image dimensions are multiplied relative to the original size.
+     * <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,
+     * <code>resize(2.0)</code> resizes to 200% of the original.
      */
-    public void resize(){
+    public void resize(double scale){
         HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
         anchor.setAnchorType(2);
 
-        HSSFClientAnchor pref = getPreferredSize();
+        HSSFClientAnchor pref = getPreferredSize(scale);
 
         int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
         int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
@@ -109,6 +111,13 @@ public class HSSFPicture
         anchor.setDy2(pref.getDy2());
     }
 
+    /**
+     * Reset the image to the original size.
+     */
+    public void resize(){
+        resize(1.0);
+    }
+
     /**
      * Calculate the preferred size for this picture.
      *
@@ -116,43 +125,56 @@ public class HSSFPicture
      * @since POI 3.0.2
      */
     public HSSFClientAnchor getPreferredSize(){
+        return getPreferredSize(1.0);
+    }
+
+    /**
+     * Calculate the preferred size for this picture.
+     *
+     * @param scale the amount by which image dimensions are multiplied relative to the original size.
+     * @return HSSFClientAnchor with the preferred size for this image
+     * @since POI 3.0.2
+     */
+    public HSSFClientAnchor getPreferredSize(double scale){
         HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
 
         Dimension size = getImageDimension();
+        double scaledWidth = size.getWidth() * scale;
+        double scaledHeight = size.getHeight() * scale;
 
         float w = 0;
 
         //space in the leftmost cell
-        w += getColumnWidthInPixels(anchor.col1)*(1 - anchor.dx1/1024);
+        w += getColumnWidthInPixels(anchor.col1)*(1 - (float)anchor.dx1/1024);
         short col2 = (short)(anchor.col1 + 1);
         int dx2 = 0;
 
-        while(w < size.width){
+        while(w < scaledWidth){
             w += getColumnWidthInPixels(col2++);
         }
 
-        if(w > size.width) {
+        if(w > scaledWidth) {
             //calculate dx2, offset in the rightmost cell
             col2--;
-            float cw = getColumnWidthInPixels(col2);
-            float delta = w - size.width;
+            double cw = getColumnWidthInPixels(col2);
+            double delta = w - scaledWidth;
             dx2 = (int)((cw-delta)/cw*1024);
         }
         anchor.col2 = col2;
         anchor.dx2 = dx2;
 
         float h = 0;
-        h += (1 - anchor.dy1/256)* getRowHeightInPixels(anchor.row1);
+        h += (1 - (float)anchor.dy1/256)* getRowHeightInPixels(anchor.row1);
         int row2 = anchor.row1 + 1;
         int dy2 = 0;
 
-        while(h < size.height){
+        while(h < scaledHeight){
             h += getRowHeightInPixels(row2++);
         }
-        if(h > size.height) {
+        if(h > scaledHeight) {
             row2--;
-            float ch = getRowHeightInPixels(row2);
-            float delta = h - size.height;
+            double ch = getRowHeightInPixels(row2);
+            double delta = h - scaledHeight;
             dy2 = (int)((ch-delta)/ch*256);
         }
         anchor.row2 = row2;
index 3e2fab6ac42362852bd4d23b9684bb55e6760617..eb5bc8828ed55e3b8ccbbf5fb89ff77975ccc863 100755 (executable)
@@ -27,4 +27,13 @@ public interface Picture {
      * Reset the image to the original size.\r
      */\r
     void resize();\r
+\r
+    /**\r
+     * Reset the image to the original size.\r
+     *\r
+     * @param scale the amount by which image dimensions are multiplied relative to the original size.\r
+     * <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,\r
+     * <code>resize(2.0)</code> resizes to 200% of the original.\r
+     */\r
+    void resize(double scale);\r
 }\r
index f786ca3ef489cdb252115aaf6b3b76e0a64190af..8e12e8606d3a409563714abe07fef451da837205 100755 (executable)
@@ -126,9 +126,20 @@ public class XSSFPicture extends XSSFShape implements Picture {
      * Reset the image to the original size.\r
      */\r
     public void resize(){\r
+        resize(1.0);\r
+    }\r
+\r
+    /**\r
+     * Reset the image to the original size.\r
+     *\r
+     * @param scale the amount by which image dimensions are multiplied relative to the original size.\r
+     * <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,\r
+     * <code>resize(2.0)</code> resizes to 200% of the original.\r
+     */\r
+    public void resize(double scale){\r
         XSSFClientAnchor anchor = (XSSFClientAnchor)getAnchor();\r
 \r
-        XSSFClientAnchor pref = getPreferredSize();\r
+        XSSFClientAnchor pref = getPreferredSize(scale);\r
 \r
         int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());\r
         int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());\r
@@ -148,10 +159,22 @@ public class XSSFPicture extends XSSFShape implements Picture {
      * @return XSSFClientAnchor with the preferred size for this image\r
      */\r
     public XSSFClientAnchor getPreferredSize(){\r
+        return getPreferredSize(1);\r
+    }\r
+\r
+    /**\r
+     * Calculate the preferred size for this picture.\r
+     *\r
+     * @param scale the amount by which image dimensions are multiplied relative to the original size.\r
+     * @return XSSFClientAnchor with the preferred size for this image\r
+     */\r
+    public XSSFClientAnchor getPreferredSize(double scale){\r
         XSSFClientAnchor anchor = (XSSFClientAnchor)getAnchor();\r
 \r
         XSSFPictureData data = getPictureData();\r
         Dimension size = getImageDimension(data.getPackagePart(), data.getPictureType());\r
+        double scaledWidth = size.getWidth() * scale;\r
+        double scaledHeight = size.getHeight() * scale;\r
 \r
         float w = 0;\r
         int col2 = anchor.getCol1();\r
@@ -163,19 +186,19 @@ public class XSSFPicture extends XSSFShape implements Picture {
 \r
         for (;;) {\r
             w += getColumnWidthInPixels(col2);\r
-            if(w > size.width) break;\r
+            if(w > scaledWidth) break;\r
             col2++;\r
         }\r
 \r
-        if(w > size.width) {\r
-            float cw = getColumnWidthInPixels(col2 + 1);\r
-            float delta = w - size.width;\r
+        if(w > scaledWidth) {\r
+            double cw = getColumnWidthInPixels(col2 + 1);\r
+            double delta = w - scaledWidth;\r
             dx2 = (int)(EMU_PER_PIXEL*(cw-delta));\r
         }\r
         anchor.setCol2(col2);\r
         anchor.setDx2(dx2);\r
 \r
-        float h = 0;\r
+        double h = 0;\r
         int row2 = anchor.getRow1();\r
         int dy2 = 0;\r
 \r
@@ -186,21 +209,21 @@ public class XSSFPicture extends XSSFShape implements Picture {
 \r
         for (;;) {\r
             h += getRowHeightInPixels(row2);\r
-            if(h > size.height) break;\r
+            if(h > scaledHeight) break;\r
             row2++;\r
         }\r
 \r
-        if(h > size.height) {\r
-            float ch = getRowHeightInPixels(row2 + 1);\r
-            float delta = h - size.height;\r
+        if(h > scaledHeight) {\r
+            double ch = getRowHeightInPixels(row2 + 1);\r
+            double delta = h - scaledHeight;\r
             dy2 = (int)(EMU_PER_PIXEL*(ch-delta));\r
         }\r
         anchor.setRow2(row2);\r
         anchor.setDy2(dy2);\r
 \r
         CTPositiveSize2D size2d =  ctPicture.getSpPr().getXfrm().getExt();\r
-        size2d.setCx(size.width*EMU_PER_PIXEL);\r
-        size2d.setCy(size.height*EMU_PER_PIXEL);\r
+        size2d.setCx((long)(scaledWidth*EMU_PER_PIXEL));\r
+        size2d.setCy((long)(scaledHeight*EMU_PER_PIXEL));\r
 \r
         return anchor;\r
     }\r