Proposal for ImageLoader interface

To make the image loading more flexible I'd like to propose an interface for file formats like this:


package com.jme.util;

import java.io.IOException;
import java.io.InputStream;

import com.jme.image.Image;

/**
 * Interface for image loaders, classes have to implement Image load(InputStream);
 * @author Galun
 *
 */
public interface ImageLoader {

        public Image load(InputStream is) throws IOException;
}



Texturemanager could then call the plugins like the following. In a second step the existing loaders can bemoved to the new interface design.


Index: TextureManager.java
===================================================================
RCS file: /cvs/jme/src/com/jme/util/TextureManager.java,v
retrieving revision 1.45
diff -u -r1.45 TextureManager.java
--- TextureManager.java 26 Jan 2006 10:07:57 -0000      1.45
+++ TextureManager.java 30 Apr 2006 13:55:13 -0000
@@ -70,12 +70,17 @@
 final public class TextureManager {

     private static HashMap m_tCache = new HashMap();
+    private static HashMap loaders = new HashMap();

     public static boolean COMPRESS_BY_DEFAULT = true;

     private TextureManager() {
     }

+    public static void addHandler(String format, ImageLoader handler) {
+       loaders.put(format, handler);
+    }
+
     /**
      * <code>loadTexture</code> loads a new texture defined by the parameter
      * string. Filter parameters are used to define the filtering of the
@@ -289,7 +294,10 @@
         com.jme.image.Image imageData = null;
         try {
             String fileExt = fileName.substring(fileName.lastIndexOf('.'));
-            if (".TGA".equalsIgnoreCase(fileExt)) { // TGA, direct to imageData
+            ImageLoader loader = (ImageLoader)loaders.get(fileExt);
+            if (loader != null)
+               imageData = loader.load(file.openStream());
+            else if (".TGA".equalsIgnoreCase(fileExt)) { // TGA, direct to imageData
                 imageData = TGALoader.loadImage(file.openStream());
             } else if (".DDS".equalsIgnoreCase(fileExt)) { // DDS, direct to
                 // imageData

I don't see any harm in this… if noone objects to it I'll add it soon. It'll allow people to use their own loaders without having to use a modified CVS file.



Galun already created the issue here:

https://jme.dev.java.net/issues/show_bug.cgi?id=194

All the formats would be converted to ImageLoader? Sounds like a good idea.

Sounds good to me as well. Let me check in a couple minor tweaks I made to TextureManager first (just to prevent any CVS conflicts). I'll get those in first thing Monday morning.

OK, I'll hold off till then (and I'll make is case insensitive).



And yes, we might convert existing loaders at some point.

Ok, I got my little changes in, and then broke CVS for a sec due to bad imports. heh. But it should be in now if you want to implement this.

Nice!



BTW: a BMPLoader is ready and waiting for the interface :slight_smile:

OK, it's in CVS. I added some javadoc, changed addHandler to registerHandler and added unregisterHandler. Handlers are now case insensitive concerning extentions.

Here are two loaders for the interface:



http://cvs.world-of-mystery.de/cgi-bin/cvsweb.cgi/wom/src/de/worldofmystery/common/ac3d/BMPLoader.java

http://cvs.world-of-mystery.de/cgi-bin/cvsweb.cgi/wom/src/de/worldofmystery/common/ac3d/SGILoader.java