KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > optional > image > Scale


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

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 JavaDoc;
25 import java.awt.image.renderable.ParameterBlock JavaDoc;
26
27 /**
28  *
29  * @see org.apache.tools.ant.taskdefs.optional.image.Image
30  */

31 public class Scale extends TransformOperation implements DrawOperation {
32     private static final int HUNDRED = 100;
33
34     private String JavaDoc widthStr = "100%";
35     private String JavaDoc heightStr = "100%";
36     private boolean xPercent = true;
37     private boolean yPercent = true;
38     private String JavaDoc proportions = "ignore";
39
40     /** Enumerated class for proportions attribute. */
41     public static class ProportionsAttribute extends EnumeratedAttribute {
42         /** {@inheritDoc}. */
43         public String JavaDoc[] getValues() {
44             return new String JavaDoc[] {"ignore", "width", "height", "cover", "fit"};
45         }
46     }
47
48     /**
49      * Sets the behaviour regarding the image proportions.
50      * @param pa the enumerated value.
51      */

52     public void setProportions(ProportionsAttribute pa) {
53         proportions = pa.getValue();
54     }
55
56     /**
57      * Sets the width of the image, either as an integer or a %.
58      * Defaults to 100%.
59      * @param width the value to use.
60      */

61     public void setWidth(String JavaDoc width) {
62         widthStr = width;
63     }
64
65     /**
66      * Sets the height of the image, either as an integer or a %. Defaults to 100%.
67      * @param height the value to use.
68      */

69     public void setHeight(String JavaDoc height) {
70         heightStr = height;
71     }
72
73     /**
74      * Get the width.
75      * @return the value converted from the width string.
76      */

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     /**
91      * Get the height.
92      * @return the value converted from the height string.
93      */

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     /**
107      * Scale an image.
108      * @param image the image to scale.
109      * @return the scaled image.
110      */

111     public PlanarImage performScale(PlanarImage image) {
112         ParameterBlock JavaDoc pb = new ParameterBlock JavaDoc();
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 JavaDoc(xFl));
137         pb.add(new Float JavaDoc(yFl));
138
139         log("\tScaling to " + (xFl * HUNDRED) + "% x "
140             + (yFl * HUNDRED) + "%");
141
142         return JAI.create("scale", pb);
143     }
144
145
146     /** {@inheritDoc}. */
147     public PlanarImage executeTransformOperation(PlanarImage image) {
148         BufferedImage JavaDoc 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     /** {@inheritDoc}. */
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                 // If this TransformOperation has DrawOperation children
171
// then Rotate the first child and return.
172
performScale(image);
173                 return image;
174             }
175         }
176         return null;
177     }
178 }
179
Popular Tags