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.*;
021import java.io.*;
022
023public class FadeFilter extends PointFilter {
024
025        private int width, height;
026        private float angle = 0.0f;
027        private float fadeStart = 1.0f;
028        private float fadeWidth = 10.0f;
029        private int sides;
030        private boolean invert;
031        private float m00 = 1.0f;
032        private float m01 = 0.0f;
033        private float m10 = 0.0f;
034        private float m11 = 1.0f;
035
036        /**
037     * Specifies the angle of the texture.
038     * @param angle the angle of the texture.
039     * @angle
040     * @see #getAngle
041     */
042        public void setAngle(float angle) {
043                this.angle = angle;
044                float cos = (float)Math.cos(angle);
045                float sin = (float)Math.sin(angle);
046                m00 = cos;
047                m01 = sin;
048                m10 = -sin;
049                m11 = cos;
050        }
051
052        /**
053     * Returns the angle of the texture.
054     * @return the angle of the texture.
055     * @see #setAngle
056     */
057        public float getAngle() {
058                return angle;
059        }
060
061        public void setSides(int sides) {
062                this.sides = sides;
063        }
064
065        public int getSides() {
066                return sides;
067        }
068
069        public void setFadeStart(float fadeStart) {
070                this.fadeStart = fadeStart;
071        }
072
073        public float getFadeStart() {
074                return fadeStart;
075        }
076
077        public void setFadeWidth(float fadeWidth) {
078                this.fadeWidth = fadeWidth;
079        }
080
081        public float getFadeWidth() {
082                return fadeWidth;
083        }
084
085        public void setInvert(boolean invert) {
086                this.invert = invert;
087        }
088
089        public boolean getInvert() {
090                return invert;
091        }
092
093        public void setDimensions(int width, int height) {
094                this.width = width;
095                this.height = height;
096                super.setDimensions(width, height);
097        }
098        
099        public int filterRGB(int x, int y, int rgb) {
100                float nx = m00*x + m01*y;
101                float ny = m10*x + m11*y;
102                if (sides == 2)
103                        nx = (float)Math.sqrt(nx*nx + ny*ny);
104                else if (sides == 3)
105                        nx = ImageMath.mod(nx, 16);
106                else if (sides == 4)
107                        nx = symmetry(nx, 16);
108                int alpha = (int)(ImageMath.smoothStep(fadeStart, fadeStart+fadeWidth, nx) * 255);
109                if (invert)
110                        alpha = 255-alpha;
111                return (alpha << 24) | (rgb & 0x00ffffff);
112        }
113
114        public float symmetry(float x, float b) {
115/*
116                int d = (int)(x / b);
117                x = ImageMath.mod(x, b);
118                if ((d & 1) == 1)
119                        return b-x;
120                return x;
121*/
122                x = ImageMath.mod(x, 2*b);
123                if (x > b)
124                        return 2*b-x;
125                return x;
126        }
127        
128/*
129        public float star(float x, float y, int sides, float rMin, float rMax) {
130                float sideAngle = 2*Math.PI / sides;
131                float angle = Math.atan2(y, x);
132                float r = Math.sqrt(x*x + y*y);
133                float t = ImageMath.mod(angle, sideAngle) / sideAngle;
134                if (t > 0.5)
135                        t = 1.0-t;
136        }
137*/
138
139        public String toString() {
140                return "Fade...";
141        }
142
143}
144