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 performs one round of the game of Life on an image.
024 */
025public class LifeFilter extends BinaryFilter {
026
027        public LifeFilter() {
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 r = 0, g = 0, b = 0;
037                                int pixel = inPixels[y*width+x];
038                                int a = pixel & 0xff000000;
039                                int neighbours = 0;
040
041                                for (int row = -1; row <= 1; row++) {
042                                        int iy = y+row;
043                                        int ioffset;
044                                        if (0 <= iy && iy < height) {
045                                                ioffset = iy*width;
046                                                for (int col = -1; col <= 1; col++) {
047                                                        int ix = x+col;
048                                                        if (!(row == 0 && col == 0) && 0 <= ix && ix < width) {
049                                                                int rgb = inPixels[ioffset+ix];
050                                                                if (blackFunction.isBlack(rgb))
051                                                                        neighbours++;
052                                                        }
053                                                }
054                                        }
055                                }
056                                
057                                if (blackFunction.isBlack(pixel))
058                                        outPixels[index++] = (neighbours == 2 || neighbours == 3) ? pixel : 0xffffffff;
059                                else
060                                        outPixels[index++] = neighbours == 3 ? 0xff000000 : pixel;
061                        }
062
063                }
064                return outPixels;
065        }
066
067        public String toString() {
068                return "Binary/Life";
069        }
070
071}
072