FileDocCategorySizeDatePackage
Image.javaAPI DocApache Ant 1.7010634Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs.optional.image

Image

public class Image extends org.apache.tools.ant.taskdefs.MatchingTask
A MatchingTask which relies on JAI (Java Advanced Imaging) to perform image manipulation operations on existing images. The operations are represented as ImageOperation DataType objects. The operations are arranged to conform to the Chaining Model of JAI. Check out the JAI Programming Guide.
see
org.apache.tools.ant.types.optional.image.ImageOperation
see
org.apache.tools.ant.types.DataType

Fields Summary
protected Vector
instructions
protected boolean
overwrite
protected Vector
filesets
protected File
srcDir
protected File
destDir
protected String
str_encoding
protected boolean
garbage_collect
private boolean
failonerror
Constructors Summary
Methods Summary
public voidadd(org.apache.tools.ant.types.optional.image.ImageOperation instr)
Add an ImageOperation to chain.

param
instr The ImageOperation to append to the chain.
since
Ant 1.7

        addImageOperation(instr);
    
public voidaddDraw(org.apache.tools.ant.types.optional.image.Draw instr)
Add a Draw ImageOperation to the chain. DrawOperation DataType objects can be nested inside the Draw object.

param
instr The Draw operation to add to the chain.
see
org.apache.tools.ant.types.optional.image.Draw
see
org.apache.tools.ant.types.optional.image.DrawOperation

        instructions.add(instr);
    
public voidaddFileset(org.apache.tools.ant.types.FileSet set)
Add a set of files to be deleted.

param
set the FileSet to add.


    // CheckStyle:MemberNameCheck ON

    // CheckStyle:VisibilityModifier ON

                       
        
        filesets.addElement(set);
    
public voidaddImageOperation(org.apache.tools.ant.types.optional.image.ImageOperation instr)
Add an ImageOperation to chain.

param
instr The ImageOperation to append to the chain.

        instructions.add(instr);
    
public voidaddRotate(org.apache.tools.ant.types.optional.image.Rotate instr)
Add a Rotate ImageOperation to the chain.

param
instr The Rotate operation to add to the chain.
see
org.apache.tools.ant.types.optional.image.Rotate

        instructions.add(instr);
    
public voidaddScale(org.apache.tools.ant.types.optional.image.Scale instr)
Add a Scale ImageOperation to the chain.

param
instr The Scale operation to add to the chain.
see
org.apache.tools.ant.types.optional.image.Scale

        instructions.add(instr);
    
public voidexecute()
Executes the Task.

throws
BuildException on error.


        validateAttributes();

        try {
            DirectoryScanner ds = null;
            String[] files = null;
            ArrayList filesList = new ArrayList();

            // deal with specified srcDir
            if (srcDir != null) {
                ds = super.getDirectoryScanner(srcDir);

                files = ds.getIncludedFiles();
                for (int i = 0; i < files.length; i++) {
                    filesList.add(new File(srcDir, files[i]));
                }
            }
            // deal with the filesets
            for (int i = 0; i < filesets.size(); i++) {
                FileSet fs = (FileSet) filesets.elementAt(i);
                ds = fs.getDirectoryScanner(getProject());
                files = ds.getIncludedFiles();
                File fromDir = fs.getDir(getProject());
                for (int j = 0; j < files.length; j++) {
                    filesList.add(new File(fromDir, files[j]));
                }
            }
            if (!overwrite) {
                // remove any files that shouldn't be overwritten.
                ArrayList filesToRemove = new ArrayList();
                for (Iterator i = filesList.iterator(); i.hasNext();) {
                    File f = (File) i.next();
                    File newFile = new File(destDir, f.getName());
                    if (newFile.exists()) {
                        filesToRemove.add(f);
                    }
                }
                filesList.removeAll(filesToRemove);
            }
            // iterator through all the files and process them.
            for (Iterator i = filesList.iterator(); i.hasNext();) {
                File file = (File) i.next();

                processFile(file);
                if (garbage_collect) {
                    System.gc();
                }
            }
        } catch (Exception err) {
            err.printStackTrace();
            throw new BuildException(err.getMessage());
        }
    
public voidprocessFile(java.io.File file)
Executes all the chained ImageOperations on the file specified.

param
file The file to be processed.

        try {
            log("Processing File: " + file.getAbsolutePath());
            FileSeekableStream input = new FileSeekableStream(file);
            PlanarImage image = JAI.create("stream", input);
            for (int i = 0; i < instructions.size(); i++) {
                Object instr = instructions.elementAt(i);
                if (instr instanceof TransformOperation) {
                    image = ((TransformOperation) instr)
                        .executeTransformOperation(image);
                } else {
                    log("Not a TransformOperation: " + instr);
                }
            }
            input.close();

            if (str_encoding.toLowerCase().equals("jpg")) {
                str_encoding = "JPEG";
            } else if (str_encoding.toLowerCase().equals("tif")) {
                str_encoding = "TIFF";
            }
            if (destDir == null) {
                destDir = srcDir;
            }
            File newFile = new File(destDir, file.getName());

            if ((overwrite && newFile.exists()) && (!newFile.equals(file))) {
                newFile.delete();
            }
            FileOutputStream stream = new FileOutputStream(newFile);

            JAI.create("encode", image, stream, str_encoding.toUpperCase(),
                       null);
            stream.flush();
            stream.close();
        } catch (IOException err) {
            if (!failonerror) {
                log("Error processing file:  " + err);
            } else {
                throw new BuildException(err);
            }
        } catch (java.lang.RuntimeException rerr) {
            if (!failonerror) {
                log("Error processing file:  " + rerr);
            } else {
                throw new BuildException(rerr);
            }
        }
    
public voidsetDestDir(java.io.File destDir)
Set the destination directory for manipulated images.

param
destDir The destination directory.

        this.destDir = destDir;
    
public voidsetEncoding(java.lang.String encoding)
Set the image encoding type. See this table in the JAI Programming Guide.

param
encoding the String image encoding.

        str_encoding = encoding;
    
public voidsetFailOnError(boolean failonerror)
Set whether to fail on error. If false, note errors to the output but keep going.

param
failonerror true or false.

        this.failonerror = failonerror;
    
public voidsetGc(boolean gc)
Set whether to invoke Garbage Collection after each image processed. Defaults to false.

param
gc whether to invoke the garbage collector.

        garbage_collect = gc;
    
public voidsetOverwrite(boolean overwrite)
Set whether to overwrite a file if there is a naming conflict.

param
overwrite whether to overwrite.

        this.overwrite = overwrite;
    
public voidsetSrcdir(java.io.File srcDir)
Set the source dir to find the image files.

param
srcDir the directory in which the image files reside.

        this.srcDir = srcDir;
    
protected voidvalidateAttributes()
Ensure we have a consistent and legal set of attributes, and set any internal flags necessary based on different combinations of attributes.

throws
BuildException on error.

        if (srcDir == null && filesets.size() == 0) {
            throw new BuildException("Specify at least one source"
                                     + "--a srcDir or a fileset.");
        }
        if (srcDir == null && destDir == null) {
            throw new BuildException("Specify the destDir, or the srcDir.");
        }