Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  loading an image with lwjgl devil  (Read 2945 times)
0 Members and 1 Guest are viewing this topic.
Offline Surgeon

Junior Newbie




Java games rock?


« Posted 2005-03-07 11:53:33 »

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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
            
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 Sad
Thanks in advance!
Offline Deadcow

Senior Newbie




Back from beyond ... Moo!


« Reply #1 - Posted 2005-03-08 05:11:15 »

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

1  
ImageIcon ii = new ImageIcon( path );


Hope this helps.
Offline Surgeon

Junior Newbie




Java games rock?


« Reply #2 - Posted 2005-03-08 06:46:54 »

Thank you Deadcow, but ImageIcon does not load the formats I want to have in my program, ie PSD, PCX  etc Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2005-03-08 09:37:12 »

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.

Offline tom
« Reply #4 - Posted 2005-03-08 11:01:14 »

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.

Offline princec

« JGO Spiffy Duke »


Medals: 973
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2005-03-08 12:11:32 »

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 Smiley

Offline Surgeon

Junior Newbie




Java games rock?


« Reply #6 - Posted 2005-03-08 15:33:06 »

Thanks a lot for your help!  Roll Eyes 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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
// 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);
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #7 - Posted 2005-03-08 16:00:26 »

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.

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.

Offline tom
« Reply #8 - Posted 2005-03-08 17:41:12 »

Quote

1  
2  
3  
//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);

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

Offline tom
« Reply #9 - Posted 2005-03-08 18:42:56 »

Quote

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.

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 Roll Eyes

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.

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #10 - Posted 2005-03-09 10:32:13 »

Quote

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 Roll Eyes

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.
Quote

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

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.

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (107 views)
2017-09-30 02:57:34

theagentd (134 views)
2017-09-26 18:23:31

cybrmynd (244 views)
2017-08-02 12:28:51

cybrmynd (236 views)
2017-08-02 12:19:43

cybrmynd (237 views)
2017-08-02 12:18:09

Sralse (251 views)
2017-07-25 17:13:48

Archive (863 views)
2017-04-27 17:45:51

buddyBro (1006 views)
2017-04-05 03:38:00

CopyableCougar4 (1564 views)
2017-03-24 15:39:42

theagentd (1372 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!