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.types.optional.image;
|
---|
18 |
|
---|
19 | import javax.media.jai.PlanarImage;
|
---|
20 | import javax.media.jai.InterpolationNearest;
|
---|
21 | import javax.media.jai.JAI;
|
---|
22 | import java.awt.image.renderable.ParameterBlock;
|
---|
23 | import java.awt.image.BufferedImage;
|
---|
24 | import java.awt.Graphics2D;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * ImageOperation to rotate an image by a certain degree
|
---|
28 | *
|
---|
29 | * @see org.apache.tools.ant.taskdefs.optional.image.Image
|
---|
30 | */
|
---|
31 | public class Rotate extends TransformOperation implements DrawOperation {
|
---|
32 | protected float angle = 0.0F;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Sets the angle of rotation in degrees.
|
---|
36 | * @param ang The angle at which to rotate the image
|
---|
37 | */
|
---|
38 | public void setAngle(String ang) {
|
---|
39 | angle = Float.parseFloat(ang);
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | public PlanarImage performRotate(PlanarImage image) {
|
---|
44 | float t_angle = (float) (angle * (Math.PI / 180.0F));
|
---|
45 | ParameterBlock pb = new ParameterBlock();
|
---|
46 | pb.addSource(image);
|
---|
47 | pb.add(0.0F);
|
---|
48 | pb.add(0.0F);
|
---|
49 | pb.add(t_angle);
|
---|
50 | pb.add(new InterpolationNearest());
|
---|
51 | return JAI.create("Rotate", pb, null);
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Performs the image rotation when being handled as a TransformOperation.
|
---|
57 | * @param image The image to perform the transformation on.
|
---|
58 | */
|
---|
59 | public PlanarImage executeTransformOperation(PlanarImage image) {
|
---|
60 | BufferedImage bi = null;
|
---|
61 | Graphics2D graphics = null;
|
---|
62 | for (int i = 0; i < instructions.size(); i++) {
|
---|
63 | ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
|
---|
64 | if (instr instanceof DrawOperation) {
|
---|
65 | // If this TransformOperation has DrawOperation children
|
---|
66 | // then Rotate the first child and return.
|
---|
67 | System.out.println("Execing Draws");
|
---|
68 | PlanarImage op = ((DrawOperation) instr).executeDrawOperation();
|
---|
69 | image = performRotate(op);
|
---|
70 | return image;
|
---|
71 | } else if (instr instanceof TransformOperation) {
|
---|
72 | bi = image.getAsBufferedImage();
|
---|
73 | graphics = (Graphics2D) bi.getGraphics();
|
---|
74 | System.out.println("Execing Transforms");
|
---|
75 | image = ((TransformOperation) instr).executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
|
---|
76 | bi = image.getAsBufferedImage();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | System.out.println("Execing as TransformOperation");
|
---|
80 | image = performRotate(image);
|
---|
81 | System.out.println(image);
|
---|
82 | return image;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Performs the image rotation when being handled as a DrawOperation.
|
---|
87 | * It absolutely requires that there be a DrawOperation nested beneath it,
|
---|
88 | * but only the FIRST DrawOperation will be handled since it can only return
|
---|
89 | * ONE image.
|
---|
90 | */
|
---|
91 | public PlanarImage executeDrawOperation() {
|
---|
92 | for (int i = 0; i < instructions.size(); i++) {
|
---|
93 | ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
|
---|
94 | if (instr instanceof DrawOperation) {
|
---|
95 | // If this TransformOperation has DrawOperation children
|
---|
96 | // then Rotate the first child and return.
|
---|
97 | PlanarImage op = ((DrawOperation) instr).executeDrawOperation();
|
---|
98 | op = performRotate(op);
|
---|
99 | return op;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return null;
|
---|
103 | }
|
---|
104 |
|
---|
105 | }
|
---|