ImageRaster on Android causes NPE

Hi,

I have the following class which works perfect on my desktop but causes a NPE when running on my android device:

[java]
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import com.jme3.texture.Texture2D;
import com.jme3.texture.image.ImageRaster;
import java.nio.ByteBuffer;
import java.util.List;

/**

  • You can draw on this texture based on a given template.
    **/
    public class TemplatedTexture extends Texture2D {

    private Texture2D template;
    private ImageRaster templateRaster;
    private ImageRaster imageRaster;

    public TemplatedTexture(Texture2D template) {
    super();
    this.template = template;
    Image image = buildFromTemplate(template);
    setImage(image);
    imageRaster = ImageRaster.create(image);
    templateRaster = ImageRaster.create(template.getImage());
    }

    private Image buildFromTemplate(Texture2D template) {
    Image imageTemplate = template.getImage();
    Image image = new Image();
    image.setWidth(imageTemplate.getWidth());
    image.setHeight(imageTemplate.getHeight());
    image.setFormat(imageTemplate.getFormat());
    image.setMultiSamples(imageTemplate.getMultiSamples());
    image.setMipMapSizes(imageTemplate.getMipMapSizes());
    List<ByteBuffer> data = imageTemplate.getData();
    for (int i = 0; i < data.size(); i++) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.get(i).capacity());
    image.setData(i, byteBuffer);
    }
    return image;
    }

    /**

    • Fills all pixels with the given color where templateColor is set.
    • @param templateColor color of the template where we want to draw something
    • @param color the color to draw with
    • @param templateToleration toleration 0f-1f
      */
      public void fill(ColorRGBA template, ColorRGBA color, float templateToleration) {
      for (int y = 0; y < getImage().getHeight(); y++) {
      for (int x = 0; x < getImage().getWidth(); x++) {
      ColorRGBA pixel = templateRaster.getPixel(x, y);
      if (tolerateEquals(pixel, template, templateToleration)) {
      imageRaster.setPixel(x, y, color);
      }
      }
      }
      }

    /**

    • Checks if both colors are equal in a given toleration range.
    • @param a
    • @param b
    • @param toleration
    • @return
      */
      public boolean tolerateEquals(ColorRGBA a, ColorRGBA b, float toleration) {
      if (FastMath.abs(a.getRed() - b.getRed()) > toleration) {
      return false;
      }
      if (FastMath.abs(a.getGreen() - b.getGreen()) > toleration) {
      return false;
      }
      if (FastMath.abs(a.getBlue() - b.getBlue()) > toleration) {
      return false;
      }
      if (FastMath.abs(a.getAlpha() - b.getAlpha()) > toleration) {
      return false;
      }
      return true;
      }
      }
      [/java]

And the NPE looks like:

[java]
08-25 22:22:39.304 31187 31222 E com.jme3.app.AndroidHarness: java.lang.NullPointerException
08-25 22:22:39.304 31187 31222 E com.jme3.app.AndroidHarness: at com.jme3.texture.image.ByteOffsetImageCodec.writeComponents(ByteOffsetImageCodec.java:85)
08-25 22:22:39.304 31187 31222 E com.jme3.app.AndroidHarness: at com.jme3.texture.image.DefaultImageRaster.setPixel(DefaultImageRaster.java:111)
08-25 22:22:39.304 31187 31222 E com.jme3.app.AndroidHarness: at mytest.TemplatedTexture.fill(TemplatedTexture.java:67)
[/java]

The ByteBuffer is null and I have no clue why that happen. I found this thread: http://hub.jmonkeyengine.org/forum/topic/imagepainter-npe-issue/ but there where no real solution to the problem.

Any suggestions why this is not working on android and how I can resolve this?

Maybe the image format you try to load isn’t supported by android? Are you sure you can display this texture at all?

Thanks for your answer normen. I can render my template image without any problems on desktop and android.

Here the image properties:
filetype: png
format: BGR8
size: 256*256
depth: null
mipMapSize: null
image data: one DirectBuffer
multiSamples: 1

This is my calling code:
[java]
Texture2D template = (Texture2D) assetManager.loadTexture(“Textures/template.png”);
TemplatedTexture texture = new TemplatedTexture(template);
Material unshadedMaterial = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
unshadedMaterial.setTexture(“ColorMap”, texture);
node.setMaterial(unshadedMaterial);
// fill all black parts with red
texture.fill(ColorRGBA.Black, ColorRGBA.Red, 0.1f);
[/java]

I have no clue :confused:

Got it working with:
[java]
private Image buildFromTemplate(Texture2D template) {
Image imageTemplate = template.getImage();
Image image = new Image();
image.setWidth(imageTemplate.getWidth());
image.setHeight(imageTemplate.getHeight());
Image.Format format = Image.Format.RGB8;
image.setFormat(format);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * format.getBitsPerPixel());
image.setData(byteBuffer);
return image;
}
[/java]