001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.image;
018
019import java.awt.*;
020import java.awt.image.*;
021
022/**
023 * A filter which acts as a superclass for filters which need to have the whole image in memory
024 * to do their stuff.
025 */
026public abstract class WholeImageFilter extends AbstractBufferedImageOp {
027
028        /**
029     * The output image bounds.
030     */
031    protected Rectangle transformedSpace;
032
033        /**
034     * The input image bounds.
035     */
036        protected Rectangle originalSpace;
037        
038        /**
039         * Construct a WholeImageFilter.
040         */
041        public WholeImageFilter() {
042        }
043
044    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
045        int width = src.getWidth();
046        int height = src.getHeight();
047                int type = src.getType();
048                WritableRaster srcRaster = src.getRaster();
049
050                originalSpace = new Rectangle(0, 0, width, height);
051                transformedSpace = new Rectangle(0, 0, width, height);
052                transformSpace(transformedSpace);
053
054        if ( dst == null ) {
055            ColorModel dstCM = src.getColorModel();
056                        dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
057                }
058                WritableRaster dstRaster = dst.getRaster();
059
060                int[] inPixels = getRGB( src, 0, 0, width, height, null );
061                inPixels = filterPixels( width, height, inPixels, transformedSpace );
062                setRGB( dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels );
063
064        return dst;
065    }
066
067        /**
068     * Calculate output bounds for given input bounds.
069     * @param rect input and output rectangle
070     */
071        protected void transformSpace(Rectangle rect) {
072        }
073        
074        /**
075     * Actually filter the pixels.
076     * @param width the image width
077     * @param height the image height
078     * @param inPixels the image pixels
079     * @param transformedSpace the output bounds
080     * @return the output pixels
081     */
082        protected abstract int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace );
083}
084