001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.optional.image;
019:
020: import javax.media.jai.PlanarImage;
021: import javax.media.jai.InterpolationNearest;
022: import javax.media.jai.JAI;
023: import java.awt.image.renderable.ParameterBlock;
024: import java.awt.image.BufferedImage;
025: import java.awt.Graphics2D;
026:
027: /**
028: * ImageOperation to rotate an image by a certain degree
029: *
030: * @see org.apache.tools.ant.taskdefs.optional.image.Image
031: */
032: public class Rotate extends TransformOperation implements DrawOperation {
033: private static final float HALF_CIRCLE = 180.0F;
034:
035: // CheckStyle:VisibilityModifier OFF - bc
036: protected float angle = 0.0F;
037:
038: // CheckStyle:VisibilityModifier ON
039:
040: /**
041: * Sets the angle of rotation in degrees.
042: * @param ang The angle at which to rotate the image
043: */
044: public void setAngle(String ang) {
045: angle = Float.parseFloat(ang);
046: }
047:
048: /**
049: * Rotate an image.
050: * @param image the image to rotate.
051: * @return the rotated image.
052: */
053: public PlanarImage performRotate(PlanarImage image) {
054: float tAngle = (float) (angle * (Math.PI / HALF_CIRCLE));
055: ParameterBlock pb = new ParameterBlock();
056: pb.addSource(image);
057: pb.add(0.0F);
058: pb.add(0.0F);
059: pb.add(tAngle);
060: pb.add(new InterpolationNearest());
061: return JAI.create("Rotate", pb, null);
062: }
063:
064: /**
065: * Performs the image rotation when being handled as a TransformOperation.
066: * @param image The image to perform the transformation on.
067: * @return the transformed image.
068: */
069: public PlanarImage executeTransformOperation(PlanarImage image) {
070: BufferedImage bi = null;
071: Graphics2D graphics = null;
072: for (int i = 0; i < instructions.size(); i++) {
073: ImageOperation instr = ((ImageOperation) instructions
074: .elementAt(i));
075: if (instr instanceof DrawOperation) {
076: // If this TransformOperation has DrawOperation children
077: // then Rotate the first child and return.
078: System.out.println("Execing Draws");
079: PlanarImage op = ((DrawOperation) instr)
080: .executeDrawOperation();
081: image = performRotate(op);
082: return image;
083: } else if (instr instanceof TransformOperation) {
084: bi = image.getAsBufferedImage();
085: graphics = (Graphics2D) bi.getGraphics();
086: System.out.println("Execing Transforms");
087: image = ((TransformOperation) instr)
088: .executeTransformOperation(PlanarImage
089: .wrapRenderedImage(bi));
090: bi = image.getAsBufferedImage();
091: }
092: }
093: System.out.println("Execing as TransformOperation");
094: image = performRotate(image);
095: System.out.println(image);
096: return image;
097: }
098:
099: /**
100: * Performs the image rotation when being handled as a DrawOperation.
101: * It absolutely requires that there be a DrawOperation nested beneath it,
102: * but only the FIRST DrawOperation will be handled since it can only return
103: * ONE image.
104: * @return the image.
105: */
106: public PlanarImage executeDrawOperation() {
107: for (int i = 0; i < instructions.size(); i++) {
108: ImageOperation instr = ((ImageOperation) instructions
109: .elementAt(i));
110: if (instr instanceof DrawOperation) {
111: // If this TransformOperation has DrawOperation children
112: // then Rotate the first child and return.
113: PlanarImage op = ((DrawOperation) instr)
114: .executeDrawOperation();
115: op = performRotate(op);
116: return op;
117: }
118: }
119: return null;
120: }
121:
122: }
|