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.*;
020import java.awt.image.*;
021
022/**
023 * Given a binary image, this filter performs binary dilation, setting all added pixels to the given 'new' color.
024 */
025public class DilateFilter extends BinaryFilter {
026
027        private int threshold = 2;
028
029        public DilateFilter() {
030        }
031
032        /**
033         * Set the threshold - the number of neighbouring pixels for dilation to occur.
034         * @param threshold the new threshold
035     * @see #getThreshold
036         */
037        public void setThreshold(int threshold) {
038                this.threshold = threshold;
039        }
040        
041        /**
042         * Return the threshold - the number of neighbouring pixels for dilation to occur.
043         * @return the current threshold
044     * @see #setThreshold
045         */
046        public int getThreshold() {
047                return threshold;
048        }
049        
050        protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
051                int[] outPixels = new int[width * height];
052
053                for (int i = 0; i < iterations; i++) {
054                        int index = 0;
055
056                        if (i > 0) {
057                                int[] t = inPixels;
058                                inPixels = outPixels;
059                                outPixels = t;
060                        }
061                        for (int y = 0; y < height; y++) {
062                                for (int x = 0; x < width; x++) {
063                                        int pixel = inPixels[y*width+x];
064                                        if (!blackFunction.isBlack(pixel)) {
065                                                int neighbours = 0;
066
067                                                for (int dy = -1; dy <= 1; dy++) {
068                                                        int iy = y+dy;
069                                                        int ioffset;
070                                                        if (0 <= iy && iy < height) {
071                                                                ioffset = iy*width;
072                                                                for (int dx = -1; dx <= 1; dx++) {
073                                                                        int ix = x+dx;
074                                                                        if (!(dy == 0 && dx == 0) && 0 <= ix && ix < width) {
075                                                                                int rgb = inPixels[ioffset+ix];
076                                                                                if (blackFunction.isBlack(rgb))
077                                                                                        neighbours++;
078                                                                        }
079                                                                }
080                                                        }
081                                                }
082                                                
083                                                if (neighbours >= threshold) {
084                                                        if (colormap != null)
085                                                                pixel = colormap.getColor((float)i/iterations);
086                                                        else
087                                                                pixel = newColor;
088                                                }
089                                        }
090                                        outPixels[index++] = pixel;
091                                }
092                        }
093                }
094                return outPixels;
095        }
096
097        public String toString() {
098                return "Binary/Dilate...";
099        }
100
101}
102