Source for java.awt.image.CropImageFilter

   1: /* CropImageFilter.java -- Java class for cropping image filter
   2:    Copyright (C) 1999, 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.awt.image;
  40: 
  41: import java.awt.Rectangle;
  42: import java.util.Hashtable;
  43: 
  44: /**
  45:  * Currently this filter does almost nothing and needs to be implemented.
  46:  *
  47:  * @author C. Brian Jones (cbj@gnu.org) 
  48:  */
  49: public class CropImageFilter extends ImageFilter
  50: {
  51:     int x;
  52:     int y;
  53:     int width;
  54:     int height;
  55: 
  56:     /**
  57:      * Construct a new <code>CropImageFilter</code> instance.
  58:      *
  59:      * @param x the x-coordinate location of the top-left of the cropped rectangle
  60:      * @param y the y-coordinate location of the top-left of the cropped rectangle
  61:      * @param width the width of the cropped rectangle
  62:      * @param height the height of the cropped rectangle
  63:      */
  64:     public CropImageFilter(int x, int y, int width, int height) {
  65:     this.x = x;
  66:     this.y = y;
  67:     this.width = width;
  68:     this.height = height;
  69:     }
  70: 
  71:     /**
  72:      * An <code>ImageProducer</code> indicates the size of the image
  73:      * being produced using this method.  This filter overrides this
  74:      * method in order to set the dimentions to the size of the
  75:      * cropped rectangle instead of the size of the image.
  76:      * 
  77:      * @param width the width of the image
  78:      * @param height the height of the image 
  79:      */
  80:     public void setDimensions(int width, int height)
  81:     {
  82:     consumer.setDimensions(this.width, this.height);
  83:     }
  84: 
  85:     /**
  86:      * An <code>ImageProducer</code> can set a list of properties
  87:      * associated with this image by using this method.
  88:      * <br>
  89:      * FIXME - What property is set for this class?
  90:      *
  91:      * @param props the list of properties associated with this image 
  92:      */
  93:     public void setProperties(Hashtable props)
  94:     {
  95:       props.put("filters", "CropImageFilter");
  96:     consumer.setProperties(props);
  97:     }
  98: 
  99:     /**
 100:      * This function delivers a rectangle of pixels where any
 101:      * pixel(m,n) is stored in the array as a <code>byte</code> at
 102:      * index (n * scansize + m + offset).  
 103:      *
 104:      * @param x the x coordinate of the rectangle
 105:      * @param y the y coordinate of the rectangle
 106:      * @param w the width of the rectangle
 107:      * @param h the height of the rectangle
 108:      * @param model the <code>ColorModel</code> used to translate the pixels
 109:      * @param pixels the array of pixel values
 110:      * @param offset the index of the first pixels in the <code>pixels</code> array
 111:      * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
 112:      */
 113:     public void setPixels(int x, int y, int w, int h, 
 114:        ColorModel model, byte[] pixels, int offset, int scansize)
 115:     {
 116:     Rectangle filterBounds = new Rectangle(this.x, this.y,
 117:                                            this.width, this.height);
 118:     Rectangle pixelBounds = new Rectangle(x, y, w, h);
 119: 
 120:     if (filterBounds.intersects(pixelBounds))
 121:     {
 122:         Rectangle bounds = filterBounds.intersection(pixelBounds);
 123: 
 124:         byte[] cropped = new byte[bounds.width * bounds.height];
 125:         for (int i = 0; i < bounds.height; i++)
 126:         {
 127:         int start = (bounds.y - pixelBounds.y + i) * scansize + offset;
 128: 
 129:         for (int j = 0; j < bounds.width; j++)
 130:             cropped[i * bounds.width + j] = pixels[start + bounds.x + j];
 131:         }
 132:         
 133:         consumer.setPixels(bounds.x, bounds.y,
 134:                            bounds.width, bounds.height,
 135:                            model, cropped, 0, bounds.width);
 136:     }
 137:     }
 138: 
 139:     /**
 140:      * This function delivers a rectangle of pixels where any
 141:      * pixel(m,n) is stored in the array as an <code>int</code> at
 142:      * index (n * scansize + m + offset).  
 143:      *
 144:      * @param x the x coordinate of the rectangle
 145:      * @param y the y coordinate of the rectangle
 146:      * @param w the width of the rectangle
 147:      * @param h the height of the rectangle
 148:      * @param model the <code>ColorModel</code> used to translate the pixels
 149:      * @param pixels the array of pixel values
 150:      * @param offset the index of the first pixels in the <code>pixels</code> array
 151:      * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
 152:      */
 153:     public void setPixels(int x, int y, int w, int h, 
 154:            ColorModel model, int[] pixels, int offset, int scansize)
 155:     {
 156:     Rectangle filterBounds = new Rectangle(this.x, this.y,
 157:                                            this.width, this.height);
 158:     Rectangle pixelBounds = new Rectangle(x, y, w, h);
 159: 
 160:     if (filterBounds.intersects(pixelBounds))
 161:     {
 162:         Rectangle bounds = filterBounds.intersection(pixelBounds);
 163: 
 164:         int[] cropped = new int[bounds.width * bounds.height];
 165:         for (int i = 0; i < bounds.height; i++)
 166:         {
 167:         int start = (bounds.y - pixelBounds.y + i) * scansize + offset;
 168: 
 169:         for (int j = 0; j < bounds.width; j++)
 170:             cropped[i * bounds.width + j] = pixels[start + bounds.x + j];
 171:         }
 172:         
 173:         consumer.setPixels(bounds.x, bounds.y,
 174:                            bounds.width, bounds.height,
 175:                            model, cropped, 0, bounds.width);
 176:     }
 177:     }
 178: 
 179: }