def test_SDL_GetRGB(self): # TODO: invalid parameters pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_RGBA8888) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) r, g, b = Uint8(), Uint8(), Uint8() pixels.SDL_GetRGB(0xFFAA8811, pformat, byref(r), byref(g), byref(b)) self.assertEqual((r.value, g.value, b.value), (0xFF, 0xAA, 0x88)) pixels.SDL_GetRGB(0x00000000, pformat, byref(r), byref(g), byref(b)) self.assertEqual((r.value, g.value, b.value), (0x00, 0x00, 0x00)) pixels.SDL_GetRGB(0xFFFFFFFF, pformat, byref(r), byref(g), byref(b)) self.assertEqual((r.value, g.value, b.value), (0xFF, 0xFF, 0xFF)) pixels.SDL_GetRGB(0x11223344, pformat, byref(r), byref(g), byref(b)) self.assertEqual((r.value, g.value, b.value), (0x11, 0x22, 0x33)) pixels.SDL_FreeFormat(pformat) fmts = (pixels.SDL_PIXELFORMAT_INDEX1LSB, pixels.SDL_PIXELFORMAT_INDEX1MSB) for fmt in fmts: pformat = pixels.SDL_AllocFormat(fmt) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) pixels.SDL_GetRGB(0x11223344, pformat, byref(r), byref(g), byref(b)) self.assertEqual((r.value, g.value, b.value), (0xFF, 0xFF, 0xFF)) pixels.SDL_GetRGB(0x00000000, pformat, byref(r), byref(g), byref(b)) # TODO: Seems to be always (0xFF, 0xFF, 0xFF)??? # self.assertEqual(rgb,(0x00, 0x00, 0x00)) pixels.SDL_FreeFormat(pformat) fmts = (pixels.SDL_PIXELFORMAT_INDEX4LSB, pixels.SDL_PIXELFORMAT_INDEX4MSB) for fmt in fmts: pformat = pixels.SDL_AllocFormat(fmt) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) # TODO pixels.SDL_FreeFormat(pformat)
def test_SDL_MapRGBA(self): pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_RGBA8888) assert isinstance(pformat.contents, pixels.SDL_PixelFormat) val = pixels.SDL_MapRGBA(pformat, 0xFF, 0xAA, 0x88, 0x11) assert val == 0xFFAA8811 pixels.SDL_FreeFormat(pformat) pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_UNKNOWN) assert isinstance(pformat.contents, pixels.SDL_PixelFormat) assert pformat.contents.format == pixels.SDL_PIXELFORMAT_UNKNOWN val = pixels.SDL_MapRGBA(pformat, 0xFF, 0xAA, 0x88, 0x11) assert val == 0x0 pixels.SDL_FreeFormat(pformat)
def test_SDL_MapRGBA(self): pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_RGBA8888) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) val = pixels.SDL_MapRGBA(pformat, 0xFF, 0xAA, 0x88, 0x11) self.assertEqual(val, 0xFFAA8811) pixels.SDL_FreeFormat(pformat) pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_UNKNOWN) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) self.assertEqual(pformat.contents.format, pixels.SDL_PIXELFORMAT_UNKNOWN) val = pixels.SDL_MapRGBA(pformat, 0xFF, 0xAA, 0x88, 0x11) self.assertEqual(val, 0x0) pixels.SDL_FreeFormat(pformat)
def test_SDL_GetSetColorKey(self): colorkeys = ( (0, 0, 0), (32, 64, 128), (10, 20, 30), (1, 2, 4), (255, 255, 255), (128, 128, 128), (127, 127, 127), ) bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32(), Uint32() for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue pformat = pixels.SDL_AllocFormat(fmt) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) ret = pixels.SDL_PixelFormatEnumToMasks(fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 10, bpp, rmask, gmask, bmask, amask) for r, g, b in colorkeys: key = pixels.SDL_MapRGB(pformat, r, g, b) surface.SDL_SetColorKey(sf, 1, key) skey = Uint32() ret = surface.SDL_GetColorKey(sf, byref(skey)) self.assertEqual(ret, 0) self.assertEqual( skey.value, key, "Could not set color key (%d, %d, %d)" % (r, g, b)) pixels.SDL_FreeFormat(pformat) surface.SDL_FreeSurface(sf)
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_AllocFreeFormat(self): pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_RGBA8888) assert isinstance(pformat.contents, pixels.SDL_PixelFormat) assert pformat.contents.format == pixels.SDL_PIXELFORMAT_RGBA8888 assert pformat.contents.BitsPerPixel == 32 assert pformat.contents.BytesPerPixel == 4 pixels.SDL_FreeFormat(pformat) pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_INDEX1LSB) assert isinstance(pformat.contents, pixels.SDL_PixelFormat) assert pformat.contents.format == pixels.SDL_PIXELFORMAT_INDEX1LSB assert pformat.contents.BitsPerPixel == 1 assert pformat.contents.BytesPerPixel == 1 pixels.SDL_FreeFormat(pformat) pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_INDEX4MSB) assert isinstance(pformat.contents, pixels.SDL_PixelFormat) assert pformat.contents.format == pixels.SDL_PIXELFORMAT_INDEX4MSB assert pformat.contents.BitsPerPixel == 4 assert pformat.contents.BytesPerPixel == 1 pixels.SDL_FreeFormat(pformat)
def test_SDL_AllocFreeFormat(self): pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_RGBA8888) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) self.assertEqual(pformat.contents.format, pixels.SDL_PIXELFORMAT_RGBA8888) self.assertEqual(pformat.contents.BitsPerPixel, 32) self.assertEqual(pformat.contents.BytesPerPixel, 4) pixels.SDL_FreeFormat(pformat) pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_INDEX1LSB) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) self.assertEqual(pformat.contents.format, pixels.SDL_PIXELFORMAT_INDEX1LSB) self.assertEqual(pformat.contents.BitsPerPixel, 1) self.assertEqual(pformat.contents.BytesPerPixel, 1) pixels.SDL_FreeFormat(pformat) pformat = pixels.SDL_AllocFormat(pixels.SDL_PIXELFORMAT_INDEX4MSB) self.assertIsInstance(pformat.contents, pixels.SDL_PixelFormat) self.assertEqual(pformat.contents.format, pixels.SDL_PIXELFORMAT_INDEX4MSB) self.assertEqual(pformat.contents.BitsPerPixel, 4) self.assertEqual(pformat.contents.BytesPerPixel, 1) pixels.SDL_FreeFormat(pformat)
def test_SDL_ConvertSurface(self): bad_combos = [] good_combos = [] tenbit = [ pixels.SDL_PACKEDLAYOUT_2101010, pixels.SDL_PACKEDLAYOUT_1010102 ] for idx in pixels.ALL_PIXELFORMATS: pfmt = pixels.SDL_AllocFormat(idx) for fmt in pixels.ALL_PIXELFORMATS: idx_name = pixels.SDL_GetPixelFormatName(idx).decode('utf-8') fmt_name = pixels.SDL_GetPixelFormatName(fmt).decode('utf-8') # SDL2 doesn't support converting fancier formats (e.g YUV, 10bit) if pixels.SDL_ISPIXELFORMAT_FOURCC( idx) or pixels.SDL_PIXELLAYOUT(idx) in tenbit: continue if pixels.SDL_ISPIXELFORMAT_FOURCC( fmt) or pixels.SDL_PIXELLAYOUT(fmt) in tenbit: continue # SDL2 doesn't support converting to formats w/ less than 8 bpp if pixels.SDL_BITSPERPIXEL(idx) < 8: continue # SDL2 doesn't support converting from indexed formats w/ 4 bpp if pixels.SDL_PIXELTYPE(fmt) == pixels.SDL_PIXELTYPE_INDEX4: continue bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32( ), Uint32() ret = pixels.SDL_PixelFormatEnumToMasks( fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 20, bpp, rmask, gmask, bmask, amask) self.assertIsInstance(sf.contents, surface.SDL_Surface) csf = surface.SDL_ConvertSurface(sf, pfmt, 0) self.assertTrue(csf) if error.SDL_GetError() == b'Blit combination not supported': bad_combos.append('{0} -> {1}'.format(fmt_name, idx_name)) error.SDL_ClearError() else: good_combos.append('{0} -> {1}'.format(fmt_name, idx_name)) self.assertIsInstance(csf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf) surface.SDL_FreeSurface(csf) pixels.SDL_FreeFormat(pfmt) self.assertEqual(len(bad_combos), 0)
def test_SDL_ConvertSurface(self): tenbit = [ pixels.SDL_PACKEDLAYOUT_2101010, pixels.SDL_PACKEDLAYOUT_1010102 ] for idx in pixels.ALL_PIXELFORMATS: pfmt = pixels.SDL_AllocFormat(idx) for fmt in pixels.ALL_PIXELFORMATS: idx_name = pixels.SDL_GetPixelFormatName(idx).decode( 'utf-8') # for debug fmt_name = pixels.SDL_GetPixelFormatName(fmt).decode( 'utf-8') # for debug # SDL2 doesn't support converting fancier formats (e.g YUV, 10bit) if pixels.SDL_ISPIXELFORMAT_FOURCC( idx) or pixels.SDL_PIXELLAYOUT(idx) in tenbit: continue if pixels.SDL_ISPIXELFORMAT_FOURCC( fmt) or pixels.SDL_PIXELLAYOUT(fmt) in tenbit: continue # SDL2 doesn't support converting to formats w/ less than 8 bpp if pixels.SDL_BITSPERPIXEL(idx) < 8: continue # SDL2 doesn't support converting from indexed formats w/ 4 bpp if pixels.SDL_PIXELTYPE(fmt) == pixels.SDL_PIXELTYPE_INDEX4: continue bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32( ), Uint32() ret = pixels.SDL_PixelFormatEnumToMasks( fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) assert ret == SDL_TRUE sf = surface.SDL_CreateRGBSurface(0, 10, 20, bpp, rmask, gmask, bmask, amask) assert isinstance(sf.contents, surface.SDL_Surface) csf = surface.SDL_ConvertSurface(sf, pfmt, 0) assert csf, error.SDL_GetError() assert isinstance(csf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf) surface.SDL_FreeSurface(csf) pixels.SDL_FreeFormat(pfmt)
def test_SDL_ConvertSurface(self): for idx in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(idx): continue pfmt = pixels.SDL_AllocFormat(idx) for fmt in pixels.ALL_PIXELFORMATS: if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt): continue bpp = c_int() rmask, gmask, bmask, amask = Uint32(), Uint32(), Uint32( ), Uint32() ret = pixels.SDL_PixelFormatEnumToMasks( fmt, byref(bpp), byref(rmask), byref(gmask), byref(bmask), byref(amask)) self.assertEqual(ret, SDL_TRUE) sf = surface.SDL_CreateRGBSurface(0, 10, 20, bpp, rmask, gmask, bmask, amask) self.assertIsInstance(sf.contents, surface.SDL_Surface) csf = surface.SDL_ConvertSurface(sf, pfmt, 0) self.assertTrue(csf, error.SDL_GetError()) self.assertIsInstance(csf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf) surface.SDL_FreeSurface(csf) pixels.SDL_FreeFormat(pfmt)