<s> from pypy . rpython . lltypesystem import lltype , rffi <EOL> from pypy . rlib . rsdl import RSDL <EOL> def get_rgb ( color , format ) : <EOL> rgb = lltype . malloc ( rffi . CArray ( RSDL . Uint8 ) , <NUM_LIT:3> , flavor = '<STR_LIT>' ) <EOL> try : <EOL> RSDL . GetRGB ( color , <EOL> format , <EOL> rffi . ptradd ( rgb , <NUM_LIT:0> ) , <EOL> rffi . ptradd ( rgb , <NUM_LIT:1> ) , <EOL> rffi . ptradd ( rgb , <NUM_LIT:2> ) ) <EOL> r = rffi . cast ( lltype . Signed , rgb [ <NUM_LIT:0> ] ) <EOL> g = rffi . cast ( lltype . Signed , rgb [ <NUM_LIT:1> ] ) <EOL> b = rffi . cast ( lltype . Signed , rgb [ <NUM_LIT:2> ] ) <EOL> result = r , g , b <EOL> finally : <EOL> lltype . free ( rgb , flavor = '<STR_LIT>' ) <EOL> return result <EOL> def get_rgba ( color , format ) : <EOL> rgb = lltype . malloc ( rffi . CArray ( RSDL . Uint8 ) , <NUM_LIT:4> , flavor = '<STR_LIT>' ) <EOL> try : <EOL> RSDL . GetRGBA ( color , <EOL> format , <EOL> rffi . ptradd ( rgb , <NUM_LIT:0> ) , <EOL> rffi . ptradd ( rgb , <NUM_LIT:1> ) , <EOL> rffi . ptradd ( rgb , <NUM_LIT:2> ) , <EOL> rffi . ptradd ( rgb , <NUM_LIT:3> ) ) <EOL> r = rffi . cast ( lltype . Signed , rgb [ <NUM_LIT:0> ] ) <EOL> g = rffi . cast ( lltype . Signed , rgb [ <NUM_LIT:1> ] ) <EOL> b = rffi . cast ( lltype . Signed , rgb [ <NUM_LIT:2> ] ) <EOL> a = rffi . cast ( lltype . Signed , rgb [ <NUM_LIT:3> ] ) <EOL> result = r , g , b , a <EOL> finally : <EOL> lltype . free ( rgb , flavor = '<STR_LIT>' ) <EOL> return result <EOL> def get_pixel ( image , x , y ) : <EOL> """<STR_LIT>""" <EOL> bpp = rffi . getintfield ( image . c_format , '<STR_LIT>' ) <EOL> pitch = rffi . getintfield ( image , '<STR_LIT>' ) <EOL> p = rffi . ptradd ( image . c_pixels , y * pitch + x * bpp ) <EOL> if bpp == <NUM_LIT:1> : <EOL> return rffi . cast ( RSDL . Uint32 , p [ <NUM_LIT:0> ] ) <EOL> elif bpp == <NUM_LIT:2> : <EOL> p = rffi . cast ( RSDL . Uint16P , p ) <EOL> return rffi . cast ( RSDL . Uint32 , p [ <NUM_LIT:0> ] ) <EOL> elif bpp == <NUM_LIT:3> : <EOL> p0 = rffi . cast ( lltype . Signed , p [ <NUM_LIT:0> ] ) <EOL> p1 = rffi . cast ( lltype . Signed , p [ <NUM_LIT:1> ] ) <EOL> p2 = rffi . cast ( lltype . Signed , p [ <NUM_LIT:2> ] ) <EOL> if RSDL . BYTEORDER == RSDL . BIG_ENDIAN : <EOL> result = p0 << <NUM_LIT:16> | p1 << <NUM_LIT:8> | p2 <EOL> else : <EOL> result = p0 | p1 << <NUM_LIT:8> | p2 << <NUM_LIT:16> <EOL> return rffi . cast ( RSDL . Uint32 , result ) <EOL> elif bpp == <NUM_LIT:4> : <EOL> p = rffi . cast ( RSDL . Uint32P , p ) <EOL> return p [ <NUM_LIT:0> ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def set_pixel ( image , x , y , pixel ) : <EOL> """<STR_LIT>""" <EOL> bpp = rffi . getintfield ( image . c_format , '<STR_LIT>' ) <EOL> pitch = rffi . getintfield ( image , '<STR_LIT>' ) <EOL> p = rffi . ptradd ( image . c_pixels , y * pitch + x * bpp ) <EOL> if bpp == <NUM_LIT:1> : <EOL> p [ <NUM_LIT:0> ] = rffi . cast ( rffi . UCHAR , pixel ) <EOL> elif bpp == <NUM_LIT:2> : <EOL> p = rffi . cast ( RSDL . Uint16P , p ) <EOL> p [ <NUM_LIT:0> ] = rffi . cast ( RSDL . Uint16 , pixel ) <EOL> elif bpp == <NUM_LIT:3> : <EOL> if RSDL . BYTEORDER == RSDL . BIG_ENDIAN : <EOL> p [ <NUM_LIT:0> ] = rffi . cast ( rffi . UCHAR , ( pixel >> <NUM_LIT:16> ) & <NUM_LIT> ) <EOL> p [ <NUM_LIT:1> ] = rffi . cast ( rffi . UCHAR , ( pixel >> <NUM_LIT:8> ) & <NUM_LIT> ) <EOL> p [ <NUM_LIT:2> ] = rffi . cast ( rffi . UCHAR , pixel & <NUM_LIT> ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = rffi . cast ( rffi . UCHAR , pixel & <NUM_LIT> ) <EOL> p [ <NUM_LIT:1> ] = rffi . cast ( rffi . UCHAR , ( pixel >> <NUM_LIT:8> ) & <NUM_LIT> ) <EOL> p [ <NUM_LIT:2> ] = rffi . cast ( rffi . UCHAR , ( pixel >> <NUM_LIT:16> ) & <NUM_LIT> ) <EOL> elif bpp == <NUM_LIT:4> : <EOL> p = rffi . cast ( RSDL . Uint32P , p ) <EOL> p [ <NUM_LIT:0> ] = rffi . cast ( RSDL . Uint32 , pixel ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def mallocrect ( x , y , w , h ) : <EOL> p = lltype . malloc ( RSDL . Rect , flavor = '<STR_LIT>' ) <EOL> rffi . setintfield ( p , '<STR_LIT>' , x ) <EOL> rffi . setintfield ( p , '<STR_LIT>' , y ) <EOL> rffi . setintfield ( p , '<STR_LIT>' , w ) <EOL> rffi . setintfield ( p , '<STR_LIT>' , h ) <EOL> return p <EOL> def blit_complete_surface ( src , dst , x , y ) : <EOL> dstrect = mallocrect ( x , y , rffi . getintfield ( src , '<STR_LIT>' ) , rffi . getintfield ( src , '<STR_LIT>' ) ) <EOL> RSDL . BlitSurface ( src , lltype . nullptr ( RSDL . Rect ) , dst , dstrect ) <EOL> lltype . free ( dstrect , flavor = '<STR_LIT>' ) </s>
