def test_SDL_SetSurfacePalette(self): invpalette = pixels.SDL_AllocPalette(10) palette = pixels.SDL_AllocPalette(1 << 16) sf = surface.SDL_CreateRGBSurface(0, 10, 10, 16, 0, 0, 0, 0) assert not sf.contents.format.contents.palette surface.SDL_SetSurfacePalette(sf, palette) assert sf.contents.format.contents.palette.contents is not None assert sf.contents.format.contents.palette.contents is not None surface.SDL_SetSurfacePalette(sf, None) assert not sf.contents.format.contents.palette surface.SDL_FreeSurface(sf) pixels.SDL_FreePalette(invpalette) pixels.SDL_FreePalette(palette)
def test_SDL_SetPaletteColors(self): colors = [] for v in range(20): colors.append(SDL_Color(v, v + 10, v + 20)) palette = pixels.SDL_AllocPalette(10) self.assertIsInstance(palette.contents, pixels.SDL_Palette) self.assertEqual(palette.contents.ncolors, 10) colors = palette.contents.colors for index in range(10): rgb = colors[index] self.assertEqual((rgb.r, rgb.g, rgb.b), (255, 255, 255)) pixels.SDL_SetPaletteColors(palette, colors, 0, 10) for index in range(10): rgb = palette.contents.colors[index] self.assertEqual(rgb, colors[index]) pixels.SDL_SetPaletteColors(palette, colors, 5, 1000) for index in range(10): rgb = palette.contents.colors[index] if index < 5: self.assertEqual(rgb, colors[index]) elif index > 5: self.assertEqual(rgb, colors[index - 5]) pixels.SDL_FreePalette(palette)
def test_SDL_SetPaletteColors(self): colors = [] for v in range(20): colors.append(SDL_Color(v, v + 10, v + 20)) palette = pixels.SDL_AllocPalette(10) assert isinstance(palette.contents, pixels.SDL_Palette) assert palette.contents.ncolors == 10 colors = palette.contents.colors for index in range(10): rgb = colors[index] assert (rgb.r, rgb.g, rgb.b) == (255, 255, 255) pixels.SDL_SetPaletteColors(palette, colors, 0, 10) for index in range(10): rgb = palette.contents.colors[index] assert rgb == colors[index] pixels.SDL_SetPaletteColors(palette, colors, 5, 1000) for index in range(10): rgb = palette.contents.colors[index] if index < 5: assert rgb == colors[index] elif index > 5: assert rgb == colors[index - 5] pixels.SDL_FreePalette(palette)
def test_SDL_SetPixelFormatPalette(self): palette = pixels.SDL_AllocPalette(10) self.assertIsInstance(palette.contents, pixels.SDL_Palette) pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_RGBA8888) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) pixels.SDL_SetPixelFormatPalette(pformat, palette) # TODO: improve tests pixels.SDL_FreeFormat(pformat) pixels.SDL_FreePalette(palette)
def test_SDL_SetSurfacePalette(self): invpalette = pixels.SDL_AllocPalette(10) palette = pixels.SDL_AllocPalette(1 << 16) sf = surface.SDL_CreateRGBSurface(0, 10, 10, 16, 0, 0, 0, 0) # self.assertRaises((AttributeError, TypeError), # surface.set_surface_palette, None, None) # self.assertRaises((AttributeError, TypeError), # surface.set_surface_palette, None, palette) self.assertFalse(sf.contents.format.contents.palette) surface.SDL_SetSurfacePalette(sf, palette) self.assertIsNotNone(sf.contents.format.contents.palette.contents) # self.assertRaises(sdl.SDLError, surface.set_surface_palette, sf, # invpalette) self.assertIsNotNone(sf.contents.format.contents.palette.contents) surface.SDL_SetSurfacePalette(sf, None) self.assertFalse(sf.contents.format.contents.palette) surface.SDL_FreeSurface(sf) pixels.SDL_FreePalette(invpalette) pixels.SDL_FreePalette(palette)
def test_SDL_AllocFreePalette(self): self.assertRaises((ArgumentError, TypeError), pixels.SDL_AllocPalette, None) self.assertRaises((ArgumentError, TypeError), pixels.SDL_AllocPalette, "Test") # self.assertRaises(ValueError, pixels.SDL_AllocPalette, -5) palette = pixels.SDL_AllocPalette(10) self.assertIsInstance(palette.contents, pixels.SDL_Palette) self.assertEqual(palette.contents.ncolors, 10) colors = palette.contents.colors for x in range(palette.contents.ncolors): self.assertIsInstance(colors[x], SDL_Color) colors[3].r = 70 self.assertEqual(colors[3].r, 70) color = colors[4] self.assertEqual(colors[4].g, 255) self.assertEqual(color.g, 255) color.g = 33 self.assertEqual(color.g, 33) self.assertEqual(colors[4].g, 33) pixels.SDL_FreePalette(palette)
def test_SDL_AllocFreePalette(self): with pytest.raises((ArgumentError, TypeError)): pixels.SDL_AllocPalette(None) with pytest.raises((ArgumentError, TypeError)): pixels.SDL_AllocPalette("Test") # self.assertRaises(ValueError, pixels.SDL_AllocPalette, -5) palette = pixels.SDL_AllocPalette(10) assert isinstance(palette.contents, pixels.SDL_Palette) assert palette.contents.ncolors == 10 colors = palette.contents.colors for x in range(palette.contents.ncolors): assert isinstance(colors[x], SDL_Color) colors[3].r = 70 assert colors[3].r == 70 color = colors[4] assert colors[4].g == 255 assert color.g == 255 color.g = 33 assert color.g == 33 assert colors[4].g == 33 pixels.SDL_FreePalette(palette)
def image2sprite(self, image): 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 endian.SDL_BYTEORDER == endian.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 endian.SDL_BYTEORDER == endian.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.tobytes() imgsurface = surface.SDL_CreateRGBSurfaceFrom(pxbuf, width, height, depth, pitch, rmask, gmask, bmask, amask) if not imgsurface: raise SDLError() imgsurface = imgsurface.contents # the pixel buffer must not be freed for the lifetime of the surface imgsurface._pxbuf = pxbuf 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 = pixels.SDL_AllocPalette(len(rgbcolors) // 3) if not sdlpalette: raise SDLError() SDL_Color = pixels.SDL_Color for idx, (r, g, b) in enumerate(_chunk(rgbcolors, 3)): sdlpalette.contents.colors[idx] = SDL_Color(r, g, b) ret = surface.SDL_SetSurfacePalette(imgsurface, sdlpalette) # This will decrease the refcount on the palette, so it gets # freed properly on releasing the SDL_Surface. pixels.SDL_FreePalette(sdlpalette) if ret != 0: raise SDLError() return self.factory.from_surface(imgsurface, free=True)