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.jingle.mediaimpl.sshare.api;
018
019import java.awt.*;
020import java.awt.geom.Point2D;
021import java.awt.geom.Rectangle2D;
022import java.awt.image.BufferedImage;
023import java.awt.image.BufferedImageOp;
024import java.awt.image.ColorModel;
025
026/**
027 * A convenience class which implements those methods of BufferedImageOp which are rarely changed.
028 */
029public abstract class AbstractBufferedImageOp implements BufferedImageOp, Cloneable {
030
031    public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
032        if ( dstCM == null )
033            dstCM = src.getColorModel();
034        return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
035    }
036    
037    public Rectangle2D getBounds2D( BufferedImage src ) {
038        return new Rectangle(0, 0, src.getWidth(), src.getHeight());
039    }
040    
041    public Point2D getPoint2D( Point2D srcPt, Point2D dstPt ) {
042        if ( dstPt == null )
043            dstPt = new Point2D.Double();
044        dstPt.setLocation( srcPt.getX(), srcPt.getY() );
045        return dstPt;
046    }
047
048    public RenderingHints getRenderingHints() {
049        return null;
050    }
051
052        /**
053         * A convenience method for getting ARGB pixels from an image. This tries to avoid the performance
054         * penalty of BufferedImage.getRGB unmanaging the image.
055     * @param image   a BufferedImage object
056     * @param x       the left edge of the pixel block
057     * @param y       the right edge of the pixel block
058     * @param width   the width of the pixel arry
059     * @param height  the height of the pixel arry
060     * @param pixels  the array to hold the returned pixels. May be null.
061     * @return the pixels
062     * @see #setRGB
063     */
064        public int[] getRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
065                int type = image.getType();
066                if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
067                        return (int [])image.getRaster().getDataElements( x, y, width, height, pixels );
068                return image.getRGB( x, y, width, height, pixels, 0, width );
069    }
070
071        /**
072         * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance
073         * penalty of BufferedImage.setRGB unmanaging the image.
074     * @param image   a BufferedImage object
075     * @param x       the left edge of the pixel block
076     * @param y       the right edge of the pixel block
077     * @param width   the width of the pixel arry
078     * @param height  the height of the pixel arry
079     * @param pixels  the array of pixels to set
080     * @see #getRGB
081         */
082        public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
083                int type = image.getType();
084                if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
085                        image.getRaster().setDataElements( x, y, width, height, pixels );
086                else
087                        image.setRGB( x, y, width, height, pixels, 0, width );
088    }
089
090        public Object clone() {
091                try {
092                        return super.clone();
093                }
094                catch ( CloneNotSupportedException e ) {
095                        return null;
096                }
097        }
098}