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:
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