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