Okay, I’ve been trying to use getPixels and setPixels in the Raster within a BufferedImage, and it is making me completely insane. I don’t know what the last parameter, the array, is for. It seems pointless, and no matter what I put in there it throws an exception and bugs out. I just want some pixels! Is that so hard?
All I am trying to do is copy one image’s pixels into another so that I can use different image types. I’m sure there is some way to simply change a BufferedImage from RGB to RGBA, but I couldn’t find it, and even if there is an easier way, it would be great if someone could tell me what those random parameters are for anyway.
Essentially I have loaded an image from a file which does not have an alpha channel (at least when I try changing pixels to alpha it does nothing) and I want it to allow transparency, and therefore the fourth parameters (alpha).
BufferedImage b = ImageIO.read(new java.io.File(System.getProperty("user.dir")+"/test.gif"));
BufferedImage r = new BufferedImage(b.getWidth(),b.getHeight(),BufferedImage.TYPE_INT_ARGB);
int[] pixels = null;
r.getRaster().setPixels(0,0,r.getWidth(),r.getHeight(),b.getData().getPixels(0,0,r.getWidth(),r.getHeight(), ???--> pixels <--??? ));
I put arrows next to what is confusing me. What the hell is that for? Every example I’ve found seems to put a null value in it. By the way, this code throws the exception
java.lang.ArrayIndexOutOfBoundsException: 1855838776
at java.awt.image.SinglePixelPackedSampleModel.setPixels(SinglePixelPackedSampleModel.java:670)
at java.awt.image.WritableRaster.setPixels(WritableRaster.java:549)
at sun.awt.image.SunWritableRaster.setPixels(SunWritableRaster.java:311)
at jpixel.PixelWindowTest$Panel.loadImage(PixelWindowTest.java:43)
at jpixel.PixelWindowTest$Panel.<init>(PixelWindowTest.java:24)
at jpixel.PixelWindowTest.<init>(PixelWindowTest.java:12)
at jpixel.PixelWindowTest.main(PixelWindowTest.java:107)
I’ve gotten a million of these, because the required array size seems very unclear. This particular one is a result of the setPixels method (where the parameter’s use is clear, because it’s a mutator method and therefore passing something to it makes sense), what really confuses me is the getPixels method, and what its array parameter means, and what it’s for.
So if anyone could enlighten me that would be great, and any ideas on how to change the initial type of a BufferedImage after it has been created (to TYPE_INT_ARGB so I can use alpha) would be excellent.
Thanks.