001 /*
002 * Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.image;
027
028 import java.awt.image.ImageConsumer;
029 import java.awt.image.ColorModel;
030 import java.util.Hashtable;
031 import java.awt.Rectangle;
032
033 /**
034 * An ImageFilter class for cropping images.
035 * This class extends the basic ImageFilter Class to extract a given
036 * rectangular region of an existing Image and provide a source for a
037 * new image containing just the extracted region. It is meant to
038 * be used in conjunction with a FilteredImageSource object to produce
039 * cropped versions of existing images.
040 *
041 * @see FilteredImageSource
042 * @see ImageFilter
043 *
044 * @version 1.25 05/05/07
045 * @author Jim Graham
046 */
047 public class CropImageFilter extends ImageFilter {
048 int cropX;
049 int cropY;
050 int cropW;
051 int cropH;
052
053 /**
054 * Constructs a CropImageFilter that extracts the absolute rectangular
055 * region of pixels from its source Image as specified by the x, y,
056 * w, and h parameters.
057 * @param x the x location of the top of the rectangle to be extracted
058 * @param y the y location of the top of the rectangle to be extracted
059 * @param w the width of the rectangle to be extracted
060 * @param h the height of the rectangle to be extracted
061 */
062 public CropImageFilter(int x, int y, int w, int h) {
063 cropX = x;
064 cropY = y;
065 cropW = w;
066 cropH = h;
067 }
068
069 /**
070 * Passes along the properties from the source object after adding a
071 * property indicating the cropped region.
072 * This method invokes <code>super.setProperties</code>,
073 * which might result in additional properties being added.
074 * <p>
075 * Note: This method is intended to be called by the
076 * <code>ImageProducer</code> of the <code>Image</code> whose pixels
077 * are being filtered. Developers using
078 * this class to filter pixels from an image should avoid calling
079 * this method directly since that operation could interfere
080 * with the filtering operation.
081 */
082 public void setProperties(Hashtable<?, ?> props) {
083 Hashtable<Object, Object> p = (Hashtable<Object, Object>) props
084 .clone();
085 p.put("croprect", new Rectangle(cropX, cropY, cropW, cropH));
086 super .setProperties(p);
087 }
088
089 /**
090 * Override the source image's dimensions and pass the dimensions
091 * of the rectangular cropped region to the ImageConsumer.
092 * <p>
093 * Note: This method is intended to be called by the
094 * <code>ImageProducer</code> of the <code>Image</code> whose
095 * pixels are being filtered. Developers using
096 * this class to filter pixels from an image should avoid calling
097 * this method directly since that operation could interfere
098 * with the filtering operation.
099 * @see ImageConsumer
100 */
101 public void setDimensions(int w, int h) {
102 consumer.setDimensions(cropW, cropH);
103 }
104
105 /**
106 * Determine whether the delivered byte pixels intersect the region to
107 * be extracted and passes through only that subset of pixels that
108 * appear in the output region.
109 * <p>
110 * Note: This method is intended to be called by the
111 * <code>ImageProducer</code> of the <code>Image</code> whose
112 * pixels are being filtered. Developers using
113 * this class to filter pixels from an image should avoid calling
114 * this method directly since that operation could interfere
115 * with the filtering operation.
116 */
117 public void setPixels(int x, int y, int w, int h, ColorModel model,
118 byte pixels[], int off, int scansize) {
119 int x1 = x;
120 if (x1 < cropX) {
121 x1 = cropX;
122 }
123 int x2 = addWithoutOverflow(x, w);
124 if (x2 > cropX + cropW) {
125 x2 = cropX + cropW;
126 }
127 int y1 = y;
128 if (y1 < cropY) {
129 y1 = cropY;
130 }
131
132 int y2 = addWithoutOverflow(y, h);
133 if (y2 > cropY + cropH) {
134 y2 = cropY + cropH;
135 }
136 if (x1 >= x2 || y1 >= y2) {
137 return;
138 }
139 consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1),
140 (y2 - y1), model, pixels, off + (y1 - y) * scansize
141 + (x1 - x), scansize);
142 }
143
144 /**
145 * Determine if the delivered int pixels intersect the region to
146 * be extracted and pass through only that subset of pixels that
147 * appear in the output region.
148 * <p>
149 * Note: This method is intended to be called by the
150 * <code>ImageProducer</code> of the <code>Image</code> whose
151 * pixels are being filtered. Developers using
152 * this class to filter pixels from an image should avoid calling
153 * this method directly since that operation could interfere
154 * with the filtering operation.
155 */
156 public void setPixels(int x, int y, int w, int h, ColorModel model,
157 int pixels[], int off, int scansize) {
158 int x1 = x;
159 if (x1 < cropX) {
160 x1 = cropX;
161 }
162 int x2 = addWithoutOverflow(x, w);
163 if (x2 > cropX + cropW) {
164 x2 = cropX + cropW;
165 }
166 int y1 = y;
167 if (y1 < cropY) {
168 y1 = cropY;
169 }
170
171 int y2 = addWithoutOverflow(y, h);
172 if (y2 > cropY + cropH) {
173 y2 = cropY + cropH;
174 }
175 if (x1 >= x2 || y1 >= y2) {
176 return;
177 }
178 consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1),
179 (y2 - y1), model, pixels, off + (y1 - y) * scansize
180 + (x1 - x), scansize);
181 }
182
183 //check for potential overflow (see bug 4801285)
184 private int addWithoutOverflow(int x, int w) {
185 int x2 = x + w;
186 if (x > 0 && w > 0 && x2 < 0) {
187 x2 = Integer.MAX_VALUE;
188 } else if (x < 0 && w < 0 && x2 > 0) {
189 x2 = Integer.MIN_VALUE;
190 }
191 return x2;
192 }
193 }
|