def make_surface(array): """pygame.numpyarray.make_surface(array): return Surface copy an array to a new surface Create a new Surface that best resembles the data and format on the array. The array can be 2D or 3D with any sized integer values. """ # Taken from from Alex Holkner's pygame-ctypes package. Thanks a # lot. bpp = 0 r = g = b = 0 shape = array.shape if len (shape) == 2: # 2D array bpp = 8 r = 0xFF >> 6 << 5 g = 0xFF >> 5 << 2 b = 0xFF >> 6 elif len (shape) == 3 and shape[2] == 3: bpp = 32 r = 0xff << 16 g = 0xff << 8 b = 0xff else: raise ValueError("must be a valid 2d or 3d array") surface = pygame.Surface ((shape[0], shape[1]), 0, bpp, (r, g, b, 0)) array_to_surface(surface, array) return surface
def test_format_newbuf(self): Exporter = self.buftools.Exporter surface = self.surface shape = surface.get_size() w, h = shape for format in ['=i', '=I', '=l', '=L', '=q', '=Q', '<i', '>i', '!i', '1i', '=1i', '@q', 'q', '4x', '8x']: surface.fill((255, 254, 253)) exp = Exporter(shape, format=format) exp._buf[:] = [42] * exp.buflen array_to_surface(surface, exp) for x in range(w): for y in range(h): self.assertEqual(surface.get_at((x, y)), (42, 42, 42, 255)) # Some unsupported formats for array_to_surface and a 32 bit surface for format in ['f', 'd', '?', 'x', '1x', '2x', '3x', '5x', '6x', '7x', '9x']: exp = Exporter(shape, format=format) self.assertRaises(ValueError, array_to_surface, surface, exp)
def blit_array (surface, array): """pygame.surfarray.blit_array(Surface, array): return None Blit directly from a array values. Directly copy values from an array into a Surface. This is faster than converting the array into a Surface and blitting. The array must be the same dimensions as the Surface and will completely replace all pixel values. Only integer, ascii character and record arrays are accepted. This function will temporarily lock the Surface as the new values are copied. """ return array_to_surface(surface, array)
def test_array_to_surface_newbuf(self): array = self.Array2D(range(0, 15)) self.assertNotEqual(array.content[0], self.surface.get_at_mapped((0, 0))) array_to_surface(self.surface, array) self.assertCopy2D(self.surface, array)