Question

I have been reading a gif Image as an ImageInputStream using java.mageio.stream api. Is there a way I can parser that stream "manually" , in order to find the index position of the beginning of the image descriptor block? According to the Gif documentation, the Image Separator - which identifies the beginning of an Image Descriptor, the fixed value 0x2C. I have been reading each byte of the stream and matched it with the "0x2c" value, but obviously this is incorrect, as there are "attributes" that are more than one byte values. I am trying to do this manually, as I am trying to avoid loading the whole image at this point. Any ideas would be more than welcomed. Thanks in advance

Was it helpful?

Solution

Just to provide an answer, so we can mark it as answered:

Instead of manually parsing the GIF structure, the easiest way of getting the data from the GIF image descriptor in Java is using ImageIO.

For simple values like image width and height, you can use the corresponding methods on ImageReader like this:

ImageInputStream stream = ImageIO.createImageInputStream(gif); // gif is File or InputStream
ImageReader reader = ImageIO.getImageReaders(stream).next();
reader.setInput(stream);
int w = reader.getWidth(0);
int h = reader.getHeight(0);

For more information on the GIF, you can use:

IIOMetadata metadata = reader.getImageMetadata(0);

For native metadata format, see GIF metadata, for standard (format neutral) metadata format, see standard metadata.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top