1212
1313from libjpeg import get_parameters
1414from libjpeg .data import get_indexed_datasets , JPEG_DIRECTORY
15+ from libjpeg .utils import LIBJPEG_ERROR_CODES
1516
1617
1718DIR_10918 = os .path .join (JPEG_DIRECTORY , "10918" )
@@ -74,6 +75,47 @@ def test_get_parameters_bytes():
7475 assert info [3 ] == params ["precision" ]
7576
7677
78+ def test_get_parameters_binary ():
79+ """Test get_parameters() using binaryio."""
80+ with open (os .path .join (DIR_10918 , "p1" , "A1.JPG" ), "rb" ) as f :
81+ params = get_parameters (f )
82+
83+ info = (257 , 255 , 4 , 8 )
84+
85+ assert (info [0 ], info [1 ]) == (params ["rows" ], params ["columns" ])
86+ assert info [2 ] == params ["nr_components" ]
87+ assert info [3 ] == params ["precision" ]
88+
89+
90+ def test_get_parameters_path ():
91+ """Test get_parameters() using a path."""
92+ params = get_parameters (os .path .join (DIR_10918 , "p1" , "A1.JPG" ))
93+
94+ info = (257 , 255 , 4 , 8 )
95+
96+ assert (info [0 ], info [1 ]) == (params ["rows" ], params ["columns" ])
97+ assert info [2 ] == params ["nr_components" ]
98+ assert info [3 ] == params ["precision" ]
99+
100+ @pytest .fixture
101+ def remove_error_code ():
102+ msg = LIBJPEG_ERROR_CODES [- 1038 ]
103+ del LIBJPEG_ERROR_CODES [- 1038 ]
104+ yield
105+ LIBJPEG_ERROR_CODES [- 1038 ] = msg
106+
107+
108+ @pytest .mark .skipif (not HAS_PYDICOM , reason = "No dependencies" )
109+ def test_get_parameters_unknown_error (remove_error_code ):
110+ """Test get_parameters() using a path."""
111+ msg = (
112+ r"Unknown error code '-1038' returned from GetJPEGParameters\(\): "
113+ r"unexpected EOF while parsing the image"
114+ )
115+ with pytest .raises (RuntimeError , match = msg ):
116+ get_parameters (b"\xFF \xD8 \xFF \xD8 \x01 \x02 \x03 " )
117+
118+
77119@pytest .mark .skipif (not HAS_PYDICOM , reason = "No pydicom" )
78120def test_non_conformant_raises ():
79121 """Test that a non-conformant JPEG image raises an exception."""
@@ -166,13 +208,8 @@ def test_jls_lossless(self, fname, info):
166208 # info: (rows, columns, spp, bps)
167209 index = get_indexed_datasets ("1.2.840.10008.1.2.4.80" )
168210 ds = index [fname ]["ds" ]
169-
170- print (fname )
171-
172211 frame = next (self .generate_frames (ds ))
173212 params = get_parameters (frame )
174- print (params )
175- print (info )
176213
177214 assert (info [0 ], info [1 ]) == (params ["rows" ], params ["columns" ])
178215 assert info [2 ] == params ["nr_components" ]
@@ -184,7 +221,6 @@ def test_jls_lossy(self, fname, info):
184221 # info: (rows, columns, spp, bps)
185222 index = get_indexed_datasets ("1.2.840.10008.1.2.4.81" )
186223 ds = index [fname ]["ds" ]
187-
188224 frame = next (self .generate_frames (ds ))
189225 params = get_parameters (frame )
190226
0 commit comments