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.List;
025    import railo.runtime.type.Struct;
026    
027    
028    
029    /**
030     * A filter which performs a threshold operation on an image.
031     */
032    public class ThresholdFilter extends PointFilter  implements DynFiltering {
033    
034            private int lowerThreshold;
035            private int lowerThreshold3;
036            private int upperThreshold;
037            private int upperThreshold3;
038            private int white = 0xffffff;
039            private int black = 0x000000;
040            
041            /**
042         * Construct a ThresholdFilter.
043         */
044        public ThresholdFilter() {
045                    this(127);
046            }
047    
048            /**
049         * Construct a ThresholdFilter.
050         * @param t the threshold value
051         */
052            public ThresholdFilter(int t) {
053                    setLowerThreshold(t);
054                    setUpperThreshold(t);
055            }
056    
057            /**
058         * Set the lower threshold value.
059         * @param lowerThreshold the threshold value
060         * @see #getLowerThreshold
061         */
062            public void setLowerThreshold(int lowerThreshold) {
063                    this.lowerThreshold = lowerThreshold;
064                    lowerThreshold3 = lowerThreshold*3;
065            }
066            
067            /**
068         * Get the lower threshold value.
069         * @return the threshold value
070         * @see #setLowerThreshold
071         */
072            public int getLowerThreshold() {
073                    return lowerThreshold;
074            }
075            
076            /**
077         * Set the upper threshold value.
078         * @param upperThreshold the threshold value
079         * @see #getUpperThreshold
080         */
081            public void setUpperThreshold(int upperThreshold) {
082                    this.upperThreshold = upperThreshold;
083                    upperThreshold3 = upperThreshold*3;
084            }
085    
086            /**
087         * Get the upper threshold value.
088         * @return the threshold value
089         * @see #setUpperThreshold
090         */
091            public int getUpperThreshold() {
092                    return upperThreshold;
093            }
094    
095            /**
096         * Set the color to be used for pixels above the upper threshold.
097         * @param white the color
098         * @see #getWhite
099         */
100            public void setWhite(int white) {
101                    this.white = white;
102            }
103    
104            /**
105         * Get the color to be used for pixels above the upper threshold.
106         * @return the color
107         * @see #setWhite
108         */
109            public int getWhite() {
110                    return white;
111            }
112    
113            /**
114         * Set the color to be used for pixels below the lower threshold.
115         * @param black the color
116         * @see #getBlack
117         */
118            public void setBlack(int black) {
119                    this.black = black;
120            }
121    
122            /**
123         * Set the color to be used for pixels below the lower threshold.
124         * @return the color
125         * @see #setBlack
126         */
127            public int getBlack() {
128                    return black;
129            }
130    
131            public int filterRGB(int x, int y, int rgb) {
132                    int a = rgb & 0xff000000;
133                    int r = (rgb >> 16) & 0xff;
134                    int g = (rgb >> 8) & 0xff;
135                    int b = rgb & 0xff;
136                    int l = r + g + b;
137                    if (l < lowerThreshold3)
138                            return a | black;
139                    else if (l > upperThreshold3)
140                            return a | white;
141                    return rgb;
142            }
143    
144            public String toString() {
145                    return "Stylize/Threshold...";
146            }
147            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
148                    Object o;
149                    if((o=parameters.removeEL(KeyImpl.init("White")))!=null)setWhite(ImageFilterUtil.toColorRGB(o,"White"));
150                    if((o=parameters.removeEL(KeyImpl.init("Black")))!=null)setBlack(ImageFilterUtil.toColorRGB(o,"Black"));
151                    if((o=parameters.removeEL(KeyImpl.init("LowerThreshold")))!=null)setLowerThreshold(ImageFilterUtil.toIntValue(o,"LowerThreshold"));
152                    if((o=parameters.removeEL(KeyImpl.init("UpperThreshold")))!=null)setUpperThreshold(ImageFilterUtil.toIntValue(o,"UpperThreshold"));
153                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
154                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
155                            setDimensions(dim[0],dim[1]);
156                    }
157    
158                    // check for arguments not supported
159                    if(parameters.size()>0) {
160                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "the parameter"+(parameters.size()>1?"s":"")+" ["+List.arrayToList(parameters.keysAsString(),", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [White, Black, LowerThreshold, UpperThreshold, Dimensions]");
161                    }
162    
163                    return filter(src, dst);
164            }
165    }