1. /*
  2. * @(#)CropImageFilter.java 1.9 01/11/29
  3. *
  4. * Copyright 2002 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.9 11/29/01
  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. */
  50. public void setProperties(Hashtable props) {
  51. props = (Hashtable) props.clone();
  52. props.put("croprect", new Rectangle(cropX, cropY, cropW, cropH));
  53. super.setProperties(props);
  54. }
  55. /**
  56. * Override the source image's dimensions and pass the dimensions
  57. * of the rectangular cropped region to the ImageConsumer.
  58. * @see ImageConsumer
  59. */
  60. public void setDimensions(int w, int h) {
  61. consumer.setDimensions(cropW, cropH);
  62. }
  63. /**
  64. * Determine whether the delivered byte pixels intersect the region to
  65. * be extracted and passes through only that subset of pixels that
  66. * appear in the output region.
  67. */
  68. public void setPixels(int x, int y, int w, int h,
  69. ColorModel model, byte pixels[], int off,
  70. int scansize) {
  71. int x1 = x;
  72. if (x1 < cropX) {
  73. x1 = cropX;
  74. }
  75. int x2 = x + w;
  76. if (x2 > cropX + cropW) {
  77. x2 = cropX + cropW;
  78. }
  79. int y1 = y;
  80. if (y1 < cropY) {
  81. y1 = cropY;
  82. }
  83. int y2 = y + h;
  84. if (y2 > cropY + cropH) {
  85. y2 = cropY + cropH;
  86. }
  87. if (x1 >= x2 || y1 >= y2) {
  88. return;
  89. }
  90. consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  91. model, pixels,
  92. off + (y1 - y) * scansize + (x1 - x), scansize);
  93. }
  94. /**
  95. * Determine if the delivered int pixels intersect the region to
  96. * be extracted and pass through only that subset of pixels that
  97. * appear in the output region.
  98. */
  99. public void setPixels(int x, int y, int w, int h,
  100. ColorModel model, int pixels[], int off,
  101. int scansize) {
  102. int x1 = x;
  103. if (x1 < cropX) {
  104. x1 = cropX;
  105. }
  106. int x2 = x + w;
  107. if (x2 > cropX + cropW) {
  108. x2 = cropX + cropW;
  109. }
  110. int y1 = y;
  111. if (y1 < cropY) {
  112. y1 = cropY;
  113. }
  114. int y2 = y + h;
  115. if (y2 > cropY + cropH) {
  116. y2 = cropY + cropH;
  117. }
  118. if (x1 >= x2 || y1 >= y2) {
  119. return;
  120. }
  121. consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  122. model, pixels,
  123. off + (y1 - y) * scansize + (x1 - x), scansize);
  124. }
  125. }