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    public abstract class TransferFilter extends PointFilter  implements DynFiltering {
028    
029            protected int[] rTable, gTable, bTable;
030            protected boolean initialized = false;
031            
032            public TransferFilter() {
033                    canFilterIndexColorModel = true;
034            }
035    
036            public int filterRGB(int x, int y, int rgb) {
037                    int a = rgb & 0xff000000;
038                    int r = (rgb >> 16) & 0xff;
039                    int g = (rgb >> 8) & 0xff;
040                    int b = rgb & 0xff;
041                    r = rTable[r];
042                    g = gTable[g];
043                    b = bTable[b];
044                    return a | (r << 16) | (g << 8) | b;
045            }
046    
047            public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
048                    if (!initialized)
049                            initialize();
050                    return super.filter( src, dst );
051            }
052    
053            protected void initialize() {
054                    initialized = true;
055                    rTable = gTable = bTable = makeTable();
056            }
057    
058            protected int[] makeTable() {
059                    int[] table = new int[256];
060                    for (int i = 0; i < 256; i++)
061                            table[i] = PixelUtils.clamp( (int)( 255 * transferFunction( i / 255.0f ) ) );
062                    return table;
063            }
064    
065            protected float transferFunction( float v ) {
066                    return 0;
067            }
068    
069            public int[] getLUT() {
070                    if (!initialized)
071                            initialize();
072                    int[] lut = new int[256];
073                    for ( int i = 0; i < 256; i++ ) {
074                            lut[i] = filterRGB( 0, 0, (i << 24) | (i << 16) | (i << 8) | i );
075                    }
076                    return lut;
077            }
078            
079            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
080                    Object o;
081                    if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
082                            int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
083                            setDimensions(dim[0],dim[1]);
084                    }
085    
086                    // check for arguments not supported
087                    if(parameters.size()>0) {
088                            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 [Dimensions]");
089                    }
090    
091                    return filter(src, dst);
092            }
093    }
094