4

Why would this method throw an index out of bounds error? Trying to create an image from data I generate myself and I expected this would work.

private BufferedImage getImageFromFloatArray(float[] data, int w, int h) {
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    System.out.println("Image pixel array size: "
                    + ((DataBufferInt) img.getRaster().getDataBuffer())
                            .getData().length);
    System.out.println("Datasize: " + data.length);
    WritableRaster raster = img.getRaster();
    raster.setPixels(0, 0, w, h, data);
    return img;
}

Stacktrace

Image pixel array size: 800000
Datasize: 800000
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 800000
    at java.awt.image.SampleModel.setPixels(Unknown Source)
    at java.awt.image.WritableRaster.setPixels(Unknown Source)
    at image.PixelAraryToImageTest.getImageFromFloatArray(PixelAraryToImageTest.java:36)
7
  • Arent you using threads ? There cound be possibility then, that you change data array. Commented Jul 30, 2013 at 19:50
  • No I am afraid it is singlethreaded, the testcase just generates a random data array and tries to create an image. Commented Jul 30, 2013 at 19:53
  • Is it an ignorant question to ask why you are storing floats into an INT_ARGB type buffer? Commented Jul 30, 2013 at 19:58
  • @Jongware Float is 4byte data type, so does ARGB. If you want to "visualise" float data as image, you dotn have any other choice. Commented Jul 30, 2013 at 20:01
  • I've solved this in my answer below. Thanks @arynaq for your post, this was interesting. Commented Jul 30, 2013 at 20:11

1 Answer 1

1

Try using the Raster width and height variables instead of the BufferedImage width and height variable. Also use Raster.getMinX() and Raster.getMinY()

Every value in the float array isn't a pixel value. Every value is a color component value. So a 2x1 image would actually need to be of length 4, as you have ARGB color components. To make it a 2x1 image red for example, would require something like...

int numColorComponents = 4;
float[] data = new float[imgWidth*imgHeight*numColorComponents];
raster.setPixels(minX,minY, rasterWidth,rasterHeight, data);

Also, unlike other graphics frameworks, the float buffer here isn't a buffer of normalized values. Its value between [0, 255]. So, to set 2x1 image to opaque red, your buffer would be:

float alpha = 255;
float red = 255;   
float[] buffer = new float[]{alpha,red,0,0,alpha,red,0,0};
Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes this makes sense, I will have to decompose each sample into four components, will post update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.