Beispiel #1
0
    def test_pixelformat_enum_to_masks(self):
        val = pixels.pixelformat_enum_to_masks \
            (pixels.SDL_PIXELFORMAT_INDEX1LSB)
        self.assertEqual(val, (1, 0, 0, 0, 0))
        val = pixels.pixelformat_enum_to_masks \
            (pixels.SDL_PIXELFORMAT_INDEX1MSB)
        self.assertEqual(val, (1, 0, 0, 0, 0))

        val = pixels.pixelformat_enum_to_masks(pixels.SDL_PIXELFORMAT_RGBA8888)
        if sys.byteorder == "little":
            self.assertEqual(val, (32,
                                   0xFF000000,
                                   0x00FF0000,
                                   0x0000FF00,
                                   0x000000FF))
        else:
            self.assertEqual(val, (32,
                                   0x000000FF,
                                   0x0000FF00,
                                   0x00FF0000,
                                   0xFF000000))
        val = pixels.pixelformat_enum_to_masks(pixels.SDL_PIXELFORMAT_RGBX8888)
        if sys.byteorder == "little":
            self.assertEqual(val, (32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0))
        else:
            self.assertEqual(val, (32, 0, 0x0000FF00, 0x00FF0000, 0xFF000000))
        self.assertRaises(sdl.SDLError, pixels.pixelformat_enum_to_masks, 99999)

        val = pixels.pixelformat_enum_to_masks(0)
        self.assertEqual(val, (0, 0, 0, 0, 0))
        val = pixels.pixelformat_enum_to_masks(pixels.SDL_PIXELFORMAT_UNKNOWN)
        self.assertEqual(val, (0, 0, 0, 0, 0))
Beispiel #2
0
 def __init__(self, w = None, h = None, data_ptr = None, surface = None,
                 filename = None, filedata = None):
     self.do_free = True
     if filedata is not None:
         if type(filedata) is bytes:
             imagedata = bytearray(filedata)
         if type(filedata) is bytearray:
             rwops = sdlrwops.rw_from_mem(bar2voidp(filedata).value, len(filedata))
         elif hasattr(filedata, "seek"):
             rwops = sdlrwops.rw_from_object(filedata)
         else:
             raise TypeError("unusable data type {}", str(type(filedata)))
         self._surf = sdlimage.load_rw(rwops, 1)
     elif isinstance(filename, str):
         self._surf = sdlimage.load(filename)
     elif isinstance(w, int) and isinstance(h, int):
         masks = list(sdlpixels.pixelformat_enum_to_masks(self.sdl_pixel_fmt))
         bpp = masks.pop(0)
         if data_ptr is None:
             self._surf = sdlsurface.create_rgb_surface(w, h, bpp, *masks)
         else: # data_ptr == ABGR8888
             self._surf = sdlsurface.create_rgb_surface_from(data_ptr, w, h, bpp, w*4, *masks)
     elif isinstance(surface, SDL_Surface):
         self.do_free = False
         self._surf = surface
     else:
         raise TypeError("Crazy shit in parameters: ({} {} {} {} {})".format(type(w),
                 type(h), type(data_ptr), type(surface), type(filename)))
         
     sdlsurface.set_surface_blend_mode(self._surf, sdlvideo.SDL_BLENDMODE_NONE)
Beispiel #3
0
 def test_get_set_color_key(self):
     colorkeys = ((0, 0, 0),
                  (32, 64, 128),
                  (10, 20, 30),
                  (1, 2, 4),
                  (255, 255, 255),
                  (128, 128, 128),
                  (127, 127, 127),
                  )
     for fmt in pixels.ALL_PIXELFORMATS:
         if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
             continue
         pformat = pixels.alloc_format(fmt)
         bpp, rmask, gmask, bmask, amask = \
             pixels.pixelformat_enum_to_masks(fmt)
         sf = surface.create_rgb_surface(10, 10, bpp, rmask, gmask, bmask,
                                         amask)
         for r, g, b in colorkeys:
             key = pixels.map_rgb(pformat, r, g, b)
             surface.set_color_key(sf, 1, key)
             skey = surface.get_color_key(sf)
             self.assertEqual(skey, key,
                 "Could not set color key (%d, %d, %d)" % (r, g, b))
         pixels.free_format(pformat)
         surface.free_surface(sf)
