@@ -20,8 +20,8 @@ def decode_pixel_data(src: bytes, ds: "Dataset", **kwargs) -> "np.ndarray":
20
20
ds : pydicom.dataset.Dataset
21
21
A :class:`~pydicom.dataset.Dataset` containing the group ``0x0028``
22
22
elements corresponding to the image frame.
23
- kwargs : dict, optional
24
- A dictionary containing options for the decoder. Current options are:
23
+ ** kwargs
24
+ Current decoding options are:
25
25
26
26
* ``{'byteorder': str}`` specify the byte ordering for the decoded data
27
27
when more than 8 bits per pixel are used, should be '<' for little
@@ -64,17 +64,16 @@ def encode_array(
64
64
The dataset corresponding to `arr` with matching values for *Rows*,
65
65
*Columns*, *Samples per Pixel* and *Bits Allocated*. Required if
66
66
the array properties aren't specified using `kwargs`.
67
- kwargs : dict, optional
68
- A dictionary containing keyword arguments. Required if `ds` isn't used,
69
- keys are:
67
+ **kwargs
68
+ Required keyword parameters if `ds` isn't used are:
70
69
71
- * ``{ 'rows': int, 'columns': int} `` the number of rows and columns
72
- contained in `arr`.
73
- * ``{ samples_per_px': int} `` the number of samples per pixel, either
70
+ * ``'rows': int`` the number of rows contained in `src`
71
+ * ``'columns': int`` the number of columns contained in `src`
72
+ * ``samples_per_px': int`` the number of samples per pixel, either
74
73
1 for monochrome or 3 for RGB or similar data.
75
- * ``{ 'bits_per_px': int} `` the number of bits needed to contain each
74
+ * ``'bits_per_px': int`` the number of bits needed to contain each
76
75
pixel, either 8, 16, 32 or 64.
77
- * ``{ 'nr_frames': int} `` the number of frames in `arr`, required if
76
+ * ``'nr_frames': int`` the number of frames in `arr`, required if
78
77
more than one frame is present.
79
78
80
79
Yields
@@ -91,11 +90,11 @@ def encode_array(
91
90
if ds :
92
91
kwargs ['rows' ] = ds .Rows
93
92
kwargs ['columns' ] = ds .Columns
94
- kwargs ['samples_per_px ' ] = ds .SamplesPerPixel
95
- kwargs ['bits_per_px ' ] = ds .BitsAllocated
96
- kwargs ['nr_frames ' ] = int (getattr (ds , "NumberOfFrames" , 1 ))
93
+ kwargs ['samples_per_pixel ' ] = ds .SamplesPerPixel
94
+ kwargs ['bits_allocated ' ] = ds .BitsAllocated
95
+ kwargs ['number_of_frames ' ] = int (getattr (ds , "NumberOfFrames" , 1 ) or 1 )
97
96
98
- if kwargs ['nr_frames ' ] > 1 :
97
+ if kwargs ['number_of_frames ' ] > 1 :
99
98
for frame in arr :
100
99
yield encode_pixel_data (frame .tobytes (), ** kwargs )
101
100
else :
@@ -127,43 +126,42 @@ def encode_pixel_data(
127
126
*Columns*, *Samples per Pixel* and *Bits Allocated*. Required if
128
127
the frame properties aren't specified using `kwargs`.
129
128
byteorder : str, optional
130
- Required if the samples per pixel is greater than 1 and the value is
131
- not passed using `kwargs`. If `src` is in little-endian byte order
132
- then ``'<'``, otherwise ``'>'`` for big-endian.
133
- kwargs : dict
134
- A dictionary containing keyword arguments. Required keys are:
135
-
136
- * ``{ 'rows': int, 'columns': int} `` the number of rows and columns
137
- contained in `src`
138
- * ``{samples_per_px ': int} `` the number of samples per pixel, either
129
+ Required if the samples per pixel is greater than 1. If `src` is in
130
+ little-endian byte order then ``'<'``, otherwise ``'>'`` for
131
+ big-endian.
132
+ ** kwargs
133
+ If `ds` is not used then the following are required :
134
+
135
+ * ``'rows': int`` the number of rows contained in `src`
136
+ * ``'columns': int`` the number of columns contained in `src`
137
+ * ``samples_per_pixel ': int`` the number of samples per pixel, either
139
138
1 for monochrome or 3 for RGB or similar data.
140
- * ``{'bits_per_px ': int} `` the number of bits needed to contain each
139
+ * ``'bits_allocated ': int`` the number of bits needed to contain each
141
140
pixel, either 8, 16, 32 or 64.
142
- * ``{'byteorder': str}``, required if the samples per pixel is greater
143
- than 1. If `src` is in little-endian byte order then ``'<'``,
144
- otherwise ``'>'`` for big-endian.
145
141
146
142
Returns
147
143
-------
148
144
bytes
149
145
The RLE encoded frame.
150
146
"""
151
147
if ds :
152
- r , c = ds .Rows , ds .Columns
148
+ r = ds .Rows
149
+ c = ds .Columns
153
150
bpp = ds .BitsAllocated
154
151
spp = ds .SamplesPerPixel
155
152
else :
156
- r , c = kwargs ['rows' ], kwargs ['columns' ]
157
- bpp = kwargs ['bits_per_px' ]
158
- spp = kwargs ['samples_per_px' ]
153
+ r = kwargs ['rows' ]
154
+ c = kwargs ['columns' ]
155
+ bpp = kwargs ['bits_allocated' ]
156
+ spp = kwargs ['samples_per_pixel' ]
159
157
160
158
# Validate input
161
159
if spp not in [1 , 3 ]:
162
- src = "(0028,0002) 'Samples per Pixel'" if ds else "'samples_per_px '"
160
+ src = "(0028,0002) 'Samples per Pixel'" if ds else "'samples_per_pixel '"
163
161
raise ValueError (src + " must be 1 or 3" )
164
162
165
163
if bpp not in [8 , 16 , 32 , 64 ]:
166
- src = "(0028,0100) 'Bits Allocated'" if ds else "'bits_per_px '"
164
+ src = "(0028,0100) 'Bits Allocated'" if ds else "'bits_allocated '"
167
165
raise ValueError (src + " must be 8, 16, 32 or 64" )
168
166
169
167
if bpp / 8 * spp > 15 :
@@ -172,7 +170,8 @@ def encode_pixel_data(
172
170
"Standard only allows a maximum of 15 segments"
173
171
)
174
172
175
- if bpp > 8 and byteorder not in ('<' , '>' ):
173
+ byteorder = '<' if bpp == 8 else byteorder
174
+ if byteorder not in ('<' , '>' ):
176
175
raise ValueError (
177
176
"A valid 'byteorder' is required when the number of bits per "
178
177
"pixel is greater than 8"
@@ -236,8 +235,9 @@ def generate_frames(ds: "Dataset", reshape: bool = True) -> "np.ndarray":
236
235
"elements are missing from the dataset: " + ", " .join (missing )
237
236
)
238
237
239
- nr_frames = getattr (ds , "NumberOfFrames" , 1 )
240
- r , c = ds .Rows , ds .Columns
238
+ nr_frames = int (getattr (ds , "NumberOfFrames" , 1 ) or 1 )
239
+ r = ds .Rows
240
+ c = ds .Columns
241
241
bpp = ds .BitsAllocated
242
242
243
243
dtype = pixel_dtype (ds )
0 commit comments