#!/usr/bin/env python from array import array class pixelstrip: "abstract class for horizontal strips of pixels" pass class mcpixelstrip(pixelstrip): "horizontal strip of multicolor pixels" def __init__(self, byte, colors): self.byte = byte self.colors = colors def pixels(self): "return a list of four pixels" return ( self.colors[self.byte >> 6], self.colors[(self.byte >> 4) & 3], self.colors[(self.byte >> 2) & 3], self.colors[self.byte & 3] ) class bitmap: "abstract class for 320x200 bitmaps" pass class mcbitmap(bitmap): "class representing a multicolor bitmap image" def __init__(self): self.charram = array('B', [0 for i in range(1000)]) self.colram = array('B', [0 for i in range(1000)]) self.gfxram = array('B', [0 for i in range(8000)]) self.bkgcolor = 0 def render(self, pixels): for y in range(25): for x in range (40): for line in range(8): byte = self.gfxram[y*320 + x*8 + line] coloffset = y * 40 + x pstrip = mcpixelstrip( byte, ( self.bkgcolor, self.charram[coloffset] >> 4, self.charram[coloffset] & 0x0f, self.colram[coloffset] & 0x0f ) ) offset = (y * 8 + line) * 320 + x * 8 for pixel in pstrip.pixels(): pixels[offset] = pixel pixels[offset + 1] = pixel offset += 2 from timeit import Timer benchmark = Timer( """ koala.render(pixels) """, """ from __main__ import mcbitmap koala = mcbitmap() koala.bkgcolor = 0 for i in range(1000): koala.charram[i] = 0xbc koala.colram[i] = 0x0f for i in range(8000): koala.gfxram[i] = 0x1b pixels = [0 for i in range(320*200)] """) print benchmark.timeit(10) / 10