17
17
18
18
import sys
19
19
20
+ try :
21
+ from bitmaptools import readinto as _bitmap_readinto
22
+ except ImportError :
23
+ _bitmap_readinto = None # pylint: disable=invalid-name
24
+
20
25
21
26
def load (
22
27
file ,
@@ -57,7 +62,7 @@ def load(
57
62
minimum_color_depth *= 2
58
63
59
64
if sys .maxsize > 1073741823 :
60
- # pylint: disable=import-outside-toplevel
65
+ # pylint: disable=import-outside-toplevel, relative-beyond-top-level
61
66
from .negative_height_check import negative_height_check
62
67
63
68
# convert unsigned int to signed int when height is negative
@@ -82,17 +87,28 @@ def load(
82
87
83
88
if compression == 0 :
84
89
chunk = bytearray (line_size )
85
- for y in range (range1 , range2 , range3 ):
86
- file .readinto (chunk )
87
- pixels_per_byte = 8 // color_depth
88
- offset = y * width
89
-
90
- for x in range (width ):
91
- i = x // pixels_per_byte
92
- pixel = (
93
- chunk [i ] >> (8 - color_depth * (x % pixels_per_byte + 1 ))
94
- ) & mask
95
- bitmap [offset + x ] = pixel
90
+
91
+ if _bitmap_readinto :
92
+ _bitmap_readinto (
93
+ bitmap ,
94
+ file ,
95
+ bits_per_pixel = color_depth ,
96
+ element_size = 4 ,
97
+ reverse_pixels_in_element = False ,
98
+ reverse_rows = True ,
99
+ )
100
+ else : # use the standard file.readinto
101
+ for y in range (range1 , range2 , range3 ):
102
+ file .readinto (chunk )
103
+ pixels_per_byte = 8 // color_depth
104
+ offset = y * width
105
+
106
+ for x in range (width ):
107
+ i = x // pixels_per_byte
108
+ pixel = (
109
+ chunk [i ] >> (8 - color_depth * (x % pixels_per_byte + 1 ))
110
+ ) & mask
111
+ bitmap [offset + x ] = pixel
96
112
elif compression in (1 , 2 ):
97
113
decode_rle (
98
114
bitmap = bitmap ,
@@ -150,6 +166,7 @@ def decode_rle(bitmap, file, compression, y_range, width):
150
166
# file is 15px wide but has data for 16px.
151
167
width_remaining = width - x
152
168
169
+ print ("doing this too" )
153
170
file .readinto (run_buf )
154
171
155
172
if run_buf [0 ] == 0 :
0 commit comments