001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/*
020*
021
022Licensed under the Apache License, Version 2.0 (the "License");
023you may not use this file except in compliance with the License.
024You may obtain a copy of the License at
025
026   http://www.apache.org/licenses/LICENSE-2.0
027
028Unless required by applicable law or agreed to in writing, software
029distributed under the License is distributed on an "AS IS" BASIS,
030WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
031See the License for the specific language governing permissions and
032limitations under the License.
033*/
034
035package lucee.runtime.img.filter;import java.awt.Rectangle;
036import java.awt.RenderingHints;
037import java.awt.geom.Point2D;
038import java.awt.geom.Rectangle2D;
039import java.awt.image.BufferedImage;
040import java.awt.image.BufferedImageOp;
041import java.awt.image.ColorModel;
042
043/**
044 * A convenience class which implements those methods of BufferedImageOp which are rarely changed.
045 */
046public abstract class AbstractBufferedImageOp implements BufferedImageOp, Cloneable {
047
048    public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
049        if ( dstCM == null )
050            dstCM = src.getColorModel();
051        return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
052    }
053    
054    public Rectangle2D getBounds2D( BufferedImage src ) {
055        return new Rectangle(0, 0, src.getWidth(), src.getHeight());
056    }
057    
058    public Point2D getPoint2D( Point2D srcPt, Point2D dstPt ) {
059        if ( dstPt == null )
060            dstPt = new Point2D.Double();
061        dstPt.setLocation( srcPt.getX(), srcPt.getY() );
062        return dstPt;
063    }
064
065    public RenderingHints getRenderingHints() {
066        return null;
067    }
068
069        /**
070         * A convenience method for getting ARGB pixels from an image. This tries to avoid the performance
071         * penalty of BufferedImage.getRGB unmanaging the image.
072     * @param image   a BufferedImage object
073     * @param x       the left edge of the pixel block
074     * @param y       the right edge of the pixel block
075     * @param width   the width of the pixel arry
076     * @param height  the height of the pixel arry
077     * @param pixels  the array to hold the returned pixels. May be null.
078     * @return the pixels
079     * @see #setRGB
080     */
081        public int[] getRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
082                int type = image.getType();
083                if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
084                        return (int [])image.getRaster().getDataElements( x, y, width, height, pixels );
085                return image.getRGB( x, y, width, height, pixels, 0, width );
086    }
087
088        /**
089         * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance
090         * penalty of BufferedImage.setRGB unmanaging the image.
091     * @param image   a BufferedImage object
092     * @param x       the left edge of the pixel block
093     * @param y       the right edge of the pixel block
094     * @param width   the width of the pixel arry
095     * @param height  the height of the pixel arry
096     * @param pixels  the array of pixels to set
097     * @see #getRGB
098         */
099        public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
100                int type = image.getType();
101                if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
102                        image.getRaster().setDataElements( x, y, width, height, pixels );
103                else
104                        image.setRGB( x, y, width, height, pixels, 0, width );
105    }
106
107        public Object clone() {
108                try {
109                        return super.clone();
110                }
111                catch ( CloneNotSupportedException e ) {
112                        return null;
113                }
114        }
115}