dcm4rs 0.2.0

Pure-Rust DICOM data model, data dictionary and file I/O
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Pixel data decoding: descriptors, frame extraction and codecs.
//!
//! The descriptor is read from the group 0028 attributes (rows, columns, samples, bit depth,
//! representation, planar configuration and photometric interpretation). Frames are extracted
//! from native Pixel Data by their fixed size, or from encapsulated Pixel Data by the Basic
//! Offset Table (one fragment per frame when the table is empty).
//!
//! RLE Lossless (PS3.5 Annex G) and JPEG Lossless (Process 14) are decoded here. JPEG
//! (eight-bit baseline and progressive, and the 12-bit extended process), JPEG-LS, JPEG
//! 2000/HTJ2K and JPEG XL are feature-gated additions; MPEG reports
//! [`Error::UnsupportedPixelData`].

use std::fmt;

use crate::dataset::DataSet;
use crate::dictionary::tags;
use crate::error::{Error, Result};
use crate::file::DicomFile;
use crate::transfer_syntax::TransferSyntax;

#[cfg(feature = "pixel-j2k")]
mod j2k;
#[cfg(feature = "pixel-jpeg")]
mod jpeg;
mod jpeg_lossless;
#[cfg(feature = "pixel-jpegls")]
mod jpegls;
#[cfg(feature = "pixel-jxl")]
mod jxl;
mod rle;

/// How a pixel sample is interpreted: unsigned or two's-complement signed
/// (`(0028,0103) Pixel Representation`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PixelRepresentation {
    /// Unsigned integer samples (value 0).
    Unsigned,
    /// Signed integer samples (value 1).
    Signed,
}

/// How samples are arranged within a frame (`(0028,0006) Planar Configuration`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PlanarConfiguration {
    /// A single sample per pixel; the attribute is not present.
    None,
    /// Sample values are interleaved per pixel (value 0).
    Interleaved,
    /// Each sample plane is stored contiguously (value 1).
    Planar,
}

/// The photometric interpretation of the pixel data (`(0028,0004)`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PhotometricInterpretation {
    /// `MONOCHROME1`: minimum value is white.
    Monochrome1,
    /// `MONOCHROME2`: minimum value is black.
    Monochrome2,
    /// `PALETTE COLOR`: samples are indexes into a color lookup table.
    PaletteColor,
    /// `RGB`: red, green and blue samples.
    Rgb,
    /// `YBR_FULL`: full-range luminance and chrominance.
    YbrFull,
    /// `YBR_FULL_422`: horizontally subsampled chrominance.
    YbrFull422,
    /// `YBR_PARTIAL_422`: partial-range, horizontally subsampled chrominance.
    YbrPartial422,
    /// `YBR_PARTIAL_420`: partial-range, horizontally and vertically subsampled chrominance.
    YbrPartial420,
    /// `YBR_ICT`: irreversible color transform (JPEG 2000).
    YbrIct,
    /// `YBR_RCT`: reversible color transform (JPEG 2000).
    YbrRct,
    /// `HSV`: hue, saturation and value.
    Hsv,
    /// `ARGB`: alpha, red, green and blue.
    Argb,
    /// `CMYK`: cyan, magenta, yellow and black.
    Cmyk,
    /// A value this crate does not interpret; the original text is retained.
    Unknown(String),
}

impl PhotometricInterpretation {
    fn from_dicom(text: &str) -> Self {
        match text.trim() {
            "MONOCHROME1" => Self::Monochrome1,
            "MONOCHROME2" => Self::Monochrome2,
            "PALETTE COLOR" => Self::PaletteColor,
            "RGB" => Self::Rgb,
            "YBR_FULL" => Self::YbrFull,
            "YBR_FULL_422" => Self::YbrFull422,
            "YBR_PARTIAL_422" => Self::YbrPartial422,
            "YBR_PARTIAL_420" => Self::YbrPartial420,
            "YBR_ICT" => Self::YbrIct,
            "YBR_RCT" => Self::YbrRct,
            "HSV" => Self::Hsv,
            "ARGB" => Self::Argb,
            "CMYK" => Self::Cmyk,
            other => Self::Unknown(other.to_owned()),
        }
    }
}

