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.Color;
022import java.awt.image.BufferedImage;
023
024import lucee.commons.color.ColorCaster;
025import lucee.commons.lang.StringUtil;
026import lucee.runtime.PageContext;
027import lucee.runtime.exp.FunctionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.img.Image;
030import lucee.runtime.op.Caster;
031
032public class ImageNew {
033
034
035        public static Object call(PageContext pc) {
036                return new Image();
037        }
038        
039        public static Object call(PageContext pc, Object source) throws PageException {
040                if(StringUtil.isEmpty(source))
041                        return call(pc);//throw new FunctionException(pc,"ImageNew",1,"source","missing argument");
042                return Image.createImage(pc, source, true,true,true,null);
043        }
044        
045        public static Object call(PageContext pc,Object source, String width) throws PageException {
046                return call(pc, source, width, null, null, null);
047        }
048        
049        public static Object call(PageContext pc,Object source, String width, String height) throws PageException {
050                return call(pc, source, width, height, null, null);
051        }
052        
053        public static Object call(PageContext pc,Object source, String width, String height, String strImageType) throws PageException {
054                return call(pc, source, width, height, strImageType, null);
055        }
056        
057        public static Object call(PageContext pc,Object source, String width, String height, String strImageType, String strCanvasColor) throws PageException {
058                if(source==null)
059                        return call(pc);
060                if(StringUtil.isEmpty(width) && StringUtil.isEmpty(height))
061                        return call(pc,source);
062                
063                if(StringUtil.isEmpty(width))
064                        throw new FunctionException(pc,"ImageNew",2,"width","missing argument");
065                if(StringUtil.isEmpty(height))
066                        throw new FunctionException(pc,"ImageNew",3,"height","missing argument");
067                        
068                if(!StringUtil.isEmpty(source))
069                        throw new FunctionException(pc,"ImageNew",1,"source","if you define width and height, source has to be empty");
070                
071                // image type
072                int imageType;
073                if(StringUtil.isEmpty(strImageType,true)) imageType=BufferedImage.TYPE_INT_RGB;
074                else {
075                        strImageType=strImageType.trim().toLowerCase();
076                        if("rgb".equals(strImageType)) imageType=BufferedImage.TYPE_INT_RGB;
077                        else if("argb".equals(strImageType)) imageType=BufferedImage.TYPE_INT_ARGB;
078                        else if("gray".equals(strImageType)) imageType=BufferedImage.TYPE_BYTE_GRAY;
079                        else if("grayscale".equals(strImageType)) imageType=BufferedImage.TYPE_BYTE_GRAY;
080                        else throw new FunctionException(pc,"ImageNew",4,"imageType","imageType has an invalid value ["+strImageType+"]," +
081                                "valid values are [rgb,argb,grayscale]");
082                }
083                // canvas color
084                Color canvasColor;
085                if(StringUtil.isEmpty(strCanvasColor,true)) canvasColor=null;
086                else canvasColor=ColorCaster.toColor(strCanvasColor);
087                
088                return new Image(Caster.toIntValue(width),Caster.toIntValue(height), imageType,canvasColor);
089        }
090}