Skip to content

Instantly share code, notes, and snippets.

@mharriger
Last active August 6, 2016 14:05
Show Gist options
  • Save mharriger/8711710 to your computer and use it in GitHub Desktop.
Save mharriger/8711710 to your computer and use it in GitHub Desktop.
Python fast monochrome bitmap ideas
from bitarray import bitarray
class Bitmap:
def __init__(self, width, height, major_axis = 'x'):
self._width = width
self._height = height
self._data = (width * height) * bitarray('0')
self._major_axis = major_axis
def _get_rows(self):
height = self._height
width = self._width
if self._major_axis == 'y':
(width, height) = (height, width)
if self._major_axis == 'x':
for y in range(height):
yield self._data[(width * y):(width * y) + width]
else:
for y in range(self._height):
yield self._data[ y:(self._width * self._height):self._height]
rows = property(_get_rows, None, None)
def _get_cols(self):
height = self._height
width = self._width
if self._major_axis == 'y':
for y in range(width):
yield self._data[(height * y):(height * y) + height]
else:
for y in range(width):
yield self._data[y:(height * width):width]
cols = property(_get_cols, None, None)
# def getColAsByte(self, page, col):
# #Used if bits are stored as sequential rows
# return self._data[(page * self._width * 8) + col:8 * self._width:128]
# def transformToColBytes(self):
# #Transforms to the format needed by the display memory of the ssd1306 controller
# data = bitarray()
# for page in range(self._height / 8):
# for col in range(self._width):
# # print page, col
# #print page * self._width * 8 + col, 8 * self._width
# #print self._data[(page * self._width * 8) + col:((page + 1) *
# # self._width * 8) - 1:self._width]
# data.extend(self._data[(page * self._width * 8) + col:((page + 1) *
# self._width * 8) - 1:self._width])
# return data
def replace_rect(self, x, y, bitmap):
height = self._height
width = self._width
bheight = bitmap._height
bwidth = bitmap._width
if self._major_axis == 'y':
(x,y) = (y,x)
(width, height) = (height, width)
(bheight, bwidth) = (bwidth, bheight)
count = 0
if self._major_axis == 'x':
for row in bitmap.rows:
self._data[((count + y) * width) + x:((count + y) * width) + x +
len(row)] \
= row
count += 1
if self._major_axis == 'y':
for col in bitmap.cols:
self._data[((count + y) * width) + x:((count + y) * width) + x +
len(col)] \
= col
count += 1
def draw_pixel(self, x, y, on=True):
height = self._height
width = self._width
if self._major_axis == 'y':
(x,y) = (y,x)
(width, height) = (height, width)
self._data[(y * width) + x] = on
def dump(self):
for row in self.rows:
print row
if __name__ == "__main__":
import time
bmp = Bitmap(32, 16, major_axis = 'x')
#for i in range(16):
# bmp.drawPixel(15, i)
#bmp.drawPixel(15, 14)
#bmp.dump()
#print "-------"
bmp2 = Bitmap(32, 8, major_axis = 'x')
bmp2._data = (32 * 8) * bitarray('1')
#for col in bmp2.cols:
# print col
#print "---"
#t1 = time.time()
#for i in range(1000):
# bmp.replaceRect(0,13,bmp2)
# bmp.transformToColBytes()
#print (time.time() - t1) / 1000
bmp.replace_rect(0,1,bmp2)
#bmp2.dump()
bmp.dump()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment