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    
029    public class RGBAdjustFilter extends PointFilter  implements DynFiltering {
030            
031            public float rFactor, gFactor, bFactor;
032    
033            public RGBAdjustFilter() {
034                    this(0, 0, 0);
035            }
036    
037            public RGBAdjustFilter(float r, float g, float b) {
038                    rFactor = 1+r;
039                    gFactor = 1+g;
040                    bFactor = 1+b;
041                    canFilterIndexColorModel = true;
042            }
043    
044            public void setRFactor( float rFactor ) {
045                    this.rFactor = 1+rFactor;
046            }
047            
048            public float getRFactor() {
049                    return rFactor-1;
050            }
051            
052            public void setGFactor( float gFactor ) {
053                    this.gFactor = 1+gFactor;
054            }
055            
056            public float getGFactor() {
057                    return gFactor-1;
058            }
059            
060            public void setBFactor( float bFactor ) {
061                    this.bFactor = 1+bFactor;
062            }
063            
064            public float getBFactor() {
065                    return bFactor-1;
066            }
067    
068            public int[] getLUT() {
069                    int[] lut = new int[256];
070                    for ( int i = 0; i < 256; i++ ) {
071                            lut[i] = filterRGB( 0, 0, (i << 24) | (i << 16) | (i << 8) | i );
072                    }
073                    return lut;
074            }
075            
076            public int filterRGB(int x, int y, int rgb) {
077                    int a = rgb & 0xff000000;
078                    int r = (rgb >> 16) & 0xff;
079                    int g = (rgb >> 8) & 0xff;
080                    int b = rgb & 0xff;
081                    r = PixelUtils.clamp((int)(r * rFactor));
082                    g = PixelUtils.clamp((int)(g * gFactor));
083                    b = PixelUtils.clamp((int)(b * bFactor));
084                    return a | (r << 16) | (g << 8) | b;
085            }
086    
087            public String toString() {
088                    return "Colors/Adjust RGB...";
089            }
090            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
091                    Object o;
092                    if((o=parameters.removeEL(KeyImpl.init("BFactor")))!=null)setBFactor(ImageFilterUtil.toFloatValue(o,"BFactor"));
093                    if((o=parameters.removeEL(KeyImpl.init("RFactor")))!=null)setRFactor(ImageFilterUtil.toFloatValue(o,"RFactor"));
094                    if((o=parameters.removeEL(KeyImpl.init("GFactor")))!=null)setGFactor(ImageFilterUtil.toFloatValue(o,"GFactor"));
095                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
096                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
097                            setDimensions(dim[0],dim[1]);
098                    }
099    
100                    // check for arguments not supported
101                    if(parameters.size()>0) {
102                            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 [BFactor, RFactor, GFactor, Dimensions]");
103                    }
104    
105                    return filter(src, dst);
106            }
107    }
108