[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] chewy changes to cvs



> > maybe this is related -- I am experiencing some
> > strangeness with the surfarray module.  

> 
> the 3d arrays should always be in RGB order,
> surfarray
> attempts to also reverse data that is in BGR order.
> there's likely a case or two that is backwards here
> :[

> > also array3d seems to either return garbled
> results or
> > cause a segfault. 

here is a script that shows these two things -- when I
run it getting pixels using array3d it crashes, and
when I mix blit_array and pixels3d calls it reverses
colors.  the reversing doesn't happen on computers
that support 24 bit mode.  look in function blitBlob
--Peter



__________________________________________________
Do You Yahoo!?
Yahoo! Shopping - Thousands of Stores. Millions of Products.
http://shopping.yahoo.com/
#!/usr/bin/env python

# look in blitBlob for problems relating to surfarray in 32bit color

import math
import pygame
from pygame.locals import *
rect = pygame.rect.Rect

try:
    from Numeric import *
    from RandomArray import *
    import pygame.surfarray
except ImportError:
    raise SystemExit, 'This example requires Numeric and the pygame surfarray module'

gSize = (640,480)
gSpotSize = (80,80)
gRed= array((255,0,0))
gGreen = array((0,255,0))
gBlue = array((0,0,255))

def makeSpot(size=(80,80),color=array((255,255,255))):
    #to do: make it do elliptical ones too
    maxdist = max(size[0],size[1])/2
    tempspotX = repeat(arange(size[0])[:,NewAxis,NewAxis],[3],-1) - size[0]/2
    tempspotY = repeat(arange(size[1])[:,NewAxis],[3],-1) - size[1]/2
    tempspot = sqrt(tempspotX**2 + tempspotY**2)
    tempspot = clip((maxdist - tempspot),0,maxdist).astype(Float) / maxdist
    tempspot = (tempspot * color).astype(Int)
    tempsurf = pygame.Surface(size)
    pygame.surfarray.blit_array(tempsurf, tempspot)
    return tempsurf

def rectConvert(someRect):
    return (someRect[0],someRect[1],someRect[0]+someRect[2],someRect[1]+someRect[3])
    

def blitBlob(dest, d_rect, source,  s_rect):
    # here, just reading but using .array3d causes a 'couldn't be "read"' error
    tempdest = pygame.surfarray.pixels3d(dest)[d_rect.left:d_rect.right,d_rect.top:d_rect.bottom]
    destavg = repeat(add.reduce(tempdest[:,:],-1)[:,:,NewAxis],[3],-1)
    tempsource =pygame.surfarray.pixels3d(source)[s_rect.left:s_rect.right,s_rect.top:s_rect.bottom]
    srcavg = repeat(add.reduce(tempsource,-1)[:,:,NewAxis],[3],-1)
    newarray = where(greater(srcavg,destavg),tempsource,tempdest)
    newsurf = pygame.Surface(s_rect.size)
    pygame.surfarray.blit_array(newsurf,newarray)
    # using pixels3d retains color; using dest.blit reverses color
    pygame.surfarray.pixels3d(dest)[d_rect.left:d_rect.right,d_rect.top:d_rect.bottom] = newarray
    #dest.blit(newsurf,d_rect.topleft)

def main():
    pygame.init()
    screen = pygame.display.set_mode(gSize)
    
    pygame.event.set_blocked(MOUSEMOTION)
    spot1 = makeSpot(size=(40,40), color=gBlue)
    spot2 = makeSpot(size=(40,40), color=gBlue)
    for k in range(0,100,30):
        for i in range(0,gSize[0]-200,gSpotSize[0]):
            for j in range(0,gSize[1]-200,gSpotSize[1]):
                blitBlob(screen,spot1.get_rect().move(i+k,j+k),spot1, spot1.get_rect() )
                blitBlob(screen,spot2.get_rect().move(i+40+k,j+40+k),spot2, spot2.get_rect())
                pygame.display.flip()
        
    while 1:
        event = pygame.event.poll()
        if event.type in (QUIT, MOUSEBUTTONDOWN):
            break


if __name__ == '__main__':
    
    main()