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