def __init__(self, imageBuffer, size):
width, height = self.get_size(size)
self.size = size
self.surface = Surface((width, height), depth=8)
# Read the header
paletteLength, imageDataLength = self.read_ugar_header(imageBuffer)
# Read the image palette and unpack
palette = self.unpack_palette(np.fromstring(imageBuffer.read(self.round_to_power(paletteLength)), dtype=np.uint16))
self.surface.set_palette(palette)
# All pixels with the index of 0 are transparent
self.surface.set_colorkey(0)
# Read the pixel data bytes
pixelData = np.fromstring(imageBuffer.read(imageDataLength), dtype=np.uint8)
# Split each byte into 2 pixels
pixels = np.stack((np.bitwise_and(pixelData, 0x0f), np.bitwise_and(pixelData >> 4, 0x0f)), axis=-1).flatten()
pixels = np.swapaxes(np.reshape(pixels, (-1, width)), 0, 1)
pixelcopy.array_to_surface(self.surface, pixels)
评论列表
文章目录