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