Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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 3347 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: 1059
Projects: 3
Exp: 20 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  
 
 

 
EgonOlsen (1669 views)
2018-06-10 19:43:48

EgonOlsen (1706 views)
2018-06-10 19:43:44

EgonOlsen (1158 views)
2018-06-10 19:43:20

DesertCoockie (1585 views)
2018-05-13 18:23:11

nelsongames (1185 views)
2018-04-24 18:15:36

nelsongames (1709 views)
2018-04-24 18:14:32

ivj94 (2533 views)
2018-03-24 14:47:39

ivj94 (1758 views)
2018-03-24 14:46:31

ivj94 (2835 views)
2018-03-24 14:43:53

Solater (971 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!