1. /*
  2. * @(#)CropImageFilter.java 1.13 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.image;
  8. import java.awt.image.ImageConsumer;
  9. import java.awt.image.ColorModel;
  10. import java.util.Hashtable;
  11. import java.awt.Rectangle;
  12. /**
  13. * An ImageFilter class for cropping images.
  14. * This class extends the basic ImageFilter Class to extract a given
  15. * rectangular region of an existing Image and provide a source for a
  16. * new image containing just the extracted region. It is meant to
  17. * be used in conjunction with a FilteredImageSource object to produce
  18. * cropped versions of existing images.
  19. *
  20. * @see FilteredImageSource
  21. * @see ImageFilter
  22. *
  23. * @version 1.13 01/23/03
  24. * @author Jim Graham
  25. */
  26. public class CropImageFilter extends ImageFilter {
  27. int cropX;
  28. int cropY;
  29. int cropW;
  30. int cropH;
  31. /**
  32. * Constructs a CropImageFilter that extracts the absolute rectangular
  33. * region of pixels from its source Image as specified by the x, y,
  34. * w, and h parameters.
  35. * @param x the x location of the top of the rectangle to be extracted
  36. * @param y the y location of the top of the rectangle to be extracted
  37. * @param w the width of the rectangle to be extracted
  38. * @param h the height of the rectangle to be extracted
  39. */
  40. public CropImageFilter(int x, int y, int w, int h) {
  41. cropX = x;
  42. cropY = y;
  43. cropW = w;
  44. cropH = h;
  45. }
  46. /**
  47. * Passes along the properties from the source object after adding a
  48. * property indicating the cropped region.
  49. * <p>
  50. * Note: This method is intended to be called by the
  51. * <code>ImageProducer</code> of the <code>Image</code> whose pixels
  52. * are being filtered. Developers using
  53. * this class to filter pixels from an image should avoid calling
  54. * this method directly since that operation could interfere
  55. * with the filtering operation.
  56. */
  57. public void setProperties(Hashtable props) {
  58. props = (Hashtable) props.clone();
  59. props.put("croprect", new Rectangle(cropX, cropY, cropW, cropH));
  60. super.setProperties(props);
  61. }
  62. /**
  63. * Override the source image's dimensions and pass the dimensions
  64. * of the rectangular cropped region to the ImageConsumer.
  65. * <p>
  66. * Note: This method is intended to be called by the
  67. * <code>ImageProducer</code> of the <code>Image</code> whose
  68. * pixels are being filtered. Developers using
  69. * this class to filter pixels from an image should avoid calling
  70. * this method directly since that operation could interfere
  71. * with the filtering operation.
  72. * @see ImageConsumer
  73. */
  74. public void setDimensions(int w, int h) {
  75. consumer.setDimensions(cropW, cropH);
  76. }
  77. /**
  78. * Determine whether the delivered byte pixels intersect the region to
  79. * be extracted and passes through only that subset of pixels that
  80. * appear in the output region.
  81. * <p>
  82. * Note: This method is intended to be called by the
  83. * <code>ImageProducer</code> of the <code>Image</code> whose
  84. * pixels are being filtered. Developers using
  85. * this class to filter pixels from an image should avoid calling
  86. * this method directly since that operation could interfere
  87. * with the filtering operation.
  88. */
  89. public void setPixels(int x, int y, int w, int h,
  90. ColorModel model, byte pixels[], int off,
  91. int scansize) {
  92. int x1 = x;
  93. if (x1 < cropX) {
  94. x1 = cropX;
  95. }
  96. int x2 = x + w;
  97. if (x2 > cropX + cropW) {
  98. x2 = cropX + cropW;
  99. }
  100. int y1 = y;
  101. if (y1 < cropY) {
  102. y1 = cropY;
  103. }
  104. int y2 = y + h;
  105. if (y2 > cropY + cropH) {
  106. y2 = cropY + cropH;
  107. }
  108. if (x1 >= x2 || y1 >= y2) {
  109. return;
  110. }
  111. consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  112. model, pixels,
  113. off + (y1 - y) * scansize + (x1 - x), scansize);
  114. }
  115. /**
  116. * Determine if the delivered int pixels intersect the region to
  117. * be extracted and pass through only that subset of pixels that
  118. * appear in the output region.
  119. * <p>
  120. * Note: This method is intended to be called by the
  121. * <code>ImageProducer</code> of the <code>Image</code> whose
  122. * pixels are being filtered. Developers using
  123. * this class to filter pixels from an image should avoid calling
  124. * this method directly since that operation could interfere
  125. * with the filtering operation.
  126. */
  127. public void setPixels(int x, int y, int w, int h,
  128. ColorModel model, int pixels[], int off,
  129. int scansize) {
  130. int x1 = x;
  131. if (x1 < cropX) {
  132. x1 = cropX;
  133. }
  134. int x2 = x + w;
  135. if (x2 > cropX + cropW) {
  136. x2 = cropX + cropW;
  137. }
  138. int y1 = y;
  139. if (y1 < cropY) {
  140. y1 = cropY;
  141. }
  142. int y2 = y + h;
  143. if (y2 > cropY + cropH) {
  144. y2 = cropY + cropH;
  145. }
  146. if (x1 >= x2 || y1 >= y2) {
  147. return;
  148. }
  149. consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  150. model, pixels,
  151. off + (y1 - y) * scansize + (x1 - x), scansize);
  152. }
  153. }