2727display .init ()
2828display .fill (WHITE )
2929
30+ def convert_555_to_565 (rgb ):
31+ return (rgb & 0x7FE0 ) << 1 | 0x20 | rgb & 0x001F
32+
3033class BMP (object ):
3134 def __init__ (self , filename ):
3235 self .filename = filename
@@ -35,7 +38,8 @@ def __init__(self, filename):
3538 self .data_size = 0
3639 self .bpp = 0
3740 self .width = 0
38- self .height = 0
41+ self .height = 0
42+ self .read_header ()
3943
4044 def read_header (self ):
4145 if self .colors :
@@ -54,7 +58,6 @@ def read_header(self):
5458 self .colors = int .from_bytes (f .read (4 ), 'little' )
5559
5660 def draw (self , disp , x = 0 , y = 0 ):
57- self .read_header ()
5861 print ("{:d}x{:d} image" .format (self .width , self .height ))
5962 print ("{:d}-bit encoding detected" .format (self .bpp ))
6063 line = 0
@@ -72,12 +75,15 @@ def draw(self, disp, x=0, y=0):
7275 if (line_size - i ) < self .bpp // 8 :
7376 break
7477 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 ])
7881 current_line_data = current_line_data + struct .pack (">H" , color )
7982 disp .setxy (x , self .height - line + y )
8083 disp .push_pixels (current_line_data )
8184 disp .set_window (0 , 0 , disp .width , disp .height )
8285
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