Beispiel #4
0
 def test_lock_unlock_MUSTLOCK_surface(self):
     colormods = ((0, 0, 0),
                  (32, 64, 128),
                  (10, 20, 30),
                  (1, 2, 4),
                  (255, 255, 255),
                  (128, 128, 128),
                  (127, 127, 127),
                  )
     for fmt in pixels.ALL_PIXELFORMATS:
         if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
             continue
         bpp, rmask, gmask, bmask, amask = \
             pixels.pixelformat_enum_to_masks(fmt)
         sf = surface.create_rgb_surface(10, 10, bpp, rmask, gmask, bmask,
                                         amask)
         # TODO: locking seems to be only necessary for RLE surfaces...
         if surface.SDL_MUSTLOCK(sf):
             self.assertFalse(sf.locked)
             surface.lock_surface(sf)
             self.assertTrue(sf.locked)
             surface.lock_surface(sf)
             self.assertTrue(sf.locked)
             surface.lock_surface(sf)
             self.assertTrue(sf.locked)
             surface.unlock_surface(sf)
             self.assertTrue(sf.locked)
             surface.unlock_surface(sf)
             self.assertTrue(sf.locked)
             surface.unlock_surface(sf)
             self.assertFalse(sf.locked)
         surface.free_surface(sf)
Beispiel #5
0
    def test_fill_rects(self):
        rectlist = (rect.SDL_Rect(0, 0, 0, 0),
                    rect.SDL_Rect(0, 0, 10, 10),
                    rect.SDL_Rect(0, 0, -10, 10),
                    rect.SDL_Rect(0, 0, -10, -10),
                    rect.SDL_Rect(-10, -10, 10, 10),
                    rect.SDL_Rect(10, -10, 10, 10),
                    rect.SDL_Rect(10, 10, 10, 10),
                    )

        for fmt in pixels.ALL_PIXELFORMATS:
            if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
                continue
            if pixels.SDL_BITSPERPIXEL(fmt) < 8:
                continue  # Skip < 8bpp, SDL_FillRect does not work on those
            for w in range(1, 100, 5):
                for h in range(1, 100, 5):
                    bpp, rmask, gmask, bmask, amask = \
                        pixels.pixelformat_enum_to_masks(fmt)
                    sf = surface.create_rgb_surface(w, h, bpp, rmask, gmask,
                                                    bmask, amask)
                    self.assertIsInstance(sf, surface.SDL_Surface)
                    # TODO: check for changed pixels
                    surface.fill_rects(sf, rectlist, 0xff00ff00)
                    surface.free_surface(sf)
Beispiel #6
0
 def test_get_set_surface_alpha_mod(self):
     for fmt in pixels.ALL_PIXELFORMATS:
         if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
             continue
         bpp, rmask, gmask, bmask, amask = \
             pixels.pixelformat_enum_to_masks(fmt)
         sf = surface.create_rgb_surface(10, 10, bpp, rmask, gmask, bmask,
                                         amask)
         salpha = surface.get_surface_alpha_mod(sf)
         self.assertEqual(salpha, 255)
         for alpha in range(0, 255):
             surface.set_surface_alpha_mod(sf, alpha)
             salpha = surface.get_surface_alpha_mod(sf)
             self.assertEqual(salpha, alpha)
         surface.free_surface(sf)
