8

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.

orlp
  • 98,226
  • 29
  • 187
  • 285

1 Answers1

11

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.

CAMOBAP
  • 5,011
  • 8
  • 53
  • 84
orlp
  • 98,226
  • 29
  • 187
  • 285
  • 4
    The link is now dead. – rr- Jan 31 '15 at 11:25
  • 4
    link has been resurrected – CAMOBAP Oct 28 '15 at 13:22
  • 1
    This is a great resource. One thing I discovered is that if your ReadDataFromInputStream function does not actually copy real PNG data to outBytes, it causes png_read_info() to crash the program. I was just returning placeholder bytes during development, and it was crashing. When I returned real bytes, it worked. (This seems a bit scary since it seems to imply that if the .png file is corrupt, libpng can crash instead of returning an error.) – M Katz Jan 21 '16 at 02:59