home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / image / cropimag.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  4.1 KB  |  145 lines

  1. /*
  2.  * @(#)CropImageFilter.java    1.2 95/08/29 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.2 08/29/95
  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.      * Construct 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.      * Pass the properties from the source object along after adding a
  65.      * property indicating the cropped region.
  66.      */
  67.     public void setProperties(Hashtable props) {
  68.     props.put("croprect", new Rectangle(cropX, cropY, cropW, cropH));
  69.     super.setProperties(props);
  70.     }
  71.  
  72.     /**
  73.      * Override the source image's dimensions and pass the dimensions
  74.      * of the rectangular cropped region to the ImageConsumer.
  75.      * @see ImageConsumer
  76.      */
  77.     public void setDimensions(int w, int h) {
  78.     consumer.setDimensions(cropW, cropH);
  79.     }
  80.     
  81.     /**
  82.      * Determine if the delivered byte pixels intersect the region to
  83.      * be extracted and pass through only that subset of pixels that
  84.      * appear in the output region.
  85.      */
  86.     public void setPixels(int x, int y, int w, int h,
  87.               ColorModel model, byte pixels[], int off,
  88.               int scansize) {
  89.     int x1 = x;
  90.     if (x1 < cropX) {
  91.         x1 = cropX;
  92.     }
  93.     int x2 = x + w;
  94.     if (x2 > cropX + cropW) {
  95.         x2 = cropX + cropW;
  96.     }
  97.     int y1 = y;
  98.     if (y1 < cropY) {
  99.         y1 = cropY;
  100.     }
  101.     int y2 = y + h;
  102.     if (y2 > cropY + cropH) {
  103.         y2 = cropY + cropH;
  104.     }
  105.     if (x1 >= x2 || y1 >= y2) {
  106.         return;
  107.     }
  108.     consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  109.                model, pixels,
  110.                off + (y1 - y) * scansize + (x1 - x), scansize);
  111.     }
  112.     
  113.     /**
  114.      * Determine if the delivered int pixels intersect the region to
  115.      * be extracted and pass through only that subset of pixels that
  116.      * appear in the output region.
  117.      */
  118.     public void setPixels(int x, int y, int w, int h,
  119.               ColorModel model, int pixels[], int off,
  120.               int scansize) {
  121.     int x1 = x;
  122.     if (x1 < cropX) {
  123.         x1 = cropX;
  124.     }
  125.     int x2 = x + w;
  126.     if (x2 > cropX + cropW) {
  127.         x2 = cropX + cropW;
  128.     }
  129.     int y1 = y;
  130.     if (y1 < cropY) {
  131.         y1 = cropY;
  132.     }
  133.     int y2 = y + h;
  134.     if (y2 > cropY + cropH) {
  135.         y2 = cropY + cropH;
  136.     }
  137.     if (x1 >= x2 || y1 >= y2) {
  138.         return;
  139.     }
  140.     consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
  141.                model, pixels,
  142.                off + (y1 - y) * scansize + (x1 - x), scansize);
  143.     }
  144. }
  145.