dcmnorm 0.20.0

Fast DICOM toolkit: parse, render, transcode, reformat, de-identify and network (DIMSE) DICOM medical images.
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
//! Raw `openjpeg-sys` FFI encoder for classic JPEG 2000 (`.90`/`.91`).
//!
//! Decode lives in the `dcmnorm-jpeg2000` crate (`openjpeg` module, used by the transfer syntax
//! registry's adapters); this module is the matching encoder, talking to `openjpeg-sys` directly
//! the same way `mpeg.rs` talks to `ffmpeg-next`'s raw `ffi` module.

#[cfg(feature = "jpeg2000-openjpeg-encode")]
mod imp {
    use openjpeg_sys as sys;
    use std::os::raw::c_void;

    /// A growable in-memory buffer that OpenJPEG can write to, seek within, and skip ahead in -
    /// J2K codestream writing isn't purely sequential (e.g. length fields get patched after the
    /// fact), so all four stream callbacks need to behave like a real seekable byte sink, not
    /// just an append-only one.
    struct MemoryTarget {
        buffer: Vec<u8>,
        pos: usize,
    }

    unsafe extern "C" fn write_fn(
        buffer: *mut c_void,
        num_bytes: sys::OPJ_SIZE_T,
        user_data: *mut c_void,
    ) -> sys::OPJ_SIZE_T {
        let target = unsafe { &mut *(user_data as *mut MemoryTarget) };
        let n = num_bytes;
        let src = unsafe { std::slice::from_raw_parts(buffer as *const u8, n) };
        let end = target.pos + n;
        if end > target.buffer.len() {
            target.buffer.resize(end, 0);
        }
        target.buffer[target.pos..end].copy_from_slice(src);
        target.pos = end;
        num_bytes
    }

    unsafe extern "C" fn skip_fn(
        num_bytes: sys::OPJ_OFF_T,
        user_data: *mut c_void,
    ) -> sys::OPJ_OFF_T {
        let target = unsafe { &mut *(user_data as *mut MemoryTarget) };
        if num_bytes < 0 {
            return -1;
        }
        let end = target.pos + num_bytes as usize;
        if end > target.buffer.len() {
            target.buffer.resize(end, 0);
        }
        target.pos = end;
        num_bytes
    }

    unsafe extern "C" fn seek_fn(
        num_bytes: sys::OPJ_OFF_T,
        user_data: *mut c_void,
    ) -> sys::OPJ_BOOL {
        let target = unsafe { &mut *(user_data as *mut MemoryTarget) };
        if num_bytes < 0 {
            return 0;
        }
        let pos = num_bytes as usize;
        if pos > target.buffer.len() {
            target.buffer.resize(pos, 0);
        }
        target.pos = pos;
        1
    }

    unsafe extern "C" fn error_fn(msg: *const std::os::raw::c_char, client_data: *mut c_void) {
        let msg = unsafe { std::ffi::CStr::from_ptr(msg) }.to_string_lossy();
        let errors = unsafe { &mut *(client_data as *mut Vec<String>) };
        errors.push(msg.trim_end().to_owned());
    }

    struct Codec(*mut sys::opj_codec_t);
    impl Drop for Codec {
        fn drop(&mut self) {
            if !self.0.is_null() {
                unsafe { sys::opj_destroy_codec(self.0) };
            }
        }
    }

    struct Stream(*mut sys::opj_stream_t);
    impl Drop for Stream {
        fn drop(&mut self) {
            if !self.0.is_null() {
                unsafe { sys::opj_stream_destroy(self.0) };
            }
        }
    }

    struct Image(*mut sys::opj_image_t);
    impl Drop for Image {
        fn drop(&mut self) {
            if !self.0.is_null() {
                unsafe { sys::opj_image_destroy(self.0) };
            }
        }
    }

