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.Color;
018    import java.awt.image.BufferedImage;
019    
020    import railo.runtime.engine.ThreadLocalPageContext;
021    import railo.runtime.exp.FunctionException;
022    import railo.runtime.exp.PageException;
023    import railo.runtime.img.ImageUtil;
024    import railo.runtime.type.KeyImpl;
025    import railo.runtime.type.Struct;
026    import railo.runtime.type.util.CollectionUtil;
027    
028    public class HSBAdjustFilter extends PointFilter  implements DynFiltering {
029            
030            public float hFactor, sFactor, bFactor;
031            private float[] hsb = new float[3];
032            
033            public HSBAdjustFilter() {
034                    this(0, 0, 0);
035            }
036    
037            public HSBAdjustFilter(float r, float g, float b) {
038                    hFactor = r;
039                    sFactor = g;
040                    bFactor = b;
041                    canFilterIndexColorModel = true;
042            }
043    
044            public void setHFactor( float hFactor ) {
045                    this.hFactor = hFactor;
046            }
047            
048            public float getHFactor() {
049                    return hFactor;
050            }
051            
052            public void setSFactor( float sFactor ) {
053                    this.sFactor = sFactor;
054            }
055            
056            public float getSFactor() {
057                    return sFactor;
058            }
059            
060            public void setBFactor( float bFactor ) {
061                    this.bFactor = bFactor;
062            }
063            
064            public float getBFactor() {
065                    return bFactor;
066            }
067            
068            public int filterRGB(int x, int y, int rgb) {
069                    int a = rgb & 0xff000000;
070                    int r = (rgb >> 16) & 0xff;
071                    int g = (rgb >> 8) & 0xff;
072                    int b = rgb & 0xff;
073                    Color.RGBtoHSB(r, g, b, hsb);
074                    hsb[0] += hFactor;
075                    while (hsb[0] < 0)
076                            hsb[0] += Math.PI*2;
077                    hsb[1] += sFactor;
078                    if (hsb[1] < 0)
079                            hsb[1] = 0;
080                    else if (hsb[1] > 1.0)
081                            hsb[1] = 1.0f;
082                    hsb[2] += bFactor;
083                    if (hsb[2] < 0)
084                            hsb[2] = 0;
085                    else if (hsb[2] > 1.0)
086                            hsb[2] = 1.0f;
087                    rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
088                    return a | (rgb & 0xffffff);
089            }
090    
091            public String toString() {
092                    return "Colors/Adjust HSB...";
093            }
094            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
095                    Object o;
096                    if((o=parameters.removeEL(KeyImpl.init("HFactor")))!=null)setHFactor(ImageFilterUtil.toFloatValue(o,"HFactor"));
097                    if((o=parameters.removeEL(KeyImpl.init("SFactor")))!=null)setSFactor(ImageFilterUtil.toFloatValue(o,"SFactor"));
098                    if((o=parameters.removeEL(KeyImpl.init("BFactor")))!=null)setBFactor(ImageFilterUtil.toFloatValue(o,"BFactor"));
099                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
100                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
101                            setDimensions(dim[0],dim[1]);
102                    }
103    
104                    // check for arguments not supported
105                    if(parameters.size()>0) {
106                            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 [HFactor, SFactor, BFactor, Dimensions]");
107                    }
108    
109                    return filter(src, dst);
110            }
111    }
112