JAL-4274 Use Cache BITMAP_* if no other image restrictions given
[jalview.git] / src / jalview / util / ImageMaker.java
index 64771c4..fa473f3 100755 (executable)
  */
 package jalview.util;
 
-import jalview.bin.Cache;
-import jalview.bin.Jalview;
-import jalview.gui.IProgressIndicator;
-import jalview.gui.LineartOptions;
-import jalview.io.JalviewFileChooser;
-import jalview.util.dialogrunner.RunResponse;
-
-import java.awt.Component;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.util.concurrent.atomic.AtomicBoolean;
+import java.io.IOException;
 
 import javax.imageio.ImageIO;
-import javax.swing.JOptionPane;
 
 import org.jfree.graphics2d.svg.SVGGraphics2D;
 import org.jfree.graphics2d.svg.SVGHints;
 import org.jibble.epsgraphics.EpsGraphics2D;
 
+import jalview.bin.Console;
+import jalview.io.JalviewFileChooser;
+import jalview.util.imagemaker.BitmapImageSizing;
+
 public class ImageMaker
 {
   public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
@@ -57,10 +52,6 @@ public class ImageMaker
 
   public static final String PNG_DESCRIPTION = "Portable  network graphics";
 
-  public static final String HTML_EXTENSION = "html";
-
-  public static final String HTML_DESCRIPTION = "Hypertext Markup Language";
-
   EpsGraphics2D pg;
 
   Graphics graphics;
@@ -71,12 +62,6 @@ public class ImageMaker
 
   TYPE type;
 
-  private IProgressIndicator pIndicator;
-
-  private long pSessionId;
-
-  private boolean headless;
-
   public enum TYPE
   {
     EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
@@ -119,79 +104,36 @@ public class ImageMaker
   }
 
   /**
-   * Constructor builds the image and writes it to file. If the supplied file
-   * name is null, the user is prompted for the output file.
+   * Constructor configures the graphics context ready for writing to
    * 
-   * @param parent
-   * @param type
-   * @param title
+   * @param imageType
    * @param width
    * @param height
    * @param file
    * @param fileTitle
-   * @param pIndicator
-   * @param pSessionId
-   * @param headless
+   * @param useLineart
+   * @param bitmapscale
+   * @throws IOException
    */
-  public ImageMaker(Component parent, TYPE type, String title, int width,
-          int height, File file, String fileTitle,
-          IProgressIndicator pIndicator, long pSessionId, boolean headless)
+  public ImageMaker(TYPE imageType, int width, int height, File file,
+          String fileTitle, boolean useLineart, BitmapImageSizing userBis)
+          throws IOException
   {
-    this.pIndicator = pIndicator;
-    this.type = type;
-    this.pSessionId = pSessionId;
-    this.headless = headless;
-    if (file == null && !Jalview.isHeadlessMode())
-    {
-      setProgressMessage(MessageManager.formatMessage(
-              "status.waiting_for_user_to_select_output_file", type.name));
-      JalviewFileChooser chooser = type.getFileChooser();
-      chooser.setFileView(new jalview.io.JalviewFileView());
-      chooser.setDialogTitle(title);
-      chooser.setToolTipText(MessageManager.getString("action.save"));
-      int value = chooser.showSaveDialog(parent);
-
-      if (value == JalviewFileChooser.APPROVE_OPTION)
-      {
-        Cache.setProperty("LAST_DIRECTORY",
-                chooser.getSelectedFile().getParent());
-        file = chooser.getSelectedFile();
-      }
-      else
-      {
-        setProgressMessage(MessageManager.formatMessage(
-                "status.cancelled_image_export_operation", type.name));
-      }
-    }
+    this.type = imageType;
 
-    if (file != null)
+    out = new FileOutputStream(file);
+    switch (imageType)
     {
-      try
-      {
-        out = new FileOutputStream(file);
-        setProgressMessage(null);
-        setProgressMessage(MessageManager.formatMessage(
-                "status.exporting_alignment_as_x_file", type.getName()));
-        if (type == TYPE.SVG)
-        {
-          setupSVG(width, height, fileTitle);
-        }
-        else if (type == TYPE.EPS)
-        {
-          setupEPS(width, height, fileTitle);
-        }
-        else if (type == TYPE.PNG)
-        {
-          setupPNG(width, height);
-        }
-
-      } catch (Exception ex)
-      {
-        System.out.println("Error creating " + type.getName() + " file.");
-
-        setProgressMessage(MessageManager
-                .formatMessage("info.error_creating_file", type.getName()));
-      }
+    case SVG:
+      setupSVG(width, height, useLineart);
+      break;
+    case EPS:
+      setupEPS(width, height, fileTitle, useLineart);
+      break;
+    case PNG:
+      setupPNG(width, height, userBis);
+      break;
+    default:
     }
   }
 
@@ -234,205 +176,211 @@ public class ImageMaker
   }
 
   /**
-   * Generates an EPS image and writes it to the (previously set) output buffer.
-   * The user is first prompted for choice of Text or Lineart rendering, unless
-   * a preference for this has been set.
+   * Sets up a graphics object for the PNG image to be written on
    * 
    * @param width
    * @param height
-   * @param title
+   * @param scale
    */
