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