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 converts it to its outline, replacing all interior pixels with the 'new' color.
024 */
025public class OutlineFilter extends BinaryFilter {
026
027        public OutlineFilter() {
028                newColor = 0xffffffff;
029        }
030
031        protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
032                int index = 0;
033                int[] outPixels = new int[width * height];
034
035                for (int y = 0; y < height; y++) {
036                        for (int x = 0; x < width; x++) {
037                                int pixel = inPixels[y*width+x];
038                                if (blackFunction.isBlack(pixel)) {
039                                        int neighbours = 0;
040
041                                        for (int dy = -1; dy <= 1; dy++) {
042                                                int iy = y+dy;
043                                                int ioffset;
044                                                if (0 <= iy && iy < height) {
045                                                        ioffset = iy*width;
046                                                        for (int dx = -1; dx <= 1; dx++) {
047                                                                int ix = x+dx;
048                                                                if (!(dy == 0 && dx == 0) && 0 <= ix && ix < width) {
049                                                                        int rgb = inPixels[ioffset+ix];
050                                                                        if (blackFunction.isBlack(rgb))
051                                                                                neighbours++;
052                                                                } else
053                                                                        neighbours++;
054                                                        }
055                                                }
056                                        }
057                                        
058                                        if (neighbours == 9)
059                                                pixel = newColor;
060                                }
061                                outPixels[index++] = pixel;
062                        }
063
064                }
065                return outPixels;
066        }
067
068        public String toString() {
069                return "Binary/Outline...";
070        }
071
072}
073