juce_loadWithCoreImage and stream position

So I have an InputStream which contains some data as well as some PNG data – if we read the stream and use

      Image image (PNGImageFormat().decodeImage (stream));

      int myIntValue = stream.readInt();

with JUCE_USING_COREIMAGE_LOADER defined then juce_loadWithCoreImage reads in the entire stream:

     MemoryBlock data;
     input.readIntoMemoryBlock (data, -1);

but never resets the stream position so then the next read (myIntValue) fails because it’s at the end of the stream.

With JUCE_USING_COREIMAGE_LOADER undefined then PNGHelpers::readImage() works fine and keeps the stream position correct for the next read.


I don’t think those classes make any promises about the state that the stream is left in, do they?

And it’d be unwise for it to be a requirement that they should always reset the stream, since this can be a very expensive operation (and even impossible) on some types of stream, and usually isn’t needed if you’re just loading from a file or memory.

I guess not – but It’s a bitch 'cause I can’t find where JUCE_USING_COREIMAGE_LOADER is defined in my code – if I grep for it I can’t find it in Xcode.

Nevermind – found it…


…but if you need to reset the stream after you’ve read the image, you can do that yourself by just taking its position first, and seeking afterwards, right?

I just need to figure out the number of bytes that decodeImage() read – I don’t think there’s an ImagePixelData::getSize() method – I’m not seeing any way to know how many bytes were read?



oh, I thought from your original post you were complaining that it wasn’t resetting the stream?

If you need to seek to the end of the image data, that’d be much more tricky, as all the seeking within the stream will be dependent on the algorithm that’s doing the reading. Not sure what to suggest, but certainly if I was creating a container format to hold multiple images, it’d be smart to make it encode a size for each image data block, and not to rely on the decoder to finish in the correct place.

Yeah – I’m going to save the Image data as a MemoryBlock in the file with a value to define it’s length and use that to keep the stream position in synch… that way I can read the data back into a MemoryBlock and load the image from that.

My original post was that I felt that juce_loadWithCoreImage() should set the stream position after loading to the end of the image data position… the same way PNGHelpers::readImage() does.