Python pygame.PixelArray() Examples

The following are 30 code examples of pygame.PixelArray(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygame , or try the search function .
Example #1
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_set_pixel (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            ar.__getitem__ (0).__setitem__ (0, (0, 255, 0))
            self.assertEqual (ar[0][0], sf.map_rgb ((0, 255, 0)))

            ar.__getitem__ (1).__setitem__ (1, (128, 128, 128))
            self.assertEqual (ar[1][1], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__(-1).__setitem__ (-1, (128, 128, 128))
            self.assertEqual (ar[9][19], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__ (-2).__setitem__ (-2, (128, 128, 128))
            self.assertEqual (ar[8][-2], sf.map_rgb ((128, 128, 128))) 
Example #2
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_close_raises(self):
        """ when you try to do an operation after it is closed.
        """
        s = pygame.Surface((10,10))
        a = pygame.PixelArray(s)
        a.close()
        def do_operation():
            a[:]
        self.assertRaises (ValueError, do_operation)

        def do_operation2():
            a[:] = 1
        self.assertRaises (ValueError, do_operation2)

        def do_operation3():
            a.make_surface()
        self.assertRaises (ValueError, do_operation3)

        def do_operation4():
            for x in a:
                pass
        self.assertRaises (ValueError, do_operation4) 
Example #3
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_as_class (self):
        # Check general new-style class freatures.
        sf = pygame.Surface ((2, 3), 0, 32)
        ar = pygame.PixelArray (sf)
        self.assertRaises (AttributeError, getattr, ar, 'nonnative')
        ar.nonnative = 'value'
        self.assertEqual (ar.nonnative, 'value')
        r = weakref.ref (ar)
        self.assertTrue (r() is ar)
        del ar
        gc.collect ()
        self.assertTrue (r() is None)

        class C (pygame.PixelArray):
            def __str__ (self):
                return "string (%i, %i)" % self.shape

        ar = C (sf)
        self.assertEqual (str (ar), "string (2, 3)")
        r = weakref.ref (ar)
        self.assertTrue (r() is ar)
        del ar
        gc.collect ()
        self.assertTrue (r() is None) 
Example #4
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_get_column (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((0, 0, 255))
            val = sf.map_rgb ((0, 0, 255))
            ar = pygame.PixelArray (sf)

            ar2 = ar.__getitem__ (1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val)

            ar2 = ar.__getitem__ (-1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val) 
Example #5
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_close_raises(self):
        """ when you try to do an operation after it is closed.
        """
        s = pygame.Surface((10,10))
        a = pygame.PixelArray(s)
        a.close()
        def do_operation():
            a[:]
        self.assertRaises (ValueError, do_operation)

        def do_operation2():
            a[:] = 1
        self.assertRaises (ValueError, do_operation2)

        def do_operation3():
            a.make_surface()
        self.assertRaises (ValueError, do_operation3)

        def do_operation4():
            for x in a:
                pass
        self.assertRaises (ValueError, do_operation4) 
Example #6
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_pixel_array (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            self.assertEqual (ar._pixels_address, sf._pixels_address)

            if sf.mustlock ():
                self.assertTrue (sf.get_locked ())

            self.assertEqual (len (ar), 10)

            del ar
            if sf.mustlock ():
                self.assertFalse (sf.get_locked ()) 
Example #7
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_as_class (self):
        # Check general new-style class freatures.
        sf = pygame.Surface ((2, 3), 0, 32)
        ar = pygame.PixelArray (sf)
        self.assertRaises (AttributeError, getattr, ar, 'nonnative')
        ar.nonnative = 'value'
        self.assertEqual (ar.nonnative, 'value')
        r = weakref.ref (ar)
        self.assertTrue (r() is ar)
        del ar
        gc.collect ()
        self.assertTrue (r() is None)

        class C (pygame.PixelArray):
            def __str__ (self):
                return "string (%i, %i)" % self.shape

        ar = C (sf)
        self.assertEqual (str (ar), "string (2, 3)")
        r = weakref.ref (ar)
        self.assertTrue (r() is ar)
        del ar
        gc.collect ()
        self.assertTrue (r() is None) 
Example #8
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_get_column (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((0, 0, 255))
            val = sf.map_rgb ((0, 0, 255))
            ar = pygame.PixelArray (sf)

            ar2 = ar.__getitem__ (1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val)

            ar2 = ar.__getitem__ (-1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val) 
Example #9
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_set_pixel (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            ar.__getitem__ (0).__setitem__ (0, (0, 255, 0))
            self.assertEqual (ar[0][0], sf.map_rgb ((0, 255, 0)))

            ar.__getitem__ (1).__setitem__ (1, (128, 128, 128))
            self.assertEqual (ar[1][1], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__(-1).__setitem__ (-1, (128, 128, 128))
            self.assertEqual (ar[9][19], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__ (-2).__setitem__ (-2, (128, 128, 128))
            self.assertEqual (ar[8][-2], sf.map_rgb ((128, 128, 128))) 
Example #10
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_contains (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            sf.set_at ((8, 8), (255, 255, 255))

            ar = pygame.PixelArray (sf)
            self.assertTrue ((0, 0, 0) in ar)
            self.assertTrue ((255, 255, 255) in ar)
            self.assertFalse ((255, 255, 0) in ar)
            self.assertFalse (0x0000ff in ar)

            # Test sliced array
            self.assertTrue ((0, 0, 0) in ar[8])
            self.assertTrue ((255, 255, 255) in ar[8])
            self.assertFalse ((255, 255, 0) in ar[8])
            self.assertFalse (0x0000ff in ar[8]) 
Example #11
Source File: surflock_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_pxarray_ref(self):
        sf = pygame.Surface((5, 5))
        ar = pygame.PixelArray(sf)
        ar2 = pygame.PixelArray(sf)

        self.assertEqual(sf.get_locked(), True)
        self.assertEqual(sf.get_locks(), (ar, ar2))

        del ar
        self.assertEqual(sf.get_locked(), True)
        self.assertEqual(sf.get_locks(), (ar2,))

        ar = ar2[:]
        self.assertEqual(sf.get_locked(), True)
        self.assertEqual(sf.get_locks(), (ar2,))

        del ar
        self.assertEqual(sf.get_locked(), True)
        self.assertEqual(len(sf.get_locks()), 1) 
Example #12
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_set_pixel (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            ar.__getitem__ (0).__setitem__ (0, (0, 255, 0))
            self.assertEqual (ar[0][0], sf.map_rgb ((0, 255, 0)))

            ar.__getitem__ (1).__setitem__ (1, (128, 128, 128))
            self.assertEqual (ar[1][1], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__(-1).__setitem__ (-1, (128, 128, 128))
            self.assertEqual (ar[9][19], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__ (-2).__setitem__ (-2, (128, 128, 128))
            self.assertEqual (ar[8][-2], sf.map_rgb ((128, 128, 128))) 
Example #13
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_flags (self):
        aim = arrinter
        common_flags = (aim.PAI_NOTSWAPPED | aim.PAI_WRITEABLE |
                        aim.PAI_ALIGNED)
        s = pygame.Surface ((10, 2), 0, 32)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags | aim.PAI_FORTRAN)

        ar2 = ar[::2,:]
        ai = aim.ArrayInterface (ar2)
        self.assertEqual (ai.flags, common_flags)

        s = pygame.Surface ((8, 2), 0, 24)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags | aim.PAI_FORTRAN)

        s = pygame.Surface ((7, 2), 0, 24)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags) 
Example #14
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_shape(self):

        for shape in [[4, 16], [5, 13]]:
            w, h = shape
            sf = pygame.Surface (shape, 0, 32)
            ar = pygame.PixelArray (sf)
            ai = arrinter.ArrayInterface (ar)
            ai_shape = [ai.shape[i] for i in range(ai.nd)]
            self.assertEqual (ai_shape, shape)
            ar2 = ar[::2,:]
            ai2 = arrinter.ArrayInterface (ar2)
            w2 = len(([0] * w)[::2])
            ai_shape = [ai2.shape[i] for i in range(ai2.nd)]
            self.assertEqual (ai_shape, [w2, h])
            ar2 = ar[:,::2]
            ai2 = arrinter.ArrayInterface (ar2)
            h2 = len(([0] * h)[::2])
            ai_shape = [ai2.shape[i] for i in range(ai2.nd)]
            self.assertEqual (ai_shape, [w, h2]) 
Example #15
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_make_surface__subclassed_surface(self):
        """Ensure make_surface can handle subclassed surfaces."""
        expected_size = (3, 5)
        expected_flags = 0
        expected_depth = 32
        original_surface = SurfaceSubclass(expected_size, expected_flags,
                                           expected_depth)
        pixelarray = pygame.PixelArray(original_surface)

        surface = pixelarray.make_surface()

        self.assertIsNot(surface, original_surface)
        self.assertIsInstance(surface, pygame.Surface)
        self.assertNotIsInstance(surface, SurfaceSubclass)
        self.assertEqual(surface.get_size(), expected_size)
        self.assertEqual(surface.get_flags(), expected_flags)
        self.assertEqual(surface.get_bitsize(), expected_depth) 
Example #16
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_get_column (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((0, 0, 255))
            val = sf.map_rgb ((0, 0, 255))
            ar = pygame.PixelArray (sf)

            ar2 = ar.__getitem__ (1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val)

            ar2 = ar.__getitem__ (-1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val) 
Example #17
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_pixel_array (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            self.assertEqual (ar._pixels_address, sf._pixels_address)

            if sf.mustlock ():
                self.assertTrue (sf.get_locked ())

            self.assertEqual (len (ar), 10)

            del ar
            if sf.mustlock ():
                self.assertFalse (sf.get_locked ()) 
Example #18
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_close_raises(self):
        """ when you try to do an operation after it is closed.
        """
        s = pygame.Surface((10,10))
        a = pygame.PixelArray(s)
        a.close()
        def do_operation():
            a[:]
        self.assertRaises (ValueError, do_operation)

        def do_operation2():
            a[:] = 1
        self.assertRaises (ValueError, do_operation2)

        def do_operation3():
            a.make_surface()
        self.assertRaises (ValueError, do_operation3)

        def do_operation4():
            for x in a:
                pass
        self.assertRaises (ValueError, do_operation4) 
Example #19
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_flags (self):
        aim = arrinter
        common_flags = (aim.PAI_NOTSWAPPED | aim.PAI_WRITEABLE |
                        aim.PAI_ALIGNED)
        s = pygame.Surface ((10, 2), 0, 32)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags | aim.PAI_FORTRAN)

        ar2 = ar[::2,:]
        ai = aim.ArrayInterface (ar2)
        self.assertEqual (ai.flags, common_flags)

        s = pygame.Surface ((8, 2), 0, 24)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags | aim.PAI_FORTRAN)

        s = pygame.Surface ((7, 2), 0, 24)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags) 
Example #20
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_shape(self):

        for shape in [[4, 16], [5, 13]]:
            w, h = shape
            sf = pygame.Surface (shape, 0, 32)
            ar = pygame.PixelArray (sf)
            ai = arrinter.ArrayInterface (ar)
            ai_shape = [ai.shape[i] for i in range(ai.nd)]
            self.assertEqual (ai_shape, shape)
            ar2 = ar[::2,:]
            ai2 = arrinter.ArrayInterface (ar2)
            w2 = len(([0] * w)[::2])
            ai_shape = [ai2.shape[i] for i in range(ai2.nd)]
            self.assertEqual (ai_shape, [w2, h])
            ar2 = ar[:,::2]
            ai2 = arrinter.ArrayInterface (ar2)
            h2 = len(([0] * h)[::2])
            ai_shape = [ai2.shape[i] for i in range(ai2.nd)]
            self.assertEqual (ai_shape, [w, h2]) 
Example #21
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_replace (self):
        #print "replace start"
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 10), 0, bpp)
            sf.fill ((255, 0, 0))
            rval = sf.map_rgb ((0, 0, 255))
            oval = sf.map_rgb ((255, 0, 0))
            ar = pygame.PixelArray (sf)
            ar[::2].replace ((255, 0, 0), (0, 0, 255))
            self.assertEqual (ar[0][0], rval)
            self.assertEqual (ar[1][0], oval)
            self.assertEqual (ar[2][3], rval)
            self.assertEqual (ar[3][6], oval)
            self.assertEqual (ar[8][9], rval)
            self.assertEqual (ar[9][9], oval)

            ar[::2].replace ((0, 0, 255), (255, 0, 0), weights=(10, 20, 50))
            self.assertEqual (ar[0][0], oval)
            self.assertEqual (ar[2][3], oval)
            self.assertEqual (ar[3][6], oval)
            self.assertEqual (ar[8][9], oval)
            self.assertEqual (ar[9][9], oval)
        #print "replace end" 
Example #22
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_2dslice_assignment (self):
        w = 2 * 5 * 8
        h = 3 * 5 * 9
        sf = pygame.Surface ((w, h), 0, 32)
        ar = pygame.PixelArray (sf)
        size = (w, h)
        strides = (1, w)
        offset = 0
        self._test_assignment (sf, ar, size, strides, offset)
        xslice = slice (None, None, 2)
        yslice = slice (None, None, 3)
        ar, size, strides, offset = self._array_slice (
            ar, size, (xslice, yslice), strides, offset)
        self._test_assignment (sf, ar, size, strides, offset)
        xslice = slice (5, None, 5)
        yslice = slice (5, None, 5)
        ar, size, strides, offset = self._array_slice (
            ar, size, (xslice, yslice), strides, offset)
        self._test_assignment (sf, ar, size, strides, offset) 
Example #23
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_2dslice_assignment (self):
        w = 2 * 5 * 8
        h = 3 * 5 * 9
        sf = pygame.Surface ((w, h), 0, 32)
        ar = pygame.PixelArray (sf)
        size = (w, h)
        strides = (1, w)
        offset = 0
        self._test_assignment (sf, ar, size, strides, offset)
        xslice = slice (None, None, 2)
        yslice = slice (None, None, 3)
        ar, size, strides, offset = self._array_slice (
            ar, size, (xslice, yslice), strides, offset)
        self._test_assignment (sf, ar, size, strides, offset)
        xslice = slice (5, None, 5)
        yslice = slice (5, None, 5)
        ar, size, strides, offset = self._array_slice (
            ar, size, (xslice, yslice), strides, offset)
        self._test_assignment (sf, ar, size, strides, offset) 
Example #24
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_replace (self):
        #print "replace start"
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 10), 0, bpp)
            sf.fill ((255, 0, 0))
            rval = sf.map_rgb ((0, 0, 255))
            oval = sf.map_rgb ((255, 0, 0))
            ar = pygame.PixelArray (sf)
            ar[::2].replace ((255, 0, 0), (0, 0, 255))
            self.assertEqual (ar[0][0], rval)
            self.assertEqual (ar[1][0], oval)
            self.assertEqual (ar[2][3], rval)
            self.assertEqual (ar[3][6], oval)
            self.assertEqual (ar[8][9], rval)
            self.assertEqual (ar[9][9], oval)

            ar[::2].replace ((0, 0, 255), (255, 0, 0), weights=(10, 20, 50))
            self.assertEqual (ar[0][0], oval)
            self.assertEqual (ar[2][3], oval)
            self.assertEqual (ar[3][6], oval)
            self.assertEqual (ar[8][9], oval)
            self.assertEqual (ar[9][9], oval)
        #print "replace end" 
Example #25
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_contains (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            sf.set_at ((8, 8), (255, 255, 255))

            ar = pygame.PixelArray (sf)
            self.assertTrue ((0, 0, 0) in ar)
            self.assertTrue ((255, 255, 255) in ar)
            self.assertFalse ((255, 255, 0) in ar)
            self.assertFalse (0x0000ff in ar)

            # Test sliced array
            self.assertTrue ((0, 0, 0) in ar[8])
            self.assertTrue ((255, 255, 255) in ar[8])
            self.assertFalse ((255, 255, 0) in ar[8])
            self.assertFalse (0x0000ff in ar[8]) 
Example #26
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_set_column (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            sf2 = pygame.Surface ((6, 8), 0, bpp)
            sf2.fill ((0, 255, 255))
            ar2 = pygame.PixelArray (sf2)

            # Test single value assignment
            ar.__setitem__ (2, (128, 128, 128))
            self.assertEqual (ar[2][0], sf.map_rgb ((128, 128, 128)))
            self.assertEqual (ar[2][1], sf.map_rgb ((128, 128, 128)))

            ar.__setitem__ (-1, (0, 255, 255))
            self.assertEqual (ar[5][0], sf.map_rgb ((0, 255, 255)))
            self.assertEqual (ar[-1][1], sf.map_rgb ((0, 255, 255)))

            ar.__setitem__ (-2, (255, 255, 0))
            self.assertEqual (ar[4][0], sf.map_rgb ((255, 255, 0)))
            self.assertEqual (ar[-2][1], sf.map_rgb ((255, 255, 0)))

            # Test list assignment.
            ar.__setitem__ (0, [(255, 255, 255)] * 8)
            self.assertEqual (ar[0][0], sf.map_rgb ((255, 255, 255)))
            self.assertEqual (ar[0][1], sf.map_rgb ((255, 255, 255)))

            # Test tuple assignment.
            # Changed in Pygame 1.9.2 - Raises an exception.
            self.assertRaises (ValueError, ar.__setitem__, 1,
                               ((204, 0, 204), (17, 17, 17), (204, 0, 204),
                                (17, 17, 17), (204, 0, 204), (17, 17, 17),
                                (204, 0, 204), (17, 17, 17)))

            # Test pixel array assignment.
            ar.__setitem__ (1, ar2.__getitem__ (3))
            self.assertEqual (ar[1][0], sf.map_rgb ((0, 255, 255)))
            self.assertEqual (ar[1][1], sf.map_rgb ((0, 255, 255))) 
Example #27
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_pixels_field(self):
        for bpp in [1, 2, 3, 4]:
            sf = pygame.Surface ((11, 7), 0, bpp * 8)
            ar = pygame.PixelArray (sf)
            ar2 = ar[1:,:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.itemsize)
            ar2 = ar[:,1:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[1])
            ar2 = ar[::-1,:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              (ar.shape[0] - 1) * ar.itemsize)
            ar2 = ar[::-2,:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              (ar.shape[0] - 1) * ar.itemsize)
            ar2 = ar[:,::-1]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              (ar.shape[1] - 1) * ar.strides[1])
            ar3 = ar2[::-1,:]
            self.assertEqual (ar3._pixels_address - ar._pixels_address,
                              (ar.shape[0] - 1) * ar.strides[0] +
                              (ar.shape[1] - 1) * ar.strides[1])
            ar2 = ar[:,::-2]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              (ar.shape[1] - 1) * ar.strides[1])
            ar2 = ar[2::,3::]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[0] * 2 + ar.strides[1] * 3)
            ar2 = ar[2::2,3::4]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[0] * 2 + ar.strides[1] * 3)
            ar2 = ar[9:2:-1,:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[0] * 9)
            ar2 = ar[:,5:2:-1]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[1] * 5)
            ##? ar2 = ar[:,9:2:-1] 
Example #28
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_ass_subscript (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((255, 255, 255))
            ar = pygame.PixelArray (sf)

            # Test ellipse working
            ar[...,...] = (0, 0, 0)
            self.assertEqual (ar[0,0], 0)
            self.assertEqual (ar[1,0], 0)
            self.assertEqual (ar[-1,-1], 0)
            ar[...,] = (0, 0, 255)
            self.assertEqual (ar[0,0], sf.map_rgb ((0, 0, 255)))
            self.assertEqual (ar[1,0], sf.map_rgb ((0, 0, 255)))
            self.assertEqual (ar[-1,-1], sf.map_rgb ((0, 0, 255)))
            ar[:,...] = (255, 0, 0)
            self.assertEqual (ar[0,0], sf.map_rgb ((255, 0, 0)))
            self.assertEqual (ar[1,0], sf.map_rgb ((255, 0, 0)))
            self.assertEqual (ar[-1,-1], sf.map_rgb ((255, 0, 0)))
            ar[...] = (0, 255, 0)
            self.assertEqual (ar[0,0], sf.map_rgb ((0, 255, 0)))
            self.assertEqual (ar[1,0], sf.map_rgb ((0, 255, 0)))
            self.assertEqual (ar[-1,-1], sf.map_rgb ((0, 255, 0)))

            # Ensure x and y are freed for array[x, y] = p
            # Bug fix: reference counting
            if hasattr(sys, 'getrefcount'):
                class Int(int):
                    """Unique int instances"""
                    pass

                sf = pygame.Surface ((2, 2), 0, 32)
                ar = pygame.PixelArray (sf)
                x, y = Int(0), Int(1)
                rx_before, ry_before = sys.getrefcount (x), sys.getrefcount (y)
                ar[x, y] = 0
                rx_after, ry_after = sys.getrefcount (x), sys.getrefcount (y)
                self.assertEqual (rx_after, rx_before)
                self.assertEqual (ry_after, ry_before) 
Example #29
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_get_surface__subclassed_surface(self):
        """Ensure the surface attribute can handle subclassed surfaces."""
        expected_surface = SurfaceSubclass((5, 3), 0, 32)
        pixelarray = pygame.PixelArray(expected_surface)

        surface = pixelarray.surface

        self.assertIs(surface, expected_surface)
        self.assertIsInstance(surface, pygame.Surface)
        self.assertIsInstance(surface, SurfaceSubclass) 
Example #30
Source File: pixelarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_get_slice (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            self.assertEqual (len (ar[0:2]), 2)
            self.assertEqual (len (ar[3:7][3]), 20)

            self.assertEqual (ar[0:0], None)
            self.assertEqual (ar[5:5], None)
            self.assertEqual (ar[9:9], None)

            # Has to resolve to ar[7:8]
            self.assertEqual (len (ar[-3:-2]), 1)      # 2D
            self.assertEqual (len (ar[-3:-2][0]), 20)  # 1D

            # Try assignments.

            # 2D assignment.
            ar[2:5] = (255, 255, 255)

            # 1D assignment
            ar[3][3:7] = (10, 10, 10)
            self.assertEqual (ar[3][5], sf.map_rgb ((10, 10, 10)))
            self.assertEqual (ar[3][6], sf.map_rgb ((10, 10, 10)))