KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > CropImageFilter


1 /*
2  * @(#)CropImageFilter.java 1.18 04/07/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.image;
9
10 import java.awt.image.ImageConsumer JavaDoc;
11 import java.awt.image.ColorModel JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.awt.Rectangle JavaDoc;
14
15 /**
16  * An ImageFilter class for cropping images.
17  * This class extends the basic ImageFilter Class to extract a given
18  * rectangular region of an existing Image and provide a source for a
19  * new image containing just the extracted region. It is meant to
20  * be used in conjunction with a FilteredImageSource object to produce
21  * cropped versions of existing images.
22  *
23  * @see FilteredImageSource
24  * @see ImageFilter
25  *
26  * @version 1.18 07/16/04
27  * @author Jim Graham
28  */

29 public class CropImageFilter extends ImageFilter JavaDoc {
30     int cropX;
31     int cropY;
32     int cropW;
33     int cropH;
34     
35     /**
36      * Constructs a CropImageFilter that extracts the absolute rectangular
37      * region of pixels from its source Image as specified by the x, y,
38      * w, and h parameters.
39      * @param x the x location of the top of the rectangle to be extracted
40      * @param y the y location of the top of the rectangle to be extracted
41      * @param w the width of the rectangle to be extracted
42      * @param h the height of the rectangle to be extracted
43      */

44     public CropImageFilter(int x, int y, int w, int h) {
45     cropX = x;
46     cropY = y;
47     cropW = w;
48     cropH = h;
49     }
50
51     /**
52      * Passes along the properties from the source object after adding a
53      * property indicating the cropped region.
54      * This method invokes <code>super.setProperties</code>,
55      * which might result in additional properties being added.
56      * <p>
57      * Note: This method is intended to be called by the
58      * <code>ImageProducer</code> of the <code>Image</code> whose pixels
59      * are being filtered. Developers using
60      * this class to filter pixels from an image should avoid calling
61      * this method directly since that operation could interfere
62      * with the filtering operation.
63      */

64     public void setProperties(Hashtable JavaDoc<?,?> props) {
65     Hashtable JavaDoc<Object JavaDoc,Object JavaDoc> p = (Hashtable JavaDoc<Object JavaDoc,Object JavaDoc>)props.clone();
66     p.put("croprect", new Rectangle JavaDoc(cropX, cropY, cropW, cropH));
67     super.setProperties(p);
68     }
69
70     /**
71      * Override the source image's dimensions and pass the dimensions
72      * of the rectangular cropped region to the ImageConsumer.
73      * <p>
74      * Note: This method is intended to be called by the
75      * <code>ImageProducer</code> of the <code>Image</code> whose
76      * pixels are being filtered. Developers using
77      * this class to filter pixels from an image should avoid calling
78      * this method directly since that operation could interfere
79      * with the filtering operation.
80      * @see ImageConsumer
81      */

82     public void setDimensions(int w, int h) {
83     consumer.setDimensions(cropW, cropH);
84     }
85    
86     /**
87      * Determine whether the delivered byte pixels intersect the region to
88      * be extracted and passes through only that subset of pixels that
89      * appear in the output region.
90      * <p>
91      * Note: This method is intended to be called by the
92      * <code>ImageProducer</code> of the <code>Image</code> whose
93      * pixels are being filtered. Developers using
94      * this class to filter pixels from an image should avoid calling
95      * this method directly since that operation could interfere
96      * with the filtering operation.
97      */

98     public void setPixels(int x, int y, int w, int h,
99               ColorModel JavaDoc model, byte pixels[], int off,
100               int scansize) {
101     int x1 = x;
102     if (x1 < cropX) {
103         x1 = cropX;
104     }
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
114     int y2 = addWithoutOverflow(y, h);
115     if (y2 > cropY + cropH) {
116         y2 = cropY + cropH;
117     }
118     if (x1 >= x2 || y1 >= y2) {
119         return;
120     }
121     consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
122                model, pixels,
123                off + (y1 - y) * scansize + (x1 - x), scansize);
124     }
125     
126     /**
127      * Determine if the delivered int pixels intersect the region to
128      * be extracted and pass through only that subset of pixels that
129      * appear in the output region.
130      * <p>
131      * Note: This method is intended to be called by the
132      * <code>ImageProducer</code> of the <code>Image</code> whose
133      * pixels are being filtered. Developers using
134      * this class to filter pixels from an image should avoid calling
135      * this method directly since that operation could interfere
136      * with the filtering operation.
137      */

138     public void setPixels(int x, int y, int w, int h,
139               ColorModel JavaDoc model, int pixels[], int off,
140               int scansize) {
141     int x1 = x;
142     if (x1 < cropX) {
143         x1 = cropX;
144     }
145     int x2 = addWithoutOverflow(x, w);
146     if (x2 > cropX + cropW) {
147         x2 = cropX + cropW;
148     }
149     int y1 = y;
150     if (y1 < cropY) {
151         y1 = cropY;
152     }
153
154     int y2 = addWithoutOverflow(y, h);
155     if (y2 > cropY + cropH) {
156         y2 = cropY + cropH;
157     }
158     if (x1 >= x2 || y1 >= y2) {
159         return;
160     }
161     consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
162                model, pixels,
163                off + (y1 - y) * scansize + (x1 - x), scansize);
164     }
165
166     //check for potential overflow (see bug 4801285)
167
private int addWithoutOverflow(int x, int w) {
168         int x2 = x + w;
169         if ( x > 0 && w > 0 && x2 < 0 ) {
170             x2 = Integer.MAX_VALUE;
171         } else if( x < 0 && w < 0 && x2 > 0 ) {
172             x2 = Integer.MIN_VALUE;
173         }
174         return x2;
175     }
176 }
177
Popular Tags