001    package railo.runtime.functions.image;
002    
003    import java.awt.Color;
004    import java.awt.image.BufferedImage;
005    
006    import railo.commons.color.ColorCaster;
007    import railo.commons.lang.StringUtil;
008    import railo.runtime.PageContext;
009    import railo.runtime.exp.FunctionException;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.img.Image;
012    import railo.runtime.op.Caster;
013    
014    public class ImageNew {
015    
016    
017            public static Object call(PageContext pc) {
018                    return new Image();
019            }
020            
021            public static Object call(PageContext pc, Object source) throws PageException {
022                    if(StringUtil.isEmpty(source))
023                            return call(pc);//throw new FunctionException(pc,"ImageNew",1,"source","missing argument");
024                    return Image.createImage(pc, source, true,true,true,null);
025            }
026            
027            public static Object call(PageContext pc,Object source, String width) throws PageException {
028                    return call(pc, source, width, null, null, null);
029            }
030            
031            public static Object call(PageContext pc,Object source, String width, String height) throws PageException {
032                    return call(pc, source, width, height, null, null);
033            }
034            
035            public static Object call(PageContext pc,Object source, String width, String height, String strImageType) throws PageException {
036                    return call(pc, source, width, height, strImageType, null);
037            }
038            
039            public static Object call(PageContext pc,Object source, String width, String height, String strImageType, String strCanvasColor) throws PageException {
040                    if(source==null)
041                            return call(pc);
042                    if(StringUtil.isEmpty(width) && StringUtil.isEmpty(height))
043                            return call(pc,source);
044                    
045                    if(StringUtil.isEmpty(width))
046                            throw new FunctionException(pc,"ImageNew",2,"width","missing argument");
047                    if(StringUtil.isEmpty(height))
048                            throw new FunctionException(pc,"ImageNew",3,"height","missing argument");
049                            
050                    if(!StringUtil.isEmpty(source))
051                            throw new FunctionException(pc,"ImageNew",1,"source","if you define width and height, source has to be empty");
052                    
053                    // image type
054                    int imageType;
055                    if(StringUtil.isEmpty(strImageType,true)) imageType=BufferedImage.TYPE_INT_RGB;
056                    else {
057                            strImageType=strImageType.trim().toLowerCase();
058                            if("rgb".equals(strImageType)) imageType=BufferedImage.TYPE_INT_RGB;
059                            else if("argb".equals(strImageType)) imageType=BufferedImage.TYPE_INT_ARGB;
060                            else if("gray".equals(strImageType)) imageType=BufferedImage.TYPE_BYTE_GRAY;
061                            else if("grayscale".equals(strImageType)) imageType=BufferedImage.TYPE_BYTE_GRAY;
062                            else throw new FunctionException(pc,"ImageNew",4,"imageType","imageType has an invalid value ["+strImageType+"]," +
063                                    "valid values are [rgb,argb,grayscale]");
064                    }
065                    // canvas color
066                    Color canvasColor;
067                    if(StringUtil.isEmpty(strCanvasColor,true)) canvasColor=null;
068                    else canvasColor=ColorCaster.toColor(strCanvasColor);
069                    
070                    return new Image(Caster.toIntValue(width),Caster.toIntValue(height), imageType,canvasColor);
071            }
072    }