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:       if (consumer != null)
  83:         consumer.setDimensions(this.width, this.height);
  84:     }
  85: 
  86:     /**
  87:      * An <code>ImageProducer</code> can set a list of properties
  88:      * associated with this image by using this method.
  89:      * <br>
  90:      * FIXME - What property is set for this class?
  91:      *
  92:      * @param props the list of properties associated with this image
  93:      */
  94:     public void setProperties(Hashtable<?, ?> props)
  95:     {
  96:       Hashtable<Object, Object> prop2 = (Hashtable<Object, Object>) props;
  97:       prop2.put("filters", "CropImageFilter");
  98:       if (consumer != null)
  99:         consumer.setProperties(prop2);
 100:     }
 101: 
 102:     /**
 103:      * This function delivers a rectangle of pixels where any
 104:      * pixel(m,n) is stored in the array as a <code>byte</code> at
 105:      * index (n * scansize + m + offset).
 106:      *
 107:      * @param x the x coordinate of the rectangle
 108:      * @param y the y coordinate of the rectangle
 109:      * @param w the width of the rectangle
 110:      * @param h the height of the rectangle
 111:      * @param model the <code>ColorModel</code> used to translate the pixels
 112:      * @param pixels the array of pixel values
 113:      * @param offset the index of the first pixels in the <code>pixels</code> array
 114:      * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
 115:      */
 116:     public void setPixels(int x, int y, int w, int h,
 117:            ColorModel model, byte[] pixels, int offset, int scansize)
 118:     {
 119:         Rectangle filterBounds = new Rectangle(this.x, this.y,
 120:                                                this.width, this.height);
 121:         Rectangle pixelBounds = new Rectangle(x, y, w, h);
 122: 
 123:         if (filterBounds.intersects(pixelBounds))
 124:         {
 125:             Rectangle bounds = filterBounds.intersection(pixelBounds);
 126: 
 127:             byte[] cropped = new byte[bounds.width * bounds.height];
 128:             for (int i = 0; i < bounds.height; i++)
 129:             {
 130:                 int start = (bounds.y - pixelBounds.y + i) * scansize + offset;
 131: 
 132:                 for (int j = 0; j < bounds.width; j++)
 133:                     cropped[i * bounds.width + j] = pixels[start + bounds.x + j];
 134:             }
 135: 
 136:             if (consumer != null)
 137:               consumer.setPixels(0, 0,
 138:                                  bounds.width, bounds.height,
 139:                                  model, cropped, 0, bounds.width);
 140:         }
 141:     }
 142: 
 143:     /**
 144:      * This function delivers a rectangle of pixels where any
 145:      * pixel(m,n) is stored in the array as an <code>int</code> at
 146:      * index (n * scansize + m + offset).
 147:      *
 148:      * @param x the x coordinate of the rectangle
 149:      * @param y the y coordinate of the rectangle
 150:      * @param w the width of the rectangle
 151:      * @param h the height of the rectangle
 152:      * @param model the <code>ColorModel</code> used to translate the pixels
 153:      * @param pixels the array of pixel values
 154:      * @param offset the index of the first pixels in the <code>pixels</code> array
 155:      * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
 156:      */
 157:     public void setPixels(int x, int y, int w, int h,
 158:            ColorModel model, int[] pixels, int offset, int scansize)
 159:     {
 160:         Rectangle filterBounds = new Rectangle(this.x, this.y,
 161:                                                this.width, this.height);
 162:         Rectangle pixelBounds = new Rectangle(x, y, w, h);
 163: 
 164:         if (filterBounds.intersects(pixelBounds))
 165:         {
 166:             Rectangle bounds = filterBounds.intersection(pixelBounds);
 167: 
 168:             int[] cropped = new int[bounds.width * bounds.height];
 169:             for (int i = 0; i < bounds.height; i++)
 170:             {
 171:                 int start = (bounds.y - pixelBounds.y + i) * scansize + offset;
 172: 
 173:                 for (int j = 0; j < bounds.width; j++)
 174:                     cropped[i * bounds.width + j] = pixels[start + bounds.x + j];
 175:             }
 176: 
 177:             if (consumer != null)
 178:               consumer.setPixels(0, 0,
 179:                                  bounds.width, bounds.height,
 180:                                  model, cropped, 0, bounds.width);
 181:         }
 182:     }
 183: 
 184: }