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