001    /*
002    *
003    
004    Licensed under the Apache License, Version 2.0 (the "License");
005    you may not use this file except in compliance with the License.
006    You may obtain a copy of the License at
007    
008       http://www.apache.org/licenses/LICENSE-2.0
009    
010    Unless required by applicable law or agreed to in writing, software
011    distributed under the License is distributed on an "AS IS" BASIS,
012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013    See the License for the specific language governing permissions and
014    limitations under the License.
015    */
016    
017    package railo.runtime.img.filter;import java.awt.image.BufferedImage;
018    
019    import railo.runtime.engine.ThreadLocalPageContext;
020    import railo.runtime.exp.FunctionException;
021    import railo.runtime.exp.PageException;
022    import railo.runtime.img.ImageUtil;
023    import railo.runtime.type.KeyImpl;
024    import railo.runtime.type.Struct;
025    import railo.runtime.type.util.CollectionUtil;
026    
027    /**
028     * A Filter to draw grids and check patterns.
029     */
030    public class CheckFilter extends PointFilter  implements DynFiltering {
031    
032            private int xScale = 8;
033            private int yScale = 8;
034            private int foreground = 0xffffffff;
035            private int background = 0xff000000;
036            private int fuzziness = 0;
037            private float angle = 0.0f;
038            private float m00 = 1.0f;
039            private float m01 = 0.0f;
040            private float m10 = 0.0f;
041            private float m11 = 1.0f;
042    
043            public CheckFilter() {
044            }
045    
046            /**
047         * Set the foreground color.
048         * @param foreground the color.
049         * @see #getForeground
050         */
051            public void setForeground(int foreground) {
052                    this.foreground = foreground;
053            }
054    
055            /**
056         * Get the foreground color.
057         * @return the color.
058         * @see #setForeground
059         */
060            public int getForeground() {
061                    return foreground;
062            }
063    
064            /**
065         * Set the background color.
066         * @param background the color.
067         * @see #getBackground
068         */
069            public void setBackground(int background) {
070                    this.background = background;
071            }
072    
073            /**
074         * Get the background color.
075         * @return the color.
076         * @see #setBackground
077         */
078            public int getBackground() {
079                    return background;
080            }
081    
082            /**
083         * Set the X scale of the texture.
084         * @param xScale the scale.
085         * @see #getXScale
086         */
087            public void setXScale(int xScale) {
088                    this.xScale = xScale;
089            }
090    
091            /**
092         * Get the X scale of the texture.
093         * @return the scale.
094         * @see #setXScale
095         */
096            public int getXScale() {
097                    return xScale;
098            }
099    
100            /**
101         * Set the Y scale of the texture.
102         * @param yScale the scale.
103         * @see #getYScale
104         */
105            public void setYScale(int yScale) {
106                    this.yScale = yScale;
107            }
108    
109            /**
110         * Get the Y scale of the texture.
111         * @return the scale.
112         * @see #setYScale
113         */
114            public int getYScale() {
115                    return yScale;
116            }
117    
118            /**
119         * Set the fuzziness of the texture.
120         * @param fuzziness the fuzziness.
121         * @see #getFuzziness
122         */
123            public void setFuzziness(int fuzziness) {
124                    this.fuzziness = fuzziness;
125            }
126    
127            /**
128         * Get the fuzziness of the texture.
129         * @return the fuzziness.
130         * @see #setFuzziness
131         */
132            public int getFuzziness() {
133                    return fuzziness;
134            }
135    
136            /**
137         * Set the angle of the texture.
138         * @param angle the angle of the texture.
139         * @angle
140         * @see #getAngle
141         */
142            public void setAngle(float angle) {
143                    this.angle = angle;
144                    float cos = (float)Math.cos(angle);
145                    float sin = (float)Math.sin(angle);
146                    m00 = cos;
147                    m01 = sin;
148                    m10 = -sin;
149                    m11 = cos;
150            }
151    
152            /**
153         * Get the angle of the texture.
154         * @return the angle of the texture.
155         * @see #setAngle
156         */
157            public float getAngle() {
158                    return angle;
159            }
160    
161            public int filterRGB(int x, int y, int rgb) {
162                    float nx = (m00*x + m01*y) / xScale;
163                    float ny = (m10*x + m11*y) / yScale;
164                    float f = ((int)(nx+100000) % 2 != (int)(ny+100000) % 2) ? 1.0f : 0.0f;
165                    if (fuzziness != 0) {
166                            float fuzz = (fuzziness/100.0f);
167                            float fx = ImageMath.smoothPulse(0, fuzz, 1-fuzz, 1, ImageMath.mod(nx, 1));
168                            float fy = ImageMath.smoothPulse(0, fuzz, 1-fuzz, 1, ImageMath.mod(ny, 1));
169                            f *= fx*fy;
170                    }
171                    return ImageMath.mixColors(f, foreground, background);
172            }
173    
174            public String toString() {
175                    return "Texture/Checkerboard...";
176            }
177            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
178                    Object o;
179                    if((o=parameters.removeEL(KeyImpl.init("Angle")))!=null)setAngle(ImageFilterUtil.toFloatValue(o,"Angle"));
180                    if((o=parameters.removeEL(KeyImpl.init("XScale")))!=null)setXScale(ImageFilterUtil.toIntValue(o,"XScale"));
181                    if((o=parameters.removeEL(KeyImpl.init("YScale")))!=null)setYScale(ImageFilterUtil.toIntValue(o,"YScale"));
182                    if((o=parameters.removeEL(KeyImpl.init("Fuzziness")))!=null)setFuzziness(ImageFilterUtil.toIntValue(o,"Fuzziness"));
183                    if((o=parameters.removeEL(KeyImpl.init("Foreground")))!=null)setForeground(ImageFilterUtil.toColorRGB(o,"Foreground"));
184                    if((o=parameters.removeEL(KeyImpl.init("Background")))!=null)setBackground(ImageFilterUtil.toColorRGB(o,"Background"));
185                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
186                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
187                            setDimensions(dim[0],dim[1]);
188                    }
189    
190                    // check for arguments not supported
191                    if(parameters.size()>0) {
192                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "the parameter"+(parameters.size()>1?"s":"")+" ["+CollectionUtil.getKeyList(parameters,", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [Angle, XScale, YScale, Fuzziness, Foreground, Background, Dimensions]");
193                    }
194    
195                    return filter(src, dst);
196            }
197    }
198