|
From: Brian M. <ma...@us...> - 2005-03-25 01:08:27
|
Update of /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/devil In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5020/org/lwjgl/devil Modified Files: IL.java ILU.java ILUT.java Log Message: dynamic loading of devil Index: ILUT.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/devil/ILUT.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- ILUT.java 15 Mar 2005 03:53:42 -0000 1.2 +++ ILUT.java 25 Mar 2005 01:08:17 -0000 1.3 @@ -31,7 +31,14 @@ */ package org.lwjgl.devil; +import java.io.File; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + import org.lwjgl.LWJGLException; +import org.lwjgl.Sys; /** * $Id$ @@ -39,22 +46,17 @@ * The DevIL ILUT API. * * @author captainjester <cap...@us...> + * @author Brian Matzon <br...@ma...> * @version $Revision$ */ public class ILUT { - /** Have we been created? */ - protected static boolean created; - - public static final int ILUT_VERSION_1_6_7 = 1; - public static final int ILUT_VERSION = 167; - -// Attribute Bits + // Attribute Bits public static final int ILUT_OPENGL_BIT = 0x00000001; public static final int ILUT_ALL_ATTRIB_BITS = 0x000FFFFF; -// Error Types + // Error Types public static final int ILUT_INVALID_ENUM = 0x0501; public static final int ILUT_OUT_OF_MEMORY = 0x0502; public static final int ILUT_INVALID_VALUE = 0x0505; @@ -67,7 +69,7 @@ public static final int ILUT_NOT_SUPPORTED = 0x0550; -// State Definitions + // State Definitions public static final int ILUT_PALETTE_MODE = 0x0600; public static final int ILUT_OPENGL_CONV = 0x0610; public static final int ILUT_MAXTEX_WIDTH = 0x0630; @@ -75,46 +77,32 @@ public static final int ILUT_MAXTEX_DEPTH = 0x0632; public static final int ILUT_GL_USE_S3TC = 0x0634; public static final int ILUT_GL_GEN_S3TC = 0x0635; - -// This new state does automatic texture target detection -// if enabled. Currently, only cubemap detection is supported. -// if the current image is no cubemap, the 2d texture is chosen. public static final int ILUT_GL_AUTODETECT_TEXTURE_TARGET = 0x0807; -// The different rendering api's...more to be added later? + // The different rendering api's...more to be added later? public static final int ILUT_OPENGL = 0; - - static { -// System.loadLibrary("ILU"); - System.loadLibrary("lwjgl-devil"); - } + + public static final int ILUT_VENDOR = IL.IL_VENDOR; + + /** Have we been created? */ + protected static boolean created; - /** - * @return true if DevIL has been created - */ - public static boolean isCreated() { - return created; - } - public static native void initNativeStubs() throws LWJGLException; - - public static native boolean ilutRenderer(int renderer); - -// ImageLib Utility Toolkit Functions + public static native boolean ilutRenderer(int renderer); public static native boolean ilutDisable(int mode); public static native boolean ilutEnable(int mode); public static native boolean ilutGetBoolean(int mode); -// public static native void ilutGetBooleanv(int mode, ILboolean *Param); public static native int ilutGetInteger(int mode); -// public static native void ilutGetIntegerv(int mode, ILint *Param); public static native String ilutGetString(int stringName); - public static native void ilutInit(); + private static native void ilutInit(); public static native boolean ilutIsDisabled(int mode); public static native boolean ilutIsEnabled(int mode); public static native void ilutPopAttrib(); public static native void ilutPushAttrib(int bits); public static native void ilutSetInteger(int Mode, int param); + // public static native void ilutGetBooleanv(int mode, ILboolean *Param); + // public static native void ilutGetIntegerv(int mode, ILint *Param); -// ImageLib Utility Toolkit's OpenGL Functions + // ImageLib Utility Toolkit's OpenGL Functions public static native int ilutGLBindTexImage(); public static native int ilutGLBindMipmaps(); public static native boolean ilutGLBuildMipmaps(); @@ -124,17 +112,142 @@ public static native boolean ilutGLSaveImage(String fileName, int texID); public static native boolean ilutGLSetTex(int texID); public static native boolean ilutGLTexImage(int level); - + + /** + * @return true if ILUT has been created + */ + public static boolean isCreated() { + return created; + } + + /** + * Creates a new instance of ILUT. Cannot be created unless IL has been created. + */ public static void create() throws LWJGLException { - if (!created) { - nCreate(); - ILUT.initNativeStubs(); - ILUT.ilutInit(); - ilutRenderer(ILUT_OPENGL); - created = true; - } - + if(!IL.isCreated()) { + throw new LWJGLException("Cannot create ILUT without having created IL instance"); + } + + String[] ilutPaths = getILUTPaths(); + nCreate(ilutPaths); + created = true; + + try { + ILUT.initNativeStubs(); + ILUT.ilutInit(); + created = true; + } catch (LWJGLException e) { + destroy(); + throw e; + } } - public static native void nCreate(); + private static native void initNativeStubs() throws LWJGLException; + + private static native void resetNativeStubs(Class clazz); + + /** + * Exit cleanly by calling destroy. + */ + public static void destroy() { + resetNativeStubs(ILUT.class); + if (created) { + nDestroy(); + } + created = false; + } + + /** + * Native method to create ILUT instance + * + * @param iluPaths Array of strings containing paths to search for ILUT library + */ + protected static native void nCreate(String[] ilutPaths) throws LWJGLException; + + /** + * Native method the destroy the ILUT + */ + protected static native void nDestroy(); + + private static String[] getILUTPaths() throws LWJGLException { + // need to pass path of possible locations of IL to native side + List possible_paths = new ArrayList(); + + String osName = System.getProperty("os.name"); + + String libname; + String platform_lib_name; + if (osName.startsWith("Win")) { + libname = "ILUT"; + platform_lib_name = "ILUT.dll"; + } else if (osName.startsWith("Lin")) { + libname = "ILUT"; + platform_lib_name = "ILUT.so"; + } else if (osName.startsWith("Mac")) { + libname = "ILUT"; + platform_lib_name = "ILUT.dylib"; + } else { + throw new LWJGLException("Unknown platform: " + osName); + } + + // Add all possible paths from java.library.path + String java_library_path = System.getProperty("java.library.path"); + StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator); + while (st.hasMoreTokens()) { + String path = st.nextToken(); + possible_paths.add(path + File.separator + platform_lib_name); + } + + String classloader_path = getPathFromClassLoader(libname); + if (classloader_path != null) { + Sys.log("getPathFromClassLoader: Path found: " + classloader_path); + possible_paths.add(classloader_path); + } + String lwjgl_classloader_path = getPathFromClassLoader("lwjgl"); + if (lwjgl_classloader_path != null) { + Sys.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path); + possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator)) + + File.separator + platform_lib_name); + } + + //add cwd path + possible_paths.add(platform_lib_name); + + //create needed string array + String[] ilutPaths = new String[possible_paths.size()]; + possible_paths.toArray(ilutPaths); + + return ilutPaths; + } + + /** + * Tries to locate ILUT from the current ClassLoader + * This method exists because ILUT is loaded from native code, and as such + * is exempt from ClassLoader library loading rutines. ILUT therefore always fails. + * We therefore invoke the protected method of the ClassLoader to see if it can + * locate it. + * + * @param libname Name of library to search for + * @return Absolute path to library if found, otherwise null + */ + private static String getPathFromClassLoader(String libname) { + try { + Sys.log("getPathFromClassLoader: searching for: " + libname); + Object o = IL.class.getClassLoader(); + Class c = o.getClass(); + while (c != null) { + try { + Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class}); + findLibrary.setAccessible(true); + Object[] arguments = new Object[] { libname}; + return (String) findLibrary.invoke(o, arguments); + } catch (NoSuchMethodException e) { + c = c.getSuperclass(); + } + } + } catch (Exception e) { + Sys.log("Failure locating ILUT using classloader:" + e); + } + return null; + } } Index: IL.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/devil/IL.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- IL.java 15 Mar 2005 03:53:42 -0000 1.7 +++ IL.java 25 Mar 2005 01:08:17 -0000 1.8 @@ -32,14 +32,22 @@ package org.lwjgl.devil; import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Method; import java.net.URL; import java.nio.ByteBuffer; import java.nio.IntBuffer; [...1169 lines suppressed...] + try { + Sys.log("getPathFromClassLoader: searching for: " + libname); + Object o = IL.class.getClassLoader(); + Class c = o.getClass(); + while (c != null) { + try { + Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class}); + findLibrary.setAccessible(true); + Object[] arguments = new Object[] { libname}; + return (String) findLibrary.invoke(o, arguments); + } catch (NoSuchMethodException e) { + c = c.getSuperclass(); + } + } + } catch (Exception e) { + Sys.log("Failure locating DevIL using classloader:" + e); + } + return null; + } } Index: ILU.java =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/java/org/lwjgl/devil/ILU.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- ILU.java 15 Mar 2005 03:53:42 -0000 1.3 +++ ILU.java 25 Mar 2005 01:08:17 -0000 1.4 @@ -31,10 +31,16 @@ */ package org.lwjgl.devil; +import java.io.File; +import java.lang.reflect.Method; import java.nio.IntBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; import org.lwjgl.BufferChecks; import org.lwjgl.LWJGLException; +import org.lwjgl.Sys; /** * $Id$ @@ -42,15 +48,11 @@ * The DevIL ILU API. * * @author captainjester <cap...@us...> + * @author Brian Matzon <br...@ma...> * @version $Revision$ */ public class ILU { - /** Have we been created? */ - protected static boolean created; - - public static final int ILU_VERSION_1_6_7 = 1; - public static final int ILU_VERSION = 167; - + public static final int ILU_FILTER = 0x2600; public static final int ILU_NEAREST = 0x2601; public static final int ILU_LINEAR = 0x2602; @@ -62,7 +64,7 @@ public static final int ILU_SCALE_LANCZOS3 = 0x2608; public static final int ILU_SCALE_MITCHELL = 0x2609; -// Error types + // Error types public static final int ILU_INVALID_ENUM = 0x0501; public static final int ILU_OUT_OF_MEMORY = 0x0502; public static final int ILU_INTERNAL_ERROR = 0x0504; @@ -70,7 +72,7 @@ public static final int ILU_ILLEGAL_OPERATION = 0x0506; public static final int ILU_INVALID_PARAM = 0x0509; -// Values + // Values public static final int ILU_PLACEMENT = 0x0700; public static final int ILU_LOWER_LEFT = 0x0701; public static final int ILU_LOWER_RIGHT = 0x0702; @@ -78,23 +80,19 @@ public static final int ILU_UPPER_RIGHT = 0x0704; public static final int ILU_CENTER = 0x0705; public static final int ILU_CONVOLUTION_MATRIX = 0x0710; -// public static final int ILU_VERSION_NUM = IL_VERSION_NUM; -// public static final int ILU_VENDOR = IL_VENDOR; + public static final int ILU_VERSION_NUM = IL.IL_VERSION_NUM; + public static final int ILU_VENDOR = IL.IL_VENDOR; + + /** Have we been created? */ + protected static boolean created; - static { -// System.loadLibrary("ILU"); - System.loadLibrary("lwjgl-devil"); - } - /** - * @return true if DevIL has been created + * @return true if ILU has been created */ public static boolean isCreated() { return created; } - public static native void initNativeStubs() throws LWJGLException; - public static native boolean iluAlienify(); public static native boolean iluBlurAvg(int iter); public static native boolean iluBlurGaussian(int iter); @@ -121,25 +119,18 @@ BufferChecks.checkDirect(param); niluGetIntegerv(mode, param, param.position()); } - public static native void niluGetIntegerv(int mode, IntBuffer param, int param_offset); + private static native void niluGetIntegerv(int mode, IntBuffer param, int param_offset); public static native String iluGetString(int stringName); public static native void iluImageParameter(int pName, int param); - public static native void iluInit(); + private static native void iluInit(); public static native boolean iluInvertAlpha(); public static native int iluLoadImage(String fileName); public static native boolean iluMirror(); public static native boolean iluNegative(); public static native boolean iluNoisify(float tolerance); public static native boolean iluPixelize(int pixSize); - // TODO result placed in a pointer -// public static native void iluRegionfv(ILpointf points[], int n); - // TODO result placed in a pointer -// public static native void iluRegioniv(ILpointi points[], int n); public static native boolean iluReplaceColour(byte red, byte green, byte blue, float tolerance); public static native boolean iluRotate(float angle); - - // TODO Not implemented in the native lib -// public static native boolean iluRotate3D(float x, float y, float z, float Angle); public static native boolean iluSaturate1f(float saturation); public static native boolean iluSaturate4f(float r, float g, float b, float saturation); public static native boolean iluScale(int width, int height, int depth); @@ -147,23 +138,13 @@ public static native boolean iluSharpen(float factor, int iter); public static native boolean iluSwapColours(); public static native boolean iluWave(float angle); - /** - * - */ - public static void create() throws LWJGLException { - if (!created) { - nCreate(); - ILU.initNativeStubs(); - ILU.iluInit(); - created = true; - } - - } - public static native void nCreate(); + // public static native void iluRegionfv(ILpointf points[], int n); + // public static native void iluRegioniv(ILpointi points[], int n); + // public static native boolean iluRotate3D(float x, float y, float z, float Angle); - //DevIL lib allows both spellings of colour. - //Will do the same this way. + /* DevIL lib allows both spellings of colour. We support that too */ + // ======================================================================== public static void iluColorsUsed() { iluColoursUsed(); } @@ -176,4 +157,136 @@ public static void iluScaleColors(float r, float g, float b) { iluScaleColours(r, g, b); } + // ------------------------------------------------------------------------ + + /** + * Creates a new instance of ILU. Cannot be created unless IL has been created. + */ + public static void create() throws LWJGLException { + if(!IL.isCreated()) { + throw new LWJGLException("Cannot create ILU without having created IL instance"); + } + + String[] iluPaths = getILUPaths(); + nCreate(iluPaths); + created = true; + + try { + ILU.initNativeStubs(); + ILU.iluInit(); + created = true; + } catch (LWJGLException e) { + destroy(); + throw e; + } + } + + private static native void initNativeStubs() throws LWJGLException; + + private static native void resetNativeStubs(Class clazz); + + /** + * Exit cleanly by calling destroy. + */ + public static void destroy() { + resetNativeStubs(ILU.class); + if (created) { + nDestroy(); + } + created = false; + } + + /** + * Native method to create ILU instance + * + * @param iluPaths Array of strings containing paths to search for ILU library + */ + protected static native void nCreate(String[] iluPaths) throws LWJGLException; + + /** + * Native method the destroy the ILU + */ + protected static native void nDestroy(); + + private static String[] getILUPaths() throws LWJGLException { + // need to pass path of possible locations of ILU to native side + List possible_paths = new ArrayList(); + + String osName = System.getProperty("os.name"); + + String libname; + String platform_lib_name; + if (osName.startsWith("Win")) { + libname = "ILU"; + platform_lib_name = "ILU.dll"; + } else if (osName.startsWith("Lin")) { + libname = "ILU"; + platform_lib_name = "ILU.so"; + } else if (osName.startsWith("Mac")) { + libname = "ILU"; + platform_lib_name = "ILU.dylib"; + } else { + throw new LWJGLException("Unknown platform: " + osName); + } + + // Add all possible paths from java.library.path + String java_library_path = System.getProperty("java.library.path"); + StringTokenizer st = new StringTokenizer(System.getProperty("java.library.path"), File.pathSeparator); + while (st.hasMoreTokens()) { + String path = st.nextToken(); + possible_paths.add(path + File.separator + platform_lib_name); + } + + String classloader_path = getPathFromClassLoader(libname); + if (classloader_path != null) { + Sys.log("getPathFromClassLoader: Path found: " + classloader_path); + possible_paths.add(classloader_path); + } + String lwjgl_classloader_path = getPathFromClassLoader("lwjgl"); + if (lwjgl_classloader_path != null) { + Sys.log("getPathFromClassLoader: Path found: " + lwjgl_classloader_path); + possible_paths.add(lwjgl_classloader_path.substring(0, lwjgl_classloader_path.lastIndexOf(File.separator)) + + File.separator + platform_lib_name); + } + + //add cwd path + possible_paths.add(platform_lib_name); + + //create needed string array + String[] iluPaths = new String[possible_paths.size()]; + possible_paths.toArray(iluPaths); + + return iluPaths; + } + + /** + * Tries to locate ILU from the current ClassLoader + * This method exists because ILU is loaded from native code, and as such + * is exempt from ClassLoader library loading rutines. ILU therefore always fails. + * We therefore invoke the protected method of the ClassLoader to see if it can + * locate it. + * + * @param libname Name of library to search for + * @return Absolute path to library if found, otherwise null + */ + private static String getPathFromClassLoader(String libname) { + try { + Sys.log("getPathFromClassLoader: searching for: " + libname); + Object o = IL.class.getClassLoader(); + Class c = o.getClass(); + while (c != null) { + try { + Method findLibrary = c.getDeclaredMethod("findLibrary", new Class[] { String.class}); + findLibrary.setAccessible(true); + Object[] arguments = new Object[] { libname}; + return (String) findLibrary.invoke(o, arguments); + } catch (NoSuchMethodException e) { + c = c.getSuperclass(); + } + } + } catch (Exception e) { + Sys.log("Failure locating ILU using classloader:" + e); + } + return null; + } } |