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

Re: [pygame] pygame.image.save(...)



On Tue, Apr 27, 2010 at 8:23 PM, Luke Paireepinart <rabidpoobear@xxxxxxxxx> wrote:
Sounds like you're leaking memory somewhere, or maybe just you're
re-allocating space for your temporary framebuffers and the
interpreter's not garbage-collecting them fast enough?
Check your memory usage while you run your app.  If it shoots up right
away to some really high number, you should be suspicious. 
Here's the relevant code, showing how the PyGame surface is derived from OpenGL readbacks.  I'll see if I can check memory usage.
 
def glLibScreenSurf(rect=GLLIB_AUTO,type=GLLIB_RGB,framebuffer=0):
    if rect == GLLIB_AUTO:
        size = glGetFloatv(GL_VIEWPORT)
        size = [int(round(size[2])),int(round(size[3]))]
        rect = [0,0,size[0],size[1]]
    glPixelStorei(GL_PACK_ROW_LENGTH,0)
    glPixelStorei(GL_PACK_SKIP_ROWS,0)
    glPixelStorei(GL_PACK_SKIP_PIXELS,0)
    if type==GLLIB_RGBA:
        glPixelStorei(GL_PACK_ALIGNMENT,4)
    elif type==GLLIB_RGB:
        glPixelStorei(GL_PACK_ALIGNMENT,1)
    elif type==GLLIB_DEPTH:
        glPixelStorei(GL_PACK_ALIGNMENT,1)
    try:
        if type==GLLIB_DEPTH:
            data = "">
        else:
            data = "">
##        print len(data)
    except:
        previous = glGetIntegerv(GL_READ_BUFFER)
        if type==GLLIB_DEPTH:
            glReadBuffer(GL_DEPTH_ATTACHMENT_EXT)
            data = "">
        else:
            glReadBuffer(GL_COLOR_ATTACHMENT0_EXT+framebuffer)
            data = "">
        glReadBuffer(previous)
    if type==GLLIB_RGBA:
        return pygame.image.fromstring(data,(rect[2],rect[3]),'RGBA',1)
    elif type==GLLIB_RGB:
        return pygame.image.fromstring(data,(rect[2],rect[3]),'RGB',1)
    elif type==GLLIB_DEPTH:
##        data = "">
##        data = "">
##        data = "">
        return pygame.surfarray.make_surface(data)
def glLibTextureDataSurf(texture):
    glLibSelectTexture(texture)
    texturedata = np.array(glGetTexImage(texture.type,0,texture.format,GL_FLOAT))*255.0
    texturedatasurf = pygame.surfarray.make_surface(np.fliplr(texturedata.transpose((1,0,2))))
    return texturedatasurf
def glLibInternal_save_surf(surface,path,overwrite):
    if overwrite:
        pygame.image.save(surface,path)
    else:
        path = path.split(".")
        counter = 1
        while True:
            try:
                if counter == 1:
                    pygame.image.load(path[0]+"."+path[1])
                else:
                    pygame.image.load(path[0]+str(counter)+"."+path[1])
            except:break
            counter += 1
        if counter == 1:
            pygame.image.save(surface,path[0]+"."+path[1])
        else:
            pygame.image.save(surface,path[0]+str(counter)+"."+path[1])
def glLibSaveScreenshot(path,rect=GLLIB_AUTO,type=GLLIB_RGB,framebuffer=0,overwrite=False):
    surface = glLibScreenSurf(rect,type,framebuffer)
    glLibInternal_save_surf(surface,path,overwrite)

Just out of curiosity, how do you know that the saves are taking
longer?  How are you timing them?  Are they taking so long that they
take longer than your frame-rate?
The framerate of the program is constant, but when the OpenGL program saves a file every frame, it gets slower with each passing frame. 
Ian