-  void setupEPS(int width, int height, String title)
+  protected void setupPNG(int width, int height, BitmapImageSizing userBis)
   {
-    String renderStyle = Cache.getDefault("EPS_RENDERING",
-            "Prompt each time");
-    AtomicBoolean textOption = new AtomicBoolean(
-            !"Lineart".equals(renderStyle));
-
-    /*
-     * configure the action to run on OK in the dialog
-     */
-    RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION)
-    {
-      @Override
-      public void run()
-      {
-        writeEPS(width, height, title, textOption.get());
-      }
-    };
-
-    /*
-     * Prompt for character rendering style if preference is not set
-     */
-    if (renderStyle.equalsIgnoreCase("Prompt each time")
-            && !(System.getProperty("java.awt.headless") != null && System
-                    .getProperty("java.awt.headless").equals("true")))
+    if (width == 0 || height == 0)
+      return;
+
+    BitmapImageSizing bis = ImageMaker.getScaleWidthHeight(width, height,
+            userBis);
+    float usescale = bis.scale();
+    int usewidth = bis.width();
+    int useheight = bis.height();
+
+    bi = new BufferedImage(usewidth, useheight, BufferedImage.TYPE_INT_RGB);
+    graphics = bi.getGraphics();
+    Graphics2D ig2 = (Graphics2D) graphics;
+    ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+            RenderingHints.VALUE_ANTIALIAS_ON);
+    if (usescale > 0.0f)
     {
-      LineartOptions epsOption = new LineartOptions("EPS_RENDERING",
-              TYPE.EPS.getName(), textOption);
-      epsOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION)
-      {
-        @Override
-        public void run()
-        {
-          setProgressMessage(MessageManager.formatMessage(
-                  "status.cancelled_image_export_operation", "EPS"));
-        }
-      });
-      epsOption.setResponseAction(okAction);
-      epsOption.showDialog();
-      /* no code here - JalviewJS cannot execute it */
+      ig2.scale(usescale, usescale);
     }
-    else
+  }
+
+  /**
+   * A helper method to configure the SVG output graphics, with choice of Text
+   * or Lineart character rendering
+   * 
+   * @param width
+   * @param height
+   * @param useLineart
+   *          true for Lineart character rendering, false for Text
+   */
+  protected void setupSVG(int width, int height, boolean useLineart)
+  {
+    SVGGraphics2D g2 = new SVGGraphics2D(width, height);
+    if (useLineart)
     {
-      /*
-       * else (if preference set) just do the export action
-       */
-      writeEPS(width, height, title, textOption.get());
+      g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
+              SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
     }
+    graphics = g2;
   }
 
   /**
-   * Sets up a graphics object for the PNG image to be written on
+   * A helper method that sets up the EPS graphics output with user choice of
+   * Text or Lineart character rendering
    * 
    * @param width
    * @param height
+   * @param title
+   * @param useLineart
+   *          true for Lineart character rendering, false for Text
+   * @throws IOException
    */
