[cairo] Re: Pycairo + PNG Output

Pete Shinners pete at shinners.org
Wed Oct 12 10:38:48 PDT 2005


Vladimir Vukicevic <vladimirv <at> gmail.com> writes:
> Ideally, you really want pycairo to allocate its own buffer for the
> cairo image surface, and let you access it, to pass it to pygame
> directly as a 32-bbp ARGB image.  Otherwise, you waste lots of CPU
> doing png encoding/decoding for no reason, especially if you do this
> often...

This would ideal, it looks trivial to map an SDL_Surface onto an existing Cairo
surface, and let them share.

This would require a secret handshake between the pygame and pycairo. (Unless
one of these libraries wants to depend on the other, heh)

Traditionally with things like PIL, PyopenGL, and Pygame. Images were swapped
with "fromstring()" and "tostring()" functions. These work well enough, but
suffers a double-copy overhead.

Pygame recently added the "frombuffer()" function, which works the same, but
avoids the additional copy step on the Pygame side. If pycairo gained a
"tobuffer()" function then we could literally map the pixels between image
libraries with no copy overhead.

The Python C-API has full support for these buffers. Use the PyBuffer_Type
objects with PyBuffer_FromMemory and PyBuffer_FromObject. To get at the
underlying memory. The buffers can be declared readonly or readwrite.

On the other hand. The types of surfaces supported by these buffers may be more
limited than what is expressed in SDL_Surface and the cairo Surface. The buffers
can't take into account things like stride, etc.

What would be excellent is some common ImageBuffer object that could be shared
by all python image libraries.




More information about the cairo mailing list