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