|
| 1 | +//! Support for JPEG-LS image decoding. |
| 2 | +
|
| 3 | +use charls::CharLS; |
| 4 | +use dicom_encoding::adapters::{decode_error, DecodeResult, PixelDataObject, PixelDataReader}; |
| 5 | +use dicom_encoding::snafu::prelude::*; |
| 6 | +use std::borrow::Cow; |
| 7 | + |
| 8 | +/// Pixel data adapter for JPEG-LS transfer syntax. |
| 9 | +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 10 | +pub struct JpegLSAdapter; |
| 11 | + |
| 12 | +impl PixelDataReader for JpegLSAdapter { |
| 13 | + /// Decode a single frame in JPEG-LS from a DICOM object. |
| 14 | + fn decode_frame( |
| 15 | + &self, |
| 16 | + src: &dyn PixelDataObject, |
| 17 | + frame: u32, |
| 18 | + dst: &mut Vec<u8>, |
| 19 | + ) -> DecodeResult<()> { |
| 20 | + let bits_allocated = src |
| 21 | + .bits_allocated() |
| 22 | + .context(decode_error::MissingAttributeSnafu { |
| 23 | + name: "BitsAllocated", |
| 24 | + })?; |
| 25 | + |
| 26 | + ensure_whatever!( |
| 27 | + bits_allocated == 8 || bits_allocated == 16, |
| 28 | + "BitsAllocated other than 8 or 16 is not supported" |
| 29 | + ); |
| 30 | + |
| 31 | + let nr_frames = src.number_of_frames().unwrap_or(1) as usize; |
| 32 | + |
| 33 | + ensure!( |
| 34 | + nr_frames > frame as usize, |
| 35 | + decode_error::FrameRangeOutOfBoundsSnafu |
| 36 | + ); |
| 37 | + |
| 38 | + let raw = src |
| 39 | + .raw_pixel_data() |
| 40 | + .whatever_context("Expected to have raw pixel data available")?; |
| 41 | + |
| 42 | + let frame_data = if raw.fragments.len() == 1 || raw.fragments.len() == nr_frames { |
| 43 | + // assuming 1:1 frame-to-fragment mapping |
| 44 | + Cow::Borrowed( |
| 45 | + raw.fragments |
| 46 | + .get(frame as usize) |
| 47 | + .with_whatever_context(|| { |
| 48 | + format!("Missing fragment #{} for the frame requested", frame) |
| 49 | + })?, |
| 50 | + ) |
| 51 | + } else { |
| 52 | + // Some embedded JPEGs might span multiple fragments. |
| 53 | + // In this case we look up the basic offset table |
| 54 | + // and gather all of the frame's fragments in a single vector. |
| 55 | + // Note: not the most efficient way to do this, |
| 56 | + // consider optimizing later with byte chunk readers |
| 57 | + let base_offset = raw.offset_table.get(frame as usize).copied(); |
| 58 | + let base_offset = if frame == 0 { |
| 59 | + base_offset.unwrap_or(0) as usize |
| 60 | + } else { |
| 61 | + base_offset |
| 62 | + .with_whatever_context(|| format!("Missing offset for frame #{}", frame))? |
| 63 | + as usize |
| 64 | + }; |
| 65 | + let next_offset = raw.offset_table.get(frame as usize + 1); |
| 66 | + |
| 67 | + let mut offset = 0; |
| 68 | + let mut fragments = Vec::new(); |
| 69 | + for fragment in &raw.fragments { |
| 70 | + // include it |
| 71 | + if offset >= base_offset { |
| 72 | + fragments.extend_from_slice(fragment); |
| 73 | + } |
| 74 | + offset += fragment.len() + 8; |
| 75 | + if let Some(&next_offset) = next_offset { |
| 76 | + if offset >= next_offset as usize { |
| 77 | + // next fragment is for the next frame |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + Cow::Owned(fragments) |
| 84 | + }; |
| 85 | + |
| 86 | + let mut decoded = CharLS::default() |
| 87 | + .decode(&frame_data) |
| 88 | + .map_err(|error| error.to_string()) |
| 89 | + .with_whatever_context(|error| error.to_string())?; |
| 90 | + |
| 91 | + dst.append(&mut decoded); |
| 92 | + |
| 93 | + Ok(()) |
| 94 | + } |
| 95 | +} |
0 commit comments