Main Menu

Patch for DevIL

Started by darkprophet, August 21, 2005, 18:39:26

Previous topic - Next topic

darkprophet

Hi all,
Below is a patch for the java code in the lwjgl_devil jar. It adds some functionality to the code by allowing the ilLoadURL to load more image formats (such as raw for terrain height maps, dds for compressed textures).

Also, the patch exposes the exceptions that get thrown when an exception occurs during file loading. This should be user handled instead of just sysouting the exception.

Anyways, heres the patch:

--- IL-old.java	2005-08-09 22:36:54.708099200 +0100
+++ IL.java	2005-08-09 22:29:06.655072000 +0100
@@ -441,68 +441,98 @@
 
 	public static native boolean ilSaveData(String FileName);	
 
+    public static int ilGetType(String extension) {
+        // Save having to use equalsIgnoreCase all the time
+        extension = extension.toLowerCase();
+        int type = IL_TYPE_UNKNOWN;
+
+        if (extension.equals("bmp")) {
+            type = IL_BMP;
+        } else if (extension.equals("cut")) {
+            type = IL_CUT;
+        } else if (extension.equals("gif")) {
+            type = IL_GIF;
+        } else if (extension.equals("ico")) {
+            type = IL_ICO;
+        } else if (extension.equals("jpg")) {
+            type = IL_JPG;
+        } else if (extension.equals("lif")) {
+            type = IL_LIF;
+        } else if (extension.equals("mng")) {
+            type = IL_MNG;
+        } else if (extension.equals("pcd")) {
+            type = IL_PCD;
+        } else if (extension.equals("pcx")) {
+            type = IL_PCX;
+        } else if (extension.equals("pic")) {
+            type = IL_PIC;
+        } else if (extension.equals("png")) {
+            type = IL_PNG;
+        } else if (extension.equals("pbm") || extension.equals("pgm")
+                || extension.equals("ppm")) {
+            type = IL_PNM;
+        } else if (extension.equals("psd")) {
+            type = IL_PSD;
+        } else if (extension.equals("psp")) {
+            type = IL_PSP;
+        } else if (extension.equals("bw") || extension.equals("rgb")
+                || extension.equals("rgba") || extension.equals("sgi")) {
+            type = IL_SGI;
+        } else if (extension.equals("tga")) {
+            type = IL_TGA;
+        } else if (extension.equals("tif") || extension.equals("tiff")) {
+            type = IL_TIF;
+        } else if (extension.equals("dds")) {
+            type = IL_DDS;
+        } else if (extension.equals("raw")) {
+            type = IL_RAW;
+        } else if (extension.equals("lbm")) {
+            type = IL_LBM;
+        } else if (extension.equals("jng")) {
+            type = IL_JNG;
+        } else if (extension.equals("wal")) {
+            type = IL_WAL;
+        } else if (extension.equals("pix")) {
+            type = IL_PIX;
+        } else if (extension.equals("mdl")) {
+            type = IL_MDL;
+        } else if (extension.equals("exif")) {
+            type = IL_EXIF;
+        } else if (extension.equals("oil")) {
+            type = IL_OIL;
+        } else if (extension.equals("dcx")) {
+            type = IL_DCX;
+        } else if (extension.equals("pxr")) {
+            type = IL_PXR;
+        } else if (extension.equals("xpm")) {
+            type = IL_XPM;
+        } else if (extension.equals("pal")) {
+            type = IL_JASC_PAL;
+        } else if (extension.equals("h")) {
+            type = IL_CHEAD;
+        }
+            
+        return type;
+    }
+    
 	/**
 	 * Loads an image from the specified url
 	 * 
 	 * @param url URL to load from
 	 * @return true if image was loaded
 	 */
-	public static boolean ilLoadFromURL(URL url) {
-		boolean result = false;
+	public static boolean ilLoadFromURL(URL url) throws IOException {
 		int type = IL_TYPE_UNKNOWN;
 
 		String file = url.toString();
 		int index = file.lastIndexOf('.');
 		if (index != -1) {
 			String extension = file.substring(index + 1);
-			if (extension.equalsIgnoreCase("bmp")) {
-				type = IL_BMP;
-			} else if (extension.equalsIgnoreCase("cut")) {
-				type = IL_CUT;
-			} else if (extension.equalsIgnoreCase("gif")) {
-				type = IL_GIF;
-			} else if (extension.equalsIgnoreCase("ico")) {
-				type = IL_ICO;
-			} else if (extension.equalsIgnoreCase("jpg")) {
-				type = IL_JPG;
-			} else if (extension.equalsIgnoreCase("lif")) {
-				type = IL_LIF;
-			} else if (extension.equalsIgnoreCase("mng")) {
-				type = IL_MNG;
-			} else if (extension.equalsIgnoreCase("pcd")) {
-				type = IL_PCD;
-			} else if (extension.equalsIgnoreCase("pcx")) {
-				type = IL_PCX;
-			} else if (extension.equalsIgnoreCase("pic")) {
-				type = IL_PIC;
-			} else if (extension.equalsIgnoreCase("png")) {
-				type = IL_PNG;
-			} else if (extension.equalsIgnoreCase("pbm") || extension.equalsIgnoreCase("pgm")
-					|| extension.equalsIgnoreCase("ppm")) {
-				type = IL_PNM;
-			} else if (extension.equalsIgnoreCase("psd")) {
-				type = IL_PSD;
-			} else if (extension.equalsIgnoreCase("psp")) {
-				type = IL_PSP;
-			} else if (extension.equalsIgnoreCase("bw") || extension.equalsIgnoreCase("rgb")
-					|| extension.equalsIgnoreCase("rgba") || extension.equalsIgnoreCase("sgi")) {
-				type = IL_SGI;
-			} else if (extension.equalsIgnoreCase("tga")) {
-				type = IL_TGA;
-			} else if (extension.equalsIgnoreCase("tif") || extension.equalsIgnoreCase("tiff")) {
-				type = IL_TIF;
-			}
+            type = ilGetType(extension);
 		}
 
 		// read stream
-		try {
-			result = ilLoadFromStream(url.openStream(), type);
-		} catch (IOException e) {
-			e.printStackTrace();
-			result = false;
-		}
-
-		return result;
+		return ilLoadFromStream(url.openStream(), type);
 	}
 
 	/**
@@ -512,9 +542,9 @@
 	 * @param type Type of image to expect
 	 * @return true if image was loaded
 	 */
-	public static boolean ilLoadFromStream(InputStream stream, int type) {
+	public static boolean ilLoadFromStream(InputStream stream, int type) throws IOException {
 		boolean result = false;
-		int lastRead = 0;
+		int lastRead;
 		byte[] buffer = new byte[4096];
 		ByteArrayOutputStream baos = new ByteArrayOutputStream();
 		BufferedInputStream buf = new BufferedInputStream(stream);
@@ -530,8 +560,10 @@
 			lump.flip();
 			result = ilLoadL(type, lump);
 		} catch (IOException e) {
-			e.printStackTrace();
-			result = false;
+			try {
+			    buf.close();
+            } catch (IOException f ) {}
+            throw e;
 		}
 
 		return result;


Enjoy...

DP