dicom_core/value/
fragments.rs

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
//! Helper module for handling pixel encapsulation into fragments
use crate::value::{InMemFragment, PixelFragmentSequence, C};

/// Represents the fragments of a single frame.
///
/// A [`PixelFragmentSequence`] can be generated from a list of [`Fragments`].
/// In case of multi-frame, a list of frames composed by 1 fragment is expected.
///
/// The frames can be independently processed, so parallel execution is possible.
///
/// # Example
/// ```
/// use dicom_core::{DataElement, Tag};
/// use dicom_core::header::EmptyObject;
/// use dicom_core::value::Value::PixelSequence;
/// use dicom_core::value::fragments::Fragments;
/// use dicom_core::value::InMemFragment;
/// use dicom_core::VR::OB;
///
/// // Frames are represented as Vec<Vec<u8>>
/// // Single 512x512 frame
/// let frames = vec![vec![0; 262144]];
/// let fragments = frames
///     .into_iter()
///     .map(|frame| Fragments::new(frame, 0))
///     .collect::<Vec<Fragments>>();
///
/// let element = DataElement::new(
///     Tag(0x7FE0, 0x0008),
///     OB,
///     PixelSequence::<EmptyObject, InMemFragment>(fragments.into())
/// );
/// ```
///
/// From this last example, it is possible to extend it to implement a pipeline, and even use rayon
/// for parallel processing of the frames.
#[derive(Debug)]
pub struct Fragments {
    fragments: Vec<InMemFragment>,
}

impl Fragments {
    pub fn new(data: Vec<u8>, fragment_size: u32) -> Self {
        let fragment_size: u32 = if fragment_size == 0 {
            data.len() as u32
        } else {
            fragment_size
        };

        let fragment_size = if fragment_size % 2 == 0 {
            fragment_size
        } else {
            fragment_size + 1
        };

        let number_of_fragments = (data.len() as f32 / fragment_size as f32).ceil() as u32;

        // Calculate the encapsulated size. If necessary pad the vector with zeroes so all the
        // chunks have the same fragment_size
        let mut data = data;
        let encapsulated_size = (fragment_size * number_of_fragments) as usize;
        if encapsulated_size > data.len() {
            data.resize(encapsulated_size, 0);
        }

        let fragments = data
            .chunks_exact(fragment_size as usize)
            .map(|fragment| fragment.to_vec())
            .collect::<Vec<InMemFragment>>();

        Fragments { fragments }
    }

    pub fn is_empty(&self) -> bool {
        self.fragments.len() == 0
    }

    pub fn is_multiframe(&self) -> bool {
        self.fragments.len() > 1
    }

    pub fn len(&self) -> u32 {
        self.fragments
            .iter()
            .fold(0u32, |acc, fragment| acc + fragment.len() as u32 + 8u32)
    }
}

impl From<Vec<Fragments>> for PixelFragmentSequence<InMemFragment> {
    fn from(value: Vec<Fragments>) -> Self {
        let mut offset_table = C::with_capacity(value.len() + 1);
        offset_table.push(0u32);
        let mut current_offset = 0u32;

        let mut fragments = Vec::new();
        let is_multiframe = value.len() > 1;
        let last_frame = value.len() - 1;

        for (index, mut frame) in value.into_iter().enumerate() {
            if frame.is_multiframe() && is_multiframe {
                panic!("More than 1 fragment per frame is invalid for multi frame pixel data");
            }

            if index < last_frame {
                let offset = frame.len();
                offset_table.push(current_offset + offset);
                current_offset += offset;
            }

            fragments.append(&mut frame.fragments);
        }

        PixelFragmentSequence {
            offset_table,
            fragments: C::from_vec(fragments),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::value::fragments::Fragments;
    use crate::value::{InMemFragment, PixelFragmentSequence};

    #[test]
    fn test_fragment_frame() {
        let fragment = Fragments::new(vec![150, 164, 200], 0);
        assert_eq!(fragment.fragments.len(), 1, "1 fragment should be present");
        assert_eq!(
            fragment.fragments[0].len(),
            4,
            "The fragment size should be 4"
        );
        assert_eq!(
            fragment.fragments[0],
            vec![150, 164, 200, 0],
            "The data should be 0 padded"
        );

        let fragment = Fragments::new(vec![150, 164, 200, 222], 4);
        assert_eq!(fragment.fragments.len(), 1, "1 fragment should be present");
        assert_eq!(
            fragment.fragments[0].len(),
            4,
            "The fragment size should be 4"
        );
        assert_eq!(
            fragment.fragments[0],
            vec![150, 164, 200, 222],
            "The data should be what was sent"
        );

        let fragment = Fragments::new(vec![150, 164, 200, 222], 2);
        assert_eq!(fragment.fragments.len(), 2, "2 fragments should be present");
        assert_eq!(fragment.fragments[0].len(), 2);
        assert_eq!(fragment.fragments[1].len(), 2);
        assert_eq!(fragment.fragments[0], vec![150, 164]);
        assert_eq!(fragment.fragments[1], vec![200, 222]);

        let fragment = Fragments::new(vec![150, 164, 200], 1);
        assert_eq!(
            fragment.fragments.len(),
            2,
            "2 fragments should be present as fragment_size < 2"
        );
        assert_eq!(fragment.fragments[0].len(), 2);
        assert_eq!(fragment.fragments[0], vec![150, 164]);
        assert_eq!(fragment.fragments[1].len(), 2);
        assert_eq!(fragment.fragments[1], vec![200, 0]);

        let fragment = Fragments::new(vec![150, 164, 200, 222], 1);
        assert_eq!(
            fragment.fragments.len(),
            2,
            "2 fragments should be present as fragment_size < 2"
        );
        assert_eq!(fragment.fragments[0].len(), 2);
        assert_eq!(fragment.fragments[0], vec![150, 164]);
        assert_eq!(fragment.fragments[1].len(), 2);
        assert_eq!(fragment.fragments[1], vec![200, 222]);
    }

    #[test]
    fn test_bot_single_fragment_generation() {
        let data = vec![Fragments::new(vec![0u8; 2], 2)];
        let fragment_sequence: PixelFragmentSequence<InMemFragment> = data.into();
        assert_eq!(fragment_sequence.offset_table.len(), 1);
        assert_eq!(fragment_sequence.offset_table[0], 0);
    }

    #[test]
    fn test_bot_multi_fragments_generation() {
        let data = vec![Fragments::new(vec![0u8; 4], 2)];
        let fragment_sequence: PixelFragmentSequence<InMemFragment> = data.into();
        assert_eq!(fragment_sequence.offset_table.len(), 1);
        assert_eq!(fragment_sequence.offset_table[0], 0);
    }

    #[test]
    fn test_bot_multi_frame_generation() {
        let data = vec![
            Fragments::new(vec![0u8; 4], 0),
            Fragments::new(vec![1u8; 6], 0),
        ];
        let fragment_sequence: PixelFragmentSequence<InMemFragment> = data.into();
        assert_eq!(fragment_sequence.offset_table.len(), 2);
        assert_eq!(fragment_sequence.offset_table[0], 0);
        assert_eq!(fragment_sequence.offset_table[1], 12); // 8 separator bytes + 4 data bytes
    }
}