@@ -29,10 +29,10 @@ import java.awt.image.renderable.ParameterBlock;
*/
public class Scale extends TransformOperation implements DrawOperation {
private String width_s tr = "100%";
private String height_s tr = "100%";
private boolean x_p ercent = true;
private boolean y_p ercent = true;
private String widthS tr = "100%";
private String heightS tr = "100%";
private boolean xP ercent = true;
private boolean yP ercent = true;
private String proportions = "ignore";
public static class ProportionsAttribute extends EnumeratedAttribute {
@@ -52,68 +52,70 @@ public class Scale extends TransformOperation implements DrawOperation {
* Sets the width of the image, either as an integer or a %. Defaults to 100%.
*/
public void setWidth(String width) {
width_s tr = width;
widthS tr = width;
}
/**
* Sets the height of the image, either as an integer or a %. Defaults to 100%.
*/
public void setHeight(String height) {
height_s tr = height;
heightS tr = height;
}
public float getWidth() {
float width = 0.0F;
int perc_index = width_s tr.indexOf('%');
if (perc_i ndex > 0) {
width = Float.parseFloat(width_str.substring(0, perc_i ndex));
x_p ercent = true;
int percIndex = widthS tr.indexOf('%');
if (percI ndex > 0) {
width = Float.parseFloat(widthStr.substring(0, percI ndex));
xP ercent = true;
return width / 100;
} else {
x_p ercent = false;
return Float.parseFloat(width_s tr);
xP ercent = false;
return Float.parseFloat(widthS tr);
}
}
public float getHeight() {
int perc_index = height_s tr.indexOf('%');
if (perc_i ndex > 0) {
float height = Float.parseFloat(height_str.substring(0, perc_i ndex));
y_p ercent = true;
int percIndex = heightS tr.indexOf('%');
if (percI ndex > 0) {
float height = Float.parseFloat(heightStr.substring(0, percI ndex));
yP ercent = true;
return height / 100;
} else {
y_p ercent = false;
return Float.parseFloat(height_s tr);
yP ercent = false;
return Float.parseFloat(heightS tr);
}
}
public PlanarImage performScale(PlanarImage image) {
ParameterBlock pb = new ParameterBlock();
pb.addSource(image);
float x_f l = getWidth();
float y_f l = getHeight();
float xF l = getWidth();
float yF l = getHeight();
if (!x_p ercent) {
x_fl = (x_f l / image.getWidth());
if (!xP ercent) {
xFl = (xF l / image.getWidth());
}
if (!y_p ercent) {
y_fl = (y_f l / image.getHeight());
if (!yP ercent) {
yFl = (yF l / image.getHeight());
}
if ("width".equals(proportions)) {
y_fl = x_f l;
yFl = xF l;
} else if ("height".equals(proportions)) {
x_fl = y_f l;
xFl = yF l;
} else if ("fit".equals(proportions)) {
x_fl = y_fl = Math.min(x_fl, y_fl);
yFl = Math.min(xFl, yFl);
xFl = yFl;
} else if ("cover".equals(proportions)) {
x_fl = y_fl = Math.max(x_fl, y_fl);
yFl = Math.max(xFl, yFl);
xFl = yFl;
}
pb.add(new Float(x_f l));
pb.add(new Float(y_f l));
pb.add(new Float(xF l));
pb.add(new Float(yF l));
log("\tScaling to " + (x_fl * 100) + "% x " + (y_f l * 100) + "%");
log("\tScaling to " + (xFl * 100) + "% x " + (yF l * 100) + "%");
return JAI.create("scale", pb);
}
@@ -127,7 +129,8 @@ public class Scale extends TransformOperation implements DrawOperation {
return performScale(image);
} else if (instr instanceof TransformOperation) {
bi = image.getAsBufferedImage();
image = ((TransformOperation) instr).executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
image = ((TransformOperation) instr)
.executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
bi = image.getAsBufferedImage();
}
}