source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/optional/image/Scale.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.9 KB
Line 
1/*
2 * Copyright 2002-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.types.optional.image;
18
19import org.apache.tools.ant.types.EnumeratedAttribute;
20
21import javax.media.jai.JAI;
22import javax.media.jai.PlanarImage;
23import java.awt.image.BufferedImage;
24import java.awt.image.renderable.ParameterBlock;
25
26/**
27 *
28 * @see org.apache.tools.ant.taskdefs.optional.image.Image
29 */
30public class Scale extends TransformOperation implements DrawOperation {
31
32 private String width_str = "100%";
33 private String height_str = "100%";
34 private boolean x_percent = true;
35 private boolean y_percent = true;
36 private String proportions = "ignore";
37
38 public static class ProportionsAttribute extends EnumeratedAttribute {
39 public String[] getValues() {
40 return new String[] {"ignore", "width", "height", "cover", "fit"};
41 }
42 }
43
44 /**
45 * Sets the behaviour regarding the image proportions.
46 */
47 public void setProportions(ProportionsAttribute pa) {
48 proportions = pa.getValue();
49 }
50
51 /**
52 * Sets the width of the image, either as an integer or a %. Defaults to 100%.
53 */
54 public void setWidth(String width) {
55 width_str = width;
56 }
57
58 /**
59 * Sets the height of the image, either as an integer or a %. Defaults to 100%.
60 */
61 public void setHeight(String height) {
62 height_str = height;
63 }
64
65 public float getWidth() {
66 float width = 0.0F;
67 int perc_index = width_str.indexOf('%');
68 if (perc_index > 0) {
69 width = Float.parseFloat(width_str.substring(0, perc_index));
70 x_percent = true;
71 return width / 100;
72 } else {
73 x_percent = false;
74 return Float.parseFloat(width_str);
75 }
76 }
77
78 public float getHeight() {
79 int perc_index = height_str.indexOf('%');
80 if (perc_index > 0) {
81 float height = Float.parseFloat(height_str.substring(0, perc_index));
82 y_percent = true;
83 return height / 100;
84 } else {
85 y_percent = false;
86 return Float.parseFloat(height_str);
87 }
88 }
89
90 public PlanarImage performScale(PlanarImage image) {
91 ParameterBlock pb = new ParameterBlock();
92 pb.addSource(image);
93 float x_fl = getWidth();
94 float y_fl = getHeight();
95
96 if (!x_percent) {
97 x_fl = (x_fl / image.getWidth());
98 }
99 if (!y_percent) {
100 y_fl = (y_fl / image.getHeight());
101 }
102
103 if ("width".equals(proportions)) {
104 y_fl = x_fl;
105 } else if ("height".equals(proportions)) {
106 x_fl = y_fl;
107 } else if ("fit".equals(proportions)) {
108 x_fl = y_fl = Math.min(x_fl, y_fl);
109 } else if ("cover".equals(proportions)) {
110 x_fl = y_fl = Math.max(x_fl, y_fl);
111 }
112
113 pb.add(new Float(x_fl));
114 pb.add(new Float(y_fl));
115
116 log("\tScaling to " + (x_fl * 100) + "% x " + (y_fl * 100) + "%");
117
118 return JAI.create("scale", pb);
119 }
120
121
122 public PlanarImage executeTransformOperation(PlanarImage image) {
123 BufferedImage bi = null;
124 for (int i = 0; i < instructions.size(); i++) {
125 ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
126 if (instr instanceof DrawOperation) {
127 return performScale(image);
128 } else if (instr instanceof TransformOperation) {
129 bi = image.getAsBufferedImage();
130 image = ((TransformOperation) instr).executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
131 bi = image.getAsBufferedImage();
132 }
133 }
134 return performScale(image);
135 }
136
137
138 public PlanarImage executeDrawOperation() {
139 for (int i = 0; i < instructions.size(); i++) {
140 ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
141 if (instr instanceof DrawOperation) {
142 PlanarImage image = null;
143 // If this TransformOperation has DrawOperation children
144 // then Rotate the first child and return.
145 performScale(image);
146 return image;
147 }
148 }
149 return null;
150 }
151}
Note: See TracBrowser for help on using the repository browser.