impl fmt::Display for PhotometricInterpretation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Monochrome1 => f.write_str("MONOCHROME1"),
            Self::Monochrome2 => f.write_str("MONOCHROME2"),
            Self::PaletteColor => f.write_str("PALETTE COLOR"),
            Self::Rgb => f.write_str("RGB"),
            Self::YbrFull => f.write_str("YBR_FULL"),
            Self::YbrFull422 => f.write_str("YBR_FULL_422"),
            Self::YbrPartial422 => f.write_str("YBR_PARTIAL_422"),
            Self::YbrPartial420 => f.write_str("YBR_PARTIAL_420"),
            Self::YbrIct => f.write_str("YBR_ICT"),
            Self::YbrRct => f.write_str("YBR_RCT"),
            Self::Hsv => f.write_str("HSV"),
            Self::Argb => f.write_str("ARGB"),
            Self::Cmyk => f.write_str("CMYK"),
            Self::Unknown(text) => f.write_str(text),
        }
    }
}

/// The attributes that describe one pixel data frame (PS3.3 C.7.6.3.1).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PixelDescriptor {
    /// Rows in the image (`(0028,0010)`).
    pub rows: u16,
    /// Columns in the image (`(0028,0011)`).
    pub columns: u16,
    /// Number of samples per pixel (`(0028,0002)`).
    pub samples_per_pixel: u16,
    /// Number of bits allocated per sample (`(0028,0100)`).
    pub bits_allocated: u16,
    /// Number of significant bits per sample (`(0028,0101)`).
    pub bits_stored: u16,
    /// Position of the most significant bit (`(0028,0102)`).
    pub high_bit: u16,
    /// Sample signedness (`(0028,0103)`).
    pub pixel_representation: PixelRepresentation,
    /// Sample arrangement (`(0028,0006)`); [`PlanarConfiguration::None`] for one sample.
    pub planar_configuration: PlanarConfiguration,
    /// Photometric interpretation (`(0028,0004)`).
    pub photometric: PhotometricInterpretation,
    /// Number of frames (`(0028,0008)`); 1 when the attribute is absent.
    pub number_of_frames: u32,
}

impl PixelDescriptor {
    /// Read the descriptor from a data set, validating the required attributes.
    pub fn from_dataset(set: &DataSet) -> Result<Self> {
        let rows = set.u16(tags::ROWS)?;
        let columns = set.u16(tags::COLUMNS)?;
        let samples_per_pixel = set.u16(tags::SAMPLES_PER_PIXEL)?;
        let bits_allocated = set.u16(tags::BITS_ALLOCATED)?;
        let bits_stored = set.u16(tags::BITS_STORED)?;
        let high_bit = set.u16(tags::HIGH_BIT)?;
        let pixel_representation = match set.u16(tags::PIXEL_REPRESENTATION)? {
            0 => PixelRepresentation::Unsigned,
            1 => PixelRepresentation::Signed,
            value => {
                return Err(Error::InvalidPixelData(format!(
                    "invalid Pixel Representation: {value}"
                )));
            }
        };
        let planar_configuration = if samples_per_pixel == 1 {
            PlanarConfiguration::None
        } else if set.contains(tags::PLANAR_CONFIGURATION) {
            match set.u16(tags::PLANAR_CONFIGURATION)? {
                0 => PlanarConfiguration::Interleaved,
                1 => PlanarConfiguration::Planar,
                value => {
                    return Err(Error::InvalidPixelData(format!(
                        "invalid Planar Configuration: {value}"
                    )));
                }
            }
        } else {
            PlanarConfiguration::Interleaved
        };
        let photometric_text = set.string(tags::PHOTOMETRIC_INTERPRETATION)?;
        let photometric = PhotometricInterpretation::from_dicom(&photometric_text);
        let number_of_frames = match set.get(tags::NUMBER_OF_FRAMES) {
            Some(element) => {
                let text = element.string()?.trim();
                text.parse::<u32>().map_err(|_| {
                    Error::InvalidPixelData(format!("invalid Number of Frames: {text:?}"))
                })?
            }
            None => 1,
        };

        if rows == 0 || columns == 0 {
            return Err(Error::InvalidPixelData(
                "Rows and Columns must be non-zero".to_owned(),
            ));
        }
        if samples_per_pixel == 0 || samples_per_pixel > 4 {
            return Err(Error::InvalidPixelData(format!(
                "invalid Samples per Pixel: {samples_per_pixel}"
            )));
        }
        if bits_allocated == 0 || bits_allocated > 32 {
            return Err(Error::InvalidPixelData(format!(
                "invalid Bits Allocated: {bits_allocated}"
            )));
        }
        if bits_stored == 0 || bits_stored > bits_allocated {
            return Err(Error::InvalidPixelData(format!(
                "invalid Bits Stored: {bits_stored} for Bits Allocated {bits_allocated}"
            )));
        }
        if high_bit >= bits_allocated {
            return Err(Error::InvalidPixelData(format!(
                "invalid High Bit: {high_bit} for Bits Allocated {bits_allocated}"
            )));
        }
        if number_of_frames == 0 {
            return Err(Error::InvalidPixelData(
                "Number of Frames must be non-zero".to_owned(),
            ));
        }

        Ok(Self {
            rows,
            columns,
            samples_per_pixel,
            bits_allocated,
            bits_stored,
            high_bit,
            pixel_representation,
            planar_configuration,
            photometric,
            number_of_frames,
        })
    }

