1 /* CropImageFilter.java -- Java class for cropping image filter
2 Copyright (C) 1999, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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., 59 Temple Place, Suite 330, Boston, MA
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
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. */
39 package java.awt.image;
41 import java.awt.Rectangle;
42 import java.util.Hashtable;
45 * Currently this filter does almost nothing and needs to be implemented.
47 * @author C. Brian Jones (cbj@gnu.org)
49 public class CropImageFilter extends ImageFilter
57 * Construct a new <code>CropImageFilter</code> instance.
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
64 public CropImageFilter(int x, int y, int width, int height) {
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.
77 * @param width the width of the image
78 * @param height the height of the image
80 public void setDimensions(int width, int height)
82 consumer.setDimensions(this.width, this.height);
86 * An <code>ImageProducer</code> can set a list of properties
87 * associated with this image by using this method.
89 * FIXME - What property is set for this class?
91 * @param props the list of properties associated with this image
93 public void setProperties(Hashtable props)
95 props.put("filters", "CropImageFilter");
96 consumer.setProperties(props);
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).
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
113 public void setPixels(int x, int y, int w, int h,
114 ColorModel model, byte[] pixels, int offset, int scansize)
116 Rectangle filterBounds = new Rectangle(this.x, this.y,
117 this.width, this.height);
118 Rectangle pixelBounds = new Rectangle(x, y, w, h);
120 if (filterBounds.intersects(pixelBounds))
122 Rectangle bounds = filterBounds.intersection(pixelBounds);
124 byte[] cropped = new byte[bounds.width * bounds.height];
125 for (int i = 0; i < bounds.height; i++)
127 int start = (bounds.y - pixelBounds.y + i) * scansize + offset;
129 for (int j = 0; j < bounds.width; j++)
130 cropped[i * bounds.width + j] = pixels[start + bounds.x + j];
133 consumer.setPixels(bounds.x, bounds.y,
134 bounds.width, bounds.height,
135 model, cropped, 0, bounds.width);
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).
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
153 public void setPixels(int x, int y, int w, int h,
154 ColorModel model, int[] pixels, int offset, int scansize)
156 Rectangle filterBounds = new Rectangle(this.x, this.y,
157 this.width, this.height);
158 Rectangle pixelBounds = new Rectangle(x, y, w, h);
160 if (filterBounds.intersects(pixelBounds))
162 Rectangle bounds = filterBounds.intersection(pixelBounds);
164 int[] cropped = new int[bounds.width * bounds.height];
165 for (int i = 0; i < bounds.height; i++)
167 int start = (bounds.y - pixelBounds.y + i) * scansize + offset;
169 for (int j = 0; j < bounds.width; j++)
170 cropped[i * bounds.width + j] = pixels[start + bounds.x + j];
173 consumer.setPixels(bounds.x, bounds.y,
174 bounds.width, bounds.height,
175 model, cropped, 0, bounds.width);