0N/A/*
2362N/A * Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.image;
0N/A
0N/Aimport java.awt.image.ImageConsumer;
0N/Aimport java.awt.image.ColorModel;
0N/Aimport java.util.Hashtable;
0N/Aimport java.awt.Rectangle;
0N/A
0N/A/**
0N/A * An ImageFilter class for cropping images.
0N/A * This class extends the basic ImageFilter Class to extract a given
0N/A * rectangular region of an existing Image and provide a source for a
0N/A * new image containing just the extracted region. It is meant to
0N/A * be used in conjunction with a FilteredImageSource object to produce
0N/A * cropped versions of existing images.
0N/A *
0N/A * @see FilteredImageSource
0N/A * @see ImageFilter
0N/A *
0N/A * @author Jim Graham
0N/A */
0N/Apublic class CropImageFilter extends ImageFilter {
0N/A int cropX;
0N/A int cropY;
0N/A int cropW;
0N/A int cropH;
0N/A
0N/A /**
0N/A * Constructs a CropImageFilter that extracts the absolute rectangular
0N/A * region of pixels from its source Image as specified by the x, y,
0N/A * w, and h parameters.
0N/A * @param x the x location of the top of the rectangle to be extracted
0N/A * @param y the y location of the top of the rectangle to be extracted
0N/A * @param w the width of the rectangle to be extracted
0N/A * @param h the height of the rectangle to be extracted
0N/A */
0N/A public CropImageFilter(int x, int y, int w, int h) {
0N/A cropX = x;
0N/A cropY = y;
0N/A cropW = w;
0N/A cropH = h;
0N/A }
0N/A
0N/A /**
0N/A * Passes along the properties from the source object after adding a
0N/A * property indicating the cropped region.
0N/A * This method invokes <code>super.setProperties</code>,
0N/A * which might result in additional properties being added.
0N/A * <p>
0N/A * Note: This method is intended to be called by the
0N/A * <code>ImageProducer</code> of the <code>Image</code> whose pixels
0N/A * are being filtered. Developers using
0N/A * this class to filter pixels from an image should avoid calling
0N/A * this method directly since that operation could interfere
0N/A * with the filtering operation.
0N/A */
0N/A public void setProperties(Hashtable<?,?> props) {
0N/A Hashtable<Object,Object> p = (Hashtable<Object,Object>)props.clone();
0N/A p.put("croprect", new Rectangle(cropX, cropY, cropW, cropH));
0N/A super.setProperties(p);
0N/A }
0N/A
0N/A /**
0N/A * Override the source image's dimensions and pass the dimensions
0N/A * of the rectangular cropped region to the ImageConsumer.
0N/A * <p>
0N/A * Note: This method is intended to be called by the
0N/A * <code>ImageProducer</code> of the <code>Image</code> whose
0N/A * pixels are being filtered. Developers using
0N/A * this class to filter pixels from an image should avoid calling
0N/A * this method directly since that operation could interfere
0N/A * with the filtering operation.
0N/A * @see ImageConsumer
0N/A */
0N/A public void setDimensions(int w, int h) {
0N/A consumer.setDimensions(cropW, cropH);
0N/A }
0N/A
0N/A /**
0N/A * Determine whether the delivered byte pixels intersect the region to
0N/A * be extracted and passes through only that subset of pixels that
0N/A * appear in the output region.
0N/A * <p>
0N/A * Note: This method is intended to be called by the
0N/A * <code>ImageProducer</code> of the <code>Image</code> whose
0N/A * pixels are being filtered. Developers using
0N/A * this class to filter pixels from an image should avoid calling
0N/A * this method directly since that operation could interfere
0N/A * with the filtering operation.
0N/A */
0N/A public void setPixels(int x, int y, int w, int h,
0N/A ColorModel model, byte pixels[], int off,
0N/A int scansize) {
0N/A int x1 = x;
0N/A if (x1 < cropX) {
0N/A x1 = cropX;
0N/A }
0N/A int x2 = addWithoutOverflow(x, w);
0N/A if (x2 > cropX + cropW) {
0N/A x2 = cropX + cropW;
0N/A }
0N/A int y1 = y;
0N/A if (y1 < cropY) {
0N/A y1 = cropY;
0N/A }
0N/A
0N/A int y2 = addWithoutOverflow(y, h);
0N/A if (y2 > cropY + cropH) {
0N/A y2 = cropY + cropH;
0N/A }
0N/A if (x1 >= x2 || y1 >= y2) {
0N/A return;
0N/A }
0N/A consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
0N/A model, pixels,
0N/A off + (y1 - y) * scansize + (x1 - x), scansize);
0N/A }
0N/A
0N/A /**
0N/A * Determine if the delivered int pixels intersect the region to
0N/A * be extracted and pass through only that subset of pixels that
0N/A * appear in the output region.
0N/A * <p>
0N/A * Note: This method is intended to be called by the
0N/A * <code>ImageProducer</code> of the <code>Image</code> whose
0N/A * pixels are being filtered. Developers using
0N/A * this class to filter pixels from an image should avoid calling
0N/A * this method directly since that operation could interfere
0N/A * with the filtering operation.
0N/A */
0N/A public void setPixels(int x, int y, int w, int h,
0N/A ColorModel model, int pixels[], int off,
0N/A int scansize) {
0N/A int x1 = x;
0N/A if (x1 < cropX) {
0N/A x1 = cropX;
0N/A }
0N/A int x2 = addWithoutOverflow(x, w);
0N/A if (x2 > cropX + cropW) {
0N/A x2 = cropX + cropW;
0N/A }
0N/A int y1 = y;
0N/A if (y1 < cropY) {
0N/A y1 = cropY;
0N/A }
0N/A
0N/A int y2 = addWithoutOverflow(y, h);
0N/A if (y2 > cropY + cropH) {
0N/A y2 = cropY + cropH;
0N/A }
0N/A if (x1 >= x2 || y1 >= y2) {
0N/A return;
0N/A }
0N/A consumer.setPixels(x1 - cropX, y1 - cropY, (x2 - x1), (y2 - y1),
0N/A model, pixels,
0N/A off + (y1 - y) * scansize + (x1 - x), scansize);
0N/A }
0N/A
0N/A //check for potential overflow (see bug 4801285)
0N/A private int addWithoutOverflow(int x, int w) {
0N/A int x2 = x + w;
0N/A if ( x > 0 && w > 0 && x2 < 0 ) {
0N/A x2 = Integer.MAX_VALUE;
0N/A } else if( x < 0 && w < 0 && x2 > 0 ) {
0N/A x2 = Integer.MIN_VALUE;
0N/A }
0N/A return x2;
0N/A }
0N/A}