JAL-4274 Use Cache BITMAP_* if no other image restrictions given
[jalview.git] / src / jalview / util / ImageMaker.java
index 3306b0d..fa473f3 100755 (executable)
@@ -189,9 +189,9 @@ public class ImageMaker
 
     BitmapImageSizing bis = ImageMaker.getScaleWidthHeight(width, height,
             userBis);
-    float usescale = bis.scale;
-    int usewidth = bis.width;
-    int useheight = bis.height;
+    float usescale = bis.scale();
+    int usewidth = bis.width();
+    int useheight = bis.height();
 
     bi = new BufferedImage(usewidth, useheight, BufferedImage.TYPE_INT_RGB);
     graphics = bi.getGraphics();
@@ -247,14 +247,22 @@ public class ImageMaker
   }
 
   /**
-   * Takes suggested float scale, int width, int height and create a bounding
-   * box returned as a BitmapImageSizing object with consistent scale, width,
-   * height fields.
+   * Takes initial width and height, and suggested float scale, int width, int
+   * height and create a bounding box returned as a BitmapImageSizing object
+   * with consistent scale, width, height fields.
    * 
+   * @param width
+   *          The unscaled image width
+   * @param height
+   *          The unscaled image height
    * @param scale
+   *          The suggested scaling
    * @param bitmapwidth
+   *          The suggested width
    * @param bitmapheight
-   * @return BitmapImageSizing
+   *          The suggested height
+   * @return BitmapImageSizing A consistent scale,width and height for the final
+   *         image
    */
   public static BitmapImageSizing getScaleWidthHeight(int width, int height,
           float scale, int bitmapwidth, int bitmapheight)
@@ -263,6 +271,13 @@ public class ImageMaker
     int usewidth = width;
     int useheight = height;
 
+    if ((width == 0 && bitmapwidth > 0)
+            || (height == 0 && bitmapheight > 0))
+    {
+      // original image is zero sized! Avoid dividing by zero!
+      return BitmapImageSizing.nullBitmapImageSizing();
+    }
+
     // use the smallest positive scale (i.e. fit in the box)
     if (scale > 0.0f)
     {
@@ -290,7 +305,7 @@ public class ImageMaker
         useheight = bitmapheight;
       }
     }
-    return new BitmapImageSizing(usescale, usewidth, useheight);
+    return new BitmapImageSizing(usescale, usewidth, useheight, false);
   }
 
   /**
@@ -304,8 +319,8 @@ public class ImageMaker
   public static BitmapImageSizing getScaleWidthHeight(int width, int height,
           BitmapImageSizing bis)
   {
-    return ImageMaker.getScaleWidthHeight(width, height, bis.scale,
-            bis.width, bis.height);
+    return ImageMaker.getScaleWidthHeight(width, height, bis.scale(),
+            bis.width(), bis.height());
   }
 
   /**
@@ -321,6 +336,13 @@ public class ImageMaker
   public static BitmapImageSizing parseScaleWidthHeightStrings(
           String scaleS, String widthS, String heightS)
   {
+    if (scaleS == null && widthS == null && heightS == null)
+    {
+      // if all items are null (i.e. not provided) we use the dynamic
+      // preferences set BIS
+      return BitmapImageSizing.defaultBitmapImageSizing();
+    }
+
     float scale = 0.0f;
     int width = 0;
     int height = 0;
@@ -359,6 +381,6 @@ public class ImageMaker
       }
     }
 
-    return new BitmapImageSizing(scale, width, height);
+    return new BitmapImageSizing(scale, width, height, false);
   }
 }