001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.image;
020
021import java.awt.image.Kernel;
022
023import lucee.runtime.PageContext;
024import lucee.runtime.exp.FunctionException;
025import lucee.runtime.exp.PageException;
026import lucee.runtime.op.Caster;
027import lucee.runtime.op.Decision;
028
029public class ImageFilterKernel {
030        public static Object call(PageContext pc, double width, double height, Object oData) throws PageException {
031                
032                float[] data=null;
033                if(oData instanceof float[])
034                        data=(float[]) oData;
035                else if(Decision.isNativeArray(oData)) {
036                        data=toFloatArray(pc,oData);
037                }
038                else if(Decision.isArray(oData)) {
039                        data=toFloatArray(pc,Caster.toNativeArray(oData));
040                }
041                else 
042                        throw new FunctionException(pc, "", 3, "data", "cannot cast data to a float array");
043                
044                return new Kernel(Caster.toIntValue(width),Caster.toIntValue(height),data);
045        }
046
047        private static float[] toFloatArray(PageContext pc,Object oData) throws PageException {
048                float[] data=null;
049                // Object[]
050                if(oData instanceof Object[]) {
051                        Object[] arr = ((Object[])oData);
052                        data=new float[arr.length];
053                        for(int i=0;i<arr.length;i++){
054                                data[i]=Caster.toFloatValue(arr[i]);
055                        }
056                }
057                // boolean[]
058                else if(oData instanceof boolean[]) {
059                        boolean[] arr = ((boolean[])oData);
060                        data=new float[arr.length];
061                        for(int i=0;i<arr.length;i++){
062                                data[i]=Caster.toFloatValue(arr[i]);
063                        }
064                }
065                // byte[]
066                else if(oData instanceof byte[]) {
067                        byte[] arr = ((byte[])oData);
068                        data=new float[arr.length];
069                        for(int i=0;i<arr.length;i++){
070                                data[i]=Caster.toFloatValue(arr[i]);
071                        }
072                }
073                // short[]
074                else if(oData instanceof short[]) {
075                        short[] arr = ((short[])oData);
076                        data=new float[arr.length];
077                        for(int i=0;i<arr.length;i++){
078                                data[i]=Caster.toFloatValue(arr[i]);
079                        }
080                }
081                // long[]
082                else if(oData instanceof long[]) {
083                        long[] arr = ((long[])oData);
084                        data=new float[arr.length];
085                        for(int i=0;i<arr.length;i++){
086                                data[i]=Caster.toFloatValue(arr[i]);
087                        }
088                }
089                // int[]
090                else if(oData instanceof int[]) {
091                        int[] arr = ((int[])oData);
092                        data=new float[arr.length];
093                        for(int i=0;i<arr.length;i++){
094                                data[i]=Caster.toFloatValue(arr[i]);
095                        }
096                }
097                // double[]
098                else if(oData instanceof double[]) {
099                        double[] arr = ((double[])oData);
100                        data=new float[arr.length];
101                        for(int i=0;i<arr.length;i++){
102                                data[i]=Caster.toFloatValue(arr[i]);
103                        }
104                }
105                else 
106                        throw new FunctionException(pc, "", 3, "data", "cannot cast data to a float array");
107                
108                return data;
109        }
110}