1 18 package org.apache.tools.ant.types.optional.image; 19 20 import org.apache.tools.ant.types.EnumeratedAttribute; 21 22 import javax.media.jai.JAI; 23 import javax.media.jai.PlanarImage; 24 import java.awt.image.BufferedImage ; 25 import java.awt.image.renderable.ParameterBlock ; 26 27 31 public class Scale extends TransformOperation implements DrawOperation { 32 private static final int HUNDRED = 100; 33 34 private String widthStr = "100%"; 35 private String heightStr = "100%"; 36 private boolean xPercent = true; 37 private boolean yPercent = true; 38 private String proportions = "ignore"; 39 40 41 public static class ProportionsAttribute extends EnumeratedAttribute { 42 43 public String [] getValues() { 44 return new String [] {"ignore", "width", "height", "cover", "fit"}; 45 } 46 } 47 48 52 public void setProportions(ProportionsAttribute pa) { 53 proportions = pa.getValue(); 54 } 55 56 61 public void setWidth(String width) { 62 widthStr = width; 63 } 64 65 69 public void setHeight(String height) { 70 heightStr = height; 71 } 72 73 77 public float getWidth() { 78 float width = 0.0F; 79 int percIndex = widthStr.indexOf('%'); 80 if (percIndex > 0) { 81 width = Float.parseFloat(widthStr.substring(0, percIndex)); 82 xPercent = true; 83 return width / HUNDRED; 84 } else { 85 xPercent = false; 86 return Float.parseFloat(widthStr); 87 } 88 } 89 90 94 public float getHeight() { 95 int percIndex = heightStr.indexOf('%'); 96 if (percIndex > 0) { 97 float height = Float.parseFloat(heightStr.substring(0, percIndex)); 98 yPercent = true; 99 return height / HUNDRED; 100 } else { 101 yPercent = false; 102 return Float.parseFloat(heightStr); 103 } 104 } 105 106 111 public PlanarImage performScale(PlanarImage image) { 112 ParameterBlock pb = new ParameterBlock (); 113 pb.addSource(image); 114 float xFl = getWidth(); 115 float yFl = getHeight(); 116 117 if (!xPercent) { 118 xFl = (xFl / image.getWidth()); 119 } 120 if (!yPercent) { 121 yFl = (yFl / image.getHeight()); 122 } 123 124 if ("width".equals(proportions)) { 125 yFl = xFl; 126 } else if ("height".equals(proportions)) { 127 xFl = yFl; 128 } else if ("fit".equals(proportions)) { 129 yFl = Math.min(xFl, yFl); 130 xFl = yFl; 131 } else if ("cover".equals(proportions)) { 132 yFl = Math.max(xFl, yFl); 133 xFl = yFl; 134 } 135 136 pb.add(new Float (xFl)); 137 pb.add(new Float (yFl)); 138 139 log("\tScaling to " + (xFl * HUNDRED) + "% x " 140 + (yFl * HUNDRED) + "%"); 141 142 return JAI.create("scale", pb); 143 } 144 145 146 147 public PlanarImage executeTransformOperation(PlanarImage image) { 148 BufferedImage bi = null; 149 for (int i = 0; i < instructions.size(); i++) { 150 ImageOperation instr = ((ImageOperation) instructions.elementAt(i)); 151 if (instr instanceof DrawOperation) { 152 return performScale(image); 153 } else if (instr instanceof TransformOperation) { 154 bi = image.getAsBufferedImage(); 155 image = ((TransformOperation) instr) 156 .executeTransformOperation(PlanarImage.wrapRenderedImage(bi)); 157 bi = image.getAsBufferedImage(); 158 } 159 } 160 return performScale(image); 161 } 162 163 164 165 public PlanarImage executeDrawOperation() { 166 for (int i = 0; i < instructions.size(); i++) { 167 ImageOperation instr = ((ImageOperation) instructions.elementAt(i)); 168 if (instr instanceof DrawOperation) { 169 PlanarImage image = null; 170 performScale(image); 173 return image; 174 } 175 } 176 return null; 177 } 178 } 179 | Popular Tags |