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.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    @Override
047    public BufferedImage filter(BufferedImage src, BufferedImage dst) {
048        int width = src.getWidth();
049        int height = src.getHeight();
050        int type = src.getType();
051        WritableRaster srcRaster = src.getRaster();
052
053        originalSpace = new Rectangle(0, 0, width, height);
054        transformedSpace = new Rectangle(0, 0, width, height);
055        transformSpace(transformedSpace);
056
057        if (dst == null) {
058            ColorModel dstCM = src.getColorModel();
059            dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
060        }
061        WritableRaster dstRaster = dst.getRaster();
062
063        int[] inPixels = getRGB(src, 0, 0, width, height, null);
064        inPixels = filterPixels(width, height, inPixels, transformedSpace);
065        setRGB(dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels);
066
067        return dst;
068    }
069
070    /**
071     * Calculate output bounds for given input bounds.
072     * @param rect input and output rectangle
073     */
074    protected void transformSpace(Rectangle rect) {
075    }
076
077    /**
078     * Actually filter the pixels.
079     * @param width the image width
080     * @param height the image height
081     * @param inPixels the image pixels
082     * @param transformedSpace the output bounds
083     * @return the output pixels
084     */
085    protected abstract int[] filterPixels(int width, int height, int[] inPixels, Rectangle transformedSpace);
086}
087