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 org.apache.tools.ant.types.EnumeratedAttribute;
  19. import javax.media.jai.JAI;
  20. import javax.media.jai.PlanarImage;
  21. import java.awt.image.BufferedImage;
  22. import java.awt.image.renderable.ParameterBlock;
  23. /**
  24. *
  25. * @see org.apache.tools.ant.taskdefs.optional.image.Image
  26. */
  27. public class Scale extends TransformOperation implements DrawOperation {
  28. private String width_str = "100%";
  29. private String height_str = "100%";
  30. private boolean x_percent = true;
  31. private boolean y_percent = true;
  32. private String proportions = "ignore";
  33. public static class ProportionsAttribute extends EnumeratedAttribute {
  34. public String[] getValues() {
  35. return new String[] {"ignore", "width", "height", "cover", "fit"};
  36. }
  37. }
  38. /**
  39. * Sets the behaviour regarding the image proportions.
  40. */
  41. public void setProportions(ProportionsAttribute pa) {
  42. proportions = pa.getValue();
  43. }
  44. /**
  45. * Sets the width of the image, either as an integer or a %. Defaults to 100%.
  46. */
  47. public void setWidth(String width) {
  48. width_str = width;
  49. }
  50. /**
  51. * Sets the height of the image, either as an integer or a %. Defaults to 100%.
  52. */
  53. public void setHeight(String height) {
  54. height_str = height;
  55. }
  56. public float getWidth() {
  57. float width = 0.0F;
  58. int perc_index = width_str.indexOf('%');
  59. if (perc_index > 0) {
  60. width = Float.parseFloat(width_str.substring(0, perc_index));
  61. x_percent = true;
  62. return width / 100;
  63. } else {
  64. x_percent = false;
  65. return Float.parseFloat(width_str);
  66. }
  67. }
  68. public float getHeight() {
  69. int perc_index = height_str.indexOf('%');
  70. if (perc_index > 0) {
  71. float height = Float.parseFloat(height_str.substring(0, perc_index));
  72. y_percent = true;
  73. return height / 100;
  74. } else {
  75. y_percent = false;
  76. return Float.parseFloat(height_str);
  77. }
  78. }
  79. public PlanarImage performScale(PlanarImage image) {
  80. ParameterBlock pb = new ParameterBlock();
  81. pb.addSource(image);
  82. float x_fl = getWidth();
  83. float y_fl = getHeight();
  84. if (!x_percent) {
  85. x_fl = (x_fl / image.getWidth());
  86. }
  87. if (!y_percent) {
  88. y_fl = (y_fl / image.getHeight());
  89. }
  90. if ("width".equals(proportions)) {
  91. y_fl = x_fl;
  92. } else if ("height".equals(proportions)) {
  93. x_fl = y_fl;
  94. } else if ("fit".equals(proportions)) {
  95. x_fl = y_fl = Math.min(x_fl, y_fl);
  96. } else if ("cover".equals(proportions)) {
  97. x_fl = y_fl = Math.max(x_fl, y_fl);
  98. }
  99. pb.add(new Float(x_fl));
  100. pb.add(new Float(y_fl));
  101. log("\tScaling to " + (x_fl * 100) + "% x " + (y_fl * 100) + "%");
  102. return JAI.create("scale", pb);
  103. }
  104. public PlanarImage executeTransformOperation(PlanarImage image) {
  105. BufferedImage bi = null;
  106. for (int i = 0; i < instructions.size(); i++) {
  107. ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
  108. if (instr instanceof DrawOperation) {
  109. return performScale(image);
  110. } else if (instr instanceof TransformOperation) {
  111. bi = image.getAsBufferedImage();
  112. image = ((TransformOperation) instr).executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
  113. bi = image.getAsBufferedImage();
  114. }
  115. }
  116. return performScale(image);
  117. }
  118. public PlanarImage executeDrawOperation() {
  119. for (int i = 0; i < instructions.size(); i++) {
  120. ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
  121. if (instr instanceof DrawOperation) {
  122. PlanarImage image = null;
  123. // If this TransformOperation has DrawOperation children
  124. // then Rotate the first child and return.
  125. performScale(image);
  126. return image;
  127. }
  128. }
  129. return null;
  130. }
  131. }