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