def test_image_pixels(): for filename in ["demo.jpg", "demo.png"]: image = RIMG.Load(os.path.join(autopath.this_dir, filename)) assert image assert rffi.getintfield(image.c_format, 'c_BytesPerPixel') in (3, 4) RSDL.LockSurface(image) result = {} try: rgb = lltype.malloc(rffi.CArray(RSDL.Uint8), 3, flavor='raw') try: for y in range(23): for x in range(y % 13, 17, 13): color = RSDL_helper.get_pixel(image, x, y) RSDL.GetRGB(color, image.c_format, rffi.ptradd(rgb, 0), rffi.ptradd(rgb, 1), rffi.ptradd(rgb, 2)) r = rffi.cast(lltype.Signed, rgb[0]) g = rffi.cast(lltype.Signed, rgb[1]) b = rffi.cast(lltype.Signed, rgb[2]) result[x, y] = r, g, b finally: lltype.free(rgb, flavor='raw') finally: RSDL.UnlockSurface(image) RSDL.FreeSurface(image) for x, y in result: f = (x * 17 + y * 23) / float(17 * 17 + 23 * 23) expected_r = int(255.0 * (1.0 - f)) expected_g = 0 expected_b = int(255.0 * f) r, g, b = result[x, y] assert abs(r - expected_r) < 10 assert abs(g - expected_g) < 10 assert abs(b - expected_b) < 10
def test_set_color_key(self): # prepare fillrect = RSDL_helper.mallocrect(10, 10, 30, 30) RSDL.FillRect(self.src_surf, fillrect, self.blue) lltype.free(fillrect, flavor='raw') assert RSDL.SetColorKey(self.src_surf, RSDL.SRCCOLORKEY, self.blue) == 0 # draw RSDL_helper.blit_complete_surface(self.src_surf, self.dst_surf, 0, 0) # check for position, color in (((0, 0), self.red), ((10, 10), self.black), ((20, 20), self.black), ((40, 40), self.red)): fetched_color = RSDL_helper.get_pixel(self.dst_surf, position[0], position[1]) assert fetched_color == color
def test_set_alpha(self): # prepare assert RSDL.SetAlpha(self.src_surf, RSDL.SRCALPHA, 128) == 0 # draw RSDL_helper.blit_complete_surface(self.src_surf, self.dst_surf, 10, 10) RSDL_helper.blit_complete_surface(self.src_surf, self.dst_surf, 20, 20) # check for position, color in ( ((0, 0), (0, 0, 0)), # no rect ((10, 10), (127, 0, 0)), # one rect ((20, 20), (191, 0, 0)) # two overlapping rects ): fetched_color = RSDL_helper.get_pixel(self.dst_surf, position[0], position[1]) assert RSDL_helper.get_rgb(fetched_color, self.dst_surf.c_format) == color
def test_set_color_key(self): # prepare fillrect = RSDL_helper.mallocrect(10, 10, 30, 30) RSDL.FillRect(self.src_surf, fillrect, self.blue) lltype.free(fillrect, flavor='raw') assert RSDL.SetColorKey(self.src_surf, RSDL.SRCCOLORKEY, self.blue) == 0 # draw RSDL_helper.blit_complete_surface(self.src_surf, self.dst_surf, 0, 0) # check for position, color in ( (( 0, 0), self.red), ((10,10), self.black), ((20,20), self.black), ((40,40), self.red) ): fetched_color = RSDL_helper.get_pixel(self.dst_surf, position[0], position[1]) assert fetched_color == color
def test_set_alpha(self): # prepare assert RSDL.SetAlpha(self.src_surf, RSDL.SRCALPHA, 128) == 0 # draw RSDL_helper.blit_complete_surface( self.src_surf, self.dst_surf, 10, 10) RSDL_helper.blit_complete_surface( self.src_surf, self.dst_surf, 20, 20) # check for position, color in ( (( 0, 0), ( 0,0,0)), # no rect ((10,10), (127,0,0)), # one rect ((20,20), (191,0,0)) # two overlapping rects ): fetched_color = RSDL_helper.get_pixel(self.dst_surf, position[0], position[1]) assert RSDL_helper.get_rgb(fetched_color, self.dst_surf.c_format) == color
def test_image_pixels(): for filename in ["demo.jpg", "demo.png"]: image = RIMG.Load(os.path.join(autopath.this_dir, filename)) assert image assert rffi.getintfield(image.c_format, 'c_BytesPerPixel') in (3, 4) RSDL.LockSurface(image) result = {} try: rgb = lltype.malloc(rffi.CArray(RSDL.Uint8), 3, flavor='raw') try: for y in range(23): for x in range(y % 13, 17, 13): color = RSDL_helper.get_pixel(image, x, y) RSDL.GetRGB(color, image.c_format, rffi.ptradd(rgb, 0), rffi.ptradd(rgb, 1), rffi.ptradd(rgb, 2)) r = rffi.cast(lltype.Signed, rgb[0]) g = rffi.cast(lltype.Signed, rgb[1]) b = rffi.cast(lltype.Signed, rgb[2]) result[x, y] = r, g, b finally: lltype.free(rgb, flavor='raw') finally: RSDL.UnlockSurface(image) RSDL.FreeSurface(image) for x, y in result: f = (x*17 + y*23) / float(17*17+23*23) expected_r = int(255.0 * (1.0-f)) expected_g = 0 expected_b = int(255.0 * f) r, g, b = result[x, y] assert abs(r-expected_r) < 10 assert abs(g-expected_g) < 10 assert abs(b-expected_b) < 10