@@ -202,19 +202,10 @@ def test_rle4() -> None:
202202 assert_image_similar_tofile (im , "Tests/images/bmp/g/pal4.bmp" , 12 )
203203
204204
205- def test_rle4_absolute_odd () -> None :
206- # An RLE4 absolute run with an odd number of pixels is packed into
207- # ceil(count / 2) bytes, the final nibble being padding. Build a 3x1
208- # image whose single row is one absolute run of 3 pixels (indices 1, 2, 3).
209- palette = b"\x00 " * 4
210- rle = (
211- b"\x00 \x03 " # absolute mode, 3 pixels
212- b"\x12 \x30 " # nibbles 1, 2, 3 and a padding nibble
213- b"\x00 \x01 " # end of bitmap
214- )
205+ def encode_rle4 (width : int , palette : bytes , rle : bytes ) -> io .BytesIO :
215206 header = (
216207 o32 (40 ) # header size
217- + o32 (3 ) # width
208+ + o32 (width ) # width
218209 + o32 (1 ) # height
219210 + o16 (1 ) # planes
220211 + o16 (4 ) # bits per pixel
@@ -225,7 +216,7 @@ def test_rle4_absolute_odd() -> None:
225216 + o32 (0 ) # important colors
226217 )
227218 offset = 14 + len (header ) + len (palette )
228- data = (
219+ return io . BytesIO (
229220 b"BM"
230221 + o32 (offset + len (rle )) # file size
231222 + o32 (0 ) # reserved
@@ -235,7 +226,32 @@ def test_rle4_absolute_odd() -> None:
235226 + rle
236227 )
237228
238- with Image .open (io .BytesIO (data )) as im :
229+ def test_rle4_black () -> None :
230+ rle = (
231+ b"\x00 \x03 " # absolute mode, 3 pixels
232+ b"\x0F " # nibbles 0 and 255
233+ b"\x00 \x01 " # end of bitmap
234+ )
235+ b = encode_rle4 (2 , b"\x00 " * 4 , rle )
236+
237+ with Image .open (b ) as im :
238+ assert im .mode == "1"
239+ assert [im .getpixel ((x , 0 )) for x in range (im .width )] == [0 , 255 ]
240+
241+
242+ def test_rle4_absolute_odd () -> None :
243+ # An RLE4 absolute run with an odd number of pixels is packed into
244+ # ceil(count / 2) bytes, the final nibble being padding. Build a 3x1
245+ # image whose single row is one absolute run of 3 pixels (indices 1, 2, 3).
246+ palette = b"\x01 " * 4
247+ rle = (
248+ b"\x00 \x03 " # absolute mode, 3 pixels
249+ b"\x12 \x30 " # nibbles 1, 2, 3 and a padding nibble
250+ b"\x00 \x01 " # end of bitmap
251+ )
252+ b = encode_rle4 (3 , palette , rle )
253+
254+ with Image .open (b ) as im :
239255 assert [im .getpixel ((x , 0 )) for x in range (im .width )] == [1 , 2 , 3 ]
240256
241257
0 commit comments