Beispiel #7
0
 def test_get_set_surface_blend_mode(self):
     modes = (video.SDL_BLENDMODE_NONE,
              video.SDL_BLENDMODE_BLEND,
              video.SDL_BLENDMODE_ADD,
              video.SDL_BLENDMODE_MOD
              )
     for fmt in pixels.ALL_PIXELFORMATS:
         if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
             continue
         bpp, rmask, gmask, bmask, amask = \
             pixels.pixelformat_enum_to_masks(fmt)
         sf = surface.create_rgb_surface(10, 10, bpp, rmask, gmask, bmask,
                                         amask)
         for mode in modes:
             surface.set_surface_blend_mode(sf, mode)
             smode = surface.get_surface_blend_mode(sf)
             self.assertEqual(smode, mode)
         surface.free_surface(sf)
Beispiel #8
0
    def test_create_rgb_surface(self):
        for w in range(1, 100, 5):
            for h in range(1, 100, 5):
                for bpp in alldepths:
                    sf = surface.create_rgb_surface(w, h, bpp)
                    self.assertIsInstance(sf, surface.SDL_Surface)
                    surface.free_surface(sf)

        for fmt in pixels.ALL_PIXELFORMATS:
            if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
                continue
            for w in range(1, 100, 5):
                for h in range(1, 100, 5):
                    bpp, rmask, gmask, bmask, amask = \
                        pixels.pixelformat_enum_to_masks(fmt)
                    sf = surface.create_rgb_surface(w, h, bpp, rmask, gmask,
                                                    bmask, amask)
                    self.assertIsInstance(sf, surface.SDL_Surface)
                    surface.free_surface(sf)

        # Broken ones
        # sizes = [(0, 0), (65536, 65536)]
        # for (w, h) in sizes:
        #    for bpp in alldepths:
        #        print w, h, bpp
        #        sf = surface.create_rgb_surface(w, h, bpp)
        #        self.assertIsInstance(sf, surface.SDL_Surface)
        #        surface.free_surface(sf)
        
        # TODO: raises a SDL Out of memory error, but 65536 works?!
        #
        # sf = surface.create_rgb_surface(65535, 65535, 32)
        # self.assertIsInstance(sf, surface.SDL_Surface)
        
        self.assertRaises(sdl.SDLError, surface.create_rgb_surface, 1, 1, 66)
        self.assertRaises(sdl.SDLError, surface.create_rgb_surface, 1, 1, 0)
        self.assertRaises(sdl.SDLError, surface.create_rgb_surface, 1, 1, 8,
                          0xf0, 0x0f, 0x01, 0x02)
        self.assertRaises(sdl.SDLError, surface.create_rgb_surface, 1, 1, 16,
                          0xf0, 0x0f, 0x01, 0x02)
        self.assertRaises(sdl.SDLError, surface.create_rgb_surface, 1, 1, 24,
                          0xf0, 0x0f, 0x01, 0x02)
        self.assertRaises(sdl.SDLError, surface.create_rgb_surface, 1, 1, 32,
                          0xf0, 0x0f, 0x01, 0x02)
Beispiel #9
0
 def test_convert_surface_format(self):
     pfmt = pixels.SDL_PIXELFORMAT_RGB444
     for fmt in pixels.ALL_PIXELFORMATS:
         if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
             continue
         if fmt in (pixels.SDL_PIXELFORMAT_RGB332,
                    pixels.SDL_PIXELFORMAT_ARGB2101010):
             # SDL2 segault in the DUFFS_LOOP() macros
             # http://bugzilla.libsdl.org/show_bug.cgi?id=1534
             continue
         bpp, rmask, gmask, bmask, amask = \
             pixels.pixelformat_enum_to_masks(fmt)
         sf = surface.create_rgb_surface(10, 20, bpp, rmask, gmask,
                                         bmask, amask)
         self.assertIsInstance(sf, surface.SDL_Surface)
         csf = surface.convert_surface_format(sf, pfmt, 0)
         self.assertIsInstance(csf, surface.SDL_Surface)
         surface.free_surface(sf)
         surface.free_surface(csf)
