001    package railo.runtime.img.filter;import java.awt.Color;
002    import java.awt.Composite;
003    import java.awt.Font;
004    import java.awt.Point;
005    import java.awt.geom.AffineTransform;
006    import java.awt.image.BufferedImage;
007    
008    import railo.commons.color.ColorCaster;
009    import railo.runtime.engine.ThreadLocalPageContext;
010    import railo.runtime.exp.ExpressionException;
011    import railo.runtime.exp.FunctionException;
012    import railo.runtime.exp.PageException;
013    import railo.runtime.img.Image;
014    import railo.runtime.img.filter.LightFilter.Material;
015    import railo.runtime.img.math.Function2D;
016    import railo.runtime.op.Caster;
017    import railo.runtime.type.List;
018    import railo.runtime.type.Struct;
019    import railo.runtime.type.util.ArrayUtil;
020    import railo.runtime.type.util.Type;
021    
022    public class ImageFilterUtil {
023    
024            public static float toFloatValue(Object value, String argName) throws FunctionException {
025                    float res = Caster.toFloatValue(value,Float.NaN);
026                    if(Float.isNaN(res)) {
027                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", msg(value,"float",argName));
028                    }
029                    return res;
030            }
031    
032            public static int toIntValue(Object value, String argName) throws FunctionException {
033                    int res = Caster.toIntValue(value,Integer.MIN_VALUE);
034                    if(Integer.MIN_VALUE==res) {
035                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", msg(value,"int",argName));
036                    }
037                    return res;
038            }
039            public static boolean toBooleanValue(Object value, String argName) throws FunctionException {
040                    Boolean res = Caster.toBoolean(value,null);
041                    if(res==null) {
042                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", msg(value,"boolean",argName));
043                    }
044                    return res;
045            }
046            public static String toString(Object value, String argName) throws FunctionException {
047                    String res = Caster.toString(value,null);
048                    if(res==null) {
049                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", msg(value,"String",argName));
050                    }
051                    return res;
052            }
053            
054    
055    
056            public static BufferedImage toBufferedImage(Object o, String argName) throws PageException {
057                    if(o instanceof BufferedImage) return (BufferedImage) o;
058                    return Image.toImage(o).getBufferedImage();
059            }
060    
061            public static Colormap toColormap(Object value, String argName) throws FunctionException {
062                    if(value instanceof Colormap)
063                            return (Colormap) value;
064                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", msg(value,"Colormap",argName)+" use function ImageFilterColorMap to create a colormap");
065            }
066            
067            ////
068    
069            public static Color toColor(Object value, String argName) throws PageException {
070                    if(value instanceof Color)
071                            return (Color) value;
072                    return ColorCaster.toColor(Caster.toString(value));
073                    
074            }
075            
076            public static int toColorRGB(Object value, String argName) throws PageException {
077                    return toColor(value, argName).getRGB();
078                    
079            }
080            
081    
082    
083            public static Point toPoint(Object value, String argName) throws PageException {
084                    if(value instanceof Point) return (Point) value;
085                    String str = Caster.toString(value);
086                    
087                    Struct sct = Caster.toStruct(value,null);
088                    if(sct!=null){
089                            return new Point(Caster.toIntValue(sct.get("x")),Caster.toIntValue(sct.get("y")));
090                    }
091                    
092                    String[] arr = List.listToStringArray(str, ',');
093                    if(arr.length==2) {
094                            return new Point(Caster.toIntValue(arr[0]),Caster.toIntValue(arr[1]));
095                    }
096                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "use the following format [x,y]");
097                    
098            }
099    
100            public static int[] toDimensions(Object value, String argName) throws PageException {
101                    return toAInt(value, argName);
102            }
103    
104            public static LightFilter.Material toLightFilter$Material(Object value, String argName) throws PageException {
105                    if(value instanceof LightFilter.Material)
106                            return (LightFilter.Material) value;
107                    
108                    Struct sct = Caster.toStruct(value,null);
109                    if(sct!=null){
110                            Material material = new LightFilter.Material();
111                            material.setDiffuseColor(toColorRGB(sct.get("color"), argName+".color"));
112                            material.setOpacity(Caster.toFloatValue(sct.get("opacity")));
113                            return material;
114                    }
115                    String str = Caster.toString(value,null);
116                    if(str!=null){
117                            String[] arr = List.listToStringArray(str, ',');
118                            if(arr.length==2) {
119                                    Material material = new LightFilter.Material();
120                                    material.setDiffuseColor(toColorRGB(arr[0], argName+"[1]"));
121                                    material.setOpacity(Caster.toFloatValue(arr[1]));
122                                    return material;
123                            }
124                            throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "use the following format [color,opacity]");
125                            
126                    }
127                    
128                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "use the following format [\"color,opacity\"] or [{color='#cc0033',opacity=0.5}]");
129                    
130            }
131    
132            public static Function2D toFunction2D(Object value, String argName) throws FunctionException {
133                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "type Function2D not supported yet!");
134            }
135    
136    
137            public static AffineTransform toAffineTransform(Object value, String argName) throws FunctionException {
138                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "type BufferedImage not supported yet!");
139            }
140    
141            public static Composite toComposite(Object value, String argName) throws FunctionException {
142                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "type Composite not supported yet!");
143            }
144    
145            public static CurvesFilter.Curve[] toACurvesFilter$Curve(Object value, String argName) throws PageException {
146                    if(value instanceof CurvesFilter.Curve[]) return (CurvesFilter.Curve[]) value;
147                    Object[] arr = Caster.toNativeArray(value);
148                    CurvesFilter.Curve[] curves=new CurvesFilter.Curve[arr.length];
149                    for(int i=0;i<arr.length;i++){
150                            curves[i]=toCurvesFilter$Curve(arr[i],argName);
151                    }
152                    return curves;
153            }
154    
155            public static CurvesFilter.Curve toCurvesFilter$Curve(Object value, String argName) throws FunctionException {
156                    if(value instanceof CurvesFilter.Curve)
157                            return (CurvesFilter.Curve) value;
158                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", msg(value,"Curve",argName)+" use function ImageFilterCurve to create a Curve");
159            }
160    
161            public static int[] toAInt(Object value, String argName) throws PageException {
162                    return ArrayUtil.toIntArray(value);
163            }
164    
165            public static float[] toAFloat(Object value, String argName) throws PageException {
166                    return ArrayUtil.toFloatArray(value);
167            }
168    
169            public static int[][] toAAInt(Object value, String argName) throws FunctionException {
170                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "type int[][] not supported yet!");
171            }
172    
173            public static WarpGrid toWarpGrid(Object value, String argName) throws FunctionException {
174                    if(value instanceof WarpGrid)
175                            return (WarpGrid) value;
176                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", msg(value,"WarpGrid",argName)+" use function ImageFilterWarpGrid to create a WarpGrid");
177            }
178    
179            public static FieldWarpFilter.Line[] toAFieldWarpFilter$Line(Object o, String string) throws FunctionException {
180                    throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "type WarpGrid not supported yet!");
181            }
182            
183            
184            
185            
186    
187            private static String msg(Object value, String type, String argName) {
188                    return "Can't cast argument ["+argName+"] from type ["+Type.getName(value)+"] to a value of type ["+type+"]";
189            }
190    
191            public static Font toFont(Object o, String string) {
192                    // TODO Auto-generated method stub
193                    return null;
194            }
195    
196    
197    
198    
199    
200            private static float range(float value, int from, int to) throws ExpressionException {
201                    if(value>=from && value<=to)
202                            return value;
203                    throw new ExpressionException("["+Caster.toString(value)+"] is out of range, value must be between ["+Caster.toString(from)+"] and ["+Caster.toString(to)+"]");
204            }
205    
206    
207    }