home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / CropImageFilter.java < prev    next >
Text File  |  1996-05-03  |  4KB  |  146 lines

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