Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First crack at adding dataset read adapter in DS read for deflate #438

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions object/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub type Result<T, E = ReadError> = std::result::Result<T, E>;
/// preamble: file meta group, followed by the rest of the data set.
pub fn from_reader<F>(file: F) -> Result<DefaultDicomObject>
where
F: Read,
F: Read + 'static,
{
OpenFileOptions::new().from_reader(file)
}
Expand Down Expand Up @@ -141,7 +141,7 @@ impl<D, T> OpenFileOptions<D, T> {
/// This method assumes
/// the standard file encoding structure without the preamble:
/// file meta group, followed by the rest of the data set.
pub fn from_reader<R>(self, from: R) -> Result<DefaultDicomObject<D>>
pub fn from_reader<'s: 'static, R: 's>(self, from: R) -> Result<DefaultDicomObject<D>>
where
R: Read,
D: DataDictionary,
Expand Down
80 changes: 60 additions & 20 deletions object/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
pub type DefaultDicomObject<D = StandardDataDictionary> = FileDicomObject<mem::InMemDicomObject<D>>;

use dicom_core::header::Header;
use dicom_encoding::Codec;
use dicom_encoding::adapters::{PixelDataObject, RawPixelData};
use dicom_encoding::transfer_syntax::TransferSyntaxIndex;
use dicom_parser::dataset::{DataSetWriter, IntoTokens};
Expand Down Expand Up @@ -424,21 +425,34 @@
.with_context(|| WriteUnsupportedTransferSyntaxSnafu {
uid: self.meta.transfer_syntax.clone(),
})?;
let mut dset_writer = DataSetWriter::with_ts(to, ts).context(CreatePrinterSnafu)?;
if let Codec::Dataset(Some(adapter))= ts.codec() {
let adapter = adapter.adapt_writer(Box::new(to));
let mut dset_writer = DataSetWriter::with_ts(adapter, ts).context(CreatePrinterSnafu)?;

// We use the default options, because only the inner object knows if something needs to change
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;
// write object
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;

Ok(())
Ok(())

} else {
let mut dset_writer = DataSetWriter::with_ts_cs(to, ts, cs).context(CreatePrinterSnafu)?;

Check failure on line 440 in object/src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (Windows)

cannot find value `cs` in this scope

Check failure on line 440 in object/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (macOS)

cannot find value `cs` in this scope

// write object
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;

Ok(())
}
}

/// Write the entire object as a DICOM file
/// into the given writer.
/// Preamble, magic code, and file meta group will be included
/// before the inner object.
pub fn write_all<W: Write>(&self, to: W) -> Result<(), WriteError> {
pub fn write_all<W: Write + 'static>(&self, to: W) -> Result<(), WriteError> {
let mut to = BufWriter::new(to);

// write preamble
Expand All @@ -456,14 +470,27 @@
.with_context(|| WriteUnsupportedTransferSyntaxSnafu {
uid: self.meta.transfer_syntax.clone(),
})?;
let mut dset_writer = DataSetWriter::with_ts(to, ts).context(CreatePrinterSnafu)?;
if let Codec::Dataset(Some(adapter))= ts.codec() {
let adapter = adapter.adapt_writer(Box::new(to));
let mut dset_writer = DataSetWriter::with_ts(adapter, ts).context(CreatePrinterSnafu)?;

// write object
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;

Ok(())

// We use the default options, because only the inner object knows if something needs to change
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;
} else {
let mut dset_writer = DataSetWriter::with_ts(to, ts).context(CreatePrinterSnafu)?;

Ok(())
// write object
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;

Ok(())
}
}

/// Write the file meta group set into the given writer.
Expand All @@ -477,7 +504,7 @@
/// without preamble, magic code, nor file meta group.
///
/// The transfer syntax is selected from the file meta table.
pub fn write_dataset<W: Write>(&self, to: W) -> Result<(), WriteError> {
pub fn write_dataset<W: Write + 'static>(&self, to: W) -> Result<(), WriteError> {
let to = BufWriter::new(to);

// prepare encoder
Expand All @@ -486,14 +513,27 @@
.with_context(|| WriteUnsupportedTransferSyntaxSnafu {
uid: self.meta.transfer_syntax.clone(),
})?;
let mut dset_writer = DataSetWriter::with_ts(to, ts).context(CreatePrinterSnafu)?;
if let Codec::Dataset(Some(adapter))= ts.codec() {
let adapter = adapter.adapt_writer(Box::new(to));
let mut dset_writer = DataSetWriter::with_ts(adapter, ts).context(CreatePrinterSnafu)?;

// write object
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;

// write object
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;
Ok(())

Ok(())
} else {
let mut dset_writer = DataSetWriter::with_ts_cs(to, ts, cs).context(CreatePrinterSnafu)?;

Check failure on line 528 in object/src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (Windows)

cannot find value `cs` in this scope

Check failure on line 528 in object/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check (macOS)

cannot find value `cs` in this scope

// write object
dset_writer
.write_sequence((&self.obj).into_tokens())
.context(PrintDataSetSnafu)?;

Ok(())
}
}
}

Expand Down
Loading
Loading