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.image.*;
020
021/**
022 * An abstract superclass for point filters. The interface is the same as the old RGBImageFilter.
023 */
024public abstract class PointFilter extends AbstractBufferedImageOp {
025
026        protected boolean canFilterIndexColorModel = false;
027
028    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
029        int width = src.getWidth();
030        int height = src.getHeight();
031                int type = src.getType();
032                WritableRaster srcRaster = src.getRaster();
033
034        if ( dst == null )
035            dst = createCompatibleDestImage( src, null );
036                WritableRaster dstRaster = dst.getRaster();
037
038        setDimensions( width, height);
039
040                int[] inPixels = new int[width];
041        for ( int y = 0; y < height; y++ ) {
042                        // We try to avoid calling getRGB on images as it causes them to become unmanaged, causing horrible performance problems.
043                        if ( type == BufferedImage.TYPE_INT_ARGB ) {
044                                srcRaster.getDataElements( 0, y, width, 1, inPixels );
045                                for ( int x = 0; x < width; x++ )
046                                        inPixels[x] = filterRGB( x, y, inPixels[x] );
047                                dstRaster.setDataElements( 0, y, width, 1, inPixels );
048                        } else {
049                                src.getRGB( 0, y, width, 1, inPixels, 0, width );
050                                for ( int x = 0; x < width; x++ )
051                                        inPixels[x] = filterRGB( x, y, inPixels[x] );
052                                dst.setRGB( 0, y, width, 1, inPixels, 0, width );
053                        }
054        }
055
056        return dst;
057    }
058
059        public void setDimensions(int width, int height) {
060        }
061
062        public abstract int filterRGB(int x, int y, int rgb);
063}