0

I'm trying to create an image using this array: [-8421505, -8421505, -8421505, -8421505,...] its length is: 62416

BufferedImage img = new BufferedImage(166, 376, BufferedImage.TYPE_INT_RGB);
int pixels[] = new int[166 * 376];

and here is where the error should be

img.getRaster().setPixels(0, 0, 166 , 376, pixels);

then I just save it

File file = new File("new.png");
ImageIO.write(img, "png", file);  

which is:

ArrayIndexOutOfBoundsException : 62416

Yousef
  • 450
  • 6
  • 13
  • 1
    Maybe this might be helpful: From the [docs](https://docs.oracle.com/javase/8/docs/api/java/awt/image/WritableRaster.html#setPixels-int-int-int-int-double:A-): `ArrayIndexOutOfBoundsException` - if the coordinates are not in bounds, or if `dArray` is too small to hold the input. – Zabuzard Sep 27 '17 at 13:53
  • (about setPixels method): Also this from javadocs: Sets all samples for a rectangle of pixels from a double array containing one sample per array element. An ArrayIndexOutOfBoundsException may be thrown if the coordinates are not in bounds. However, explicit bounds checking is not guaranteed. – pringi Sep 27 '17 at 13:55
  • 1
    It is because your raster uses 1 ints per channel (3 in total). so your array needs to be `new int[166*376*3]` – matt Sep 27 '17 at 14:09
  • thank you @matt , the error is gone but it has drawn 3 copies of the original picture. how can I make it just ordinary, meaning: I have an array that holds binary data for 376*166 pixel. and I want to create an image using this array. As simple as that – Yousef Sep 27 '17 at 18:10
  • and also for some reason, it places the picture on the upper side of the whole picture (376*166). and the colors are turning to grayscale. is there a better way of creating my image using the array@matt – Yousef Sep 27 '17 at 18:28
  • You need to mask your ints and place them in your array correctly. eg. `pixels[3*i] = data[i]&0xff; `pixels[3*i+1]= (data[i]>>8)&0xff;` `pixels[3*i+2]=(data[i]>>16)&0xff;` – matt Sep 27 '17 at 18:35
  • Do you want binary data, or do you want color data? Maybe if you can include 25 elements of your array (eg 5x5 image) and describe what you want it to look like, I (or somebody else) will provide you with a more complete example for an answer. – matt Sep 27 '17 at 18:37
  • I solved the problem thanks for your help @matt can you answer so I can choose it. – Yousef Sep 27 '17 at 20:44

1 Answers1

1

For some reason, even though it seems like BufferedImage.TYPE_INT_RGB should have 1 int per pixel. When you use WritableRaster#setPixels it actually needs 3 ints per pixel.

Given some input data,

int[] values = {...};

Where each int corresponds to a pixel, with 8 bits for RGB respectively. They would need to be put into a bigger array, and unpacked.

int pixels[] = new int[values.length*3];
for(int i = 0; i<values.length; i++){
   pixels[3*i] = (values[i]>>16)&0xff;
   pixels[3*i+1] = (values[i]>>8)&0xff;
   pixels[3*i+2] = (values[i])&0xff;
}

I checked this by creating a values array with red, green and blue.

int[] values = new int[166*376];
for(int i = 0; i<166*125; i++){
    values[i] = 0xff0000; //red
}
for(int i = 166*125; i<166*250; i++){
    values[i] = 0xff00; //green
}
for(int i = 166*250; i<166*376; i++){
    values[i] = 0xff; //blue
}
matt
  • 10,892
  • 3
  • 22
  • 34