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    import java.awt.image.WritableRaster;
020    
021    import railo.runtime.engine.ThreadLocalPageContext;
022    import railo.runtime.exp.FunctionException;
023    import railo.runtime.exp.PageException;
024    import railo.runtime.img.ImageUtil;
025    import railo.runtime.type.KeyImpl;
026    import railo.runtime.type.List;
027    import railo.runtime.type.Struct;
028    /**
029     * A filter which can be used to produce wipes by transferring the luma of a Destination image into the alpha channel of the source.
030     */
031    public class ChromaKeyFilter extends AbstractBufferedImageOp  implements DynFiltering {
032            
033            private float hTolerance = 0;
034            private float sTolerance = 0;
035            private float bTolerance = 0;
036            private int color;
037    
038            public ChromaKeyFilter() {
039            }
040    
041            /**
042             * Set the tolerance of the image in the range 0..1.
043             * *arg tolerance The tolerance
044             */
045            public void setHTolerance( float hTolerance ) {
046                    this.hTolerance = hTolerance;
047            }
048            
049            public float getHTolerance() {
050                    return hTolerance;
051            }
052            
053            public void setSTolerance( float sTolerance ) {
054                    this.sTolerance = sTolerance;
055            }
056            
057            public float getSTolerance() {
058                    return sTolerance;
059            }
060            
061            public void setBTolerance( float bTolerance ) {
062                    this.bTolerance = bTolerance;
063            }
064            
065            public float getBTolerance() {
066                    return bTolerance;
067            }
068            
069            public void setColor( int color ) {
070                    this.color = color;
071            }
072            
073            public int getColor() {
074                    return color;
075            }
076                    
077        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
078            int width = src.getWidth();
079            int height = src.getHeight();
080                    int type = src.getType();
081                    WritableRaster srcRaster = src.getRaster();
082    
083            if ( dst == null )
084                dst = createCompatibleDestImage( src, null );
085                    WritableRaster dstRaster = dst.getRaster();
086    
087                    float[] hsb1 = null;
088                    float[] hsb2 = null;
089                    int rgb2 = color;
090                    int r2 = (rgb2 >> 16) & 0xff;
091                    int g2 = (rgb2 >> 8) & 0xff;
092                    int b2 = rgb2 & 0xff;
093                    hsb2 = Color.RGBtoHSB( r2, b2, g2, hsb2 );
094                    int[] inPixels = null;
095                    for ( int y = 0; y < height; y++ ) {
096                            inPixels = getRGB( src, 0, y, width, 1, inPixels );
097                            for ( int x = 0; x < width; x++ ) {
098                                    int rgb1 = inPixels[x];
099    
100                                    int r1 = (rgb1 >> 16) & 0xff;
101                                    int g1 = (rgb1 >> 8) & 0xff;
102                                    int b1 = rgb1 & 0xff;
103                                    hsb1 = Color.RGBtoHSB( r1, b1, g1, hsb1 );
104    //                    int tolerance = (int)(255*tolerance);
105    //                    return Math.abs(r1-r2) <= tolerance && Math.abs(g1-g2) <= tolerance && Math.abs(b1-b2) <= tolerance;
106    
107    //                   if ( PixelUtils.nearColors( in, clean, (int)(255*tolerance) ) )
108                                    if ( Math.abs( hsb1[0] - hsb2[0] ) < hTolerance && Math.abs( hsb1[1] - hsb2[1] ) < sTolerance && Math.abs( hsb1[2] - hsb2[2] ) < bTolerance )
109                                            inPixels[x] = rgb1 & 0xffffff;
110                                    else
111                                            inPixels[x] = rgb1;
112                            }
113                            setRGB( dst, 0, y, width, 1, inPixels );
114                    }
115    
116            return dst;
117        }
118    
119            public String toString() {
120                    return "Keying/Chroma Key...";
121            }
122            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
123                    Object o;
124                    if((o=parameters.removeEL(KeyImpl.init("HTolerance")))!=null)setHTolerance(ImageFilterUtil.toFloatValue(o,"HTolerance"));
125                    if((o=parameters.removeEL(KeyImpl.init("STolerance")))!=null)setSTolerance(ImageFilterUtil.toFloatValue(o,"STolerance"));
126                    if((o=parameters.removeEL(KeyImpl.init("BTolerance")))!=null)setBTolerance(ImageFilterUtil.toFloatValue(o,"BTolerance"));
127                    if((o=parameters.removeEL(KeyImpl.init("Color")))!=null)setColor(ImageFilterUtil.toIntValue(o,"Color"));
128    
129                    // check for arguments not supported
130                    if(parameters.size()>0) {
131                            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 [HTolerance, STolerance, BTolerance, Color]");
132                    }
133    
134                    return filter(src, dst);
135            }
136    }