-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add support for deflate writing
* Note, not all paths lead to writing, for example using `InMemDicomObject.read_dataset_with_dict` would bypass deflate. * No support for writing right now
- Loading branch information
1 parent
a633f1a
commit a0c9f47
Showing
10 changed files
with
159 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
use std::{io::BufReader, fs::File}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#[derive(debug)] | ||
pub struct EncapsulatedAdapter; | ||
|
||
pub const ENCAPSULATED_UNCOMPRESSED_EXPLICIT_VR_LITTLE_ENDIAN: TransferSyntax<EncapsulatedAdapter> = TransferSyntax::new( | ||
"1.2.840.10008.1.2.1.98", | ||
"Encapsulated Uncompressed Explicit VR Little Endian", | ||
Endianness::Little, | ||
true, | ||
Codec::Encapsulated(Some(EncapsulatedAdapter), Some(EncapsulatedAdapter), | ||
); | ||
|
||
|
||
impl PixelDataReader for EncapsulatedAdapter { | ||
fn decode_frame( | ||
&self, | ||
src: &dyn PixelDataObject, | ||
frame: u32, | ||
dst: &mut Vec<u8>, | ||
) -> DecodeResult<()>{ | ||
let cols = src | ||
.cols() | ||
.context(decode_error::MissingAttributeSnafu { name: "Columns" })?; | ||
let rows = src | ||
.rows() | ||
.context(decode_error::MissingAttributeSnafu { name: "Rows" })?; | ||
let samples_per_pixel = | ||
src.samples_per_pixel() | ||
.context(decode_error::MissingAttributeSnafu { | ||
name: "SamplesPerPixel", | ||
})?; | ||
let bits_allocated = src | ||
.bits_allocated() | ||
.context(decode_error::MissingAttributeSnafu { | ||
name: "BitsAllocated", | ||
})?; | ||
|
||
if bits_allocated != 8 && bits_allocated != 16 { | ||
whatever!("BitsAllocated other than 8 or 16 is not supported"); | ||
} | ||
// Encapsulated Uncompressed has each frame encoded in one fragment | ||
// So we can assume 1 frag = 1 frame | ||
// ref. PS3.5 A.4.11 | ||
let nr_frames = | ||
src.number_of_fragments() | ||
.whatever_context("Invalid pixel data, no fragments found")? as usize; | ||
ensure!( | ||
nr_frames > frame as usize, | ||
decode_error::FrameRangeOutOfBoundsSnafu | ||
); | ||
|
||
let bytes_per_sample = (bits_allocated / 8) as usize; | ||
let samples_per_pixel = samples_per_pixel as usize; | ||
let decoded_pixel_data = match &src.fragment(frame as usize).value(); | ||
dst | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,35 @@ | ||
//! Implementation of Deflated Explicit VR Little Endian. | ||
use std::io::{Read, Write}; | ||
|
||
use byteordered::Endianness; | ||
use dicom_encoding::{Codec, TransferSyntax, transfer_syntax::DataRWAdapter}; | ||
use dicom_encoding::transfer_syntax::DataRWAdapter; | ||
use flate2; | ||
use flate2::Compression; | ||
|
||
/// Immaterial type representing an adapter for deflated data. | ||
#[derive(Debug)] | ||
pub struct FlateAdapter; | ||
|
||
/// **Fully implemented**: Deflated Explicit VR Little Endian | ||
pub const DEFLATED_EXPLICIT_VR_LITTLE_ENDIAN: TransferSyntax<FlateAdapter> = TransferSyntax::new( | ||
"1.2.840.10008.1.2.1.99", | ||
"Deflated Explicit VR Little Endian", | ||
Endianness::Little, | ||
true, | ||
Codec::Dataset(FlateAdapter), | ||
); | ||
|
||
impl<R: 'static, W: 'static> DataRWAdapter<R, W> for FlateAdapter | ||
where | ||
R: Read, | ||
W: Write, | ||
{ | ||
type Reader = flate2::read::DeflateDecoder<R>; | ||
type Writer = flate2::write::DeflateEncoder<W>; | ||
// type Reader = Box<flate2::read::DeflateDecoder<R>>; | ||
// type Writer = Box<flate2::write::DeflateEncoder<W>>; | ||
type Reader = Box<dyn Read>; | ||
type Writer = Box<dyn Write>; | ||
|
||
fn adapt_reader(&self, reader: R) -> Self::Reader | ||
where | ||
R: Read, | ||
{ | ||
flate2::read::DeflateDecoder::new(reader) | ||
Box::new(flate2::read::DeflateDecoder::new(reader)) | ||
} | ||
|
||
fn adapt_writer(&self, writer: W) -> Self::Writer | ||
where | ||
W: Write, | ||
{ | ||
flate2::write::DeflateEncoder::new(writer, Compression::fast()) | ||
Box::new(flate2::write::DeflateEncoder::new(writer, Compression::fast())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
use std::{io::BufReader, fs::File}; | ||
|
||
// use dicom_object::OpenFileOptions; | ||
// use dicom_pixeldata::PixelDecoder; | ||
|
||
// #[test] | ||
// fn test_read_data_with_preamble() { | ||
// let path = dicom_test_files::path("pydicom/image_dfl.dcm").expect("test DICOM file should exist"); | ||
// let source = BufReader::new(File::open(path).unwrap()); | ||
|
||
// // should read preamble even though it's from a reader | ||
// let object = OpenFileOptions::new() | ||
// .from_reader(source) | ||
// .expect("Should read from source successfully"); | ||
|
||
// let res = object.decode_pixel_data(); | ||
// println!("{:?}", res); | ||
|
||
// } |