    /// Bytes per sample in the stored pixel data.
    pub fn bytes_per_sample(&self) -> usize {
        usize::from(self.bits_allocated).div_ceil(8)
    }

    /// Bytes in one native frame, including the packed layout when bits are allocated below 8.
    pub fn frame_size(&self) -> usize {
        let pixels = usize::from(self.rows)
            * usize::from(self.columns)
            * usize::from(self.samples_per_pixel);
        if self.bits_allocated < 8 {
            pixels.div_ceil(8)
        } else {
            pixels * self.bytes_per_sample()
        }
    }
}

impl DataSet {
    /// Read the pixel descriptor from this data set.
    pub fn pixel_descriptor(&self) -> Result<PixelDescriptor> {
        PixelDescriptor::from_dataset(self)
    }

    /// Split encapsulated Pixel Data into frames.
    ///
    /// The first fragment is the Basic Offset Table. An empty table means one frame per
    /// fragment; otherwise its 32-bit offsets locate each frame within the concatenated
    /// fragment data.
    pub fn encapsulated_frames(&self) -> Result<Vec<Vec<u8>>> {
        let element = self.element(tags::PIXEL_DATA)?;
        let Some(fragments) = element.value.as_fragments() else {
            return Err(Error::InvalidPixelData(
                "Pixel Data is not encapsulated".to_owned(),
            ));
        };
        if fragments.is_empty() {
            return Ok(Vec::new());
        }
        let basic_offset_table = &fragments[0];
        if basic_offset_table.is_empty() {
            return self.frameless_encapsulated_frames(&fragments[1..]);
        }
        if basic_offset_table.len() % 4 != 0 {
            return Err(Error::InvalidPixelData(format!(
                "Basic Offset Table length {} is not a multiple of 4",
                basic_offset_table.len()
            )));
        }
        let offsets: Vec<usize> = basic_offset_table
            .chunks_exact(4)
            .map(|chunk| u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]) as usize)
            .collect();
        // The offsets are measured from the first fragment's item tag, so each fragment
        // contributes its value plus the 8-byte item tag and length. Frames may span several
        // fragments; the table only marks the frame boundaries. This mirrors pydicom's
        // `generate_fragmented_frames`.
        let fragments = &fragments[1..];
        let final_index = offsets.len() - 1;
        let mut frames = Vec::new();
        let mut frame = Vec::new();
        let mut current = 0usize;
        let mut stream_offset = 0usize;
        for fragment in fragments {
            if current == final_index {
                frame.extend_from_slice(fragment);
                stream_offset += fragment.len() + 8;
                continue;
            }
            if stream_offset < offsets[current + 1] {
                frame.extend_from_slice(fragment);
            } else {
                frames.push(std::mem::take(&mut frame));
                current += 1;
                frame = fragment.clone();
            }
            stream_offset += fragment.len() + 8;
        }
        if !frame.is_empty() || frames.is_empty() {
            frames.push(frame);
        }
        Ok(frames)
    }

    /// Split fragments into frames when the Basic Offset Table is empty.
    ///
    /// Mirrors pydicom's `generate_fragmented_frames`: a single fragment is one frame,
    /// `Number of Frames` equal to the fragment count means one fragment per frame, one frame
    /// means every fragment belongs to it, and otherwise a JPEG end-of-image marker (`FF D9`)
    /// near the end of a fragment closes a frame.
    fn frameless_encapsulated_frames(&self, fragments: &[Vec<u8>]) -> Result<Vec<Vec<u8>>> {
        if fragments.is_empty() {
            return Ok(Vec::new());
        }
        let number_of_frames = match self.get(tags::NUMBER_OF_FRAMES) {
            Some(element) => {
                let text = element.string()?.trim();
                text.parse::<u32>().map_err(|_| {
                    Error::InvalidPixelData(format!("invalid Number of Frames: {text:?}"))
                })?
            }
            None => 1,
        }
        .max(1) as usize;
        if fragments.len() == 1 || number_of_frames == 1 {
            return Ok(vec![fragments.concat()]);
        }
        if fragments.len() == number_of_frames {
            return Ok(fragments.to_vec());
        }
        if fragments.len() < number_of_frames {
            return Err(Error::InvalidPixelData(format!(
                "encapsulated Pixel Data has {} fragments for {number_of_frames} frames",
                fragments.len()
            )));
        }
        let mut frames = Vec::new();
        let mut frame = Vec::new();
        for fragment in fragments {
            frame.extend_from_slice(fragment);
            let tail = &fragment[fragment.len().saturating_sub(10)..];
            if tail.windows(2).any(|pair| pair == [0xFF, 0xD9]) {
                frames.push(std::mem::take(&mut frame));
            }
        }
        if !frame.is_empty() {
            frames.push(frame);
        }
        Ok(frames)
    }
}