    /// Encodes one frame's worth of raw, native-byte-order, interleaved pixel samples to a
    /// classic JPEG 2000 codestream (raw `.j2k`, no JP2 box wrapper - matching what DICOM
    /// PixelData fragments for `.90`/`.91` expect).
    ///
    /// `lossless=true` uses OpenJPEG's defaults, which - per `opj_j2k_setup_encoder`'s own
    /// normalization of a zeroed `tcp_numlayers`/`tcp_rates` - already means a single,
    /// rate-unconstrained layer with the reversible 5/3 wavelet (`irreversible=0`): genuinely
    /// lossless, not merely "high quality". `lossless=false` switches to the irreversible 9/7
    /// wavelet and targets `lossy_compression_ratio` (e.g. `10.0` for 10:1), OpenJPEG's own
    /// convention for `tcp_rates` (original-size-to-compressed-size ratio, not a byte count).
    #[allow(clippy::too_many_arguments)]
    pub fn encode(
        pixel_data: &[u8],
        rows: u32,
        cols: u32,
        samples_per_pixel: u32,
        bits_stored: u32,
        is_signed: bool,
        lossless: bool,
        lossy_compression_ratio: f32,
    ) -> Result<Vec<u8>, String> {
        if samples_per_pixel != 1 && samples_per_pixel != 3 {
            return Err(format!(
                "JPEG2000 encoding only supports 1 or 3 samples per pixel, got {samples_per_pixel}"
            ));
        }
        let bytes_per_sample = if bits_stored > 8 { 2usize } else { 1usize };
        let expected_len =
            rows as usize * cols as usize * samples_per_pixel as usize * bytes_per_sample;
        if pixel_data.len() != expected_len {
            return Err(format!(
                "pixel data length {} does not match expected {expected_len} for \
                 {rows}x{cols}x{samples_per_pixel} at {bytes_per_sample} byte(s)/sample",
                pixel_data.len()
            ));
        }

        let comp_params: Vec<sys::opj_image_cmptparm_t> = (0..samples_per_pixel)
            .map(|_| sys::opj_image_cmptparm_t {
                dx: 1,
                dy: 1,
                w: cols,
                h: rows,
                x0: 0,
                y0: 0,
                prec: bits_stored,
                bpp: bits_stored,
                sgnd: is_signed as sys::OPJ_UINT32,
            })
            .collect();

        let color_space = if samples_per_pixel == 1 {
            sys::OPJ_COLOR_SPACE::OPJ_CLRSPC_GRAY
        } else {
            sys::OPJ_COLOR_SPACE::OPJ_CLRSPC_SRGB
        };

        let image = unsafe {
            sys::opj_image_create(
                samples_per_pixel,
                comp_params.as_ptr() as *mut _,
                color_space,
            )
        };
        if image.is_null() {
            return Err("opj_image_create failed".to_owned());
        }
        let image = Image(image);

        unsafe {
            (*image.0).x0 = 0;
            (*image.0).y0 = 0;
            (*image.0).x1 = cols;
            (*image.0).y1 = rows;

            let comps =
                std::slice::from_raw_parts_mut((*image.0).comps, samples_per_pixel as usize);
            for (c, comp) in comps.iter_mut().enumerate() {
                let data = std::slice::from_raw_parts_mut(comp.data, (rows * cols) as usize);
                for (i, sample) in data.iter_mut().enumerate() {
                    let offset = (i * samples_per_pixel as usize + c) * bytes_per_sample;
                    *sample = if bytes_per_sample == 1 {
                        if is_signed {
                            pixel_data[offset] as i8 as sys::OPJ_INT32
                        } else {
                            pixel_data[offset] as sys::OPJ_INT32
                        }
                    } else {
                        let raw = u16::from_le_bytes([pixel_data[offset], pixel_data[offset + 1]]);
                        if is_signed {
                            raw as i16 as sys::OPJ_INT32
                        } else {
                            raw as sys::OPJ_INT32
                        }
                    };
                }
            }
        }

        let codec = unsafe { sys::opj_create_compress(sys::OPJ_CODEC_FORMAT::OPJ_CODEC_J2K) };
        if codec.is_null() {
            return Err("opj_create_compress failed".to_owned());
        }
        let codec = Codec(codec);
        // Production OpenJPEG used to encode single-threaded; one large frame (a 4096x3328
        // mammogram) now spreads across the shared JPEG 2000 thread budget - see
        // dcmnorm_jpeg2000::threads.
        let threads = dcmnorm_jpeg2000::threads::per_call_threads();
        if threads > 1 && unsafe { sys::opj_has_thread_support() } != 0 {
            unsafe { sys::opj_codec_set_threads(codec.0, threads as i32) };
        }
        let mut errors: Vec<String> = Vec::new();
        unsafe {
            sys::opj_set_error_handler(
                codec.0,
                Some(error_fn),
                &mut errors as *mut Vec<String> as *mut c_void,
            );
        }

        let mut params: sys::opj_cparameters_t = unsafe { std::mem::zeroed() };
        unsafe { sys::opj_set_default_encoder_parameters(&mut params) };
        // The default of 6 wavelet decomposition levels needs the smallest image dimension to
        // be at least 2^5=32 (with the default single-tile layout); DICOM images can be smaller
        // than that (icon/thumbnail frames, tiny secondary captures), so scale it down rather
        // than let OpenJPEG reject an otherwise perfectly encodable small image.
        let smallest_dim = rows.min(cols).max(1);
        params.numresolution = (32 - smallest_dim.leading_zeros()).clamp(1, 6) as i32;
        if !lossless {
            params.irreversible = 1;
            params.cp_disto_alloc = 1;
            params.tcp_numlayers = 1;
            params.tcp_rates[0] = lossy_compression_ratio;
        }

        let setup_ok = unsafe { sys::opj_setup_encoder(codec.0, &mut params, image.0) };
        if setup_ok == 0 {
            return Err(format!("opj_setup_encoder failed: {}", errors.join("; ")));
        }

        let stream = unsafe { sys::opj_stream_create(1024 * 1024, 0) };
        if stream.is_null() {
            return Err("opj_stream_create failed".to_owned());
        }
        let stream = Stream(stream);

        let mut target = Box::new(MemoryTarget {
            buffer: Vec::new(),
            pos: 0,
        });
        unsafe {
            sys::opj_stream_set_write_function(stream.0, Some(write_fn));
            sys::opj_stream_set_skip_function(stream.0, Some(skip_fn));
            sys::opj_stream_set_seek_function(stream.0, Some(seek_fn));
            sys::opj_stream_set_user_data(
                stream.0,
                target.as_mut() as *mut MemoryTarget as *mut c_void,
                None,
            );
        }

        let ok = unsafe {
            sys::opj_start_compress(codec.0, image.0, stream.0) != 0
                && sys::opj_encode(codec.0, stream.0) != 0
                && sys::opj_end_compress(codec.0, stream.0) != 0
        };
        if !ok {
            return Err(format!(
                "OpenJPEG JPEG2000 encode failed: {}",
                errors.join("; ")
            ));
        }

        Ok(target.buffer)
    }
}

