read gif file to a bytebuffer

N

Nazneen Malik

hi all,

I need to read a gif file to a bytebuffer

what steps do i have to take?

if I get it as an image, i cant cast it to a buffered image.

I need to do this for texture mapping in OpenGL using JOGL and I am
new to java

please help

Regards,
Nazneen
 
N

Nazneen Malik

hi all,

I need to read a gif file to a bytebuffer

what steps do i have to take?

if I get it as an image, i cant cast it to a buffered image.

I need to do this for texture mapping in OpenGL using JOGL and I am
new to java

please help

Regards,
Nazneen



this is my code so far:

Image img;

img = Toolkit.getDefaultToolkit().getImage("Smiley-face.gif");

try {
FileImageInputStream fs = new FileImageInputStream(new File("Smiley-
face.gif"));


int cnt = img.getHeight(null)*img.getWidth(null);
int i = 0;

while(i< cnt)
{
pixels.put(fs.readByte());
++i;
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
N

Nazneen Malik

I need to read a gif file to a bytebuffer
what steps do i have to take?
if I get it as an image, i cant cast it to a buffered image.
I need to do this for texture mapping in OpenGL using JOGL and I am
new to java

this is my code so far:
       Image img;
         img = Toolkit.getDefaultToolkit().getImage("Smiley-face.gif");
         try {
                   FileImageInputStream fs = new FileImageInputStream(new File("Smiley-
face.gif"));
                   int cnt = img.getHeight(null)*img.getWidth(null);
                   int i = 0;
                   while(i< cnt)
                   {
                                   pixels.put(fs.readByte());
                                   ++i;
                   }
[...]

What format do you need the image data in?  If you're looking for pixel  
data, then no...I wouldn't think the code you posted would work.  After  
all, you're trying to read bytes from a compressed GIF file as if each one  
was an actual pixel.  That's completely wrong.

Maybe I'm missing something, but it seems to me that you ought to be using  
the ImageIO.read() method to return a BufferedImage, from which you can  
then get a Raster instance (using BufferedImage.getData()) that has the  
actual bytes for the image.

If the GIF file is not actually in the same format that you need for your  
OpenGL texture, then you'll need to convert first.  I think the simplest  
way to do that would be to create a new BufferedImage instance in the  
desired format, get the Graphics2D instance for it, draw your source image  
into the BufferedImage with the Graphics2D instance, and then get the  
Raster from the BufferedImage instance.

Pete

yep...ur right...i changed it to

img is my buffered gif image

File sourceimage = new File("Smiley-face.gif");
try {

img = ImageIO.read(sourceimage);


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


int w,h;

w = img.getWidth(null);
h = img.getHeight(null);

int i = w * h;

pixels.allocateDirect(i);


byte[] data = ((DataBufferByte)
img.getRaster().getDataBuffer()).getData();

//pixels.put(data);

now the last line throws a null pointer exception

why do i have to draw my source image into the BufferedImage with the
Graphics2D instance?

do you see any obvious errors?

tx so much!
Nazneen
 
N

Nazneen Malik

[...]
                   byte[] data = ((DataBufferByte)
img.getRaster().getDataBuffer()).getData();
                   //pixels.put(data);
now the last line throws a null pointer exception
why do i have to draw my source image into the BufferedImage with the
Graphics2D instance?

I don't know that you do.  It depends on whether or not the exact bytes  
 from the GIF you're loading can be copied directly into your texture  
buffer.  If they can, you don't need to draw your source image into a new  
image.  If they can't, then you need to in order to get Java to convert  
the image format to one that's correct.
do you see any obvious errors?
I discovered TextureIO...so no issues:)
tx though:)
Well, you aren't specific about _what_ is null when you get the exception,  
and I don't actually know that you can assume the DataBuffer is actually a  
DataBufferByte (maybe you can, but I don't personally know that to be  
true).  At the very least, if you want to use that code, you should debug  
it to figure out exactly what's failing.

That said, it seems to me that you should be able to just call  
Raster.getPixels() to copy the samples directly into an int[].  You can  
copy the samples into your bytes array from there.  Maybe not as  
convenient as going to DataBufferByte, but then doing it that way doesn't  
break if the DataBuffer turns out not to be a DataBufferByte.  :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top