libpng load file from memory buffer

orlp picture orlp · Jan 27, 2013 · Viewed 7.4k times · Source

I tried looking through the libpng documentation, but didn't find what I was looking for.

I have a PNG file fully in a memory buffer, how can I load this file (apart from the obvious solution of creating a temporary file)?

Not sure if it's relevant, but I'm calling libpng from Python using ctypes.

Answer

orlp picture orlp · Jan 28, 2013

I found this article which describes pretty well how to read a PNG file from memory.

Summarized, you have to create a custom callback function and give it to libpng using png_set_read_fn. Then in that callback function you read from your memory buffer rather than a file.