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 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.image;
|
---|
18 |
|
---|
19 | import com.sun.media.jai.codec.FileSeekableStream;
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.DirectoryScanner;
|
---|
22 | import org.apache.tools.ant.taskdefs.MatchingTask;
|
---|
23 | import org.apache.tools.ant.types.FileSet;
|
---|
24 | import org.apache.tools.ant.types.optional.image.Draw;
|
---|
25 | import org.apache.tools.ant.types.optional.image.ImageOperation;
|
---|
26 | import org.apache.tools.ant.types.optional.image.Rotate;
|
---|
27 | import org.apache.tools.ant.types.optional.image.Scale;
|
---|
28 | import org.apache.tools.ant.types.optional.image.TransformOperation;
|
---|
29 |
|
---|
30 | import javax.media.jai.JAI;
|
---|
31 | import javax.media.jai.PlanarImage;
|
---|
32 | import java.io.File;
|
---|
33 | import java.io.FileOutputStream;
|
---|
34 | import java.io.IOException;
|
---|
35 | import java.util.ArrayList;
|
---|
36 | import java.util.Iterator;
|
---|
37 | import java.util.Vector;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * A MatchingTask which relies on
|
---|
41 | * <A HREF="http://java.sun.com/products/java-media/jai">JAI (Java Advanced Imaging)</A>
|
---|
42 | * to perform image manipulation operations on existing images. The
|
---|
43 | * operations are represented as ImageOperation DataType objects.
|
---|
44 | * The operations are arranged to conform to the Chaining Model
|
---|
45 | * of JAI.
|
---|
46 | * Check out the
|
---|
47 | * <A HREF="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/">
|
---|
48 | * JAI Programming Guide</A>
|
---|
49 | *
|
---|
50 | * @see org.apache.tools.ant.types.optional.image.ImageOperation
|
---|
51 | * @see org.apache.tools.ant.types.DataType
|
---|
52 | */
|
---|
53 | public class Image extends MatchingTask {
|
---|
54 | protected Vector instructions = new Vector();
|
---|
55 | protected String str_encoding = "JPEG";
|
---|
56 | protected boolean overwrite = false;
|
---|
57 | protected boolean garbage_collect = false;
|
---|
58 | private boolean failonerror = true;
|
---|
59 | protected Vector filesets = new Vector();
|
---|
60 |
|
---|
61 |
|
---|
62 | protected File srcDir = null;
|
---|
63 | protected File destDir = null;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Adds a set of files to be deleted.
|
---|
67 | */
|
---|
68 | public void addFileset(FileSet set) {
|
---|
69 | filesets.addElement(set);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * If false, note errors to the output but keep going.
|
---|
74 | * @param failonerror true or false
|
---|
75 | */
|
---|
76 | public void setFailOnError(boolean failonerror) {
|
---|
77 | this.failonerror = failonerror;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Set the source dir to find the image files.
|
---|
82 | */
|
---|
83 | public void setSrcdir(File srcDir) {
|
---|
84 | this.srcDir = srcDir;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Set the image encoding type.
|
---|
89 | * <A HREF="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#56610">
|
---|
90 | * See this table in the JAI Programming Guide</A>.
|
---|
91 | */
|
---|
92 | public void setEncoding(String encoding) {
|
---|
93 | str_encoding = encoding;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Sets whether or not to overwrite a file if there is a naming conflict.
|
---|
98 | */
|
---|
99 | public void setOverwrite(boolean overwrite) {
|
---|
100 | this.overwrite = overwrite;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Enables Garbage Collection after each image processed. Defaults to false.
|
---|
105 | */
|
---|
106 | public void setGc(boolean gc) {
|
---|
107 | garbage_collect = gc;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Sets the destination directory for manipulated images.
|
---|
113 | * @param destDir The destination directory
|
---|
114 | */
|
---|
115 | public void setDestDir(File destDir) {
|
---|
116 | this.destDir = destDir;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Adds an ImageOperation to chain.
|
---|
121 | * @param instr The ImageOperation to append to the chain
|
---|
122 | */
|
---|
123 | public void addImageOperation(ImageOperation instr) {
|
---|
124 | instructions.add(instr);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Adds a Rotate ImageOperation to the chain
|
---|
129 | * @param instr The Rotate operation to add to the chain
|
---|
130 | * @see org.apache.tools.ant.types.optional.image.Rotate
|
---|
131 | */
|
---|
132 | public void addRotate(Rotate instr) {
|
---|
133 | instructions.add(instr);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Adds a Scale ImageOperation to the chain
|
---|
138 | * @param instr The Scale operation to add to the chain
|
---|
139 | * @see org.apache.tools.ant.types.optional.image.Scale
|
---|
140 | */
|
---|
141 | public void addScale(Scale instr) {
|
---|
142 | instructions.add(instr);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Adds a Draw ImageOperation to the chain. DrawOperation
|
---|
147 | * DataType objects can be nested inside the Draw object
|
---|
148 | * @param instr The Draw operation to add to the chain
|
---|
149 | * @see org.apache.tools.ant.types.optional.image.Draw
|
---|
150 | * @see org.apache.tools.ant.types.optional.image.DrawOperation
|
---|
151 | */
|
---|
152 | public void addDraw(Draw instr) {
|
---|
153 | instructions.add(instr);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Executes all the chained ImageOperations on the file
|
---|
158 | * specified.
|
---|
159 | * @param file The file to be processed
|
---|
160 | */
|
---|
161 | public void processFile(File file) {
|
---|
162 | try {
|
---|
163 | log("Processing File: " + file.getAbsolutePath());
|
---|
164 | FileSeekableStream input = new FileSeekableStream(file);
|
---|
165 | PlanarImage image = JAI.create("stream", input);
|
---|
166 | for (int i = 0; i < instructions.size(); i++) {
|
---|
167 | Object instr = instructions.elementAt(i);
|
---|
168 | if (instr instanceof TransformOperation) {
|
---|
169 | image = ((TransformOperation) instr).executeTransformOperation(image);
|
---|
170 | } else {
|
---|
171 | log("Not a TransformOperation: " + instr);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | input.close();
|
---|
175 |
|
---|
176 | if (str_encoding.toLowerCase().equals("jpg")) {
|
---|
177 | str_encoding = "JPEG";
|
---|
178 | } else if (str_encoding.toLowerCase().equals("tif")) {
|
---|
179 | str_encoding = "TIFF";
|
---|
180 | }
|
---|
181 |
|
---|
182 | if (destDir == null) {
|
---|
183 | destDir = srcDir;
|
---|
184 | }
|
---|
185 |
|
---|
186 | File new_file = new File(destDir.getAbsolutePath() + File.separator + file.getName());
|
---|
187 |
|
---|
188 | if ((overwrite && new_file.exists()) && (!new_file.equals(file))) {
|
---|
189 | new_file.delete();
|
---|
190 | }
|
---|
191 |
|
---|
192 | FileOutputStream stream = new FileOutputStream(new_file);
|
---|
193 |
|
---|
194 | JAI.create("encode", image, stream, str_encoding.toUpperCase(), null);
|
---|
195 | stream.flush();
|
---|
196 | stream.close();
|
---|
197 |
|
---|
198 |
|
---|
199 | } catch (IOException err) {
|
---|
200 | if (!failonerror) {
|
---|
201 | log("Error processing file: " + err);
|
---|
202 | } else {
|
---|
203 | throw new BuildException(err);
|
---|
204 | }
|
---|
205 | } catch (java.lang.RuntimeException rerr) {
|
---|
206 | if (!failonerror) {
|
---|
207 | log("Error processing file: " + rerr);
|
---|
208 | } else {
|
---|
209 | throw new BuildException(rerr);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Executes the Task
|
---|
217 | */
|
---|
218 | public void execute() throws BuildException {
|
---|
219 |
|
---|
220 | validateAttributes();
|
---|
221 |
|
---|
222 | try {
|
---|
223 | DirectoryScanner ds = null;
|
---|
224 | String[] files = null;
|
---|
225 | ArrayList filesList = new ArrayList();
|
---|
226 |
|
---|
227 |
|
---|
228 | // deal with specified srcDir
|
---|
229 | if (srcDir != null) {
|
---|
230 | ds = super.getDirectoryScanner(srcDir);
|
---|
231 |
|
---|
232 | files = ds.getIncludedFiles();
|
---|
233 | for (int i = 0; i < files.length; i++) {
|
---|
234 | filesList.add(new File(srcDir.getAbsolutePath() + File.separator + files[i]));
|
---|
235 | }
|
---|
236 | }
|
---|
237 | // deal with the filesets
|
---|
238 | for (int i = 0; i < filesets.size(); i++) {
|
---|
239 | FileSet fs = (FileSet) filesets.elementAt(i);
|
---|
240 | ds = fs.getDirectoryScanner(getProject());
|
---|
241 | files = ds.getIncludedFiles();
|
---|
242 | File fromDir = fs.getDir(getProject());
|
---|
243 | for (int j = 0; j < files.length; j++) {
|
---|
244 | filesList.add(new File(fromDir.getAbsolutePath() + File.separator + files[j]));
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (!overwrite) {
|
---|
249 | // remove any files that shouldn't be overwritten.
|
---|
250 | ArrayList filesToRemove = new ArrayList();
|
---|
251 | for (Iterator i = filesList.iterator(); i.hasNext();) {
|
---|
252 | File f = (File) i.next();
|
---|
253 | File new_file = new File(destDir.getAbsolutePath()
|
---|
254 | + File.separator + f.getName());
|
---|
255 | if (new_file.exists()) {
|
---|
256 | filesToRemove.add(f);
|
---|
257 | }
|
---|
258 | }
|
---|
259 | filesList.removeAll(filesToRemove);
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | // iterator through all the files and process them.
|
---|
264 | for (Iterator i = filesList.iterator(); i.hasNext();) {
|
---|
265 | File file = (File) i.next();
|
---|
266 |
|
---|
267 | processFile(file);
|
---|
268 | if (garbage_collect) {
|
---|
269 | System.gc();
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | } catch (Exception err) {
|
---|
275 | err.printStackTrace();
|
---|
276 | throw new BuildException(err.getMessage());
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Ensure we have a consistent and legal set of attributes, and set
|
---|
283 | * any internal flags necessary based on different combinations
|
---|
284 | * of attributes.
|
---|
285 | */
|
---|
286 | protected void validateAttributes() throws BuildException {
|
---|
287 | if (srcDir == null && filesets.size() == 0) {
|
---|
288 | throw new BuildException("Specify at least one source "
|
---|
289 | + "- a srcDir or a fileset.");
|
---|
290 | }
|
---|
291 |
|
---|
292 | if (srcDir == null && destDir == null) {
|
---|
293 | throw new BuildException("Specify the destDir, or the srcDir.");
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | }
|
---|
298 |
|
---|
299 | }
|
---|
300 |
|
---|