Painting an Image using com.jme3.texture.Image

Hello!



I have some understanding problems with the com.jme3.texture.Image class. The usage looks not very intuitive to me.



The documentation says that the Image data is stored in

[java]protected java.util.ArrayList<java.nio.ByteBuffer> data [/java]



How can I translate these "ByteBuffer"s into something like ColorRGBA?

How can I access pixel (x,y) of my Image?

How can I create a “com.jme3.texture.Image” from a Path to a picture on my disk?

How can I export it?



Thanks in advance.

  1. Kinda answered this in answer 2… see below
  2. ImageRaster (or ImagePainter from @zarch ) to get access to the individual pixels.
  3. AssetManager to load an image as a texture… or JME Image
  4. No clue.

Yes ImageRaster is what you want (ImagePainter maybe as well).



In answer to 4 I was recently musing that if we did an ImageRaster wrapper for BufferedImage that would give you a really easy way to move data into and out of more “standard” java images as that is something people sometimes need. ImagePainter for example would then be able to write to and from BufferedImage in exactly the same way as it can for JME3 Image and you get full interoperability between the two.



This is my current “export to disk” method, ugly as hell:



[java]

void saveImage(String name, String type, Image image, float scale) {



if (scale != 1) {

ImagePainter scaled = new ImagePainter(Image.Format.RGBA8, (int)(image.getWidth()*scale), (int)(image.getHeight()*scale));

scaled.paintStretchedImage(0, 0, scaled.getImage().getWidth(), scaled.getImage().getHeight(), image, ImagePainter.BlendMode.SET, 1);

image = scaled.getImage();

}



File file = new File(outputFolder, name);



BufferedImage output;

if (type.equals(“jpg”)) {

output = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);

} else {

output = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

}

Graphics2D g = output.createGraphics();

ColorRGBA colour = new ColorRGBA();



int height = image.getHeight();



for (int y=0;y<image.getHeight();y++) {

for (int x=0;x<image.getWidth();x++) {

colour = image.getPixel(x, y, colour);

g.setColor(new Color(colour.r, colour.g, colour.b, colour.a));

g.drawRect(x, height-y, 0, 0);

}

}



try {

ImageIO.write(output, type, file);

} catch (IOException ex) {

Logger.getLogger(Exporter.class.getName()).log(Level.SEVERE, null, ex);

}

}

[/java]

(I should note that this export is only done in offline dev tools so it isn’t even slightly optimised - it could be considerably faster).