#[cfg(not(feature = "jpeg2000-openjpeg-encode"))]
mod imp {
    pub fn encode(
        _pixel_data: &[u8],
        _rows: u32,
        _cols: u32,
        _samples_per_pixel: u32,
        _bits_stored: u32,
        _is_signed: bool,
        _lossless: bool,
        _lossy_compression_ratio: f32,
    ) -> Result<Vec<u8>, String> {
        Err("JPEG2000 OpenJPEG encoding requires the 'jpeg2000-openjpeg-encode' feature".to_owned())
    }
}

pub(super) use imp::encode as encode_jpeg2000_with_openjpeg;

#[cfg(all(test, feature = "jpeg2000-openjpeg-encode"))]
mod tests {
    use super::encode_jpeg2000_with_openjpeg;
    use dcmnorm_core::value::PixelFragmentSequence;
    use dcmnorm_core::{DataElement, PrimitiveValue, VR};
    use dcmnorm_object::{DefaultDicomObject, FileMetaTableBuilder};

    const WIDTH: u32 = 37;
    const HEIGHT: u32 = 29;

    /// Deliberately not a gradient or anything a lossy codec would reconstruct "close enough" -
    /// sharp discontinuities are exactly what would expose a genuinely lossy encode masquerading
    /// as lossless.
    fn pattern_8bit() -> Vec<u8> {
        (0..WIDTH * HEIGHT)
            .map(|i| {
                let (x, y) = (i % WIDTH, i / WIDTH);
                ((x.wrapping_mul(37) ^ y.wrapping_mul(101)) % 256) as u8
            })
            .collect()
    }

    fn pattern_16bit() -> Vec<u8> {
        (0..WIDTH * HEIGHT)
            .flat_map(|i| {
                let (x, y) = (i % WIDTH, i / WIDTH);
                let v = ((x.wrapping_mul(733) ^ y.wrapping_mul(1301)) % 4096) as u16;
                v.to_le_bytes()
            })
            .collect()
    }

