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