Menu

#190 glReadPixels breaks 2.x compat. breaks pygame.

v3.0.0
closed-fixed
GL (74)
5
2008-06-13
2008-05-07
Anonymous
No

Hi,

with glReadPixels changing its interface from 1, 2.x, it breaks pygame.image.save(screen).

This is the only pyopengl function that pygame uses.

It would be nice if pyopengl 3.x kept the same interface, of returning a string.

cheers,

Discussion

  • Mike C. Fletcher

    Logged In: YES
    user_id=34901
    Originator: NO

    Pygame has already changed their code to support PyOpenGL 3.x, the new version allows the user to specify the data-type they want to have returned from array-producing code, but at the moment strings are not an option (no way to generate them in the correct dimensions yet without potentially falling afoul of the small-string caching mechanisms).

    I'm afraid I'll have to mark this "won't fix" as a result. If you feel this is a major problem, reopen the ticket and I'll consider it again, but aside from maybe implementing a "string" output type that could be selected, I don't see it as a desirable change to make the function always return strings.

     
  • Mike C. Fletcher

    • assigned_to: nobody --> mcfletch
    • status: open --> closed-wont-fix
     
  • Rene Dudfield

    Rene Dudfield - 2008-06-06

    Logged In: YES
    user_id=2042
    Originator: NO

    Hi,

    lots of old versions of pygame are still in use - so this pyopengl API break is still important to fix for pyopengl+pygame users using old versions of pygame.

    I don't think it should always return a string, but instead default to returning a string. This will break less software since this is what 1.x and 2.x do. Note pygame using programs are not the only program that expects a string to be returned.

    I don't know what you're talking about with the small string caching mechanisms breaking things? Is this a pyopengl3 small string cache? You can just convert a ctypes string to a python string with no issues.

    I can make the fix if you think it's ok?

    Here's a work around screenshot function for gl - that works for all versions of pyopengl.
    The main lines are these:
    data = glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE)
    if hasattr(data, "tostring"):
    data = data.tostring()

    Where it looks to see if the return variable has a tostring() method - which is what the numpy instance has.

    def savescreen(screen, filename):

    def readScreen(x, y, width, height):
    """ Read in the screen information in the area specified """
    glFinish()
    glPixelStorei(GL_PACK_ALIGNMENT, 4)
    glPixelStorei(GL_PACK_ROW_LENGTH, 0)
    glPixelStorei(GL_PACK_SKIP_ROWS, 0)
    glPixelStorei(GL_PACK_SKIP_PIXELS, 0)

    data = glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE)
    if hasattr(data, "tostring"):
    data = data.tostring()

    return data
    def saveImageData(width, height, data, filename):
    """ Save image data """
    surface = pygame.image.fromstring(data, (width, height), 'RGB', 1)
    pygame.image.save(surface, filename)

    data = readScreen(0,0, screen.get_width(), screen.get_height())
    saveImageData(screen.get_width(), screen.get_height(), data, filename)

     
  • Rene Dudfield

    Rene Dudfield - 2008-06-06
    • status: closed-wont-fix --> open-wont-fix
     
  • Mike C. Fletcher

    Logged In: YES
    user_id=34901
    Originator: NO

    Okay, I'm swayed.

    I was thinking of using the Python string as an input. If you convert from another object it should be fine. I'm okay with the change (can't say I like it, but compatibility is a pretty high priority).

    If you don't get to it I'll try to get it done on Friday before doing a release.

     
  • Mike C. Fletcher

    Logged In: YES
    user_id=34901
    Originator: NO

    Okay, glReadPixels and glGetTexImage now return strings in the default calls iff you specify GL_UNSIGNED_BYTE format. There's a test case that should trigger if that changes in the future.

     
  • Mike C. Fletcher

    Logged In: YES
    user_id=34901
    Originator: NO

    Forgot to close.

     
  • Mike C. Fletcher

    • status: open-wont-fix --> closed-fixed
     

Log in to post a comment.