-  void setupPNG(int width, int height)
+  protected void setupEPS(int width, int height, String title,
+          boolean useLineart) throws IOException
   {
-    bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-    graphics = bi.getGraphics();
-    Graphics2D ig2 = (Graphics2D) graphics;
+    pg = new EpsGraphics2D(title, out, 0, 0, width, height);
+    Graphics2D ig2 = pg;
     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
             RenderingHints.VALUE_ANTIALIAS_ON);
-    setProgressMessage(MessageManager
-            .formatMessage("status.export_complete", type.getName()));
+    pg.setAccurateTextMode(useLineart);
+    graphics = pg;
   }
 
   /**
-   * Sets up a graphics object for the SVG image to be written on. The user is
-   * first prompted for choice of Text or Lineart rendering, unless a preference
-   * for this has been set.
+   * 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
-   * @param title
+   *          The unscaled image height
+   * @param scale
+   *          The suggested scaling
+   * @param bitmapwidth
+   *          The suggested width
+   * @param bitmapheight
+   *          The suggested height
+   * @return BitmapImageSizing A consistent scale,width and height for the final
+   *         image
    */
-  void setupSVG(int width, int height, String title)
+  public static BitmapImageSizing getScaleWidthHeight(int width, int height,
+          float scale, int bitmapwidth, int bitmapheight)
   {
-    String renderStyle = Cache.getDefault("SVG_RENDERING",
-            "Prompt each time");
-    AtomicBoolean textOption = new AtomicBoolean(
-            !"Lineart".equals(renderStyle));
-
-    /*
-     * configure the action to run on OK in the dialog
-     */
-    RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION)
+    float usescale = 0.0f;
+    int usewidth = width;
+    int useheight = height;
+
+    if ((width == 0 && bitmapwidth > 0)
+            || (height == 0 && bitmapheight > 0))
     {
-      @Override
-      public void run()
-      {
-        setupSVG(width, height, textOption.get());
-      }
-    };
-
-    /*
-     * Prompt for character rendering style if preference is not set
-     */
-    if (renderStyle.equalsIgnoreCase("Prompt each time")
-            && !(System.getProperty("java.awt.headless") != null && System
-                    .getProperty("java.awt.headless").equals("true")))
+      // 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)
     {
-      LineartOptions svgOption = new LineartOptions("SVG_RENDERING",
-              TYPE.SVG.getName(), textOption);
-      svgOption.setResponseAction(new RunResponse(JOptionPane.NO_OPTION)
-      {
-        @Override
-        public void run()
-        {
-          setProgressMessage(MessageManager.formatMessage(
-                  "status.cancelled_image_export_operation", "SVG"));
-        }
-      });
-      svgOption.setResponseAction(okAction);
-      svgOption.showDialog();
-      /* no code here - JalviewJS cannot execute it */
+      usescale = scale;
+      usewidth = Math.round(scale * width);
+      useheight = Math.round(scale * height);
     }
-    else
+    if (bitmapwidth > 0)
     {
-      /*
-       * else (if preference set) just do the export action
-       */
-      setupSVG(width, height, textOption.get());
+      float wscale = (float) bitmapwidth / width;
+      if (wscale > 0.0f && (usescale == 0.0f || wscale < usescale))
+      {
+        usescale = wscale;
+        usewidth = bitmapwidth;
+        useheight = Math.round(usescale * height);
+      }
     }
