27
27
display .init ()
28
28
display .fill (WHITE )
29
29
30
+ def convert_555_to_565 (rgb ):
31
+ return (rgb & 0x7FE0 ) << 1 | 0x20 | rgb & 0x001F
32
+
30
33
class BMP (object ):
31
34
def __init__ (self , filename ):
32
35
self .filename = filename
@@ -35,7 +38,8 @@ def __init__(self, filename):
35
38
self .data_size = 0
36
39
self .bpp = 0
37
40
self .width = 0
38
- self .height = 0
41
+ self .height = 0
42
+ self .read_header ()
39
43
40
44
def read_header (self ):
41
45
if self .colors :
@@ -54,7 +58,6 @@ def read_header(self):
54
58
self .colors = int .from_bytes (f .read (4 ), 'little' )
55
59
56
60
def draw (self , disp , x = 0 , y = 0 ):
57
- self .read_header ()
58
61
print ("{:d}x{:d} image" .format (self .width , self .height ))
59
62
print ("{:d}-bit encoding detected" .format (self .bpp ))
60
63
line = 0
@@ -72,12 +75,15 @@ def draw(self, disp, x=0, y=0):
72
75
if (line_size - i ) < self .bpp // 8 :
73
76
break
74
77
if self .bpp == 16 :
75
- color = line_data [i ] << 8 | line_data [i + 1 ]
76
- if self .bpp == 24 :
77
- color = color565 (line_data [i ], line_data [i + 1 ], line_data [i + 2 ])
78
+ color = convert_555_to_565 ( line_data [i ] | line_data [i + 1 ] << 8 )
79
+ if self .bpp == 24 or self . bpp == 32 :
80
+ color = color565 (line_data [i + 2 ], line_data [i + 1 ], line_data [i ])
78
81
current_line_data = current_line_data + struct .pack (">H" , color )
79
82
disp .setxy (x , self .height - line + y )
80
83
disp .push_pixels (current_line_data )
81
84
disp .set_window (0 , 0 , disp .width , disp .height )
82
85
83
- BMP ("/ra8875_blinka.bmp" ).draw (display , 287 , 127 )
86
+ bitmap = BMP ("/ra8875_blinka.bmp" )
87
+ x_position = (display .width // 2 ) - (bitmap .width // 2 )
88
+ y_position = (display .height // 2 ) - (bitmap .height // 2 )
89
+ bitmap .draw (display , x_position , y_position )
0 commit comments