From a0406cafe6a8019f43e573018ad38f7095db879f Mon Sep 17 00:00:00 2001 From: Antoine Levy-Lambert Date: Fri, 22 Aug 2003 09:20:56 +0000 Subject: [PATCH] Quoting Rob Oxspring : I was using the image task to create thumbnails and couldn't figure out how to keep proportions of an image but keep it within a fixed size. My solution was to change the keepproportions attribute to be a little cleverer. The keepproportions attribute is no more and has been replaced by the proportions attribute, which has been added with the following features: proportions="ignore" - treat the dimensions independently (==keepproportions="false" and is default) proportions="width" - keep proportions based on the width (==keepproportions="true") proportions="height" - keep proportions based on the height proportions="fit" - keep proportions and fit in the supplied dimensions proportions="cover" - keep proportions and cover the supplied dimensions Submitted by: Rob Oxspring (roxspring at imapmail dot org) git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275127 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 ++ docs/manual/OptionalTasks/image.html | 14 ++++--- .../taskdefs/optional/image/image.xml | 10 ++--- .../tools/ant/types/optional/image/Scale.java | 39 ++++++++++++++++--- 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index c8e0d6fcb..9b4408d17 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -262,6 +262,9 @@ Other changes: * Added task (requires JAI). +* task has now proportions attribute in the nested element + instead of keepproportions (bringing in more functionality) + * New condition * now has a preservelastmodified attribute to preserve the diff --git a/docs/manual/OptionalTasks/image.html b/docs/manual/OptionalTasks/image.html index 2570c97a3..014a29182 100644 --- a/docs/manual/OptionalTasks/image.html +++ b/docs/manual/OptionalTasks/image.html @@ -138,11 +138,15 @@ ImageOperation can handle nested Rotate, Draw, Rectangle, Text and Scale objects Description Required - - keepproportions - Boolean value. Sets whether the proportion heigth/width should be kept. - no (defaults to false) - + proportions + Sets which dimension to control proportions from. Valid values are:
    +
  • "ignore" - treat the dimensions independently.
  • +
  • "height" - keep proportions based on the width.
  • +
  • "width" - keep proportions based on the height.
  • +
  • "cover" - keep proportions and fit in the supplied dimensions.
  • +
  • "fit" - keep proportions and cover the supplied dimensions.
  • +
++ no (defaults to ignore) width Sets the width of the image, either as an integer or a %. diff --git a/src/etc/testcases/taskdefs/optional/image/image.xml b/src/etc/testcases/taskdefs/optional/image/image.xml index 79bd73489..ade003133 100644 --- a/src/etc/testcases/taskdefs/optional/image/image.xml +++ b/src/etc/testcases/taskdefs/optional/image/image.xml @@ -18,35 +18,35 @@ - + - + - + - + - + diff --git a/src/main/org/apache/tools/ant/types/optional/image/Scale.java b/src/main/org/apache/tools/ant/types/optional/image/Scale.java index d8685aebb..59f59ee21 100644 --- a/src/main/org/apache/tools/ant/types/optional/image/Scale.java +++ b/src/main/org/apache/tools/ant/types/optional/image/Scale.java @@ -53,26 +53,42 @@ */ package org.apache.tools.ant.types.optional.image; +import org.apache.tools.ant.types.EnumeratedAttribute; + import javax.media.jai.JAI; import javax.media.jai.PlanarImage; +import javax.media.jai.InterpolationBilinear; +import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.awt.image.renderable.ParameterBlock; /** * * @author Kevin Z Grey + * @author Rob Oxspring * @see org.apache.tools.ant.taskdefs.optional.image.Image */ public class Scale extends TransformOperation implements DrawOperation { + private String width_str = "100%"; private String height_str = "100%"; private boolean x_percent = true; private boolean y_percent = true; - private boolean keep_proportions = false; + private String proportions = "ignore"; + + public static class ProportionsAttribute extends EnumeratedAttribute { + public String[] getValues() { + return new String[] {"ignore", "width", "height", "cover", "fit"}; + } + } - public void setKeepproportions(boolean props) { - keep_proportions = props; + /** + * Sets the behaviour regarding the image proportions. + */ + public void setProportions(ProportionsAttribute pa){ + proportions = pa.getValue(); } + /** * Sets the width of the image, either as an integer or a %. Defaults to 100%. */ @@ -117,19 +133,32 @@ public class Scale extends TransformOperation implements DrawOperation { pb.addSource(image); float x_fl = getWidth(); float y_fl = getHeight(); + if (!x_percent) { x_fl = (x_fl / image.getWidth()); } if (!y_percent) { y_fl = (y_fl / image.getHeight()); } - if (keep_proportions) { + + if("width".equals(proportions)){ y_fl = x_fl; } + else if("height".equals(proportions)){ + x_fl = y_fl; + } + else if("fit".equals(proportions)){ + x_fl = y_fl = Math.min(x_fl,y_fl); + } + else if("cover".equals(proportions)){ + x_fl = y_fl = Math.max(x_fl,y_fl); + } + pb.add(new Float(x_fl)); pb.add(new Float(y_fl)); - log("\tScaling to " + x_fl + "% x " + y_fl + "%"); + log("\tScaling to " + (x_fl*100) + "% x " + (y_fl*100)+ "%"); + return JAI.create("scale", pb); }