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