impl DicomFile {
    /// The pixel descriptor of this file's data set.
    pub fn pixel_descriptor(&self) -> Result<PixelDescriptor> {
        self.dataset.pixel_descriptor()
    }

    /// The encoded frames of this file, without decompression.
    ///
    /// Native Pixel Data is split by [`PixelDescriptor::frame_size`]; encapsulated Pixel Data is
    /// split by the Basic Offset Table. The transfer syntax is taken from the file meta
    /// information.
    pub fn pixel_frames(&self) -> Result<Vec<Vec<u8>>> {
        let syntax = self.transfer_syntax()?;
        let descriptor = self.dataset.pixel_descriptor()?;
        if syntax.is_encapsulated() {
            return self.dataset.encapsulated_frames();
        }
        let element = self.dataset.element(tags::PIXEL_DATA)?;
        let Some(bytes) = element.value.as_bytes() else {
            return Err(Error::InvalidPixelData(
                "Pixel Data is not a byte value".to_owned(),
            ));
        };
        let frame_size = descriptor.frame_size();
        if frame_size == 0 {
            return Err(Error::InvalidPixelData("frame size is zero".to_owned()));
        }
        let expected = frame_size
            .checked_mul(descriptor.number_of_frames as usize)
            .ok_or_else(|| Error::InvalidPixelData("pixel data size overflows".to_owned()))?;
        if bytes.len() < expected {
            return Err(Error::InvalidPixelData(format!(
                "Pixel Data has {} bytes, {expected} required for {} frame(s)",
                bytes.len(),
                descriptor.number_of_frames
            )));
        }
        Ok((0..descriptor.number_of_frames as usize)
            .map(|index| bytes[index * frame_size..(index + 1) * frame_size].to_vec())
            .collect())
    }

    /// Decode one frame (zero-based).
    ///
    /// Native transfer syntaxes return the stored bytes. Encapsulated transfer syntaxes are
    /// decoded by their codec; RLE Lossless is supported, other codecs report
    /// [`Error::UnsupportedPixelData`].
    pub fn decode_frame(&self, index: usize) -> Result<Vec<u8>> {
        let descriptor = self.dataset.pixel_descriptor()?;
        let syntax = self.transfer_syntax()?;
        let frames = self.pixel_frames()?;
        let frame = frames.get(index).ok_or_else(|| {
            Error::InvalidPixelData(format!(
                "frame index {index} out of range (have {})",
                frames.len()
            ))
        })?;
        decode_frame_bytes(syntax, &descriptor, frame)
    }

