def test_set_pixelformat_palette(self): palette = pixels.alloc_palette(10) self.assertIsInstance(palette, pixels.SDL_Palette) pformat = pixels.alloc_format(pixels.SDL_PIXELFORMAT_RGBA8888) self.assertIsInstance(pformat, pixels.SDL_PixelFormat) pixels.set_pixelformat_palette(pformat, palette) # TODO: improve tests pixels.free_format(pformat) pixels.free_palette(palette)
def test_set_surface_palette(self): invpalette = pixels.alloc_palette(10) palette = pixels.alloc_palette(1 << 16) sf = surface.create_rgb_surface(10, 10, 16) self.assertRaises((AttributeError, TypeError), surface.set_surface_palette, None, None) self.assertRaises((AttributeError, TypeError), surface.set_surface_palette, None, palette) self.assertIsNone(sf.format.palette) surface.set_surface_palette(sf, palette) self.assertIsNotNone(sf.format.palette) self.assertRaises(sdl.SDLError, surface.set_surface_palette, sf, invpalette) self.assertIsNotNone(sf.format.palette) surface.set_surface_palette(sf, None) self.assertIsNone(sf.format.palette) surface.free_surface(sf) pixels.free_palette(invpalette) pixels.free_palette(palette)
def test_alloc_free_palette(self): self.assertRaises(TypeError, pixels.alloc_palette, None) self.assertRaises(ValueError, pixels.alloc_palette, "Test") self.assertRaises(ValueError, pixels.alloc_palette, -5) palette = pixels.alloc_palette(10) self.assertIsInstance(palette, pixels.SDL_Palette) self.assertEqual(palette.ncolors, 10) self.assertEqual(len(palette.colors), 10) for x in range(palette.ncolors): self.assertIsInstance(palette.colors[x], SDL_Color) palette.colors[3].r = 70 self.assertEqual(palette.colors[3].r, 70) color = palette.colors[4] self.assertEqual(palette.colors[4].g, 255) self.assertEqual(color.g, 255) color.g = 33 self.assertEqual(color.g, 33) self.assertEqual(palette.colors[4].g, 33) pixels.free_palette(palette)
def test_set_palette_colors(self): colors = [] for v in range(20): colors.append(SDL_Color(v, v + 10, v + 20)) palette = pixels.alloc_palette(10) self.assertIsInstance(palette, pixels.SDL_Palette) self.assertEqual(palette.ncolors, 10) for rgb in palette.colors: self.assertEqual((rgb.r, rgb.g, rgb.b), (255, 255, 255)) pixels.set_palette_colors(palette, colors, 0, 10) for index, rgb in enumerate(palette.colors): self.assertEqual(rgb, colors[index]) pixels.set_palette_colors(palette, colors, 5, 1000) for index, rgb in enumerate(palette.colors): if index < 5: self.assertEqual(rgb, colors[index]) elif index > 5: self.assertEqual(rgb, colors[index - 5]) pixels.free_palette(palette)
def load_image(fname, enforce=None): """Creates a SDL_Surface from an image file. If assurface is True, a SDL_Surface will be returned instead of a Sprite or SoftSprite object. If renderer is set to a SDL_Renderer, a Sprite will be returned. This function makes use of the Python Imaging Library, if it is available on the target execution environment. The function will try to load the file via pygame2.sdlimage first. If the file could not be loaded, it will try to load it via PIL. You can force the function to use only one of them, by passing the enforce as either "PIL" or "SDL". Note: This will call pygame2.sdlimage.init() implicitly with the default arguments, if the module is available. """ if enforce is not None and enforce not in ("PIL", "SDL"): raise ValueError("enforce must be either 'PIL' or 'SDL', if set") if enforce == "PIL" and not _HASPIL: raise UnsupportedError("PIL loading") if enforce == "SDL" and not _HASSDLIMAGE: raise UnsupportedError("SDL loading") surface = None if enforce != "PIL" and _HASSDLIMAGE: try: sdlimage.init() surface = sdlimage.load(fname) except: # An error occured - if we do not try PIL, break out now if not _HASPIL or enforce == "SDL": raise if enforce != "SDL" and _HASPIL and surface is None: image = Image.open(fname) mode = image.mode width, height = image.size rmask = gmask = bmask = amask = 0 if mode in ("1", "L", "P"): # 1 = B/W, 1 bit per byte # "L" = greyscale, 8-bit # "P" = palette-based, 8-bit pitch = width depth = 8 elif mode == "RGB": # 3x8-bit, 24bpp if SDL_BYTEORDER == SDL_LIL_ENDIAN: rmask = 0x0000FF gmask = 0x00FF00 bmask = 0xFF0000 else: rmask = 0xFF0000 gmask = 0x00FF00 bmask = 0x0000FF depth = 24 pitch = width * 3 elif mode in ("RGBA", "RGBX"): # RGBX: 4x8-bit, no alpha # RGBA: 4x8-bit, alpha if SDL_BYTEORDER == SDL_LIL_ENDIAN: rmask = 0x000000FF gmask = 0x0000FF00 bmask = 0x00FF0000 if mode == "RGBA": amask = 0xFF000000 else: rmask = 0xFF000000 gmask = 0x00FF0000 bmask = 0x0000FF00 if mode == "RGBA": amask = 0x000000FF depth = 32 pitch = width * 4 else: # We do not support CMYK or YCbCr for now raise TypeError("unsupported image format") pxbuf = image.tostring() surface = sdlsurface.create_rgb_surface_from(pxbuf, width, height, depth, pitch, rmask, gmask, bmask, amask) if mode == "P": # Create a SDL_Palette for the SDL_Surface def _chunk(seq, size): for x in range(0, len(seq), size): yield seq[x:x + size] rgbcolors = image.getpalette() sdlpalette = sdlpixels.alloc_palette(len(rgbcolors) // 3) SDL_Color = sdlpixels.SDL_Color for idx, (r, g, b) in enumerate(_chunk(rgbcolors, 3)): sdlpalette.colors[idx] = SDL_Color(r, g, b) try: sdlsurface.set_surface_palette(surface, sdlpalette) except: sdlsurface.free_surface(surface) raise finally: # This will decrease the refcount on the palette, so it gets # freed properly on releasing the SDL_Surface. sdlpixels.free_palette(sdlpalette) return surface