001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.image;
018
019import java.awt.*;
020import java.awt.geom.*;
021import java.awt.image.*;
022
023/**
024 * A convenience class which implements those methods of BufferedImageOp which are rarely changed.
025 */
026public abstract class AbstractBufferedImageOp implements BufferedImageOp, Cloneable {
027
028    public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
029        if ( dstCM == null )
030            dstCM = src.getColorModel();
031        return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
032    }
033    
034    public Rectangle2D getBounds2D( BufferedImage src ) {
035        return new Rectangle(0, 0, src.getWidth(), src.getHeight());
036    }
037    
038    public Point2D getPoint2D( Point2D srcPt, Point2D dstPt ) {
039        if ( dstPt == null )
040            dstPt = new Point2D.Double();
041        dstPt.setLocation( srcPt.getX(), srcPt.getY() );
042        return dstPt;
043    }
044
045    public RenderingHints getRenderingHints() {
046        return null;
047    }
048
049        /**
050         * A convenience method for getting ARGB pixels from an image. This tries to avoid the performance
051         * penalty of BufferedImage.getRGB unmanaging the image.
052     * @param image   a BufferedImage object
053     * @param x       the left edge of the pixel block
054     * @param y       the right edge of the pixel block
055     * @param width   the width of the pixel arry
056     * @param height  the height of the pixel arry
057     * @param pixels  the array to hold the returned pixels. May be null.
058     * @return the pixels
059     * @see #setRGB
060     */
061        public int[] getRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
062                int type = image.getType();
063                if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
064                        return (int [])image.getRaster().getDataElements( x, y, width, height, pixels );
065                return image.getRGB( x, y, width, height, pixels, 0, width );
066    }
067
068        /**
069         * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance
070         * penalty of BufferedImage.setRGB unmanaging the image.
071     * @param image   a BufferedImage object
072     * @param x       the left edge of the pixel block
073     * @param y       the right edge of the pixel block
074     * @param width   the width of the pixel arry
075     * @param height  the height of the pixel arry
076     * @param pixels  the array of pixels to set
077     * @see #getRGB
078         */
079        public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
080                int type = image.getType();
081                if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
082                        image.getRaster().setDataElements( x, y, width, height, pixels );
083                else
084                        image.setRGB( x, y, width, height, pixels, 0, width );
085    }
086
087        public Object clone() {
088                try {
089                        return super.clone();
090                }
091                catch ( CloneNotSupportedException e ) {
092                        return null;
093                }
094        }
095}