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