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 * A filter which adds Gaussian blur to an image, producing a glowing effect.
023 * @author Jerry Huxtable
024 */
025public class HighPassFilter extends GaussianFilter {
026        
027        public HighPassFilter() {
028                radius = 10;
029        }
030        
031    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
032        int width = src.getWidth();
033        int height = src.getHeight();
034
035        if ( dst == null )
036            dst = createCompatibleDestImage( src, null );
037
038        int[] inPixels = new int[width*height];
039        int[] outPixels = new int[width*height];
040        src.getRGB( 0, 0, width, height, inPixels, 0, width );
041
042                if ( radius > 0 ) {
043                        convolveAndTranspose(kernel, inPixels, outPixels, width, height, alpha, alpha && premultiplyAlpha, false, CLAMP_EDGES);
044                        convolveAndTranspose(kernel, outPixels, inPixels, height, width, alpha, false, alpha && premultiplyAlpha, CLAMP_EDGES);
045                }
046
047        src.getRGB( 0, 0, width, height, outPixels, 0, width );
048
049                int index = 0;
050                for ( int y = 0; y < height; y++ ) {
051                        for ( int x = 0; x < width; x++ ) {
052                                int rgb1 = outPixels[index];
053                                int r1 = (rgb1 >> 16) & 0xff;
054                                int g1 = (rgb1 >> 8) & 0xff;
055                                int b1 = rgb1 & 0xff;
056
057                                int rgb2 = inPixels[index];
058                                int r2 = (rgb2 >> 16) & 0xff;
059                                int g2 = (rgb2 >> 8) & 0xff;
060                                int b2 = rgb2 & 0xff;
061
062                                r1 = (r1 + 255-r2) / 2;
063                                g1 = (g1 + 255-g2) / 2;
064                                b1 = (b1 + 255-b2) / 2;
065
066                                inPixels[index] = (rgb1 & 0xff000000) | (r1 << 16) | (g1 << 8) | b1;
067                                index++;
068                        }
069                }
070
071        dst.setRGB( 0, 0, width, height, inPixels, 0, width );
072        return dst;
073    }
074
075        public String toString() {
076                return "Blur/High Pass...";
077        }
078}