11# SPDX-FileCopyrightText: 2018 Scott Shawcroft for Adafruit Industries
2- # SPDX-FileCopyrightText: 2022 Matt Land
2+ # SPDX-FileCopyrightText: 2022-2023 Matt Land
33#
44# SPDX-License-Identifier: MIT
55
@@ -43,9 +43,9 @@ def load(
4343 color_depth : int ,
4444 compression : int ,
4545 * ,
46- bitmap : BitmapConstructor = None ,
47- palette : PaletteConstructor = None ,
48- ) -> Tuple [Bitmap , Optional [Palette ]]:
46+ bitmap : Optional [ BitmapConstructor ] = None ,
47+ palette : Optional [ PaletteConstructor ] = None ,
48+ ) -> Tuple [Optional [ Bitmap ] , Optional [Palette ]]:
4949 """Loads indexed bitmap data into bitmap and palette objects.
5050
5151 :param file file: The open bmp file
@@ -54,19 +54,24 @@ def load(
5454 :param int data_start: Byte location where the data starts (after headers)
5555 :param int colors: Number of distinct colors in the image
5656 :param int color_depth: Number of bits used to store a value
57- :param int compression: 0 - none, 1 - 8bit RLE, 2 - 4bit RLE"""
57+ :param int compression: 0 - none, 1 - 8bit RLE, 2 - 4bit RLE
58+ :param BitmapConstructor bitmap: a function that returns a displayio.Bitmap
59+ :param PaletteConstructor palette: a function that returns a displayio.Palette
60+ """
5861 # pylint: disable=too-many-arguments,too-many-locals,too-many-branches
62+ palette_obj = None
5963 if palette :
60- palette = palette (colors ) # type: Palette
64+ palette_obj = palette (colors )
6165
6266 file .seek (data_start - colors * 4 )
6367 for value in range (colors ):
6468 c_bytes = file .read (4 )
6569 # Need to swap red & blue bytes (bytes 0 and 2)
66- palette [value ] = bytes (
70+ palette_obj [value ] = bytes (
6771 b"" .join ([c_bytes [2 :3 ], c_bytes [1 :2 ], c_bytes [0 :1 ], c_bytes [3 :1 ]])
6872 )
6973
74+ bitmap_obj = None
7075 if bitmap :
7176 minimum_color_depth = 1
7277 while colors > 2 ** minimum_color_depth :
@@ -78,7 +83,7 @@ def load(
7883
7984 # convert unsigned int to signed int when height is negative
8085 height = negative_height_check (height )
81- bitmap = bitmap (width , abs (height ), colors ) # type: Bitmap
86+ bitmap_obj = bitmap (width , abs (height ), colors )
8287 file .seek (data_start )
8388 line_size = width // (8 // color_depth )
8489 if width % (8 // color_depth ) != 0 :
@@ -97,10 +102,9 @@ def load(
97102 range3 = 1
98103
99104 if compression == 0 :
100-
101105 if _bitmap_readinto :
102106 _bitmap_readinto (
103- bitmap ,
107+ bitmap_obj ,
104108 file ,
105109 bits_per_pixel = color_depth ,
106110 element_size = 4 ,
@@ -120,17 +124,17 @@ def load(
120124 pixel = (
121125 chunk [i ] >> (8 - color_depth * (x % pixels_per_byte + 1 ))
122126 ) & mask
123- bitmap [offset + x ] = pixel
127+ bitmap_obj [offset + x ] = pixel
124128 elif compression in (1 , 2 ):
125129 decode_rle (
126- bitmap = bitmap ,
130+ bitmap = bitmap_obj ,
127131 file = file ,
128132 compression = compression ,
129133 y_range = (range1 , range2 , range3 ),
130134 width = width ,
131135 )
132136
133- return bitmap , palette
137+ return bitmap_obj , palette_obj
134138
135139
136140def decode_rle (
0 commit comments