001 package railo.runtime.img; 002 003 import java.awt.image.BufferedImage; 004 import java.awt.image.RenderedImage; 005 import java.io.File; 006 import java.io.IOException; 007 import java.io.InputStream; 008 import java.io.OutputStream; 009 import java.lang.reflect.InvocationTargetException; 010 import java.lang.reflect.Method; 011 012 import railo.commons.io.IOUtil; 013 import railo.commons.io.SystemUtil; 014 import railo.commons.io.res.Resource; 015 import railo.commons.io.res.util.ResourceUtil; 016 import railo.commons.lang.ClassUtil; 017 import railo.commons.lang.IDGenerator; 018 import railo.commons.lang.StringUtil; 019 020 public class JAIUtil { 021 022 private static Class _RenderedOp; 023 private static Method getAsBufferedImage; 024 private static Class _JAI; 025 private static Method create1; 026 private static Method create3; 027 private static Boolean supported=null; 028 private static String[] readFormats=new String[]{"tiff","pnm","fpx"}; 029 private static String[] writeFormats=new String[]{"tiff","pnm"}; 030 031 032 public static boolean isSupportedWriteFormat(String format) { 033 return "tiff".equalsIgnoreCase(format) || "pnm".equalsIgnoreCase(format); 034 } 035 036 public static boolean isSupportedReadFormat(String format) { 037 return "tiff".equalsIgnoreCase(format) || "pnm".equalsIgnoreCase(format) || "fpx".equalsIgnoreCase(format); 038 } 039 040 public static String[] getSupportedReadFormat() { 041 return readFormats; 042 } 043 044 public static String[] getSupportedWriteFormat() { 045 return writeFormats; 046 } 047 048 public static boolean isJAISupported() { 049 if(supported==null) { 050 supported=ClassUtil.loadClass("javax.media.jai.JAI",null)!=null?Boolean.TRUE:Boolean.FALSE; 051 } 052 return supported.booleanValue(); 053 } 054 055 056 057 public static BufferedImage read(Resource res) throws IOException { 058 Resource tmp=null; 059 try{ 060 if(!(res instanceof File)) { 061 tmp=SystemUtil.getTempDirectory().getRealResource(IDGenerator.intId()+"-"+res.getName()); 062 IOUtil.copy(res, tmp); 063 res=tmp; 064 } 065 //Object im = JAI.create("fileload", res.getAbsolutePath()); 066 return getAsBufferedImage(create("fileload", res.getAbsolutePath())); 067 } 068 finally { 069 if(tmp!=null) ResourceUtil.removeEL(tmp, false); 070 } 071 } 072 public static BufferedImage read(InputStream is,String format) throws IOException { 073 Resource tmp=null; 074 try{ 075 tmp=SystemUtil.getTempDirectory().getRealResource(IDGenerator.intId()+(StringUtil.isEmpty(format)?"":"."+format)); 076 IOUtil.copy(is, tmp,false); 077 //Object im = JAI.create("fileload", tmp.getAbsolutePath()); 078 return getAsBufferedImage(create("fileload", tmp.getAbsolutePath())); 079 } 080 finally { 081 if(tmp!=null) ResourceUtil.removeEL(tmp, false); 082 } 083 } 084 085 public static void write(BufferedImage img, Resource res,String format) throws IOException { 086 Resource tmp=res; 087 try{ 088 if(!(res instanceof File)) { 089 tmp=SystemUtil.getTempDirectory().getRealResource(IDGenerator.intId()+"-"+res.getName()); 090 } 091 //JAI.create("filestore", img, tmp.getAbsolutePath(),format); 092 create("filestore", img, tmp.getAbsolutePath(),format); 093 } 094 finally { 095 if(tmp!=res) { 096 IOUtil.copy(tmp, res); 097 ResourceUtil.removeEL(tmp, false); 098 } 099 } 100 } 101 102 public static void write(BufferedImage img, OutputStream os,String format) throws IOException { 103 Resource tmp=null; 104 try{ 105 tmp=SystemUtil.getTempDirectory().getRealResource(IDGenerator.intId()+"."+format); 106 create("filestore", img, tmp.getAbsolutePath(),format); 107 IOUtil.copy(tmp, os,false); 108 } 109 finally { 110 if(tmp!=null) ResourceUtil.removeEL(tmp, false); 111 } 112 } 113 114 //////////////////////////////////////////////////////////////////// 115 116 private static Object create(String name, Object param) throws IOException { 117 try { 118 return create1().invoke(null, new Object[]{name,param}); 119 } catch (Exception e) { 120 throw toIOException(e); 121 } 122 } 123 124 private static Object create(String name, Object img, Object param1, Object param2) throws IOException { 125 try { 126 return create3().invoke(null, new Object[]{name,img,param1,param2}); 127 } 128 catch (Exception e) { 129 throw toIOException(e); 130 } 131 } 132 133 private static BufferedImage getAsBufferedImage(Object im) throws IOException { 134 //RenderedOp.getAsBufferedImage(); 135 try { 136 return (BufferedImage) getAsBufferedImage().invoke(im, new Object[0]); 137 } 138 catch (Exception e) { 139 throw toIOException(e); 140 } 141 } 142 143 private static Method getAsBufferedImage() throws IOException { 144 if(getAsBufferedImage==null) { 145 try { 146 getAsBufferedImage = getRenderedOp().getMethod("getAsBufferedImage", new Class[0]); 147 } 148 catch (Exception e) { 149 throw toIOException(e); 150 } 151 } 152 return getAsBufferedImage; 153 } 154 155 private static Method create1() throws IOException { 156 if(create1==null) { 157 try { 158 create1 = getJAI().getMethod("create", new Class[]{String.class,Object.class}); 159 } 160 catch (Exception e) { 161 throw toIOException(e); 162 } 163 } 164 return create1; 165 } 166 167 private static Method create3() throws IOException { 168 if(create3==null) { 169 try { 170 create3 = getJAI().getMethod("create", new Class[]{String.class,RenderedImage.class,Object.class,Object.class}); 171 } catch (Exception e) { 172 throw toIOException(e); 173 } 174 } 175 return create3; 176 } 177 178 private static Class getRenderedOp() throws IOException { 179 if(_RenderedOp==null) { 180 _RenderedOp = ClassUtil.loadClass("javax.media.jai.RenderedOp",null); 181 if(_RenderedOp==null) 182 throw new IOException("JAI is not installed on the system but needed for this extension"); 183 } 184 return _RenderedOp; 185 } 186 187 private static Class getJAI() throws IOException { 188 if(_JAI==null) { 189 _JAI = ClassUtil.loadClass("javax.media.jai.JAI",null); 190 if(_JAI==null) 191 throw new IOException("JAI is not installed on the system but needed for this extension"); 192 } 193 return _JAI; 194 } 195 196 197 198 199 private static IOException toIOException(Throwable e) { 200 if(e instanceof InvocationTargetException) 201 e=((InvocationTargetException)e).getTargetException(); 202 203 if(e instanceof IOException) return (IOException) e; 204 IOException ioe = new IOException(e.getMessage()); 205 ioe.setStackTrace(e.getStackTrace()); 206 return ioe; 207 } 208 }