浏览代码

- add ImageToAwt fallback (using reflection, no AWT dependency) to TextureAtlas conversion

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9062 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
nor..67 13 年之前
父节点
当前提交
3b91333636
共有 1 个文件被更改,包括 41 次插入2 次删除
  1. 41 2
      engine/src/tools/jme3tools/optimize/TextureAtlas.java

+ 41 - 2
engine/src/tools/jme3tools/optimize/TextureAtlas.java

@@ -46,6 +46,7 @@ import com.jme3.texture.Image.Format;
 import com.jme3.texture.Texture;
 import com.jme3.texture.Texture;
 import com.jme3.texture.Texture2D;
 import com.jme3.texture.Texture2D;
 import com.jme3.util.BufferUtils;
 import com.jme3.util.BufferUtils;
+import java.lang.reflect.InvocationTargetException;
 import java.nio.ByteBuffer;
 import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
 import java.nio.FloatBuffer;
 import java.util.ArrayList;
 import java.util.ArrayList;
@@ -274,6 +275,7 @@ public class TextureAtlas {
         ByteBuffer sourceData = source.getData(0);
         ByteBuffer sourceData = source.getData(0);
         int height = source.getHeight();
         int height = source.getHeight();
         int width = source.getWidth();
         int width = source.getWidth();
+        Image newImage = null;
         for (int yPos = 0; yPos < height; yPos++) {
         for (int yPos = 0; yPos < height; yPos++) {
             for (int xPos = 0; xPos < width; xPos++) {
             for (int xPos = 0; xPos < width; xPos++) {
                 int i = ((xPos + x) + (yPos + y) * atlasWidth) * 4;
                 int i = ((xPos + x) + (yPos + y) * atlasWidth) * 4;
@@ -307,19 +309,56 @@ public class TextureAtlas {
                     image[i + 1] = sourceData.get(j); //b
                     image[i + 1] = sourceData.get(j); //b
                     image[i + 2] = sourceData.get(j); //g
                     image[i + 2] = sourceData.get(j); //g
                     image[i + 3] = sourceData.get(j); //r
                     image[i + 3] = sourceData.get(j); //r
-                }  else if (source.getFormat() == Format.Luminance8Alpha8) {
+                } else if (source.getFormat() == Format.Luminance8Alpha8) {
                     int j = (xPos + yPos * width) * 2;
                     int j = (xPos + yPos * width) * 2;
                     image[i] = sourceData.get(j + 1); //a
                     image[i] = sourceData.get(j + 1); //a
                     image[i + 1] = sourceData.get(j); //b
                     image[i + 1] = sourceData.get(j); //b
                     image[i + 2] = sourceData.get(j); //g
                     image[i + 2] = sourceData.get(j); //g
                     image[i + 3] = sourceData.get(j); //r
                     image[i + 3] = sourceData.get(j); //r
                 } else {
                 } else {
-                    throw new UnsupportedOperationException("Cannot draw textures with format " + source.getFormat());
+                    //ImageToAwt conversion
+                    if (newImage == null) {
+                        newImage = convertImageToAwt(source);
+                        if (newImage != null) {
+                            source = newImage;
+                            sourceData = source.getData(0);
+                            int j = (xPos + yPos * width) * 4;
+                            image[i] = sourceData.get(j); //a
+                            image[i + 1] = sourceData.get(j + 1); //b
+                            image[i + 2] = sourceData.get(j + 2); //g
+                            image[i + 3] = sourceData.get(j + 3); //r
+                        }else{
+                            throw new UnsupportedOperationException("Cannot draw or convert textures with format " + source.getFormat());
+                        }
+                    } else {
+                        throw new UnsupportedOperationException("Cannot draw textures with format " + source.getFormat());
+                    }
                 }
                 }
             }
             }
         }
         }
     }
     }
 
 
+    private Image convertImageToAwt(Image source) {
+        //use awt dependent classes without actual dependency via reflection
+        try {
+            Class clazz = Class.forName("jme3tools.converters.ImageToAwt");
+            if (clazz == null) {
+                return null;
+            }
+            Image newImage = new Image(format, source.getWidth(), source.getHeight(), BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 4));
+            clazz.getMethod("convert", Image.class, Image.class).invoke(clazz.newInstance(), source, newImage);
+            return newImage;
+        } catch (InstantiationException ex) {
+        } catch (IllegalAccessException ex) {
+        } catch (IllegalArgumentException ex) {
+        } catch (InvocationTargetException ex) {
+        } catch (NoSuchMethodException ex) {
+        } catch (SecurityException ex) {
+        } catch (ClassNotFoundException ex) {
+        }
+        return null;
+    }
+
     /**
     /**
      * Get the <code>TextureAtlasTile</code> for the given Texture
      * Get the <code>TextureAtlasTile</code> for the given Texture
      * @param texture The texture to retrieve the <code>TextureAtlasTile</code> for.
      * @param texture The texture to retrieve the <code>TextureAtlasTile</code> for.