@@ -278,8 +278,36 @@ def test_bits2byte__limit() -> None:
278278 bits2byte (data = b"TEST" , size = (9000 , 8500 ), bits = 8 )
279279
280280
281- def test_bits2byte__truncated_data (caplog ) -> None :
281+ def test_bits2byte__truncated_data (caplog : pytest . LogCaptureFixture ) -> None :
282282 # 4x4 image at 2 bits per sample needs 4 bytes; provide only 1.
283283 result = bits2byte (data = b"\x00 " , size = (4 , 4 ), bits = 2 )
284284 assert result == bytes (16 )
285285 assert "Image data is not rectangular. Adding padding." in caplog .text
286+
287+
288+ def test_handle_flate__truncated_2bit_image (caplog : pytest .LogCaptureFixture ) -> None :
289+ # A 3x3 indexed image at 2 bits per sample needs 3 bytes; provide only 1.
290+ # Padding the missing bytes lets the image still be loaded instead of
291+ # raising IndexError out of bits2byte.
292+ lookup = DecodedStreamObject ()
293+ lookup .set_data (bytes ([0 , 0 , 0 , 10 , 10 , 10 , 20 , 20 , 20 , 30 , 30 , 30 ]))
294+ result = _handle_flate (
295+ size = (3 , 3 ),
296+ data = b"\xe4 " ,
297+ mode = "2bits" ,
298+ color_space = ArrayObject (
299+ [NameObject ("/Indexed" ), NameObject ("/DeviceRGB" ), NumberObject (3 ), lookup ]
300+ ),
301+ colors = 1 ,
302+ obj_as_text = "dummy" ,
303+ )
304+ image = result [0 ]
305+ image .load ()
306+ assert image .mode == "RGB"
307+ assert image .size == (3 , 3 )
308+ assert get_image_data (image ) == (
309+ (30 , 30 , 30 ), (20 , 20 , 20 ), (10 , 10 , 10 ),
310+ (0 , 0 , 0 ), (0 , 0 , 0 ), (0 , 0 , 0 ),
311+ (0 , 0 , 0 ), (0 , 0 , 0 ), (0 , 0 , 0 ),
312+ )
313+ assert "Image data is not rectangular. Adding padding." in caplog .text
0 commit comments