loading an image with lwjgl devil

I am trying to load an image using the lwjgl devil lib but it’s not working… And yes, I want to draw it on the swing component, is it possible?

            
IL.ilLoadImage(path);

int width = IL.ilGetInteger(IL.IL_IMAGE_WIDTH);
int height = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT);

IL.ilConvertImage(IL.IL_RGB, IL.IL_BYTE);

ByteBuffer scratch = ByteBuffer.allocateDirect(width * height * 3);
IL.ilCopyPixels(0, 0, 0, width, height, 1, IL.IL_RGB, IL.IL_BYTE, scratch);

byte[] imageBuffer = new byte[scratch.limit()];
scratch.get(imageBuffer);

ImageIcon ii = new ImageIcon(imageBuffer);

Though imageBuffer contains some values and the image is loaded (i see the width) ii is not displayed :frowning:
Thanks in advance!

ilCopyPixels returns an array of pixels. The ImageIcon constructor wants the bytes of an image file. Why don’t you just try that :


ImageIcon ii = new ImageIcon( path );

Hope this helps.

Thank you Deadcow, but ImageIcon does not load the formats I want to have in my program, ie PSD, PCX etc :slight_smile:

Here is your problem.

ImageIcon(byte[] imageData)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF or JPEG.

You need another way to get the data into an Image. The easiest way, although a little dated, is to use Toolkit.createImage(ImageProducer producer). So the class that is loading using DevIL can implement the ImageProducer interface and can be passed as the argument to the createImage(…) call.

Pack the data read using devil into a int[] (alpha, red, green, blue) array. Create a java.awt.image.BufferedImage of the size of the image. Then use BufferedImage.setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) with the int array. Then you can pass the BufferedImage into the ImageIcon constructor. Easy, huh.

Even more cunningly, perhaps you should copy that intermediary bufferedimage into a compatible image for the display, and give the compatible image to the ImageIcon. Might be faster to render.

Cas :slight_smile:

Thanks a lot for your help! ::slight_smile: Unfortunately the image is crapped up and a chaostic noise is displayed instead. Maybe you’ll find a minute to look through the code, please:


// Pack the data read using devil into a int[] (alpha, red, green, blue) array

IL.ilConvertImage(IL.IL_RGB, IL.IL_BYTE);
ByteBuffer scratch = ByteBuffer.allocateDirect(width * height * 4);
scratch = IL.ilGetData();

IntBuffer ib = scratch.asIntBuffer();
int[] array = new int[ib.limit()];
ib.get(array);

//Create a java.awt.image.BufferedImage of the size of the image
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

//use BufferedImage.setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 
//with the int array.
bi.setRGB(0, 0, width, height, array, 0, 1);

//give the compatible image to the ImageIcon            
ii = new ImageIcon(bi); 

[quote]Pack the data read using devil into a int[] (alpha, red, green, blue) array. Create a java.awt.image.BufferedImage of the size of the image. Then use BufferedImage.setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) with the int array. Then you can pass the BufferedImage into the ImageIcon constructor. Easy, huh.
[/quote]
Last time I tried to use that method, only the first line of pixels was copied, everything else was ignored(bug in Java or my code?). That is why I ddin’t recommend that method.

Look at the BufferedImage javadoc. Use the width as scansize.:
“bi.setRGB(0, 0, width, height, array, 0, width);”

Implementing the ImageProducer interface yourself is just to complicated. If you had to use that crap you would use MemoryImageSource instead. But why would you when you got BufferedImage. Btw, I’m convinced the sun designers were high when they created the old Image stuff. What a mess ::slight_smile:

Probably your code. Did you understand this line from the javadoc:
“pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)];”

Actually, it is not too bad. All you are doing is receiving an ImageConsumer, then you send all your data in one shot to the consumer.

Yeah. When I did it, I tried to dump the whole array at once, using the width as the scansize and it didn’t work. It might have been that I set up the wrong image type.