    /// Decode every frame of this file.
    pub fn decode_pixel_data(&self) -> Result<Vec<Vec<u8>>> {
        let descriptor = self.dataset.pixel_descriptor()?;
        let syntax = self.transfer_syntax()?;
        self.pixel_frames()?
            .iter()
            .map(|frame| decode_frame_bytes(syntax, &descriptor, frame))
            .collect()
    }
}

fn decode_frame_bytes(
    syntax: TransferSyntax,
    descriptor: &PixelDescriptor,
    frame: &[u8],
) -> Result<Vec<u8>> {
    if let TransferSyntax::Encapsulated(uid) = syntax {
        if uid == "1.2.840.10008.1.2.5" {
            return rle::decode(frame, descriptor);
        }
        if is_jpeg_lossless_uid(uid) {
            return jpeg_lossless::decode(frame, descriptor);
        }
        #[cfg(feature = "pixel-jpeg")]
        if is_jpeg_uid(uid) {
            return jpeg::decode(frame, descriptor);
        }
        #[cfg(feature = "pixel-jpegls")]
        if is_jpegls_uid(uid) {
            return jpegls::decode(frame, descriptor);
        }
        #[cfg(feature = "pixel-j2k")]
        if is_j2k_uid(uid) {
            return j2k::decode(frame, descriptor);
        }
        #[cfg(feature = "pixel-jxl")]
        if is_jxl_uid(uid) {
            return jxl::decode(frame, descriptor);
        }
        return Err(Error::UnsupportedPixelData(format!(
            "transfer syntax {uid}"
        )));
    }
    Ok(frame.to_vec())
}

/// JPEG Lossless (Process 14) and Process 14 Selection Value 1 (`.57`, `.70`).
fn is_jpeg_lossless_uid(uid: &str) -> bool {
    matches!(uid, "1.2.840.10008.1.2.4.57" | "1.2.840.10008.1.2.4.70")
}

/// JPEG Baseline (Process 1), Extended (Process 2/4), Spectral Selection and Full Progression.
///
/// JPEG Lossless is decoded by [`jpeg_lossless`] and JPEG-LS is a different codec.
#[cfg(feature = "pixel-jpeg")]
fn is_jpeg_uid(uid: &str) -> bool {
    matches!(
        uid,
        "1.2.840.10008.1.2.4.50"
            | "1.2.840.10008.1.2.4.51"
            | "1.2.840.10008.1.2.4.52"
            | "1.2.840.10008.1.2.4.53"
            | "1.2.840.10008.1.2.4.54"
    )
}

/// JPEG-LS Lossless (`.80`) and Near-lossless (`.81`).
#[cfg(feature = "pixel-jpegls")]
fn is_jpegls_uid(uid: &str) -> bool {
    matches!(uid, "1.2.840.10008.1.2.4.80" | "1.2.840.10008.1.2.4.81")
}

/// JPEG XL Lossless (`.110`), JPEG Recompression (`.111`) and Lossy (`.112`).
#[cfg(feature = "pixel-jxl")]
fn is_jxl_uid(uid: &str) -> bool {
    matches!(
        uid,
        "1.2.840.10008.1.2.4.110" | "1.2.840.10008.1.2.4.111" | "1.2.840.10008.1.2.4.112"
    )
}

/// JPEG 2000 (`.90`, `.91`) and HTJ2K (`.201`–`.204`).
#[cfg(feature = "pixel-j2k")]
fn is_j2k_uid(uid: &str) -> bool {
    matches!(
        uid,
        "1.2.840.10008.1.2.4.90"
            | "1.2.840.10008.1.2.4.91"
            | "1.2.840.10008.1.2.4.201"
            | "1.2.840.10008.1.2.4.202"
            | "1.2.840.10008.1.2.4.203"
            | "1.2.840.10008.1.2.4.204"
    )
}