[cairo] Is posible create a png surface from a png buffer read from a zip file?
Lawrence D'Oliveiro
ldo at geek-central.gen.nz
Fri Nov 6 13:49:00 PST 2015
On Fri, 6 Nov 2015 15:18:23 +0100, KiwiLib KiwiLib wrote:
> I found the cairo_image_surface_create_from_png_stream
> <http://cairographics.org/manual/cairo-PNG-Support.html#cairo-image-surface-create-from-png-stream>
> () is hard to manage and not references directly a buffer. It wil be
> easy to implement ?.
This is an example of a programming technique known as a “callback”. It
is very useful for writing general, reusable code that makes minimal
assumptions about what data structures the caller might be
using--instead, you let the caller provide the interface to that.
Here is an example in Python, showing how to decode a PNG stream
contained in a bytes or array buffer. It comes from Qahirah
<https://github.com/ldo/qahirah>:
def create_from_png_bytes(data) :
"creates an ImageSurface from a PNG format data sequence. This can be" \
" of the bytes or bytearray types, or an array.array with \"B\" type code."
offset = 0
def read_data(_, data, length) :
nonlocal offset
if offset + length <= len(data) :
libc.memcpy(data, baseadr + offset, length)
offset += length
status = CAIRO.STATUS_SUCCESS
else :
status = CAIRO.STATUS_READ_ERROR
#end if
return \
status
#end read_data
#begin create_from_png_bytes
if isinstance(data, bytes) or isinstance(data, bytearray) :
data = array.array("B", data)
elif not isinstance(data, array.array) or data.typecode != "B" :
raise TypeError("data is not bytes, bytearray or array of bytes")
#end if
baseadr = data.buffer_info()[0]
return \
ImageSurface.create_from_png_stream(read_data, None)
#end create_from_png_bytes
More information about the cairo
mailing list