33import pytest
44
55try :
6- from pydicom .encaps import generate_frames
6+ from pydicom import __version__
7+ from pydicom .encaps import generate_pixel_data_frame
8+ from pydicom .pixel_data_handlers .util import (
9+ reshape_pixel_array ,
10+ pixel_dtype ,
11+ )
712
813 HAS_PYDICOM = True
914except ImportError :
1217from openjpeg import get_parameters , decode_pixel_data
1318from openjpeg .data import get_indexed_datasets
1419
20+ if HAS_PYDICOM :
21+ PYD_VERSION = int (__version__ .split ("." )[0 ])
1522
16- # def generate_frames(ds.PixelData):
17- # """Return a frame generator for DICOM datasets."""
18- # nr_frames = ds.get("NumberOfFrames", 1)
19- # return generate_frames(ds.PixelData, number_of_frames=nr_frames)
23+
24+ def generate_frames (ds ):
25+ """Return a frame generator for DICOM datasets."""
26+ nr_frames = ds .get ("NumberOfFrames" , 1 )
27+ return generate_pixel_data_frame (ds .PixelData , nr_frames )
2028
2129
2230@pytest .mark .skipif (not HAS_PYDICOM , reason = "pydicom unavailable" )
@@ -27,7 +35,7 @@ def test_invalid_type_raises(self):
2735 """Test decoding using invalid type raises."""
2836 index = get_indexed_datasets ("1.2.840.10008.1.2.4.90" )
2937 ds = index ["MR_small_jp2klossless.dcm" ]["ds" ]
30- frame = tuple (next (generate_frames (ds . PixelData )))
38+ frame = tuple (next (generate_frames (ds )))
3139 assert not hasattr (frame , "tell" ) and not isinstance (frame , bytes )
3240
3341 msg = "a bytes-like object is required, not 'tuple'"
@@ -37,7 +45,7 @@ def test_invalid_type_raises(self):
3745 def test_no_dataset (self ):
3846 index = get_indexed_datasets ("1.2.840.10008.1.2.4.90" )
3947 ds = index ["MR_small_jp2klossless.dcm" ]["ds" ]
40- frame = next (generate_frames (ds . PixelData ))
48+ frame = next (generate_frames (ds ))
4149 arr = decode_pixel_data (frame )
4250 assert arr .flags .writeable
4351 assert "uint8" == arr .dtype
@@ -113,10 +121,17 @@ def test_raises(self):
113121 assert 8 == ds .BitsAllocated == ds .BitsStored
114122 assert 0 == ds .PixelRepresentation
115123
116- msg = (
117- "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
118- "not installed"
119- )
124+ if PYD_VERSION < 3 :
125+ msg = (
126+ "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
127+ "not installed"
128+ )
129+ else :
130+ msg = (
131+ r"Unable to decompress 'JPEG Baseline \(Process 1\)' pixel data because "
132+ "all plugins are missing dependencies:"
133+ )
134+
120135 with pytest .raises (RuntimeError , match = msg ):
121136 ds .pixel_array
122137
@@ -143,10 +158,17 @@ def test_raises(self):
143158 assert 10 == ds .BitsStored
144159 assert 0 == ds .PixelRepresentation
145160
146- msg = (
147- "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
148- "not installed"
149- )
161+ if PYD_VERSION < 3 :
162+ msg = (
163+ "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
164+ "not installed"
165+ )
166+ else :
167+ msg = (
168+ r"Unable to decompress 'JPEG Extended \(Process 2 and 4\)' pixel data because "
169+ "all plugins are missing dependencies:"
170+ )
171+
150172 with pytest .raises (RuntimeError , match = msg ):
151173 ds .pixel_array
152174
@@ -171,10 +193,17 @@ def test_raises(self):
171193 assert 12 == ds .BitsStored
172194 assert 0 == ds .PixelRepresentation
173195
174- msg = (
175- "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
176- "not installed"
177- )
196+ if PYD_VERSION < 3 :
197+ msg = (
198+ "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
199+ "not installed"
200+ )
201+ else :
202+ msg = (
203+ r"Unable to decompress 'JPEG Lossless, Non-Hierarchical \(Process "
204+ r"14\)' pixel data because all plugins are missing dependencies:"
205+ )
206+
178207 with pytest .raises (RuntimeError , match = msg ):
179208 ds .pixel_array
180209
@@ -200,10 +229,18 @@ def test_raises(self):
200229 assert 8 == ds .BitsStored
201230 assert 0 == ds .PixelRepresentation
202231
203- msg = (
204- "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
205- "not installed"
206- )
232+ if PYD_VERSION < 3 :
233+ msg = (
234+ "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
235+ "not installed"
236+ )
237+ else :
238+ msg = (
239+ "Unable to decompress 'JPEG Lossless, Non-Hierarchical, First-Order "
240+ r"Prediction \(Process 14 \[Selection Value 1\]\)' "
241+ "pixel data because all plugins are missing dependencies:"
242+ )
243+
207244 with pytest .raises (RuntimeError , match = msg ):
208245 ds .pixel_array
209246
@@ -229,10 +266,17 @@ def test_raises(self):
229266 assert 16 == ds .BitsStored
230267 assert 1 == ds .PixelRepresentation
231268
232- msg = (
233- "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
234- "not installed"
235- )
269+ if PYD_VERSION < 3 :
270+ msg = (
271+ "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
272+ "not installed"
273+ )
274+ else :
275+ msg = (
276+ r"Unable to decompress 'JPEG-LS Lossless Image Compression' "
277+ "pixel data because all plugins are missing dependencies:"
278+ )
279+
236280 with pytest .raises (RuntimeError , match = msg ):
237281 ds .pixel_array
238282
@@ -257,10 +301,17 @@ def test_raises(self):
257301 assert 16 == ds .BitsStored
258302 assert 1 == ds .PixelRepresentation
259303
260- msg = (
261- "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
262- "not installed"
263- )
304+ if PYD_VERSION < 3 :
305+ msg = (
306+ "Unable to convert the Pixel Data as the 'pylibjpeg-libjpeg' plugin is "
307+ "not installed"
308+ )
309+ else :
310+ msg = (
311+ r"Unable to decompress 'JPEG-LS Lossy \(Near-Lossless\) Image Compression' "
312+ "pixel data because all plugins are missing dependencies:"
313+ )
314+
264315 with pytest .raises (RuntimeError , match = msg ):
265316 ds .pixel_array
266317
@@ -585,7 +636,7 @@ def test_data_unsigned_pr_1(self):
585636 # Note: if PR is 1 but the JPEG data is unsigned then it should
586637 # probably be converted to signed using 2s complement
587638 ds .pixel_array
588- frame = next (generate_frames (ds . PixelData ))
639+ frame = next (generate_frames (ds ))
589640 params = get_parameters (frame )
590641 assert params ["is_signed" ] is False
591642
@@ -605,7 +656,7 @@ def test_data_signed_pr_0(self):
605656 # Note: if PR is 0 but the JPEG data is signed then... ?
606657 ds .pixel_array
607658
608- frame = next (generate_frames (ds . PixelData ))
659+ frame = next (generate_frames (ds ))
609660 params = get_parameters (frame )
610661 assert params ["is_signed" ] is True
611662
0 commit comments