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 * A filter which replcaes each pixel by the maximum of itself and its eight neightbours.
024 */
025public class MaximumFilter extends WholeImageFilter {
026
027        public MaximumFilter() {
028        }
029
030        protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
031                int index = 0;
032                int[] outPixels = new int[width * height];
033
034                for (int y = 0; y < height; y++) {
035                        for (int x = 0; x < width; x++) {
036                                int pixel = 0xff000000;
037                                for (int dy = -1; dy <= 1; dy++) {
038                                        int iy = y+dy;
039                                        int ioffset;
040                                        if (0 <= iy && iy < height) {
041                                                ioffset = iy*width;
042                                                for (int dx = -1; dx <= 1; dx++) {
043                                                        int ix = x+dx;
044                                                        if (0 <= ix && ix < width) {
045                                                                pixel = PixelUtils.combinePixels(pixel, inPixels[ioffset+ix], PixelUtils.MAX);
046                                                        }
047                                                }
048                                        }
049                                }
050                                outPixels[index++] = pixel;
051                        }
052                }
053                return outPixels;
054        }
055
056        public String toString() {
057                return "Blur/Maximum";
058        }
059
060}
061