home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / CropImageFilter.java < prev    next >
Text File  |  1997-08-30  |  4KB  |  149 lines

  1. /*
  2.  * @(#)CropImageFilter.java    1.5 96/11/23
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.awt.image;
  24.  
  25. import java.awt.image.ImageConsumer;
  26. import java.awt.image.ColorModel;
  27. import java.util.Hashtable;
  28. import java.awt.Rectangle;
  29.  
  30. /**
  31.  * An ImageFilter class for cropping images.
  32.  * This class extends the basic ImageFilter Class to extract a given
  33.  * rectangular region of an existing Image and provide a source for a
  34.  * new image containing just the extracted region.  It is meant to
  35.  * be used in conjunction with a FilteredImageSource object to produce
  36.  * cropped versions of existing images.
  37.  *
  38.  * @see FilteredImageSource
  39.  * @see ImageFilter
  40.  *
  41.  * @version    1.5 11/23/96
  42.  * @author     Jim Graham
  43.  */
  44. public class CropImageFilter extends ImageFilter {
  45.     int cropX;
  46.     int cropY;
  47.     int cropW;
  48.     int cropH;
  49.     
  50.     /**
  51.      * Constructs a CropImageFilter that extracts the absolute rectangular
  52.      * region of pixels from its source Image as specified by the x, y,
  53.      * w, and h parameters.
  54.      * @param x the x location of the top of the rectangle to be extracted
  55.      * @param y the y location of the top of the rectangle to be extracted
  56.      * @param w the width of the rectangle to be extracted
  57.      * @param h the height of the rectangle to be extracted
  58.      */
  59.     public CropImageFilter(int x, int y, int w, int h) {
  60.     cropX = x;
  61.     cropY = y;
  62.     cropW = w;
  63.     cropH = h;
  64.     }
  65.  
  66.     /**
  67.      * Passes along  the properties from the source object after adding a
  68.      * property indicating the cropped region.
  69.      */
  70.     public void setProperties(Hashtable props) {
  71.     props = (Hashtable) props.clone();
  72.     props.put("croprect", new Rectangle(cropX, cropY, cropW, cropH));
  73.     super.setProperties(props);
  74.     }
  75.  
  76.     /**
  77.      * Override the source image's dimensions and pass the dimensions
  78.      * of the rectangular cropped region to the ImageConsumer.
  79.      * @see ImageConsumer
  80.      */
  81.     public void setDimensions(int w, int h) {
  82.     consumer.setDimensions(cropW, cropH);
  83.     }
  84.    
  85.     /**
  86.      * Determine whether the delivered byte pixels intersect the region to
  87.      * be extracted and passes through only that subset of pixels that
  88.      * appear in the output region.
  89.      */
  90.     public void setPixels(int x, int y, int w, int h,
  91.               ColorModel model, byte pixels[], int off,
  92.               int scansize) {
  93.     int x1 = x;
  94.     if (x1 < cropX) {
  95.         x1 = cropX;
  96.     }
  97.     int x2 = x + w;
  98.     if (x2 > cropX + cropW) {
  99.         x2 = cropX + cropW;
  100.     }
  101.     int y1 = y;
  102.     if (y1 < cropY) {
  103.         y1 = cropY;
  104.     }
  105.     int y2 = y + h;
  106.     if (y2 > cropY + cropH) {
  107.         y2 = cropY + cropH;
  108.     }
  109.     if (x1 >= x2 || y1 >= y2) {
  110.         return;
  111.     }
  112.     consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  113.                model, pixels,
  114.                off + (y1 - y) * scansize + (x1 - x), scansize);
  115.     }
  116.     
  117.     /**
  118.      * Determine if the delivered int pixels intersect the region to
  119.      * be extracted and pass through only that subset of pixels that
  120.      * appear in the output region.
  121.      */
  122.     public void setPixels(int x, int y, int w, int h,
  123.               ColorModel model, int pixels[], int off,
  124.               int scansize) {
  125.     int x1 = x;
  126.     if (x1 < cropX) {
  127.         x1 = cropX;
  128.     }
  129.     int x2 = x + w;
  130.     if (x2 > cropX + cropW) {
  131.         x2 = cropX + cropW;
  132.     }
  133.     int y1 = y;
  134.     if (y1 < cropY) {
  135.         y1 = cropY;
  136.     }
  137.     int y2 = y + h;
  138.     if (y2 > cropY + cropH) {
  139.         y2 = cropY + cropH;
  140.     }
  141.     if (x1 >= x2 || y1 >= y2) {
  142.         return;
  143.     }
  144.     consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  145.                model, pixels,
  146.                off + (y1 - y) * scansize + (x1 - x), scansize);
  147.     }
  148. }
  149.