-
Notifications
You must be signed in to change notification settings - Fork 0
/
readbmp.py
43 lines (35 loc) · 1.46 KB
/
readbmp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import framebuf,gc
# Read a BMP image direct into a framebuffer
# 24bit uncompressed
# Allocate the framebufffer to the same size as image, and returns framebuffer
# designed for low memory e.g. pi pico
def readbmp(filename):
def lebytes_to_int(bytes):
n = 0x00
while len(bytes) > 0:
n <<= 8
n |= bytes.pop()
return int(n)
f = open(filename, 'rb')
img_bytes = list(bytearray(f.read(26))) # just read header
start_pos = lebytes_to_int(img_bytes[10:14])
width = lebytes_to_int(img_bytes[18:22])
height = lebytes_to_int(img_bytes[22:26])
seektostart = f.read(start_pos - 26)
gc.collect()
buffer = bytearray(height * width * 2)
spritesheet = framebuf.FrameBuffer(buffer, width, height, framebuf.RGB565)
for x in range(height):
colrow= list(bytearray(f.read(3 * width)))
for y in range(width):
b,g,r = colrow[y*3:y*3+3]
# RGB565
rgb = ((r >> 3) << 11) | ((g >>2) << 5) | (b >> 3 )
# swap needed for ILI9341 screen
swapL = rgb >> 8
swapH = (rgb & 0x00FF) << 8
col = swapL | swapH
fb.pixel(y,height - x,col)
spritesheet.pixel(y,height - x,col)
f.close()
return (spritesheet)