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
021
022import lucee.runtime.PageContext;
023import lucee.runtime.exp.ExpressionException;
024import lucee.runtime.exp.PageException;
025import lucee.runtime.ext.function.Function;
026import lucee.runtime.img.Image;
027import lucee.runtime.op.Caster;
028
029public class ImageRotate implements Function {
030        
031        public static String call(PageContext pc, Object name, String angle) throws PageException {
032                return _call(pc, name,-1F,-1F,Caster.toFloatValue(angle),"nearest");
033        }
034        
035        public static String call(PageContext pc, Object name, String angle, String strInterpolation) throws PageException {
036                return _call(pc, name,-1F,-1F,Caster.toFloatValue(angle),strInterpolation);
037        }
038        
039        public static String call(PageContext pc, Object name, String x, String y, String angle) throws PageException {
040                return _call(pc, name,Caster.toFloatValue(x),Caster.toFloatValue(y),Caster.toFloatValue(angle),"nearest");
041        }
042
043        public static String call(PageContext pc, Object name, String x, String y, String angle, String strInterpolation) throws PageException {
044                return _call(pc, name,Caster.toFloatValue(x),Caster.toFloatValue(y),Caster.toFloatValue(angle),strInterpolation);
045        }
046
047        private static String _call(PageContext pc, Object name, float x, float y, float angle, String strInterpolation) throws PageException {
048                //if(name instanceof String) name=pc.getVariable(Caster.toString(name));
049                Image img = Image.toImage(pc,name);
050                strInterpolation=strInterpolation.trim().toLowerCase();
051                int interpolation;
052                if("nearest".equals(strInterpolation)) interpolation=lucee.runtime.img.Image.INTERPOLATION_NEAREST;
053                else if("bilinear".equals(strInterpolation)) interpolation=lucee.runtime.img.Image.INTERPOLATION_BILINEAR;
054                else if("bicubic".equals(strInterpolation)) interpolation=lucee.runtime.img.Image.INTERPOLATION_BICUBIC;
055                else if("none".equals(strInterpolation)) interpolation=lucee.runtime.img.Image.INTERPOLATION_NONE;
056                else throw new ExpressionException("invalid interpolation definition ["+strInterpolation+"]," +
057                                " valid values are [nearest,bilinear,bicubic]");
058                
059                img.rotate(x,y,angle,interpolation);
060                return null;
061                
062        }
063}