-  }
-
-  void setProgressMessage(String message)
-  {
-    if (pIndicator != null && !headless)
+    if (bitmapheight > 0)
     {
-      pIndicator.setProgressBar(message, pSessionId);
+      float hscale = (float) bitmapheight / height;
+      if (hscale > 0.0f && (usescale == 0.0f || hscale < usescale))
+      {
+        usescale = hscale;
+        usewidth = Math.round(usescale * width);
+        useheight = bitmapheight;
+      }
     }
+    return new BitmapImageSizing(usescale, usewidth, useheight, false);
   }
 
   /**
-   * A helper method to configure the SVG output graphics, with choice of Text
-   * or Lineart character rendering
+   * Takes suggested scale, width, height as a BitmapImageSizing object and
+   * create a bounding box returned as a BitmapImageSizing object with
+   * consistent scale, width, height fields.
    * 
-   * @param width
-   * @param height
-   * @param textOption
-   *          true for Text, false for Lineart
+   * @param bis
+   * @return BitmapImageSizing
    */
-  protected void setupSVG(int width, int height, boolean textOption)
+  public static BitmapImageSizing getScaleWidthHeight(int width, int height,
+          BitmapImageSizing bis)
   {
-    SVGGraphics2D g2 = new SVGGraphics2D(width, height);
-    if (!textOption) // Lineart selected
-    {
-      g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
-              SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
-    }
-    setProgressMessage(MessageManager
-            .formatMessage("status.export_complete", type.getName()));
-    graphics = g2;
+    return ImageMaker.getScaleWidthHeight(width, height, bis.scale(),
+            bis.width(), bis.height());
   }
 
   /**
-   * A helper method that sets up the EPS graphics output with user choice of
-   * Text or Lineart character rendering
+   * Takes String versions of 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
-   * @param height
-   * @param title
-   * @param textOption
-   *          true for Text, false for Lineart
+   * @param scaleS
+   * @param widthS
+   * @param heightS
+   * @return BitmapImageSizing
    */
-  protected void writeEPS(int width, int height, String title,
-          boolean textOption)
+  public static BitmapImageSizing parseScaleWidthHeightStrings(
+          String scaleS, String widthS, String heightS)
   {
-    try
+    if (scaleS == null && widthS == null && heightS == null)
     {
-      pg = new EpsGraphics2D(title, out, 0, 0, width, height);
-      Graphics2D ig2 = pg;
-      ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-              RenderingHints.VALUE_ANTIALIAS_ON);
-      pg.setAccurateTextMode(!textOption); // true = Lineart
-      graphics = pg;
-      setProgressMessage(MessageManager
-              .formatMessage("status.export_complete", type.getName()));
-    } catch (Exception ex)
-    {
-      System.err.println("Error writing PNG: " + ex.toString());
+      // if all items are null (i.e. not provided) we use the dynamic
+      // preferences set BIS
+      return BitmapImageSizing.defaultBitmapImageSizing();
     }
-  }
 
-  static JalviewFileChooser getSVGChooser()
-  {
-    if (Jalview.isHeadlessMode())
+    float scale = 0.0f;
+    int width = 0;
+    int height = 0;
+
+    if (scaleS != null)
+    {
+      try
+      {
+        scale = Float.parseFloat(scaleS);
+      } catch (NumberFormatException e)
+      {
+        Console.warn("Did not understand scale '" + scaleS
+                + "', won't be used.");
+      }
+    }
+    if (widthS != null)
     {
-      return null;
+      try
+      {
+        width = Integer.parseInt(widthS);
+      } catch (NumberFormatException e)
+      {
+        Console.warn("Did not understand width '" + widthS
+                + "', won't be used.");
+      }
     }
-    return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);
+    if (heightS != null)
+    {
+      try
+      {
+        height = Integer.parseInt(heightS);
+      } catch (NumberFormatException e)
+      {
+        Console.warn("Did not understand height '" + heightS
+                + "', won't be used.");
+      }
+    }
+
+    return new BitmapImageSizing(scale, width, height, false);
   }
 }