    fn decode_via_full_pipeline(ts_uid: &str, codestream: Vec<u8>, bits_stored: u16) -> Vec<u8> {
        let meta = FileMetaTableBuilder::new()
            .transfer_syntax(ts_uid)
            .build()
            .unwrap();
        let mut object = DefaultDicomObject::new_empty_with_meta(meta);
        for element in [
            DataElement::new(
                dcmnorm_dictionary::tags::ROWS,
                VR::US,
                PrimitiveValue::from(HEIGHT as u16),
            ),
            DataElement::new(
                dcmnorm_dictionary::tags::COLUMNS,
                VR::US,
                PrimitiveValue::from(WIDTH as u16),
            ),
            DataElement::new(
                dcmnorm_dictionary::tags::SAMPLES_PER_PIXEL,
                VR::US,
                PrimitiveValue::from(1u16),
            ),
            DataElement::new(
                dcmnorm_dictionary::tags::BITS_ALLOCATED,
                VR::US,
                PrimitiveValue::from(if bits_stored > 8 { 16u16 } else { 8u16 }),
            ),
            DataElement::new(
                dcmnorm_dictionary::tags::BITS_STORED,
                VR::US,
                PrimitiveValue::from(bits_stored),
            ),
            DataElement::new(
                dcmnorm_dictionary::tags::HIGH_BIT,
                VR::US,
                PrimitiveValue::from(bits_stored - 1),
            ),
            DataElement::new(
                dcmnorm_dictionary::tags::PIXEL_REPRESENTATION,
                VR::US,
                PrimitiveValue::from(0u16),
            ),
            DataElement::new(
                dcmnorm_dictionary::tags::PHOTOMETRIC_INTERPRETATION,
                VR::CS,
                PrimitiveValue::from("MONOCHROME2".to_owned()),
            ),
            DataElement::new(
                dcmnorm_dictionary::tags::PIXEL_DATA,
                VR::OB,
                PixelFragmentSequence::new(vec![0], vec![codestream]),
            ),
        ] {
            object.put(element);
        }

        let transcoded = super::super::transcode_dcmnorm_object(
            &object,
            dcmnorm_dictionary::uids::EXPLICIT_VR_LITTLE_ENDIAN,
        )
        .expect("decode of our own encoded codestream should succeed");
        transcoded
            .element(dcmnorm_dictionary::tags::PIXEL_DATA)
            .unwrap()
            .to_bytes()
            .unwrap()
            .to_vec()
    }

    /// Encodes with our raw-FFI OpenJPEG encoder and decodes with the *already-verified*
    /// OpenJPEG-backed `Jpeg2000Adapter` (proven correct in earlier work this session) - an
    /// exact byte match proves the encoder is genuinely lossless, not merely "OpenJPEG didn't
    /// error".
    #[test]
    fn lossless_8bit_encode_round_trips_exactly_via_full_decode_pipeline() {
        let original = pattern_8bit();
        let codestream =
            encode_jpeg2000_with_openjpeg(&original, HEIGHT, WIDTH, 1, 8, false, true, 1.0)
                .expect("lossless encode should succeed");
        let decoded = decode_via_full_pipeline("1.2.840.10008.1.2.4.90", codestream, 8);
        assert_eq!(decoded, original);
    }

    #[test]
    fn lossless_16bit_encode_round_trips_exactly_via_full_decode_pipeline() {
        let original = pattern_16bit();
        let codestream =
            encode_jpeg2000_with_openjpeg(&original, HEIGHT, WIDTH, 1, 12, false, true, 1.0)
                .expect("lossless encode should succeed");
        let decoded = decode_via_full_pipeline("1.2.840.10008.1.2.4.90", codestream, 12);
        assert_eq!(decoded, original);
    }

    /// Lossy encoding must actually compress (smaller than the lossless codestream for the same
    /// image) and must NOT be byte-identical after round-tripping - if it were, the "lossy" path
    /// would secretly be lossless, which would be its own (differently wrong) bug.
    #[test]
    fn lossy_encode_is_smaller_and_not_byte_identical() {
        let original = pattern_8bit();
        let lossless =
            encode_jpeg2000_with_openjpeg(&original, HEIGHT, WIDTH, 1, 8, false, true, 1.0)
                .expect("lossless encode should succeed");
        let lossy =
            encode_jpeg2000_with_openjpeg(&original, HEIGHT, WIDTH, 1, 8, false, false, 8.0)
                .expect("lossy encode should succeed");

        assert!(
            lossy.len() < lossless.len(),
            "lossy ({} bytes) should be smaller than lossless ({} bytes)",
            lossy.len(),
            lossless.len()
        );

        let decoded = decode_via_full_pipeline("1.2.840.10008.1.2.4.91", lossy, 8);
        assert_eq!(decoded.len(), original.len());
        assert_ne!(
            decoded, original,
            "lossy output should not be byte-identical to the source"
        );
    }

    #[test]
    fn rejects_pixel_data_length_mismatch() {
        let result =
            encode_jpeg2000_with_openjpeg(&[0u8; 4], HEIGHT, WIDTH, 1, 8, false, true, 1.0);
        assert!(result.is_err(), "expected an error, got {result:?}");
    }
}