Trouble finding Images once packaged ImageIO.read

Hi All
I have been using

image = ImageIO.read(new File(“assets/Textures/alphamap.jpg”));

to load a bufferedImage

i use the image get the rgb value of a co-ordinate, to place vegetation on my terrain.

int pixel = image.getRGB(x, y);
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;

This works great when I’m running the game through JME SDK but when i package the game into a jar and try to run it, it cant find the image

javax.imageio.IIOException: Can’t read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at com.jme3.lostVictories.SimpleGrassControl.setSpatial(SimpleGrassControl.java:154)

I can see the image included in assets.jar and
assetManager.loadModel(“Models/Vehicles/armored_car2.j3o”); and
imageBuilder.filename(
both seem to work.

Is there some way i can get ImageIO.read to find the right file path from the jar? or is there a easier way for me to load the image and the RGB of a pixel?

you should not use ImageIO.read to read files else you have this issue.
The suggested way is to load image via assetManager.
try something like :

tex = assetManager.loadTexture("Textures/alphamap.jpg");
image = text.getImage(); // not a BufferedImage like provided by ImageIO

If you prefer to use ImageIO, then you can create an AssetLoader that use it to create image,…

javadoc is your friend ! : AssetManager

2 Likes

ok I did think of this, but i cant figure out how get RGB values out of a jme com.jme3.texture.Image

I can see it has getData(int index) would getData(0) give me the RGB values?

Yes but the encoding depends of Image.Format. check if it’s RGBA8, …

Edit you can see how we reorder color in com/jme3x/jfx/util/FormatUtils :

There is an ImageRaster class that can help you to read data from a JME Image.

1 Like