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.util.*;
021import com.jhlabs.math.*;
022
023public class PointillizeFilter extends CellularFilter {
024
025        private float edgeThickness = 0.4f;
026        private boolean fadeEdges = false;
027        private int edgeColor = 0xff000000;
028        private float fuzziness = 0.1f;
029
030        public PointillizeFilter() {
031                setScale(16);
032                setRandomness(0.0f);
033        }
034        
035        public void setEdgeThickness(float edgeThickness) {
036                this.edgeThickness = edgeThickness;
037        }
038
039        public float getEdgeThickness() {
040                return edgeThickness;
041        }
042
043        public void setFadeEdges(boolean fadeEdges) {
044                this.fadeEdges = fadeEdges;
045        }
046
047        public boolean getFadeEdges() {
048                return fadeEdges;
049        }
050
051        public void setEdgeColor(int edgeColor) {
052                this.edgeColor = edgeColor;
053        }
054
055        public int getEdgeColor() {
056                return edgeColor;
057        }
058
059        public void setFuzziness(float fuzziness) {
060                this.fuzziness = fuzziness;
061        }
062
063        public float getFuzziness() {
064                return fuzziness;
065        }
066
067        public int getPixel(int x, int y, int[] inPixels, int width, int height) {
068                float nx = m00*x + m01*y;
069                float ny = m10*x + m11*y;
070                nx /= scale;
071                ny /= scale * stretch;
072                nx += 1000;
073                ny += 1000;     // Reduce artifacts around 0,0
074                float f = evaluate(nx, ny);
075
076                float f1 = results[0].distance;
077                int srcx = ImageMath.clamp((int)((results[0].x-1000)*scale), 0, width-1);
078                int srcy = ImageMath.clamp((int)((results[0].y-1000)*scale), 0, height-1);
079                int v = inPixels[srcy * width + srcx];
080
081                if (fadeEdges) {
082                        float f2 = results[1].distance;
083                        srcx = ImageMath.clamp((int)((results[1].x-1000)*scale), 0, width-1);
084                        srcy = ImageMath.clamp((int)((results[1].y-1000)*scale), 0, height-1);
085                        int v2 = inPixels[srcy * width + srcx];
086                        v = ImageMath.mixColors(0.5f*f1/f2, v, v2);
087                } else {
088                        f = 1-ImageMath.smoothStep(edgeThickness, edgeThickness+fuzziness, f1);
089                        v = ImageMath.mixColors(f, edgeColor, v);
090                }
091                return v;
092        }
093
094        public String toString() {
095                return "Pixellate/Pointillize...";
096        }
097        
098}