From 3b91333636b660217c62561f6a5883799eb29af6 Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Sun, 15 Jan 2012 19:33:27 +0000 Subject: [PATCH] - 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 --- .../jme3tools/optimize/TextureAtlas.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/engine/src/tools/jme3tools/optimize/TextureAtlas.java b/engine/src/tools/jme3tools/optimize/TextureAtlas.java index 35ca819b7..328c5f11e 100644 --- a/engine/src/tools/jme3tools/optimize/TextureAtlas.java +++ b/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.Texture2D; import com.jme3.util.BufferUtils; +import java.lang.reflect.InvocationTargetException; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.util.ArrayList; @@ -274,6 +275,7 @@ public class TextureAtlas { ByteBuffer sourceData = source.getData(0); int height = source.getHeight(); int width = source.getWidth(); + Image newImage = null; for (int yPos = 0; yPos < height; yPos++) { for (int xPos = 0; xPos < width; xPos++) { int i = ((xPos + x) + (yPos + y) * atlasWidth) * 4; @@ -307,19 +309,56 @@ public class TextureAtlas { image[i + 1] = sourceData.get(j); //b image[i + 2] = sourceData.get(j); //g 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; image[i] = sourceData.get(j + 1); //a image[i + 1] = sourceData.get(j); //b image[i + 2] = sourceData.get(j); //g image[i + 3] = sourceData.get(j); //r } 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 TextureAtlasTile for the given Texture * @param texture The texture to retrieve the TextureAtlasTile for.