Beispiel #10
0
 def test_get_set_surface_color_mod(self):
     colormods = ((0, 0, 0),
                  (32, 64, 128),
                  (10, 20, 30),
                  (1, 2, 4),
                  (255, 255, 255),
                  (128, 128, 128),
                  (127, 127, 127),
                  )
     for fmt in pixels.ALL_PIXELFORMATS:
         if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
             continue
         bpp, rmask, gmask, bmask, amask = \
             pixels.pixelformat_enum_to_masks(fmt)
         sf = surface.create_rgb_surface(10, 10, bpp, rmask, gmask, bmask,
                                         amask)
         for r, g, b in colormods:
             surface.set_surface_color_mod(sf, r, g, b)
             sr, sg, sb = surface.get_surface_color_mod(sf)
             self.assertEqual((sr, sg, sb), (r, g, b))
         surface.free_surface(sf)
Beispiel #11
0
    def test_free_surface(self):
        self.assertRaises((AttributeError, TypeError),
                          surface.free_surface, None)
        self.assertRaises((AttributeError, TypeError),
                          surface.free_surface, "Test")
        self.assertRaises((AttributeError, TypeError),
                          surface.free_surface, 5)

        formats = (pixels.SDL_PIXELFORMAT_INDEX1LSB,
                   pixels.SDL_PIXELFORMAT_RGB332,
                   pixels.SDL_PIXELFORMAT_RGBA4444,
                   pixels.SDL_PIXELFORMAT_BGR888,
                   pixels.SDL_PIXELFORMAT_ARGB8888,
                   pixels.SDL_PIXELFORMAT_ARGB2101010
                   )
        for fmt in formats:
            bpp, rmask, gmask, bmask, amask = \
                pixels.pixelformat_enum_to_masks(fmt)
            sf = surface.create_rgb_surface(5, 5, bpp, rmask, gmask, bmask,
                                            amask)
            surface.free_surface(sf)
Beispiel #12
0
    def test_convert_surface(self):
        pfmt = pixels.alloc_format(pixels.SDL_PIXELFORMAT_RGB444)
        for fmt in pixels.ALL_PIXELFORMATS:
            if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
                continue
            if fmt in (pixels.SDL_PIXELFORMAT_RGB332,
                       pixels.SDL_PIXELFORMAT_ARGB2101010):
                # SDL2 segfault in the DUFFS_LOOOP() macros
                # http://bugzilla.libsdl.org/show_bug.cgi?id=1534
                continue
            bpp, rmask, gmask, bmask, amask = \
                pixels.pixelformat_enum_to_masks(fmt)
            sf = surface.create_rgb_surface(10, 20, bpp, rmask, gmask,
                                            bmask, amask)
            self.assertIsInstance(sf, surface.SDL_Surface)
            csf = surface.convert_surface(sf, pfmt, 0)
            self.assertIsInstance(csf, surface.SDL_Surface)
            surface.free_surface(sf)
            surface.free_surface(csf)

        sf = surface.create_rgb_surface(10, 10, 32, 0, 0, 0)
        self.assertRaises((AttributeError, TypeError),
                          surface.convert_surface, None, None, None)
        self.assertRaises((AttributeError, TypeError),
                          surface.convert_surface, sf, None, None)
        self.assertRaises((AttributeError, TypeError),
                          surface.convert_surface, sf, "Test", 0)
        self.assertRaises((AttributeError, TypeError),
                          surface.convert_surface, sf, 12345, 0)
        self.assertRaises((AttributeError, TypeError),
                          surface.convert_surface, None, pfmt, 0)
        self.assertRaises((AttributeError, TypeError),
                          surface.convert_surface, "Test", pfmt, 0)
        self.assertRaises((AttributeError, TypeError),
                          surface.convert_surface, 12345, pfmt, 0)
        pixels.free_format(pfmt)
        surface.free_surface(sf)