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.image.BufferedImage;
021import java.awt.image.ColorModel;
022import java.awt.image.WritableRaster;
023
024/**
025 * A filter which acts as a superclass for filters which need to have the whole image in memory
026 * to do their stuff.
027 */
028public abstract class WholeImageFilter extends AbstractBufferedImageOp {
029
030        /**
031     * The output image bounds.
032     */
033    protected Rectangle transformedSpace;
034
035        /**
036     * The input image bounds.
037     */
038        protected Rectangle originalSpace;
039        
040        /**
041         * Construct a WholeImageFilter.
042         */
043        public WholeImageFilter() {
044        }
045
046    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
047        int width = src.getWidth();
048        int height = src.getHeight();
049                int type = src.getType();
050                WritableRaster srcRaster = src.getRaster();
051
052                originalSpace = new Rectangle(0, 0, width, height);
053                transformedSpace = new Rectangle(0, 0, width, height);
054                transformSpace(transformedSpace);
055
056        if ( dst == null ) {
057            ColorModel dstCM = src.getColorModel();
058                        dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
059                }
060                WritableRaster dstRaster = dst.getRaster();
061
062                int[] inPixels = getRGB( src, 0, 0, width, height, null );
063                inPixels = filterPixels( width, height, inPixels, transformedSpace );
064                setRGB( dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels );
065
066        return dst;
067    }
068
069        /**
070     * Calculate output bounds for given input bounds.
071     * @param rect input and output rectangle
072     */
073        protected void transformSpace(Rectangle rect) {
074        }
075        
076        /**
077     * Actually filter the pixels.
078     * @param width the image width
079     * @param height the image height
080     * @param inPixels the image pixels
081     * @param transformedSpace the output bounds
082     * @return the output pixels
083     */
084        protected abstract int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace );
085}
086