[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] .png saving



Brian Fisher wrote:
I've been playing with the latest pygame source code, and it looks
like this is the situation with openGL display surfaces and saving
pngs...

the image module source has a c function in it "opengltosdl" in it
that was written to use glReadPixels from the pyOpenGL package to get
a surface from the openGL display. It looks like it is used with the
basic image save function and with image.tostring so it should work
with those 2 things - however with PyOpenGL 3.0, glReadPixels now
returns a "OpenGL.arrays.ctypesarrays" instead of a python string, so
"opengltosdl" segfaults when trying to copy data from the array into a
surface.

the imagext module has a c function to save a surface to a png, but
it's not in pygame 1.7. it appears to use libpng directly, so it seems
pygame should be able to save a png. However the function uses a blit
in order to convert the surface to a format that is appropriate for
png saving (24 or 32 bit), so the current version of that function
will fail with an opengl display because openGL display surfaces are
created with "dummy" surface contents, which can be passed to blit
functions, but will not work (apparently they crash in such
instances).

So with PyOpenGL 3.0 and pygame svn as it is, all options to get at
the openGL display will crash...

----
so does anybody know how to get a string from a ctypes sized array in
python or how to get the void * to a ctypes sized array in extension
source code?

ctypes objects support the buffer interface, and slicing a entire buffer object as a slice returns a string.

>>> from ctypes import *
>>> A3 = c_short * 3
>>> a = A3(1, 2, 3)
>>> buffer(a)[:]
'\x01\x00\x02\x00\x03\x00'

c_void_p objects and Python integers are pretty much interchangeable, so you can use addressof(). An array can also be cast to c_void_p.

>>> vp = cast(a, c_void_p)
>>> vp
c_void_p(13711000)

If the array is an array of character then it has a raw attribute.

>>> C5 = c_char * 5
>>> c = C5('a','b','c','d','e')
>>> c.raw
'abcde'

--
Lenard Lindstrom
<len-l@xxxxxxxxx>