Hi,
I've been working on a level design tool, and I am trying to display the current texture of a spatial in a javax.swing.JLabel. Is there a way to convert the com.jme.Image returned into a java.awt.Image, so that I can create an java.awt.ImageIcon out of this and pass it to the JLabel.setIcon method? This would be simple if the com.jme.Image data was in a simple uncompressed bitmap-like form, but the textures I have loaded come from jpg or png files and get stored as the com.jme.Image.RGB888_DXT1 format which I have no clue as to decode. So the following code I developed can only convert from 24-bit RGB format or 32-bit RGBA format:
private void updateUIFromTexture()
{
Texture tex = textureState.getTexture();
if(tex != null)
{
// rescale the image to be IMG_DIM
com.jme.image.Image jmeImg = tex.getImage();
BufferedImage awtImg;
int awtType;
int jmeType = jmeImg.getType();
if(jmeType == com.jme.image.Image.RGB888)
awtType = BufferedImage.TYPE_INT_RGB;
else if(jmeType == com.jme.image.Image.RGBA8888)
awtType = BufferedImage.TYPE_INT_ARGB;
else
throw new AssertionError("jmeType=" + jmeType + " not supported");
int w = jmeImg.getWidth();
int h = jmeImg.getHeight();
awtImg = new BufferedImage(w,h,awtType);
ByteBuffer data = jmeImg.getData();
data.rewind();
ColorRGBA color = new ColorRGBA();
boolean alpha = jmeType == com.jme.image.Image.RGBA8888;
boolean reverseOrder = data.order() == ByteOrder.LITTLE_ENDIAN;
for(int i = 0; i < w; ++i)
{
for(int j = 0; j < h; ++j)
{
color.r = (data.get() + 128) / 255.0f;
color.g = (data.get() + 128) / 255.0f;
color.b = (data.get() + 128) / 255.0f;
if(alpha)
color.a = (data.get() + 128) / 255.0f;
// convert to single 32-bit int
int value;
if(alpha) value = ColorUtil.asIntRGBA(color);
else value = ColorUtil.asIntRGB(color);
if(reverseOrder) value = Integer.reverseBytes(value);
awtImg.setRGB(i, j, value);
}
}
lblTexture.setIcon(new ImageIcon(awtImg.getScaledInstance(
IMG_DIM.width, IMG_DIM.height, Image.SCALE_SMOOTH)));
lblTexture.setText("");
}
else
{
lblTexture.setIcon(null);
lblTexture.setText("<NO IMAGE>");
}
}
If I could convert the DX_1 format to regular 24-bit RGB I should be good to go, but I am not sure how to do this. Any help would be much appreciated.
Thanks.