This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new b7f72c8f32 add MathUtil (#1092)
b7f72c8f32 is described below

commit b7f72c8f32352336a954fc1645cac9ea3f8fa0a6
Author: PJ Fanning <[email protected]>
AuthorDate: Wed May 27 15:01:01 2026 +0100

    add MathUtil (#1092)
---
 .../java/org/apache/poi/sl/draw/DrawPaint.java     |  2 +-
 .../java/org/apache/poi/sl/draw/DrawSheet.java     |  5 ++-
 .../org/apache/poi/sl/draw/DrawTexturePaint.java   | 24 ++++++-----
 .../java/org/apache/poi/ss/util/ImageUtils.java    |  3 +-
 .../main/java/org/apache/poi/util/MathUtil.java    | 47 ++++++++++++++++++++++
 5 files changed, 68 insertions(+), 13 deletions(-)

diff --git a/poi/src/main/java/org/apache/poi/sl/draw/DrawPaint.java 
b/poi/src/main/java/org/apache/poi/sl/draw/DrawPaint.java
index ad2956ebee..613102787c 100644
--- a/poi/src/main/java/org/apache/poi/sl/draw/DrawPaint.java
+++ b/poi/src/main/java/org/apache/poi/sl/draw/DrawPaint.java
@@ -94,7 +94,7 @@ public class DrawPaint {
                         return new Color(color.getRed(), color.getGreen(), 
color.getBlue());
                     }
                     @Override
-                    public int getAlpha() { return 
(int)Math.round(color.getAlpha()*100000./255.); }
+                    public int getAlpha() { return 
Math.toIntExact(Math.round(color.getAlpha()*100000./255.)); }
                     @Override
                     public int getHueOff() { return -1; }
                     @Override
diff --git a/poi/src/main/java/org/apache/poi/sl/draw/DrawSheet.java 
b/poi/src/main/java/org/apache/poi/sl/draw/DrawSheet.java
index 4134b5ace0..50b28b2eb9 100644
--- a/poi/src/main/java/org/apache/poi/sl/draw/DrawSheet.java
+++ b/poi/src/main/java/org/apache/poi/sl/draw/DrawSheet.java
@@ -25,6 +25,7 @@ import java.awt.geom.AffineTransform;
 import org.apache.poi.sl.usermodel.MasterSheet;
 import org.apache.poi.sl.usermodel.Shape;
 import org.apache.poi.sl.usermodel.Sheet;
+import org.apache.poi.util.MathUtil;
 
 
 public class DrawSheet implements Drawable {
@@ -40,7 +41,9 @@ public class DrawSheet implements Drawable {
         Dimension dim = sheet.getSlideShow().getPageSize();
         Color whiteTrans = new Color(1f,1f,1f,0f);
         graphics.setColor(whiteTrans);
-        graphics.fillRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
+        graphics.fillRect(0, 0,
+                MathUtil.safeDoubleToInt(dim.getWidth()),
+                MathUtil.safeDoubleToInt(dim.getHeight()));
         
         DrawFactory drawFact = DrawFactory.getInstance(graphics);
         MasterSheet<?,?> master = sheet.getMasterSheet();
diff --git a/poi/src/main/java/org/apache/poi/sl/draw/DrawTexturePaint.java 
b/poi/src/main/java/org/apache/poi/sl/draw/DrawTexturePaint.java
index 52950bbacd..66c76bf315 100644
--- a/poi/src/main/java/org/apache/poi/sl/draw/DrawTexturePaint.java
+++ b/poi/src/main/java/org/apache/poi/sl/draw/DrawTexturePaint.java
@@ -35,6 +35,7 @@ import org.apache.poi.sl.usermodel.Insets2D;
 import org.apache.poi.sl.usermodel.PaintStyle;
 import org.apache.poi.util.Dimension2DDouble;
 import org.apache.poi.util.Internal;
+import org.apache.poi.util.MathUtil;
 
 @Internal
 public class DrawTexturePaint extends java.awt.TexturePaint {
@@ -115,20 +116,20 @@ public class DrawTexturePaint extends 
java.awt.TexturePaint {
             final int height = bi.getHeight();
 
             bi = bi.getSubimage(
-                (int)(Math.max(insets.left,0)/100_000 * width),
-                (int)(Math.max(insets.top,0)/100_000 * height),
-                
(int)((100_000-Math.max(insets.left,0)-Math.max(insets.right,0))/100_000 * 
width),
-                
(int)((100_000-Math.max(insets.top,0)-Math.max(insets.bottom,0))/100_000 * 
height)
+                    MathUtil.safeDoubleToInt(Math.max(insets.left,0)/100_000 * 
width),
+                    MathUtil.safeDoubleToInt(Math.max(insets.top,0)/100_000 * 
height),
+                    
MathUtil.safeDoubleToInt((100_000-Math.max(insets.left,0)-Math.max(insets.right,0))/100_000
 * width),
+                    
MathUtil.safeDoubleToInt((100_000-Math.max(insets.top,0)-Math.max(insets.bottom,0))/100_000
 * height)
             );
 
-            int addTop = (int)(Math.max(-insets.top, 0)/100_000 * height);
-            int addLeft = (int)(Math.max(-insets.left, 0)/100_000 * width);
-            int addBottom = (int)(Math.max(-insets.bottom, 0)/100_000 * 
height);
-            int addRight = (int)(Math.max(-insets.right, 0)/100_000 * width);
+            int addTop = MathUtil.safeDoubleToInt(Math.max(-insets.top, 
0)/100_000 * height);
+            int addLeft = MathUtil.safeDoubleToInt(Math.max(-insets.left, 
0)/100_000 * width);
+            int addBottom = MathUtil.safeDoubleToInt(Math.max(-insets.bottom, 
0)/100_000 * height);
+            int addRight = MathUtil.safeDoubleToInt(Math.max(-insets.right, 
0)/100_000 * width);
 
             // handle outsets
             if (addTop > 0 || addLeft > 0 || addBottom > 0 || addRight > 0) {
-                int[] buf = new int[bi.getWidth()*bi.getHeight()];
+                int[] buf = new int[Math.multiplyExact(bi.getWidth(), 
bi.getHeight())];
                 bi.getRGB(0, 0, bi.getWidth(), bi.getHeight(), buf, 0, 
bi.getWidth());
                 BufferedImage borderBi = new 
BufferedImage(bi.getWidth()+addLeft+addRight, bi.getHeight()+addTop+addBottom, 
bi.getType());
                 borderBi.setRGB(addLeft, addTop, bi.getWidth(), 
bi.getHeight(), buf, 0, bi.getWidth());
@@ -148,7 +149,10 @@ public class DrawTexturePaint extends 
java.awt.TexturePaint {
                 (100_000-stretch.top-stretch.bottom)/100_000 * 
userBounds.getHeight()
             );
 
-            BufferedImage stretchBi = new 
BufferedImage((int)userBounds.getWidth(), (int)userBounds.getHeight(), 
BufferedImage.TYPE_INT_ARGB);
+            BufferedImage stretchBi = new BufferedImage(
+                    MathUtil.safeDoubleToInt(userBounds.getWidth()),
+                    MathUtil.safeDoubleToInt(userBounds.getHeight()),
+                    BufferedImage.TYPE_INT_ARGB);
             Graphics2D g = stretchBi.createGraphics();
 
             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON);
diff --git a/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java 
b/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java
index 418e4f990c..15785d3b06 100644
--- a/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java
+++ b/poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java
@@ -40,6 +40,7 @@ import org.apache.poi.ss.usermodel.PictureData;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.util.MathUtil;
 import org.apache.poi.util.Units;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -317,6 +318,6 @@ public final class ImageUtils {
             }
         }
 
-        return (int)Math.rint(targetSize);
+        return MathUtil.safeDoubleToInt(Math.rint(targetSize));
     }
 }
diff --git a/poi/src/main/java/org/apache/poi/util/MathUtil.java 
b/poi/src/main/java/org/apache/poi/util/MathUtil.java
new file mode 100644
index 0000000000..a653887a36
--- /dev/null
+++ b/poi/src/main/java/org/apache/poi/util/MathUtil.java
@@ -0,0 +1,47 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.util;
+
+/**
+ * Utility methods for dealing with conversions
+ *
+ * @since 6.0.0
+ */
+public class MathUtil {
+    private MathUtil() {}
+
+    public static int safeFloatToInt(float f) {
+        if (f > Integer.MAX_VALUE || f < Integer.MIN_VALUE) {
+            throw new IllegalArgumentException("Value out of range: " + f);
+        }
+        return (int) f;
+    }
+
+    public static int safeDoubleToInt(double d) {
+        if (Double.isNaN(d)) {
+            throw new IllegalArgumentException("Cannot convert NaN to int");
+        }
+        if (Double.isInfinite(d)) {
+            throw new IllegalArgumentException("Cannot convert infinity to 
int");
+        }
+        if (d > Integer.MAX_VALUE || d < Integer.MIN_VALUE) {
+            throw new IllegalArgumentException("Value out of range: " + d);
+        }
+        return (int) d;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to